# Repro script for http://bugs.python.org/issue6333 # You'll need py.test to run this script: # # $ py.test issue6333repro.py # ========================================================= test session starts ========================================================== # python: platform linux2 -- Python 2.6.2 # test object 1: /home/sridharr/as/pypm-txtui/issue6333repro.py # issue6333repro.py . # ======================================================= 1 passed in 0.05 seconds ======================================================= # Error in atexit._run_exitfuncs: # Traceback (most recent call last): # File "/opt/ActivePython-2.6/lib/python2.6/atexit.py", line 24, in _run_exitfuncs # func(*targs, **kargs) # File "/opt/ActivePython-2.6/lib/python2.6/logging/__init__.py", line 1486, in shutdown # h.flush() # File "/opt/ActivePython-2.6/lib/python2.6/logging/__init__.py", line 746, in flush # self.stream.flush() # ValueError: I/O operation on closed file # Error in sys.exitfunc: # Traceback (most recent call last): # File "/opt/ActivePython-2.6/lib/python2.6/atexit.py", line 24, in _run_exitfuncs # func(*targs, **kargs) # File "/opt/ActivePython-2.6/lib/python2.6/logging/__init__.py", line 1486, in shutdown # h.flush() # File "/opt/ActivePython-2.6/lib/python2.6/logging/__init__.py", line 746, in flush # self.stream.flush() # ValueError: I/O operation on closed file # (pypm:^/branches/txtui:r427*@double) ~/as/pypm-txtui # $ import logging import sys __author__ = 'Sridhar Ratnakumar ' LOG = logging.getLogger(__name__) def attachToConsole(): h = ConsoleHandler() LOG.addHandler(h) def doSomething(): LOG.info('I am info') LOG.debug('I am debug') LOG.warn('I am warn') LOG.error('oopsie') def test_something(): attachToConsole() doSomething() # -- internal class ConsoleHandler(logging.StreamHandler): """A handler that logs to console in the sensible way. StreamHandler can log to *one of* sys.stdout or sys.stderr. It is more sensible to log to sys.stdout by default with only error (logging.ERROR and above) messages going to sys.stderr. This is how ConsoleHandler behaves. """ def __init__(self): logging.StreamHandler.__init__(self) self.stream = None # reset it; we are not going to use it anyway def emit(self, record): if record.levelno >= logging.ERROR: self.__emit(record, sys.stderr) else: self.__emit(record, sys.stdout) def __emit(self, record, strm): self.stream = strm logging.StreamHandler.emit(self, record) # def flush(self): # # Workaround a bug in logging module # # See: # # http://bugs.python.org/issue6333 # if self.stream and hasattr(self.stream, 'flush') and not self.stream.closed: # logging.StreamHandler.flush(self)