Skip to content

Commit aca90d2

Browse files
committed
beta1 updates
1 parent ae7302b commit aca90d2

4 files changed

Lines changed: 52 additions & 17 deletions

File tree

src/bytecode/instr.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ class CommonConstant(enum.IntEnum):
269269
BUILTIN_TUPLE = 2
270270
BUILTIN_ALL = 3
271271
BUILTIN_ANY = 4
272+
BUILTIN_LIST = 5
273+
BUILTIN_SET = 6
274+
CONSTANT_NONE = 7
275+
CONSTANT_EMPTY_STR = 8
276+
CONSTANT_TRUE = 9
277+
CONSTANT_FALSE = 10
278+
CONSTANT_MINUS_ONE = 11
272279

273280

274281
# This make type checking happy but means it won't catch attempt to manipulate an unset

tests/test_bytecode.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import unittest
88

99
from bytecode import Bytecode, ConcreteInstr, FreeVar, Instr, Label, SetLineno
10-
from bytecode.instr import BinaryOp, FormatValue, InstrLocation
11-
from bytecode.utils import PY312, PY313, PY314
10+
from bytecode.instr import BinaryOp, CommonConstant, FormatValue, InstrLocation
11+
from bytecode.utils import PY312, PY313, PY314, PY315
1212

1313
from . import TestCase, get_code
1414

@@ -168,6 +168,18 @@ def test_from_code(self):
168168
bytecode = Bytecode.from_code(code)
169169
label_else = Label()
170170
if PY314:
171+
172+
def _ret_none(lineno):
173+
return (
174+
Instr(
175+
"LOAD_COMMON_CONSTANT",
176+
CommonConstant.CONSTANT_NONE,
177+
lineno=lineno,
178+
)
179+
if PY315
180+
else Instr("LOAD_CONST", None, lineno=lineno)
181+
)
182+
171183
self.assertInstructionListEqual(
172184
bytecode,
173185
[
@@ -178,12 +190,12 @@ def test_from_code(self):
178190
Instr("NOT_TAKEN", lineno=1),
179191
Instr("LOAD_SMALL_INT", 1, lineno=2),
180192
Instr("STORE_NAME", "x", lineno=2),
181-
Instr("LOAD_CONST", None, lineno=2),
193+
_ret_none(2),
182194
Instr("RETURN_VALUE", lineno=2),
183195
label_else,
184196
Instr("LOAD_SMALL_INT", 2, lineno=4),
185197
Instr("STORE_NAME", "x", lineno=4),
186-
Instr("LOAD_CONST", None, lineno=4),
198+
_ret_none(4),
187199
Instr("RETURN_VALUE", lineno=4),
188200
],
189201
)
@@ -292,7 +304,11 @@ def func():
292304
]
293305
+ (
294306
[
295-
Instr("LOAD_CONST", None, lineno=3),
307+
Instr(
308+
"LOAD_COMMON_CONSTANT" if PY315 else "LOAD_CONST",
309+
CommonConstant.CONSTANT_NONE if PY315 else None,
310+
lineno=3,
311+
),
296312
Instr("RETURN_VALUE", lineno=3),
297313
]
298314
if PY314

tests/test_cfg.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
SetLineno,
2020
dump_bytecode,
2121
)
22+
from bytecode.instr import CommonConstant
2223
from bytecode.utils import PY312, PY313, PY314, PY315
2324

