Skip to content

Commit b64334c

Browse files
mariocj89miss-islington
authored andcommitted
bpo-36820: Break unnecessary cycle in socket.py, codeop.py and dyld.py (GH-13135)
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
1 parent efefe25 commit b64334c

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

Lib/codeop.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ def _maybe_compile(compiler, source, filename, symbol):
9393
except SyntaxError as e:
9494
err2 = e
9595

96-
if code:
97-
return code
98-
if not code1 and repr(err1) == repr(err2):
99-
raise err1
96+
try:
97+
if code:
98+
return code
99+
if not code1 and repr(err1) == repr(err2):
100+
raise err1
101+
finally:
102+
err1 = err2 = None
100103

101104
def _compile(source, filename, symbol):
102105
return compile(source, filename, symbol, PyCF_DONT_IMPLY_DEDENT)

Lib/ctypes/macholib/dyld.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ def framework_find(fn, executable_path=None, env=None):
149149
return dyld_find(fn, executable_path=executable_path, env=env)
150150
except ValueError:
151151
raise error
152+
finally:
153+
error = None
152154

153155
def test_dyld_find():
154156
env = {}

Lib/socket.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,11 @@ def create_connection(address, timeout=_GLOBAL_DEFAULT_TIMEOUT,
839839
sock.close()
840840

841841
if err is not None:
842-
raise err
842+
try:
843+
raise err
844+
finally:
845+
# Break explicitly a reference cycle
846+
err = None
843847
else:
844848
raise error("getaddrinfo returns an empty list")
845849

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Break cycle generated when saving an exception in socket.py, codeop.py and
2+
dyld.py as they keep alive not only the exception but user objects through
3+
the ``__traceback__`` attribute. Patch by Mario Corchero.

0 commit comments

Comments
 (0)