diff options
author | Zackery Spytz <zspytz@gmail.com> | 2019-08-25 09:44:09 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2019-08-25 09:44:09 (GMT) |
commit | ce6a070414ed1e1374d1e6212bfbff61b6d5d755 (patch) | |
tree | 55f9d0c54b98ad3c95e78a235668f6dbc314087b /Lib | |
parent | 8371799e300475c8f9f967e900816218d3500e5d (diff) | |
download | cpython-ce6a070414ed1e1374d1e6212bfbff61b6d5d755.zip cpython-ce6a070414ed1e1374d1e6212bfbff61b6d5d755.tar.gz cpython-ce6a070414ed1e1374d1e6212bfbff61b6d5d755.tar.bz2 |
bpo-34880: Add the LOAD_ASSERTION_ERROR opcode. (GH-15073)
Fix assert statement misbehavior if AssertionError is shadowed.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 3 | ||||
-rw-r--r-- | Lib/opcode.py | 2 | ||||
-rw-r--r-- | Lib/test/test_dis.py | 2 | ||||
-rw-r--r-- | Lib/test/test_exceptions.py | 16 |
4 files changed, 20 insertions, 3 deletions
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 67bd1d3..671b043 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -271,6 +271,7 @@ _code_type = type(_write_atomic.__code__) # Python 3.8b2 3412 (Swap the position of positional args and positional # only args in ast.arguments #37593) # Python 3.8b4 3413 (Fix "break" and "continue" in "finally" #37830) +# Python 3.9a0 3420 (add LOAD_ASSERTION_ERROR #34880) # # MAGIC must change whenever the bytecode emitted by the compiler may no # longer be understood by older implementations of the eval loop (usually @@ -279,7 +280,7 @@ _code_type = type(_write_atomic.__code__) # Whenever MAGIC_NUMBER is changed, the ranges in the magic_values array # in PC/launcher.c must also be updated. -MAGIC_NUMBER = (3413).to_bytes(2, 'little') + b'\r\n' +MAGIC_NUMBER = (3420).to_bytes(2, 'little') + b'\r\n' _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c _PYCACHE = '__pycache__' diff --git a/Lib/opcode.py b/Lib/opcode.py index 3fb716b..0ae0068 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -109,7 +109,7 @@ def_op('PRINT_EXPR', 70) def_op('LOAD_BUILD_CLASS', 71) def_op('YIELD_FROM', 72) def_op('GET_AWAITABLE', 73) - +def_op('LOAD_ASSERTION_ERROR', 74) def_op('INPLACE_LSHIFT', 75) def_op('INPLACE_RSHIFT', 76) def_op('INPLACE_AND', 77) 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 @@ def bug1333982(x=[]): 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 @@ 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): |