Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile time DEF expression inside IF/ELIF/ELSE statement #1796

Closed
ameli opened this issue Jul 24, 2017 · 3 comments · Fixed by #1799
Closed

Compile time DEF expression inside IF/ELIF/ELSE statement #1796

ameli opened this issue Jul 24, 2017 · 3 comments · Fixed by #1799
Milestone

Comments

@ameli
Copy link

ameli commented Jul 24, 2017

I am trying to use processor's DEF inside IF. I have asked this question here as well. The equivalent code in C of what I am trying to write is:

#define A 1
#if A == 1
    #define B 2
#else
    #define B 3
#endif

In above B should be 2, whereas it gives 3 if I write it in Cython. Here is a sample:

# in Definitions.pxi
DEF A=1
IF A==1:
    DEF B=2
    print('B should be 2.')
ELSE:
    DEF B=3
    print('B should be 3.')

and

# In test.pyx
include "Definitions.pxi"
print('B is: %d'%B)

and

# In setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
setup(name = "test",ext_modules = cythonize(Extension("*",["test.pyx"])))

Now, testing it:

>>> ipython
In [1]: %reload_ext Cython
In [2]: import test

B should be 2
B is: 3

It seems that the very last DEF statement is being used and all IF as well as ELIF and ELSE are ignored. I am wondering if this is a bug or I am not using it correctly.

Thanks.

@ameli ameli changed the title DEF inside IF/ELIF/ELSE statement Compile time DEF inside IF/ELIF/ELSE statement Jul 24, 2017
@ameli ameli changed the title Compile time DEF inside IF/ELIF/ELSE statement Compile time DEF expression inside IF/ELIF/ELSE statement Jul 24, 2017
@scoder
Copy link
Contributor

scoder commented Jul 24, 2017

Interesting case. Semantically, the code you wrote does not mean what you intended. Instead, IF means: if the condition is true, then include its code block in the compilation. By design, DEF does not belong into the code block, because it is evaluated at compile time, not included at compile time. DEF is a compile time thing and not controlled by conditions itself. Meaning, both DEF assignments are being evaluated, in code order, and the last one survives and goes into the execution of the print().

Now, the fix could go either way: either implement a control flow for DEF inside of conditionals, or disallow indented DEFs. I'm slightly leaning towards the latter since it's not really clear to me that the first is a feature worth having. These compile time expressions have a tendency to be generally overused...

@ameli
Copy link
Author

ameli commented Jul 24, 2017

Thanks for your response. I see the difference now. However it seems the documentation here is not giving this detail. I am quoting

An IF statement can appear anywhere that a normal statement or declaration can appear, and it can contain any statements or declarations that would be valid in that context, including DEF statements and other IF statements.

I am actually using this for early-binding. User at compile time can set which classes to be compiled.

@scoder
Copy link
Contributor

scoder commented Jul 26, 2017

Granted. Fixed in #1799, but I wouldn't include this fix in a minor bug fix release for now.

@scoder scoder closed this as completed Jul 26, 2017
@scoder scoder added this to the 0.27 milestone Aug 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants