MCPcopy
hub / github.com/rocky/python-uncompyle6 / make_function36

Function make_function36

uncompyle6/semantics/make_function36.py:37–379  ·  view source on GitHub ↗

Dump function definition, doc string, and function body in Python version 3.6 and above.

(self, node, is_lambda, nested=1, code_node=None)

Source from the content-addressed store, hash-verified

35
36
37def make_function36(self, node, is_lambda, nested=1, code_node=None):
38 """Dump function definition, doc string, and function body in
39 Python version 3.6 and above.
40 """
41
42 # MAKE_CLOSURE adds a closure slot
43
44 # In Python 3.6 and above stack change again. I understand
45 # 3.7 changes some of those changes, although I don't
46 # see it in this code yet. Yes, it is hard to follow,
47 # and I am sure I haven't been able to keep up.
48
49 # Thank you, Python.
50
51 def build_param(ast, name, default, annotation=None):
52 """build parameters:
53 - handle defaults
54 - handle format tuple parameters
55 """
56 value = default
57 if annotation:
58 result = "%s: %s=%s" % (name, annotation, value)
59 else:
60 result = "%s=%s" % (name, value)
61
62 # The below can probably be removed. This is probably
63 # a holdover from days when LOAD_CONST erroneously
64 # didn't handle LOAD_CONST None properly
65 if result[-2:] == "= ": # default was 'LOAD_CONST None'
66 result += "None"
67
68 return result
69
70 # MAKE_FUNCTION_... or MAKE_CLOSURE_...
71 assert node[-1].kind.startswith("MAKE_")
72
73 # Python 3.3+ adds a qualified name at TOS (-1)
74 # moving down the LOAD_LAMBDA instruction
75 lambda_index = -3
76
77 args_node = node[-1]
78
79 annotate_dict = {}
80
81 # Get a list of tree nodes that constitute the values for the "default
82 # parameters"; these are default values that appear before any *, and are
83 # not to be confused with keyword parameters which may appear after *.
84 args_attr = args_node.attr
85
86 if len(args_attr) == 3:
87 _, kw_args, annotate_argc = args_attr
88 else:
89 _, kw_args, annotate_argc, closure = args_attr
90
91 if node[-2] != "docstring":
92 i = -4
93 else:
94 i = -5

Callers 1

make_functionMethod · 0.90

Calls 14

CodeClass · 0.90
find_all_globalsFunction · 0.90
find_noneFunction · 0.90
build_paramFunction · 0.70
traverseMethod · 0.45
build_astMethod · 0.45
writeMethod · 0.45
appendMethod · 0.45
getMethod · 0.45
joinMethod · 0.45
printlnMethod · 0.45

Tested by

no test coverage detected