Index: Lib/logging/__init__.py =================================================================== --- Lib/logging/__init__.py (revision 76505) +++ Lib/logging/__init__.py (working copy) @@ -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', @@ -593,6 +594,15 @@ _handlers = {} #map of handler names to handlers _handlerList = [] # added to allow handlers to be removed in reverse of order initialized +def _removeWeakref(wr): + if wr in _handlerList: + #get the module data lock, as we're updating a shared structure. + _acquireLock() + try: #unlikely to raise an exception, but you never know... + _handlerList.remove(wr) + finally: + _releaseLock() + class Handler(Filterer): """ Handler instances dispatch logging events to specific destinations. @@ -614,7 +624,7 @@ #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, weakref.ref(self, _removeWeakref)) finally: _releaseLock() self.createLock() @@ -724,7 +734,7 @@ """ Tidy up any resources used by the handler. - This version does removes the handler from an internal list + This version does remove the handler from an internal list of handlers which is closed when shutdown() is called. Subclasses should ensure that this gets called from overridden close() methods. @@ -734,7 +744,6 @@ 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) finally: _releaseLock() @@ -1532,10 +1541,11 @@ Should be called at application exit. """ - for h in handlerList[:]: + for wr in handlerList[:]: #errors might occur, for example, if files are locked #we just ignore them if raiseExceptions is not set try: + h = wr() h.flush() h.close() except: