Index: Lib/logging/__init__.py =================================================================== --- Lib/logging/__init__.py (révision 76501) +++ Lib/logging/__init__.py (copie de travail) @@ -24,6 +24,7 @@ """ import sys, os, time, cStringIO, traceback, warnings +import weakref __all__ = ['BASIC_FORMAT', 'BufferingFormatter', 'CRITICAL', 'DEBUG', 'ERROR', 'FATAL', 'FileHandler', 'Filter', 'Formatter', 'Handler', 'INFO', @@ -611,10 +612,11 @@ self._name = None self.level = _checkLevel(level) self.formatter = None + self_weakref = weakref.ref(self) #get the module data lock, as we're updating a shared structure. _acquireLock() try: #unlikely to raise an exception, but you never know... - _handlerList.insert(0, self) + _handlerList.insert(0, self._weakref) finally: _releaseLock() self.createLock() @@ -734,7 +736,7 @@ try: #unlikely to raise an exception, but you never know... if self._name and self._name in _handlers: del _handlers[self._name] - _handlerList.remove(self) + _handlerList.remove(self._weakref) finally: _releaseLock() @@ -759,6 +761,17 @@ finally: del ei + def __del__(self): + """Perform any cleanup actions in the handler.""" + #errors might occur, for example, if files are locked + #we just ignore them if raiseExceptions is not set + try: + self.flush() + self.close() + except: + if raiseExceptions: + raise + class StreamHandler(Handler): """ A handler class which writes logging records, appropriately formatted, @@ -1532,7 +1545,7 @@ Should be called at application exit. """ - for h in handlerList[:]: + for h in [wr() for wr in handlerList if wr() is not None]: #errors might occur, for example, if files are locked #we just ignore them if raiseExceptions is not set try: