Discussion:
questions about future package here?
Ed Schofield
2014-04-13 01:22:37 UTC
Permalink
Hi everyone,
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.

``newint`` is simply a subclass of ``long`` on Python 2 that behaves more like Python 3’s ``int``. It’s pure-Python, so it won’t 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 doesn’t 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 doesn’t 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 don’t 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

Loading...