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

Reply

Python - Define a constant in Python C extension

 
Thread Tools Search this Thread
Old 02-27-2009, 08:54 PM   #1
Default Define a constant in Python C extension


I'm writing a C extension for Python, is it possible to define
constants in the C code and have them visible from Python?


fredbasset1000@gmail.com
  Reply With Quote
Old 02-27-2009, 09:03 PM   #2
Robert Kern
 
Posts: n/a
Default Re: Define a constant in Python C extension
On 2009-02-27 14:54, wrote:
> I'm writing a C extension for Python, is it possible to define
> constants in the C code and have them visible from Python?


In your init<module> function, create Python objects from the constants
and insert them into the module using PyModule_AddObject(). The
initspam() function in this section does this (though not with #defined
constants):

http://docs.python.org/extending/ext...tension-module

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though
it had
an underlying truth."
-- Umberto Eco



Robert Kern
  Reply With Quote
Old 02-27-2009, 09:15 PM   #3
Mel
 
Posts: n/a
Default Re: Define a constant in Python C extension
wrote:

> I'm writing a C extension for Python, is it possible to define
> constants in the C code and have them visible from Python?


There's an API function to create a module-level name with a given value,
e.g.

PyModule_AddIntConstant (module, "S_IRUSR", S_IRUSR); // user read

In this example, S_IRUSR was defined in one of the .h files for the
functions being wrapped.

Mel.



Mel
  Reply With Quote
Old 02-28-2009, 06:38 AM   #4
Stefan Behnel
 
Posts: n/a
Default Re: Define a constant in Python C extension
wrote:
> I'm writing a C extension for Python, is it possible to define
> constants in the C code and have them visible from Python?


Have you looked at Cython? It allows you to define e.g. enums as "public"
and will generate the rest for you.

http://cython.org

Stefan


Stefan Behnel
  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
Which Domain Extension best reshmaa General Help Related Topics 0 10-07-2009 09:56 AM
Torrent file with wrong file extension Marc DVD Video 3 02-25-2007 04:07 AM
SVGA monitor extension question SBFan2000 A+ Certification 6 12-14-2006 12:42 PM
DVD Verdict reviews: THE CONSTANT GARDENER, TRANSPORTER 2, and more! DVD Verdict DVD Video 0 01-10-2006 09:21 AM
File Extension Raymond A+ Certification 7 02-18-2005 01:24 AM




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