[pypy-commit] pypy numpy-refactor: consolidate array creation funcs in ctors.py

bdkearns noreply at buildbot.pypy.org
Thu Feb 27 07:11:25 CET 2014


Author: Brian Kearns <bdkearns at gmail.com>
Branch: numpy-refactor
Changeset: r69495:9fa7ed888869
Date: 2014-02-27 01:01 -0500
http://bitbucket.org/pypy/pypy/changeset/9fa7ed888869/

Log:	consolidate array creation funcs in ctors.py

diff --git a/pypy/module/cpyext/ndarrayobject.py b/pypy/module/cpyext/ndarrayobject.py
--- a/pypy/module/cpyext/ndarrayobject.py
+++ b/pypy/module/cpyext/ndarrayobject.py
@@ -7,7 +7,8 @@
 from rpython.rtyper.lltypesystem import rffi, lltype
 from pypy.module.cpyext.api import cpython_api, Py_ssize_t, CANNOT_FAIL
 from pypy.module.cpyext.api import PyObject
-from pypy.module.micronumpy.ndarray import W_NDimArray, array
+from pypy.module.micronumpy.ndarray import W_NDimArray
+from pypy.module.micronumpy.ctors import array
 from pypy.module.micronumpy.descriptor import get_dtype_cache, W_Dtype
 from pypy.module.micronumpy.concrete import ConcreteArray
 from rpython.rlib.rawstorage import RAW_STORAGE_PTR
diff --git a/pypy/module/micronumpy/__init__.py b/pypy/module/micronumpy/__init__.py
--- a/pypy/module/micronumpy/__init__.py
+++ b/pypy/module/micronumpy/__init__.py
@@ -7,14 +7,14 @@
         'ndarray': 'ndarray.W_NDimArray',
         'dtype': 'descriptor.W_Dtype',
 
-        'array': 'ndarray.array',
-        'zeros': 'ndarray.zeros',
-        'empty': 'ndarray.zeros',
-        'empty_like': 'ndarray.empty_like',
+        'array': 'ctors.array',
+        'zeros': 'ctors.zeros',
+        'empty': 'ctors.zeros',
+        'empty_like': 'ctors.empty_like',
         '_reconstruct' : 'ndarray._reconstruct',
-        'scalar' : 'ndarray.build_scalar',
+        'scalar' : 'ctors.build_scalar',
         'dot': 'arrayops.dot',
-        'fromstring': 'interp_support.fromstring',
+        'fromstring': 'ctors.fromstring',
         'flatiter': 'flatiter.W_FlatIterator',
         'concatenate': 'arrayops.concatenate',
         'where': 'arrayops.where',
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
@@ -86,7 +86,7 @@
 
 
 def convert_to_array(space, w_obj):
-    from pypy.module.micronumpy.ndarray import array
+    from pypy.module.micronumpy.ctors import array
     if isinstance(w_obj, W_NDimArray):
         return w_obj
     return array(space, w_obj)
diff --git a/pypy/module/micronumpy/boxes.py b/pypy/module/micronumpy/boxes.py
--- a/pypy/module/micronumpy/boxes.py
+++ b/pypy/module/micronumpy/boxes.py
@@ -40,7 +40,7 @@
         return get_dtype_cache(space).dtypes_by_num[num]
 
     def descr__new__(space, w_subtype, w_value=None):
-        from pypy.module.micronumpy.ndarray import array
+        from pypy.module.micronumpy.ctors import array
         dtype = _get_dtype(space)
         if not space.is_none(w_value):
             w_arr = array(space, w_value, dtype, copy=False)
diff --git a/pypy/module/micronumpy/compile.py b/pypy/module/micronumpy/compile.py
--- a/pypy/module/micronumpy/compile.py
+++ b/pypy/module/micronumpy/compile.py
@@ -10,7 +10,7 @@
 from pypy.module.micronumpy import boxes
 from pypy.module.micronumpy.descriptor import get_dtype_cache
 from pypy.module.micronumpy.base import W_NDimArray
