Skip to content

Commit 2100f91

Browse files
author
rocky
committed
Cell/Free var fixes? ...
Also move some invariant code out of a loop.
1 parent 69d34c1 commit 2100f91

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

pytest/testdata/test_nested_scopes-extended-bytes-2.1.right

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@
351351
# 0: blurb
352352
# 1: y3a
353353
# 2: flurb
354-
# Cell variables:
354+
# Free variables:
355355
# 0: blurb
356356
# 1: flurb
357357

@@ -418,7 +418,7 @@
418418
# Local variables:
419419
# 0: y3
420420
# 1: blurb
421-
# Cell variables:
421+
# Free variables:
422422
# 0: blurb
423423

424424
43: 0 |7f 00 2b| SET_LINENO 43
@@ -579,7 +579,7 @@
579579
# Local variables:
580580
# 0: blurb
581581
# 1: y
582-
# Cell variables:
582+
# Free variables:
583583
# 0: blurb
584584

585585
76: 0 |7f 00 4c| SET_LINENO 76
@@ -634,7 +634,7 @@
634634
# 6: c
635635
# 7: d
636636
# 8: y
637-
# Cell variables:
637+
# Free variables:
638638
# 0: a
639639

640640
83: 0 |7f 00 53| SET_LINENO 83
@@ -805,7 +805,7 @@
805805
# x
806806
# Positional arguments:
807807
# x
808-
# Free variables:
808+
# Cell variables:
809809
# 0: blurb
810810
# 1: flurb
811811

@@ -815,10 +815,10 @@
815815

816816
36: 6 |64 00 01| LOAD_CONST ("y-blurb =") ; TOS = "y-blurb ="
817817
9 |47 | PRINT_ITEM print "y-blurb =",
818-
10 |88 00 00| LOAD_DEREF (0) ; TOS = 0
819-
13 |47 | PRINT_ITEM print 0,
820-
14 |88 00 01| LOAD_DEREF (1) ; TOS = 1
821-
17 |47 | PRINT_ITEM print 1,
818+
10 |88 00 00| LOAD_DEREF (blurb) ; TOS = blurb
819+
13 |47 | PRINT_ITEM print blurb,
820+
14 |88 00 01| LOAD_DEREF (flurb) ; TOS = flurb
821+
17 |47 | PRINT_ITEM print flurb,
822822
18 |48 | PRINT_NEWLINE print "\n"
823823
19 |64 00 00| LOAD_CONST (None) ; TOS = None
824824
22 |53 | RETURN_VALUE return None
@@ -844,7 +844,7 @@
844844
# x
845845
# Local variables:
846846
# 1: z
847-
# Free variables:
847+
# Cell variables:
848848
# 0: blurb
849849

850850
44: 0 |7f 00 2c| SET_LINENO 44
@@ -865,8 +865,8 @@
865865

866866
49: 28 |64 00 02| LOAD_CONST ("y-blurb =") ; TOS = "y-blurb ="
867867
31 |47 | PRINT_ITEM print "y-blurb =",
868-
32 |88 00 00| LOAD_DEREF (0) ; TOS = 0
869-
35 |47 | PRINT_ITEM print 0,
868+
32 |88 00 00| LOAD_DEREF (blurb) ; TOS = blurb
869+
35 |47 | PRINT_ITEM print blurb,
870870
36 |64 00 00| LOAD_CONST (None) ; TOS = None
871871
39 |53 | RETURN_VALUE return None
872872

@@ -893,7 +893,7 @@
893893
# Local variables:
894894
# 1: blurb
895895
# 2: z
896-
# Cell variables:
896+
# Free variables:
897897
# 0: blurb
898898

899899
56: 0 |7f 00 38| SET_LINENO 56
@@ -978,7 +978,7 @@
978978
# x
979979
# Positional arguments:
980980
# x
981-
# Free variables:
981+
# Cell variables:
982982
# 0: blurb
983983

984984
77: 0 |7f 00 4d| SET_LINENO 77
@@ -987,8 +987,8 @@
987987

988988
78: 6 |64 00 01| LOAD_CONST ("y-blurb =") ; TOS = "y-blurb ="
989989
9 |47 | PRINT_ITEM print "y-blurb =",
990-
10 |88 00 00| LOAD_DEREF (0) ; TOS = 0
991-
13 |47 | PRINT_ITEM print 0,
990+
10 |88 00 00| LOAD_DEREF (blurb) ; TOS = blurb
991+
13 |47 | PRINT_ITEM print blurb,
992992
14 |48 | PRINT_NEWLINE print "\n"
993993
15 |64 00 00| LOAD_CONST (None) ; TOS = None
994994
18 |53 | RETURN_VALUE return None
@@ -1010,7 +1010,7 @@
10101010
# x
10111011
# Positional arguments:
10121012
# x
1013-
# Free variables:
1013+
# Cell variables:
10141014
# 0: a
10151015

10161016
84: 0 |7f 00 54| SET_LINENO 84
@@ -1019,8 +1019,8 @@
10191019

