extension modules in C for python, check for korrekt Object

T

Torsten Mohr

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.
 
G

Greg Ewing

Torsten said:
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.

Use PyObject_IsInstance().
 
H

Heiko Wundram

Am Mittwoch, 2. Juni 2004 05:26 schrieb Greg Ewing:
Use PyObject_IsInstance().

Or PyObject_TypeCheck()... All documented in the Python C/API documentation on
python.org.

Heiko.
 
M

Michael Hudson

Torsten Mohr said:
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.

It can check type, using the "O!" incantation.

If you're asking "how do I get hold of the type object I defined in
some other extension module?", then that's a good question. One
approach is to use "CObjects":

http://docs.python.org/ext/using-cobjects.html
Do i need to check tp_name in the objects type?

This sounds fragile :)

Cheers,
mwh
 
S

Steve Menard

Torsten said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top