On Dec 6, 2008, at 6:49 PM, David Cournapeau wrote:

> On Sun, Dec 7, 2008 at 4:46 AM, Dag Sverre Seljebotn
> <[EMAIL PROTECTED]> wrote:
>> David Cournapeau wrote:
>>> Hi,
>>>
>>> I would like to know whether adding an IFDEF-like feature in cython
>>> would be feasible ? I would like to define some cython code  
>>> depending
>>> on some defines in the header of the library I am wrapping.
>>
>> Could you tell us what your exact usecase is?
>
> Sure. I am wrapping a library which supports several file formats: for
> each format, there is an enum value:
>
> enum {
> FORMAT1,
> ...
> FORMATN
> }
>
> New versions add some new enums, which can be used in the library. I
> would like to support the new formats, without forcing people to use
> the new library. For now, I hardcode the enum value for the recent
> added values, but that's obviously not a nice solution.
>
>> Some things are possible
>> to do in other ways. For instance, if there's a type that's sometimes
>> "long" and sometimes "short", then Cython doesn't really care about
>> that, you can always use "int" in Cython anyway. There might be  
>> similar
>> workarounds for whatever your case is.
>
> it won't work in that case: I want to use a symbol which may or not
> exist depending on the header version, not a symbol whose value may
> change.


Cython doesn't actually read the .h files, so it can't figure out if  
a certain symbol is or is not defined. But if I understand your  
question correction, you want to be able to write some code with  
emits the #ifdef's?

I would be in favor of adding something like this to the language,  
but cython.defined(SOME_NAME) makes it sound like its asking if  
SOME_NAME is defined in the current scope (e.g. with a DEF  
statement). Perhaps something like

if cython.cmacro(cython.defined(SOME_NAME)):
     ...
else:
     ...

would generate

#if defined(SOME_NAME)
...
#else
...
#endif

One could then use it for more general macros as well. (Note that it  
still couldn't be used inside struct/enum/class definitions).

As a hack, you can do

cdef extern from *:
     cdef void emit_ifdef "#if defined(SOME_NAME) //" ()
     cdef void emit_endif "#endif //" ()

emit_ifdef()
print "hi"
emit_endif()

- Robert

_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to