views:

380

answers:

2

I'm using Pygame/SDL's joystick module to get input from a gamepad. Every time I call its get_hat() method it prints to the console. This is problematic since I use the console to help me debug and now it gets flooded with SDL_JoystickGetHat value:0: 60 times every second. Is there a way I can disable this? Either through an option in Pygame/SDL or suppress console output while the function calls? I saw no mention of this in the Pygame documentation.

edit: This turns out to be due to debugging being turned on when the SDL library was compiled.

+1  A: 

You can get around this by assigning the standard out/error (I don't know which one it's going to) to the null device. In Python, the standard out/error files are sys.stdout/sys.stderr, and the null device is os.devnull, so you do

sys.stdout = os.devnull
sys.stderr = os.devnull

This should disable these error messages completely. Unfortunately, this will also disable all console output. To get around this, disable output right before calling the get_hat() the method, and then restore it by doing

sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__

which restores standard out and error to their original value.

ascent1729
I wasn't aware of this technique before. It appears to be what I want, but when I try using it the get_hat() function continues to print to the console. Could this be an issue with SDL?
jjackson
+1  A: 

Here's the relevant block of code from joystick.c (via SVN at http://svn.seul.org/viewcvs/viewvc.cgi/trunk/src/joystick.c?view=markup&revision=2652&root=PyGame)

    value = SDL_JoystickGetHat (joy, _index);
#ifdef DEBUG
    printf("SDL_JoystickGetHat value:%d:\n", value);
#endif
    if (value & SDL_HAT_UP) {

Looks like a problem with having debugging turned on.

fencepost
How would I disable SDL debugging though Python? Google tells me the environment variable is `SDL_DEBUG` but inserting `os.environ['SDL_DEBUG'] = '0'` appears to have no effect.
jjackson
@jackson that's a compile time debug option for SDL. The message is printing because when your SDL library was compiled, the DEBUG symbol was defined.
Geoff Reedy
Ah, okay. Thanks for the help!
jjackson