How to declare python ints in C extensions?

T

Tony Houghton

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.
 
S

Saju Pillai

Tony said:
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
 
P

Philip Semanchuk

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.

This works for me:
PyModule_AddIntConstant(module, "O_CREAT", O_CREAT);

I've had to learn a lot about writing extensions from looking at the
Python source code. Lots of valuable tricks to be learned there.


HTH
Philip
 
T

Tony Houghton

Thanks, that's perfect. I see it is documented, but I didn't know where
to look.
This trick makes it even easier:

#ifndef PyModule_AddIntMacro
#define PyModule_AddIntMacro(m, c) PyModule_AddIntConstant(m, #c, c)
#endif

Good idea, but I'm a lot more experienced with C in general than in
interfacing it with python, so I already thought of it :).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top