10201020
85: 6 |64 00 01| LOAD_CONST ("y-a =") ; TOS = "y-a ="
10211021
9 |47 | PRINT_ITEM print "y-a =",
1022-
10 |88 00 00| LOAD_DEREF (0) ; TOS = 0
1023-
13 |47 | PRINT_ITEM print 0,
1022+
10 |88 00 00| LOAD_DEREF (a) ; TOS = a
1023+
13 |47 | PRINT_ITEM print a,
10241024
14 |48 | PRINT_NEWLINE print "\n"
10251025
15 |64 00 00| LOAD_CONST (None) ; TOS = None
10261026
18 |53 | RETURN_VALUE return None
@@ -1098,7 +1098,7 @@
10981098
# 1: 'z-blurb ='
10991099
# Names:
11001100
# 0: blurb
1101-
# Free variables:
1101+
# Cell variables:
11021102
# 0: blurb
11031103

11041104
57: 0 |7f 00 39| SET_LINENO 57
@@ -1107,8 +1107,8 @@
11071107

11081108
58: 6 |64 00 01| LOAD_CONST ("z-blurb =") ; TOS = "z-blurb ="
11091109
9 |47 | PRINT_ITEM print "z-blurb =",
1110-
10 |88 00 00| LOAD_DEREF (0) ; TOS = 0
1111-
13 |47 | PRINT_ITEM print 0,
1110+
10 |88 00 00| LOAD_DEREF (blurb) ; TOS = blurb
1111+
13 |47 | PRINT_ITEM print blurb,
11121112
14 |64 00 00| LOAD_CONST (None) ; TOS = None
11131113
17 |53 | RETURN_VALUE return None
11141114

xdis/bytecode.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,11 @@ def get_logical_instruction_at_offset(
276276
last_op_was_extended_arg = True
277277
i = offset
278278

279+
# create a localsplusnames table that resolves duplicates.
280+
localsplusnames = (varnames or tuple()) + tuple(
281+
name for name in (cells or tuple()) if name not in varnames
282+
)
283+
279284
while i < n and last_op_was_extended_arg:
280285
op = code2num(bytecode, i)
281286
opname = opc.opname[op]
@@ -316,11 +321,6 @@ def get_logical_instruction_at_offset(
316321

317322
argval = arg
318323

319-
# create a localsplusnames table that resolves duplicates.
320-
localsplusnames = (varnames or tuple()) + tuple(
321-
name for name in (cells or tuple()) if name not in varnames
322-
)
323-
324324
if op in opc.CONST_OPS:
325325
argval, argrepr = _get_const_info(arg, constants)
326326
elif op in opc.NAME_OPS:
@@ -470,7 +470,7 @@ def get_instructions_bytes(
470470
constants: tuple = code_object.co_consts
471471
names: tuple = code_object.co_names
472472
varnames: tuple = code_object.co_varnames
473-
cells: tuple = code_object.co_cells if hasattr(code_object, "co_cells") else tuple()
473+
cells: tuple = code_object.co_cellvars if hasattr(code_object, "co_cellvars") else tuple()
474474
exception_entries = code_object.exception_entries if hasattr(code_object, "exception_entries") else tuple()
475475
# freevars: tuple = code_object.co_freevars
476476

xdis/bytecode_graal.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def get_instructions_bytes_graal(
2929
constants: tuple = code_object.co_consts
3030
names: tuple = code_object.co_names
3131
varnames: tuple = code_object.co_varnames
32-
# cells: tuple = code_object.co_cells
33-
# freevars: tuple = code_object.co_freevars
32+
cells: tuple = code_object.co_cellvars
33+
freevars: tuple = code_object.co_freevars
3434

3535
i = 0
3636
n = len(bytecode)
@@ -114,11 +114,10 @@ def get_instructions_bytes_graal(
114114
opc.opmap["STORE_DEREF"],
115115
opc.opmap["DELETE_DEREF"],
116116
):
117-
# if arg >= len(cells):
118-
# import pdb; pdb.set_trace()
119-
# argrepr = freevars[arg - len(cells)]
120-
# else:
121-
# argrepr = cells[arg]
117+
if arg >= len(cells):
118+
argrepr = freevars[arg - len(cells)]
119+
else:
120+
argrepr = cells[arg]
122121
break
123122
elif opcode in (
124123
opc.opmap["LOAD_FAST"],

xdis/lineoffsets_graal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(
6262
bytecode: bytes = code_object.co_code
6363
start_column: int = code_object.startColumn
6464
start_line: int = code_object.startLine
65-
# cells: tuple = code_object.co_cells
65+
# cells: tuple = code_object.co_cellvars
6666
# freevars: tuple = code_object.co_freevars
6767
arg_counts: Dict[int, int] = opc.arg_counts
6868

xdis/unmarshal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,8 +771,8 @@ def t_code(self, save_ref, bytes_for_s: bool = False):
771771
co_varnames = tuple()
772772

773773
if self.version_triple >= (2, 0):
774-
co_freevars = self.r_object(bytes_for_s=bytes_for_s)
775774
co_cellvars = self.r_object(bytes_for_s=bytes_for_s)
775+
co_freevars = self.r_object(bytes_for_s=bytes_for_s)
776776

777777
co_filename = self.r_object(bytes_for_s=bytes_for_s)
778778
co_name = self.r_object(bytes_for_s=bytes_for_s)

0 commit comments

Comments
 (0)