Creating custom Python objects from C code

Antoine Pitrou solipsis at pitrou.net
Wed Jan 5 11:39:00 EST 2011


On Wed, 5 Jan 2011 11:27:02 -0500
Eric Frederich <eric.frederich at gmail.com> wrote:
> I have read through all the documentation here:
> 
>     http://docs.python.org/extending/newtypes.html
> 
> I have not seen any documentation anywhere else explaining how to
> create custom defined objects from C.
> I have this need to create custom objects from C and pass them as
> arguments to a function call.

What do you mean? Create instances of a type defined in Python code?

The C API is not very different from Python-land. When you want to
instantiate a type, just call that type (as a PyObject pointer) with the
right arguments (using PyObject_Call() and friends). Whether that type
has been defined in C or in Python does not make a difference.

> Question 2: How do I make C helper functions that are part of my
> extension available to other C projects in the same way that PyList_*,
> PyString_*, PyInt_* functions are available?
> Is it possible to have distutils make a .lib file for me?

I don't know. I'd say "probably" :S
Otherwise you can use the PyCapsule system, but that seems quite a bit
more involved: http://docs.python.org/c-api/capsule.html

Regards

Antoine.





More information about the Python-list mailing list