Checking the type of a PyObject
Alex Martelli
aleaxit at yahoo.com
Sun Aug 12 03:38:03 EDT 2001
"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:Hdod7.27408$c8.8315711 at news1.denver1.co.home.com...
...
> One approach would be the obvious:
>
> Mod_Function( PyObject *self, PyObject *args ) {
> char * String;
> long IVal;
> if (PyArg_ParseTuple( args, "s", &String )) {
> /* do something with String */
> } else if (PyArg_ParseTuple( args, "i", &IVal)) {
> /* do something with IVal */
One caveat: I think you need to reset the error-situation here,
as the first (failing) PyArg_ParseTuple has set the exception
and I don't think this second one resets it (I could be wrong --
no direct experience of this idiom, as I strive to avoid
typechecks whenever I can...).
Another issue is that "i" also accepts and silently truncates
floats, which may or may not be the right thing to do (it's
sure quite different from checking the type of the object
for equality to integer-type:-).
> } else {
> return NULL;
And here I think the error message would only mention
needing a string argument, so you probably want to
substitute for it one mentioning both string and int.
> }
> }
Alex
More information about the Python-list
mailing list