[New-bugs-announce] [issue2677] Argument rules in callables do not apply when function uses PyArg_ParseTuple

Ludovico Gardenghi report at bugs.python.org
Thu Apr 24 12:01:26 CEST 2008

New submission from Ludovico Gardenghi <garden at acheronte.it>:

(It seems strange to me that this issue hasn't been raised in the past,
maybe I just failed to find it in the BTS. In that case please excuse me
and please point me to the original discussion.)

The Language Reference, section 5.3.4, states that, for every callable

"[...] If keyword arguments are present, they are first converted to
positional arguments, as follows. First, a list of unfilled slots is
created for the formal parameters. [...] Next, for each keyword
argument, the identifier is used to determine the corresponding slot (if
the identifier is the same as the first formal parameter name, the first
slot is used, and so on). [...]"

This is not true if the function is defined using the C function
PyArg_ParseTuple, and this happens a lot in the standard library. I
discovered it trying to call os.open("filename", flag=os.O_RDONLY), just
to make an example. In this case it seems useless to specify the
keyword, but I have to write a generic "wrapping" function that takes a
function name and its arguments (as keyword arguments) and call the
original function after having changed the content of some of the arguments.

Apart from the reason, I believe that this behavior is inconsistent with
the language definition and should be fixed. I'm very new to Python, but
maybe the format string of ParseTuple should be extended in order to
accept also the name of the positional arguments? Or something like
ParseTupleAndKeywords should be used instead?

I tried only on Python 2.4 and 2.5 but I took a look at the source code
of 2.6 and 3.0 and I believe the issue is still there.

components: Library (Lib)
messages: 65712
nosy: garden
severity: normal
status: open
title: Argument rules in callables do not apply when function uses PyArg_ParseTuple
type: behavior
versions: Python 2.4, Python 2.5, Python 2.6, Python 3.0

Tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list