[Cython] Bug: Returning real value crashes the code, complex value does not
Dear Cython developers, I stumbled upon a strange error when using Cython. I made a minimal working example, see attachment for the two necessary files. (btw I didn't find the e-mail address of Robert Bradshaw so I could not request him for an account on the issue tracker. Is it possible to put the bug on there?) To reproduce the bug: 1) Reboot to Windows :) (the bug only appears on Windows) 2) Run compile_bug.py to generate the Cython extension 3) Try to run the my_func_exposed function: python >>> import complex_double (does not crash) >>> complex_double.my_func_exposed(1,1j) (crashes) >>> complex_double.my_func_exposed(1,1) If I put a breakpoint in the code with gdb, jump in the code, and leave the function again, it does not crash! Also, it is no problem on Linux. It has to do with the fact that in the first case, a real value was used. In the complex-value case, it does not crash. I went through the generated cpp file and I don't see any issues there (the reason I use cpp is because it's in a big project that needs cpp enabled; it is further linked and so on). gcc version used: 4.6.2 (mingw) cython version used: 0.18 (I did pip install Cython) python version used: python 2.7.3 (MSC v.1500 32 bit). Looking forward to hearing from you! With kind regards, Martin -- ------- ir. Martin Fiers Photonics Research Group Universiteit Gent - Ghent University Sint-Pietersnieuwstraat 41 9000 Gent - Belgium T + 32 9 264 34 48 E martin.fi...@intec.ugent.be W www.caphesim.com --- from distutils.core import setup from Cython.Build import cythonize from distutils.extension import Extension from Cython.Distutils import build_ext import os path = '.' lib_name = 'complex_double' full_file ='complex_double.pyx' additional_include_directories = [] script_args = ["build_ext", "--build-lib", path] def isWindows(): return os.name=='nt' if(isWindows()): script_args.append('--compiler=mingw32') ext_module = Extension(lib_name, sources=[full_file], language="c++", #extra_compile_args = ["-O0"] # Doesn't work? It just appends it, but we wish to replace it. During production, we want -O2/-O3 and -ffast-math to be enabled extra_compile_args = ["-ffast-math", "-g"] ) # FIXME: Find the proper way to pass these arguments. ext_module.cython_gdb = True #ext_module.cython_create_listing = True setup( name = "Cythonized module", ext_modules = [ext_module], cmdclass={'build_ext': build_ext}, script_args = script_args, include_dirs = additional_include_directories, ) import cython @cython.cdivision(True) cdef public double complex my_func(int a, b): if(a==0): return 1; else: return b; def my_func_exposed(int a, b): return my_func(a,b)___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
Re: [Cython] Bug: Returning real value crashes the code, complex value does not
On 3/26/2013 6:48 PM, Robert Bradshaw wrote: On Tue, Mar 26, 2013 at 2:52 AM, Martin Fiers wrote: Dear Cython developers, I stumbled upon a strange error when using Cython. I made a minimal working example, see attachment for the two necessary files. (btw I didn't find the e-mail address of Robert Bradshaw so I could not request him for an account on the issue tracker. Is it possible to put the bug on there?) Sure. You should have my email now. Thank you! I just sent a mail. Also, thanks for replying so quickly. Replies follow inline. To reproduce the bug: 1) Reboot to Windows :) (the bug only appears on Windows) 2) Run compile_bug.py to generate the Cython extension 3) Try to run the my_func_exposed function: python import complex_double (does not crash) complex_double.my_func_exposed(1,1j) (crashes) complex_double.my_func_exposed(1,1) If I put a breakpoint in the code with gdb, jump in the code, and leave the function again, it does not crash! Also, it is no problem on Linux. It has to do with the fact that in the first case, a real value was used. In the complex-value case, it does not crash. I went through the generated cpp file and I don't see any issues there (the reason I use cpp is because it's in a big project that needs cpp enabled; it is further linked and so on). gcc version used: 4.6.2 (mingw) cython version used: 0.18 (I did pip install Cython) python version used: python 2.7.3 (MSC v.1500 32 bit). Very strange. Does calling PyComplex_AsCComplex directly produce the same crash? What about I'm not sure how to call this directly. Do you mean by modifying the generated cpp file and then manually building an extension module? cdef complex double x = 1.0 This one works. or cdef object py_x = 1.0 cdef complex double x = py_x This one crashes! Regards, Martin ? - Robert ___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel -- ------- ir. Martin Fiers Photonics Research Group Universiteit Gent - Ghent University Sint-Pietersnieuwstraat 41 9000 Gent - Belgium T + 32 9 264 34 48 E martin.fi...@intec.ugent.be W www.caphesim.com --- ___ cython-devel mailing list cython-devel@python.org http://mail.python.org/mailman/listinfo/cython-devel
Re: [Cython] Bug: Returning real value crashes the code, complex value does not
On 03/27/2013 10:36 PM, Robert Bradshaw wrote: On Wed, Mar 27, 2013 at 2:44 AM, Martin Fiers wrote: On 3/27/2013 3:54 AM, Robert Bradshaw wrote: On Tue, Mar 26, 2013 at 5:12 PM, Martin Fiers wrote: On 3/26/2013 6:48 PM, Robert Bradshaw wrote: On Tue, Mar 26, 2013 at 2:52 AM, Martin Fiers wrote: Dear Cython developers, I stumbled upon a strange error when using Cython. I made a minimal working example, see attachment for the two necessary files. (btw I didn't find the e-mail address of Robert Bradshaw so I could not request him for an account on the issue tracker. Is it possible to put the bug on there?) Sure. You should have my email now. Thank you! I just sent a mail. Also, thanks for replying so quickly. Replies follow inline. To reproduce the bug: 1) Reboot to Windows :) (the bug only appears on Windows) 2) Run compile_bug.py to generate the Cython extension 3) Try to run the my_func_exposed function: python import complex_double (does not crash) complex_double.my_func_exposed(1,1j) (crashes) complex_double.my_func_exposed(1,1) If I put a breakpoint in the code with gdb, jump in the code, and leave the function again, it does not crash! Also, it is no problem on Linux. It has to do with the fact that in the first case, a real value was used. In the complex-value case, it does not crash. I went through the generated cpp file and I don't see any issues there (the reason I use cpp is because it's in a big project that needs cpp enabled; it is further linked and so on). gcc version used: 4.6.2 (mingw) cython version used: 0.18 (I did pip install Cython) python version used: python 2.7.3 (MSC v.1500 32 bit). Very strange. Does calling PyComplex_AsCComplex directly produce the same crash? What about I'm not sure how to call this directly. Do you mean by modifying the generated cpp file and then manually building an extension module? cdef complex double x = 1.0 This one works. or cdef object py_x = 1.0 cdef complex double x = py_x This one crashes! Ah. Try from cpython.complex cimport Py_complex, PyComplex_AsCComplex cdef Py_complex x = PyComplex_AsCComplex(py_x) print x.real, x.imag Ok. I tried this, and it also crashes. Here's the modification: from cpython.complex cimport Py_complex from cpython.complex cimport PyComplex_AsCComplex @cython.cdivision(True) cdef public double complex my_func(int a, b): cdef object py_x = 1.0 #cdef double complex x = 1.0# Does not crash #cdef double complex x2 = py_x # Crashes for py_x = 1, not for py_x=1j. #cdef Py_complex x = PyComplex_AsCComplex(py_x) # Crashes, even for py_x=1j #print x.real, x.imag And as you can see, the PyComplex_AsCComplex also crashes (SIGSEGV). I tried to compile with debug information, as in the instructions in http://docs.cython.org/src/userguide/debugging.html But I cannot get the line numbers. Probably I need a debug-python version, but that seems to be very nontrivial on Windows. Not sure if I can think of other options to test it and/or track down the bug... Now it even crashes when py_x = 1j. So maybe there's something else going wrong here too. I wonder if it's a compiler miss-match or something like that. In this case, this causes a significant problem for us. All other code works as expected, and I'm compiling a very large GCC project which uses Cython for certain functionality. Switching to the MSC tools would be quite a pain. I attached the latest version that isolates the bug. Can you give me directions on how to put this on the tracker as an issue: - type: defect - milestone: ? - keywords: complex, cpp - priority: for me it is critical (there's a workaround -although it requires the user to manually add some code) - component: C++ ? Is there anything else I could try to resolve/debug this issue? Are there a debug build binaries of Python available? Apparently, it's not that easy to find. Thanks for the help so far! Martin Fiers Regards, Martin P.S. I only replied to you because you didn't put the cython-devel@python.org in the previous mail. Oops. Un-intentional oversight. from distutils.core import setup from Cython.Build import cythonize from distutils.extension import Extension from Cython.Distutils import build_ext import os path = '.' lib_name = 'complex_double' full_file ='complex_double.pyx' additional_include_directories = [] script_args = ["build_ext", "--build-lib", path] def isWindows(): return os.name=='nt' if(isWindows()): script_args.append('--compiler=mingw32') ext_module = Extension(lib_name, sources=[full_file], language="c++", #extra_compile_args = ["-O0"] # Doesn't work? It just appends it, but we wish to replace it. During production, we want -