Author: Romain Guillebert <[email protected]>
Branch: object-dtype
Changeset: r75536:dca1f6200f69
Date: 2015-01-26 15:19 +0100
http://bitbucket.org/pypy/pypy/changeset/dca1f6200f69/
Log: Object ndarrays are now initialized with Nones
diff --git a/pypy/module/micronumpy/base.py b/pypy/module/micronumpy/base.py
--- a/pypy/module/micronumpy/base.py
+++ b/pypy/module/micronumpy/base.py
@@ -34,11 +34,13 @@
@staticmethod
def from_shape(space, shape, dtype, order='C', w_instance=None, zero=True):
- from pypy.module.micronumpy import concrete
+ from pypy.module.micronumpy import concrete, descriptor, boxes
from pypy.module.micronumpy.strides import calc_strides
strides, backstrides = calc_strides(shape, dtype.base, order)
impl = concrete.ConcreteArray(shape, dtype.base, order, strides,
backstrides, zero=zero)
+ if dtype == descriptor.get_dtype_cache(space).w_objectdtype:
+ impl.fill(space, boxes.W_ObjectBox(space.w_None))
if w_instance:
return wrap_impl(space, space.type(w_instance), w_instance, impl)
return W_NDimArray(impl)
diff --git a/pypy/module/micronumpy/descriptor.py
b/pypy/module/micronumpy/descriptor.py
--- a/pypy/module/micronumpy/descriptor.py
+++ b/pypy/module/micronumpy/descriptor.py
@@ -586,8 +586,7 @@
if w_dtype is dtype.w_box_type:
return dtype
if space.isinstance_w(w_dtype, space.w_type):
- raise oefmt(space.w_NotImplementedError,
- "cannot create dtype with type '%N'", w_dtype)
+ return cache.w_objectdtype
raise oefmt(space.w_TypeError, "data type not understood")
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
@@ -1345,9 +1345,12 @@
import sys
class Polynomial(object):
pass
- if '__pypy__' in sys.builtin_module_names:
- exc = raises(NotImplementedError, array, Polynomial())
- assert exc.value.message.find('unable to create dtype from
objects') >= 0
- else:
- a = array(Polynomial())
- assert a.shape == ()
+ a = array(Polynomial())
+ assert a.shape == ()
+
+ def test_uninitialized_object_array_is_filled_by_None(self):
+ import numpy as np
+
+ a = np.ndarray([5], dtype="O")
+
+ assert a[0] == None
diff --git a/pypy/module/micronumpy/types.py b/pypy/module/micronumpy/types.py
--- a/pypy/module/micronumpy/types.py
+++ b/pypy/module/micronumpy/types.py
@@ -1624,7 +1624,7 @@
_all_objs_for_tests = [] # for tests
-class ObjectType(BaseType):
+class ObjectType(Primitive, BaseType):
T = lltype.Signed
BoxType = boxes.W_ObjectBox
@@ -1669,6 +1669,10 @@
def str_format(self, space, box):
return space.str_w(space.repr(self.unbox(box)))
+ @staticmethod
+ def for_computation(v):
+ return v
+
class FlexibleType(BaseType):
def get_element_size(self):
return rffi.sizeof(self.T)
diff --git a/pypy/module/micronumpy/ufuncs.py b/pypy/module/micronumpy/ufuncs.py
--- a/pypy/module/micronumpy/ufuncs.py
+++ b/pypy/module/micronumpy/ufuncs.py
@@ -608,6 +608,7 @@
uint64_dtype = descriptor.get_dtype_cache(space).w_uint64dtype
complex_dtype = descriptor.get_dtype_cache(space).w_complex128dtype
float_dtype = descriptor.get_dtype_cache(space).w_float64dtype
+ object_dtype = descriptor.get_dtype_cache(space).w_objectdtype
if isinstance(w_obj, boxes.W_GenericBox):
dtype = w_obj.get_dtype(space)
return find_binop_result_dtype(space, dtype, current_guess)
@@ -638,9 +639,10 @@
return descriptor.variable_dtype(space,
'S%d' % space.len_w(w_obj))
return current_guess
- raise oefmt(space.w_NotImplementedError,
- 'unable to create dtype from objects, "%T" instance not '
- 'supported', w_obj)
+ return object_dtype
+ #raise oefmt(space.w_NotImplementedError,
+ # 'unable to create dtype from objects, "%T" instance not '
+ # 'supported', w_obj)
def ufunc_dtype_caller(space, ufunc_name, op_name, argcount, comparison_func,
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit