Fix bug in 3.6 handling kwonly params ...

when there are annotation args
This commit is contained in:
rocky 2020-01-24 05:47:41 -05:00
parent 5616f56442
commit 5c31fdc362
5 changed files with 48 additions and 33 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -59,3 +59,14 @@ assert p == Point(**dict(x=11, y=22))
def posonly_sum(pos_arg1, *arg, **kwarg): def posonly_sum(pos_arg1, *arg, **kwarg):
return pos_arg1 + sum(arg) + sum(kwarg.values()) return pos_arg1 + sum(arg) + sum(kwarg.values())
assert 1+2+3+4 == posonly_sum(1,*(2,3),**{"4":4}) assert 1+2+3+4 == posonly_sum(1,*(2,3),**{"4":4})
# From 3.7 test_grammar.py
# Bug was in handling keyword-only parameters when there are annotations.
# The stack order from least- to most-recent is:
# default, keyword, annotation, closure
# This changes in between Python 3.5 and 3.6.
def f(a, b: 1, c: 2, d, e: 3=4, f=5, *g: 6, h: 7, i=8, j: 9=10,
**k: 11) -> 12: pass
assert f.__annotations__ == {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
'k': 11, 'return': 12}

View File

@ -92,38 +92,40 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
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
if closure: if annotate_argc:
# FIXME: fill in # Turn into subroutine and DRY with other use
i -= 1 annotate_node = node[i]
if annotate_node == "expr":
if annotate_argc: annotate_node = annotate_node[0]
# Turn into subroutine and DRY with other use annotate_name_node = annotate_node[-1]
annotate_node = node[i] if annotate_node == "dict" and annotate_name_node.kind.startswith(
if annotate_node == "expr": "BUILD_CONST_KEY_MAP"
annotate_node = annotate_node[0] ):
annotate_name_node = annotate_node[-1] types = [
if annotate_node == "dict" and annotate_name_node.kind.startswith( self.traverse(n, indent="") for n in annotate_node[:-2]
"BUILD_CONST_KEY_MAP" ]
): names = annotate_node[-2].attr
types = [ l = len(types)
self.traverse(n, indent="") for n in annotate_node[:-2] assert l == len(names)
] for i in range(l):
names = annotate_node[-2].attr annotate_dict[names[i]] = types[i]
l = len(types)
assert l == len(names)
for i in range(l):
annotate_dict[names[i]] = types[i]
pass
pass pass
i -= 1 pass
if kw_args: i -= 1
kw_node = node[i]
if kw_node == "expr": if closure:
kw_node = kw_node[0] # FIXME: fill in
if kw_node == "dict": # annotate = node[i]
kw_pairs = kw_node[-1].attr i -= 1
if kw_args:
kw_node = node[pos_args]
if kw_node == "expr":
kw_node = kw_node[0]
if kw_node == "dict":
kw_pairs = kw_node[-1].attr
defparams = [] defparams = []
# FIXME: DRY with code below # FIXME: DRY with code below
@ -323,7 +325,9 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
self.write(" -> %s" % annotate_dict["return"]) self.write(" -> %s" % annotate_dict["return"])
self.println(":") self.println(":")
if node[-2] == "docstring" and not is_lambda: if (
node[-2] == "docstring" and not is_lambda
):
# docstring exists, dump it # docstring exists, dump it
self.println(self.traverse(node[-2])) self.println(self.traverse(node[-2]))
@ -351,7 +355,7 @@ def make_function36(self, node, is_lambda, nested=1, code_node=None):
# was optimized away. Here, we need to put in unreachable code to # was optimized away. Here, we need to put in unreachable code to
# add in "yield" just so that the compiler will mark # add in "yield" just so that the compiler will mark
# the GENERATOR bit of the function. See for example # the GENERATOR bit of the function. See for example
# Python 3.x's test_generator.py test program. # Python 3.x's test_connection.py and test_contexlib_async test programs.
if code.co_flags & (CO_GENERATOR | CO_ASYNC_GENERATOR): if code.co_flags & (CO_GENERATOR | CO_ASYNC_GENERATOR):
need_bogus_yield = True need_bogus_yield = True
for token in scanner_code._tokens: for token in scanner_code._tokens: