Mailing List Archive

[3.9] bpo-46411: Remove unnecessary calls to sys.exc_info() in tests (GH-30638) (GH-30658)
https://github.com/python/cpython/commit/1d6530dd0564a6bb75989b9fca25a649b5ddc1b0
commit: 1d6530dd0564a6bb75989b9fca25a649b5ddc1b0
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
committer: miss-islington <31488909+miss-islington@users.noreply.github.com>
date: 2022-01-17T23:29:02-08:00
summary:

[3.9] bpo-46411: Remove unnecessary calls to sys.exc_info() in tests (GH-30638) (GH-30658)



(cherry picked from commit a287b31bcb065e4122400cb59167340d25480e6d)


Co-authored-by: Irit Katriel <1055913+iritkatriel@users.noreply.github.com>

Automerge-Triggered-By: GH:iritkatriel

files:
M Lib/test/test_argparse.py
M Lib/test/test_builtin.py
M Lib/test/test_inspect.py
M Lib/test/test_logging.py
M Lib/test/test_raise.py
M Lib/test/test_zipimport.py

diff --git a/Lib/test/test_argparse.py b/Lib/test/test_argparse.py
index 043a9cf93f771..cc5e8491b4c49 100644
--- a/Lib/test/test_argparse.py
+++ b/Lib/test/test_argparse.py
@@ -101,8 +101,8 @@ def stderr_to_parser_error(parse_args, *args, **kwargs):
if getattr(result, key) is sys.stderr:
setattr(result, key, old_stderr)
return result
- except SystemExit:
- code = sys.exc_info()[1].code
+ except SystemExit as e:
+ code = e.code
stdout = sys.stdout.getvalue()
stderr = sys.stderr.getvalue()
raise ArgumentParserError(
@@ -1828,8 +1828,7 @@ def __call__(self, parser, namespace, value, option_string=None):
raise AssertionError('value: %s' % value)
assert expected_ns == namespace, ('expected %s, got %s' %
(expected_ns, namespace))
- except AssertionError:
- e = sys.exc_info()[1]
+ except AssertionError as e:
raise ArgumentParserError('opt_action failed: %s' % e)
setattr(namespace, 'spam', value)

@@ -1854,8 +1853,7 @@ def __call__(self, parser, namespace, value, option_string=None):
raise AssertionError('value: %s' % value)
assert expected_ns == namespace, ('expected %s, got %s' %
(expected_ns, namespace))
- except AssertionError:
- e = sys.exc_info()[1]
+ except AssertionError as e:
raise ArgumentParserError('arg_action failed: %s' % e)
setattr(namespace, 'badger', value)

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 1f224bfe1ba99..9927b7ec3728c 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -581,8 +581,8 @@ def __dir__(self):
# dir(traceback)
try:
raise IndexError
- except:
- self.assertEqual(len(dir(sys.exc_info()[2])), 4)
+ except IndexError as e:
+ self.assertEqual(len(dir(e.__traceback__)), 4)

# test that object has a __dir__()
self.assertEqual(sorted([].__dir__()), dir([]))
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 08aed248b9665..2a6de956cdc6d 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -127,8 +127,8 @@ def test_excluding_predicates(self):
self.istest(inspect.iscode, 'mod.spam.__code__')
try:
1/0
- except:
- tb = sys.exc_info()[2]
+ except Exception as e:
+ tb = e.__traceback__
self.istest(inspect.isframe, 'tb.tb_frame')
self.istest(inspect.istraceback, 'tb')
if hasattr(types, 'GetSetDescriptorType'):
diff --git a/Lib/test/test_logging.py b/Lib/test/test_logging.py
index 363ea29d3af2f..0a9d449078f10 100644
--- a/Lib/test/test_logging.py
+++ b/Lib/test/test_logging.py
@@ -5374,8 +5374,8 @@ def test_compute_rollover(self, when=when, exp=exp):
print('currentSecond: %s' % currentSecond, file=sys.stderr)
print('r: %s' % r, file=sys.stderr)
print('result: %s' % result, file=sys.stderr)
- except Exception:
- print('exception in diagnostic code: %s' % sys.exc_info()[1], file=sys.stderr)
+ except Exception as e:
+ print('exception in diagnostic code: %s' % e, file=sys.stderr)
self.assertEqual(exp, actual)
rh.close()
setattr(TimedRotatingFileHandlerTest, "test_compute_rollover_%s" % when, test_compute_rollover)
diff --git a/Lib/test/test_raise.py b/Lib/test/test_raise.py
index 8dc62a933e872..8225504c4756d 100644
--- a/Lib/test/test_raise.py
+++ b/Lib/test/test_raise.py
@@ -12,8 +12,8 @@
def get_tb():
try:
raise OSError()
- except:
- return sys.exc_info()[2]
+ except OSError as e:
+ return e.__traceback__


class Context:
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index 2e2438863da28..b7347a384f1ab 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -628,8 +628,8 @@ def testDoctestSuite(self):
def doTraceback(self, module):
try:
module.do_raise()
- except:
- tb = sys.exc_info()[2].tb_next
+ except Exception as e:
+ tb = e.__traceback__.tb_next

f,lno,n,line = extract_tb(tb, 1)[0]
self.assertEqual(line, raise_src.strip())

_______________________________________________
Python-checkins mailing list
Python-checkins@python.org
https://mail.python.org/mailman/listinfo/python-checkins