[Python-porting] Fwd: questions about future package here?

Ed Schofield ed at pythoncharmers.com
Sun Apr 13 03:22:37 CEST 2014


Hi everyone,

It seems I forgot to CC the list. Here was my reply to Neal:


> I'm  just experimenting with future package.
> 
> 1st problem:
> I tried converting a small py2 module
> futurize mod.py -w
> 
> Then I get segfault. It seems to want to do:
> 
> from future.builtins import int
> 
> Then when my code says:
> 
> <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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20140413/f5b01329/attachment.html>


More information about the Python-porting mailing list