From: arcege at shore.net (Michael P. Reilly)
Date: Fri, 30 Apr 1999 20:17:24 GMT
Subject: Extension Doc bug
References: <WtnU2.298$7j4.117754@news.shore.net> <14120.52559.376120.364972@weyr.cnri.reston.va.us>
Message-ID: <odoW2.207$9L5.69331@news.shore.net>
Content-Length: 2347
X-UID: 1659                                                  

Fred L. Drake <fdrake at cnri.reston.va.us> wrote:

: Michael P. Reilly writes:
:  > I just spent the morning trying to find a very obscure bug related to
:  > the passing keyword arguments to a builtin method/function.

: Michael,
:   You didn't post your original code that exhibited the bug, so I
: can't be sure of my conclusions.  If you can send source for enough of 
: your extension module that someone can compile it, that would be
: helpful.
:   My first inclination, however, is that you passed in illegal
: arguments to PyArg_ParseTupleAndKeywords().  Passing NULL for the
: keywords dictionary is allowed; I've been looking at the
: implementation and don't see a way for that to be a problem (but I
: might have missed something).


I don't have the code anymore, I took a more rudimentary approach.

I wanted something to emulate PyArg_ParseTuple(args, ""); the documentation
for PyArg_ParseTupleAndKeywords() states that the format is exactly as
documented for PyArg_ParseTuple() ('A format string of zero or more "format
units"'), so I wrote something along the lines of:

  { static char *kwlist[] = { NULL };
    if (PyArg_ParseTupleAndKeywords(args, keywds, "", kwlist)) {
      /* process no arguments */
    } else {
      /* process arguments */
    }
  }

My current code is:
    /* are there arguments?  keywds might be NULL */
    if (PyTuple_Size(args) > 0 ||
        (keywds != NULL && PyDict_Size(keywds) > 0)) {
      PyErr_Clear();
      if (ExpPy_stty(self->id, args, keywds)) {
        result = Py_None;
        Py_INCREF(Py_None);
      }
    } else
      result = ExpPy_unparse_stty_tuple(self->id);
    return result;

And this seems to do me well enough.

Still, regardless of there being a source bug (and my original posting
suggested that there was no bug), API users should be told that kwdict
might be passed NULL.  Currently the doc only states (based on the
contents of http://www.python.org/doc/ext/parseTupleAndKeywords.html at
Fri Apr 30 16:14:26 EDT 1999):
  The arg and format parameters are identical to those of the
  PyArg_ParseTuple() function.  The kwdict parameter is a dictionary of
  keywords received as the third parameter from the Python runtime.

Nothing is mentioned about the kwdict parameter being NULL, and that is
a documentation error (and the jist of my posting).

  -Arcege





