How to block calls to print?

Posted on

Question :

How to block calls to print?

Is there a way to stop a function from calling print?

I am using the pygame.joystick module for a game I am working on.

I created a pygame.joystick.Joystick object and in the actual loop of the game call its member function get_button to check for user input. The function does everything I need it to do, but the problem is that it also calls print, which slows down the game considerably.

Can I block this call to print?

Asked By: dmlicht


Answer #1:

Python lets you overwrite standard output (stdout) with any file object. This should work cross platform and write to the null device.

import sys, os

# Disable
def blockPrint():
    sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
    sys.stdout = sys.__stdout__

print 'This will print'

print "This won't"

print "This will too"

If you don’t want that one function to print, call blockPrint() before it, and enablePrint() when you want it to continue. If you want to disable all printing, start blocking at the top of the file.

Answered By: Brigand

Answer #2:

Use with

Based on @FakeRainBrigand solution I’m suggesting a safer solution:

import os, sys

class HiddenPrints:
    def __enter__(self):
        self._original_stdout = sys.stdout
        sys.stdout = open(os.devnull, 'w')

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout = self._original_stdout

Then you can use it like this:

with HiddenPrints():
    print("This will not be printed")

print("This will be printed as before")

This is much safer because you can not forget to re-enable stdout, which is especially critical when handling exceptions.

Without with — Bad practice

The following example uses enable/disable prints functions that were suggested in previous answer.

Imagine that there is a code that may raise an exception. We had to use finally statement in order to enable prints in any case.

    enable_prints() # This will not help in case of exception
except ValueError as err:
    enable_prints() # That's where it needs to go.

If you forgot the finally clause, none of your print calls would print anything anymore.

It is safer to use the with statement, which makes sure that prints will be reenabled.

Note: It is not safe to use sys.stdout = None, because someone could call methods like sys.stdout.write()

Answered By: Alexander Chzhen

Answer #3:

As @Alexander Chzhen suggested, using a context manager would be safer than calling a pair of state-changing functions.

However, you don’t need to reimplement the context manager – it’s already in the standard library. You can redirect stdout (the file object that print uses) with contextlib.redirect_stdout, and also stderr with contextlib.redirect_stderr.

import os
import contextlib

with open(os.devnull, "w") as f, contextlib.redirect_stdout(f):
    print("This won't be printed.")
Answered By: ForgottenUmbrella

Answer #4:

If you want to block print calls made by a particular function, there is a neater solution using decorators. Define the following decorator:

# decorater used to block function printing to the console
def blockPrinting(func):
    def func_wrapper(*args, **kwargs):
        # block all printing to the console
        sys.stdout = open(os.devnull, 'w')
        # call the method in question
        value = func(*args, **kwargs)
        # enable all printing to the console
        sys.stdout = sys.__stdout__
        # pass the return value of the method back
        return value

    return func_wrapper

Then just place @blockPrinting before any function. For example:

# This will print
def helloWorld():
    print("Hello World!")

# This will not print
def helloWorld2():
    print("Hello World!")
Answered By: Fowler

Answer #5:

No, there is not, especially that majority of PyGame is written in C.

But if this function calls print, then it’s PyGame bug, and you should just report it.

Answered By: Cat Plus Plus

Answer #6:

I have had the same problem, and I did not come to another solution but to redirect the output of the program (I don’t know exactly whether the spamming happens on stdout or stderr) to /dev/null nirvana.

Indeed, it’s open source, but I wasn’t passionate enough to dive into the pygame sources – and the build process – to somehow stop the debug spam.


The pygame.joystick module has calls to printf in all functions that return the actual values to Python:

printf("SDL_JoystickGetButton value:%d:n", value);

Unfortunately you would need to comment these out and recompile the whole thing. Maybe the provided would make this easier than I thought. You could try this…

Answered By: moooeeeep

Answer #7:

A completely different approach would be redirecting at the command line. If you’re on Windows, this means a batch script. On Linux, bash.

/full/path/to/my/game/ > /dev/null
C:FullPathToMyGame.exe > nul

Unless you’re dealing with multiple processes, this should work. For Windows users this could be the shortcuts you’re creating (start menu / desktop).

Answered By: Brigand

Answer #8:

The module I used printed to stderr. So the solution in that case would be:

sys.stdout = open(os.devnull, 'w')
Answered By: David Schumann

Leave a Reply

Your email address will not be published. Required fields are marked *