[pypy-commit] pypy default: test, fix failure in ndarray creation from list of unicodes

mattip noreply at buildbot.pypy.org
Mon Nov 2 07:48:48 EST 2015


Author: mattip
Branch: 
Changeset: r80500:058bbfa7fe93
Date: 2015-11-02 14:39 +0200
http://bitbucket.org/pypy/pypy/changeset/058bbfa7fe93/

Log:	test, fix failure in ndarray creation from list of unicodes

diff --git a/pypy/module/micronumpy/ndarray.py b/pypy/module/micronumpy/ndarray.py
--- a/pypy/module/micronumpy/ndarray.py
+++ b/pypy/module/micronumpy/ndarray.py
@@ -669,14 +669,17 @@
             raise oefmt(space.w_NotImplementedError,
                         "astype(%s) not implemented yet",
                         new_dtype.get_name())
-        if new_dtype.is_str() and new_dtype.elsize == 0:
+        if new_dtype.is_str_or_unicode() and new_dtype.elsize == 0:
             elsize = 0
+            ch = new_dtype.char
             itype = cur_dtype.itemtype
             for i in range(self.get_size()):
-                elsize = max(elsize, len(itype.str_format(self.implementation.getitem(i), add_quotes=False)))
+                elsize = max(elsize, space.len_w(itype.to_builtin_type(space, self.implementation.getitem(i))))
             new_dtype = descriptor.variable_dtype(
-                    space, 'S' + str(elsize))
-
+                    space, ch + str(elsize))
+        if new_dtype.elsize == 0:
+            # XXX Should not happen
+            raise oefmt(space.w_ValueError, "new dtype has elsize of 0")
         if not can_cast_array(space, self, new_dtype, casting):
             raise oefmt(space.w_TypeError, "Cannot cast array from %s to %s"
                         "according to the rule %s",
diff --git a/pypy/module/micronumpy/test/test_object_arrays.py b/pypy/module/micronumpy/test/test_object_arrays.py
--- a/pypy/module/micronumpy/test/test_object_arrays.py
+++ b/pypy/module/micronumpy/test/test_object_arrays.py
@@ -174,6 +174,11 @@
         b = a.astype('S')
         assert b.dtype == 'S100'
         assert 'a' * 100 in str(b)
+        a = np.array([u'a' * 100], dtype='O')
+        assert 'a' * 100 in str(a)
+        b = a.astype('U')
+        assert b.dtype == 'U100'
+
         a = np.array([123], dtype='U')
         assert a[0] == u'123'
         b = a.astype('O')


More information about the pypy-commit mailing list