Author: Matti Picus <[email protected]>
Branch:
Changeset: r61006:fa8c91ae3a18
Date: 2013-02-09 22:23 +0200
http://bitbucket.org/pypy/pypy/changeset/fa8c91ae3a18/
Log: allow abstract dtypes in array construction, issue # 1160
diff --git a/pypy/module/micronumpy/interp_dtype.py
b/pypy/module/micronumpy/interp_dtype.py
--- a/pypy/module/micronumpy/interp_dtype.py
+++ b/pypy/module/micronumpy/interp_dtype.py
@@ -270,13 +270,12 @@
return dtype_from_list(space, w_dtype)
elif space.isinstance_w(w_dtype, space.w_dict):
return dtype_from_dict(space, w_dtype)
- else:
- for dtype in cache.builtin_dtypes:
- if w_dtype in dtype.alternate_constructors:
- return dtype
- if w_dtype is dtype.w_box_type:
- return dtype
- raise OperationError(space.w_TypeError, space.wrap("data type %s not
understood"))
+ for dtype in cache.builtin_dtypes:
+ if w_dtype in dtype.alternate_constructors:
+ return dtype
+ if w_dtype is dtype.w_box_type:
+ return dtype
+ raise OperationError(space.w_TypeError, space.wrap("data type %r not
understood" % w_dtype))
W_Dtype.typedef = TypeDef("dtype",
__module__ = "numpypy",
@@ -400,7 +399,10 @@
name=name,
char="l",
w_box_type=space.gettypefor(interp_boxes.W_LongBox),
- alternate_constructors=[space.w_int],
+ alternate_constructors=[space.w_int,
+
space.gettypefor(interp_boxes.W_IntegerBox),
+
space.gettypefor(interp_boxes.W_SignedIntegerBox),
+ ],
aliases=['int'],
)
self.w_ulongdtype = W_Dtype(
@@ -410,6 +412,8 @@
name="u" + name,
char="L",
w_box_type=space.gettypefor(interp_boxes.W_ULongBox),
+ alternate_constructors=[
space.gettypefor(interp_boxes.W_UnsignedIntegerBox),
+ ],
)
self.w_int64dtype = W_Dtype(
types.Int64(),
@@ -443,7 +447,9 @@
name="float64",
char="d",
w_box_type = space.gettypefor(interp_boxes.W_Float64Box),
- alternate_constructors=[space.w_float],
+ alternate_constructors=[space.w_float,
+ space.gettypefor(interp_boxes.W_NumberBox),
+ ],
aliases=["float"],
)
self.w_complex64dtype = W_ComplexDtype(
@@ -546,6 +552,8 @@
w_box_type = space.gettypefor(interp_boxes.W_VoidBox),
#alternate_constructors=[space.w_buffer],
# XXX no buffer in space
+
#alternate_constructors=[space.gettypefor(interp_boxes.W_GenericBox)],
+ # XXX fix, leads to _coerce error
)
self.w_float16dtype = W_Dtype(
types.Float16(),
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
@@ -266,6 +266,18 @@
assert 'unsignedinteger' in str(exc.value)
raises(TypeError, numpy.floating, 0)
raises(TypeError, numpy.inexact, 0)
+ # numpy allows abstract types in array creation
+ a = numpy.array([4,4], numpy.integer)
+ assert a.dtype is numpy.dtype('int64')
+ a = numpy.array([4,4], numpy.number)
+ assert a.dtype is numpy.dtype('float')
+ a = numpy.array([4,4], numpy.signedinteger)
+ assert a.dtype is numpy.dtype('int64')
+ a = numpy.array([4,4], numpy.unsignedinteger)
+ assert a.dtype is numpy.dtype('uint64')
+ # too ambitious for now
+ #a = numpy.array('xxxx', numpy.generic)
+ #assert a.dtype is numpy.dtype('|V4')
def test_new(self):
import _numpypy as np
@@ -472,7 +484,7 @@
assert numpy.float64(2.0) == 2.0
assert numpy.float64('23.4') == numpy.float64(23.4)
raises(ValueError, numpy.float64, '23.2df')
- assert "{:g}".format(numpy.complex(0.5+1.5j)) ==
'{:g}'.format(0.5+1.5j)
+ assert "{:g}".format(numpy.complex_(0.5+1.5j)) ==
'{:g}'.format(0.5+1.5j)
def test_longfloat(self):
import _numpypy as 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
@@ -2470,7 +2470,7 @@
class AppTestRecordDtype(BaseNumpyAppTest):
def test_zeros(self):
- from _numpypy import zeros
+ from _numpypy import zeros, integer
a = zeros(2, dtype=[('x', int), ('y', float)])
raises(IndexError, 'a[0]["xyz"]')
assert a[0]['x'] == 0
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit