Author: Brian Kearns <[email protected]>
Branch: 
Changeset: r70302:297f0a5cd941
Date: 2014-03-26 19:31 -0400
http://bitbucket.org/pypy/pypy/changeset/297f0a5cd941/

Log:    fix descr_int/long/float on numpy scalars (issue1590)

diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -161,22 +161,25 @@
         return space.index(self.item(space))
 
     def descr_int(self, space):
-        if isinstance(self, W_UnsignedIntegerBox):
-            box = self.convert_to(space, W_UInt64Box._get_dtype(space))
+        if isinstance(self, W_ComplexFloatingBox):
+            box = self.descr_get_real(space)
         else:
-            box = self.convert_to(space, W_Int64Box._get_dtype(space))
-        return space.int(box.item(space))
+            box = self
+        return space.call_function(space.w_int, box.item(space))
 
     def descr_long(self, space):
-        if isinstance(self, W_UnsignedIntegerBox):
-            box = self.convert_to(space, W_UInt64Box._get_dtype(space))
+        if isinstance(self, W_ComplexFloatingBox):
+            box = self.descr_get_real(space)
         else:
-            box = self.convert_to(space, W_Int64Box._get_dtype(space))
-        return space.long(box.item(space))
+            box = self
+        return space.call_function(space.w_long, box.item(space))
 
     def descr_float(self, space):
-        box = self.convert_to(space, W_Float64Box._get_dtype(space))
-        return space.float(box.item(space))
+        if isinstance(self, W_ComplexFloatingBox):
+            box = self.descr_get_real(space)
+        else:
+            box = self
+        return space.call_function(space.w_float, box.item(space))
 
     def descr_oct(self, space):
         return space.oct(self.descr_int(space))
@@ -185,8 +188,7 @@
         return space.hex(self.descr_int(space))
 
     def descr_nonzero(self, space):
-        dtype = self.get_dtype(space)
-        return space.wrap(dtype.itemtype.bool(self))
+        return space.wrap(self.get_dtype(space).itemtype.bool(self))
 
     def _unaryop_impl(ufunc_name):
         def impl(self, space, w_out=None):
diff --git a/pypy/module/micronumpy/test/test_scalar.py 
b/pypy/module/micronumpy/test/test_scalar.py
--- a/pypy/module/micronumpy/test/test_scalar.py
+++ b/pypy/module/micronumpy/test/test_scalar.py
@@ -36,6 +36,24 @@
         exc = raises(ValueError, "int(np.str_('abc'))")
         assert exc.value.message.startswith('invalid literal for int()')
         assert int(np.uint64((2<<63) - 1)) == (2<<63) - 1
+        exc = raises(ValueError, "int(np.float64(np.nan))")
+        assert str(exc.value) == "cannot convert float NaN to integer"
+        exc = raises(OverflowError, "int(np.float64(np.inf))")
+        assert str(exc.value) == "cannot convert float infinity to integer"
+        assert int(np.float64(1e100)) == int(1e100)
+        assert long(np.float64(1e100)) == int(1e100)
+        assert int(np.complex128(1e100+2j)) == int(1e100)
+        exc = raises(OverflowError, "int(np.complex64(1e100+2j))")
+        assert str(exc.value) == "cannot convert float infinity to integer"
+        assert int(np.str_('100000000000000000000')) == 100000000000000000000
+        assert long(np.str_('100000000000000000000')) == 100000000000000000000
+
+        assert float(np.float64(1e100)) == 1e100
+        assert float(np.complex128(1e100+2j)) == 1e100
+        assert float(np.str_('1e100')) == 1e100
+        assert float(np.str_('inf')) == np.inf
+        assert str(float(np.float64(np.nan))) == 'nan'
+
         assert oct(np.int32(11)) == '013'
         assert oct(np.float32(11.6)) == '013'
         assert oct(np.complex64(11-12j)) == '013'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to