bpo-36820: Break unnecessary cycle in socket.py, codeop.py and dyld.p… · python/cpython@b64334c

4 files changed

lines changed

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)

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 = {}

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
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.