[Python-checkins] cpython: Issue #15528: Delay importing atexit until weakref.finalize() used.

richard.oudkerk python-checkins at python.org
Sat Jun 8 17:53:59 CEST 2013


http://hg.python.org/cpython/rev/d6ad9d7468f7
changeset:   84057:d6ad9d7468f7
user:        Richard Oudkerk <shibturn at gmail.com>
date:        Sat Jun 08 16:52:29 2013 +0100
summary:
  Issue #15528: Delay importing atexit until weakref.finalize() used.

files:
  Lib/weakref.py |  10 +++++++---
  1 files changed, 7 insertions(+), 3 deletions(-)


diff --git a/Lib/weakref.py b/Lib/weakref.py
--- a/Lib/weakref.py
+++ b/Lib/weakref.py
@@ -23,7 +23,6 @@
 import collections  # Import after _weakref to avoid circular import.
 import sys
 import itertools
-import atexit
 
 ProxyTypes = (ProxyType, CallableProxyType)
 
@@ -464,11 +463,18 @@
     _shutdown = False
     _index_iter = itertools.count()
     _dirty = False
+    _registered_with_atexit = False
 
     class _Info:
         __slots__ = ("weakref", "func", "args", "kwargs", "atexit", "index")
 
     def __init__(self, obj, func, *args, **kwargs):
+        if not self._registered_with_atexit:
+            # We may register the exit function more than once because
+            # of a thread race, but that is harmless
+            import atexit
+            atexit.register(self._exitfunc)
+            finalize._registered_with_atexit = True
         info = self._Info()
         info.weakref = ref(obj, self)
         info.func = func
@@ -569,5 +575,3 @@
             finalize._shutdown = True
             if reenable_gc:
                 gc.enable()
-
-atexit.register(finalize._exitfunc)

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list