2

I have the following code in test.pyx

cdef class Test:
    cdef long long i

    def __cinit__(self, long long i):
        self.i = i

    def __truediv__(Test self, Test other):
        return Test(self.i / other.i)

In a short python script I have this:

import test
print('Done')

When I run the script after compiling test.pyx, I get the following output along with a Windows Error dialog "python.exe has stopped working".

Done

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

Note that I never call the compiled code, only import it. Also note that the crash only occurs after the python script completes execution. I've noticed that changing i from a long long to just a long causes the error to go away. Is there something basic I'm missing, or is division by a long long not always safe?

Some information about my setup if it makes a difference:

OS: Windows 7, 64-bit

Python version: 3.4.2, MSC v 1600 32 bit

Cython version: 0.22

Compiler: mingw32

EDIT: Additional Notes

  • Also tested on Python 2.7.9 32 bit with the same results
  • Replacing / with // gives the same result
  • If all code involving division is removed (commented out, removed by the compiler, etc.), the error disappears
  • The compiled code seems to be functioning properly when called even though Python crashes upon completion.
Mobious
  • 122
  • 8
  • Does anything different happen if you use `//`? – user2357112 May 20 '15 at 02:47
  • No, same result. However, I noticed that changing \_\_truediv__ to just \_\_div__ makes the error disappear. However, that may just be because \_\_div__ isn't used in Python 3 and just gets removed by the compiler. – Mobious May 20 '15 at 03:59
  • 1
    FYI, no crash on OS X, Cython 0.22, Python 2.7.9. Sometimes, on Windows, my experience is that you need to use the exact same compiler as Python uses to avoid ABI incompatibility (long long division might be one of those places where the MinGW ABI and the MSVC ABI differ). Try using the Microsoft compiler if available. – nneonneo May 20 '15 at 04:57
  • No crash on Win8.1 using MinGW and Cython 20.1. What happens if you run the Python command line and import this thing as a module? At the prompt can you print it, do a dir() on it, inspect its attributes and do all the minimal stuff you can with any module? – Paul Cornelius May 20 '15 at 05:06
  • @nneonneo Thanks for the info. I'll see what I can do about trying a different compiler. – Mobious May 20 '15 at 06:35
  • @PaulCornelius Same thing happens form the Python console. Imports fine, can inspect, call functions on it, call functions in it, etc. Only thing that goes wrong is Python crashes on exit. – Mobious May 20 '15 at 06:37
  • Is there any difference if you replace `long long` with `int64_t` (which you get by `from libc.stdint cimport int64_t`)? – DavidW May 20 '15 at 08:56
  • @DavidW Nope, same thing with int64_t. – Mobious May 20 '15 at 18:10
  • I would strongly consider reporting this to the Cython developers - it's very, very weird. – Paul Cornelius May 21 '15 at 00:37
  • A ```long long``` is 64 bits, a ```long``` is 32 bits on Windows (also on Win64). A ```Python int is a C long``` on Python 2.7, which means it causes some sort of binary f*ck up. That is, all arguments to ```__cinit__``` must be Python types, so you get some sort of stack error due to a 32 bit int being pushed and a 64 bit int being popped. This is obviously a Cython bug. Change ```__cinit__``` to take an object instead, and do the cast to long long inside the function. – Sturla Molden May 31 '15 at 03:18
  • @SturlaMolden This doesn't fix the problem. Keep in mind that the error occurs even if I never execute any of the code or create an instance of the Test class. All I have to do is import compiled code involving division by a long long. – Mobious Jun 01 '15 at 01:11

0 Answers0