You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-02 16:44:46 +08:00
NT funcdef -> function_def to match AST
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# From 2.6.9 abc.py
|
||||
|
||||
# For 2.6:
|
||||
# genexpr_func ::= setup_loop_lf FOR_ITER designator comp_iter JUMP_BACK come_from_pop JUMP_BACK POP_BLOCK COME_FROM
|
||||
# genexpr_func ::= setup_loop_lf FOR_ITER store comp_iter JUMP_BACK come_from_pop JUMP_BACK POP_BLOCK COME_FROM
|
||||
|
||||
# This has been a bug in other Pythons after 2.6 were set comprehension {} is used instead of set().
|
||||
abstracts = set(name
|
||||
|
@@ -3,7 +3,7 @@
|
||||
# Python2 grammar includes:
|
||||
# list_compr ::= BUILD_LIST_0 list_iter
|
||||
# list_iter ::= list_for
|
||||
# list_for ::= expr _for designator list_iter JUMP_BACK
|
||||
# list_for ::= expr _for store list_iter JUMP_BACK
|
||||
# list_iter ::= lc_body
|
||||
# lc_body ::= expr LIST_APPEND
|
||||
#
|
||||
|
@@ -5,7 +5,7 @@
|
||||
# and ::= expr jmp_false expr \e__come_from
|
||||
# expr ::= list_compr
|
||||
# list_iter ::= list_for
|
||||
# list_for ::= expr _for designator list_iter JUMP_BACK
|
||||
# list_for ::= expr _for store list_iter JUMP_BACK
|
||||
# list_iter ::= lc_body
|
||||
# lc_body ::= expr LIST_APPEND
|
||||
|
||||
@@ -19,9 +19,9 @@
|
||||
# Python2:
|
||||
# list_compr ::= BUILD_LIST_0 list_iter
|
||||
# list_iter ::= list_for
|
||||
# list_for ::= expr _for designator list_iter JUMP_BACK
|
||||
# list_for ::= expr _for store list_iter JUMP_BACK
|
||||
# list_iter ::= list_for
|
||||
# list_for ::= expr _for designator list_iter JUMP_BACK
|
||||
# list_for ::= expr _for store list_iter JUMP_BACK
|
||||
# list_iter ::= lc_body
|
||||
# lc_body ::= expr LIST_APPEND
|
||||
# [ i * j for i in range(4) for j in range(7) ]
|
||||
|
@@ -6,7 +6,7 @@
|
||||
# 76 JUMP_ABSOLUTE 17 (to 17)
|
||||
|
||||
# And getting:
|
||||
# list_for ::= expr _for designator list_iter JUMP_BACK
|
||||
# list_for ::= expr _for store list_iter JUMP_BACK
|
||||
# list_iter ::= list_if JUMP_BACK
|
||||
# ^^^^^^^^^ added to 2.6 grammar
|
||||
# list_iter ::= list_for
|
||||
|
@@ -1,11 +1,11 @@
|
||||
# Tests:
|
||||
#
|
||||
# For Python3:
|
||||
# classdef ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST CALL_FUNCTION_2 designator
|
||||
# classdef ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST CALL_FUNCTION_2 store
|
||||
# mkfunc ::= LOAD_CONST LOAD_CONST MAKE_FUNCTION_0
|
||||
|
||||
# For Python2:
|
||||
# classdef ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS designator
|
||||
# classdef ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS store
|
||||
# mkfunc ::= LOAD_CONST MAKE_FUNCTION_0
|
||||
|
||||
class A:
|
||||
|
@@ -1,15 +1,15 @@
|
||||
# Tests
|
||||
# Python3:
|
||||
# funcdef ::= mkfunc designator
|
||||
# designator ::= STORE_DEREF
|
||||
# function_def ::= mkfunc store
|
||||
# store ::= STORE_DEREF
|
||||
# mkfunc ::= load_closure BUILD_TUPLE_1 LOAD_CONST LOAD_CONST MAKE_CLOSURE_0
|
||||
# load_closure ::= LOAD_CLOSURE
|
||||
#
|
||||
# Python2:
|
||||
|
||||
# funcdef ::= mkfunc designator
|
||||
# designator ::= STORE_DEREF
|
||||
# mkfunc ::= load_closure LOAD_CONST MAKE_CLOSURE_0
|
||||
# function_def ::= mkfunc store
|
||||
# store ::= STORE_DEREF
|
||||
# mkfunc ::= load_closure LOAD_CONST MAKE_CLOSURE_0
|
||||
# load_closure ::= LOAD_CLOSURE
|
||||
|
||||
|
||||
|
@@ -1,12 +1,12 @@
|
||||
# Tests:
|
||||
# importstmt ::= LOAD_CONST LOAD_CONST import_as
|
||||
# import_as ::= IMPORT_NAME designator
|
||||
# import_as ::= IMPORT_NAME store
|
||||
|
||||
# Since Python 3.3:
|
||||
# classdef ::= buildclass designator
|
||||
# designator ::= STORE_NAME
|
||||
# classdef ::= buildclass store
|
||||
# store ::= STORE_NAME
|
||||
# buildclass ::= LOAD_BUILD_CLASS mkfunc LOAD_CONST expr CALL_FUNCTION_3
|
||||
# mkfunc ::= LOAD_CONST LOAD_CONST MAKE_FUNCTION_0
|
||||
# mkfunc ::= LOAD_CONST LOAD_CONST MAKE_FUNCTION_0
|
||||
|
||||
import io
|
||||
class BZ2File(io.BufferedIOBase):
|
||||
|
@@ -12,9 +12,9 @@
|
||||
# mkfunc ::= load_closure LOAD_CONST MAKE_CLOSURE_0
|
||||
|
||||
# mkfuncdeco0 ::= mkfunc
|
||||
# mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
|
||||
# designator ::= STORE_FAST
|
||||
# funcdefdeco ::= mkfuncdeco designator
|
||||
# mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
|
||||
# store ::= STORE_FAST
|
||||
# funcdefdeco ::= mkfuncdeco store
|
||||
# stmt ::= funcdefdeco
|
||||
|
||||
|
||||
|
@@ -12,9 +12,9 @@
|
||||
# mkfunc ::= load_closure LOAD_CONST MAKE_CLOSURE_0
|
||||
|
||||
# mkfuncdeco0 ::= mkfunc
|
||||
# mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
|
||||
# designator ::= STORE_FAST
|
||||
# funcdefdeco ::= mkfuncdeco designator
|
||||
# mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
|
||||
# store ::= STORE_FAST
|
||||
# funcdefdeco ::= mkfuncdeco store
|
||||
# stmt ::= funcdefdeco
|
||||
from functools import wraps
|
||||
|
||||
|
@@ -5,8 +5,8 @@
|
||||
# mkfuncdeco0 ::= mkfunc
|
||||
# classdefdeco2 ::= LOAD_CONST expr mkfunc CALL_FUNCTION_0 BUILD_CLASS
|
||||
# classdefdeco1 ::= expr classdefdeco1 CALL_FUNCTION_1
|
||||
# designator ::= STORE_NAME
|
||||
# classdefdeco ::= classdefdeco1 designator
|
||||
# store ::= STORE_NAME
|
||||
# classdefdeco ::= classdefdeco1 store
|
||||
|
||||
def author(*author_names):
|
||||
def author_func(cls):
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# Tests:
|
||||
# forstmt ::= SETUP_LOOP expr _for designator for_block POP_BLOCK COME_FROM
|
||||
# forstmt ::= SETUP_LOOP expr _for store for_block POP_BLOCK COME_FROM
|
||||
# for_block ::= l_stmts_opt JUMP_BACK
|
||||
# trystmt ::= SETUP_EXCEPT suite_stmts_opt POP_BLOCK try_middle COME_FROM
|
||||
# try_middle ::= jmp_abs COME_FROM except_stmts END_FINALLY
|
||||
|
@@ -4,16 +4,16 @@
|
||||
# get_iter ::= expr GET_ITER
|
||||
# expr ::= get_iter
|
||||
# _for ::= GET_ITER FOR_ITER
|
||||
# designator ::= STORE_FAST
|
||||
# store ::= STORE_FAST
|
||||
# expr ::= LOAD_FAST
|
||||
# yield ::= expr YIELD_VALUE
|
||||
# expr ::= yield
|
||||
# gen_comp_body ::= expr YIELD_VALUE POP_TOP
|
||||
# comp_body ::= gen_comp_body
|
||||
# comp_iter ::= comp_body
|
||||
# comp_for ::= expr _for designator comp_iter JUMP_BACK
|
||||
# comp_for ::= expr _for store comp_iter JUMP_BACK
|
||||
# comp_iter ::= comp_for
|
||||
# genexpr_func ::= LOAD_FAST FOR_ITER designator comp_iter JUMP_BACK
|
||||
# genexpr_func ::= LOAD_FAST FOR_ITER store comp_iter JUMP_BACK
|
||||
|
||||
def multi_genexpr(blog_posts):
|
||||
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# Tests:
|
||||
# forstmt ::= SETUP_LOOP expr _for designator
|
||||
# forstmt ::= SETUP_LOOP expr _for store
|
||||
# for_block POP_BLOCK COME_FROM
|
||||
for a in [1]:
|
||||
c = 2
|
||||
|
@@ -1,5 +1,5 @@
|
||||
# Tests:
|
||||
# assign ::= expr designator
|
||||
# assign ::= expr store
|
||||
|
||||
a = 'None'
|
||||
b = None
|
||||
|
@@ -1,3 +1,3 @@
|
||||
# Tests:
|
||||
# assign ::= expr designator
|
||||
# assign ::= expr store
|
||||
pass
|
||||
|
@@ -2,7 +2,7 @@
|
||||
# Bug in 2.6.9 was handling with as. Added rules
|
||||
|
||||
#
|
||||
# withasstmt ::= expr setupwithas designator suite_stmts_opt
|
||||
# withasstmt ::= expr setupwithas store suite_stmts_opt
|
||||
# POP_BLOCK LOAD_CONST COME_FROM WITH_CLEANUP END_FINALLY
|
||||
# setupwithas ::= DUP_TOP LOAD_ATTR ROT_TWO LOAD_ATTR CALL_FUNCTION_0 STORE_FAST
|
||||
# SETUP_FINALLY LOAD_FAST DELETE_FAST
|
||||
|
@@ -169,8 +169,8 @@ class PythonParser(GenericASTBuilder):
|
||||
return GenericASTBuilder.ambiguity(self, children)
|
||||
|
||||
def resolve(self, list):
|
||||
if len(list) == 2 and 'funcdef' in list and 'assign' in list:
|
||||
return 'funcdef'
|
||||
if len(list) == 2 and 'function_def' in list and 'assign' in list:
|
||||
return 'function_def'
|
||||
if 'grammar' in list and 'expr' in list:
|
||||
return 'expr'
|
||||
# print >> sys.stderr, 'resolve', str(list)
|
||||
@@ -280,15 +280,15 @@ class PythonParser(GenericASTBuilder):
|
||||
"""
|
||||
pass
|
||||
|
||||
def p_funcdef(self, args):
|
||||
def p_function_def(self, args):
|
||||
'''
|
||||
stmt ::= funcdef
|
||||
funcdef ::= mkfunc store
|
||||
stmt ::= funcdefdeco
|
||||
funcdefdeco ::= mkfuncdeco store
|
||||
mkfuncdeco ::= expr mkfuncdeco CALL_FUNCTION_1
|
||||
mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
|
||||
mkfuncdeco0 ::= mkfunc
|
||||
stmt ::= function_def
|
||||
function_def ::= mkfunc store
|
||||
stmt ::= funcdefdeco
|
||||
funcdefdeco ::= mkfuncdeco store
|
||||
mkfuncdeco ::= expr mkfuncdeco CALL_FUNCTION_1
|
||||
mkfuncdeco ::= expr mkfuncdeco0 CALL_FUNCTION_1
|
||||
mkfuncdeco0 ::= mkfunc
|
||||
load_closure ::= load_closure LOAD_CLOSURE
|
||||
load_closure ::= LOAD_CLOSURE
|
||||
'''
|
||||
|
@@ -285,8 +285,8 @@ class Python3Parser(PythonParser):
|
||||
def p_def_annotations3(self, args):
|
||||
"""
|
||||
# Annotated functions
|
||||
stmt ::= funcdef_annotate
|
||||
funcdef_annotate ::= mkfunc_annotate store
|
||||
stmt ::= function_def_annotate
|
||||
function_def_annotate ::= mkfunc_annotate store
|
||||
|
||||
mkfuncdeco0 ::= mkfunc_annotate
|
||||
|
||||
|
@@ -196,12 +196,12 @@ TABLE_DIRECT = {
|
||||
'compare_chained1': ( '%[3]{pattr} %p %p', (0, 19), (-2, 19)),
|
||||
'compare_chained2': ( '%[1]{pattr} %p', (0, 19)),
|
||||
# 'classdef': (), # handled by n_classdef()
|
||||
'funcdef': ( '\n\n%|def %c\n', -2), # -2 to handle closures
|
||||
'funcdefdeco': ( '\n\n%c', 0),
|
||||
'function_def': ( '\n\n%|def %c\n', -2), # -2 to handle closures
|
||||
'funcdefdeco': ( '\n\n%c', 0),
|
||||
'mkfuncdeco': ( '%|@%c\n%c', 0, 1),
|
||||
'mkfuncdeco0': ( '%|def %c\n', 0),
|
||||
'classdefdeco': ( '\n\n%c', 0),
|
||||
'classdefdeco1': ( '%|@%c\n%c', 0, 1),
|
||||
'mkfuncdeco0': ( '%|def %c\n', 0),
|
||||
'classdefdeco': ( '\n\n%c', 0),
|
||||
'classdefdeco1': ( '%|@%c\n%c', 0, 1),
|
||||
'kwarg': ( '%[0]{pattr}=%c', 1),
|
||||
'kwargs': ( '%D', (0, maxint, ', ') ),
|
||||
|
||||
|
@@ -361,7 +361,7 @@ class SourceWalker(GenericASTTraversal, object):
|
||||
|
||||
if version >= 3.0:
|
||||
TABLE_DIRECT.update({
|
||||
'funcdef_annotate': ( '\n\n%|def %c%c\n', -1, 0),
|
||||
'function_def_annotate': ( '\n\n%|def %c%c\n', -1, 0),
|
||||
'store_locals': ( '%|# inspect.currentframe().f_locals = __locals__\n', ),
|
||||
})
|
||||
|
||||
@@ -386,7 +386,7 @@ class SourceWalker(GenericASTTraversal, object):
|
||||
break
|
||||
|
||||
# FIXME: the real situation is that when derived from
|
||||
# funcdef_annotate we the name has been filled in.
|
||||
# function_def_annotate we the name has been filled in.
|
||||
# But when derived from funcdefdeco it hasn't Would like a better
|
||||
# way to distinquish.
|
||||
if self.f.getvalue()[-4:] == 'def ':
|
||||
@@ -474,7 +474,7 @@ class SourceWalker(GenericASTTraversal, object):
|
||||
self.default(node)
|
||||
self.n_call = n_call
|
||||
|
||||
def n_funcdef(node):
|
||||
def n_function_def(node):
|
||||
if self.version == 3.6:
|
||||
code_node = node[0][0]
|
||||
else:
|
||||
@@ -489,7 +489,7 @@ class SourceWalker(GenericASTTraversal, object):
|
||||
self.template_engine(('\n\n%|def %c\n', -2),
|
||||
node)
|
||||
self.prune()
|
||||
self.n_funcdef = n_funcdef
|
||||
self.n_function_def = n_function_def
|
||||
|
||||
def n_unmapexpr(node):
|
||||
last_n = node[0][-1]
|
||||
|
Reference in New Issue
Block a user