X Tutup
Skip to content

Commit eefbc40

Browse files
committed
option to show asm and DRY.
Get ready for some 2.3 support
1 parent 6bdddb6 commit eefbc40

File tree

11 files changed

+69
-49
lines changed

11 files changed

+69
-49
lines changed

uncompyle6/scanner.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,17 @@ def __init__(self, co, scanner, classname=None):
4545

4646
class Scanner(object):
4747

48-
def __init__(self, version):
48+
def __init__(self, version, show_asm=False):
4949
self.version = version
50+
self.show_asm = show_asm
51+
5052
# FIXME: DRY
5153
if version == 2.7:
5254
from xdis.opcodes import opcode_27
5355
self.opc = opcode_27
56+
elif version == 2.3:
57+
from xdis.opcodes import opcode_23
58+
self.opc = opcode_23
5459
elif version == 2.6:
5560
from xdis.opcodes import opcode_26
5661
self.opc = opcode_26
@@ -281,43 +286,42 @@ def restrict_to_parent(self, target, parent):
281286
target = parent['end']
282287
return target
283288

284-
def get_scanner(version):
289+
def get_scanner(version, show_asm=False):
285290
# Pick up appropriate scanner
286291
# from trepan.api import debug;
287292
# debug(start_opts={'startup-profile': True})
288293

289294
# FIXME: see if we can do better
290295
if version == 2.7:
291296
import uncompyle6.scanners.scanner27 as scan
292-
scanner = scan.Scanner27()
297+
scanner = scan.Scanner27(show_asm=show_asm)
298+
elif version == 2.3:
299+
import uncompyle6.scanners.scanner23 as scan
300+
scanner = scan.Scanner23(show_asm)
293301
elif version == 2.6:
294302
import uncompyle6.scanners.scanner26 as scan
295-
scanner = scan.Scanner26()
303+
scanner = scan.Scanner26(show_asm)
296304
elif version == 2.5:
297305
import uncompyle6.scanners.scanner25 as scan
298-
scanner = scan.Scanner25()
306+
scanner = scan.Scanner25(show_asm)
299307
elif version == 3.2:
300308
import uncompyle6.scanners.scanner32 as scan
301-
scanner = scan.Scanner32()
309+
scanner = scan.Scanner32(show_asm)
302310
elif version == 3.3:
303311
import uncompyle6.scanners.scanner33 as scan
304-
scanner = scan.Scanner33()
312+
scanner = scan.Scanner33(show_asm)
305313
elif version == 3.4:
306314
import uncompyle6.scanners.scanner34 as scan
307-
scanner = scan.Scanner34()
315+
scanner = scan.Scanner34(show_asm)
308316
elif version == 3.5:
309317
import uncompyle6.scanners.scanner35 as scan
310-
scanner = scan.Scanner35()
318+
scanner = scan.Scanner35(show_asm)
311319
else:
312320
raise RuntimeError("Unsupported Python version %s" % version)
313321
return scanner
314322

315323
if __name__ == "__main__":
316324
import inspect, uncompyle6
317325
co = inspect.currentframe().f_code
318-
scanner = get_scanner(uncompyle6.PYTHON_VERSION)
326+
scanner = get_scanner(uncompyle6.PYTHON_VERSION, True)
319327
tokens, customize = scanner.disassemble(co, {})
320-
print('-' * 30)
321-
for t in tokens:
322-
print(t)
323-
pass

uncompyle6/scanners/scanner2.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
import uncompyle6.scanner as scan
3333

3434
class Scanner2(scan.Scanner):
35-
def __init__(self, version):
36-
scan.Scanner.__init__(self, version)
35+
def __init__(self, version, show_asm=False):
36+
scan.Scanner.__init__(self, version, show_asm)
3737
self.pop_jump_if = frozenset([self.opc.PJIF, self.opc.PJIT])
3838
self.jump_forward = frozenset([self.opc.JA, self.opc.JF])
3939

@@ -204,6 +204,13 @@ def unmangle(name):
204204
tokens.append(Token(op_name, oparg, pattr, offset, linestart))
205205
else:
206206
tokens.append(Token(replace[offset], oparg, pattr, offset, linestart))
207+
pass
208+
pass
209+
210+
if self.show_asm:
211+
for t in tokens:
212+
print(t)
213+
print()
207214
return tokens, customize
208215

209216
def op_size(self, op):

