[Python-checkins] cpython: Remove Lib/site.py hack to unregister patched builtins.

antoine.pitrou python-checkins at python.org
Thu Aug 1 19:46:39 CEST 2013


http://hg.python.org/cpython/rev/4ffa5f9e4ac2
changeset:   84951:4ffa5f9e4ac2
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Thu Aug 01 19:46:04 2013 +0200
summary:
  Remove Lib/site.py hack to unregister patched builtins.
It creates a refleak in subinterpreters, as atexit callbacks aren't triggered at their end.

files:
  Lib/site.py |  39 ++++++++++-----------------------------
  1 files changed, 10 insertions(+), 29 deletions(-)


diff --git a/Lib/site.py b/Lib/site.py
--- a/Lib/site.py
+++ b/Lib/site.py
@@ -68,7 +68,6 @@
 ImportError exception, it is silently ignored.
 """
 
-import atexit
 import sys
 import os
 import re
@@ -87,25 +86,6 @@
 USER_BASE = None
 
 
-_no_builtin = object()
-
-def _patch_builtins(**items):
-    # When patching builtins, we make some objects almost immortal
-    # (builtins are only reclaimed at the very end of the interpreter
-    #  shutdown sequence).  To avoid keeping to many references alive,
-    # we register callbacks to undo our builtins additions.
-    old_items = {k: getattr(builtins, k, _no_builtin) for k in items}
-    def unpatch(old_items=old_items):
-        for k, v in old_items.items():
-            if v is _no_builtin:
-                delattr(builtins, k)
-            else:
-                setattr(builtins, k, v)
-    for k, v in items.items():
-        setattr(builtins, k, v)
-    atexit.register(unpatch)
-
-
 def makepath(*paths):
     dir = os.path.join(*paths)
     try:
@@ -377,7 +357,8 @@
             except:
                 pass
             raise SystemExit(code)
-    _patch_builtins(quit=Quitter('quit'), exit=Quitter('exit'))
+    builtins.quit = Quitter('quit')
+    builtins.exit = Quitter('exit')
 
 
 class _Printer(object):
@@ -442,20 +423,20 @@
 
 def setcopyright():
     """Set 'copyright' and 'credits' in builtins"""
-    _patch_builtins(copyright=_Printer("copyright", sys.copyright))
+    builtins.copyright = _Printer("copyright", sys.copyright)
     if sys.platform[:4] == 'java':
-        _patch_builtins(credits=_Printer(
+        builtins.credits = _Printer(
             "credits",
-            "Jython is maintained by the Jython developers (www.jython.org)."))
+            "Jython is maintained by the Jython developers (www.jython.org).")
     else:
-        _patch_builtins(credits=_Printer("credits", """\
+        builtins.credits = _Printer("credits", """\
     Thanks to CWI, CNRI, BeOpen.com, Zope Corporation and a cast of thousands
-    for supporting Python development.  See www.python.org for more information."""))
+    for supporting Python development.  See www.python.org for more information.""")
     here = os.path.dirname(os.__file__)
-    _patch_builtins(license=_Printer(
+    builtins.license = _Printer(
         "license", "See http://www.python.org/%.3s/license.html" % sys.version,
         ["LICENSE.txt", "LICENSE"],
-        [os.path.join(here, os.pardir), here, os.curdir]))
+        [os.path.join(here, os.pardir), here, os.curdir])
 
 
 class _Helper(object):
@@ -472,7 +453,7 @@
         return pydoc.help(*args, **kwds)
 
 def sethelper():
-    _patch_builtins(help=_Helper())
+    builtins.help = _Helper()
 
 def enablerlcompleter():
     """Enable default readline configuration on interactive prompts, by

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


More information about the Python-checkins mailing list