diff options
author | Mark Shannon <mark@hotpy.org> | 2022-06-27 11:24:23 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-27 11:24:23 (GMT) |
commit | c0453a40faaadb43d2e69767af6c9680f8689063 (patch) | |
tree | d8ab19e5244e552e5d1b371651593be9be5142e4 /Lib | |
parent | 33fc3b5e42f241ab81cc6d115711545b4f9e271e (diff) | |
download | cpython-c0453a40faaadb43d2e69767af6c9680f8689063.zip cpython-c0453a40faaadb43d2e69767af6c9680f8689063.tar.gz cpython-c0453a40faaadb43d2e69767af6c9680f8689063.tar.bz2 |
GH-94163: Add BINARY_SLICE and STORE_SLICE instructions. (GH-94168)
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_compile.py | 36 |
3 files changed, 40 insertions, 1 deletions
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index 5c72049..6cd3538 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -409,6 +409,7 @@ _code_type = type(_write_atomic.__code__) # Python 3.12a1 3503 (Shrink LOAD_METHOD cache) # Python 3.12a1 3504 (Merge LOAD_METHOD back into LOAD_ATTR) # Python 3.12a1 3505 (Specialization/Cache for FOR_ITER) +# Python 3.12a1 3506 (Add BINARY_SLICE and STORE_SLICE instructions) # Python 3.13 will start with 3550 @@ -422,7 +423,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 = (3505).to_bytes(2, 'little') + b'\r\n' +MAGIC_NUMBER = (3506).to_bytes(2, 'little') + b'\r\n' _RAW_MAGIC_NUMBER = int.from_bytes(MAGIC_NUMBER, 'little') # For import.c diff --git a/Lib/opcode.py b/Lib/opcode.py index f515aaa..c7bc12a 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -66,6 +66,8 @@ def_op('UNARY_NOT', 12) def_op('UNARY_INVERT', 15) def_op('BINARY_SUBSCR', 25) +def_op('BINARY_SLICE', 26) +def_op('STORE_SLICE', 27) def_op('GET_LEN', 30) def_op('MATCH_MAPPING', 31) diff --git a/Lib/test/test_compile.py b/Lib/test/test_compile.py index d435724..85b2d82 100644 --- a/Lib/test/test_compile.py +++ b/Lib/test/test_compile.py @@ -1026,6 +1026,42 @@ if 1: for instr in dis.Bytecode(while_not_chained): self.assertNotEqual(instr.opname, "EXTENDED_ARG") + @support.cpython_only + def test_uses_slice_instructions(self): + + def check_op_count(func, op, expected): + actual = 0 + for instr in dis.Bytecode(func): + if instr.opname == op: + actual += 1 + self.assertEqual(actual, expected) + + def load(): + return x[a:b] + x [a:] + x[:b] + x[:] + + def store(): + x[a:b] = y + x [a:] = y + x[:b] = y + x[:] = y + + def long_slice(): + return x[a:b:c] + + def aug(): + x[a:b] += y + + check_op_count(load, "BINARY_SLICE", 4) + check_op_count(load, "BUILD_SLICE", 0) + check_op_count(store, "STORE_SLICE", 4) + check_op_count(store, "BUILD_SLICE", 0) + check_op_count(long_slice, "BUILD_SLICE", 1) + check_op_count(long_slice, "BINARY_SLICE", 0) + check_op_count(aug, "BINARY_SLICE", 1) + check_op_count(aug, "STORE_SLICE", 1) + check_op_count(aug, "BUILD_SLICE", 0) + + @requires_debug_ranges() class TestSourcePositions(unittest.TestCase): # Ensure that compiled code snippets have correct line and column numbers |