[Patches] [ python-Patches-985713 ] bug skipping optional keyword arguments of type "w#"

SourceForge.net noreply at sourceforge.net
Wed Jun 1 20:24:29 CEST 2005


Patches item #985713, was opened at 2004-07-06 06:37
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=305470&aid=985713&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Core (C code)
Group: None
>Status: Closed
>Resolution: Out of Date
Priority: 5
Submitted By: Mark D. Roth (mdr0)
Assigned to: Nobody/Anonymous (nobody)
Summary: bug skipping optional keyword arguments of type "w#"

Initial Comment:
When using PyArg_ParseTupleAndKeywords(), if you use
"w#" as an optional keyword argument, you cannot skip
it and still specify an optional argument that follows
it.  For example, take the following function:

static PyObject *
dummy_func(PyObject *self, PyObject *args, PyObject *kwds)
{
        char *string;
        char *buf = NULL;
        int bufsize = 0;
        unsigned short ushort = 5;
        static char *kwlist[] = { "string", "buffer",
"ushort", NULL };

        if (! PyArg_ParseTupleAndKeywords(args, kwds,
"s|w#H", kwlist,
                                          &string,
                                          &buf,
                                          &bufsize,
                                          &ushort))
                return NULL;

        printf("buf=%p, bufsize=%d, ushort=%hu\n", buf,
bufsize, ushort);
        snprintf(buf, bufsize, "you said \%s\\n",
hostname);

        return Py_BuildValue("s", hostname);
}

You can call this function successfully as follows:

# don't specify buffer or ushort
dummy_func("text")

Or like this:

# specify buffer
buf = array.array('c', [' ' for x in range(0, 1023)])
dummy_func("text", buffer=buf)

Or like this:

# specify buffer and ushort
buf = array.array('c', [' ' for x in range(0, 1023)])
dummy_func("text", buffer=buf, ushort=10)

However, the following does NOT work:

# specify ushort without first specifying buffer
dummy_func("text", ushort=10)

It fails with the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: argument 2 impossible<bad format char>

This is because the skipitem() function in
Python/getargs.c does not know how to skip arguments
for the "w#" format unit.
I've attached a patch that fixes this.

Note that skipitem() is also missing code for many
other legal format units, in addition to "w" and "w#".
 In particular:

  u, u#     (see patch #853890)
  es, es#
  et, et#
  I (capital i)
  k
  K
  D
  S
  U
  t#
  (tuple)

It might be a good idea to refactor the code so that
if/when new format units are added, the same code will
handle them for both skipitem() and convertsimple().


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

>Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-06-01 20:24

Message:
Logged In: YES 
user_id=1188172

Closing; subsumed by #1212928.

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

Comment By: Michiel de Hoon (mdehoon)
Date: 2005-02-27 15:41

Message:
Logged In: YES 
user_id=488897

I applied your patch and ran the Python test suite, finding
no problems with this patch. I'll send a patch review to
python-dev.

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

Comment By: Mark D. Roth (mdr0)
Date: 2004-07-06 06:42

Message:
Logged In: YES 
user_id=994239

Whoops!  You'll need to change "hostname" to "string" in
that example function.  I missed a few occurances of that
variable when I renamed it to make the example more clear...
*sheepish grin*


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

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


More information about the Patches mailing list