msg128902 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-02-20 12:11 |
inspect.getcallargs raises TypeError if given a function with only **kwargs, and some keyword arguments: Python 3.3a0 (py3k:88451, Feb 20 2011, 12:37:22) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> from inspect import getcallargs >>> >>> def f(**kwargs): pass ... >>> f(a=1, b=2) >>> >>> getcallargs(f, a=1, b=2) Traceback (most recent call last): ... TypeError: f() takes no arguments (2 given) In line 946 of inspect.py the "num_args == 0 and num_total" condition is true: the function expects 0 positional arguments and got more than zero arguments, but in this case these are keyword arguments, so it shouldn't raise TypeError. |
|
|
msg128907 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-02-20 13:32 |
Here is a patch. It also includes tests that would have detected this bug. It also corrects a case when getcallargs raised an exception with a different message (there are tests also for this): >>> def f(**kwargs): pass ... >>> f(1, a=2) Traceback (most recent call last): ... TypeError: f() takes exactly 0 positional arguments (2 given) >>> >>> getcallargs(f, 1, a=2) Traceback (most recent call last): ... TypeError: f() takes no arguments (2 given) There is a comment in the patch about this case: the message given by Python is also incorrect, because it says that 2 positional arguments are given, but there was only 1 positional argument (the other was a keyword argument). The patch currently handles this case by producing the same (incorrect) message as Python. |
|
|
msg129538 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-02-26 12:54 |
Updated patch with extra tests. |
|
|
msg129608 - (view) |
Author: Andreas Stührk (Trundle) * |
Date: 2011-02-27 03:20 |
Confirmed under Python 2.7, 3.2 and 3.3. Patch looks good to me. Attached is a patch for 2.7. |
|
|
msg129620 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-02-27 10:09 |
I found another case, when this is a problem: if there are no **kwargs, but there are some keyword-only arguments: >>> def f(*, a, b): pass ... >>> f(a=1, b=2) >>> >>> getcallargs(f, a=1, b=2) Traceback (most recent call last): ... TypeError: f() takes no arguments (2 given) The attached issue11256_3.diff patch also fixes this problem, and adds tests that would have detected this case. |
|
|
msg131340 - (view) |
Author: Daniel Urban (daniel.urban) *  |
Date: 2011-03-18 17:20 |
Updated the patch for mercurial. |
|
|
msg132439 - (view) |
Author: Roundup Robot (python-dev)  |
Date: 2011-03-28 22:41 |
New changeset 57e99f5f5e8f by Benjamin Peterson in branch '3.2': Correct handling of functions with only kwarg args in getcallargs (closes #11256) http://hg.python.org/cpython/rev/57e99f5f5e8f New changeset b19d76d9d2a7 by Benjamin Peterson in branch '2.7': Correct handling of functions with only kwarg args in getcallargs (closes #11256) http://hg.python.org/cpython/rev/b19d76d9d2a7 |
|
|