[Python-bugs-list] [ python-Bugs-733667 ] kwargs handled incorrectly

SourceForge.net noreply@sourceforge.net
Wed, 07 May 2003 02:23:16 -0700


Bugs item #733667, was opened at 2003-05-07 01:10
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=733667&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
Status: Open
Resolution: None
Priority: 6
Submitted By: Andrew Dalke (dalke)
Assigned to: Raymond Hettinger (rhettinger)
Summary: kwargs handled incorrectly

Initial Comment:
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)
>>> 



----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-07 04:23

Message:
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.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-05-07 02:54

Message:
Logged In: YES 
user_id=80475

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




----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=733667&group_id=5470