bpo-34880: Add the LOAD_ASSERTION_ERROR opcode. (GH-15073)
Fix assert statement misbehavior if AssertionError is shadowed.
diff --git a/Lib/test/test_dis.py b/Lib/test/test_dis.py
index 03b2860..11f97e6 100644
--- a/Lib/test/test_dis.py
+++ b/Lib/test/test_dis.py
@@ -147,7 +147,7 @@
dis_bug1333982 = """\
%3d 0 LOAD_CONST 1 (0)
2 POP_JUMP_IF_TRUE 26
- 4 LOAD_GLOBAL 0 (AssertionError)
+ 4 LOAD_ASSERTION_ERROR
6 LOAD_CONST 2 (<code object <listcomp> at 0x..., file "%s", line %d>)
8 LOAD_CONST 3 ('bug1333982.<locals>.<listcomp>')
10 MAKE_FUNCTION 0
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index 10c1e07..4d1aa4b 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1285,6 +1285,22 @@
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):