extension modules in C for python, check for korrekt Object
steve.menard at videotron.ca
Wed Jun 2 16:23:36 CEST 2004
Torsten Mohr wrote:
> i write a set of extension modules for python in C.
> I also use some new types that i defined there.
> At the moment i write some methods that expect some
> of these types as parameters. I now want to check
> that the right types were given as parameters.
> How can i do that?
> PyArg_ParseTuple only checks that an object was
> given as parameter, but not its type.
> Do i need to check tp_name in the objects type?
> Thanks for hints,
The PyArg_ParseTuple method can do type checking for you, as long as
your requirements are pretty simple.
if you method accepts only one argument, and that argument must be of a
specific type, the following will work :
PyObject* some_method(PyObject* self, PyObject* args)
if (! PyArg_ParseTuple(args, "O!", &typeStruct, &value))
// Exception ahs already been set byt the call ... so
// only return NULL
typeStruct is a reference to the PyObejctType structure that defines the
type. either the one you defined or those already available through python.
More information about the Python-list