Torsten Mohr wrote:
Hi,
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,
Torsten.
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)
{
PyObject* value;
if (! PyArg_ParseTuple(args, "O!", &typeStruct, &value))
{
// Exception ahs already been set byt the call ... so
// only return NULL
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.
Steve