2425
from . import TestCase, disassemble as _disassemble
@@ -33,15 +34,20 @@ def disassemble(
3334
# drop LOAD_CONST+RETURN_VALUE to only keep 2 instructions,
3435
# to make unit tests shorter
3536
block = blocks[-1]
36-
test = (
37-
(block[-1].name == "RETURN_CONST" and block[-1].arg is None)
38-
if PY312 and not PY314
39-
else (
37+
if PY315:
38+
test = (
39+
block[-2].name == "LOAD_COMMON_CONSTANT"
40+
and block[-2].arg == CommonConstant.CONSTANT_NONE
41+
and block[-1].name == "RETURN_VALUE"
42+
)
43+
elif PY312 and not PY314:
44+
test = block[-1].name == "RETURN_CONST" and block[-1].arg is None
45+
else:
46+
test = (
4047
block[-2].name == "LOAD_CONST"
4148
and block[-2].arg is None
4249
and block[-1].name == "RETURN_VALUE"
4350
)
44-
)
4551
if not test:
4652
raise ValueError(
4753
"unable to find implicit RETURN_VALUE <None>: %s" % block[-2:]

tests/test_concrete.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
SetLineno,
2222
)
2323
from bytecode.concrete import ExceptionTableEntry
24+
from bytecode.instr import CommonConstant
2425
from bytecode.utils import PY312, PY313, PY314, PY315
2526

2627
from . import TestCase, get_code
@@ -244,7 +245,7 @@ def test_eq(self):
244245
def test_attr(self):
245246
code_obj = get_code("x = 5")
246247
code = ConcreteBytecode.from_code(code_obj)
247-
self.assertEqual(code.consts, [5, None])
248+
self.assertEqual(code.consts, [5] if PY315 else [5, None])
248249
self.assertEqual(code.names, ["x"])
249250
self.assertEqual(code.varnames, [])
250251
self.assertEqual(code.freevars, [])
@@ -256,7 +257,7 @@ def test_attr(self):
256257
ConcreteInstr("CACHE", 0, lineno=0),
257258
ConcreteInstr("LOAD_SMALL_INT", 5, lineno=1),
258259
ConcreteInstr("STORE_NAME", 0, lineno=1),
259-
ConcreteInstr("LOAD_CONST", 1, lineno=1),
260+
ConcreteInstr("LOAD_COMMON_CONSTANT", 7, lineno=1),
260261
ConcreteInstr("RETURN_VALUE", lineno=1),
261262
]
262263
if PY315
@@ -333,7 +334,9 @@ def f():
333334
]
334335
+ (
335336
[
336-
ConcreteInstr("LOAD_CONST", 1),
337+
ConcreteInstr("LOAD_COMMON_CONSTANT", CommonConstant.CONSTANT_NONE)
338+
if PY315
339+
else ConcreteInstr("LOAD_CONST", 1),
337340
ConcreteInstr("RETURN_VALUE"),
338341
]
339342
if PY314
@@ -457,7 +460,9 @@ def test_extended_lnotab2(self):
457460
]
458461
+ (
459462
[
460-
ConcreteInstr("LOAD_CONST", 1),
463+
ConcreteInstr("LOAD_COMMON_CONSTANT", CommonConstant.CONSTANT_NONE)
464+
if PY315
465+
else ConcreteInstr("LOAD_CONST", 1),
461466
ConcreteInstr("RETURN_VALUE"),
462467
]
463468
if PY314
@@ -764,17 +769,18 @@ def foo(x: int, y: int):
764769
ConcreteInstr("MAKE_FUNCTION", lineno=1),
765770
ConcreteInstr("SET_FUNCTION_ATTRIBUTE", 16, lineno=1),
766771
ConcreteInstr("STORE_NAME", 0, lineno=1),
767-
ConcreteInstr("LOAD_CONST", 2, lineno=1),
772+
ConcreteInstr("LOAD_COMMON_CONSTANT", 7, lineno=1),
768773
ConcreteInstr("RETURN_VALUE", lineno=1),
769774
]
775+
expected_consts = [ann_code, func_code]
770776
self.assertSequenceEqual(concrete.names, ["foo"])
771-
self.assertSequenceEqual(concrete.consts, [ann_code, func_code, None])
777+
self.assertSequenceEqual(concrete.consts, expected_consts)
772778
self.assertInstructionListEqual(list(concrete), expected_py315)
773779
concrete = ConcreteBytecode.from_code(code_obj, extended_arg=True)
774780
ann_code = concrete.consts[0]
775781
func_code = concrete.consts[1]
776782
self.assertEqual(concrete.names, ["foo"])
777-
self.assertEqual(concrete.consts, [ann_code, func_code, None])
783+
self.assertEqual(concrete.consts, expected_consts)
778784
self.assertInstructionListEqual(list(concrete), expected_py315)
779785
return
780786
if PY314:

0 commit comments

Comments
 (0)