Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b204a423
Commit
b204a423
authored
Jun 05, 2011
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
greatly improve argument parsing error messages (closes #12265)
parent
40b408d4
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
301 additions
and
198 deletions
+301
-198
Lib/inspect.py
Lib/inspect.py
+59
-50
Lib/test/test_extcall.py
Lib/test/test_extcall.py
+72
-10
Lib/test/test_keywordonlyarg.py
Lib/test/test_keywordonlyarg.py
+1
-1
Misc/NEWS
Misc/NEWS
+3
-0
Python/ceval.c
Python/ceval.c
+166
-137
No files found.
Lib/inspect.py
View file @
b204a423
...
...
@@ -914,6 +914,29 @@ def formatargvalues(args, varargs, varkw, locals,
specs
.
append
(
formatvarkw
(
varkw
)
+
formatvalue
(
locals
[
varkw
]))
return
'('
+
', '
.
join
(
specs
)
+
')'
def
_positional_error
(
f_name
,
args
,
kwonly
,
varargs
,
defcount
,
given
,
values
):
atleast
=
len
(
args
)
-
defcount
if
given
is
None
:
given
=
len
([
arg
for
arg
in
args
if
arg
in
values
])
kwonly_given
=
len
([
arg
for
arg
in
kwonly
if
arg
in
values
])
if
varargs
:
plural
=
atleast
!=
1
sig
=
"at least %d"
%
(
atleast
,)
elif
defcount
:
plural
=
True
sig
=
"from %d to %d"
%
(
atleast
,
len
(
args
))
else
:
plural
=
len
(
args
)
!=
1
sig
=
str
(
len
(
args
))
kwonly_sig
=
""
if
kwonly_given
:
msg
=
" positional argument%s (and %d keyword-only argument%s)"
kwonly_sig
=
(
msg
%
(
"s"
if
given
!=
1
else
""
,
kwonly_given
,
"s"
if
kwonly_given
!=
1
else
""
))
raise
TypeError
(
"%s() takes %s positional argument%s but %d%s %s given"
%
(
f_name
,
sig
,
"s"
if
plural
else
""
,
given
,
kwonly_sig
,
"was"
if
given
==
1
and
not
kwonly_given
else
"were"
))
def
getcallargs
(
func
,
*
positional
,
**
named
):
"""Get the mapping of arguments to values.
...
...
@@ -925,64 +948,50 @@ def getcallargs(func, *positional, **named):
f_name
=
func
.
__name__
arg2value
=
{}
if
ismethod
(
func
)
and
func
.
__self__
is
not
None
:
# implicit 'self' (or 'cls' for classmethods) argument
positional
=
(
func
.
__self__
,)
+
positional
num_pos
=
len
(
positional
)
num_total
=
num_pos
+
len
(
named
)
num_args
=
len
(
args
)
num_defaults
=
len
(
defaults
)
if
defaults
else
0
for
arg
,
value
in
zip
(
args
,
positional
):
arg2value
[
arg
]
=
value
n
=
min
(
num_pos
,
num_args
)
for
i
in
range
(
n
):
arg2value
[
args
[
i
]]
=
positional
[
i
]
if
varargs
:
if
num_pos
>
num_args
:
arg2value
[
varargs
]
=
positional
[
-
(
num_pos
-
num_args
):]
else
:
arg2value
[
varargs
]
=
()
elif
0
<
num_args
<
num_pos
:
raise
TypeError
(
'%s() takes %s %d positional %s (%d given)'
%
(
f_name
,
'at most'
if
defaults
else
'exactly'
,
num_args
,
'arguments'
if
num_args
>
1
else
'argument'
,
num_total
))
elif
num_args
==
0
and
num_total
:
if
varkw
or
kwonlyargs
:
if
num_pos
:
# XXX: We should use num_pos, but Python also uses num_total:
raise
TypeError
(
'%s() takes exactly 0 positional arguments '
'(%d given)'
%
(
f_name
,
num_total
))
else
:
raise
TypeError
(
'%s() takes no arguments (%d given)'
%
(
f_name
,
num_total
))
for
arg
in
itertools
.
chain
(
args
,
kwonlyargs
):
if
arg
in
named
:
if
arg
in
arg2value
:
raise
TypeError
(
"%s() got multiple values for keyword "
"argument '%s'"
%
(
f_name
,
arg
))
else
:
arg2value
[
arg
]
=
named
.
pop
(
arg
)
for
kwonlyarg
in
kwonlyargs
:
if
kwonlyarg
not
in
arg2value
:
try
:
arg2value
[
kwonlyarg
]
=
kwonlydefaults
[
kwonlyarg
]
except
KeyError
:
raise
TypeError
(
"%s() needs keyword-only argument %s"
%
(
f_name
,
kwonlyarg
))
if
defaults
:
# fill in any missing values with the defaults
for
arg
,
value
in
zip
(
args
[
-
num_defaults
:],
defaults
):
if
arg
not
in
arg2value
:
arg2value
[
arg
]
=
value
arg2value
[
varargs
]
=
tuple
(
positional
[
n
:])
possible_kwargs
=
set
(
args
+
kwonlyargs
)
if
varkw
:
arg2value
[
varkw
]
=
named
elif
named
:
unexpected
=
next
(
iter
(
named
))
raise
TypeError
(
"%s() got an unexpected keyword argument '%s'"
%
(
f_name
,
unexpected
))
unassigned
=
num_args
-
len
([
arg
for
arg
in
args
if
arg
in
arg2value
])
if
unassigned
:
num_required
=
num_args
-
num_defaults
raise
TypeError
(
'%s() takes %s %d %s (%d given)'
%
(
f_name
,
'at least'
if
defaults
else
'exactly'
,
num_required
,
'arguments'
if
num_required
>
1
else
'argument'
,
num_total
))
arg2value
[
varkw
]
=
{}
for
kw
,
value
in
named
.
items
():
if
kw
not
in
possible_kwargs
:
if
not
varkw
:
raise
TypeError
(
"%s() got an unexpected keyword argument %r"
%
(
f_name
,
kw
))
arg2value
[
varkw
][
kw
]
=
value
continue
if
kw
in
arg2value
:
raise
TypeError
(
"%s() got multiple values for argument %r"
%
(
f_name
,
kw
))
arg2value
[
kw
]
=
value
if
num_pos
>
num_args
and
not
varargs
:
_positional_error
(
f_name
,
args
,
kwonlyargs
,
varargs
,
num_defaults
,
num_pos
,
arg2value
)
if
num_pos
<
num_args
:
for
arg
in
args
[:
num_args
-
num_defaults
]:
if
arg
not
in
arg2value
:
_positional_error
(
f_name
,
args
,
kwonlyargs
,
varargs
,
num_defaults
,
None
,
arg2value
)
for
i
,
arg
in
enumerate
(
args
[
num_args
-
num_defaults
:]):
if
arg
not
in
arg2value
:
arg2value
[
arg
]
=
defaults
[
i
]
for
kwarg
in
kwonlyargs
:
if
kwarg
not
in
arg2value
:
if
kwarg
not
in
kwonlydefaults
:
raise
TypeError
(
"%s() requires keyword-only argument %r"
%
(
f_name
,
kwarg
))
arg2value
[
kwarg
]
=
kwonlydefaults
[
kwarg
]
return
arg2value
# -------------------------------------------------- stack frame extraction
...
...
Lib/test/test_extcall.py
View file @
b204a423
...
...
@@ -66,17 +66,17 @@ Verify clearing of SF bug #733667
>>> g()
Traceback (most recent call last):
...
TypeError: g() takes at least 1
argument (0 given)
TypeError: g() takes at least 1
positional argument but 0 were given
>>> g(*())
Traceback (most recent call last):
...
TypeError: g() takes at least 1
argument (0 given)
TypeError: g() takes at least 1
positional argument but 0 were given
>>> g(*(), **{})
Traceback (most recent call last):
...
TypeError: g() takes at least 1
argument (0 given)
TypeError: g() takes at least 1
positional argument but 0 were given
>>> g(1)
1 () {}
...
...
@@ -151,7 +151,7 @@ What about willful misconduct?
>>> g(1, 2, 3, **{'x': 4, 'y': 5})
Traceback (most recent call last):
...
TypeError: g() got multiple values for
keyword
argument 'x'
TypeError: g() got multiple values for argument 'x'
>>> f(**{1:2})
Traceback (most recent call last):
...
...
@@ -263,29 +263,91 @@ the function call setup. See <http://bugs.python.org/issue2016>.
>>> f(**x)
1 2
A obscure message
:
Some additional tests about positional argument errors
:
>>> def f(a, b):
... pass
>>> f(b=1)
Traceback (most recent call last):
...
TypeError: f() takes exactly 2 arguments (1 given)
The number of arguments passed in includes keywords:
TypeError: f() takes 2 positional arguments but 1 was given
>>> def f(a):
... pass
>>> f(6, a=4, *(1, 2, 3))
Traceback (most recent call last):
...
TypeError: f()
takes exactly 1 positional argument (5 given)
TypeError: f()
got multiple values for argument 'a'
>>> def f(a, *, kw):
... pass
>>> f(6, 4, kw=4)
Traceback (most recent call last):
...
TypeError: f() takes exactly 1 positional argument (3 given)
TypeError: f() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given
>>> def f(a):
... pass
>>> f()
Traceback (most recent call last):
...
TypeError: f() takes 1 positional argument but 0 were given
>>> def f(a, b):
... pass
>>> f(1)
Traceback (most recent call last):
...
TypeError: f() takes 2 positional arguments but 1 was given
>>> def f(a, *b):
... pass
>>> f()
Traceback (most recent call last):
...
TypeError: f() takes at least 1 positional argument but 0 were given
>>> def f(a, *, kw=4):
... pass
>>> f(kw=4)
Traceback (most recent call last):
...
TypeError: f() takes 1 positional argument but 0 positional arguments (and 1 keyword-only argument) were given
>>> def f(a, b=2):
... pass
>>> f()
Traceback (most recent call last):
...
TypeError: f() takes from 1 to 2 positional arguments but 0 were given
>>> def f(a, *b):
... pass
>>> f()
Traceback (most recent call last):
...
TypeError: f() takes at least 1 positional argument but 0 were given
>>> def f(*, kw):
... pass
>>> f(3, kw=4)
Traceback (most recent call last):
...
TypeError: f() takes 0 positional arguments but 1 positional argument (and 1 keyword-only argument) were given
>>> def f(a, c=3, *b, kw):
... pass
>>> f()
Traceback (most recent call last):
...
TypeError: f() takes at least 1 positional argument but 0 were given
>>> f(kw=3)
Traceback (most recent call last):
...
TypeError: f() takes at least 1 positional argument but 0 positional arguments (and 1 keyword-only argument) were given
>>> f(kw=3, c=4)
Traceback (most recent call last):
...
TypeError: f() takes at least 1 positional argument but 1 positional argument (and 1 keyword-only argument) were given
"""
import
sys
...
...
Lib/test/test_keywordonlyarg.py
View file @
b204a423
...
...
@@ -78,7 +78,7 @@ class KeywordOnlyArgTestCase(unittest.TestCase):
pass
with
self
.
assertRaises
(
TypeError
)
as
exc
:
f
(
1
,
2
,
3
)
expected
=
"f() takes
at most 2 positional arguments (3 given)
"
expected
=
"f() takes
from 1 to 2 positional arguments but 3 were given
"
self
.
assertEqual
(
str
(
exc
.
exception
),
expected
)
def
testSyntaxErrorForFunctionCall
(
self
):
...
...
Misc/NEWS
View file @
b204a423
...
...
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins
-----------------
- Issue #12265: Make error messages produced by passing an invalid set of
arguments to a function more informative.
- Issue #12225: Still allow Python to build if Python is not in its hg repo or
mercurial is not installed.
...
...
Python/ceval.c
View file @
b204a423
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment