Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > extension modules in C for python, check for korrekt Object

Reply
Thread Tools

extension modules in C for python, check for korrekt Object

 
 
Torsten Mohr
Guest
Posts: n/a
 
      06-01-2004
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.

 
Reply With Quote
 
 
 
 
Greg Ewing
Guest
Posts: n/a
 
      06-02-2004
Torsten Mohr wrote:
> 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().

--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg

 
Reply With Quote
 
 
 
 
Heiko Wundram
Guest
Posts: n/a
 
      06-02-2004
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.

 
Reply With Quote
 
Michael Hudson
Guest
Posts: n/a
 
      06-02-2004
Torsten Mohr <(E-Mail Removed)> writes:

> 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

--
Of the four project development variables - scope, cost, time and
quality - quality isn't really a free variable. The only possible
values are "excellent" and "insanely excellent", depending on
whether lives are at stake. -- Kent Beck, XP Explained
 
Reply With Quote
 
Steve Menard
Guest
Posts: n/a
 
      06-02-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FILE object in Python3.0 extension modules Joachim Dahl Python 6 06-03-2009 06:55 AM
How to test python extension modules during 'make check' / 'make distcheck'? Mark Asbach Python 1 11-03-2006 02:29 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Unload extension modules when python22.dll unloads... [using C extension interpreter] Anand Python 3 11-08-2003 05:50 AM



Advertisments