Go Back   Velocity Reviews > Newsgroups > Python
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

Python - How to declare python ints in C extensions?

 
Thread Tools Search this Thread
Old 01-04-2009, 05:20 PM   #1
Tony Houghton
 
Posts: n/a
Default How to declare python ints in C extensions?

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

  Reply With Quote
Old 01-04-2009, 05:38 PM   #2
Saju Pillai
 
Posts: n/a
Default Re: How to declare python ints in C extensions?

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
  Reply With Quote
Old 01-04-2009, 05:46 PM   #3
Philip Semanchuk
 
Posts: n/a
Default Re: How to declare python ints in C extensions?


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
  Reply With Quote
Old 01-04-2009, 10:47 PM   #4
Tony Houghton
 
Posts: n/a
Default Re: How to declare python ints in C extensions?

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

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47