Author: Justin Peel <[email protected]>
Branch: numpy-dtype
Changeset: r46227:cc5e673fa4a4
Date: 2011-08-02 23:49 -0600
http://bitbucket.org/pypy/pypy/changeset/cc5e673fa4a4/
Log: add ability to get dtypes with types (e.g. dtype(bool) or
array([1],bool))
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
@@ -2,6 +2,10 @@
from pypy.interpreter.error import OperationError
from pypy.interpreter.gateway import interp2app
from pypy.interpreter.typedef import TypeDef, GetSetProperty
+from pypy.objspace.std.boolobject import W_BoolObject
+from pypy.objspace.std.floatobject import W_FloatObject
+from pypy.objspace.std.intobject import W_IntObject
+from pypy.objspace.std.longobject import W_LongObject
from pypy.rlib.rarithmetic import r_int, r_uint, LONG_BIT, LONGLONG_BIT
from pypy.rpython.lltypesystem import lltype, rffi
@@ -170,10 +174,15 @@
raise OperationError(space.w_ValueError,
space.wrap("type not recognized"))
elif space.is_true(space.isinstance(w_string_or_type, space.w_type)):
- # XXX: need to implement this
- return Float64_dtype
- else:
- raise OperationError(space.w_TypeError,
+ if space.is_w(w_string_or_type,
space.gettypeobject(W_IntObject.typedef)):
+ return Long_dtype
+ if space.is_w(w_string_or_type,
space.gettypeobject(W_LongObject.typedef)):
+ return Int64_dtype
+ if space.is_w(w_string_or_type,
space.gettypeobject(W_FloatObject.typedef)):
+ return Float64_dtype
+ if space.is_w(w_string_or_type,
space.gettypeobject(W_BoolObject.typedef)):
+ return Bool_dtype
+ raise OperationError(space.w_TypeError,
space.wrap("data type not understood"))
def find_result_dtype(d1, d2):
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
@@ -8,6 +8,13 @@
d = dtype('l')
assert d.num == 7
assert d.kind == 'i'
+
+ def test_dtype_with_types(self):
+ from numpy import dtype
+ assert dtype(bool).num == 0
+ assert dtype(int).num == 7
+ assert dtype(long).num == 9
+ assert dtype(float).num == 12
def test_bool_array(self):
from numpy import array
@@ -41,7 +48,7 @@
types = ('?','b','B','h','H','i','I','l','L','q','Q','f','d','g')
dtypes = [dtype(t) for t in types]
N = len(types)
- a = array([True],'?')
+ a = array([True], '?')
for i in xrange(N):
assert (a + array([0], types[i])).dtype is dtypes[i]
# need more tests for binop result types
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit