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', @@ -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() @@ -724,7 +726,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 +736,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() @@ -759,6 +760,16 @@ finally: del ei + def __del__(self): + """Delete weak reference.""" + #get the module data lock, as we're updating a shared structure. + _acquireLock() + try: + if self._weakref in _handlerList: + _handlerList.remove(self._weakref) + finally: + _releaseLock() + class StreamHandler(Handler): """ A handler class which writes logging records, appropriately formatted, @@ -1532,7 +1543,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: