Skip to content

Instantly share code, notes, and snippets.

@msabramo
Last active December 20, 2015 04:59
Show Gist options
  • Save msabramo/6074572 to your computer and use it in GitHub Desktop.
Save msabramo/6074572 to your computer and use it in GitHub Desktop.
A Possible bug in Cython? This is the code for a message that I'm posting to the cython-users group at https://groups.google.com/forum/#!forum/cython-users
cdef struct MyStruct:
int x
def byte_bugginess(x=1):
cdef char val = 1
cdef MyStruct foo
print("1 - val = %d" % val)
if x in (3, 4):
print("2 - val = %d" % val)
val = 2
print("3 - val = %d" % val)
else:
print("4 - val = %d" % val)
val = foo.x
print("5 - val = %d" % val)
print("6 - val = %d" % val)
test: hello.so
python -c 'import hello; hello.byte_bugginess()'
hello.so: hello.pyx
python setup.py build_ext --inplace
clean:
$(RM) -r *.so *.c *.pyc __pycache__ *.egg-info build
import sys
try:
from setuptools import setup
from setuptools.extension import Extension
except ImportError:
print("Couldn't import setuptools. Falling back to distutils.")
from distutils.core import setup
#
# Force `setup_requires` stuff like Cython to be installed before proceeding
#
from setuptools.dist import Distribution
Distribution(dict(setup_requires='Cython'))
try:
from Cython.Distutils import build_ext
except ImportError:
print("Could not import Cython.Distutils. Install `cython` and rerun.")
sys.exit(1)
ext_modules = [Extension("hello", ["hello.pyx"])]
setup(
name = 'Hello world app',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules,
setup_requires = ['Cython'],
test_suite='test',
)
@msabramo
Copy link
Author

Sample output

marca@marca-mac:~/dev/git-repos/cython-test$ make
python setup.py build_ext --inplace
running build_ext
gcc-4.2 not found, using clang instead
cythoning hello.pyx to hello.c
building 'hello' extension
creating build
creating build/temp.macosx-10.6-intel-2.7
clang -fno-strict-aliasing -fno-common -dynamic -isysroot /Developer/SDKs/MacOSX10.6.sdk -arch i386 -arch x86_64 -g -O2 -DNDEBUG -g -O3 -I/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c hello.c -o build/temp.macosx-10.6-intel-2.7/hello.o
clang -bundle -undefined dynamic_lookup -arch i386 -arch x86_64 -isysroot /Developer/SDKs/MacOSX10.6.sdk -isysroot /Developer/SDKs/MacOSX10.6.sdk -g build/temp.macosx-10.6-intel-2.7/hello.o -o /Users/marca/dev/git-repos/cython-test/hello.so
python -c 'import hello; hello.byte_bugginess(1)'
1 - val = 1
4 - val = 1
5 - val = 0
6 - val = 2

How the heck is val == 2?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment