How to declare python ints in C extensions?

Saju Pillai saju.pillai at gmail.com
Sun Jan 4 13:38:05 EST 2009


Tony Houghton wrote:
> I want to write python wrappers for the Linux DVB API. The underlying
> structures and constants may change from time to time, and some of the
> constants are generated from macros, so I think it would be better to
> write the module in C rather than just copying the constants into pure
> python code and using python's ioctl and struct modules.
> 
> The trouble is I can't find out how to define a simple python int
> variable/constant in a C extension. The docs only seem to tell you how
> to define functions/methods and new types.
> 
> For example, where the kernel header dvb/frontend.h defines:
> 
> typedef enum fe_type {
> 	FE_QPSK,
> 	FE_QAM,
> 	FE_OFDM,
> 	FE_ATSC
> } fe_type_t;
> 
> I want to make them available as if there was a python module
> dvb/frontend.py containing:
> 
> FE_QPSK = 0
> FE_QAM = 1
> FE_OFDM = 2
> FE_ATSC = 3
> 
> but in real life the module would be dvb/frontendmodule.c.
> 

Create Python Int objects for each of these constants and set them into 
your module object's dictionary

foo = PyInt_FromLong(1L);
PyDict_SetItemString(PyModule_GetDict(your_module), "foo", foo);
Py_DECREF(foo)


srp
-- 
http://saju.net.in



More information about the Python-list mailing list