uncompyle6/scanners/scanner25.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
# The history is that 2.7 support is the cleanest,
2424
# then from that we got 2.6 and so on.
2525
class Scanner25(scan.Scanner26):
26-
def __init__(self):
27-
scan2.Scanner2.__init__(self, 2.5)
26+
def __init__(self, show_asm):
27+
scan2.Scanner2.__init__(self, 2.5, show_asm)
2828
self.stmt_opcodes = frozenset([
2929
self.opc.SETUP_LOOP, self.opc.BREAK_LOOP,
3030
self.opc.SETUP_FINALLY, self.opc.END_FINALLY,

uncompyle6/scanners/scanner26.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
JUMP_OPs = opcode_26.JUMP_OPs
1919

2020
class Scanner26(scan.Scanner2):
21-
def __init__(self):
22-
super(Scanner26, self).__init__(2.6)
21+
def __init__(self, show_asm=False):
22+
super(Scanner26, self).__init__(2.6, show_asm)
2323
self.stmt_opcodes = frozenset([
2424
self.opc.SETUP_LOOP, self.opc.BREAK_LOOP,
2525
self.opc.SETUP_FINALLY, self.opc.END_FINALLY,
@@ -251,9 +251,10 @@ def unmangle(name):
251251
pass
252252
pass
253253

254-
# Debug
255-
# for t in tokens:
256-
# print t
254+
if self.show_asm:
255+
for t in tokens:
256+
print(t)
257+
print()
257258
return tokens, customize
258259

259260
def getOpcodeToDel(self, i):
@@ -586,8 +587,8 @@ def detect_structure(self, pos, op=None):
586587
if (jump_back and jump_back != self.prev[end]
587588
and code[jump_back + 3] in self.jump_forward):
588589
if (code[self.prev[end]] == self.opc.RETURN_VALUE
589-
or code[self.prev[end]] == self.opc.POP_BLOCK
590-
and code[self.prev[self.prev[end]]] == self.opc.RETURN_VALUE):
590+
or (code[self.prev[end]] == self.opc.POP_BLOCK
591+
and code[self.prev[self.prev[end]]] == self.opc.RETURN_VALUE)):
591592
jump_back = None
592593
if not jump_back: # loop suite ends in return. wtf right?
593594
jump_back = self.last_instr(start, end, self.opc.JA, start, False)
@@ -604,7 +605,7 @@ def detect_structure(self, pos, op=None):
604605
else:
605606
if self.get_target(jump_back) >= next_line_byte:
606607
jump_back = self.last_instr(start, end, self.opc.JA, start, False)
607-
if end > jump_back + 4 and code[end] in (self.opc.JF, self.opc.JA):
608+
if end > jump_back + 4 and code[end] in self.jump_forward:
608609
if code[jump_back + 4] in (self.opc.JA, self.opc.JF):
609610
if self.get_target(jump_back+4) == self.get_target(end):
610611
self.fixed_jumps[pos] = jump_back+4
@@ -807,9 +808,7 @@ def detect_structure(self, pos, op=None):
807808
if PYTHON_VERSION == 2.6:
808809
import inspect
809810
co = inspect.currentframe().f_code
810-
tokens, customize = Scanner26().disassemble(co)
811-
for t in tokens:
812-
print(t.format())
811+
tokens, customize = Scanner26(show_asm=True).disassemble(co)
813812
else:
814813
print("Need to be Python 2.6 to demo; I am %s." %
815814
PYTHON_VERSION)

uncompyle6/scanners/scanner27.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
JUMP_OPs = opcode_27.JUMP_OPs
1818

1919
class Scanner27(Scanner2):
20-
def __init__(self):
21-
super(Scanner27, self).__init__(2.7)
20+
def __init__(self, show_asm=False):
21+
super(Scanner27, self).__init__(2.7, show_asm)
2222

2323
# opcodes that start statements
2424
self.stmt_opcodes = frozenset([

uncompyle6/scanners/scanner3.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040

4141
class Scanner3(scan.Scanner):
4242

43-
def __init__(self, version):
44-
super(Scanner3, self).__init__(version)
43+
def __init__(self, version, show_asm=False):
44+
super(Scanner3, self).__init__(version, show_asm)
4545

4646
def disassemble(self, co, classname=None, code_objects={}):
4747
"""

uncompyle6/scanners/scanner32.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88

99
from __future__ import print_function
1010

11-
import xdis
12-
1311
# bytecode verification, verify(), uses JUMP_OPs from here
14-
# JUMP_OPs = xdis.opcodes.opcode_32.JUMP_OPs
12+
from xdis.opcodes import opcode_32 as opc
13+
JUMP_OPs = map(lambda op: opc.opname[op], opc.hasjrel + opc.hasjabs)
1514

1615
from uncompyle6.scanners.scanner3 import Scanner3
1716
class Scanner32(Scanner3):
1817

19-
def __init__(self):
20-
super(Scanner3, self).__init__(3.2)
18+
def __init__(self, show_asm=False):
19+
super(Scanner3, self).__init__(3.2, show_asm)
2120
return
2221
pass
2322

uncompyle6/scanners/scanner33.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88

99
from __future__ import print_function
1010

11-
from xdis.opcodes import opcode_33 as opc
12-
1311
# bytecode verification, verify(), uses JUMP_OPs from here
12+
from xdis.opcodes import opcode_33 as opc
1413
JUMP_OPs = map(lambda op: opc.opname[op], opc.hasjrel + opc.hasjabs)
1514

1615
from uncompyle6.scanners.scanner3 import Scanner3
1716
class Scanner33(Scanner3):
1817

19-
def __init__(self):
20-
super(Scanner3, self).__init__(3.3)
18+
def __init__(self, show_asm=False):
19+
super(Scanner3, self).__init__(3.3, show_asm)
2120
return
2221
pass
2322

uncompyle6/scanners/scanner34.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from uncompyle6.scanners.scanner3 import Scanner3
1818
class Scanner34(Scanner3):
1919

20-
def __init__(self):
21-
super(Scanner3, self).__init__(3.4)
20+
def __init__(self, show_asm=False):
21+
super(Scanner3, self).__init__(3.4, show_asm)
2222
return
2323
pass
2424

uncompyle6/scanners/scanner35.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88

99
from __future__ import print_function
1010

11-
from xdis.opcodes import opcode_35 as opc
12-
1311
from uncompyle6.scanners.scanner3 import Scanner3
1412

1513
# bytecode verification, verify(), uses JUMP_OPs from here
14+
from xdis.opcodes import opcode_35 as opc
1615
JUMP_OPs = map(lambda op: opc.opname[op], opc.hasjrel + opc.hasjabs)
1716

1817
class Scanner35(Scanner3):
1918

20-
def __init__(self):
21-
super(Scanner35, self).__init__(3.5)
19+
def __init__(self, show_asm=False):
20+
super(Scanner35, self).__init__(3.5, show_asm)
2221
return
2322
pass
2423

0 commit comments

Comments
 (0)
X Tutup