]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-36820: Break unnecessary cycle in socket.py, codeop.py and dyld.py (GH-13135)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 6 Dec 2019 14:59:49 +0000 (06:59 -0800)
committerGitHub <noreply@github.com>
Fri, 6 Dec 2019 14:59:49 +0000 (06:59 -0800)
Break cycle generated when saving an exception in socket.py, codeop.py and dyld.py as they keep alive not only the exception but user objects through the ``__traceback__`` attribute.

https://bugs.python.org/issue36820

Automerge-Triggered-By: @pablogsal
(cherry picked from commit b64334cb93d0ddbb551c8cd712942bab2fc72772)

Co-authored-by: Mario Corchero <mcorcherojim@bloomberg.net>
Lib/codeop.py
Lib/ctypes/macholib/dyld.py
Lib/socket.py
Misc/NEWS.d/next/Library/2019-05-06-15-34-17.bpo-36820.Eh5mIB.rst [new file with mode: 0644]

index fb759da42ad1a2b79a30f9d5df5f63b3014fa1a4..0fa677f609b74b94987c3fbcd79ee47dd7c8e612 100644 (file)
@@ -93,10 +93,13 @@ def _maybe_compile(compiler, source, filename, symbol):
     except SyntaxError as e:
         err2 = e
 
-    if code:
-        return code
-    if not code1 and repr(err1) == repr(err2):
-        raise err1
+    try:
+        if code:
+            return code
+        if not code1 and repr(err1) == repr(err2):
+            raise err1
+    finally:
+        err1 = err2 = None
 
 def _compile(source, filename, symbol):
     return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)
index c158e672f0511ab0f7b125937330be20c1a7c4ba..9d86b058765a3eb8a918a48db314f9558d8f1b2b 100644 (file)
@@ -149,6 +149,8 @@ def framework_find(fn, executable_path=None, env=None):
         return dyld_find(fn, executable_path=executable_path, env=env)
     except ValueError:
         raise error
+    finally:
+        error = None
 
 def test_dyld_find():
     env = {}
index 813f4ef5c3e1f579dd0d5f35e39a2eb6cf034595..5b17906ef479a12ebaddfa4696fc9b3adc464c1b 100644 (file)
@@ -804,7 +804,11 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
                 sock.close()
 
     if err is not None:
-        raise err
+        try:
+            raise err
+        finally:
+            # Break explicitly a reference cycle
+            err = None
     else:
         raise error("getaddrinfo returns an empty list")
 
diff --git a/Misc/NEWS.d/next/Library/2019-05-06-15-34-17.bpo-36820.Eh5mIB.rst b/Misc/NEWS.d/next/Library/2019-05-06-15-34-17.bpo-36820.Eh5mIB.rst
new file mode 100644 (file)
index 0000000..82f6635
--- /dev/null
@@ -0,0 +1,3 @@
+Break cycle generated when saving an exception in socket.py, codeop.py and
+dyld.py as they keep alive not only the exception but user objects through
+the ``__traceback__`` attribute. Patch by Mario Corchero.