Remove binding of captured exceptions when not used to reduce the chances of creating cycles (GH-17246)
Capturing exceptions into names can lead to reference cycles though the __traceback__ attribute of the exceptions in some obscure cases that have been reported previously and fixed individually. As these variables are not used anyway, we can remove the binding to reduce the chances of creating reference cycles.
See for example GH-13135
diff --git a/Lib/test/pythoninfo.py b/Lib/test/pythoninfo.py
index 797b3af..eab82c3 100644
--- a/Lib/test/pythoninfo.py
+++ b/Lib/test/pythoninfo.py
@@ -754,7 +754,7 @@
):
try:
collect_func(info_add)
- except Exception as exc:
+ except Exception:
error = True
print("ERROR: %s() failed" % (collect_func.__name__),
file=sys.stderr)
diff --git a/Lib/test/test_cgitb.py b/Lib/test/test_cgitb.py
index e299ec3..8991bc1 100644
--- a/Lib/test/test_cgitb.py
+++ b/Lib/test/test_cgitb.py
@@ -31,7 +31,7 @@
def test_text(self):
try:
raise ValueError("Hello World")
- except ValueError as err:
+ except ValueError:
text = cgitb.text(sys.exc_info())
self.assertIn("ValueError", text)
self.assertIn("Hello World", text)
diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py
index 456f1be..7524f58 100644
--- a/Lib/test/test_class.py
+++ b/Lib/test/test_class.py
@@ -529,7 +529,7 @@
# In debug mode, printed XXX undetected error and
# raises AttributeError
I()
- except AttributeError as x:
+ except AttributeError:
pass
else:
self.fail("attribute error for I.__init__ got masked")
diff --git a/Lib/test/test_codecs.py b/Lib/test/test_codecs.py
index e1638c1..3aec34c 100644
--- a/Lib/test/test_codecs.py
+++ b/Lib/test/test_codecs.py
@@ -11,7 +11,7 @@
try:
import _testcapi
-except ImportError as exc:
+except ImportError:
_testcapi = None
try:
diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py
index 1f37b53..fe0cfc7 100644
--- a/Lib/test/test_decimal.py
+++ b/Lib/test/test_decimal.py
@@ -5600,13 +5600,13 @@
args, kwds = mkargs(C, c_sig)
try:
getattr(c_type(9), attr)(*args, **kwds)
- except Exception as err:
+ except Exception:
raise TestFailed("invalid signature for %s: %s %s" % (c_func, args, kwds))
args, kwds = mkargs(P, p_sig)
try:
getattr(p_type(9), attr)(*args, **kwds)
- except Exception as err:
+ except Exception:
raise TestFailed("invalid signature for %s: %s %s" % (p_func, args, kwds))
doit('Decimal')
diff --git a/Lib/test/test_ftplib.py b/Lib/test/test_ftplib.py
index b0e4641..b8eef82 100644
--- a/Lib/test/test_ftplib.py
+++ b/Lib/test/test_ftplib.py
@@ -346,7 +346,7 @@
if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
ssl.SSL_ERROR_WANT_WRITE):
return
- except OSError as err:
+ except OSError:
# Any "socket error" corresponds to a SSL_ERROR_SYSCALL return
# from OpenSSL's SSL_shutdown(), corresponding to a
# closed socket condition. See also:
diff --git a/Lib/test/test_sys_settrace.py b/Lib/test/test_sys_settrace.py
index fdd7894..d4e1ac2 100644
--- a/Lib/test/test_sys_settrace.py
+++ b/Lib/test/test_sys_settrace.py
@@ -161,8 +161,8 @@
def test_raise():
try:
raises()
- except Exception as exc:
- x = 1
+ except Exception:
+ pass
test_raise.events = [(0, 'call'),
(1, 'line'),
@@ -191,7 +191,7 @@
def settrace_and_raise(tracefunc):
try:
_settrace_and_raise(tracefunc)
- except RuntimeError as exc:
+ except RuntimeError:
pass
settrace_and_raise.events = [(2, 'exception'),
diff --git a/Lib/test/test_time.py b/Lib/test/test_time.py
index 8d8d31e..80e43fa 100644
--- a/Lib/test/test_time.py
+++ b/Lib/test/test_time.py
@@ -825,7 +825,7 @@
try:
result = pytime_converter(value, time_rnd)
expected = expected_func(value)
- except Exception as exc:
+ except Exception:
self.fail("Error on timestamp conversion: %s" % debug_info)
self.assertEqual(result,
expected,
diff --git a/Lib/test/test_traceback.py b/Lib/test/test_traceback.py
index 72dc7af..7135d99 100644
--- a/Lib/test/test_traceback.py
+++ b/Lib/test/test_traceback.py
@@ -313,7 +313,7 @@
with captured_output("stderr") as stderr_f:
try:
f()
- except RecursionError as exc:
+ except RecursionError:
render_exc()
else:
self.fail("no recursion occurred")
@@ -360,7 +360,7 @@
with captured_output("stderr") as stderr_g:
try:
g()
- except ValueError as exc:
+ except ValueError:
render_exc()
else:
self.fail("no value error was raised")
@@ -396,7 +396,7 @@
with captured_output("stderr") as stderr_h:
try:
h()
- except ValueError as exc:
+ except ValueError:
render_exc()
else:
self.fail("no value error was raised")
@@ -424,7 +424,7 @@
with captured_output("stderr") as stderr_g:
try:
g(traceback._RECURSIVE_CUTOFF)
- except ValueError as exc:
+ except ValueError:
render_exc()
else:
self.fail("no error raised")
@@ -452,7 +452,7 @@
with captured_output("stderr") as stderr_g:
try:
g(traceback._RECURSIVE_CUTOFF + 1)
- except ValueError as exc:
+ except ValueError:
render_exc()
else:
self.fail("no error raised")
diff --git a/Lib/test/test_urllib2net.py b/Lib/test/test_urllib2net.py
index 040a2ce..bb0500e 100644
--- a/Lib/test/test_urllib2net.py
+++ b/Lib/test/test_urllib2net.py
@@ -199,7 +199,7 @@
try:
with urllib.request.urlopen(URL) as res:
pass
- except ValueError as e:
+ except ValueError:
self.fail("urlopen failed for site not sending \
Connection:close")
else:
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index ddf7e6d..b76c60e 100644
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -471,7 +471,7 @@
# the value from too_large_getter above.
try:
self.uuid.uuid1(node=node)
- except ValueError as e:
+ except ValueError:
self.fail('uuid1 was given an invalid node ID')
def test_uuid1(self):