Commit ccd34cbc authored by Stefan Behnel's avatar Stefan Behnel

some code cleanup and beautification, fix error message to match Python when...

some code cleanup and beautification, fix error message to match Python when passing too many positional arguments into a function with required keyword arguments
parent 871351b5
...@@ -1753,10 +1753,10 @@ class DefNode(FuncDefNode): ...@@ -1753,10 +1753,10 @@ class DefNode(FuncDefNode):
if self.star_arg: if self.star_arg:
code.putln('default:') code.putln('default:')
for i in range(max_positional_args-1, -1, -1): for i in range(max_positional_args-1, -1, -1):
code.putln('case %d:' % (i+1)) code.put('case %2d: ' % (i+1))
code.putln("values[%d] = PyTuple_GET_ITEM(%s, %d);" % ( code.putln("values[%d] = PyTuple_GET_ITEM(%s, %d);" % (
i, Naming.args_cname, i)) i, Naming.args_cname, i))
code.putln('case 0: break;') code.putln('case 0: break;')
if not self.star_arg: if not self.star_arg:
code.put('default: ') # more arguments than allowed code.put('default: ') # more arguments than allowed
code.put_goto(argtuple_error_label) code.put_goto(argtuple_error_label)
...@@ -1769,7 +1769,7 @@ class DefNode(FuncDefNode): ...@@ -1769,7 +1769,7 @@ class DefNode(FuncDefNode):
if self.star_arg and i == max_positional_args: if self.star_arg and i == max_positional_args:
code.putln('default:') code.putln('default:')
else: else:
code.putln('case %d:' % i) code.putln('case %2d:' % i)
code.putln('values[%d] = PyDict_GetItem(%s, *%s[%d]);' % ( code.putln('values[%d] = PyDict_GetItem(%s, *%s[%d]);' % (
i, Naming.kwds_cname, Naming.pykwdlist_cname, i)) i, Naming.kwds_cname, Naming.pykwdlist_cname, i))
if i < min_positional_args: if i < min_positional_args:
...@@ -1828,11 +1828,11 @@ class DefNode(FuncDefNode): ...@@ -1828,11 +1828,11 @@ class DefNode(FuncDefNode):
if self.num_required_kw_args: if self.num_required_kw_args:
# pure error case: keywords required but not passed # pure error case: keywords required but not passed
if max_positional_args > min_positional_args and not self.star_arg:
code.putln('} else if (PyTuple_GET_SIZE(%s) > %d) {' % (
Naming.args_cname, max_positional_args))
code.put_goto(argtuple_error_label)
code.putln('} else {') code.putln('} else {')
if not self.star_arg and not self.starstar_arg:
# optional args missing?
self.generate_positional_args_check(
max_positional_args, has_fixed_positional_count, code)
for i, arg in enumerate(kw_only_args): for i, arg in enumerate(kw_only_args):
if not arg.default: if not arg.default:
# required keyword-only argument missing # required keyword-only argument missing
...@@ -1856,12 +1856,15 @@ class DefNode(FuncDefNode): ...@@ -1856,12 +1856,15 @@ class DefNode(FuncDefNode):
reversed_args = list(enumerate(positional_args))[::-1] reversed_args = list(enumerate(positional_args))[::-1]
for i, arg in reversed_args: for i, arg in reversed_args:
if i >= min_positional_args-1: if i >= min_positional_args-1:
code.putln('case %d:' % (i+1)) if min_positional_args > 1:
code.putln('case %2d:' % (i+1)) # pure code beautification
else:
code.put('case %2d: ' % (i+1))
item = "PyTuple_GET_ITEM(%s, %d)" % (Naming.args_cname, i) item = "PyTuple_GET_ITEM(%s, %d)" % (Naming.args_cname, i)
self.generate_arg_assignment(arg, item, code) self.generate_arg_assignment(arg, item, code)
if not self.star_arg: if not self.star_arg:
if min_positional_args == 0: if min_positional_args == 0:
code.putln('case 0:') code.put('case 0: ')
code.putln('break;') code.putln('break;')
code.put('default: ') code.put('default: ')
code.put_goto(argtuple_error_label) code.put_goto(argtuple_error_label)
......
...@@ -114,6 +114,21 @@ __doc__ = u""" ...@@ -114,6 +114,21 @@ __doc__ = u"""
>>> call2d(k) >>> call2d(k)
Traceback (most recent call last): Traceback (most recent call last):
TypeError: k() needs keyword-only argument f TypeError: k() needs keyword-only argument f
>>> call0abc(m)
1 2 3
>>> call2c(m)
1 2 1
>>> call3(m)
Traceback (most recent call last):
TypeError: m() takes at most 2 positional arguments (3 given)
>>> call2(m)
Traceback (most recent call last):
TypeError: m() needs keyword-only argument c
>>> call2cd(m)
Traceback (most recent call last):
TypeError: 'd' is an invalid keyword argument for this function
""" """
# the calls: # the calls:
...@@ -212,3 +227,6 @@ def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds): ...@@ -212,3 +227,6 @@ def k(a, b, c=1, *args, d = 42, e = 17, f, **kwds):
kwlist = list(kwds.items()) kwlist = list(kwds.items())
kwlist.sort() kwlist.sort()
print a,b,c,d,e,f, args, kwlist print a,b,c,d,e,f, args, kwlist
def m(a, b=1, *, c):
print a,b,c
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment