[Python-Dev] Constants from C headers and int/long unification
Guido van Rossum
guido at python.org
Fri Dec 5 18:24:05 EST 2003
> I have C header files which I parse and to which I can do some simple
> processing before outputting them as Python code. An example C file
> will contain
>
> enum {
> kConstantOne = 0xffffffff,
> kConstantTwo = -1,
> };
>
> Regexps will grab the two constants and output them to Python. The
> Python programmer will pass these constants to an API routine, at which
> point I need a PyArg_Parse format char that will convert both of these
> values to the 32 bit pattern 0xffffffff.
>
> Question 1: Do I need to add anything to the constants before
> outputting them as Python code?
No -- kConstantTwo automatically become a long with value 2**32-1 in
Python 2.3 and up.
> Question 2: Is there a format char that will convert both of the values
> produced by the answer to question 1 to the 32 bit pattern 0xffffffff?
If you mean a PyArg_Parse format char, yes: use 'k'.
If you meant a Python % format char, you'd have to write "%x" % (x&0xffffffff).
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list