Hi,
I've debugged my problem with passing NumPy scalars via Cython into C++
down to the fact that inside Cython code `isinstance(x, float)` returns
True on floating point NumPy scalars, but `isinstance(x, (int, long))`
returns False on integer NumPy scalars.
However, when I run the same code from within Python, both checks work
just fine. So is this a genuine bug in Cython, or, rather, NumPy is
doing some black magic behind the scenes, that prevents Cython-compiled
modules from working correctly?
Here is a minimal reproducer for your convenience (tested with Cython
0.19.1, Python 2.7.1 and NumPy 1.7.1):
# bt.pyx
def foo(x):
if isinstance(x, (int, long)):
print("I'm an integer number")
elif isinstance(x, float):
print("I'm a floating point number")
else:
print("I don't know who I am")
# IPython transcript
In [1]: import numpy as np
In [2]: import bt
In [3]: bt.foo(np.array( (1., 2., 3.), dtype=np.float )[2])
I'm a floating point number
In [4]: bt.foo(np.array( (1., 2., 3.), dtype=np.int )[2])
I don't know who I am
In [5]: %paste
def bar(x):
if isinstance(x, (int, long)):
print("I'm an integer number")
elif isinstance(x, float):
print("I'm a floating point number")
else:
print("I don't know who I am")
## -- End pasted text --
In [6]: bar(np.array( (1., 2., 3.), dtype=np.float )[2])
I'm a floating point number
In [7]: bar(np.array( (1., 2., 3.), dtype=np.int )[2])
I'm an integer number
Thanks in advance for pointing me in the right direction!
--
Sincerely yours,
Yury V. Zaytsev
_______________________________________________
cython-devel mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cython-devel