X Tutup
Skip to content

Commit 34841ab

Browse files
committed
scanner3: Python 2.6 compatibility: change set initializations. Get rid
of * import opcode_*: only a little of the much-needed larger cleanup Makefile: remove 3.x bytecode checking from Python 2.x for now. DRY Makefile a little bit (but more is needed)
1 parent 226f3c7 commit 34841ab

File tree

7 files changed

+38
-49
lines changed

7 files changed

+38
-49
lines changed

README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,7 @@ Known Bugs/Restrictions
8686
-----------------------
8787

8888
Python 2 deparsing is probably as solid as the various versions of
89-
uncompyle2. Python 3 deparsing is not as solid. Using Python 2 to
90-
deparse Python 3 has severe limitations, due to byte code format
91-
differences and the current inablity to retrieve code object fields across
92-
different Python versions. (I envy the pycdc C++ code which doesn't have such
93-
problems because they live totally outside of Python.)
89+
uncompyle2. Python 3 deparsing is okay but not as solid.
9490

9591
See Also
9692
--------

test/Makefile

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,28 @@ check:
1919
@$(PYTHON) -V && PYTHON_VERSION=`$(PYTHON) -V 2>&1 | cut -d ' ' -f 2 | cut -d'.' -f1,2`; \
2020
$(MAKE) check-$$PYTHON_VERSION
2121

22-
#: Run working tests from Python 2.6
23-
check-2.6: check-bytecode-2.7 check-bytecode-2.5
24-
$(PYTHON) test_pythonlib.py --bytecode-2.6 --verify $(COMPILE)
25-
26-
#: Run working tests from Python 2.7
27-
check-2.7: check-bytecode check-2.7-ok
22+
#: Run working tests from Python 2.6 or 2.7
23+
check-2.6 check-2.7: check-bytecode-2 check-2.7-ok
2824

2925
#: Run working tests from Python 3.3
30-
check-3.3: check-bytecode-3.3 check-bytecode-2.7
26+
check-3.3: check-bytecode
3127
$(PYTHON) test_pythonlib.py --bytecode-3.3 --verify $(COMPILE)
3228

3329
#: Run working tests from Python 3.4
34-
check-3.4: check-bytecode-2.7 check-bytecode-3.2
35-
$(PYTHON) test_pythonlib.py --bytecode-3.4 --ok-2.7 --verify $(COMPILE)
30+
check-3.4: check-bytecode check-2.7-ok
3631

3732
#: Check deparsing only, but from a different Python version
3833
check-disasm:
3934
$(PYTHON) dis-compare.py
4035

36+
#: Check deparsing bytecode only
37+
check-bytecode-2:
38+
$(PYTHON) test_pythonlib.py --bytecode-2.5 --bytecode-2.6 --bytecode-2.7
39+
4140
#: Check deparsing bytecode only
4241
check-bytecode:
43-
$(PYTHON) test_pythonlib.py --bytecode-2.5 --bytecode-2.7 --bytecode-3.2
42+
$(PYTHON) test_pythonlib.py --bytecode-2.5 --bytecode-2.6 --bytecode-2.7 \
43+
--bytecode-3.2 --bytecode-3.3 --bytecode-3.4
4444

4545
#: Check deparsing Python 2.5
4646
check-bytecode-2.5:

uncompyle6/opcodes/opcode_32.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
parsing and semantic interpretation.
88
"""
99

10-
__all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
11-
"haslocal", "hascompare", "hasfree", "opname", "opmap",
12-
"HAVE_ARGUMENT", "EXTENDED_ARG"]
13-
1410
cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
1511
'is not', 'exception match', 'BAD')
1612

@@ -21,6 +17,7 @@
2117
haslocal = []
2218
hascompare = []
2319
hasfree = []
20+
hasnargs = []
2421

2522
opmap = {}
2623
opname = [''] * 256
@@ -179,6 +176,7 @@ def updateGlobal():
179176

180177
def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
181178
def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
179+
hasnargs.append(131)
182180
def_op('MAKE_FUNCTION', 132) # Number of args with default values
183181
def_op('BUILD_SLICE', 133) # Number of items
184182
def_op('MAKE_CLOSURE', 134)
@@ -192,8 +190,11 @@ def updateGlobal():
192190
hasfree.append(138)
193191

194192
def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
193+
hasnargs.append(140)
195194
def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
195+
hasnargs.append(141)
196196
def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
197+
hasnargs.append(142)
197198

198199
jrel_op('SETUP_WITH', 143)
199200

uncompyle6/opcodes/opcode_33.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
parsing and semantic interpretation.
88
"""
99

10-
# Note: this should look exactly like Python 3.4's opcode.py
11-
12-
__all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
13-
"haslocal", "hascompare", "hasfree", "opname", "opmap",
14-
"HAVE_ARGUMENT", "EXTENDED_ARG"]
15-
1610
cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
1711
'is not', 'exception match', 'BAD')
1812

