Author: mattip <[email protected]>
Branch: numpy-flags
Changeset: r77377:a1a4c7af8ad6
Date: 2015-05-17 22:51 +0300
http://bitbucket.org/pypy/pypy/changeset/a1a4c7af8ad6/
Log: add attribute flags to BaseConcreteArray
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
@@ -22,6 +22,9 @@
"""Base class for ndarrays and scalars (aka boxes)."""
_attrs_ = []
+ def get_flags(self):
+ return 0
+
class W_NDimArray(W_NumpyObject):
__metaclass__ = extendabletype
@@ -134,6 +137,9 @@
def get_start(self):
return self.implementation.start
+ def get_flags(self):
+ return self.implementation.flags
+
def ndims(self):
return len(self.get_shape())
ndims._always_inline_ = True
diff --git a/pypy/module/micronumpy/concrete.py
b/pypy/module/micronumpy/concrete.py
--- a/pypy/module/micronumpy/concrete.py
+++ b/pypy/module/micronumpy/concrete.py
@@ -7,11 +7,12 @@
from rpython.rtyper.lltypesystem import rffi, lltype, llmemory
from pypy.module.micronumpy import support, loop, constants as NPY
from pypy.module.micronumpy.base import convert_to_array, W_NDimArray, \
- ArrayArgumentException
+ ArrayArgumentException, W_NumpyObject
from pypy.module.micronumpy.iterators import ArrayIter
from pypy.module.micronumpy.strides import (Chunk, Chunks, NewAxisChunk,
RecordChunk, calc_strides, calc_new_strides, shape_agreement,
- calculate_broadcast_strides, calc_backstrides, calc_start)
+ calculate_broadcast_strides, calc_backstrides, calc_start, is_c_contiguous,
+ is_f_contiguous)
from rpython.rlib.objectmodel import keepalive_until_here
from rpython.rtyper.annlowlevel import cast_gcref_to_instance
from pypy.interpreter.baseobjspace import W_Root
@@ -19,7 +20,8 @@
class BaseConcreteArray(object):
_immutable_fields_ = ['dtype?', 'storage', 'start', 'size', 'shape[*]',
- 'strides[*]', 'backstrides[*]', 'order', 'gcstruct']
+ 'strides[*]', 'backstrides[*]', 'order', 'gcstruct',
+ 'flags']
start = 0
parent = None
flags = 0
@@ -443,6 +445,11 @@
ConcreteArrayNotOwning.__init__(self, shape, dtype, order, strides,
backstrides,
storage, start=start)
self.gcstruct = gcstruct
+ self.flags = NPY.ARRAY_ALIGNED | NPY.ARRAY_WRITEABLE
+ if is_c_contiguous(self):
+ self.flags |= NPY.ARRAY_C_CONTIGUOUS
+ if is_f_contiguous(self):
+ self.flags |= NPY.ARRAY_F_CONTIGUOUS
def __del__(self):
if self.gcstruct:
@@ -456,18 +463,39 @@
ConcreteArrayNotOwning.__init__(self, shape, dtype, order,
strides, backstrides, storage, start)
self.orig_base = orig_base
+ if isinstance(orig_base, W_NumpyObject):
+ self.flags = orig_base.get_flags() & NPY.ARRAY_ALIGNED
+ self.flags |= orig_base.get_flags() & NPY.ARRAY_WRITEABLE
+ else:
+ self.flags = 0
+ if is_c_contiguous(self):
+ self.flags |= NPY.ARRAY_C_CONTIGUOUS
+ if is_f_contiguous(self):
+ self.flags |= NPY.ARRAY_F_CONTIGUOUS
def base(self):
return self.orig_base
class ConcreteNonWritableArrayWithBase(ConcreteArrayWithBase):
+ def __init__(self, shape, dtype, order, strides, backstrides, storage,
+ orig_base, start=0):
+ ConcreteArrayWithBase.__init__(self, shape, dtype, order, strides,
+ backstrides, storage, orig_base, start)
+ self.flags &= ~ NPY.ARRAY_WRITEABLE
+
def descr_setitem(self, space, orig_array, w_index, w_value):
raise OperationError(space.w_ValueError, space.wrap(
"assignment destination is read-only"))
class NonWritableArray(ConcreteArray):
+ def __init__(self, shape, dtype, order, strides, backstrides,
+ storage=lltype.nullptr(RAW_STORAGE), zero=True):
+ ConcreteArray.__init__(self, shape, dtype, order, strides, backstrides,
+ storage, zero)
+ self.flags &= ~ NPY.ARRAY_WRITEABLE
+
def descr_setitem(self, space, orig_array, w_index, w_value):
raise OperationError(space.w_ValueError, space.wrap(
"assignment destination is read-only"))
@@ -491,6 +519,12 @@
self.size = support.product(shape) * self.dtype.elsize
self.start = start
self.orig_arr = orig_arr
+ self.flags = parent.flags & NPY.ARRAY_ALIGNED
+ self.flags |= parent.flags & NPY.ARRAY_WRITEABLE
+ if is_c_contiguous(self):
+ self.flags |= NPY.ARRAY_C_CONTIGUOUS
+ if is_f_contiguous(self):
+ self.flags |= NPY.ARRAY_F_CONTIGUOUS
def base(self):
return self.orig_arr
@@ -538,6 +572,12 @@
return sort_array(self, space, w_axis, w_order)
class NonWritableSliceArray(SliceArray):
+ def __init__(self, start, strides, backstrides, shape, parent, orig_arr,
+ dtype=None):
+ SliceArray.__init__(self, start, strides, backstrides, shape, parent,
+ orig_arr, dtype)
+ self.flags &= ~NPY.ARRAY_WRITEABLE
+
def descr_setitem(self, space, orig_array, w_index, w_value):
raise OperationError(space.w_ValueError, space.wrap(
"assignment destination is read-only"))
@@ -549,6 +589,8 @@
self.gcstruct = V_OBJECTSTORE
self.dtype = dtype
self.size = size
+ self.flags = (NPY.ARRAY_C_CONTIGUOUS | NPY.ARRAY_F_CONTIGUOUS |
+ NPY.ARRAY_WRITEABLE | NPY.ARRAY_ALIGNED)
def __del__(self):
free_raw_storage(self.storage)
diff --git a/pypy/module/micronumpy/constants.py
b/pypy/module/micronumpy/constants.py
--- a/pypy/module/micronumpy/constants.py
+++ b/pypy/module/micronumpy/constants.py
@@ -77,8 +77,20 @@
WRAP = 1
RAISE = 2
+# These can be requested in constructor functions and tested for
ARRAY_C_CONTIGUOUS = 0x0001
ARRAY_F_CONTIGUOUS = 0x0002
+ARRAY_ALIGNED = 0x0100
+ARRAY_WRITEABLE = 0x0400
+ARRAY_UPDATEIFCOPY = 0x1000 # base contains a ref to an array, update it too
+# These can be tested for
+ARRAY_OWNDATA = 0x004
+# These can be requested in constructor functions
+ARRAY_FORECAST = 0x0010 # causes a cast to occur even if not safe to do so
+ARRAY_ENSURECOPY = 0x0020 # returned array will be CONTIGUOUS, ALIGNED,
WRITEABLE
+ARRAY_ENSUREARRAY = 0x0040 # return only ndarray, not subtype
+ARRAY_ELEMENTSTRIDES = 0x0080 # strides are units of the dtype element size
+ARRAY_NOTSWAPPED = 0x0200 #native byte order
LITTLE = '<'
BIG = '>'
diff --git a/pypy/module/micronumpy/nditer.py b/pypy/module/micronumpy/nditer.py
--- a/pypy/module/micronumpy/nditer.py
+++ b/pypy/module/micronumpy/nditer.py
@@ -4,7 +4,7 @@
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault
from pypy.interpreter.error import OperationError, oefmt
from pypy.module.micronumpy import support, concrete
-from pypy.module.micronumpy.base import W_NDimArray, convert_to_array
+from pypy.module.micronumpy.base import W_NDimArray, convert_to_array,
W_NumpyObject
from pypy.module.micronumpy.descriptor import decode_w_dtype
from pypy.module.micronumpy.iterators import ArrayIter
from pypy.module.micronumpy.strides import (calculate_broadcast_strides,
@@ -363,7 +363,7 @@
return ret
-class W_NDIter(W_Root):
+class W_NDIter(W_NumpyObject):
_immutable_fields_ = ['ndim', ]
def __init__(self, space, w_seq, w_flags, w_op_flags, w_op_dtypes,
w_casting, w_op_axes, w_itershape, buffersize=0, order='K'):
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit