You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 09:22:40 +08:00
Python 3 build class parsing
This commit is contained in:
BIN
test/bytecode_3.5/07_classparam.pyc
Normal file
BIN
test/bytecode_3.5/07_classparam.pyc
Normal file
Binary file not shown.
6
test/simple_source/def/07_classparam.py
Normal file
6
test/simple_source/def/07_classparam.py
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Tests Python 3:
|
||||||
|
# build_class ::= LOAD_BUILD_CLASS mkfunc expr call_function CALL_FUNCTION_3
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
|
class Event(namedtuple('Event', 'time, priority, action, argument')):
|
||||||
|
pass
|
@@ -393,9 +393,14 @@ class Python3Parser(PythonParser):
|
|||||||
|
|
||||||
def custom_build_class_rule(self, opname, i, token, tokens, customize):
|
def custom_build_class_rule(self, opname, i, token, tokens, customize):
|
||||||
"""
|
"""
|
||||||
|
# Should the first rule be somehow folded into the 2nd one?
|
||||||
build_class ::= LOAD_BUILD_CLASS mkfunc
|
build_class ::= LOAD_BUILD_CLASS mkfunc
|
||||||
LOAD_CLASSNAME {expr}^n CALL_FUNCTION_n+2
|
LOAD_CLASSNAME {expr}^n CALL_FUNCTION_n+2
|
||||||
LOAD_CONST CALL_FUNCTION_n
|
LOAD_CONST CALL_FUNCTION_n
|
||||||
|
build_class ::= LOAD_BUILD_CLASS mkfunc
|
||||||
|
expr
|
||||||
|
call_function
|
||||||
|
CALL_FUNCTION_3
|
||||||
"""
|
"""
|
||||||
# FIXME: I bet this can be simplified
|
# FIXME: I bet this can be simplified
|
||||||
# look for next MAKE_FUNCTION
|
# look for next MAKE_FUNCTION
|
||||||
@@ -405,14 +410,14 @@ class Python3Parser(PythonParser):
|
|||||||
pass
|
pass
|
||||||
assert i < len(tokens), "build_class needs to find MAKE_FUNCTION"
|
assert i < len(tokens), "build_class needs to find MAKE_FUNCTION"
|
||||||
assert tokens[i+1].type == 'LOAD_CONST', \
|
assert tokens[i+1].type == 'LOAD_CONST', \
|
||||||
"build_class expecing CONST after MAKE_FUNCTION"
|
"build_class expecting CONST after MAKE_FUNCTION"
|
||||||
for i in range(i, len(tokens)):
|
for i in range(i, len(tokens)):
|
||||||
if tokens[i].type == 'CALL_FUNCTION':
|
if tokens[i].type == 'CALL_FUNCTION':
|
||||||
call_fn_tok = tokens[i]
|
call_fn_tok = tokens[i]
|
||||||
break
|
break
|
||||||
assert call_fn_tok, "build_class custom rule needs to find CALL_FUNCTION"
|
assert call_fn_tok, "build_class custom rule needs to find CALL_FUNCTION"
|
||||||
|
|
||||||
# customize CALL_FUNCTION
|
# customize build_class rule
|
||||||
call_function = self.call_fn_name(call_fn_tok)
|
call_function = self.call_fn_name(call_fn_tok)
|
||||||
args_pos = call_fn_tok.attr & 0xff
|
args_pos = call_fn_tok.attr & 0xff
|
||||||
args_kw = (call_fn_tok.attr >> 8) & 0xff
|
args_kw = (call_fn_tok.attr >> 8) & 0xff
|
||||||
@@ -420,6 +425,11 @@ class Python3Parser(PythonParser):
|
|||||||
"%s" % (('expr ' * (args_pos - 1) + ('kwarg ' * args_kw)),
|
"%s" % (('expr ' * (args_pos - 1) + ('kwarg ' * args_kw)),
|
||||||
call_function))
|
call_function))
|
||||||
self.add_unique_rule(rule, opname, token.attr, customize)
|
self.add_unique_rule(rule, opname, token.attr, customize)
|
||||||
|
|
||||||
|
# Can the above build_class rule be folded into this rule?
|
||||||
|
rule = ("build_class ::= LOAD_BUILD_CLASS mkfunc expr call_function "
|
||||||
|
"CALL_FUNCTION_" + str(args_pos+1))
|
||||||
|
self.add_unique_rule(rule, opname, token.attr, customize)
|
||||||
return
|
return
|
||||||
|
|
||||||
def custom_classfunc_rule(self, opname, token, customize):
|
def custom_classfunc_rule(self, opname, token, customize):
|
||||||
|
Reference in New Issue
Block a user