This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: kwargs handled incorrectly
Type: Stage:
Components: Interpreter Core Versions: Python 2.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jhylton Nosy List: dalke, jhylton, rhettinger
Priority: high Keywords:

Created on 2003-05-07 06:10 by dalke, last changed 2022-04-10 16:08 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
ceval.diff rhettinger, 2003-05-07 14:43 Diff for ceval.c
Messages (6)
msg15883 - (view) Author: Andrew Dalke (dalke) * (Python committer) Date: 2003-05-07 06:10
I'm using Python2.3 (first with a1 then tested with CVS version 
"Python 2.3b1+ (#7, May  6 2003, 23:41:12)" compiled a few
minutes ago).

There's a bug in how it handles an error condition with keyword
arguments.  Here's a reproducible

Python 2.3b1+ (#7, May  6 2003, 23:41:12) 
[GCC 3.1 20020420 (prerelease)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> def add(a, b):
...   print "add", repr(a), repr(b)
...   return a + b
... 
>>> add(a=3)
add 'a' 3
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 3, in add
TypeError: cannot concatenate 'str' and 'int' objects
>>>

The expected behaviour is what Python 2.2 does, which is

>>> def add(a, b):
...   return a + b
... 
>>> add(a=3)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: add() takes exactly 2 non-keyword arguments (1 given)
>>> 

msg15884 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-07 07:54
Logged In: YES 
user_id=80475

Ouch! This is a bad one.  Bumping up the priority.


msg15885 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-07 09:23
Logged In: YES 
user_id=80475

def f(a,b):
    print a, b

def g(a,b,**kw):
    print a, b, kw
	
f(c=3)   # prints c 3
g(c=3)  # raises correct error for non-keyword arguments.

So, the restated version of the problem is that:
when keyword arguments are passed to a function not 
defining **kw, then the keyword in interpreted as a single 
for the first positional argument and the keyword value as 
the second positional argument.

Weird and bad!

Reverting to earlier versions of getargs.c did not help.  
Likewise the dict(red=3, blue=4) patch is not the culprit.
The prime suspect is now the function call optimizations.
msg15886 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-07 10:37
Logged In: YES 
user_id=80475

Jeremy, it looks like this bug was part of your patches on 
2/5/2003.  CVS runs the code the day before, but not on 
the day after.

The culprit is likely in taking the fast_function() path which 
bypasses the usual keyword argument handling.  See 
ceval.c 2.348 and the related changes to sysmodule.c and 
ceval.h.
msg15887 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-07 14:42
Logged In: YES 
user_id=80475

Attaching a patch (actually a band-aid temporary fix).
msg15888 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-05-31 07:04
Logged In: YES 
user_id=80475

Fixed.  See:
   Python/ceval.c 2.363
   Lib/test/test_extcall.py 1.20
History
Date User Action Args
2022-04-10 16:08:36adminsetgithub: 38455
2003-05-07 06:10:25dalkecreate