[pypy-commit] pypy default: test/improve build_scalar function

bdkearns noreply at buildbot.pypy.org
Tue Feb 25 03:59:28 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: 
Changeset: r69384:b8e50f96bf3c
Date: 2014-02-24 18:44 -0500
http://bitbucket.org/pypy/pypy/changeset/b8e50f96bf3c/

Log:	test/improve build_scalar function

diff --git a/pypy/module/micronumpy/interp_numarray.py b/pypy/module/micronumpy/interp_numarray.py
--- a/pypy/module/micronumpy/interp_numarray.py
+++ b/pypy/module/micronumpy/interp_numarray.py
@@ -1519,19 +1519,25 @@
     return W_NDimArray.from_shape(space, w_a.get_shape(), dtype=dtype,
                                   w_instance=w_a if subok else None)
 
-def _reconstruct(space, w_subtype, w_shape, w_dtype):
-    return descr_new_array(space, w_subtype, w_shape, w_dtype)
-
 def build_scalar(space, w_dtype, w_state):
     from rpython.rtyper.lltypesystem import rffi, lltype
-
-    assert isinstance(w_dtype, interp_dtype.W_Dtype)
-
+    if not isinstance(w_dtype, interp_dtype.W_Dtype):
+        raise oefmt(space.w_TypeError,
+                    "argument 1 must be numpy.dtype, not %T", w_dtype)
+    if w_dtype.elsize == 0:
+        raise oefmt(space.w_ValueError, "itemsize cannot be zero")
+    if not space.isinstance_w(w_state, space.w_str):
+        raise oefmt(space.w_TypeError, "initializing object must be a string")
+    if space.len_w(w_state) != w_dtype.elsize:
+        raise oefmt(space.w_ValueError, "initialization string is too small")
     state = rffi.str2charp(space.str_w(w_state))
     box = w_dtype.itemtype.box_raw_data(state)
     lltype.free(state, flavor="raw")
     return box
 
+def _reconstruct(space, w_subtype, w_shape, w_dtype):
+    return descr_new_array(space, w_subtype, w_shape, w_dtype)
+
 
 W_FlatIterator.typedef = TypeDef("flatiter",
     __module__ = "numpy",
diff --git a/pypy/module/micronumpy/test/test_numarray.py b/pypy/module/micronumpy/test/test_numarray.py
--- a/pypy/module/micronumpy/test/test_numarray.py
+++ b/pypy/module/micronumpy/test/test_numarray.py
@@ -773,6 +773,23 @@
         a[()] = 4
         assert a == 4
 
+    def test_build_scalar(self):
+        from numpy import dtype
+        try:
+            from numpy.core.multiarray import scalar
+        except ImportError:
+            from numpy import scalar
+        exc = raises(TypeError, scalar, int, 2)
+        assert exc.value[0] == 'argument 1 must be numpy.dtype, not type'
+        exc = raises(ValueError, scalar, dtype('void'), 'abc')
+        assert exc.value[0] == 'itemsize cannot be zero'
+        exc = raises(TypeError, scalar, dtype(float), 2.5)
+        assert exc.value[0] == 'initializing object must be a string'
+        exc = raises(ValueError, scalar, dtype(float), 'abc')
+        assert exc.value[0] == 'initialization string is too small'
+        a = scalar(dtype('<f8'), dtype('<f8').type(2.5).tostring())
+        assert a == 2.5
+
     def test_len(self):
         from numpypy import array
         a = array(range(5))


More information about the pypy-commit mailing list