473,396 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

extension modules in C for python, check for korrekt Object

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.

Jul 18 '05 #1
4 1995
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

Jul 18 '05 #2
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.

Jul 18 '05 #3
Torsten Mohr <tm***@s.netic.de> 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
Jul 18 '05 #4
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
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Peter Sparago | last post by:
(Sorry in advance for the long post.) Hi, I'm having a great deal of difficulty buiding a Python COM extension. I am using the MSHTML ActiveX control in my application but I need to interact...
0
by: Mark English | last post by:
Basic problem: If there is a C-extension module in a package and it tries to import another python module in the same package without using the fully qualified path, the import fails. Config:...
3
by: nnorwitz | last post by:
If you don't write or otherwise maintain Python Extension Modules written in C (or C++), you can stop reading. Python 2.5 alpha 1 is in the process of being released later today. There are...
0
by: Neal Norwitz | last post by:
If you don't write or otherwise maintain Python Extension Modules written in C (or C++) or embed Python in your application, you can stop reading. Python 2.5 alpha 1 was released April 5, 2006. ...
1
by: Mark Asbach | last post by:
Hi pythonians, I'm one of the maintainers of an open source image processing toolkit (OpenCV) and responsible for parts of the autotools setup. The package mainly consists of four shared...
1
by: Petr Prikryl | last post by:
Do you think that the following could became PEP (pre PEP). Please, read it, comment it, reformulate it,... Abstract Introduction of the mechanism for language extensions via modules...
5
by: Adam Atlas | last post by:
Does anyone know if it would be possible to create a CPython extension -- or use the ctypes module -- to access Python's own embedding API (http://docs.python.org/api/initialization.html &c.)?...
7
by: MD | last post by:
Hi, I would like to access "variables" defined in my Python program in a C module extension for Python. Is this possible? I looked at the Python C API reference but didn't find anything there...
15
by: kyosohma | last post by:
Hi, I am trying to get a small group of volunteers together to create Windows binaries for any Python extension developer that needs them, much like the package/extension builders who volunteer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.