diff options
author | Richard Oudkerk <shibturn@gmail.com> | 2013-06-08 15:52:29 (GMT) |
---|---|---|
committer | Richard Oudkerk <shibturn@gmail.com> | 2013-06-08 15:52:29 (GMT) |
commit | a81dd6594067a9501f3ba65b7c05dd50accac5de (patch) | |
tree | eac9f02115ca2dacfb1800583f0a206fadbbfb48 /Lib/weakref.py | |
parent | 99c5afcb60ff143a60c448c49fe8e7afc679e09d (diff) | |
download | cpython-a81dd6594067a9501f3ba65b7c05dd50accac5de.zip cpython-a81dd6594067a9501f3ba65b7c05dd50accac5de.tar.gz cpython-a81dd6594067a9501f3ba65b7c05dd50accac5de.tar.bz2 |
Issue #15528: Delay importing atexit until weakref.finalize() used.
Diffstat (limited to 'Lib/weakref.py')
-rw-r--r-- | Lib/weakref.py | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/Lib/weakref.py b/Lib/weakref.py index 4c0b26e..f6a40ca 100644 --- a/Lib/weakref.py +++ b/Lib/weakref.py @@ -23,7 +23,6 @@ from _weakrefset import WeakSet, _IterationGuard import collections # Import after _weakref to avoid circular import. import sys import itertools -import atexit ProxyTypes = (ProxyType, CallableProxyType) @@ -464,11 +463,18 @@ class finalize: _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 @@ class finalize: finalize._shutdown = True if reenable_gc: gc.enable() - -atexit.register(finalize._exitfunc) |