You've already forked python-uncompyle6
mirror of
https://github.com/rocky/python-uncompyle6.git
synced 2025-08-04 01:09:52 +08:00
Merge pull request #257 from rocky/annotation-types-3.6
Annotation types 3.6
This commit is contained in:
@@ -135,6 +135,10 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
indent = ' ' * l
|
indent = ' ' * l
|
||||||
line_number = self.line_number
|
line_number = self.line_number
|
||||||
|
|
||||||
|
if code_has_star_arg(code):
|
||||||
|
self.write('*%s' % code.co_varnames[argc + kw_pairs])
|
||||||
|
argc += 1
|
||||||
|
|
||||||
i = len(paramnames) - len(defparams)
|
i = len(paramnames) - len(defparams)
|
||||||
suffix = ''
|
suffix = ''
|
||||||
|
|
||||||
@@ -144,7 +148,7 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
self.write(suffix, param)
|
self.write(suffix, param)
|
||||||
suffix = ', '
|
suffix = ', '
|
||||||
if param in annotate_tuple[0].attr:
|
if param in annotate_tuple[0].attr:
|
||||||
p = annotate_tuple[0].attr.index(param) + pos_args
|
p = annotate_tuple[0].attr.index(param)
|
||||||
self.write(': ')
|
self.write(': ')
|
||||||
self.preorder(node[p])
|
self.preorder(node[p])
|
||||||
if (line_number != self.line_number):
|
if (line_number != self.line_number):
|
||||||
@@ -183,17 +187,7 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
suffix = ', '
|
suffix = ', '
|
||||||
|
|
||||||
|
|
||||||
if code_has_star_arg(code):
|
|
||||||
star_arg = code.co_varnames[argc + kw_pairs]
|
|
||||||
self.write(suffix, '*%s' % star_arg)
|
|
||||||
if star_arg in annotate_tuple[0].attr:
|
|
||||||
p = annotate_tuple[0].attr.index(star_arg) + pos_args + kw_args
|
|
||||||
self.write(': ')
|
|
||||||
self.preorder(node[p])
|
|
||||||
argc += 1
|
|
||||||
|
|
||||||
# self.println(indent, '#flags:\t', int(code.co_flags))
|
# self.println(indent, '#flags:\t', int(code.co_flags))
|
||||||
ends_in_comma = False
|
|
||||||
if kw_args + annotate_argc > 0:
|
if kw_args + annotate_argc > 0:
|
||||||
if no_paramnames:
|
if no_paramnames:
|
||||||
if not code_has_star_arg(code):
|
if not code_has_star_arg(code):
|
||||||
@@ -204,47 +198,49 @@ def make_function3_annotate(self, node, is_lambda, nested=1,
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
self.write(", ")
|
self.write(", ")
|
||||||
ends_in_comma = True
|
|
||||||
|
|
||||||
kwargs = node[1]
|
kwargs = node[0]
|
||||||
last = len(kwargs)-1
|
last = len(kwargs)-1
|
||||||
i = 0
|
i = 0
|
||||||
for n in node[1]:
|
for n in node[0]:
|
||||||
if n == 'kwarg':
|
if n == 'kwarg':
|
||||||
if argc > 0 and not ends_in_comma:
|
if (line_number != self.line_number):
|
||||||
self.write(', ')
|
self.write("\n" + indent)
|
||||||
if (line_number != self.line_number):
|
line_number = self.line_number
|
||||||
self.write("\n" + indent)
|
self.write('%s=' % n[0].pattr)
|
||||||
line_number = self.line_number
|
self.preorder(n[1])
|
||||||
kn = n[0].attr
|
if i < last:
|
||||||
if kn in annotate_tuple[0].attr:
|
self.write(', ')
|
||||||
p = annotate_tuple[0].attr.index(star_arg) + pos_args + kw_args
|
i += 1
|
||||||
self.write('%s: ' % kn)
|
pass
|
||||||
self.preorder(node[p])
|
pass
|
||||||
self.write('=')
|
annotate_args = []
|
||||||
else:
|
for n in node:
|
||||||
self.write('%s=' % kn)
|
if n == 'annotate_arg':
|
||||||
self.preorder(n[1])
|
annotate_args.append(n[0])
|
||||||
if i < last:
|
elif n == 'annotate_tuple':
|
||||||
self.write(', ')
|
t = n[0].attr
|
||||||
ends_in_comma = True
|
if t[-1] == 'return':
|
||||||
else:
|
t = t[0:-1]
|
||||||
ends_in_comma = False
|
annotate_args = annotate_args[:-1]
|
||||||
i += 1
|
pass
|
||||||
|
last = len(annotate_args) - 1
|
||||||
|
for i in range(len(annotate_args)):
|
||||||
|
self.write("%s: " % (t[i]))
|
||||||
|
self.preorder(annotate_args[i])
|
||||||
|
if i < last:
|
||||||
|
self.write(', ')
|
||||||
|
pass
|
||||||
|
pass
|
||||||
|
break
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
if code_has_star_star_arg(code):
|
if code_has_star_star_arg(code):
|
||||||
if argc > 0 and not ends_in_comma:
|
if argc > 0:
|
||||||
self.write(', ')
|
self.write(', ')
|
||||||
star_star_arg = code.co_varnames[argc + kw_pairs]
|
self.write('**%s' % code.co_varnames[argc + kw_pairs])
|
||||||
self.write('**%s' % star_star_arg)
|
|
||||||
if star_star_arg in annotate_tuple[0].attr:
|
|
||||||
p = annotate_tuple[0].attr.index(star_star_arg) + pos_args + kw_args
|
|
||||||
self.write(': ')
|
|
||||||
self.preorder(node[p])
|
|
||||||
|
|
||||||
if is_lambda:
|
if is_lambda:
|
||||||
self.write(": ")
|
self.write(": ")
|
||||||
@@ -480,7 +476,7 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
|
|
||||||
# Thank you, Python.
|
# Thank you, Python.
|
||||||
|
|
||||||
def build_param(ast, name, default, annotation=None):
|
def build_param(ast, name, default):
|
||||||
"""build parameters:
|
"""build parameters:
|
||||||
- handle defaults
|
- handle defaults
|
||||||
- handle format tuple parameters
|
- handle format tuple parameters
|
||||||
@@ -490,10 +486,7 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
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:
|
result = '%s=%s' % (name, value)
|
||||||
result = '%s: %s=%s' % (name, annotation, value)
|
|
||||||
else:
|
|
||||||
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
|
||||||
@@ -688,30 +681,17 @@ 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(build_param(ast, paramnames[i], defparam))
|
||||||
annotate_dict.get(paramnames[i])))
|
|
||||||
|
|
||||||
for param in paramnames[i+1:]:
|
params += paramnames[i+1:]
|
||||||
if param in annotate_dict:
|
|
||||||
params.append("%s: %s" % (param, annotate_dict[param]))
|
|
||||||
else:
|
|
||||||
params.append(param)
|
|
||||||
else:
|
else:
|
||||||
for param in paramnames:
|
params = paramnames
|
||||||
if param in annotate_dict:
|
|
||||||
params.append("%s: %s" % (param, annotate_dict[param]))
|
|
||||||
else:
|
|
||||||
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 + kw_pairs]
|
params.append('*%s' % code.co_varnames[argc + kw_pairs])
|
||||||
if star_arg in annotate_dict:
|
|
||||||
params.append('*%s: %s' %(star_arg, annotate_dict[star_arg]))
|
|
||||||
else:
|
|
||||||
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
|
||||||
@@ -736,11 +716,14 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
ast[-1] = ast_expr
|
ast[-1] = ast_expr
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
|
# FIXME: add annotations here
|
||||||
self.write("(", ", ".join(params))
|
self.write("(", ", ".join(params))
|
||||||
# self.println(indent, '#flags:\t', int(code.co_flags))
|
# self.println(indent, '#flags:\t', int(code.co_flags))
|
||||||
|
|
||||||
|
kwonlyargcount = 0
|
||||||
|
|
||||||
ends_in_comma = False
|
ends_in_comma = False
|
||||||
if kw_args > 0:
|
if kw_args + annotate_argc > 0:
|
||||||
if not (4 & code.co_flags):
|
if not (4 & code.co_flags):
|
||||||
if argc > 0:
|
if argc > 0:
|
||||||
self.write(", *, ")
|
self.write(", *, ")
|
||||||
@@ -784,11 +767,11 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
break
|
break
|
||||||
elif self.version >= 3.6:
|
elif self.version >= 3.6:
|
||||||
# argc = node[-1].attr
|
# argc = node[-1].attr
|
||||||
# co = node[-3].attr
|
co = node[-3].attr
|
||||||
# argcount = co.co_argcount
|
# argcount = co.co_argcount
|
||||||
# kwonlyargcount = co.co_kwonlyargcount
|
kwcount = kwonlyargcount = co.co_kwonlyargcount
|
||||||
|
|
||||||
free_tup = annotate_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,
|
||||||
@@ -798,7 +781,7 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
free_tup = node[index]
|
free_tup = node[index]
|
||||||
index -= 1
|
index -= 1
|
||||||
if fn_bits[-2]:
|
if fn_bits[-2]:
|
||||||
annotate_dict = node[index]
|
ann_dict = node[index]
|
||||||
index -= 1
|
index -= 1
|
||||||
if fn_bits[-3]:
|
if fn_bits[-3]:
|
||||||
kw_dict = node[index]
|
kw_dict = node[index]
|
||||||
@@ -819,19 +802,39 @@ def make_function3(self, node, is_lambda, nested=1, code_node=None):
|
|||||||
# FIXME: possibly handle line breaks
|
# FIXME: possibly handle line breaks
|
||||||
for i, n in enumerate(names):
|
for i, n in enumerate(names):
|
||||||
self.write(sep)
|
self.write(sep)
|
||||||
self.write("%s=%s" % (n, defaults[i]))
|
if annotate_dict and n in annotate_dict:
|
||||||
|
self.write("%s: %s=%s" % (n, annotate_dict[n], defaults[i]))
|
||||||
|
else:
|
||||||
|
self.write("%s=%s" % (n, defaults[i]))
|
||||||
sep = ', '
|
sep = ', '
|
||||||
ends_in_comma = False
|
ends_in_comma = False
|
||||||
|
kwcount -= 1
|
||||||
pass
|
pass
|
||||||
pass
|
pass
|
||||||
|
if ann_dict:
|
||||||
|
annotate_args = ann_dict[0][-2].attr
|
||||||
|
if annotate_args[-1] == 'return':
|
||||||
|
annotate_args = annotate_args[:-1]
|
||||||
|
|
||||||
|
sep = ''
|
||||||
|
if not ends_in_comma:
|
||||||
|
sep = ', '
|
||||||
|
for n in annotate_args:
|
||||||
|
if kwcount == 0:
|
||||||
|
break
|
||||||
|
self.write(sep)
|
||||||
|
self.write('%s: %s' %(n, annotate_dict[n]))
|
||||||
|
sep = ', '
|
||||||
|
ends_in_comma = False
|
||||||
|
kwcount -= 1
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if code_has_star_star_arg(code):
|
if code_has_star_star_arg(code):
|
||||||
if argc > 0 and not ends_in_comma:
|
if not ends_in_comma:
|
||||||
self.write(', ')
|
self.write(', ')
|
||||||
star_star_arg = code.co_varnames[argc + kw_pairs]
|
star_star_arg = code.co_varnames[argc + kwonlyargcount]
|
||||||
if annotate_dict and star_star_arg 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)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user