From 5dc3af3238404f917724802cfd0ac15b6e66dae6 Mon Sep 17 00:00:00 2001 From: rocky Date: Mon, 21 Dec 2015 06:39:01 -0500 Subject: [PATCH] Start slice and build list on Python3. Do sanity check on marshal load of code. --- test/bytecode_2.7/01-slice.pyc | Bin 0 -> 166 bytes test/bytecode_3.4/01-slice.pyc | Bin 0 -> 142 bytes uncompyle6/__init__.py | 4 +--- uncompyle6/marsh.py | 9 ++++++++- uncompyle6/parsers/parse2.py | 9 +++++---- uncompyle6/parsers/parse3.py | 26 +++++++++++++++++++------- 6 files changed, 33 insertions(+), 15 deletions(-) create mode 100644 test/bytecode_2.7/01-slice.pyc create mode 100644 test/bytecode_3.4/01-slice.pyc diff --git a/test/bytecode_2.7/01-slice.pyc b/test/bytecode_2.7/01-slice.pyc new file mode 100644 index 0000000000000000000000000000000000000000..4410ecad623c9a71a333a6a17dd87d3f14849e5a GIT binary patch literal 166 zcmZSn%*$naygV$K0ScIbv;zrRygW>dfq~&M5W@j8kmUfx#fm^81&A0KQkWRhnHi!O7*c^;W{^lL6N4lp zkQuDW@{$p#?j;k50BQBpWQ<}?EUH||P{ab{f{9<;28O!DIho0+dIgoYIBatBQ%ZAE M?LZofK^plP0W4M+_y7O^ literal 0 HcmV?d00001 diff --git a/uncompyle6/__init__.py b/uncompyle6/__init__.py index 0e622162..678ad9e1 100644 --- a/uncompyle6/__init__.py +++ b/uncompyle6/__init__.py @@ -52,8 +52,6 @@ import uncompyle6.semantics.pysource import uncompyle6.semantics.fragments # Conventience functions so you can say: -# from uncompyle6 import deparse_code and -# from uncompyle6 import deparse_code_fragments +# from uncompyle6 import deparse_code deparse_code = uncompyle6.semantics.pysource.deparse_code -deparse_fragments = uncompyle6.semantics.fragments.deparse_code diff --git a/uncompyle6/marsh.py b/uncompyle6/marsh.py index 0a4b7629..79772e5d 100644 --- a/uncompyle6/marsh.py +++ b/uncompyle6/marsh.py @@ -41,12 +41,19 @@ def load_code(fp, magic_int): """ global internStrings internStrings = [] + seek_pos = fp.tell() + # Do a sanity check. Is this a code type? + if fp.read(1).decode('utf-8') != 'c': + raise TypeError("File %s doesn't smell like Python bytecode" % fp.name) + + fp.seek(seek_pos) return load_code_internal(fp, magic_int) def load_code_internal(fp, magic_int, bytes_for_s=False): global internStrings - marshalType = fp.read(1).decode('utf-8') + b1 = fp.read(1) + marshalType = b1.decode('utf-8') if marshalType == 'c': Code = types.CodeType diff --git a/uncompyle6/parsers/parse2.py b/uncompyle6/parsers/parse2.py index 52924520..1acb5fd2 100644 --- a/uncompyle6/parsers/parse2.py +++ b/uncompyle6/parsers/parse2.py @@ -650,11 +650,12 @@ class Python2Parser(PythonParser): Special handling for opcodes that take a variable number of arguments -- we add a new rule for each: - expr ::= {expr}^n BUILD_LIST_n - expr ::= {expr}^n BUILD_TUPLE_n + build_list ::= {expr}^n BUILD_TUPLE_n + build_list ::= {expr}^n BUILD_LIST_n unpack_list ::= UNPACK_LIST {expr}^n - unpack ::= UNPACK_TUPLE {expr}^n - unpack ::= UNPACK_SEQEUENE {expr}^n + unpack ::= UNPACK_TUPLE {expr}^n + unpack ::= UNPACK_SEQEUENCE {expr}^n + mkfunc ::= {expr}^n LOAD_CONST MAKE_FUNCTION_n mklambda ::= {expr}^n LOAD_LAMBDA MAKE_FUNCTION_n mkfunc ::= {expr}^n load_closure LOAD_CONST MAKE_FUNCTION_n diff --git a/uncompyle6/parsers/parse3.py b/uncompyle6/parsers/parse3.py index af59a959..ba4217ca 100644 --- a/uncompyle6/parsers/parse3.py +++ b/uncompyle6/parsers/parse3.py @@ -26,7 +26,6 @@ class Python3Parser(PythonParser): def __init__(self): self.added_rules = set() GenericASTBuilder.__init__(self, AST, 'stmts') - self.customized = {} self.new_rules = set() def add_unique_rule(self, rule, opname, count, customize): @@ -36,6 +35,7 @@ class Python3Parser(PythonParser): self.new_rules.add(rule) self.addRule(rule, nop_func) customize[opname] = count + # print("XXXX" , rule) pass return @@ -666,11 +666,12 @@ class Python3Parser(PythonParser): Special handling for opcodes that take a variable number of arguments -- we add a new rule for each: - expr ::= {expr}^n BUILD_LIST_n - expr ::= {expr}^n BUILD_TUPLE_n + build_list ::= {expr}^n BUILD_LIST_n + build_list ::= {expr}^n BUILD_TUPLE_n unpack_list ::= UNPACK_LIST {expr}^n unpack ::= UNPACK_TUPLE {expr}^n - unpack ::= UNPACK_SEQEUENE {expr}^n + unpack ::= UNPACK_SEQEUENCE {expr}^n + mkfunc ::= {expr}^n LOAD_CONST MAKE_FUNCTION_n mklambda ::= {expr}^n LOAD_LAMBDA MAKE_FUNCTION_n mkfunc ::= {expr}^n load_closure LOAD_CONST MAKE_FUNCTION_n @@ -679,8 +680,13 @@ class Python3Parser(PythonParser): expr ::= expr {expr}^n CALL_FUNCTION_VAR_KW_n POP_TOP expr ::= expr {expr}^n CALL_FUNCTION_KW_n POP_TOP """ + + # from trepan.api import debug + # debug(start_opts={'startup-profile': True}) for token in tokens: opname = token.type + opname_base = opname[:opname.rfind('_')] + if opname in ('CALL_FUNCTION', 'CALL_FUNCTION_VAR', 'CALL_FUNCTION_VAR_KW', 'CALL_FUNCTION_KW'): # Low byte indicates number of positional paramters, @@ -694,9 +700,15 @@ class Python3Parser(PythonParser): + ('kwarg ' * args_kw) + 'expr ' * nak + token.type) self.add_unique_rule(rule, token.type, args_pos, customize) - elif opname.startswith('MAKE_FUNCTION_'): - # from trepan.api import debug - # debug(start_opts={'startup-profile': True}) + elif opname_base in ('BUILD_LIST', 'BUILD_TUPLE', 'BUILD_SET'): + rule = 'build_list ::= ' + 'expr ' * token.attr + opname + self.add_unique_rule(rule, opname, token.attr, customize) + elif opname_base in ('UNPACK_TUPLE', 'UNPACK_SEQUENCE'): + rule = 'unpack ::= ' + opname + ' designator' * token.attr + self.add_unique_rule(rule, opname, token.attr, customize) + elif opname_base == 'UNPACK_LIST': + rule = 'unpack_list ::= ' + opname + ' designator' * token.attr + elif opname_base == ('MAKE_FUNCTION'): self.addRule('mklambda ::= %s LOAD_LAMBDA %s' % ('expr ' * token.attr, opname), nop_func) rule = 'mkfunc ::= %s LOAD_CONST LOAD_CONST %s' % ('expr ' * token.attr, opname)