![]() |
|
|
|||||||
![]() |
Python - How to declare python ints in C extensions? |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Posts: n/a
|
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. -- TH * http://www.realh.co.uk |
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
On Jan 4, 2009, at 1:20 PM, 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. 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 |
|
|
|
#4 |
|
Posts: n/a
|
On Sun, 04 Jan 2009 21:05:14 +0100
Christian Heimes <> wrote: > Philip Semanchuk schrieb: > > 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. 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 -- TH * http://www.realh.co.uk |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Must declare the scalar variable "@Product_ID". | iquad | Software | 0 | 11-10-2007 05:26 AM |
| File Extensions | Hubert Borrmann | DVD Video | 2 | 02-16-2006 05:26 AM |
| Monty Python and the Holy Grail SE DVD questions | Mark | DVD Video | 0 | 04-11-2005 04:29 PM |
| Help regarding damaged Monty Python DVD? | Bex | DVD Video | 5 | 04-16-2004 01:11 AM |
| Universal Python Meaning Of Life reply | Peter Williams | DVD Video | 31 | 09-27-2003 08:13 PM |