uncompyle6/opcodes/opcode_34.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@
77
parsing and semantic interpretation.
88
"""
99

10-
__all__ = ["cmp_op", "hasconst", "hasname", "hasjrel", "hasjabs",
11-
"haslocal", "hascompare", "hasfree", "opname", "opmap",
12-
"HAVE_ARGUMENT", "EXTENDED_ARG", "hasnargs"]
13-
1410
cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is',
1511
'is not', 'exception match', 'BAD')
1612

uncompyle6/scanners/scanner3.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919

2020
# Get all the opcodes into globals
21-
globals().update(dis.opmap)
22-
from uncompyle6.opcodes.opcode_33 import *
21+
import uncompyle6.opcodes.opcode_33 as op3
22+
globals().update(op3.opmap)
23+
24+
2325
import uncompyle6.scanner as scan
2426

2527
class Code3:
@@ -122,23 +124,23 @@ def unmangle(name):
122124
jump_idx = 0
123125
for jump_offset in jump_targets[offset]:
124126
tokens.append(Token('COME_FROM', None, repr(jump_offset),
125-
offset='{}_{}'.format(offset, jump_idx)))
127+
offset='%s_%s' % (offset, jump_idx)))
126128
jump_idx += 1
127129
pass
128130
pass
129131

130132
op = code[offset]
131-
op_name = opname[op]
133+
op_name = op3.opname[op]
132134

133135
oparg = None; pattr = None
134136

135-
if op >= HAVE_ARGUMENT:
137+
if op >= op3.HAVE_ARGUMENT:
136138
oparg = self.get_argument(offset) + extended_arg
137139
extended_arg = 0
138-
if op == EXTENDED_ARG:
140+
if op == op3.EXTENDED_ARG:
139141
extended_arg = oparg * scan.L65536
140142
continue
141-
if op in hasconst:
143+
if op in op3.hasconst:
142144
const = co.co_consts[oparg]
143145
if not PYTHON3 and isinstance(const, str):
144146
if const in code_objects:
@@ -168,17 +170,17 @@ def unmangle(name):
168170
pattr = '<code_object ' + const.co_name + '>'
169171
else:
170172
pattr = const
171-
elif op in hasname:
173+
elif op in op3.hasname:
172174
pattr = names[oparg]
173-
elif op in hasjrel:
175+
elif op in op3.hasjrel:
174176
pattr = repr(offset + 3 + oparg)
175-
elif op in hasjabs:
177+
elif op in op3.hasjabs:
176178
pattr = repr(oparg)
177-
elif op in haslocal:
179+
elif op in op3.haslocal:
178180
pattr = varnames[oparg]
179-
elif op in hascompare:
180-
pattr = cmp_op[oparg]
181-
elif op in hasfree:
181+
elif op in op3.hascompare:
182+
pattr = op3.cmp_op[oparg]
183+
elif op in op3.hasfree:
182184
pattr = free[oparg]
183185

184186
if op in (BUILD_LIST, BUILD_TUPLE, BUILD_SET, BUILD_SLICE,
@@ -338,23 +340,23 @@ def build_statement_indices(self):
338340
start = 0
339341
end = codelen = len(code)
340342

341-
statement_opcodes = {
343+
statement_opcodes = set([
342344
SETUP_LOOP, BREAK_LOOP, CONTINUE_LOOP,
343345
SETUP_FINALLY, END_FINALLY, SETUP_EXCEPT, SETUP_WITH,
344346
POP_BLOCK, STORE_FAST, DELETE_FAST, STORE_DEREF,
345347
STORE_GLOBAL, DELETE_GLOBAL, STORE_NAME, DELETE_NAME,
346348
STORE_ATTR, DELETE_ATTR, STORE_SUBSCR, DELETE_SUBSCR,
347349
RETURN_VALUE, RAISE_VARARGS, POP_TOP, PRINT_EXPR,
348350
JUMP_ABSOLUTE
349-
}
351+
])
350352

351353
statement_opcode_sequences = [(POP_JUMP_IF_FALSE, JUMP_FORWARD), (POP_JUMP_IF_FALSE, JUMP_ABSOLUTE),
352354
(POP_JUMP_IF_TRUE, JUMP_FORWARD), (POP_JUMP_IF_TRUE, JUMP_ABSOLUTE)]
353355

354-
designator_ops = {
356+
designator_ops = set([
355357
STORE_FAST, STORE_NAME, STORE_GLOBAL, STORE_DEREF, STORE_ATTR,
356358
STORE_SUBSCR, UNPACK_SEQUENCE, JUMP_ABSOLUTE
357-
}
359+
])
358360

359361
# Compose preliminary list of indices with statements,
360362
# using plain statement opcodes

uncompyle6/scanners/scanner32.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
import uncompyle6.scanners.scanner3 as scan3
1313

14-
import uncompyle6.opcodes.opcode_34
14+
import uncompyle6.opcodes.opcode_32
1515
# verify uses JUMP_OPs from here
16-
JUMP_OPs = uncompyle6.opcodes.opcode_34.JUMP_OPs
16+
JUMP_OPs = uncompyle6.opcodes.opcode_32.JUMP_OPs
1717

1818
class Scanner32(scan3.Scanner3):
1919

0 commit comments

Comments
 (0)
X Tutup