[pypy-commit] pypy numpy-dtype-strings: Updated int and float types to take strings in their constructors so things like int32('34') work

jterrace noreply at buildbot.pypy.org
Mon Dec 12 22:52:53 CET 2011


Author: Jeff Terrace <jterrace at gmail.com>
Branch: numpy-dtype-strings
Changeset: r50448:aaa9b6a48bbb
Date: 2011-12-11 09:08 -0500
http://bitbucket.org/pypy/pypy/changeset/aaa9b6a48bbb/

Log:	Updated int and float types to take strings in their constructors so
	things like int32('34') work

diff --git a/pypy/module/micronumpy/test/test_dtypes.py b/pypy/module/micronumpy/test/test_dtypes.py
--- a/pypy/module/micronumpy/test/test_dtypes.py
+++ b/pypy/module/micronumpy/test/test_dtypes.py
@@ -193,6 +193,7 @@
 
         assert type(X(True)) is numpy.bool_
         assert X(True) is numpy.True_
+        assert numpy.bool_("False") is numpy.True_
 
     def test_int8(self):
         import numpypy as numpy
@@ -211,6 +212,10 @@
 
         assert type(int(x)) is int
         assert int(x) == -128
+        assert numpy.int8('50') == numpy.int8(50)
+        raises(ValueError, numpy.int8, '50.2')
+        assert numpy.int8('127') == 127
+        assert numpy.int8('128') == -128
 
     def test_uint8(self):
         import numpypy as numpy
@@ -232,6 +237,8 @@
 
         assert numpy.uint8(255) == 255
         assert numpy.uint8(256) == 0
+        assert numpy.uint8('255') == 255
+        assert numpy.uint8('256') == 0
 
     def test_int16(self):
         import numpypy as numpy
@@ -240,12 +247,16 @@
         assert x == 3
         assert numpy.int16(32767) == 32767
         assert numpy.int16(32768) == -32768
+        assert numpy.int16('32767') == 32767
+        assert numpy.int16('32768') == -32768
 
     def test_uint16(self):
         import numpypy as numpy
 
         assert numpy.uint16(65535) == 65535
         assert numpy.uint16(65536) == 0
+        assert numpy.uint16('65535') == 65535
+        assert numpy.uint16('65536') == 0
 
     def test_int32(self):
         import numpypy as numpy
@@ -254,12 +265,16 @@
         assert x == 23
         assert numpy.int32(2147483647) == 2147483647
         assert numpy.int32(2147483648) == -2147483648
+        assert numpy.int32('2147483647') == 2147483647
+        assert numpy.int32('2147483648') == -2147483648
 
     def test_uint32(self):
         import numpypy as numpy
 
         assert numpy.uint32(4294967295) == 4294967295
         assert numpy.uint32(4294967296) == 0
+        assert numpy.uint32('4294967295') == 4294967295
+        assert numpy.uint32('4294967296') == 0
 
     def test_int_(self):
         import numpypy as numpy
@@ -281,6 +296,9 @@
 
         assert numpy.int64(9223372036854775807) == 9223372036854775807
         raises(OverflowError, numpy.int64, 9223372036854775808)
+        
+        assert numpy.int64('9223372036854775807') == 9223372036854775807
+        raises(OverflowError, numpy.int64, '9223372036854775808')
 
     def test_uint64(self):
         import sys
@@ -304,6 +322,8 @@
         assert numpy.float32.mro() == [numpy.float32, numpy.floating, numpy.inexact, numpy.number, numpy.generic, object]
 
         assert numpy.float32(12) == numpy.float64(12)
+        assert numpy.float32('23.4') == numpy.float32(23.4)
+        raises(ValueError, numpy.float32, '23.2df')
 
     def test_float64(self):
         import numpypy as numpy
@@ -315,6 +335,8 @@
         assert numpy.dtype(float).type is numpy.float64
 
         assert numpy.float64(2.0) == 2.0
+        assert numpy.float64('23.4') == numpy.float64(23.4)
+        raises(ValueError, numpy.float64, '23.2df')
 
     def test_subclass_type(self):
         import numpypy as numpy
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -191,7 +191,16 @@
     _mixin_ = True
 
     def _coerce(self, space, w_item):
-        return self.box(space.int_w(space.int(w_item)))
+        if space.isinstance_w(w_item, space.w_str):
+            try:
+                val = int(space.str_w(space.str(w_item)))
+            except ValueError:
+                raise OperationError(space.w_ValueError, space.wrap("Invalid integer value"))
+            if not isinstance(val, int):
+                raise OperationError(space.w_OverflowError, space.wrap("Value out of range"))
+        else:
+            val = space.int_w(space.int(w_item))
+        return self.box(val)
 
     def str_format(self, box):
         value = self.unbox(box)
@@ -289,7 +298,14 @@
     _mixin_ = True
 
     def _coerce(self, space, w_item):
-        return self.box(space.float_w(space.float(w_item)))
+        if space.isinstance_w(w_item, space.w_str):
+            try:
+                val = float(space.str_w(space.str(w_item)))
+            except ValueError:
+                raise OperationError(space.w_ValueError, space.wrap("Invalid float value"))
+        else:
+            val = space.float_w(space.float(w_item))
+        return self.box(val)
 
     def str_format(self, box):
         value = self.unbox(box)


More information about the pypy-commit mailing list