Ed Schofield
2014-04-13 01:22:37 UTC
Hi everyone,
``newint`` is simply a subclass of ``long`` on Python 2 that behaves more like Python 3s ``int``. Its pure-Python, so it wont be causing any segfaults by itself. It is either NumPy or (perhaps more likely) some custom C code that is segfaulting when it gets a NumPy array of dtype object that it doesnt handle.
It seems that the astype method of ndarray understands ``long`` objects as a dtype on Py2 and interprets them as np.int64 objects, but it doesnt handle subclasses of ``long``. Here is a minimal example that reproduces the problem:
```
class longsubclass(long):
pass
def test_numpy_cast_as_long():
import numpy as np
a = np.arange(100, dtype=np.float64)
b = a.astype(long)
c = a.astype(longsubclass)
assert b.dtype == c.dtype
test_numpy_cast_as_long() # fails
```
Ideally NumPy would be tweaked so that it interprets subclasses of the Python data types similarly to the base types.
As a simple workaround for your code for now, you could either use <some numpy container>.astype(np.int64) or just remove the ``future.builtins.int`` import if you dont need it. See here <http://python-future.org/what_else.html#int> for more info on what it does.
Best wishes,
Ed
--
Dr. Edward Schofield
Python Charmers
+61 (0)405 676 229
http://pythoncharmers.com
I'm just experimenting with future package.
I tried converting a small py2 module
futurize mod.py -w
from future.builtins import int
<some numpy container>.astype(int)
instead of converting to int, it is converted to object.
So what is this newint anyway?
Thanks for your question.I tried converting a small py2 module
futurize mod.py -w
from future.builtins import int
<some numpy container>.astype(int)
instead of converting to int, it is converted to object.
So what is this newint anyway?
``newint`` is simply a subclass of ``long`` on Python 2 that behaves more like Python 3s ``int``. Its pure-Python, so it wont be causing any segfaults by itself. It is either NumPy or (perhaps more likely) some custom C code that is segfaulting when it gets a NumPy array of dtype object that it doesnt handle.
It seems that the astype method of ndarray understands ``long`` objects as a dtype on Py2 and interprets them as np.int64 objects, but it doesnt handle subclasses of ``long``. Here is a minimal example that reproduces the problem:
```
class longsubclass(long):
pass
def test_numpy_cast_as_long():
import numpy as np
a = np.arange(100, dtype=np.float64)
b = a.astype(long)
c = a.astype(longsubclass)
assert b.dtype == c.dtype
test_numpy_cast_as_long() # fails
```
Ideally NumPy would be tweaked so that it interprets subclasses of the Python data types similarly to the base types.
As a simple workaround for your code for now, you could either use <some numpy container>.astype(np.int64) or just remove the ``future.builtins.int`` import if you dont need it. See here <http://python-future.org/what_else.html#int> for more info on what it does.
Best wishes,
Ed
--
Dr. Edward Schofield
Python Charmers
+61 (0)405 676 229
http://pythoncharmers.com