473,699 Members | 2,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ParseTupl e 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 2008
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_IsInst ance().

--
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_IsInst ance().


Or PyObject_TypeCh eck()... 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_ParseTupl e 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_ParseTupl e 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_ParseTupl e 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(PyO bject* self, PyObject* args)
{
PyObject* value;

if (! PyArg_ParseTupl e(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
2684
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 with and implement some Custom COM interfaces. All is well with the basic operation of the control using Python. Basically, I want to take over the right-click (context) menu from
0
1368
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: Python 2.4 on Windows 2000 For example: mypackage contains:
3
1005
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 important changes that are in 2.5 to support 64-bit systems. These changes can cause Python to crash if your module is not upgraded to support the changes. Python was changed internally to use 64-bit values on 64-bit machines for indices. If you've...
0
846
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. The second alpha should be released in a few weeks. There are several changes which can cause C extension modules or embedded applications to crash the interpreter if not fixed. Periodically, I will send out these reminders with updated...
1
1626
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 libraries but is accompanied by a python package containing some pure python code and of course extension modules for the four libraries. Now during the last month we were preparing a major release which means testing, fixing, testing, fixing, ... in...
1
2082
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 written using other languages. Extensions of Python could be done via special interpreter extensions. From Python sources, the special modules would look like other modules, with the Python API (the key feature from
5
1641
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.)? Could a Python program itself create a sub-interpreter, and work with it with all the privileges and capabilities that an actual C program would have? I realize that this may be a bit too... mystical? ... for a lot of people's tastes, but I'm just...
7
1541
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 that could help me. Thanks in advance for any help/tips. Regards, -MD
15
3220
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 their time to create Linux RPMs. The main thing I need are people willing to test the binaries to make sure the extension is stable. This would require installing the binary and probably downloading the source too to get the developer's test
0
8691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9180
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9038
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8920
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7755
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6536
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2012
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.