Re: cytpes and python C API combinations

Mark McMahon <[email protected]>
Newsgroups gmane.comp.python.ctypes
Message-ID <36FA17CA56D6A84D9E422A359CB4F047705BC5A543@ADSK-NAMSG-02.MGDADSK.autodesk.com>
Hi,

I have done stuff like this:

I wanted to create an addin for a product which provided a C++ API...

I created a very simple forwarding wrapper in C + Pyrex to the C++ api (which called the python code, but needed to pass an pointer to a C instance).

In general I just treated all pointers as c_long, and python/ctypes for the most part treated them has handles (i.e. it never did anything with them). For some structs that python needed access to - I just called ctypes_type_or_structure.from_address(pointer)  (where ctypes_type_or_structure was usually an instance of some ctypes.Structure class).

For example...
- Application asks the plugin to do something (passing various bits of information, one or more of which are pointers to C stuff)
- The part of my plugin receives this and calls the appropriate Python with same parameters (Pyrex code for me - but could be done with Python C API, most pointers are treated as longs)
- The python code either:
    * knows how to deal with the pointer (e.g. a structure it knows how to deal with) and does a "from_address" (ctypes)
    * or doesn't need to know the internals of the pointer (usually a pointer to a C++ interface) and just stores it for later
- When the python code needs to request something from that interface it calls (ctypes) a method in the plugin DLL which accepts the pointer + other parameters (the function it calls is usually one line, which casts the pointer back to the right C++ type and calls a method of that object with the other parameters)

That is all pretty complex - and I guess my explanation wasn't as simple as I would like, but I hope it gives you an idea?

If not - then I suggest you give a bit more details on what exactly you are trying to achieve.

Hope this helps,
  Mark

-----Original Message-----
From: [email protected] [mailto:[email protected]] 
Sent: Tuesday, January 12, 2010 3:28 AM
To: [email protected]
Subject: [ctypes-users] cytpes and python C API combinations

Ideas on this one? Anyone done anything like this before?

>From C, I load a python module, call a python function, and pass it
parameters. Ideally, I would like one of those parameters to be a c_void_p
object pointing to something from the C world. 

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
ctypes-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ctypes-users

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.