[pypy-commit] pypy default: fix crashes by disallowing astype(numeric_type) for str arrays, allow astype(str) for numeric arrays since it was implemented previously
mattip
noreply at buildbot.pypy.org
Thu Apr 4 22:03:53 CEST 2013
Author: Matti Picus <matti.picus at gmail.com>
Branch:
Changeset: r63016:c4b86631bce3
Date: 2013-04-04 19:33 +0300
http://bitbucket.org/pypy/pypy/changeset/c4b86631bce3/
Log: fix crashes by disallowing astype(numeric_type) for str arrays,
allow astype(str) for numeric arrays since it was implemented
previously
diff --git a/pypy/module/micronumpy/arrayimpl/concrete.py b/pypy/module/micronumpy/arrayimpl/concrete.py
--- a/pypy/module/micronumpy/arrayimpl/concrete.py
+++ b/pypy/module/micronumpy/arrayimpl/concrete.py
@@ -281,7 +281,7 @@
def astype(self, space, dtype):
new_arr = W_NDimArray.from_shape(self.get_shape(), dtype)
- if dtype.is_str_or_unicode():
+ if self.dtype.is_str_or_unicode() and not dtype.is_str_or_unicode():
raise OperationError(space.w_NotImplementedError, space.wrap(
"astype(%s) not implemented yet" % self.dtype))
else:
diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/interp_support.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/interp_support.py
@@ -15,7 +15,7 @@
items = []
num_items = 0
idx = 0
-
+
while (num_items < count or count == -1) and idx < len(s):
nextidx = s.find(sep, idx)
if nextidx < 0:
@@ -45,7 +45,7 @@
items.append(val)
num_items += 1
idx = nextidx + 1
-
+
if count > num_items:
raise OperationError(space.w_ValueError, space.wrap(
"string is smaller than requested size"))
@@ -70,7 +70,7 @@
if count * itemsize > length:
raise OperationError(space.w_ValueError, space.wrap(
"string is smaller than requested size"))
-
+
a = W_NDimArray.from_shape([count], dtype=dtype)
loop.fromstring_loop(a, dtype, itemsize, s)
return space.wrap(a)
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
@@ -1677,15 +1677,15 @@
a = array('x').astype('S3').dtype
assert a.itemsize == 3
# scalar vs. array
+ a = array([1, 2, 3.14156]).astype('S3').dtype
+ assert a.itemsize == 3
+ a = array(3.1415).astype('S3').dtype
+ assert a.itemsize == 3
try:
a = array(['1', '2','3']).astype(float)
assert a[2] == 3.0
- a = array([1, 2, 3.14156]).astype('S3').dtype
- assert a.itemsize == 3
- a = array(3.1415).astype('S3').dtype
- assert a.itemsize == 3
except NotImplementedError:
- skip('astype("S3") not implemented for numeric arrays')
+ skip('astype("float") not implemented for str arrays')
def test_base(self):
from numpypy import array
More information about the pypy-commit
mailing list