-from pypy.module.micronumpy.ndarray import array
+from pypy.module.micronumpy.ctors import array
 from pypy.module.micronumpy.arrayops import where
 from pypy.module.micronumpy import ufuncs
 from rpython.rlib.objectmodel import specialize, instantiate
diff --git a/pypy/module/micronumpy/converters.py b/pypy/module/micronumpy/converters.py
--- a/pypy/module/micronumpy/converters.py
+++ b/pypy/module/micronumpy/converters.py
@@ -84,3 +84,15 @@
                 "duplicate value in 'axis'"))
         out[axis] = True
     return out
+
+
+def shape_converter(space, w_size, dtype):
+    if space.is_none(w_size):
+        return []
+    if space.isinstance_w(w_size, space.w_int):
+        return [space.int_w(w_size)]
+    shape = []
+    for w_item in space.fixedview(w_size):
+        shape.append(space.int_w(w_item))
+    shape += dtype.shape
+    return shape[:]
diff --git a/pypy/module/micronumpy/interp_support.py b/pypy/module/micronumpy/ctors.py
rename from pypy/module/micronumpy/interp_support.py
rename to pypy/module/micronumpy/ctors.py
--- a/pypy/module/micronumpy/interp_support.py
+++ b/pypy/module/micronumpy/ctors.py
@@ -3,9 +3,117 @@
 from rpython.rtyper.lltypesystem import lltype, rffi
 from pypy.module.micronumpy import descriptor, loop
 from rpython.rlib.rstring import strip_spaces
-from pypy.module.micronumpy.base import W_NDimArray
+from pypy.module.micronumpy import ufuncs
+from pypy.module.micronumpy.base import W_NDimArray, convert_to_array
+from pypy.module.micronumpy.converters import shape_converter
+from pypy.module.micronumpy.strides import find_shape_and_elems
 
-FLOAT_SIZE = rffi.sizeof(lltype.Float)
+
+def build_scalar(space, w_dtype, w_state):
+    from rpython.rtyper.lltypesystem import rffi, lltype
+    if not isinstance(w_dtype, descriptor.W_Dtype):
+        raise oefmt(space.w_TypeError,
+                    "argument 1 must be numpy.dtype, not %T", w_dtype)
+    if w_dtype.elsize == 0:
+        raise oefmt(space.w_ValueError, "itemsize cannot be zero")
+    if not space.isinstance_w(w_state, space.w_str):
+        raise oefmt(space.w_TypeError, "initializing object must be a string")
+    if space.len_w(w_state) != w_dtype.elsize:
+        raise oefmt(space.w_ValueError, "initialization string is too small")
+    state = rffi.str2charp(space.str_w(w_state))
+    box = w_dtype.itemtype.box_raw_data(state)
+    lltype.free(state, flavor="raw")
+    return box
+
+
+ at unwrap_spec(ndmin=int, copy=bool, subok=bool)
+def array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False,
+          ndmin=0):
+    # for anything that isn't already an array, try __array__ method first
+    if not isinstance(w_object, W_NDimArray):
+        w___array__ = space.lookup(w_object, "__array__")
+        if w___array__ is not None:
+            if space.is_none(w_dtype):
+                w_dtype = space.w_None
+            w_array = space.get_and_call_function(w___array__, w_object, w_dtype)
+            if isinstance(w_array, W_NDimArray):
+                # feed w_array back into array() for other properties
+                return array(space, w_array, w_dtype, False, w_order, subok, ndmin)
+            else:
+                raise oefmt(space.w_ValueError,
+                            "object __array__ method not producing an array")
+
+    dtype = descriptor.decode_w_dtype(space, w_dtype)
+
+    if space.is_none(w_order):
+        order = 'C'
+    else:
+        order = space.str_w(w_order)
+        if order != 'C':  # or order != 'F':
+            raise oefmt(space.w_ValueError, "Unknown order: %s", order)
+
+    # arrays with correct dtype
+    if isinstance(w_object, W_NDimArray) and \
+            (space.is_none(w_dtype) or w_object.get_dtype() is dtype):
+        shape = w_object.get_shape()
+        if copy:
+            w_ret = w_object.descr_copy(space)
+        else:
+            if ndmin <= len(shape):
+                return w_object
+            new_impl = w_object.implementation.set_shape(space, w_object, shape)
+            w_ret = W_NDimArray(new_impl)
+        if ndmin > len(shape):
+            shape = [1] * (ndmin - len(shape)) + shape
+            w_ret.implementation = w_ret.implementation.set_shape(space,
+                                                                  w_ret, shape)
+        return w_ret
+
+    # not an array or incorrect dtype
+    shape, elems_w = find_shape_and_elems(space, w_object, dtype)
+    if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
+        for w_elem in elems_w:
+            if isinstance(w_elem, W_NDimArray) and w_elem.is_scalar():
+                w_elem = w_elem.get_scalar_value()
+            dtype = ufuncs.find_dtype_for_scalar(space, w_elem, dtype)
+        if dtype is None:
+            dtype = descriptor.get_dtype_cache(space).w_float64dtype
+        elif dtype.is_str_or_unicode() and dtype.elsize < 1:
+            # promote S0 -> S1, U0 -> U1
+            dtype = descriptor.variable_dtype(space, dtype.char + '1')
+
+    if ndmin > len(shape):
+        shape = [1] * (ndmin - len(shape)) + shape
+    w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
+    arr_iter = w_arr.create_iter()
+    for w_elem in elems_w:
+        arr_iter.setitem(dtype.coerce(space, w_elem))
+        arr_iter.next()
+    return w_arr
+
+
+def zeros(space, w_shape, w_dtype=None, w_order=None):
+    dtype = space.interp_w(descriptor.W_Dtype,
+        space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
+    if dtype.is_str_or_unicode() and dtype.elsize < 1:
+        dtype = descriptor.variable_dtype(space, dtype.char + '1')
+    shape = shape_converter(space, w_shape, dtype)
+    return W_NDimArray.from_shape(space, shape, dtype=dtype)
+
+
+ at unwrap_spec(subok=bool)
+def empty_like(space, w_a, w_dtype=None, w_order=None, subok=True):
+    w_a = convert_to_array(space, w_a)
+    if w_dtype is None:
+        dtype = w_a.get_dtype()
+    else:
+        dtype = space.interp_w(descriptor.W_Dtype,
+            space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
+        if dtype.is_str_or_unicode() and dtype.elsize < 1:
+            dtype = descriptor.variable_dtype(space, dtype.char + '1')
+    return W_NDimArray.from_shape(space, w_a.get_shape(), dtype=dtype,
+                                  w_instance=w_a if subok else None)
+
 
 def _fromstring_text(space, s, count, sep, length, dtype):
     sep_stripped = strip_spaces(sep)
@@ -57,6 +165,7 @@
 
     return space.wrap(a)
 
+
 def _fromstring_bin(space, s, count, length, dtype):
     itemsize = dtype.elsize
     assert itemsize >= 0
@@ -74,11 +183,11 @@
     loop.fromstring_loop(space, a, dtype, itemsize, s)
     return space.wrap(a)
 
+
 @unwrap_spec(s=str, count=int, sep=str, w_dtype=WrappedDefault(None))
 def fromstring(space, s, w_dtype=None, count=-1, sep=''):
     dtype = space.interp_w(descriptor.W_Dtype,
-        space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype)
-    )
+        space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
     length = len(s)
     if sep == '':
         return _fromstring_bin(space, s, count, length, dtype)
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
@@ -7,9 +7,8 @@
 from pypy.module.micronumpy.base import W_NDimArray, convert_to_array,\
      ArrayArgumentException, wrap_impl
 from pypy.module.micronumpy import descriptor, ufuncs, boxes, arrayops
-from pypy.module.micronumpy.strides import find_shape_and_elems,\
-     get_shape_from_iterable, to_coords, shape_agreement, \
-     shape_agreement_multiple
+from pypy.module.micronumpy.strides import get_shape_from_iterable, to_coords, \
+    shape_agreement, shape_agreement_multiple
 from pypy.module.micronumpy.flagsobj import W_FlagsObject
 from pypy.module.micronumpy.flatiter import W_FlatIterator
 from pypy.module.micronumpy.appbridge import get_appbridge_cache
@@ -19,20 +18,11 @@
 from rpython.rlib import jit
 from rpython.rlib.rstring import StringBuilder
 from pypy.module.micronumpy.concrete import BaseConcreteArray
-from pypy.module.micronumpy.converters import order_converter, multi_axis_converter
+from pypy.module.micronumpy.converters import order_converter, shape_converter, \
+    multi_axis_converter
 from pypy.module.micronumpy import support
 from pypy.module.micronumpy import constants as NPY
 
-def _find_shape(space, w_size, dtype):
-    if space.is_none(w_size):
-        return []
-    if space.isinstance_w(w_size, space.w_int):
-        return [space.int_w(w_size)]
-    shape = []
-    for w_item in space.fixedview(w_size):
-        shape.append(space.int_w(w_item))
-    shape += dtype.shape
-    return shape[:]
 
 def _match_dot_shapes(space, left, right):
     left_shape = left.get_shape()
@@ -1141,7 +1131,7 @@
     from pypy.module.micronumpy.support import calc_strides
     dtype = space.interp_w(descriptor.W_Dtype,
           space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
-    shape = _find_shape(space, w_shape, dtype)
+    shape = shape_converter(space, w_shape, dtype)
 
     if not space.is_none(w_buffer):
         if (not space.is_none(w_strides)):
@@ -1194,7 +1184,7 @@
     dtype = space.interp_w(descriptor.W_Dtype,
                      space.call_function(space.gettypefor(descriptor.W_Dtype),
                              w_dtype))
-    shape = _find_shape(space, w_shape, dtype)
+    shape = shape_converter(space, w_shape, dtype)
     if w_subtype:
         if not space.isinstance_w(w_subtype, space.w_type):
             raise OperationError(space.w_ValueError, space.wrap(
@@ -1395,107 +1385,6 @@
     __array__         = interp2app(W_NDimArray.descr___array__),
 )
 
- at unwrap_spec(ndmin=int, copy=bool, subok=bool)
-def array(space, w_object, w_dtype=None, copy=True, w_order=None, subok=False,
-          ndmin=0):
-    # for anything that isn't already an array, try __array__ method first
-    if not isinstance(w_object, W_NDimArray):
-        w___array__ = space.lookup(w_object, "__array__")
-        if w___array__ is not None:
-            if space.is_none(w_dtype):
-                w_dtype = space.w_None
-            w_array = space.get_and_call_function(w___array__, w_object, w_dtype)
-            if isinstance(w_array, W_NDimArray):
-                # feed w_array back into array() for other properties
-                return array(space, w_array, w_dtype, False, w_order, subok, ndmin)
-            else:
-                raise oefmt(space.w_ValueError,
-                            "object __array__ method not producing an array")
-
-    dtype = descriptor.decode_w_dtype(space, w_dtype)
-
-    if space.is_none(w_order):
-        order = 'C'
-    else:
-        order = space.str_w(w_order)
-        if order != 'C':  # or order != 'F':
-            raise oefmt(space.w_ValueError, "Unknown order: %s", order)
-
-    # arrays with correct dtype
-    if isinstance(w_object, W_NDimArray) and \
-            (space.is_none(w_dtype) or w_object.get_dtype() is dtype):
-        shape = w_object.get_shape()
-        if copy:
-            w_ret = w_object.descr_copy(space)
-        else:
-            if ndmin <= len(shape):
-                return w_object
-            new_impl = w_object.implementation.set_shape(space, w_object, shape)
-            w_ret = W_NDimArray(new_impl)
-        if ndmin > len(shape):
-            shape = [1] * (ndmin - len(shape)) + shape
-            w_ret.implementation = w_ret.implementation.set_shape(space,
-                                                                  w_ret, shape)
-        return w_ret
-
-    # not an array or incorrect dtype
-    shape, elems_w = find_shape_and_elems(space, w_object, dtype)
-    if dtype is None or (dtype.is_str_or_unicode() and dtype.elsize < 1):
-        for w_elem in elems_w:
-            if isinstance(w_elem, W_NDimArray) and w_elem.is_scalar():
-                w_elem = w_elem.get_scalar_value()
-            dtype = ufuncs.find_dtype_for_scalar(space, w_elem, dtype)
-        if dtype is None:
-            dtype = descriptor.get_dtype_cache(space).w_float64dtype
-        elif dtype.is_str_or_unicode() and dtype.elsize < 1:
-            # promote S0 -> S1, U0 -> U1
-            dtype = descriptor.variable_dtype(space, dtype.char + '1')
-
-    if ndmin > len(shape):
-        shape = [1] * (ndmin - len(shape)) + shape
-    w_arr = W_NDimArray.from_shape(space, shape, dtype, order=order)
-    arr_iter = w_arr.create_iter()
-    for w_elem in elems_w:
-        arr_iter.setitem(dtype.coerce(space, w_elem))
-        arr_iter.next()
-    return w_arr
-
-def zeros(space, w_shape, w_dtype=None, w_order=None):
-    dtype = space.interp_w(descriptor.W_Dtype,
-        space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
-    if dtype.is_str_or_unicode() and dtype.elsize < 1:
-        dtype = descriptor.variable_dtype(space, dtype.char + '1')
-    shape = _find_shape(space, w_shape, dtype)
-    return W_NDimArray.from_shape(space, shape, dtype=dtype)
-
- at unwrap_spec(subok=bool)
-def empty_like(space, w_a, w_dtype=None, w_order=None, subok=True):
-    w_a = convert_to_array(space, w_a)
-    if w_dtype is None:
-        dtype = w_a.get_dtype()
-    else:
-        dtype = space.interp_w(descriptor.W_Dtype,
-            space.call_function(space.gettypefor(descriptor.W_Dtype), w_dtype))
-        if dtype.is_str_or_unicode() and dtype.elsize < 1:
-            dtype = descriptor.variable_dtype(space, dtype.char + '1')
-    return W_NDimArray.from_shape(space, w_a.get_shape(), dtype=dtype,
-                                  w_instance=w_a if subok else None)
-
-def build_scalar(space, w_dtype, w_state):
-    from rpython.rtyper.lltypesystem import rffi, lltype
-    if not isinstance(w_dtype, descriptor.W_Dtype):
-        raise oefmt(space.w_TypeError,
-                    "argument 1 must be numpy.dtype, not %T", w_dtype)
-    if w_dtype.elsize == 0:
-        raise oefmt(space.w_ValueError, "itemsize cannot be zero")
-    if not space.isinstance_w(w_state, space.w_str):
-        raise oefmt(space.w_TypeError, "initializing object must be a string")
-    if space.len_w(w_state) != w_dtype.elsize:
-        raise oefmt(space.w_ValueError, "initialization string is too small")
-    state = rffi.str2charp(space.str_w(w_state))
-    box = w_dtype.itemtype.box_raw_data(state)
-    lltype.free(state, flavor="raw")
-    return box
 
 def _reconstruct(space, w_subtype, w_shape, w_dtype):
     return descr_new_array(space, w_subtype, w_shape, w_dtype)


More information about the pypy-commit mailing list