Skip to content

bpo-46411: Modernize exception handling in tests #30638

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions Lib/test/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,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(
Expand Down Expand Up @@ -1850,8 +1850,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)

Expand All @@ -1876,8 +1875,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)

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([]))
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,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'):
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -5524,8 +5524,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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with this change.

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)
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_zipimport.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,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())
Expand Down