Extension Doc bug
Michael P. Reilly
arcege at shore.net
Tue May 4 18:33:16 EDT 1999
Fred L. Drake <fdrake at cnri.reston.va.us> wrote:
: Michael P. Reilly writes:
: > Nothing is mentioned about the kwdict parameter being NULL, and that is
: > a documentation error (and the jist of my posting).
: I must have misunderstood your original post; I thought you meant
: that passing NULL instead of a keywords dictionary caused a core
: dump. (Or did I get that part right?)
No, passing a NULL was causing an exception on the next function call
in the Python interpreter because my C function was returning Py_None,
but PyArg_ParseTupleAndKeywords() raised an exception (I can't
recreate my code right now, so I can't say which branch of the
enclosing, C "if" statement it went down.
So I guess the bug is that the interpreter (interactive mode) should
perform an PyErr_Clear() when the function returns (regardless of the
return value), because on the next function call, the exception will be
printed and the function not called.
Code example is:
#include <stdio.h>
#include <Python.h>
static PyObject *test_with_keywords(self, args, kwdict)
PyObject *self, *args, *kwdict;
{ static char *kwlist[] = { NULL };
int rc;
rc = PyArg_ParseTupleAndKeywords(args, kwdict, "", kwlist);
/* no PyErr_Clear(); */
fprintf(stderr, "rc = %d\n", rc);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef testmodmethods[] = {
{ "test", (PyCFunction)test_with_keywords, METH_VARARGS|METH_KEYWORDS },
{ NULL, NULL },
};
void inittestmod()
{ (void)Py_InitModule("testmod", testmodmethods);
}
Then run:
Python 1.5.2 (#6, Apr 20 1999, 10:35:35) [GCC 2.7.2.2] on sunos5
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> from testmod import test
>>> test()
rc = 1
>>> test()
rc = 1
>>> test(1)
rc = 0
>>> test(1)
TypeError: function requires exactly 0 arguments; 1 given
>>> test(1)
rc = 0
>>> test()
TypeError: function requires exactly 0 arguments; 1 given
>>>
On the last two sets of calls you get a TypeError exception but no
printout from the C function. (This is only in interactive mode.)
: If passing a NULL for the keyword dict caused a core dump, there's
: definately space for a check to be made in the runtime.
There is a check in the code already (see my other post) what if kwdict
is not NULL that a PyDictObject is passed.
: I will make the possibility of NULL for the keywords dictionary
: clear in the documentation after I've had a chance to verify the
: intent with Guido; maybe he'd expect a core dump even if I don't. ;-)
: -Fred
Thanks Fred! :)
-Arcege
More information about the Python-list
mailing list