You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-03 00:45:53 +08:00
Python3 compatibility: (disas, scanner*)
Reduce useless verbiage in status messages and DRY code a little; __init__, uncompyle6
This commit is contained in:
@@ -47,7 +47,7 @@ Usage_short = \
|
|||||||
|
|
||||||
import sys, os, getopt
|
import sys, os, getopt
|
||||||
import os.path
|
import os.path
|
||||||
from uncompyle6 import main, verify
|
from uncompyle6 import main, status_msg, verify
|
||||||
import time
|
import time
|
||||||
|
|
||||||
if sys.version[:3] != '2.7' and sys.version[:3] != '3.4':
|
if sys.version[:3] != '2.7' and sys.version[:3] != '3.4':
|
||||||
@@ -128,8 +128,12 @@ if timestamp:
|
|||||||
print(time.strftime(timestampfmt))
|
print(time.strftime(timestampfmt))
|
||||||
if numproc <= 1:
|
if numproc <= 1:
|
||||||
try:
|
try:
|
||||||
result = main(src_base, out_base, files, codes, outfile, showasm, showast, do_verify)
|
result = main(src_base, out_base, files, codes, outfile, showasm,
|
||||||
print('# decompiled %i files: %i okay, %i failed, %i verify failed' % result)
|
showast, do_verify)
|
||||||
|
if len(files) > 1:
|
||||||
|
mess = status_msg(do_verify, *result)
|
||||||
|
print('# ' + mess)
|
||||||
|
pass
|
||||||
except (KeyboardInterrupt):
|
except (KeyboardInterrupt):
|
||||||
pass
|
pass
|
||||||
except verify.VerifyCmpError:
|
except verify.VerifyCmpError:
|
||||||
|
@@ -107,10 +107,10 @@ def uncompyle(version, co, out=None, showasm=0, showast=0):
|
|||||||
import uncompyle6.scanner27 as scan
|
import uncompyle6.scanner27 as scan
|
||||||
scanner = scan.Scanner27()
|
scanner = scan.Scanner27()
|
||||||
elif version == 2.6:
|
elif version == 2.6:
|
||||||
import scanner26 as scan
|
import uncompyle6.scanner26 as scan
|
||||||
scanner = scan.Scanner26()
|
scanner = scan.Scanner26()
|
||||||
elif version == 2.5:
|
elif version == 2.5:
|
||||||
import scanner25 as scan
|
import uncompyle6.scanner25 as scan
|
||||||
scanner = scan.Scanner25()
|
scanner = scan.Scanner25()
|
||||||
scanner.setShowAsm(showasm, out)
|
scanner.setShowAsm(showasm, out)
|
||||||
tokens, customize = scanner.disassemble(co)
|
tokens, customize = scanner.disassemble(co)
|
||||||
@@ -166,6 +166,23 @@ else:
|
|||||||
def __memUsage():
|
def __memUsage():
|
||||||
return ''
|
return ''
|
||||||
|
|
||||||
|
def status_msg(do_verify, tot_files, okay_files, failed_files,
|
||||||
|
verify_failed_files):
|
||||||
|
if tot_files == 1:
|
||||||
|
if failed_files:
|
||||||
|
return "decompile failed"
|
||||||
|
elif verify_failed_files:
|
||||||
|
return "decompile verify failed"
|
||||||
|
else:
|
||||||
|
return "Successfully decompiled file"
|
||||||
|
pass
|
||||||
|
pass
|
||||||
|
mess = "decompiled %i files: %i okay, %i failed" % (tot_files, okay_files, failed_files)
|
||||||
|
if do_verify:
|
||||||
|
mess += (", %i verify failed" % verify_failed_files)
|
||||||
|
return mess
|
||||||
|
|
||||||
|
|
||||||
def main(in_base, out_base, files, codes, outfile=None,
|
def main(in_base, out_base, files, codes, outfile=None,
|
||||||
showasm=0, showast=0, do_verify=0):
|
showasm=0, showast=0, do_verify=0):
|
||||||
'''
|
'''
|
||||||
@@ -249,10 +266,8 @@ def main(in_base, out_base, files, codes, outfile=None,
|
|||||||
okay_files += 1
|
okay_files += 1
|
||||||
if not outfile: print('\n# okay decompyling', infile, __memUsage())
|
if not outfile: print('\n# okay decompyling', infile, __memUsage())
|
||||||
if outfile:
|
if outfile:
|
||||||
mess = "decompiled %i files: %i okay, %i failed" % (tot_files, okay_files, failed_files)
|
sys.stdout.write("%s\r" %
|
||||||
if do_verify:
|
status_msg(do_verify, tot_files, okay_files, failed_files, verify_failed_files))
|
||||||
mess += (", %i verify failed" % verify_failed_files)
|
|
||||||
sys.stdout.write("%s\r" % mess)
|
|
||||||
sys.stdout.flush()
|
sys.stdout.flush()
|
||||||
if outfile:
|
if outfile:
|
||||||
sys.stdout.write("\n")
|
sys.stdout.write("\n")
|
||||||
|
@@ -20,9 +20,12 @@ internStrings = []
|
|||||||
# XXX For backwards compatibility
|
# XXX For backwards compatibility
|
||||||
disco = Mdis.disassemble
|
disco = Mdis.disassemble
|
||||||
|
|
||||||
if (sys.version_info >= (3, 0)):
|
PYTHON3 = (sys.version_info >= (3, 0))
|
||||||
def long(n):
|
if PYTHON3:
|
||||||
return n
|
def long(n): return n
|
||||||
|
|
||||||
|
def compat_str(s):
|
||||||
|
return s.decode('utf-8', errors='ignore') if PYTHON3 else str(s)
|
||||||
|
|
||||||
def marshalLoad(fp):
|
def marshalLoad(fp):
|
||||||
global internStrings
|
global internStrings
|
||||||
@@ -53,9 +56,21 @@ def load(fp):
|
|||||||
co_name = load(fp)
|
co_name = load(fp)
|
||||||
co_firstlineno = unpack('i', fp.read(4))[0]
|
co_firstlineno = unpack('i', fp.read(4))[0]
|
||||||
co_lnotab = load(fp)
|
co_lnotab = load(fp)
|
||||||
return Code(co_argcount, co_nlocals, co_stacksize, co_flags, co_code,
|
if PYTHON3:
|
||||||
co_consts, co_names, co_varnames, co_filename, co_name,
|
# The Python3 code object is different than Python2's which
|
||||||
co_firstlineno, co_lnotab, co_freevars, co_cellvars)
|
# we are reading if we get here. In particular, it has a
|
||||||
|
# kwonlyargcount parameter which we set to 0.
|
||||||
|
# Also various parameters which were strings are now
|
||||||
|
# bytes (which is probably more logical).
|
||||||
|
return Code(co_argcount, 0, co_nlocals, co_stacksize, co_flags,
|
||||||
|
bytes(co_code, encoding='utf-8'),
|
||||||
|
co_consts, co_names, co_varnames, co_filename, co_name,
|
||||||
|
co_firstlineno, bytes(co_lnotab, encoding='utf-8'),
|
||||||
|
co_freevars, co_cellvars)
|
||||||
|
else:
|
||||||
|
return Code(co_argcount, co_nlocals, co_stacksize, co_flags, co_code,
|
||||||
|
co_consts, co_names, co_varnames, co_filename, co_name,
|
||||||
|
co_firstlineno, co_lnotab, co_freevars, co_cellvars)
|
||||||
|
|
||||||
# const type
|
# const type
|
||||||
elif marshalType == '.':
|
elif marshalType == '.':
|
||||||
@@ -105,10 +120,10 @@ def load(fp):
|
|||||||
return internStrings[refnum]
|
return internStrings[refnum]
|
||||||
elif marshalType == 's':
|
elif marshalType == 's':
|
||||||
strsize = unpack('i', fp.read(4))[0]
|
strsize = unpack('i', fp.read(4))[0]
|
||||||
return str(fp.read(strsize))
|
return compat_str(fp.read(strsize))
|
||||||
elif marshalType == 't':
|
elif marshalType == 't':
|
||||||
strsize = unpack('i', fp.read(4))[0]
|
strsize = unpack('i', fp.read(4))[0]
|
||||||
interned = str(fp.read(strsize))
|
interned = compat_str(fp.read(strsize))
|
||||||
internStrings.append(interned)
|
internStrings.append(interned)
|
||||||
return interned
|
return interned
|
||||||
elif marshalType == 'u':
|
elif marshalType == 'u':
|
||||||
|
@@ -16,7 +16,10 @@ from operator import itemgetter
|
|||||||
|
|
||||||
if (sys.version_info > (3, 0)):
|
if (sys.version_info > (3, 0)):
|
||||||
intern = sys.intern
|
intern = sys.intern
|
||||||
|
L65536 = 65536
|
||||||
import uncompyle6
|
import uncompyle6
|
||||||
|
else:
|
||||||
|
L65536 = long(65536)
|
||||||
|
|
||||||
from uncompyle6.opcodes import opcode_25, opcode_26, opcode_27
|
from uncompyle6.opcodes import opcode_25, opcode_26, opcode_27
|
||||||
|
|
||||||
|
@@ -12,9 +12,9 @@ from array import array
|
|||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
from struct import *
|
from struct import *
|
||||||
|
|
||||||
from uncompyle6.opcodes.opcode_25 import *
|
|
||||||
import dis
|
import dis
|
||||||
import scanner as scan
|
from uncompyle6.opcodes.opcode_25 import *
|
||||||
|
import uncompyle6.scanner as scan
|
||||||
|
|
||||||
class Scanner25(scan.Scanner):
|
class Scanner25(scan.Scanner):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
@@ -131,7 +131,7 @@ class Scanner25(scan.Scanner):
|
|||||||
extended_arg = 0
|
extended_arg = 0
|
||||||
if op == EXTENDED_ARG:
|
if op == EXTENDED_ARG:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
extended_arg = oparg * 65536L
|
extended_arg = oparg * scan.L65536
|
||||||
continue
|
continue
|
||||||
if op in hasconst:
|
if op in hasconst:
|
||||||
const = co.co_consts[oparg]
|
const = co.co_consts[oparg]
|
||||||
@@ -377,7 +377,7 @@ class Scanner25(scan.Scanner):
|
|||||||
result.append((block[0]+startBlock, block[1]))
|
result.append((block[0]+startBlock, block[1]))
|
||||||
self.linestarts = result
|
self.linestarts = result
|
||||||
# handle opcodeToChange deplacement
|
# handle opcodeToChange deplacement
|
||||||
for index in xrange(len(self.toChange)):
|
for index in range(len(self.toChange)):
|
||||||
change = self.toChange[index]
|
change = self.toChange[index]
|
||||||
delta = 0
|
delta = 0
|
||||||
for toDel in listDel:
|
for toDel in listDel:
|
||||||
|
@@ -133,7 +133,7 @@ class Scanner26(scan.Scanner):
|
|||||||
extended_arg = 0
|
extended_arg = 0
|
||||||
if op == EXTENDED_ARG:
|
if op == EXTENDED_ARG:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
extended_arg = oparg * 65536L
|
extended_arg = oparg * scan.L65536
|
||||||
continue
|
continue
|
||||||
if op in hasconst:
|
if op in hasconst:
|
||||||
const = co.co_consts[oparg]
|
const = co.co_consts[oparg]
|
||||||
@@ -375,7 +375,7 @@ class Scanner26(scan.Scanner):
|
|||||||
result.append((block[0]+startBlock, block[1]))
|
result.append((block[0]+startBlock, block[1]))
|
||||||
self.linestarts = result
|
self.linestarts = result
|
||||||
# handle opcodeToChange deplacement
|
# handle opcodeToChange deplacement
|
||||||
for index in xrange(len(self.toChange)):
|
for index in range(len(self.toChange)):
|
||||||
change = self.toChange[index]
|
change = self.toChange[index]
|
||||||
delta = 0
|
delta = 0
|
||||||
for toDel in listDel:
|
for toDel in listDel:
|
||||||
|
@@ -55,7 +55,7 @@ class Scanner27(scan.Scanner):
|
|||||||
while j < start_byte:
|
while j < start_byte:
|
||||||
self.lines.append(linetuple(prev_line_no, start_byte))
|
self.lines.append(linetuple(prev_line_no, start_byte))
|
||||||
j += 1
|
j += 1
|
||||||
(prev_start_byte, prev_line_no) = (start_byte, line_no)
|
(_, prev_line_no) = (start_byte, line_no)
|
||||||
while j < n:
|
while j < n:
|
||||||
self.lines.append(linetuple(prev_line_no, n))
|
self.lines.append(linetuple(prev_line_no, n))
|
||||||
j+=1
|
j+=1
|
||||||
@@ -122,7 +122,7 @@ class Scanner27(scan.Scanner):
|
|||||||
oparg = self.get_argument(offset) + extended_arg
|
oparg = self.get_argument(offset) + extended_arg
|
||||||
extended_arg = 0
|
extended_arg = 0
|
||||||
if op == EXTENDED_ARG:
|
if op == EXTENDED_ARG:
|
||||||
extended_arg = oparg * 65536
|
extended_arg = oparg * scan.L65536
|
||||||
continue
|
continue
|
||||||
if op in hasconst:
|
if op in hasconst:
|
||||||
const = co.co_consts[oparg]
|
const = co.co_consts[oparg]
|
||||||
|
Reference in New Issue
Block a user