473,467 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is there an extension library?

I'm creating Python extensions for several c/c++ components. I'm using
swig to create the extensions. The biggest challenge so far is working with
the c args between Python and the Python extension. Is there a 3rd party
library of extension helpers that assist in representing c-types between
Python and c? Are there any tips and tricks?

Here are some specific scenarios I am running into.

1) Refrencing and derefrencing:
For example, condsider this c api:

void foo_alloc(void** handle_ptr);
void foo_use(void* handle);

In c &handle. How to get pointer to pointer in Python?
In c *handle_ptr. How to derefrence a pointer in Python?
2) Coercion:
For example, suppose you want to initialize a char array in Python.

buf = CharArray(100)
for i in range(100):
buf[i] = i

The above results in:
"TypeError: CharArray___setitem__() argument 3 must be char, not int"
In c, buf[i] = (char)i, is implicit. How do I do it in Python. Why doesn't
Python make this conversion implicitly?
3) It seems like I'm doing something unatural with my Python extension. It
seems someone else may have already figured out the best way to transform c
arguments between Python and Python extensions genericly.

Thanks,
Jason
Jul 18 '05 #1
2 1736
"Letbetter, Jason" <le*******@ti.com> writes:
I'm creating Python extensions for several c/c++ components. I'm using
swig to create the extensions. The biggest challenge so far is working with
the c args between Python and the Python extension. Is there a 3rd party
library of extension helpers that assist in representing c-types between
Python and c? Are there any tips and tricks?
I suggest not to use swig, or any tools - atleast not until you fully
understand how to use these tools (I, for one, don't know how to use
swig, so I don't use it).
1) Refrencing and derefrencing:
For example, condsider this c api:

void foo_alloc(void** handle_ptr);
void foo_use(void* handle);

In c &handle. How to get pointer to pointer in Python?
In c *handle_ptr. How to derefrence a pointer in Python?
I recommend to wrap this in a Python object type:

typedef struct{
PyObject_HEAD
void *handle;
} PyFoo;

Then, in PyFoo_New, invoke foo_alloc, and implement a method PyFoo_use
of the PyFoo objects.
"TypeError: CharArray___setitem__() argument 3 must be char, not int"
In c, buf[i] = (char)i, is implicit. How do I do it in Python. Why doesn't
Python make this conversion implicitly?
Because in Python, characters are *not* numbers (unlike in C). In
Python, characters are strings of length 1.

Again, if you want to store chars in setitem: don't use swig, but
write it yourself.
3) It seems like I'm doing something unatural with my Python extension. It
seems someone else may have already figured out the best way to transform c
arguments between Python and Python extensions genericly.


The best way to transform a C library into a Python extension is to
write the extension module by hand.

Regards,
Martin
Jul 18 '05 #2
Letbetter, Jason wrote:
I'm creating Python extensions for several c/c++ components. I'm using
swig to create the extensions. The biggest challenge so far is working
with
the c args between Python and the Python extension. Is there a 3rd party
library of extension helpers that assist in representing c-types between
Python and c? Are there any tips and tricks?
The vast SWIG manual, at www.swig.org, does give many tips and tricks;
SWIG also comes with a librayr of helpers such as you request.

Here are some specific scenarios I am running into.

1) Refrencing and derefrencing:
For example, condsider this c api:

void foo_alloc(void** handle_ptr);
void foo_use(void* handle);

In c &handle. How to get pointer to pointer in Python?
In c *handle_ptr. How to derefrence a pointer in Python?
You can use typemaps to ensure "void** handle_ptr" as an
argument becomes a "return value" (some opaque representation
of the resulting void*) in Python, and you'll just pass that
opaque value back into foo_use, no need to dereference.

2) Coercion:
For example, suppose you want to initialize a char array in Python.

buf = CharArray(100)
for i in range(100):
buf[i] = i

The above results in:
"TypeError: CharArray___setitem__() argument 3 must be char, not int"
In c, buf[i] = (char)i, is implicit. How do I do it in Python. Why
doesn't Python make this conversion implicitly?
In Python, "a character" is "a string of length one" and implicitly
converting that to/from "an integer that happens to be the ASCII code
for that character" would be an utter, extremely error-prone design
disaster. Use ord() if you want the ASCII code for the one and only
character in a string of length 1, chr() if you want to make a string
of length one starting from a character's ASCII code. [I don't know
what a CharArray _IS_, and how come you got that weird indirectness
requested, but I'm just answering your questions anyway].

3) It seems like I'm doing something unatural with my Python extension.
It seems someone else may have already figured out the best way to
transform c arguments between Python and Python extensions genericly.


SWIG does a good job (for C). Boost Python does an arguably even
better one (for C++, though -- and you do need a decently standard
compliant C++ compiler that won't choke on abstruse templates; also,
I'm not sure if Boost Python is out yet in a release supporting
Python 2.3 -- when I tried to build from the CVS sources I found
the task quite tiresome).
Alex

Jul 18 '05 #3

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

Similar topics

4
by: Ever Olano | last post by:
Hi. I have the following questions regarding writing a PHP extension. You don't have to answer all questions. Please answer everything you can. I'd really appreciate it. 1. What is the most...
6
by: Gyger | last post by:
Hello, Three weeks ago, I have started to develop a binding extension for Qt and PHP 5. Now, I can display a dialog box containing some widgets like label, buttons and edit line. I have just...
4
by: Mike Walsh | last post by:
I would like to use PHP's Sybase extension. I am running my development area on Windows under IIS. When I enable the Sybase extension, I get a message that indicates the extension didn't load. ...
2
by: Lonnie Princehouse | last post by:
I've been trying to debug this for two days now, and it's a longshot but I'm hoping that someone here might recognize a solution. I've got a C extension which calls a function in a C library,...
4
by: pepcag | last post by:
I used http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconalteringsoapmessageusingsoapextensions.asp as a template to create a very simple web method with soap...
6
by: Pieter | last post by:
Hi, I'm trying to use the Edanmo Shell Extension Library (http://www.mvps.org/emorcillo/en/code/shell/shellextensions.shtml) to make a Context Menu in the Windows Explorer with VB.NET 2005. It...
5
by: malkarouri | last post by:
Hi everyone, Is it possible to write a Python extension that uses the Boehm garbage collector? I have a C library written that makes use of boehm-gc for memory management. To use that, I have...
5
by: Chuck Anderson | last post by:
I run Apache 2.0.55, and Php (both 4.4.1 and 5.2.5) on my home PC (Windows XP). One of the scripts that I run daily needs to access a secure URL (https://..............). When I am running Php4,...
6
by: tommybiegs | last post by:
I'm having a weird problem. I can't seem to force php to load an extension using php.ini, but it loads perfectly if I use dl() at the beginning of a test script. In php.ini I've got: ...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
1
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.