BadFileDescriptor error and, consequently, memory access violation caused by the fact that stdout applications in windowed mode is a buffer of a fixed size. So, if you write to stdout , either using print or sys.stdout directly, after a while you will see these errors.
You can fix this:
- Removing / commenting on
stdout entries - Using
logging instead of printing in stdout - Redirecting
stdout at the start of your application. This is a solution that requires less code to modify, although I think that choosing logging debug statements would be a better choice.
To redirect stdout , you can use code like this:
import sys import tempfile sys.stdout = tempfile.TemporaryFile() sys.stderr = tempfile.TemporaryFile()
Before executing your program. You can also use some kind of custom object to put the output in "log" files or something else, the important thing is that the output should not fill a fixed size buffer.
For example, you can do something similar to be able to use the logging module without changing too much code:
import sys import logging debug_logger = logging.getLogger('debug') debug_logger.write = debug_logger.debug
The disadvantage of this approach is that print makes more stdout.write calls for each line:
>>> print 'test' DEBUG:debug:test DEBUG:debug:
If you want, you can probably avoid this behavior by creating a real write function that calls the_logger.debug with "full lines" only.
In any case, I think that such a solution should only be temporary and should only be used before transferring print to logging.debug calls.
(Obviously, registrars should write to a file, not to stdout , to avoid errors.)
source share