You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
Reformat (via black) make_function.py
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
# Copyright (c) 2015-2018 by Rocky Bernstein
|
# Copyright (c) 2015-2019 by Rocky Bernstein
|
||||||
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
|
# Copyright (c) 2000-2002 by hartmut Goebel <h.goebel@crazy-compilers.com>
|
||||||
#
|
#
|
||||||
# This program is free software: you can redistribute it and/or modify
|
# This program is free software: you can redistribute it and/or modify
|
||||||
@@ -23,8 +23,11 @@ from uncompyle6 import PYTHON3
|
|||||||
from uncompyle6.semantics.parser_error import ParserError
|
from uncompyle6.semantics.parser_error import ParserError
|
||||||
from uncompyle6.parser import ParserError as ParserError2
|
from uncompyle6.parser import ParserError as ParserError2
|
||||||
from uncompyle6.semantics.helper import (
|
from uncompyle6.semantics.helper import (
|
||||||
print_docstring, find_all_globals, find_globals_and_nonlocals, find_none
|
print_docstring,
|
||||||
)
|
find_all_globals,
|
||||||
|
find_globals_and_nonlocals,
|
||||||
|
find_none,
|
||||||
|
)
|
||||||
|
|
||||||
if PYTHON3:
|
if PYTHON3:
|
||||||
from itertools import zip_longest
|
from itertools import zip_longest
|
||||||
@@ -35,8 +38,9 @@ from uncompyle6.show import maybe_show_tree_param_default
|
|||||||
|
|
||||||
# FIXME: DRY the below code...
|
# FIXME: DRY the below code...
|
||||||
|
|
||||||
def make_function3_annotate(self, node, is_lambda, nested=1,
|
def make_function3_annotate(
|
||||||
code_node=None, annotate_last=-1):
|
self, node, is_lambda, nested=1, code_node=None, annotate_last=-1
|
||||||
|
):
|
||||||
"""
|
"""
|
||||||
Dump function defintion, doc string, and function
|
Dump function defintion, doc string, and function
|
||||||
body. This code is specialized for Python 3"""
|
body. This code is specialized for Python 3"""
|
||||||
@@ -47,33 +51,35 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
- handle format tuple parameters
|
- handle format tuple parameters
|
||||||
"""
|
"""
|
||||||
if default:
|
if default:
|
||||||
value = self.traverse(default, indent='')
|
value = self.traverse(default, indent="")
|
||||||
maybe_show_tree_param_default(self, name, value)
|
maybe_show_tree_param_default(self, name, value)
|
||||||
result = '%s=%s' % (name, value)
|
result = "%s=%s" % (name, value)
|
||||||
if result[-2:] == '= ': # default was 'LOAD_CONST None'
|
if result[-2:] == "= ": # default was 'LOAD_CONST None'
|
||||||
result += 'None'
|
result += "None"
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
return name
|
return name
|
||||||
|
|
||||||
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
|
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
|
||||||
assert node[-1].kind.startswith('MAKE_')
|
assert node[-1].kind.startswith("MAKE_")
|
||||||
|
|
||||||
annotate_tuple = None
|
annotate_tuple = None
|
||||||
for annotate_last in range(len(node)-1, -1, -1):
|
for annotate_last in range(len(node) - 1, -1, -1):
|
||||||
if node[annotate_last] == 'annotate_tuple':
|
if node[annotate_last] == "annotate_tuple":
|
||||||
annotate_tuple = node[annotate_last]
|
annotate_tuple = node[annotate_last]
|
||||||
break
|
break
|
||||||
annotate_args = {}
|
annotate_args = {}
|
||||||
|
|
||||||
if (annotate_tuple == 'annotate_tuple'
|
if (
|
||||||
and annotate_tuple[0] in ('LOAD_CONST', 'LOAD_NAME')
|
annotate_tuple == "annotate_tuple"
|
||||||
and isinstance(annotate_tuple[0].attr, tuple)):
|
and annotate_tuple[0] in ("LOAD_CONST", "LOAD_NAME")
|
||||||
|
and isinstance(annotate_tuple[0].attr, tuple)
|
||||||
|
):
|
||||||
annotate_tup = annotate_tuple[0].attr
|
annotate_tup = annotate_tuple[0].attr
|
||||||
i = -1
|
i = -1
|
||||||
j = annotate_last-1
|
j = annotate_last - 1
|
||||||
l = -len(node)
|
l = -len(node)
|
||||||
while j >= l and node[j].kind in ('annotate_arg', 'annotate_tuple'):
|
while j >= l and node[j].kind in ("annotate_arg", "annotate_tuple"):
|
||||||
annotate_args[annotate_tup[i]] = node[j][0]
|
annotate_args[annotate_tup[i]] = node[j][0]
|
||||||
i -= 1
|
i -= 1
|
||||||
j -= 1
|
j -= 1
|
||||||
@@ -81,20 +87,20 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
args_node = node[-1]
|
args_node = node[-1]
|
||||||
if isinstance(args_node.attr, tuple):
|
if isinstance(args_node.attr, tuple):
|
||||||
# positional args are before kwargs
|
# positional args are before kwargs
|
||||||
defparams = node[:args_node.attr[0]]
|
defparams = node[: args_node.attr[0]]
|
||||||
pos_args, kw_args, annotate_argc = args_node.attr
|
pos_args, kw_args, annotate_argc = args_node.attr
|
||||||
if 'return' in annotate_args.keys():
|
if "return" in annotate_args.keys():
|
||||||
annotate_argc = len(annotate_args) - 1
|
annotate_argc = len(annotate_args) - 1
|
||||||
else:
|
else:
|
||||||
defparams = node[:args_node.attr]
|
defparams = node[: args_node.attr]
|
||||||
kw_args = 0
|
kw_args = 0
|
||||||
annotate_argc = 0
|
annotate_argc = 0
|
||||||
pass
|
pass
|
||||||
|
|
||||||
annotate_dict = {}
|
annotate_dict = {}
|
||||||
|
|
||||||
for name in annotate_args.keys():
|
for name in annotate_args.keys():
|
||||||
n = self.traverse(annotate_args[name], indent='')
|
n = self.traverse(annotate_args[name], indent="")
|
||||||
annotate_dict[name] = n
|
annotate_dict[name] = n
|
||||||
|
|
||||||
if 3.0 <= self.version <= 3.2:
|
if 3.0 <= self.version <= 3.2:
|
||||||
@@ -105,7 +111,7 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
lambda_index = None
|
lambda_index = None
|
||||||
|
|
||||||
if lambda_index and is_lambda and iscode(node[lambda_index].attr):
|
if lambda_index and is_lambda and iscode(node[lambda_index].attr):
|
||||||
assert node[lambda_index].kind == 'LOAD_LAMBDA'
|
assert node[lambda_index].kind == "LOAD_LAMBDA"
|
||||||
code = node[lambda_index].attr
|
code = node[lambda_index].attr
|
||||||
else:
|
else:
|
||||||
code = code_node.attr
|
code = code_node.attr
|
||||||
@@ -119,13 +125,15 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
|
|
||||||
paramnames = list(code.co_varnames[:argc])
|
paramnames = list(code.co_varnames[:argc])
|
||||||
if kwonlyargcount > 0:
|
if kwonlyargcount > 0:
|
||||||
kwargs = list(code.co_varnames[argc:argc+kwonlyargcount])
|
kwargs = list(code.co_varnames[argc : argc + kwonlyargcount])
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ast = self.build_ast(code._tokens,
|
ast = self.build_ast(
|
||||||
code._customize,
|
code._tokens,
|
||||||
is_lambda = is_lambda,
|
code._customize,
|
||||||
noneInNames = ('None' in code.co_names))
|
is_lambda=is_lambda,
|
||||||
|
noneInNames=("None" in code.co_names),
|
||||||
|
)
|
||||||
except (ParserError, ParserError2) as p:
|
except (ParserError, ParserError2) as p:
|
||||||
self.write(str(p))
|
self.write(str(p))
|
||||||
if not self.tolerate_errors:
|
if not self.tolerate_errors:
|
||||||
@@ -142,18 +150,18 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
|
|
||||||
last_line = self.f.getvalue().split("\n")[-1]
|
last_line = self.f.getvalue().split("\n")[-1]
|
||||||
l = len(last_line)
|
l = len(last_line)
|
||||||
indent = ' ' * l
|
indent = " " * l
|
||||||
line_number = self.line_number
|
line_number = self.line_number
|
||||||
|
|
||||||
i = len(paramnames) - len(defparams)
|
i = len(paramnames) - len(defparams)
|
||||||
suffix = ''
|
suffix = ""
|
||||||
|
|
||||||
for param in paramnames[:i]:
|
for param in paramnames[:i]:
|
||||||
self.write(suffix, param)
|
self.write(suffix, param)
|
||||||
suffix = ', '
|
suffix = ", "
|
||||||
if param in annotate_dict:
|
if param in annotate_dict:
|
||||||
self.write(': %s' % annotate_dict[param])
|
self.write(": %s" % annotate_dict[param])
|
||||||
if (line_number != self.line_number):
|
if line_number != self.line_number:
|
||||||
suffix = ",\n" + indent
|
suffix = ",\n" + indent
|
||||||
line_number = self.line_number
|
line_number = self.line_number
|
||||||
# value, string = annotate_args[param]
|
# value, string = annotate_args[param]
|
||||||
@@ -162,10 +170,9 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
# else:
|
# else:
|
||||||
# self.write(': %s' % value)
|
# self.write(': %s' % value)
|
||||||
|
|
||||||
|
suffix = ", " if i > 0 else ""
|
||||||
suffix = ', ' if i > 0 else ''
|
|
||||||
for n in node:
|
for n in node:
|
||||||
if n == 'pos_arg':
|
if n == "pos_arg":
|
||||||
self.write(suffix)
|
self.write(suffix)
|
||||||
param = paramnames[i]
|
param = paramnames[i]
|
||||||
self.write(param)
|
self.write(param)
|
||||||
@@ -175,25 +182,24 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
aa = aa[0]
|
aa = aa[0]
|
||||||
self.write(': "%s"' % aa)
|
self.write(': "%s"' % aa)
|
||||||
elif isinstance(aa, SyntaxTree):
|
elif isinstance(aa, SyntaxTree):
|
||||||
self.write(': ')
|
self.write(": ")
|
||||||
self.preorder(aa)
|
self.preorder(aa)
|
||||||
|
|
||||||
self.write('=')
|
self.write("=")
|
||||||
i += 1
|
i += 1
|
||||||
self.preorder(n)
|
self.preorder(n)
|
||||||
if (line_number != self.line_number):
|
if line_number != self.line_number:
|
||||||
suffix = ",\n" + indent
|
suffix = ",\n" + indent
|
||||||
line_number = self.line_number
|
line_number = self.line_number
|
||||||
else:
|
else:
|
||||||
suffix = ', '
|
suffix = ", "
|
||||||
|
|
||||||
|
|
||||||
if code_has_star_arg(code):
|
if code_has_star_arg(code):
|
||||||
star_arg = code.co_varnames[argc + kwonlyargcount]
|
star_arg = code.co_varnames[argc + kwonlyargcount]
|
||||||
if annotate_dict and star_arg in annotate_dict:
|
if annotate_dict and star_arg in annotate_dict:
|
||||||
self.write(suffix, '*%s: %s' % (star_arg, annotate_dict[star_arg]))
|
self.write(suffix, "*%s: %s" % (star_arg, annotate_dict[star_arg]))
|
||||||
else:
|
else:
|
||||||
self.write(suffix, '*%s' % star_arg)
|
self.write(suffix, "*%s" % star_arg)
|
||||||
argc += 1
|
argc += 1
|
||||||
|
|
||||||
# self.println(indent, '#flags:\t', int(code.co_flags))
|
# self.println(indent, '#flags:\t', int(code.co_flags))
|
||||||
@@ -214,16 +220,16 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
kw_args = [None] * kwonlyargcount
|
kw_args = [None] * kwonlyargcount
|
||||||
|
|
||||||
for n in node:
|
for n in node:
|
||||||
if n == 'kwargs':
|
if n == "kwargs":
|
||||||
n = n[0]
|
n = n[0]
|
||||||
if n == 'kwarg':
|
if n == "kwarg":
|
||||||
name = eval(n[0].pattr)
|
name = eval(n[0].pattr)
|
||||||
idx = kwargs.index(name)
|
idx = kwargs.index(name)
|
||||||
default = self.traverse(n[1], indent='')
|
default = self.traverse(n[1], indent="")
|
||||||
if annotate_dict and name in annotate_dict:
|
if annotate_dict and name in annotate_dict:
|
||||||
kw_args[idx] = '%s: %s=%s' % (name, annotate_dict[name], default)
|
kw_args[idx] = "%s: %s=%s" % (name, annotate_dict[name], default)
|
||||||
else:
|
else:
|
||||||
kw_args[idx] = '%s=%s' % (name, default)
|
kw_args[idx] = "%s=%s" % (name, default)
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@@ -233,11 +239,11 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
if flag:
|
if flag:
|
||||||
n = kwargs[i]
|
n = kwargs[i]
|
||||||
if n in annotate_dict:
|
if n in annotate_dict:
|
||||||
kw_args[i] = "%s: %s" %(n, annotate_dict[n])
|
kw_args[i] = "%s: %s" % (n, annotate_dict[n])
|
||||||
else:
|
else:
|
||||||
kw_args[i] = "%s" % n
|
kw_args[i] = "%s" % n
|
||||||
|
|
||||||
self.write(', '.join(kw_args))
|
self.write(", ".join(kw_args))
|
||||||
ends_in_comma = False
|
ends_in_comma = False
|
||||||
|
|
||||||
else:
|
else:
|
||||||
@@ -246,52 +252,56 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
|
|
||||||
if code_has_star_star_arg(code):
|
if code_has_star_star_arg(code):
|
||||||
if not ends_in_comma:
|
if not ends_in_comma:
|
||||||
self.write(', ')
|
self.write(", ")
|
||||||
star_star_arg = code.co_varnames[argc + kwonlyargcount]
|
star_star_arg = code.co_varnames[argc + kwonlyargcount]
|
||||||
if annotate_dict and star_star_arg in annotate_dict:
|
if annotate_dict and star_star_arg in annotate_dict:
|
||||||
self.write('**%s: %s' % (star_star_arg, annotate_dict[star_star_arg]))
|
self.write("**%s: %s" % (star_star_arg, annotate_dict[star_star_arg]))
|
||||||
else:
|
else:
|
||||||
self.write('**%s' % star_star_arg)
|
self.write("**%s" % star_star_arg)
|
||||||
|
|
||||||
if is_lambda:
|
if is_lambda:
|
||||||
self.write(": ")
|
self.write(": ")
|
||||||
else:
|
else:
|
||||||
self.write(')')
|
self.write(")")
|
||||||
if 'return' in annotate_tuple[0].attr:
|
if "return" in annotate_tuple[0].attr:
|
||||||
if (line_number != self.line_number) and not no_paramnames:
|
if (line_number != self.line_number) and not no_paramnames:
|
||||||
self.write("\n" + indent)
|
self.write("\n" + indent)
|
||||||
line_number = self.line_number
|
line_number = self.line_number
|
||||||
self.write(' -> ')
|
self.write(" -> ")
|
||||||
# value, string = annotate_args['return']
|
# value, string = annotate_args['return']
|
||||||
# if string:
|
# if string:
|
||||||
# self.write(' -> "%s"' % value)
|
# self.write(' -> "%s"' % value)
|
||||||
# else:
|
# else:
|
||||||
# self.write(' -> %s' % value)
|
# self.write(' -> %s' % value)
|
||||||
self.preorder(node[annotate_last-1])
|
self.preorder(node[annotate_last - 1])
|
||||||
|
|
||||||
self.println(":")
|
self.println(":")
|
||||||
|
|
||||||
if (len(code.co_consts) > 0 and
|
if (
|
||||||
code.co_consts[0] is not None and not is_lambda): # ugly
|
len(code.co_consts) > 0 and code.co_consts[0] is not None and not is_lambda
|
||||||
|
): # ugly
|
||||||
# docstring exists, dump it
|
# docstring exists, dump it
|
||||||
print_docstring(self, self.indent, code.co_consts[0])
|
print_docstring(self, self.indent, code.co_consts[0])
|
||||||
|
|
||||||
code._tokens = None # save memory
|
code._tokens = None # save memory
|
||||||
assert ast == 'stmts'
|
assert ast == "stmts"
|
||||||
|
|
||||||
all_globals = find_all_globals(ast, set())
|
all_globals = find_all_globals(ast, set())
|
||||||
globals, nonlocals = find_globals_and_nonlocals(ast, set(), set(),
|
globals, nonlocals = find_globals_and_nonlocals(
|
||||||
code, self.version)
|
ast, set(), set(), code, self.version
|
||||||
|
)
|
||||||
for g in sorted((all_globals & self.mod_globs) | globals):
|
for g in sorted((all_globals & self.mod_globs) | globals):
|
||||||
self.println(self.indent, 'global ', g)
|
self.println(self.indent, "global ", g)
|
||||||
for nl in sorted(nonlocals):
|
for nl in sorted(nonlocals):
|
||||||
self.println(self.indent, 'nonlocal ', nl)
|
self.println(self.indent, "nonlocal ", nl)
|
||||||
self.mod_globs -= all_globals
|
self.mod_globs -= all_globals
|
||||||
has_none = 'None' in code.co_names
|
has_none = "None" in code.co_names
|
||||||
rn = has_none and not find_none(ast)
|
rn = has_none and not find_none(ast)
|
||||||
self.gen_source(ast, code.co_name, code._customize, is_lambda=is_lambda,
|
self.gen_source(
|
||||||
returnNone=rn)
|
ast, code.co_name, code._customize, is_lambda=is_lambda, returnNone=rn
|
||||||
code._tokens = code._customize = None # save memory
|
)
|
||||||
|
code._tokens = code._customize = None # save memory
|
||||||
|
|
||||||
|
|
||||||
def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
||||||
"""
|
"""
|
||||||
@@ -309,38 +319,38 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
"""
|
"""
|
||||||
# if formal parameter is a tuple, the paramater name
|
# if formal parameter is a tuple, the paramater name
|
||||||
# starts with a dot (eg. '.1', '.2')
|
# starts with a dot (eg. '.1', '.2')
|
||||||
if name.startswith('.'):
|
if name.startswith("."):
|
||||||
# replace the name with the tuple-string
|
# replace the name with the tuple-string
|
||||||
name = self.get_tuple_parameter(ast, name)
|
name = self.get_tuple_parameter(ast, name)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if default:
|
if default:
|
||||||
value = self.traverse(default, indent='')
|
value = self.traverse(default, indent="")
|
||||||
maybe_show_tree_param_default(self.showast, name, value)
|
maybe_show_tree_param_default(self.showast, name, value)
|
||||||
result = '%s=%s' % (name, value)
|
result = "%s=%s" % (name, value)
|
||||||
if result[-2:] == '= ': # default was 'LOAD_CONST None'
|
if result[-2:] == "= ": # default was 'LOAD_CONST None'
|
||||||
result += 'None'
|
result += "None"
|
||||||
return result
|
return result
|
||||||
else:
|
else:
|
||||||
return name
|
return name
|
||||||
|
|
||||||
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
|
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
|
||||||
assert node[-1].kind.startswith('MAKE_')
|
assert node[-1].kind.startswith("MAKE_")
|
||||||
|
|
||||||
args_node = node[-1]
|
args_node = node[-1]
|
||||||
if isinstance(args_node.attr, tuple):
|
if isinstance(args_node.attr, tuple):
|
||||||
# positional args are after kwargs
|
# positional args are after kwargs
|
||||||
defparams = node[1:args_node.attr[0]+1]
|
defparams = node[1 : args_node.attr[0] + 1]
|
||||||
pos_args, kw_args, annotate_argc = args_node.attr
|
pos_args, kw_args, annotate_argc = args_node.attr
|
||||||
else:
|
else:
|
||||||
defparams = node[:args_node.attr]
|
defparams = node[: args_node.attr]
|
||||||
kw_args = 0
|
kw_args = 0
|
||||||
pass
|
pass
|
||||||
|
|
||||||
lambda_index = None
|
lambda_index = None
|
||||||
|
|
||||||
if lambda_index and is_lambda and iscode(node[lambda_index].attr):
|
if lambda_index and is_lambda and iscode(node[lambda_index].attr):
|
||||||
assert node[lambda_index].kind == 'LOAD_LAMBDA'
|
assert node[lambda_index].kind == "LOAD_LAMBDA"
|
||||||
code = node[lambda_index].attr
|
code = node[lambda_index].attr
|
||||||
else:
|
else:
|
||||||
code = code_node.attr
|
code = code_node.attr
|
||||||
@@ -353,13 +363,16 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
paramnames = list(code.co_varnames[:argc])
|
paramnames = list(code.co_varnames[:argc])
|
||||||
|
|
||||||
# defaults are for last n parameters, thus reverse
|
# defaults are for last n parameters, thus reverse
|
||||||
paramnames.reverse(); defparams.reverse()
|
paramnames.reverse()
|
||||||
|
defparams.reverse()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ast = self.build_ast(code._tokens,
|
ast = self.build_ast(
|
||||||
code._customize,
|
code._tokens,
|
||||||
is_lambda = is_lambda,
|
code._customize,
|
||||||
noneInNames = ('None' in code.co_names))
|
is_lambda=is_lambda,
|
||||||
|
noneInNames=("None" in code.co_names),
|
||||||
|
)
|
||||||
except (ParserError, ParserError2) as p:
|
except (ParserError, ParserError2) as p:
|
||||||
self.write(str(p))
|
self.write(str(p))
|
||||||
if not self.tolerate_errors:
|
if not self.tolerate_errors:
|
||||||
@@ -370,12 +383,14 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
indent = self.indent
|
indent = self.indent
|
||||||
|
|
||||||
# build parameters
|
# build parameters
|
||||||
params = [build_param(ast, name, default) for
|
params = [
|
||||||
name, default in zip_longest(paramnames, defparams, fillvalue=None)]
|
build_param(ast, name, default)
|
||||||
params.reverse() # back to correct order
|
for name, default in zip_longest(paramnames, defparams, fillvalue=None)
|
||||||
|
]
|
||||||
|
params.reverse() # back to correct order
|
||||||
|
|
||||||
if code_has_star_arg(code):
|
if code_has_star_arg(code):
|
||||||
params.append('*%s' % code.co_varnames[argc])
|
params.append("*%s" % code.co_varnames[argc])
|
||||||
argc += 1
|
argc += 1
|
||||||
|
|
||||||
# dump parameter list (with default values)
|
# dump parameter list (with default values)
|
||||||
@@ -387,13 +402,15 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
# drop the (return) None since that was just put there
|
# drop the (return) None since that was just put there
|
||||||
# to have something to after the yield finishes.
|
# to have something to after the yield finishes.
|
||||||
# FIXME: this is a bit hoaky and not general
|
# FIXME: this is a bit hoaky and not general
|
||||||
if (len(ast) > 1 and
|
if (
|
||||||
self.traverse(ast[-1]) == 'None' and
|
len(ast) > 1
|
||||||
self.traverse(ast[-2]).strip().startswith('yield')):
|
and self.traverse(ast[-1]) == "None"
|
||||||
|
and self.traverse(ast[-2]).strip().startswith("yield")
|
||||||
|
):
|
||||||
del ast[-1]
|
del ast[-1]
|
||||||
# Now pick out the expr part of the last statement
|
# Now pick out the expr part of the last statement
|
||||||
ast_expr = ast[-1]
|
ast_expr = ast[-1]
|
||||||
while ast_expr.kind != 'expr':
|
while ast_expr.kind != "expr":
|
||||||
ast_expr = ast_expr[0]
|
ast_expr = ast_expr[0]
|
||||||
ast[-1] = ast_expr
|
ast[-1] = ast_expr
|
||||||
pass
|
pass
|
||||||
@@ -411,7 +428,7 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
self.write(", ")
|
self.write(", ")
|
||||||
|
|
||||||
for n in node:
|
for n in node:
|
||||||
if n == 'pos_arg':
|
if n == "pos_arg":
|
||||||
continue
|
continue
|
||||||
else:
|
else:
|
||||||
self.preorder(n)
|
self.preorder(n)
|
||||||
@@ -420,38 +437,43 @@ def make_function2(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
|
|
||||||
if code_has_star_star_arg(code):
|
if code_has_star_star_arg(code):
|
||||||
if argc > 0:
|
if argc > 0:
|
||||||
self.write(', ')
|
self.write(", ")
|
||||||
self.write('**%s' % code.co_varnames[argc + kw_pairs])
|
self.write("**%s" % code.co_varnames[argc + kw_pairs])
|
||||||
|
|
||||||
if is_lambda:
|
if is_lambda:
|
||||||
self.write(": ")
|
self.write(": ")
|
||||||
else:
|
else:
|
||||||
self.println("):")
|
self.println("):")
|
||||||
|
|
||||||
if len(code.co_consts) > 0 and code.co_consts[0] is not None and not is_lambda: # ugly
|
if (
|
||||||
|
len(code.co_consts) > 0 and code.co_consts[0] is not None and not is_lambda
|
||||||
|
): # ugly
|
||||||
# docstring exists, dump it
|
# docstring exists, dump it
|
||||||
print_docstring(self, indent, code.co_consts[0])
|
print_docstring(self, indent, code.co_consts[0])
|
||||||
|
|
||||||
code._tokens = None # save memory
|
code._tokens = None # save memory
|
||||||
if not is_lambda:
|
if not is_lambda:
|
||||||
assert ast == 'stmts'
|
assert ast == "stmts"
|
||||||
|
|
||||||
all_globals = find_all_globals(ast, set())
|
all_globals = find_all_globals(ast, set())
|
||||||
|
|
||||||
globals, nonlocals = find_globals_and_nonlocals(ast, set(), set(),
|
globals, nonlocals = find_globals_and_nonlocals(
|
||||||
code, self.version)
|
ast, set(), set(), code, self.version
|
||||||
|
)
|
||||||
|
|
||||||
# Python 2 doesn't support the "nonlocal" statement
|
# Python 2 doesn't support the "nonlocal" statement
|
||||||
assert self.version >= 3.0 or not nonlocals
|
assert self.version >= 3.0 or not nonlocals
|
||||||
|
|
||||||
for g in sorted((all_globals & self.mod_globs) | globals):
|
for g in sorted((all_globals & self.mod_globs) | globals):
|
||||||
self.println(self.indent, 'global ', g)
|
self.println(self.indent, "global ", g)
|
||||||
self.mod_globs -= all_globals
|
self.mod_globs -= all_globals
|
||||||
has_none = 'None' in code.co_names
|
has_none = "None" in code.co_names
|
||||||
rn = has_none and not find_none(ast)
|
rn = has_none and not find_none(ast)
|
||||||
self.gen_source(ast, code.co_name, code._customize, is_lambda=is_lambda,
|
self.gen_source(
|
||||||
returnNone=rn)
|
ast, code.co_name, code._customize, is_lambda=is_lambda, returnNone=rn
|
||||||
code._tokens = None; code._customize = None # save memory
|
)
|
||||||
|
code._tokens = None
|
||||||
|
code._customize = None # save memory
|
||||||
|
|
||||||
|
|
||||||
def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
||||||
@@ -495,23 +517,23 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
if self.version >= 3.6:
|
if self.version >= 3.6:
|
||||||
value = default
|
value = default
|
||||||
else:
|
else:
|
||||||
value = self.traverse(default, indent='')
|
value = self.traverse(default, indent="")
|
||||||
maybe_show_tree_param_default(self.showast, name, value)
|
maybe_show_tree_param_default(self.showast, name, value)
|
||||||
if annotation:
|
if annotation:
|
||||||
result = '%s: %s=%s' % (name, annotation, value)
|
result = "%s: %s=%s" % (name, annotation, value)
|
||||||
else:
|
else:
|
||||||
result = '%s=%s' % (name, value)
|
result = "%s=%s" % (name, value)
|
||||||
|
|
||||||
# The below can probably be removed. This is probably
|
# The below can probably be removed. This is probably
|
||||||
# a holdover from days when LOAD_CONST erroneously
|
# a holdover from days when LOAD_CONST erroneously
|
||||||
# didn't handle LOAD_CONST None properly
|
# didn't handle LOAD_CONST None properly
|
||||||
if result[-2:] == '= ': # default was 'LOAD_CONST None'
|
if result[-2:] == "= ": # default was 'LOAD_CONST None'
|
||||||
result += 'None'
|
result += "None"
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
|
# MAKE_FUNCTION_... or MAKE_CLOSURE_...
|
||||||
assert node[-1].kind.startswith('MAKE_')
|
assert node[-1].kind.startswith("MAKE_")
|
||||||
|
|
||||||
# Python 3.3+ adds a qualified name at TOS (-1)
|
# Python 3.3+ adds a qualified name at TOS (-1)
|
||||||
# moving down the LOAD_LAMBDA instruction
|
# moving down the LOAD_LAMBDA instruction
|
||||||
@@ -531,11 +553,13 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
# not to be confused with keyword parameters which may appear after *.
|
# not to be confused with keyword parameters which may appear after *.
|
||||||
args_attr = args_node.attr
|
args_attr = args_node.attr
|
||||||
|
|
||||||
if isinstance(args_attr, tuple) or (self.version >= 3.6 and isinstance(args_attr, list)):
|
if isinstance(args_attr, tuple) or (
|
||||||
|
self.version >= 3.6 and isinstance(args_attr, list)
|
||||||
|
):
|
||||||
if len(args_attr) == 3:
|
if len(args_attr) == 3:
|
||||||
pos_args, kw_args, annotate_argc = args_attr
|
pos_args, kw_args, annotate_argc = args_attr
|
||||||
else:
|
else:
|
||||||
pos_args, kw_args, annotate_argc, closure = args_attr
|
pos_args, kw_args, annotate_argc, closure = args_attr
|
||||||
|
|
||||||
i = -4
|
i = -4
|
||||||
kw_pairs = 0
|
kw_pairs = 0
|
||||||
@@ -545,86 +569,103 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
if annotate_argc:
|
if annotate_argc:
|
||||||
# Turn into subroutine and DRY with other use
|
# Turn into subroutine and DRY with other use
|
||||||
annotate_node = node[i]
|
annotate_node = node[i]
|
||||||
if annotate_node == 'expr':
|
if annotate_node == "expr":
|
||||||
annotate_node = annotate_node[0]
|
annotate_node = annotate_node[0]
|
||||||
annotate_name_node = annotate_node[-1]
|
annotate_name_node = annotate_node[-1]
|
||||||
if annotate_node == 'dict' and annotate_name_node.kind.startswith('BUILD_CONST_KEY_MAP'):
|
if annotate_node == "dict" and annotate_name_node.kind.startswith(
|
||||||
types = [self.traverse(n, indent='') for n in annotate_node[:-2]]
|
"BUILD_CONST_KEY_MAP"
|
||||||
|
):
|
||||||
|
types = [
|
||||||
|
self.traverse(n, indent="") for n in annotate_node[:-2]
|
||||||
|
]
|
||||||
names = annotate_node[-2].attr
|
names = annotate_node[-2].attr
|
||||||
l = len(types)
|
l = len(types)
|
||||||
assert l == len(names)
|
assert l == len(names)
|
||||||
for i in range(l): annotate_dict[names[i]] = types[i]
|
for i in range(l):
|
||||||
|
annotate_dict[names[i]] = types[i]
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
i -= 1
|
i -= 1
|
||||||
if kw_args:
|
if kw_args:
|
||||||
kw_node = node[i]
|
kw_node = node[i]
|
||||||
if kw_node == 'expr':
|
if kw_node == "expr":
|
||||||
kw_node = kw_node[0]
|
kw_node = kw_node[0]
|
||||||
if kw_node == 'dict':
|
if kw_node == "dict":
|
||||||
kw_pairs = kw_node[-1].attr
|
kw_pairs = kw_node[-1].attr
|
||||||
|
|
||||||
|
|
||||||
# FIXME: there is probably a better way to classify this.
|
# FIXME: there is probably a better way to classify this.
|
||||||
have_kwargs = node[0].kind.startswith('kwarg') or node[0] == 'no_kwargs'
|
have_kwargs = node[0].kind.startswith("kwarg") or node[0] == "no_kwargs"
|
||||||
if len(node) >= 4:
|
if len(node) >= 4:
|
||||||
lc_index = -4
|
lc_index = -4
|
||||||
else:
|
else:
|
||||||
lc_index = -3
|
lc_index = -3
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if (3.0 <= self.version <= 3.3 and len(node) > 2 and
|
if (
|
||||||
node[lambda_index] != 'LOAD_LAMBDA' and
|
3.0 <= self.version <= 3.3
|
||||||
(have_kwargs or node[lc_index].kind != 'load_closure')):
|
and len(node) > 2
|
||||||
|
and node[lambda_index] != "LOAD_LAMBDA"
|
||||||
|
and (have_kwargs or node[lc_index].kind != "load_closure")
|
||||||
|
):
|
||||||
|
|
||||||
# Find the index in "node" where the first default
|
# Find the index in "node" where the first default
|
||||||
# parameter value is located. Note this is in contrast to
|
# parameter value is located. Note this is in contrast to
|
||||||
# key-word arguments, pairs of (name, value), which appear after "*".
|
# key-word arguments, pairs of (name, value), which appear after "*".
|
||||||
# "default_values_start" is this location.
|
# "default_values_start" is this location.
|
||||||
default_values_start = 0
|
default_values_start = 0
|
||||||
if node[0] == 'no_kwargs':
|
if node[0] == "no_kwargs":
|
||||||
default_values_start += 1
|
default_values_start += 1
|
||||||
# args are after kwargs; kwargs are bundled as one node
|
# args are after kwargs; kwargs are bundled as one node
|
||||||
if node[default_values_start] == 'kwargs':
|
if node[default_values_start] == "kwargs":
|
||||||
default_values_start += 1
|
default_values_start += 1
|
||||||
defparams = node[default_values_start:default_values_start+args_node.attr[0]]
|
defparams = node[
|
||||||
|
default_values_start : default_values_start + args_node.attr[0]
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
if self.version < 3.6:
|
if self.version < 3.6:
|
||||||
defparams = node[:args_node.attr[0]]
|
defparams = node[: args_node.attr[0]]
|
||||||
kw_args = 0
|
kw_args = 0
|
||||||
else:
|
else:
|
||||||
defparams = []
|
defparams = []
|
||||||
# FIXME: DRY with code below
|
# FIXME: DRY with code below
|
||||||
default, kw_args, annotate_argc = args_node.attr[0:3]
|
default, kw_args, annotate_argc = args_node.attr[0:3]
|
||||||
if default:
|
if default:
|
||||||
expr_node = node[0]
|
expr_node = node[0]
|
||||||
if node[0] == 'pos_arg':
|
if node[0] == "pos_arg":
|
||||||
expr_node = expr_node[0]
|
expr_node = expr_node[0]
|
||||||
assert expr_node == 'expr', "expecting mkfunc default node to be an expr"
|
assert (
|
||||||
if (expr_node[0] == 'LOAD_CONST' and
|
expr_node == "expr"
|
||||||
isinstance(expr_node[0].attr, tuple)):
|
), "expecting mkfunc default node to be an expr"
|
||||||
|
if expr_node[0] == "LOAD_CONST" and isinstance(
|
||||||
|
expr_node[0].attr, tuple
|
||||||
|
):
|
||||||
defparams = [repr(a) for a in expr_node[0].attr]
|
defparams = [repr(a) for a in expr_node[0].attr]
|
||||||
elif expr_node[0] in frozenset(('list', 'tuple', 'dict', 'set')):
|
elif expr_node[0] in frozenset(("list", "tuple", "dict", "set")):
|
||||||
defparams = [self.traverse(n, indent='') for n in expr_node[0][:-1]]
|
defparams = [
|
||||||
|
self.traverse(n, indent="") for n in expr_node[0][:-1]
|
||||||
|
]
|
||||||
else:
|
else:
|
||||||
defparams = []
|
defparams = []
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
if self.version < 3.6:
|
if self.version < 3.6:
|
||||||
defparams = node[:args_node.attr]
|
defparams = node[: args_node.attr]
|
||||||
kw_args = 0
|
kw_args = 0
|
||||||
else:
|
else:
|
||||||
default, kw_args, annotate, closure = args_node.attr
|
default, kw_args, annotate, closure = args_node.attr
|
||||||
if default:
|
if default:
|
||||||
expr_node = node[0]
|
expr_node = node[0]
|
||||||
if node[0] == 'pos_arg':
|
if node[0] == "pos_arg":
|
||||||
expr_node = expr_node[0]
|
expr_node = expr_node[0]
|
||||||
assert expr_node == 'expr', "expecting mkfunc default node to be an expr"
|
assert (
|
||||||
if (expr_node[0] == 'LOAD_CONST' and
|
expr_node == "expr"
|
||||||
isinstance(expr_node[0].attr, tuple)):
|
), "expecting mkfunc default node to be an expr"
|
||||||
|
if expr_node[0] == "LOAD_CONST" and isinstance(
|
||||||
|
expr_node[0].attr, tuple
|
||||||
|
):
|
||||||
defparams = [repr(a) for a in expr_node[0].attr]
|
defparams = [repr(a) for a in expr_node[0].attr]
|
||||||
elif expr_node[0] in frozenset(('list', 'tuple', 'dict', 'set')):
|
elif expr_node[0] in frozenset(("list", "tuple", "dict", "set")):
|
||||||
defparams = [self.traverse(n, indent='') for n in expr_node[0][:-1]]
|
defparams = [self.traverse(n, indent="") for n in expr_node[0][:-1]]
|
||||||
else:
|
else:
|
||||||
defparams = []
|
defparams = []
|
||||||
|
|
||||||
@@ -637,28 +678,33 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
if annotate_argc:
|
if annotate_argc:
|
||||||
# Turn into subroutine and DRY with other use
|
# Turn into subroutine and DRY with other use
|
||||||
annotate_node = node[i]
|
annotate_node = node[i]
|
||||||
if annotate_node == 'expr':
|
if annotate_node == "expr":
|
||||||
annotate_node = annotate_node[0]
|
annotate_node = annotate_node[0]
|
||||||
annotate_name_node = annotate_node[-1]
|
annotate_name_node = annotate_node[-1]
|
||||||
if annotate_node == 'dict' and annotate_name_node.kind.startswith('BUILD_CONST_KEY_MAP'):
|
if annotate_node == "dict" and annotate_name_node.kind.startswith(
|
||||||
types = [self.traverse(n, indent='') for n in annotate_node[:-2]]
|
"BUILD_CONST_KEY_MAP"
|
||||||
|
):
|
||||||
|
types = [
|
||||||
|
self.traverse(n, indent="") for n in annotate_node[:-2]
|
||||||
|
]
|
||||||
names = annotate_node[-2].attr
|
names = annotate_node[-2].attr
|
||||||
l = len(types)
|
l = len(types)
|
||||||
assert l == len(names)
|
assert l == len(names)
|
||||||
for i in range(l): annotate_dict[names[i]] = types[i]
|
for i in range(l):
|
||||||
|
annotate_dict[names[i]] = types[i]
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
i -= 1
|
i -= 1
|
||||||
if kw_args:
|
if kw_args:
|
||||||
kw_node = node[i]
|
kw_node = node[i]
|
||||||
if kw_node == 'expr':
|
if kw_node == "expr":
|
||||||
kw_node = kw_node[0]
|
kw_node = kw_node[0]
|
||||||
if kw_node == 'dict':
|
if kw_node == "dict":
|
||||||
kw_pairs = kw_node[-1].attr
|
kw_pairs = kw_node[-1].attr
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if lambda_index and is_lambda and iscode(node[lambda_index].attr):
|
if lambda_index and is_lambda and iscode(node[lambda_index].attr):
|
||||||
assert node[lambda_index].kind == 'LOAD_LAMBDA'
|
assert node[lambda_index].kind == "LOAD_LAMBDA"
|
||||||
code = node[lambda_index].attr
|
code = node[lambda_index].attr
|
||||||
else:
|
else:
|
||||||
code = code_node.attr
|
code = code_node.attr
|
||||||
@@ -672,17 +718,19 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
|
|
||||||
paramnames = list(scanner_code.co_varnames[:argc])
|
paramnames = list(scanner_code.co_varnames[:argc])
|
||||||
if kwonlyargcount > 0:
|
if kwonlyargcount > 0:
|
||||||
kwargs = list(scanner_code.co_varnames[argc:argc+kwonlyargcount])
|
kwargs = list(scanner_code.co_varnames[argc : argc + kwonlyargcount])
|
||||||
|
|
||||||
# defaults are for last n parameters, thus reverse
|
# defaults are for last n parameters, thus reverse
|
||||||
paramnames.reverse();
|
paramnames.reverse()
|
||||||
defparams.reverse()
|
defparams.reverse()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
ast = self.build_ast(scanner_code._tokens,
|
ast = self.build_ast(
|
||||||
scanner_code._customize,
|
scanner_code._tokens,
|
||||||
is_lambda = is_lambda,
|
scanner_code._customize,
|
||||||
noneInNames = ('None' in code.co_names))
|
is_lambda=is_lambda,
|
||||||
|
noneInNames=("None" in code.co_names),
|
||||||
|
)
|
||||||
except (ParserError, ParserError2) as p:
|
except (ParserError, ParserError2) as p:
|
||||||
self.write(str(p))
|
self.write(str(p))
|
||||||
if not self.tolerate_errors:
|
if not self.tolerate_errors:
|
||||||
@@ -701,10 +749,13 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
params = []
|
params = []
|
||||||
if defparams:
|
if defparams:
|
||||||
for i, defparam in enumerate(defparams):
|
for i, defparam in enumerate(defparams):
|
||||||
params.append(build_param(ast, paramnames[i], defparam,
|
params.append(
|
||||||
annotate_dict.get(paramnames[i])))
|
build_param(
|
||||||
|
ast, paramnames[i], defparam, annotate_dict.get(paramnames[i])
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
for param in paramnames[i+1:]:
|
for param in paramnames[i + 1 :]:
|
||||||
if param in annotate_dict:
|
if param in annotate_dict:
|
||||||
params.append("%s: %s" % (param, annotate_dict[param]))
|
params.append("%s: %s" % (param, annotate_dict[param]))
|
||||||
else:
|
else:
|
||||||
@@ -716,17 +767,17 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
else:
|
else:
|
||||||
params.append(param)
|
params.append(param)
|
||||||
|
|
||||||
params.reverse() # back to correct order
|
params.reverse() # back to correct order
|
||||||
|
|
||||||
if code_has_star_arg(code):
|
if code_has_star_arg(code):
|
||||||
if self.version > 3.0:
|
if self.version > 3.0:
|
||||||
star_arg = code.co_varnames[argc + kwonlyargcount]
|
star_arg = code.co_varnames[argc + kwonlyargcount]
|
||||||
if annotate_dict and star_arg in annotate_dict:
|
if annotate_dict and star_arg in annotate_dict:
|
||||||
params.append('*%s: %s' % (star_arg, annotate_dict[star_arg]))
|
params.append("*%s: %s" % (star_arg, annotate_dict[star_arg]))
|
||||||
else:
|
else:
|
||||||
params.append('*%s' % star_arg)
|
params.append("*%s" % star_arg)
|
||||||
else:
|
else:
|
||||||
params.append('*%s' % code.co_varnames[argc])
|
params.append("*%s" % code.co_varnames[argc])
|
||||||
argc += 1
|
argc += 1
|
||||||
|
|
||||||
# dump parameter list (with default values)
|
# dump parameter list (with default values)
|
||||||
@@ -738,13 +789,15 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
# drop the (return) None since that was just put there
|
# drop the (return) None since that was just put there
|
||||||
# to have something to after the yield finishes.
|
# to have something to after the yield finishes.
|
||||||
# FIXME: this is a bit hoaky and not general
|
# FIXME: this is a bit hoaky and not general
|
||||||
if (len(ast) > 1 and
|
if (
|
||||||
self.traverse(ast[-1]) == 'None' and
|
len(ast) > 1
|
||||||
self.traverse(ast[-2]).strip().startswith('yield')):
|
and self.traverse(ast[-1]) == "None"
|
||||||
|
and self.traverse(ast[-2]).strip().startswith("yield")
|
||||||
|
):
|
||||||
del ast[-1]
|
del ast[-1]
|
||||||
# Now pick out the expr part of the last statement
|
# Now pick out the expr part of the last statement
|
||||||
ast_expr = ast[-1]
|
ast_expr = ast[-1]
|
||||||
while ast_expr.kind != 'expr':
|
while ast_expr.kind != "expr":
|
||||||
ast_expr = ast_expr[0]
|
ast_expr = ast_expr[0]
|
||||||
ast[-1] = ast_expr
|
ast[-1] = ast_expr
|
||||||
pass
|
pass
|
||||||
@@ -776,7 +829,7 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
if kw_nodes == "kwargs":
|
if kw_nodes == "kwargs":
|
||||||
for n in kw_nodes:
|
for n in kw_nodes:
|
||||||
name = eval(n[0].pattr)
|
name = eval(n[0].pattr)
|
||||||
default = self.traverse(n[1], indent='')
|
default = self.traverse(n[1], indent="")
|
||||||
idx = kwargs.index(name)
|
idx = kwargs.index(name)
|
||||||
kw_args[idx] = "%s=%s" % (name, default)
|
kw_args[idx] = "%s=%s" % (name, default)
|
||||||
|
|
||||||
@@ -785,7 +838,7 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
for i, flag in enumerate(other_kw):
|
for i, flag in enumerate(other_kw):
|
||||||
if flag:
|
if flag:
|
||||||
kw_args[i] = "%s" % kwargs[i]
|
kw_args[i] = "%s" % kwargs[i]
|
||||||
self.write(', '.join(kw_args))
|
self.write(", ".join(kw_args))
|
||||||
ends_in_comma = False
|
ends_in_comma = False
|
||||||
elif self.version >= 3.6:
|
elif self.version >= 3.6:
|
||||||
# argc = node[-1].attr
|
# argc = node[-1].attr
|
||||||
@@ -796,9 +849,9 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
free_tup = ann_dict = kw_dict = default_tup = None
|
free_tup = ann_dict = kw_dict = default_tup = None
|
||||||
fn_bits = node[-1].attr
|
fn_bits = node[-1].attr
|
||||||
index = -4 # Skip over:
|
index = -4 # Skip over:
|
||||||
# MAKE_FUNCTION,
|
# MAKE_FUNCTION,
|
||||||
# LOAD_CONST qualified name,
|
# LOAD_CONST qualified name,
|
||||||
# LOAD_CONST code object
|
# LOAD_CONST code object
|
||||||
if fn_bits[-1]:
|
if fn_bits[-1]:
|
||||||
free_tup = node[index]
|
free_tup = node[index]
|
||||||
index -= 1
|
index -= 1
|
||||||
@@ -811,18 +864,18 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
if fn_bits[-4]:
|
if fn_bits[-4]:
|
||||||
default_tup = node[index]
|
default_tup = node[index]
|
||||||
|
|
||||||
if kw_dict == 'expr':
|
if kw_dict == "expr":
|
||||||
kw_dict = kw_dict[0]
|
kw_dict = kw_dict[0]
|
||||||
|
|
||||||
# FIXME: handle free_tup, annotate_dict, and default_tup
|
# FIXME: handle free_tup, annotate_dict, and default_tup
|
||||||
kw_args = [None] * kwonlyargcount
|
kw_args = [None] * kwonlyargcount
|
||||||
|
|
||||||
if kw_dict:
|
if kw_dict:
|
||||||
assert kw_dict == 'dict'
|
assert kw_dict == "dict"
|
||||||
defaults = [self.traverse(n, indent='') for n in kw_dict[:-2]]
|
defaults = [self.traverse(n, indent="") for n in kw_dict[:-2]]
|
||||||
names = eval(self.traverse(kw_dict[-2]))
|
names = eval(self.traverse(kw_dict[-2]))
|
||||||
assert len(defaults) == len(names)
|
assert len(defaults) == len(names)
|
||||||
sep = ''
|
sep = ""
|
||||||
# FIXME: possibly handle line breaks
|
# FIXME: possibly handle line breaks
|
||||||
for i, n in enumerate(names):
|
for i, n in enumerate(names):
|
||||||
idx = kwargs.index(n)
|
idx = kwargs.index(n)
|
||||||
@@ -841,11 +894,11 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
if flag:
|
if flag:
|
||||||
n = kwargs[i]
|
n = kwargs[i]
|
||||||
if ann_dict and n in annotate_dict:
|
if ann_dict and n in annotate_dict:
|
||||||
kw_args[i] = "%s: %s" %(n, annotate_dict[n])
|
kw_args[i] = "%s: %s" % (n, annotate_dict[n])
|
||||||
else:
|
else:
|
||||||
kw_args[i] = "%s" % n
|
kw_args[i] = "%s" % n
|
||||||
|
|
||||||
self.write(', '.join(kw_args))
|
self.write(", ".join(kw_args))
|
||||||
ends_in_comma = False
|
ends_in_comma = False
|
||||||
|
|
||||||
pass
|
pass
|
||||||
@@ -855,41 +908,46 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
|
|
||||||
if code_has_star_star_arg(code):
|
if code_has_star_star_arg(code):
|
||||||
if not ends_in_comma:
|
if not ends_in_comma:
|
||||||
self.write(', ')
|
self.write(", ")
|
||||||
star_star_arg = code.co_varnames[argc + kwonlyargcount]
|
star_star_arg = code.co_varnames[argc + kwonlyargcount]
|
||||||
if annotate_dict and star_star_arg in annotate_dict:
|
if annotate_dict and star_star_arg in annotate_dict:
|
||||||
self.write('**%s: %s' % (star_star_arg, annotate_dict[star_star_arg]))
|
self.write("**%s: %s" % (star_star_arg, annotate_dict[star_star_arg]))
|
||||||
else:
|
else:
|
||||||
self.write('**%s' % star_star_arg)
|
self.write("**%s" % star_star_arg)
|
||||||
|
|
||||||
if is_lambda:
|
if is_lambda:
|
||||||
self.write(": ")
|
self.write(": ")
|
||||||
else:
|
else:
|
||||||
self.write(')')
|
self.write(")")
|
||||||
if annotate_dict and 'return' in annotate_dict:
|
if annotate_dict and "return" in annotate_dict:
|
||||||
self.write(' -> %s' % annotate_dict['return'])
|
self.write(" -> %s" % annotate_dict["return"])
|
||||||
self.println(":")
|
self.println(":")
|
||||||
|
|
||||||
if len(code.co_consts) > 0 and code.co_consts[0] is not None and not is_lambda: # ugly
|
if (
|
||||||
|
len(code.co_consts) > 0 and code.co_consts[0] is not None and not is_lambda
|
||||||
|
): # ugly
|
||||||
# docstring exists, dump it
|
# docstring exists, dump it
|
||||||
print_docstring(self, self.indent, code.co_consts[0])
|
print_docstring(self, self.indent, code.co_consts[0])
|
||||||
|
|
||||||
scanner_code._tokens = None # save memory
|
scanner_code._tokens = None # save memory
|
||||||
assert ast == 'stmts'
|
assert ast == "stmts"
|
||||||
|
|
||||||
all_globals = find_all_globals(ast, set())
|
all_globals = find_all_globals(ast, set())
|
||||||
globals, nonlocals = find_globals_and_nonlocals(ast, set(),
|
globals, nonlocals = find_globals_and_nonlocals(
|
||||||
set(), code, self.version)
|
ast, set(), set(), code, self.version
|
||||||
|
)
|
||||||
|
|
||||||
for g in sorted((all_globals & self.mod_globs) | globals):
|
for g in sorted((all_globals & self.mod_globs) | globals):
|
||||||
self.println(self.indent, 'global ', g)
|
self.println(self.indent, "global ", g)
|
||||||
|
|
||||||
for nl in sorted(nonlocals):
|
for nl in sorted(nonlocals):
|
||||||
self.println(self.indent, 'nonlocal ', nl)
|
self.println(self.indent, "nonlocal ", nl)
|
||||||
|
|
||||||
self.mod_globs -= all_globals
|
self.mod_globs -= all_globals
|
||||||
has_none = 'None' in code.co_names
|
has_none = "None" in code.co_names
|
||||||
rn = has_none and not find_none(ast)
|
rn = has_none and not find_none(ast)
|
||||||
self.gen_source(ast, code.co_name, scanner_code._customize, is_lambda=is_lambda,
|
self.gen_source(
|
||||||
returnNone=rn)
|
ast, code.co_name, scanner_code._customize, is_lambda=is_lambda, returnNone=rn
|
||||||
scanner_code._tokens = None; scanner_code._customize = None # save memory
|
)
|
||||||
|
scanner_code._tokens = None
|
||||||
|
scanner_code._customize = None # save memory
|
||||||
|
Reference in New Issue
Block a user