bpo-34880: Add the LOAD_ASSERTION_ERROR opcode. (GH-15073)

Fix assert statement misbehavior if AssertionError is shadowed.
This commit is contained in:
Zackery Spytz 2019-08-25 03:44:09 -06:00 committed by Serhiy Storchaka
parent 8371799e30
commit ce6a070414
14 changed files with 2664 additions and 2627 deletions

View file

@ -1285,6 +1285,22 @@ class ExceptionTests(unittest.TestCase):
next(i)
next(i)
@unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
def test_assert_shadowing(self):
# Shadowing AssertionError would cause the assert statement to
# misbehave.
global AssertionError
AssertionError = TypeError
try:
assert False, 'hello'
except BaseException as e:
del AssertionError
self.assertIsInstance(e, AssertionError)
self.assertEqual(str(e), 'hello')
else:
del AssertionError
self.fail('Expected exception')
class ImportErrorTests(unittest.TestCase):