diff options
author | Mark Wiebe <mwiebe@enthought.com> | 2011-07-26 12:14:21 -0500 |
---|---|---|
committer | Mark Wiebe <mwiebe@enthought.com> | 2011-07-26 12:14:21 -0500 |
commit | 9163993794f1bc56c279ab3d90796370d6b579c4 (patch) | |
tree | f5a40b3c0ca60bf9d7a645073e0600e36ccdd60f /numpy | |
parent | 1b62bdfb04e56f75fc61dbbd1f2600a72951b19d (diff) | |
parent | affea42d886e8233fdd6f3c5760708e3a9e9b1b8 (diff) | |
download | numpy-9163993794f1bc56c279ab3d90796370d6b579c4.tar.gz |
Merge branch 'deprecate_array_field_access'
Diffstat (limited to 'numpy')
67 files changed, 2974 insertions, 2371 deletions
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index e46e1441a..3d6702095 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -132,60 +132,42 @@ def _reconstruct(subtype, shape, dtype): return ndarray.__new__(subtype, shape, dtype) -# format_re and _split were taken from numarray by J. Todd Miller +# format_re was originally from numarray by J. Todd Miller -def _split(input): - """Split the input formats string into field formats without splitting - the tuple used to specify multi-dimensional arrays.""" - - newlist = [] - hold = asbytes('') - - listinput = input.split(asbytes(',')) - for element in listinput: - if hold != asbytes(''): - item = hold + asbytes(',') + element - else: - item = element - left = item.count(asbytes('(')) - right = item.count(asbytes(')')) - - # if the parenthesis is not balanced, hold the string - if left > right : - hold = item - - # when balanced, append to the output list and reset the hold - elif left == right: - newlist.append(item.strip()) - hold = asbytes('') - - # too many close parenthesis is unacceptable - else: - raise SyntaxError(item) - - # if there is string left over in hold - if hold != asbytes(''): - raise SyntaxError(hold) - - return newlist - -format_re = re.compile(asbytes(r'(?P<order1>[<>|=]?)(?P<repeats> *[(]?[ ,0-9]*[)]? *)(?P<order2>[<>|=]?)(?P<dtype>[A-Za-z0-9.]*)')) +format_re = re.compile(asbytes( + r'(?P<order1>[<>|=]?)' + r'(?P<repeats> *[(]?[ ,0-9]*[)]? *)' + r'(?P<order2>[<>|=]?)' + r'(?P<dtype>[A-Za-z0-9.]*(?:\[[a-zA-Z0-9,.]+\])?)')) +sep_re = re.compile(asbytes(r'\s*,\s*')) +space_re = re.compile(asbytes(r'\s+$')) # astr is a string (perhaps comma separated) _convorder = {asbytes('='): _nbo} def _commastring(astr): - res = _split(astr) - if (len(res)) < 1: - raise ValueError("unrecognized formant") + startindex = 0 result = [] - for k,item in enumerate(res): - # convert item + while startindex < len(astr): + mo = format_re.match(astr, pos=startindex) try: - (order1, repeats, order2, dtype) = format_re.match(item).groups() + (order1, repeats, order2, dtype) = mo.groups() except (TypeError, AttributeError): - raise ValueError('format %s is not recognized' % item) + raise ValueError('format number %d of "%s" is not recognized' % + (len(result)+1, astr)) + startindex = mo.end() + # Separator or ending padding + if startindex < len(astr): + if space_re.match(astr, pos=startindex): + startindex = len(astr) + else: + mo = sep_re.match(astr, pos=startindex) + if not mo: + raise ValueError( + 'format number %d of "%s" is not recognized' % + (len(result)+1, astr)) + startindex = mo.end() if order2 == asbytes(''): order = order1 @@ -195,7 +177,7 @@ def _commastring(astr): order1 = _convorder.get(order1, order1) order2 = _convorder.get(order2, order2) if (order1 != order2): - raise ValueError('in-consistent byte-order specification %s and %s' % (order1, order2)) + raise ValueError('inconsistent byte-order specification %s and %s' % (order1, order2)) order = order1 if order in [asbytes('|'), asbytes('='), _nbo]: diff --git a/numpy/core/code_generators/generate_numpy_api.py b/numpy/core/code_generators/generate_numpy_api.py index 2f4316d17..7cd4b9f6a 100644 --- a/numpy/core/code_generators/generate_numpy_api.py +++ b/numpy/core/code_generators/generate_numpy_api.py @@ -220,7 +220,9 @@ def do_generate_api(targets, sources): for name, index in types_api.items(): multiarray_api_dict[name] = TypeApi(name, index, 'PyTypeObject', api_name) - assert len(multiarray_api_dict) == len(multiarray_api_index) + if len(multiarray_api_dict) != len(multiarray_api_index): + raise AssertionError, "Multiarray API size mismatch %d %d" % \ + (len(multiarray_api_dict), len(multiarray_api_index)) extension_list = [] for name, index in genapi.order_dict(multiarray_api_index): diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py index 296f4683d..477cd122b 100644 --- a/numpy/core/code_generators/generate_umath.py +++ b/numpy/core/code_generators/generate_umath.py @@ -121,7 +121,7 @@ import string if sys.version_info[0] < 3: UPPER_TABLE = string.maketrans(string.ascii_lowercase, string.ascii_uppercase) else: - UPPER_TABLE = bytes.maketrans(bytes(string.ascii_lowercase, "ascii"), + UPPER_TABLE = bytes.maketrans(bytes(string.ascii_lowercase, "ascii"), bytes(string.ascii_uppercase, "ascii")) def english_upper(s): diff --git a/numpy/core/code_generators/numpy_api.py b/numpy/core/code_generators/numpy_api.py index d83371319..a256d849a 100644 --- a/numpy/core/code_generators/numpy_api.py +++ b/numpy/core/code_generators/numpy_api.py @@ -318,6 +318,7 @@ multiarray_funcs_api = { # End 1.6 API 'PyArray_MaskedCopyInto': 281, 'PyArray_MaskedMoveInto': 282, + 'PyArray_SetBaseObject': 283, } ufunc_types_api = { diff --git a/numpy/core/include/numpy/ndarrayobject.h b/numpy/core/include/numpy/ndarrayobject.h index 04aa8d0e3..03dfcd9cd 100644 --- a/numpy/core/include/numpy/ndarrayobject.h +++ b/numpy/core/include/numpy/ndarrayobject.h @@ -42,7 +42,8 @@ extern "C" CONFUSE_EMACS #define PyArray_HasArrayInterface(op, out) \ PyArray_HasArrayInterfaceType(op, NULL, NULL, out) -#define PyArray_IsZeroDim(op) (PyArray_Check(op) && (PyArray_NDIM(op) == 0)) +#define PyArray_IsZeroDim(op) (PyArray_Check(op) && \ + (PyArray_NDIM((PyArrayObject *)op) == 0)) #define PyArray_IsScalar(obj, cls) \ (PyObject_TypeCheck(obj, &Py##cls##ArrType_Type)) @@ -159,12 +160,18 @@ extern "C" CONFUSE_EMACS (k)*PyArray_STRIDES(obj)[2] + \ (l)*PyArray_STRIDES(obj)[3])) -#define PyArray_XDECREF_ERR(obj) \ - if (obj && (PyArray_FLAGS(obj) & NPY_ARRAY_UPDATEIFCOPY)) { \ - PyArray_FLAGS(PyArray_BASE(obj)) |= NPY_ARRAY_WRITEABLE; \ - PyArray_FLAGS(obj) &= ~NPY_ARRAY_UPDATEIFCOPY; \ - } \ - Py_XDECREF(obj) +static NPY_INLINE void +PyArray_XDECREF_ERR(PyArrayObject *arr) +{ + if (arr != NULL) { + if (PyArray_FLAGS(arr) & NPY_ARRAY_UPDATEIFCOPY) { + PyArrayObject *base = (PyArrayObject *)PyArray_BASE(arr); + PyArray_ENABLEFLAGS(base, NPY_ARRAY_WRITEABLE); + PyArray_CLEARFLAGS(arr, NPY_ARRAY_UPDATEIFCOPY); + } + Py_DECREF(arr); + } +} #define PyArray_DESCR_REPLACE(descr) do { \ PyArray_Descr *_new_; \ diff --git a/numpy/core/include/numpy/ndarraytypes.h b/numpy/core/include/numpy/ndarraytypes.h index 0f0673825..af705b936 100644 --- a/numpy/core/include/numpy/ndarraytypes.h +++ b/numpy/core/include/numpy/ndarraytypes.h @@ -74,7 +74,7 @@ enum NPY_TYPES { NPY_BOOL=0, * New 1.6 types appended, may be integrated * into the above in 2.0. */ - NPY_DATETIME, NPY_TIMEDELTA, NPY_HALF, + NPY_DATETIME, NPY_TIMEDELTA, NPY_HALF, NPY_NTYPES, NPY_NOTYPE, @@ -519,7 +519,7 @@ typedef struct { NPY_ITEM_IS_POINTER | NPY_ITEM_REFCOUNT | \ NPY_NEEDS_INIT | NPY_NEEDS_PYAPI) -#define PyDataType_FLAGCHK(dtype, flag) \ +#define PyDataType_FLAGCHK(dtype, flag) \ (((dtype)->flags & (flag)) == (flag)) #define PyDataType_REFCHK(dtype) \ @@ -573,12 +573,16 @@ typedef struct _arr_descr { } PyArray_ArrayDescr; /* - * The main array object structure. It is recommended to use the macros - * defined below (PyArray_DATA and friends) access fields here, instead - * of the members themselves. + * The main array object structure. + * + * It has been recommended to use the inline functions defined below + * (PyArray_DATA and friends) to access fields here for a number of + * releases. Direct access to the members themselves is deprecated. + * To ensure that your code does not use deprecated access, + * #define NPY_NO_DEPRECATED_API. */ - -typedef struct PyArrayObject { +/* This struct will be moved to a private header in a future release */ +typedef struct tagPyArrayObject_fieldaccess { PyObject_HEAD char *data; /* pointer to raw data buffer */ int nd; /* number of dimensions, also called ndim */ @@ -605,7 +609,25 @@ typedef struct PyArrayObject { PyArray_Descr *descr; /* Pointer to type structure */ int flags; /* Flags describing array -- see below */ PyObject *weakreflist; /* For weakreferences */ +} PyArrayObject_fieldaccess; + +/* + * To hide the implementation details, we only expose + * the Python struct HEAD. + */ +#ifdef NPY_NO_DEPRECATED_API +typedef struct tagPyArrayObject { + PyObject_HEAD } PyArrayObject; +#else +/* + * Can't put this in npy_deprecated_api.h like the others. + * PyArrayObject field access is deprecated as of NumPy 1.7. + */ +typedef PyArrayObject_fieldaccess PyArrayObject; +#endif + +#define NPY_SIZEOF_PYARRAYOBJECT (sizeof(PyArrayObject_fieldaccess)) /* Array Flags Object */ typedef struct PyArrayFlagsObject { @@ -785,9 +807,6 @@ typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *); */ -#define PyArray_CHKFLAGS(m, FLAGS) \ - ((((PyArrayObject *)(m))->flags & (FLAGS)) == (FLAGS)) - #define PyArray_ISCONTIGUOUS(m) PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) #define PyArray_ISWRITEABLE(m) PyArray_CHKFLAGS(m, NPY_ARRAY_WRITEABLE) #define PyArray_ISALIGNED(m) PyArray_CHKFLAGS(m, NPY_ARRAY_ALIGNED) @@ -939,122 +958,122 @@ struct PyArrayIterObject_tag { #define PyArrayIter_Check(op) PyObject_TypeCheck(op, &PyArrayIter_Type) #define _PyAIT(it) ((PyArrayIterObject *)(it)) -#define PyArray_ITER_RESET(it) { \ - _PyAIT(it)->index = 0; \ - _PyAIT(it)->dataptr = _PyAIT(it)->ao->data; \ - memset(_PyAIT(it)->coordinates, 0, \ - (_PyAIT(it)->nd_m1+1)*sizeof(npy_intp)); \ +#define PyArray_ITER_RESET(it) { \ + _PyAIT(it)->index = 0; \ + _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \ + memset(_PyAIT(it)->coordinates, 0, \ + (_PyAIT(it)->nd_m1+1)*sizeof(npy_intp)); \ } -#define _PyArray_ITER_NEXT1(it) { \ - (it)->dataptr += _PyAIT(it)->strides[0]; \ - (it)->coordinates[0]++; \ +#define _PyArray_ITER_NEXT1(it) { \ + (it)->dataptr += _PyAIT(it)->strides[0]; \ + (it)->coordinates[0]++; \ } -#define _PyArray_ITER_NEXT2(it) { \ - if ((it)->coordinates[1] < (it)->dims_m1[1]) { \ - (it)->coordinates[1]++; \ - (it)->dataptr += (it)->strides[1]; \ - } \ - else { \ - (it)->coordinates[1] = 0; \ - (it)->coordinates[0]++; \ - (it)->dataptr += (it)->strides[0] - \ - (it)->backstrides[1]; \ - } \ +#define _PyArray_ITER_NEXT2(it) { \ + if ((it)->coordinates[1] < (it)->dims_m1[1]) { \ + (it)->coordinates[1]++; \ + (it)->dataptr += (it)->strides[1]; \ + } \ + else { \ + (it)->coordinates[1] = 0; \ + (it)->coordinates[0]++; \ + (it)->dataptr += (it)->strides[0] - \ + (it)->backstrides[1]; \ + } \ } -#define _PyArray_ITER_NEXT3(it) { \ - if ((it)->coordinates[2] < (it)->dims_m1[2]) { \ - (it)->coordinates[2]++; \ - (it)->dataptr += (it)->strides[2]; \ - } \ - else { \ - (it)->coordinates[2] = 0; \ - (it)->dataptr -= (it)->backstrides[2]; \ - if ((it)->coordinates[1] < (it)->dims_m1[1]) { \ - (it)->coordinates[1]++; \ - (it)->dataptr += (it)->strides[1]; \ - } \ - else { \ - (it)->coordinates[1] = 0; \ - (it)->coordinates[0]++; \ - (it)->dataptr += (it)->strides[0] - \ - (it)->backstrides[1]; \ - } \ - } \ +#define _PyArray_ITER_NEXT3(it) { \ + if ((it)->coordinates[2] < (it)->dims_m1[2]) { \ + (it)->coordinates[2]++; \ + (it)->dataptr += (it)->strides[2]; \ + } \ + else { \ + (it)->coordinates[2] = 0; \ + (it)->dataptr -= (it)->backstrides[2]; \ + if ((it)->coordinates[1] < (it)->dims_m1[1]) { \ + (it)->coordinates[1]++; \ + (it)->dataptr += (it)->strides[1]; \ + } \ + else { \ + (it)->coordinates[1] = 0; \ + (it)->coordinates[0]++; \ + (it)->dataptr += (it)->strides[0] \ + (it)->backstrides[1]; \ + } \ + } \ } -#define PyArray_ITER_NEXT(it) { \ - _PyAIT(it)->index++; \ - if (_PyAIT(it)->nd_m1 == 0) { \ - _PyArray_ITER_NEXT1(_PyAIT(it)); \ - } \ - else if (_PyAIT(it)->contiguous) \ - _PyAIT(it)->dataptr += _PyAIT(it)->ao->descr->elsize; \ - else if (_PyAIT(it)->nd_m1 == 1) { \ - _PyArray_ITER_NEXT2(_PyAIT(it)); \ - } \ - else { \ - int __npy_i; \ +#define PyArray_ITER_NEXT(it) { \ + _PyAIT(it)->index++; \ + if (_PyAIT(it)->nd_m1 == 0) { \ + _PyArray_ITER_NEXT1(_PyAIT(it)); \ + } \ + else if (_PyAIT(it)->contiguous) \ + _PyAIT(it)->dataptr += PyArray_DESCR(_PyAIT(it)->ao)->elsize; \ + else if (_PyAIT(it)->nd_m1 == 1) { \ + _PyArray_ITER_NEXT2(_PyAIT(it)); \ + } \ + else { \ + int __npy_i; \ for (__npy_i=_PyAIT(it)->nd_m1; __npy_i >= 0; __npy_i--) { \ - if (_PyAIT(it)->coordinates[__npy_i] < \ - _PyAIT(it)->dims_m1[__npy_i]) { \ - _PyAIT(it)->coordinates[__npy_i]++; \ - _PyAIT(it)->dataptr += \ - _PyAIT(it)->strides[__npy_i]; \ - break; \ - } \ - else { \ - _PyAIT(it)->coordinates[__npy_i] = 0; \ - _PyAIT(it)->dataptr -= \ - _PyAIT(it)->backstrides[__npy_i]; \ - } \ - } \ - } \ + if (_PyAIT(it)->coordinates[__npy_i] < \ + _PyAIT(it)->dims_m1[__npy_i]) { \ + _PyAIT(it)->coordinates[__npy_i]++; \ + _PyAIT(it)->dataptr += \ + _PyAIT(it)->strides[__npy_i]; \ + break; \ + } \ + else { \ + _PyAIT(it)->coordinates[__npy_i] = 0; \ + _PyAIT(it)->dataptr -= \ + _PyAIT(it)->backstrides[__npy_i]; \ + } \ + } \ + } \ } -#define PyArray_ITER_GOTO(it, destination) { \ - int __npy_i; \ - _PyAIT(it)->index = 0; \ - _PyAIT(it)->dataptr = _PyAIT(it)->ao->data; \ - for (__npy_i = _PyAIT(it)->nd_m1; __npy_i>=0; __npy_i--) { \ - if (destination[__npy_i] < 0) { \ - destination[__npy_i] += \ - _PyAIT(it)->dims_m1[__npy_i]+1; \ - } \ - _PyAIT(it)->dataptr += destination[__npy_i] * \ - _PyAIT(it)->strides[__npy_i]; \ - _PyAIT(it)->coordinates[__npy_i] = \ - destination[__npy_i]; \ - _PyAIT(it)->index += destination[__npy_i] * \ - ( __npy_i==_PyAIT(it)->nd_m1 ? 1 : \ - _PyAIT(it)->dims_m1[__npy_i+1]+1) ; \ - } \ +#define PyArray_ITER_GOTO(it, destination) { \ + int __npy_i; \ + _PyAIT(it)->index = 0; \ + _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \ + for (__npy_i = _PyAIT(it)->nd_m1; __npy_i>=0; __npy_i--) { \ + if (destination[__npy_i] < 0) { \ + destination[__npy_i] += \ + _PyAIT(it)->dims_m1[__npy_i]+1; \ + } \ + _PyAIT(it)->dataptr += destination[__npy_i] * \ + _PyAIT(it)->strides[__npy_i]; \ + _PyAIT(it)->coordinates[__npy_i] = \ + destination[__npy_i]; \ + _PyAIT(it)->index += destination[__npy_i] * \ + ( __npy_i==_PyAIT(it)->nd_m1 ? 1 : \ + _PyAIT(it)->dims_m1[__npy_i+1]+1) ; \ + } \ } -#define PyArray_ITER_GOTO1D(it, ind) { \ - int __npy_i; \ - npy_intp __npy_ind = (npy_intp) (ind); \ - if (__npy_ind < 0) __npy_ind += _PyAIT(it)->size; \ - _PyAIT(it)->index = __npy_ind; \ - if (_PyAIT(it)->nd_m1 == 0) { \ - _PyAIT(it)->dataptr = _PyAIT(it)->ao->data + \ - __npy_ind * _PyAIT(it)->strides[0]; \ - } \ - else if (_PyAIT(it)->contiguous) \ - _PyAIT(it)->dataptr = _PyAIT(it)->ao->data + \ - __npy_ind * _PyAIT(it)->ao->descr->elsize; \ - else { \ - _PyAIT(it)->dataptr = _PyAIT(it)->ao->data; \ - for (__npy_i = 0; __npy_i<=_PyAIT(it)->nd_m1; \ - __npy_i++) { \ - _PyAIT(it)->dataptr += \ +#define PyArray_ITER_GOTO1D(it, ind) { \ + int __npy_i; \ + npy_intp __npy_ind = (npy_intp) (ind); \ + if (__npy_ind < 0) __npy_ind += _PyAIT(it)->size; \ + _PyAIT(it)->index = __npy_ind; \ + if (_PyAIT(it)->nd_m1 == 0) { \ + _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao) + \ + __npy_ind * _PyAIT(it)->strides[0]; \ + } \ + else if (_PyAIT(it)->contiguous) \ + _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao) + \ + __npy_ind * PyArray_DESCR(_PyAIT(it)->ao)->elsize; \ + else { \ + _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \ + for (__npy_i = 0; __npy_i<=_PyAIT(it)->nd_m1; \ + __npy_i++) { \ + _PyAIT(it)->dataptr += \ (__npy_ind / _PyAIT(it)->factors[__npy_i]) \ - * _PyAIT(it)->strides[__npy_i]; \ - __npy_ind %= _PyAIT(it)->factors[__npy_i]; \ - } \ - } \ + * _PyAIT(it)->strides[__npy_i]; \ + __npy_ind %= _PyAIT(it)->factors[__npy_i]; \ + } \ + } \ } #define PyArray_ITER_DATA(it) ((void *)(_PyAIT(it)->dataptr)) @@ -1238,12 +1257,17 @@ PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter); #define NPY_DEFAULT_TYPE NPY_DOUBLE /* - * All sorts of useful ways to look into a PyArrayObject. These are - * the recommended over casting to PyArrayObject and accessing the - * members directly. + * All sorts of useful ways to look into a PyArrayObject. It is recommended + * to use PyArrayObject * objects instead of always casting from PyObject *, + * for improved type checking. + * + * In many cases here the macro versions of the accessors are deprecated, + * but can't be immediately changed to inline functions because the + * preexisting macros accept PyObject * and do automatic casts. Inline + * functions accepting PyArrayObject * provides for some compile-time + * checking of correctness when working with these objects in C. */ -#define PyArray_NDIM(obj) (((PyArrayObject *)(obj))->nd) #define PyArray_ISONESEGMENT(m) (PyArray_NDIM(m) == 0 || \ PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) || \ PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS)) @@ -1255,27 +1279,154 @@ PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter); NPY_ARRAY_F_CONTIGUOUS : 0)) #define FORTRAN_IF PyArray_FORTRAN_IF -#define PyArray_DATA(obj) ((void *)(((PyArrayObject *)(obj))->data)) -#define PyArray_BYTES(obj) (((PyArrayObject *)(obj))->data) -#define PyArray_DIMS(obj) (((PyArrayObject *)(obj))->dimensions) -#define PyArray_STRIDES(obj) (((PyArrayObject *)(obj))->strides) + +#ifdef NPY_NO_DEPRECATED_API +/* + * Changing access macros into functions, to allow for future hiding + * of the internal memory layout. This later hiding will allow the 2.x series + * to change the internal representation of arrays without affecting + * ABI compatibility. + */ + +static NPY_INLINE int +PyArray_NDIM(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->nd; +} + +static NPY_INLINE char * +PyArray_DATA(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->data; +} + +static NPY_INLINE npy_intp * +PyArray_DIMS(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->dimensions; +} + +static NPY_INLINE npy_intp * +PyArray_STRIDES(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->strides; +} + +static NPY_INLINE npy_intp +PyArray_DIM(PyArrayObject *arr, int idim) +{ + return ((PyArrayObject_fieldaccess *)arr)->dimensions[idim]; +} + +static NPY_INLINE npy_intp +PyArray_STRIDE(PyArrayObject *arr, int istride) +{ + return ((PyArrayObject_fieldaccess *)arr)->strides[istride]; +} + +static NPY_INLINE PyObject * +PyArray_BASE(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->base; +} + +static NPY_INLINE PyArray_Descr * +PyArray_DESCR(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->descr; +} + +static NPY_INLINE int +PyArray_FLAGS(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->flags; +} + +static NPY_INLINE npy_intp +PyArray_ITEMSIZE(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->descr->elsize; +} + +static NPY_INLINE int +PyArray_TYPE(PyArrayObject *arr) +{ + return ((PyArrayObject_fieldaccess *)arr)->descr->type_num; +} + +static NPY_INLINE int +PyArray_CHKFLAGS(PyArrayObject *arr, int flags) +{ + return (PyArray_FLAGS(arr) & flags) == flags; +} + +static NPY_INLINE PyObject * +PyArray_GETITEM(PyArrayObject *arr, char *itemptr) +{ + return ((PyArrayObject_fieldaccess *)arr)->descr->f->getitem( + itemptr, + arr); +} + +static NPY_INLINE int +PyArray_SETITEM(PyArrayObject *arr, char *itemptr, PyObject *v) +{ + return ((PyArrayObject_fieldaccess *)arr)->descr->f->setitem( + v, + itemptr, + arr); +} + +#else + +/* Macros are deprecated as of NumPy 1.7. */ +#define PyArray_NDIM(obj) (((PyArrayObject_fieldaccess *)(obj))->nd) +#define PyArray_DATA(obj) ((void *)(((PyArrayObject_fieldaccess *)(obj))->data)) +#define PyArray_DIMS(obj) (((PyArrayObject_fieldaccess *)(obj))->dimensions) +#define PyArray_STRIDES(obj) (((PyArrayObject_fieldaccess *)(obj))->strides) #define PyArray_DIM(obj,n) (PyArray_DIMS(obj)[n]) #define PyArray_STRIDE(obj,n) (PyArray_STRIDES(obj)[n]) -#define PyArray_BASE(obj) (((PyArrayObject *)(obj))->base) -#define PyArray_DESCR(obj) (((PyArrayObject *)(obj))->descr) -#define PyArray_FLAGS(obj) (((PyArrayObject *)(obj))->flags) -#define PyArray_ITEMSIZE(obj) (((PyArrayObject *)(obj))->descr->elsize) -#define PyArray_TYPE(obj) (((PyArrayObject *)(obj))->descr->type_num) +#define PyArray_BASE(obj) (((PyArrayObject_fieldaccess *)(obj))->base) +#define PyArray_DESCR(obj) (((PyArrayObject_fieldaccess *)(obj))->descr) +#define PyArray_FLAGS(obj) (((PyArrayObject_fieldaccess *)(obj))->flags) +#define PyArray_CHKFLAGS(m, FLAGS) \ + ((((PyArrayObject_fieldaccess *)(m))->flags & (FLAGS)) == (FLAGS)) +#define PyArray_ITEMSIZE(obj) \ + (((PyArrayObject_fieldaccess *)(obj))->descr->elsize) +#define PyArray_TYPE(obj) \ + (((PyArrayObject_fieldaccess *)(obj))->descr->type_num) +#define PyArray_GETITEM(obj,itemptr) \ + PyArray_DESCR(obj)->f->getitem((char *)(itemptr), \ + (PyArrayObject *)(obj)) + +#define PyArray_SETITEM(obj,itemptr,v) \ + PyArray_DESCR(obj)->f->setitem((PyObject *)(v), \ + (char *)(itemptr), \ + (PyArrayObject *)(obj)) +#endif -#define PyArray_GETITEM(obj,itemptr) \ - ((PyArrayObject *)(obj))->descr->f->getitem((char *)(itemptr), \ - (PyArrayObject *)(obj)) +/* Same as PyArray_DATA */ +#define PyArray_BYTES(arr) PyArray_DATA(arr) -#define PyArray_SETITEM(obj,itemptr,v) \ - ((PyArrayObject *)(obj))->descr->f->setitem((PyObject *)(v), \ - (char *)(itemptr), \ - (PyArrayObject *)(obj)) +/* + * Enables the specified array flags. Does no checking, + * assumes you know what you're doing. + */ +static NPY_INLINE void +PyArray_ENABLEFLAGS(PyArrayObject *arr, int flags) +{ + ((PyArrayObject_fieldaccess *)arr)->flags |= flags; +} +/* + * Clears the specified array flags. Does no checking, + * assumes you know what you're doing. + */ +static NPY_INLINE void +PyArray_CLEARFLAGS(PyArrayObject *arr, int flags) +{ + ((PyArrayObject_fieldaccess *)arr)->flags &= ~flags; +} #define PyTypeNum_ISBOOL(type) ((type) == NPY_BOOL) diff --git a/numpy/core/include/numpy/npy_common.h b/numpy/core/include/numpy/npy_common.h index ad326da68..118850541 100644 --- a/numpy/core/include/numpy/npy_common.h +++ b/numpy/core/include/numpy/npy_common.h @@ -135,7 +135,7 @@ typedef unsigned PY_LONG_LONG npy_ulonglong; /* "%Ld" only parses 4 bytes -- "L" is floating modifier on MacOS X/BSD */ # define NPY_LONGLONG_FMT "lld" # define NPY_ULONGLONG_FMT "llu" -/* +/* another possible variant -- *quad_t works on *BSD, but is deprecated: #define LONGLONG_FMT "qd" #define ULONGLONG_FMT "qu" diff --git a/numpy/core/include/numpy/numpyconfig.h b/numpy/core/include/numpy/numpyconfig.h index ff7938cd9..19685b3dc 100644 --- a/numpy/core/include/numpy/numpyconfig.h +++ b/numpy/core/include/numpy/numpyconfig.h @@ -3,7 +3,7 @@ #include "_numpyconfig.h" -/* +/* * On Mac OS X, because there is only one configuration stage for all the archs * in universal builds, any macro which depends on the arch needs to be * harcoded diff --git a/numpy/core/include/numpy/ufuncobject.h b/numpy/core/include/numpy/ufuncobject.h index c7096371d..d00fe10ea 100644 --- a/numpy/core/include/numpy/ufuncobject.h +++ b/numpy/core/include/numpy/ufuncobject.h @@ -108,14 +108,14 @@ typedef struct _tagPyUFuncObject { void *ptr; PyObject *obj; PyObject *userloops; - + /* generalized ufunc parameters */ /* 0 for scalar ufunc; 1 for generalized ufunc */ int core_enabled; /* number of distinct dimension names in signature */ int core_num_dim_ix; - + /* * dimension indices of input/output argument k are stored in * core_dim_ixs[core_offsets[k]..core_offsets[k]+core_num_dims[k]-1] diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 5a7aaba1c..1ab9f87fc 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -26,6 +26,15 @@ class memmap(ndarray): memmap's are array-like objects. This differs from Python's ``mmap`` module, which uses file-like objects. + This subclass of ndarray has some unpleasant interactions with + some operations, because it doesn't quite fit properly as a subclass. + An alternative to using this subclass is to create the ``mmap`` + object yourself, then create an ndarray with ndarray.__new__ directly, + passing the object created in its 'buffer=' parameter. + + This class may at some point be turned into a factory function + which returns a view into an mmap buffer. + Parameters ---------- filename : str or file-like object @@ -275,27 +284,5 @@ class memmap(ndarray): memmap """ - if self._mmap is not None: - self._mmap.flush() - - def _close(self): - """Close the memmap file. Only do this when deleting the object.""" - if self.base is self._mmap: - # The python mmap probably causes flush on close, but - # we put this here for safety - self._mmap.flush() - self._mmap.close() - self._mmap = None - - def __del__(self): - # We first check if we are the owner of the mmap, rather than - # a view, so deleting a view does not call _close - # on the parent mmap - if self._mmap is self.base: - try: - # First run tell() to see whether file is open - self._mmap.tell() - except ValueError: - pass - else: - self._close() + if self.base is not None and hasattr(self.base, 'flush'): + self.base.flush() diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index a1dc4399a..cd2060133 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -65,6 +65,57 @@ PyArray_Size(PyObject *op) } } +/*NUMPY_API + * Sets the 'base' attribute of the array. This steals a reference + * to 'obj'. + * + * Returns 0 on success, -1 on failure. + */ +NPY_NO_EXPORT int +PyArray_SetBaseObject(PyArrayObject *arr, PyObject *obj) +{ + if (obj == NULL) { + PyErr_SetString(PyExc_ValueError, + "Cannot set the NumPy array 'base' " + "dependency to NULL after initialization"); + return -1; + } + /* + * Allow the base to be set only once. Once the object which + * owns the data is set, it doesn't make sense to change it. + */ + if (PyArray_BASE(arr) != NULL) { + Py_DECREF(obj); + PyErr_SetString(PyExc_ValueError, + "Cannot set the NumPy array 'base' " + "dependency more than once"); + return -1; + } + /* + * Don't allow chains of views, always set the base + * to the owner of the data + */ + while (PyArray_Check(obj) && + (PyObject *)arr != obj && + PyArray_BASE((PyArrayObject *)obj) != NULL) { + PyObject *tmp = PyArray_BASE((PyArrayObject *)obj); + Py_INCREF(tmp); + Py_DECREF(obj); + obj = tmp; + } + /* Disallow circular references */ + if ((PyObject *)arr == obj) { + Py_DECREF(obj); + PyErr_SetString(PyExc_ValueError, + "Cannot create a circular NumPy array 'base' dependency"); + return -1; + } + ((PyArrayObject_fieldaccess *)arr)->base = obj; + + return 0; +} + + /*NUMPY_API*/ NPY_NO_EXPORT int PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object) @@ -80,13 +131,14 @@ PyArray_CopyObject(PyArrayObject *dest, PyObject *src_object) * Special code to mimic Numeric behavior for * character arrays. */ - if (dest->descr->type == PyArray_CHARLTR && dest->nd > 0 \ - && PyString_Check(src_object)) { + if (PyArray_DESCR(dest)->type == PyArray_CHARLTR && + PyArray_NDIM(dest) > 0 && + PyString_Check(src_object)) { npy_intp n_new, n_old; char *new_string; PyObject *tmp; - n_new = dest->dimensions[dest->nd-1]; + n_new = PyArray_DIMS(dest)[PyArray_NDIM(dest)-1]; n_old = PyString_Size(src_object); if (n_new > n_old) { new_string = (char *)malloc(n_new); @@ -220,26 +272,29 @@ PyArray_TypeNumFromName(char *str) /* array object functions */ static void -array_dealloc(PyArrayObject *self) { +array_dealloc(PyArrayObject *self) +{ + PyArrayObject_fieldaccess *fa = (PyArrayObject_fieldaccess *)self; _array_dealloc_buffer_info(self); - if (self->weakreflist != NULL) { + if (fa->weakreflist != NULL) { PyObject_ClearWeakRefs((PyObject *)self); } - if (self->base) { + if (fa->base) { /* * UPDATEIFCOPY means that base points to an * array that should be updated with the contents * of this array upon destruction. - * self->base->flags must have been WRITEABLE + * fa->base->flags must have been WRITEABLE * (checked previously) and it was locked here * thus, unlock it. */ - if (self->flags & NPY_ARRAY_UPDATEIFCOPY) { - ((PyArrayObject *)self->base)->flags |= NPY_ARRAY_WRITEABLE; + if (fa->flags & NPY_ARRAY_UPDATEIFCOPY) { + PyArray_ENABLEFLAGS(((PyArrayObject *)fa->base), + NPY_ARRAY_WRITEABLE); Py_INCREF(self); /* hold on to self in next call */ - if (PyArray_CopyAnyInto((PyArrayObject *)self->base, self) < 0) { + if (PyArray_CopyAnyInto((PyArrayObject *)fa->base, self) < 0) { PyErr_Print(); PyErr_Clear(); } @@ -252,12 +307,12 @@ array_dealloc(PyArrayObject *self) { * In any case base is pointing to something that we need * to DECREF -- either a view or a buffer object */ - Py_DECREF(self->base); + Py_DECREF(fa->base); } - if ((self->flags & NPY_ARRAY_OWNDATA) && self->data) { + if ((fa->flags & NPY_ARRAY_OWNDATA) && fa->data) { /* Free internal references if an Object array */ - if (PyDataType_FLAGCHK(self->descr, NPY_ITEM_REFCOUNT)) { + if (PyDataType_FLAGCHK(fa->descr, NPY_ITEM_REFCOUNT)) { Py_INCREF(self); /*hold on to self */ PyArray_XDECREF(self); /* @@ -265,11 +320,11 @@ array_dealloc(PyArrayObject *self) { * self already... */ } - PyDataMem_FREE(self->data); + //PyDataMem_FREE(fa->data); } - PyDimMem_FREE(self->dimensions); - Py_DECREF(self->descr); + PyDimMem_FREE(fa->dimensions); + Py_DECREF(fa->descr); Py_TYPE(self)->tp_free((PyObject *)self); } @@ -277,7 +332,7 @@ static int dump_data(char **string, int *n, int *max_n, char *data, int nd, npy_intp *dimensions, npy_intp *strides, PyArrayObject* self) { - PyArray_Descr *descr=self->descr; + PyArray_Descr *descr=PyArray_DESCR(self); PyObject *op, *sp; char *ostring; npy_intp i, N; @@ -352,9 +407,9 @@ array_repr_builtin(PyArrayObject *self, int repr) else { n = 0; } - if (dump_data(&string, &n, &max_n, self->data, - self->nd, self->dimensions, - self->strides, self) < 0) { + if (dump_data(&string, &n, &max_n, PyArray_DATA(self), + PyArray_NDIM(self), PyArray_DIMS(self), + PyArray_STRIDES(self), self) < 0) { _pya_free(string); return NULL; } @@ -362,12 +417,12 @@ array_repr_builtin(PyArrayObject *self, int repr) if (repr) { if (PyArray_ISEXTENDED(self)) { char buf[100]; - PyOS_snprintf(buf, sizeof(buf), "%d", self->descr->elsize); - sprintf(string+n, ", '%c%s')", self->descr->type, buf); + PyOS_snprintf(buf, sizeof(buf), "%d", PyArray_DESCR(self)->elsize); + sprintf(string+n, ", '%c%s')", PyArray_DESCR(self)->type, buf); ret = PyUString_FromStringAndSize(string, n + 6 + strlen(buf)); } else { - sprintf(string+n, ", '%c')", self->descr->type); + sprintf(string+n, ", '%c')", PyArray_DESCR(self)->type); ret = PyUString_FromStringAndSize(string, n+6); } } @@ -702,11 +757,8 @@ _uni_release(char *ptr, int nc) } \ } -#define _loop(CMP) if (rstrip) _rstrip_loop(CMP) \ - else _reg_loop(CMP) - static int -_compare_strings(PyObject *result, PyArrayMultiIterObject *multi, +_compare_strings(PyArrayObject *result, PyArrayMultiIterObject *multi, int cmp_op, void *func, int rstrip) { PyArrayIterObject *iself, *iother; @@ -723,8 +775,8 @@ _compare_strings(PyObject *result, PyArrayMultiIterObject *multi, iself = multi->iters[0]; iother = multi->iters[1]; size = multi->size; - N1 = iself->ao->descr->elsize; - N2 = iother->ao->descr->elsize; + N1 = PyArray_DESCR(iself->ao)->elsize; + N2 = PyArray_DESCR(iother->ao)->elsize; if ((void *)compfunc == (void *)_myunincmp) { N1 >>= 2; N2 >>= 2; @@ -737,23 +789,47 @@ _compare_strings(PyObject *result, PyArrayMultiIterObject *multi, } switch (cmp_op) { case Py_EQ: - _loop(==) - break; + if (rstrip) { + _rstrip_loop(==); + } else { + _reg_loop(==); + } + break; case Py_NE: - _loop(!=) - break; + if (rstrip) { + _rstrip_loop(!=); + } else { + _reg_loop(!=); + } + break; case Py_LT: - _loop(<) - break; + if (rstrip) { + _rstrip_loop(<); + } else { + _reg_loop(<); + } + break; case Py_LE: - _loop(<=) - break; + if (rstrip) { + _rstrip_loop(<=); + } else { + _reg_loop(<=); + } + break; case Py_GT: - _loop(>) - break; + if (rstrip) { + _rstrip_loop(>); + } else { + _reg_loop(>); + } + break; case Py_GE: - _loop(>=) - break; + if (rstrip) { + _rstrip_loop(>=); + } else { + _reg_loop(>=); + } + break; default: PyErr_SetString(PyExc_RuntimeError, "bad comparison operator"); return -1; @@ -761,7 +837,6 @@ _compare_strings(PyObject *result, PyArrayMultiIterObject *multi, return 0; } -#undef _loop #undef _reg_loop #undef _rstrip_loop #undef SMALL_STRING @@ -770,26 +845,25 @@ NPY_NO_EXPORT PyObject * _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, int rstrip) { - PyObject *result; + PyArrayObject *result; PyArrayMultiIterObject *mit; int val; /* Cast arrays to a common type */ - if (self->descr->type_num != other->descr->type_num) { + if (PyArray_DESCR(self)->type_num != PyArray_DESCR(other)->type_num) { #if defined(NPY_PY3K) /* * Comparison between Bytes and Unicode is not defined in Py3K; * we follow. */ - result = Py_NotImplemented; - Py_INCREF(result); - return result; + Py_INCREF(Py_NotImplemented); + return Py_NotImplemented; #else PyObject *new; - if (self->descr->type_num == PyArray_STRING && - other->descr->type_num == PyArray_UNICODE) { - PyArray_Descr* unicode = PyArray_DescrNew(other->descr); - unicode->elsize = self->descr->elsize << 2; + if (PyArray_DESCR(self)->type_num == PyArray_STRING && + PyArray_DESCR(other)->type_num == PyArray_UNICODE) { + PyArray_Descr* unicode = PyArray_DescrNew(PyArray_DESCR(other)); + unicode->elsize = PyArray_DESCR(self)->elsize << 2; new = PyArray_FromAny((PyObject *)self, unicode, 0, 0, 0, NULL); if (new == NULL) { @@ -798,10 +872,10 @@ _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, Py_INCREF(other); self = (PyArrayObject *)new; } - else if (self->descr->type_num == PyArray_UNICODE && - other->descr->type_num == PyArray_STRING) { - PyArray_Descr* unicode = PyArray_DescrNew(self->descr); - unicode->elsize = other->descr->elsize << 2; + else if (PyArray_DESCR(self)->type_num == PyArray_UNICODE && + PyArray_DESCR(other)->type_num == PyArray_STRING) { + PyArray_Descr* unicode = PyArray_DescrNew(PyArray_DESCR(self)); + unicode->elsize = PyArray_DESCR(other)->elsize << 2; new = PyArray_FromAny((PyObject *)other, unicode, 0, 0, 0, NULL); if (new == NULL) { @@ -831,7 +905,7 @@ _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, return NULL; } - result = PyArray_NewFromDescr(&PyArray_Type, + result = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, PyArray_DescrFromType(PyArray_BOOL), mit->nd, mit->dimensions, @@ -841,7 +915,7 @@ _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, goto finish; } - if (self->descr->type_num == PyArray_UNICODE) { + if (PyArray_DESCR(self)->type_num == NPY_UNICODE) { val = _compare_strings(result, mit, cmp_op, _myunincmp, rstrip); } else { @@ -849,12 +923,13 @@ _strings_richcompare(PyArrayObject *self, PyArrayObject *other, int cmp_op, } if (val < 0) { - Py_DECREF(result); result = NULL; + Py_DECREF(result); + result = NULL; } finish: Py_DECREF(mit); - return result; + return (PyObject *)result; } /* @@ -884,7 +959,7 @@ _void_compare(PyArrayObject *self, PyArrayObject *other, int cmp_op) PyArray_NDIM(self) : PyArray_NDIM(other); op = (cmp_op == Py_EQ ? n_ops.logical_and : n_ops.logical_or); - while (PyDict_Next(self->descr->fields, &pos, &key, &value)) { + while (PyDict_Next(PyArray_DESCR(self)->fields, &pos, &key, &value)) { if NPY_TITLE_KEY(key, value) { continue; } @@ -912,16 +987,17 @@ _void_compare(PyArrayObject *self, PyArrayObject *other, int cmp_op) * dimensions will have been appended to `a` and `b`. * In that case, reduce them using `op`. */ - if (PyArray_Check(temp) && PyArray_NDIM(temp) > result_ndim) { + if (PyArray_Check(temp) && + PyArray_NDIM((PyArrayObject *)temp) > result_ndim) { /* If the type was multidimensional, collapse that part to 1-D */ - if (PyArray_NDIM(temp) != result_ndim+1) { + if (PyArray_NDIM((PyArrayObject *)temp) != result_ndim+1) { npy_intp dimensions[NPY_MAXDIMS]; PyArray_Dims newdims; newdims.ptr = dimensions; newdims.len = result_ndim+1; - memcpy(dimensions, PyArray_DIMS(temp), + memcpy(dimensions, PyArray_DIMS((PyArrayObject *)temp), sizeof(intp)*result_ndim); dimensions[result_ndim] = -1; temp2 = PyArray_Newshape((PyArrayObject *)temp, @@ -977,7 +1053,8 @@ _void_compare(PyArrayObject *self, PyArrayObject *other, int cmp_op) NPY_NO_EXPORT PyObject * array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) { - PyObject *array_other, *result = NULL; + PyArrayObject *array_other; + PyObject *result = NULL; int typenum; switch (cmp_op) { @@ -994,40 +1071,39 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) Py_INCREF(Py_False); return Py_False; } - /* Try to convert other to an array */ - if (!PyArray_Check(other)) { - typenum = self->descr->type_num; + /* Make sure 'other' is an array */ + if (PyArray_Check(other)) { + Py_INCREF(other); + array_other = (PyArrayObject *)other; + } + else { + typenum = PyArray_DESCR(self)->type_num; if (typenum != PyArray_OBJECT) { typenum = PyArray_NOTYPE; } - array_other = PyArray_FromObject(other, typenum, 0, 0); + array_other = (PyArrayObject *)PyArray_FromObject(other, + typenum, 0, 0); /* * If not successful, indicate that the items cannot be compared * this way. */ - if ((array_other == NULL) || - (array_other == Py_None)) { + if (array_other == NULL) { Py_XDECREF(array_other); PyErr_Clear(); Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } } - else { - Py_INCREF(other); - array_other = other; - } result = PyArray_GenericBinaryFunction(self, - array_other, + (PyObject *)array_other, n_ops.equal); if ((result == Py_NotImplemented) && - (self->descr->type_num == PyArray_VOID)) { + (PyArray_DESCR(self)->type_num == PyArray_VOID)) { int _res; _res = PyObject_RichCompareBool - ((PyObject *)self->descr, - (PyObject *)\ - PyArray_DESCR(array_other), + ((PyObject *)PyArray_DESCR(self), + (PyObject *)PyArray_DESCR(array_other), Py_EQ); if (_res < 0) { Py_DECREF(result); @@ -1036,10 +1112,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) } if (_res) { Py_DECREF(result); - result = _void_compare - (self, - (PyArrayObject *)array_other, - cmp_op); + result = _void_compare(self, array_other, cmp_op); Py_DECREF(array_other); } return result; @@ -1061,39 +1134,38 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) Py_INCREF(Py_True); return Py_True; } - /* Try to convert other to an array */ - if (!PyArray_Check(other)) { - typenum = self->descr->type_num; + /* Make sure 'other' is an array */ + if (PyArray_Check(other)) { + Py_INCREF(other); + array_other = (PyArrayObject *)other; + } + else { + typenum = PyArray_DESCR(self)->type_num; if (typenum != PyArray_OBJECT) { typenum = PyArray_NOTYPE; } - array_other = PyArray_FromObject(other, typenum, 0, 0); + array_other = (PyArrayObject *)PyArray_FromObject(other, + typenum, 0, 0); /* * If not successful, then objects cannot be * compared this way */ - if ((array_other == NULL) || (array_other == Py_None)) { + if (array_other == NULL || (PyObject *)array_other == Py_None) { Py_XDECREF(array_other); PyErr_Clear(); Py_INCREF(Py_NotImplemented); return Py_NotImplemented; } } - else { - Py_INCREF(other); - array_other = other; - } - result = PyArray_GenericBinaryFunction(self, - array_other, + result = PyArray_GenericBinaryFunction(self, (PyObject *)array_other, n_ops.not_equal); if ((result == Py_NotImplemented) && - (self->descr->type_num == PyArray_VOID)) { + (PyArray_DESCR(self)->type_num == PyArray_VOID)) { int _res; _res = PyObject_RichCompareBool( - (PyObject *)self->descr, - (PyObject *) - PyArray_DESCR(array_other), + (PyObject *)PyArray_DESCR(self), + (PyObject *)PyArray_DESCR(array_other), Py_EQ); if (_res < 0) { Py_DECREF(result); @@ -1102,10 +1174,7 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) } if (_res) { Py_DECREF(result); - result = _void_compare( - self, - (PyArrayObject *)array_other, - cmp_op); + result = _void_compare(self, array_other, cmp_op); Py_DECREF(array_other); } return result; @@ -1132,10 +1201,11 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) } if (result == Py_NotImplemented) { /* Try to handle string comparisons */ - if (self->descr->type_num == PyArray_OBJECT) { + if (PyArray_DESCR(self)->type_num == PyArray_OBJECT) { return result; } - array_other = PyArray_FromObject(other,PyArray_NOTYPE, 0, 0); + array_other = (PyArrayObject *)PyArray_FromObject(other, + PyArray_NOTYPE, 0, 0); if (PyArray_ISSTRING(self) && PyArray_ISSTRING(array_other)) { Py_DECREF(result); result = _strings_richcompare(self, (PyArrayObject *) @@ -1149,13 +1219,24 @@ array_richcompare(PyArrayObject *self, PyObject *other, int cmp_op) /*NUMPY_API */ NPY_NO_EXPORT int -PyArray_ElementStrides(PyObject *arr) +PyArray_ElementStrides(PyObject *obj) { - int itemsize = PyArray_ITEMSIZE(arr); - int i, N = PyArray_NDIM(arr); - npy_intp *strides = PyArray_STRIDES(arr); + PyArrayObject *arr; + int itemsize; + int i, ndim; + npy_intp *strides; + + if (!PyArray_Check(obj)) { + return 0; + } - for (i = 0; i < N; i++) { + arr = (PyArrayObject *)obj; + + itemsize = PyArray_ITEMSIZE(arr); + ndim = PyArray_NDIM(arr); + strides = PyArray_STRIDES(arr); + + for (i = 0; i < ndim; i++) { if ((strides[i] % itemsize) != 0) { return 0; } @@ -1332,8 +1413,12 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) goto fail; } PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); - ret->base = buffer.base; Py_INCREF(buffer.base); + if (PyArray_SetBaseObject(ret, buffer.base) < 0) { + Py_DECREF(ret); + ret = NULL; + goto fail; + } } PyDimMem_FREE(dims.ptr); @@ -1357,7 +1442,7 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) static PyObject * array_iter(PyArrayObject *arr) { - if (arr->nd == 0) { + if (PyArray_NDIM(arr) == 0) { PyErr_SetString(PyExc_TypeError, "iteration over a 0-d array"); return NULL; @@ -1370,7 +1455,7 @@ array_alloc(PyTypeObject *type, Py_ssize_t NPY_UNUSED(nitems)) { PyObject *obj; /* nitems will always be 0 */ - obj = (PyObject *)_pya_malloc(sizeof(PyArrayObject)); + obj = (PyObject *)_pya_malloc(NPY_SIZEOF_PYARRAYOBJECT); PyObject_Init(obj, type); return obj; } @@ -1384,7 +1469,7 @@ NPY_NO_EXPORT PyTypeObject PyArray_Type = { 0, /* ob_size */ #endif "numpy.ndarray", /* tp_name */ - sizeof(PyArrayObject), /* tp_basicsize */ + NPY_SIZEOF_PYARRAYOBJECT, /* tp_basicsize */ 0, /* tp_itemsize */ /* methods */ (destructor)array_dealloc, /* tp_dealloc */ @@ -1419,7 +1504,7 @@ NPY_NO_EXPORT PyTypeObject PyArray_Type = { (traverseproc)0, /* tp_traverse */ (inquiry)0, /* tp_clear */ (richcmpfunc)array_richcompare, /* tp_richcompare */ - offsetof(PyArrayObject, weakreflist), /* tp_weaklistoffset */ + offsetof(PyArrayObject_fieldaccess, weakreflist), /* tp_weaklistoffset */ (getiterfunc)array_iter, /* tp_iter */ (iternextfunc)0, /* tp_iternext */ array_methods, /* tp_methods */ diff --git a/numpy/core/src/multiarray/arraytypes.c.src b/numpy/core/src/multiarray/arraytypes.c.src index f8362b27f..cb76d5af9 100644 --- a/numpy/core/src/multiarray/arraytypes.c.src +++ b/numpy/core/src/multiarray/arraytypes.c.src @@ -130,7 +130,7 @@ static PyObject * return @func1@((@type1@)t1); } else { - ap->descr->f->copyswap(&t1, ip, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(&t1, ip, !PyArray_ISNOTSWAPPED(ap), ap); return @func1@((@type1@)t1); } } @@ -157,7 +157,7 @@ static int if (ap == NULL || PyArray_ISBEHAVED(ap)) *((@type@ *)ov)=temp; else { - ap->descr->f->copyswap(ov, &temp, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(ov, &temp, !PyArray_ISNOTSWAPPED(ap), ap); } return 0; } @@ -205,9 +205,10 @@ static int int rsize; if (!(PyArray_IsScalar(op, @kind@))) { - if (PyArray_Check(op) && (PyArray_NDIM(op) == 0)) { - op2 = ((PyArrayObject *)op)->descr->f->getitem - (((PyArrayObject *)op)->data, (PyArrayObject *)op); + if (PyArray_Check(op) && (PyArray_NDIM((PyArrayObject *)op) == 0)) { + op2 = PyArray_DESCR((PyArrayObject *)op)->f->getitem( + PyArray_BYTES((PyArrayObject *)op), + (PyArrayObject *)op); } else { op2 = op; Py_INCREF(op); @@ -229,7 +230,7 @@ static int else { temp = ((Py@kind@ScalarObject *)op)->obval; } - memcpy(ov, &temp, ap->descr->elsize); + memcpy(ov, &temp, PyArray_DESCR(ap)->elsize); if (!PyArray_ISNOTSWAPPED(ap)) { byte_swap_vector(ov, 2, sizeof(@type@)); } @@ -247,7 +248,7 @@ static int static PyObject * LONGDOUBLE_getitem(char *ip, PyArrayObject *ap) { - return PyArray_Scalar(ip, ap->descr, NULL); + return PyArray_Scalar(ip, PyArray_DESCR(ap), NULL); } static int @@ -268,7 +269,7 @@ LONGDOUBLE_setitem(PyObject *op, char *ov, PyArrayObject *ap) { *((longdouble *)ov) = temp; } else { - copy_and_swap(ov, &temp, ap->descr->elsize, 1, 0, + copy_and_swap(ov, &temp, PyArray_DESCR(ap)->elsize, 1, 0, !PyArray_ISNOTSWAPPED(ap)); } return 0; @@ -277,14 +278,14 @@ LONGDOUBLE_setitem(PyObject *op, char *ov, PyArrayObject *ap) { static PyObject * CLONGDOUBLE_getitem(char *ip, PyArrayObject *ap) { - return PyArray_Scalar(ip, ap->descr, NULL); + return PyArray_Scalar(ip, PyArray_DESCR(ap), NULL); } /* UNICODE */ static PyObject * UNICODE_getitem(char *ip, PyArrayObject *ap) { - intp elsize = ap->descr->elsize; + intp elsize = PyArray_DESCR(ap)->elsize; intp mysize = elsize/sizeof(PyArray_UCS4); int alloc = 0; PyArray_UCS4 *buffer = NULL; @@ -378,10 +379,10 @@ UNICODE_setitem(PyObject *op, char *ov, PyArrayObject *ap) datalen = PyUnicode_GET_DATA_SIZE(temp); #ifdef Py_UNICODE_WIDE - memcpy(ov, ptr, MIN(ap->descr->elsize, datalen)); + memcpy(ov, ptr, MIN(PyArray_DESCR(ap)->elsize, datalen)); #else if (!PyArray_ISALIGNED(ap)) { - buffer = PyArray_malloc(ap->descr->elsize); + buffer = PyArray_malloc(PyArray_DESCR(ap)->elsize); if (buffer == NULL) { Py_DECREF(temp); PyErr_NoMemory(); @@ -392,7 +393,7 @@ UNICODE_setitem(PyObject *op, char *ov, PyArrayObject *ap) buffer = ov; } datalen = PyUCS2Buffer_AsUCS4(ptr, (PyArray_UCS4 *)buffer, - datalen >> 1, ap->descr->elsize >> 2); + datalen >> 1, PyArray_DESCR(ap)->elsize >> 2); datalen <<= 2; if (!PyArray_ISALIGNED(ap)) { memcpy(ov, buffer, datalen); @@ -400,11 +401,11 @@ UNICODE_setitem(PyObject *op, char *ov, PyArrayObject *ap) } #endif /* Fill in the rest of the space with 0 */ - if (ap->descr->elsize > datalen) { - memset(ov + datalen, 0, (ap->descr->elsize - datalen)); + if (PyArray_DESCR(ap)->elsize > datalen) { + memset(ov + datalen, 0, (PyArray_DESCR(ap)->elsize - datalen)); } if (!PyArray_ISNOTSWAPPED(ap)) { - byte_swap_vector(ov, ap->descr->elsize >> 2, 4); + byte_swap_vector(ov, PyArray_DESCR(ap)->elsize >> 2, 4); } Py_DECREF(temp); return 0; @@ -420,7 +421,7 @@ STRING_getitem(char *ip, PyArrayObject *ap) { /* Will eliminate NULLs at the end */ char *ptr; - int size = ap->descr->elsize; + int size = PyArray_DESCR(ap)->elsize; ptr = ip + size - 1; while (*ptr-- == '\0' && size > 0) { @@ -437,8 +438,9 @@ STRING_setitem(PyObject *op, char *ov, PyArrayObject *ap) PyObject *temp = NULL; /* Handle case of assigning from an array scalar */ - if (PyArray_Check(op) && PyArray_NDIM(op) == 0) { - temp = PyArray_ToScalar(PyArray_DATA(op), op); + if (PyArray_Check(op) && PyArray_NDIM((PyArrayObject *)op) == 0) { + temp = PyArray_ToScalar(PyArray_BYTES((PyArrayObject *)op), + (PyArrayObject *)op); if (temp == NULL) { return -1; } @@ -489,13 +491,13 @@ STRING_setitem(PyObject *op, char *ov, PyArrayObject *ap) Py_DECREF(temp); return -1; } - memcpy(ov, ptr, MIN(ap->descr->elsize,len)); + memcpy(ov, ptr, MIN(PyArray_DESCR(ap)->elsize,len)); /* * If string lenth is smaller than room in array * Then fill the rest of the element size with NULL */ - if (ap->descr->elsize > len) { - memset(ov + len, 0, (ap->descr->elsize - len)); + if (PyArray_DESCR(ap)->elsize > len) { + memset(ov + len, 0, (PyArray_DESCR(ap)->elsize - len)); } Py_DECREF(temp); return 0; @@ -540,11 +542,11 @@ OBJECT_setitem(PyObject *op, char *ov, PyArrayObject *ap) static PyObject * VOID_getitem(char *ip, PyArrayObject *ap) { - PyObject *u = NULL; + PyArrayObject *u = NULL; PyArray_Descr* descr; int itemsize; - descr = ap->descr; + descr = PyArray_DESCR(ap); if (PyDataType_HASFIELDS(descr)) { PyObject *key; PyObject *names; @@ -559,35 +561,39 @@ VOID_getitem(char *ip, PyArrayObject *ap) names = descr->names; n = PyTuple_GET_SIZE(names); ret = PyTuple_New(n); - savedflags = ap->flags; + savedflags = PyArray_FLAGS(ap); for (i = 0; i < n; i++) { key = PyTuple_GET_ITEM(names, i); tup = PyDict_GetItem(descr->fields, key); if (!PyArg_ParseTuple(tup, "Oi|O", &new, &offset, &title)) { Py_DECREF(ret); - ap->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->descr = descr; return NULL; } - ap->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)ap)->descr = new; /* update alignment based on offset */ if ((new->alignment > 1) && ((((intp)(ip+offset)) % new->alignment) != 0)) { - ap->flags &= ~NPY_ARRAY_ALIGNED; + PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED); } else { - ap->flags |= NPY_ARRAY_ALIGNED; + PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED); } PyTuple_SET_ITEM(ret, i, new->f->getitem(ip+offset, ap)); - ap->flags = savedflags; + ((PyArrayObject_fieldaccess *)ap)->flags = savedflags; } - ap->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->descr = descr; return ret; } if (descr->subarray) { /* return an array of the basic type */ PyArray_Dims shape = {NULL, -1}; - PyObject *ret; + PyArrayObject *ret; if (!(PyArray_IntpConverter(descr->subarray->shape, &shape))) { PyDimMem_FREE(shape.ptr); @@ -596,17 +602,20 @@ VOID_getitem(char *ip, PyArrayObject *ap) return NULL; } Py_INCREF(descr->subarray->base); - ret = PyArray_NewFromDescr(&PyArray_Type, + ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, descr->subarray->base, shape.len, shape.ptr, - NULL, ip, ap->flags&(~NPY_ARRAY_F_CONTIGUOUS), NULL); + NULL, ip, PyArray_FLAGS(ap)&(~NPY_ARRAY_F_CONTIGUOUS), NULL); PyDimMem_FREE(shape.ptr); if (!ret) { return NULL; } - PyArray_BASE(ret) = (PyObject *)ap; Py_INCREF(ap); + if (PyArray_SetBaseObject(ret, (PyObject *)ap) < 0) { + Py_DECREF(ret); + return NULL; + } PyArray_UpdateFlags((PyArrayObject *)ret, NPY_ARRAY_UPDATE_ALL); - return ret; + return (PyObject *)ret; } if (PyDataType_FLAGCHK(descr, NPY_ITEM_HASOBJECT) @@ -615,7 +624,7 @@ VOID_getitem(char *ip, PyArrayObject *ap) "tried to get void-array with object members as buffer."); return NULL; } - itemsize = ap->descr->elsize; + itemsize = PyArray_DESCR(ap)->elsize; #if defined(NPY_PY3K) /* * Return a byte array; there are no plain buffer objects on Py3 @@ -625,13 +634,16 @@ VOID_getitem(char *ip, PyArrayObject *ap) PyArray_Descr *descr; dims[0] = itemsize; strides[0] = 1; - descr = PyArray_DescrNewFromType(PyArray_BYTE); - u = PyArray_NewFromDescr(&PyArray_Type, descr, 1, dims, strides, - ip, + descr = PyArray_DescrNewFromType(NPY_BYTE); + u = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, + descr, 1, dims, strides, ip, PyArray_ISWRITEABLE(ap) ? NPY_ARRAY_WRITEABLE : 0, NULL); - ((PyArrayObject*)u)->base = ap; Py_INCREF(ap); + if (PyArray_SetBaseObject(u, ap) < 0) { + Py_DECREF(u); + return NULL; + } } #else /* @@ -639,19 +651,16 @@ VOID_getitem(char *ip, PyArrayObject *ap) * current item a view of it */ if (PyArray_ISWRITEABLE(ap)) { - u = PyBuffer_FromReadWriteMemory(ip, itemsize); + u = (PyArrayObject *)PyBuffer_FromReadWriteMemory(ip, itemsize); } else { - u = PyBuffer_FromMemory(ip, itemsize); + u = (PyArrayObject *)PyBuffer_FromMemory(ip, itemsize); } #endif if (u == NULL) { - goto fail; + return NULL; } - return u; - -fail: - return NULL; + return (PyObject *)u; } @@ -661,10 +670,10 @@ static int VOID_setitem(PyObject *op, char *ip, PyArrayObject *ap) { PyArray_Descr* descr; - int itemsize=ap->descr->elsize; + int itemsize=PyArray_DESCR(ap)->elsize; int res; - descr = ap->descr; + descr = PyArray_DESCR(ap); if (descr->names && PyTuple_Check(op)) { PyObject *key; PyObject *names; @@ -683,37 +692,41 @@ VOID_setitem(PyObject *op, char *ip, PyArrayObject *ap) "size of tuple must match number of fields."); return -1; } - savedflags = ap->flags; + savedflags = PyArray_FLAGS(ap); for (i = 0; i < n; i++) { key = PyTuple_GET_ITEM(names, i); tup = PyDict_GetItem(descr->fields, key); if (!PyArg_ParseTuple(tup, "Oi|O", &new, &offset, &title)) { - ap->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->descr = descr; return -1; } - ap->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)ap)->descr = new; /* remember to update alignment flags */ if ((new->alignment > 1) && ((((intp)(ip+offset)) % new->alignment) != 0)) { - ap->flags &= ~NPY_ARRAY_ALIGNED; + PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED); } else { - ap->flags |= NPY_ARRAY_ALIGNED; + PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED); } res = new->f->setitem(PyTuple_GET_ITEM(op, i), ip+offset, ap); - ap->flags = savedflags; + ((PyArrayObject_fieldaccess *)ap)->flags = savedflags; if (res < 0) { break; } } - ap->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->descr = descr; return res; } if (descr->subarray) { /* copy into an array of the same basic type */ PyArray_Dims shape = {NULL, -1}; - PyObject *ret; + PyArrayObject *ret; if (!(PyArray_IntpConverter(descr->subarray->shape, &shape))) { PyDimMem_FREE(shape.ptr); PyErr_SetString(PyExc_ValueError, @@ -721,17 +734,20 @@ VOID_setitem(PyObject *op, char *ip, PyArrayObject *ap) return -1; } Py_INCREF(descr->subarray->base); - ret = PyArray_NewFromDescr(&PyArray_Type, - descr->subarray->base, shape.len, shape.ptr, - NULL, ip, ap->flags, NULL); + ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, + descr->subarray->base, shape.len, shape.ptr, + NULL, ip, PyArray_FLAGS(ap), NULL); PyDimMem_FREE(shape.ptr); if (!ret) { return -1; } - PyArray_BASE(ret) = (PyObject *)ap; Py_INCREF(ap); - PyArray_UpdateFlags((PyArrayObject *)ret, NPY_ARRAY_UPDATE_ALL); - res = PyArray_CopyObject((PyArrayObject *)ret, op); + if (PyArray_SetBaseObject(ret, (PyObject *)ap) < 0) { + Py_DECREF(ret); + return -1; + } + PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); + res = PyArray_CopyObject(ret, op); Py_DECREF(ret); return res; } @@ -776,7 +792,7 @@ DATETIME_getitem(char *ip, PyArrayObject *ap) { dt = *((npy_datetime *)ip); } else { - ap->descr->f->copyswap(&dt, ip, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(&dt, ip, !PyArray_ISNOTSWAPPED(ap), ap); } return convert_datetime_to_pyobject(dt, meta); @@ -798,7 +814,7 @@ TIMEDELTA_getitem(char *ip, PyArrayObject *ap) { td = *((npy_timedelta *)ip); } else { - ap->descr->f->copyswap(&td, ip, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(&td, ip, !PyArray_ISNOTSWAPPED(ap), ap); } return convert_timedelta_to_pyobject(td, meta); @@ -827,7 +843,7 @@ DATETIME_setitem(PyObject *op, char *ov, PyArrayObject *ap) { *((npy_datetime *)ov)=temp; } else { - ap->descr->f->copyswap(ov, &temp, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(ov, &temp, !PyArray_ISNOTSWAPPED(ap), ap); } return 0; @@ -856,7 +872,7 @@ TIMEDELTA_setitem(PyObject *op, char *ov, PyArrayObject *ap) { *((npy_timedelta *)ov)=temp; } else { - ap->descr->f->copyswap(ov, &temp, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(ov, &temp, !PyArray_ISNOTSWAPPED(ap), ap); } return 0; @@ -1137,7 +1153,7 @@ static void * longlong, ulonglong, npy_half, float, double, longdouble, * cfloat, cdouble, clongdouble, char, char, char, PyObject *, * datetime, timedelta# - * #skip = 1*18, aip->descr->elsize*3, 1*3# + * #skip = 1*18, PyArray_DESCR(aip)->elsize*3, 1*3# */ static void @FROMTYPE@_to_OBJECT(@fromtype@ *ip, PyObject **op, intp n, PyArrayObject *aip, @@ -1187,7 +1203,7 @@ static void * longlong, ulonglong, npy_half, float, double, longdouble, * cfloat, cdouble, clongdouble, char, char, char, datetime, * timedelta# - * #skip = 1*18, aop->descr->elsize*3, 1*2# + * #skip = 1*18, PyArray_DESCR(aop)->elsize*3, 1*2# */ static void OBJECT_to_@TOTYPE@(PyObject **ip, @totype@ *op, intp n, @@ -1214,7 +1230,9 @@ OBJECT_to_@TOTYPE@(PyObject **ip, @totype@ *op, intp n, * #fromtyp = char*69# * #to = (BOOL, BYTE, UBYTE, SHORT, USHORT, INT, UINT, LONG, ULONG, LONGLONG, ULONGLONG, HALF, FLOAT, DOUBLE, LONGDOUBLE, CFLOAT, CDOUBLE, CLONGDOUBLE, STRING, UNICODE, VOID, DATETIME, TIMEDELTA)*3# * #totyp = (Bool, byte, ubyte, short, ushort, int, uint, long, ulong, longlong, ulonglong, npy_half, float, double, longdouble, cfloat, cdouble, clongdouble, char, char, char, datetime, timedelta)*3# - * #oskip = (1*18,aop->descr->elsize*3,1*2)*3# + * #oskip = 1*18,(PyArray_DESCR(aop)->elsize)*3,1*2, + * 1*18,(PyArray_DESCR(aop)->elsize)*3,1*2, + * 1*18,(PyArray_DESCR(aop)->elsize)*3,1*2# * #convert = 1*18, 0*3, 1*2, 1*18, 0*3, 1*2, 0*23# * #convstr = (Int*9, Long*2, Float*4, Complex*3, Tuple*3, Long*2)*3# */ @@ -1224,7 +1242,7 @@ static void { intp i; PyObject *temp = NULL; - int skip = aip->descr->elsize; + int skip = PyArray_DESCR(aip)->elsize; int oskip = @oskip@; for (i = 0; i < n; i++, ip+=skip, op+=oskip) { @@ -1280,7 +1298,7 @@ static void intp i; PyObject *temp = NULL; int skip = 1; - int oskip = aop->descr->elsize; + int oskip = PyArray_DESCR(aop)->elsize; for (i = 0; i < n; i++, ip += skip, op += oskip) { temp = @from@_getitem((char *)ip, aip); if (temp == NULL) { @@ -1793,7 +1811,7 @@ STRING_copyswapn (char *dst, intp dstride, char *src, intp sstride, intp n, int NPY_UNUSED(swap), PyArrayObject *arr) { if (src != NULL && arr != NULL) { - int itemsize = arr->descr->elsize; + int itemsize = PyArray_DESCR(arr)->elsize; if (dstride == itemsize && sstride == itemsize) { memcpy(dst, src, itemsize * n); @@ -1820,33 +1838,41 @@ VOID_copyswapn (char *dst, intp dstride, char *src, intp sstride, int offset; Py_ssize_t pos = 0; - descr = arr->descr; + descr = PyArray_DESCR(arr); while (PyDict_Next(descr->fields, &pos, &key, &value)) { if NPY_TITLE_KEY(key, value) { continue; } if (!PyArg_ParseTuple(value, "Oi|O", &new, &offset, &title)) { - arr->descr = descr; + ((PyArrayObject_fieldaccess *)arr)->descr = descr; return; } - arr->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)arr)->descr = new; new->f->copyswapn(dst+offset, dstride, (src != NULL ? src+offset : NULL), sstride, n, swap, arr); } - arr->descr = descr; + ((PyArrayObject_fieldaccess *)arr)->descr = descr; return; } - if (swap && arr->descr->subarray != NULL) { + if (swap && PyArray_DESCR(arr)->subarray != NULL) { PyArray_Descr *descr, *new; npy_intp num; npy_intp i; int subitemsize; char *dstptr, *srcptr; - descr = arr->descr; + descr = PyArray_DESCR(arr); new = descr->subarray->base; - arr->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)arr)->descr = new; dstptr = dst; srcptr = src; subitemsize = new->elsize; @@ -1859,11 +1885,11 @@ VOID_copyswapn (char *dst, intp dstride, char *src, intp sstride, srcptr += sstride; } } - arr->descr = descr; + ((PyArrayObject_fieldaccess *)arr)->descr = descr; return; } if (src != NULL) { - memcpy(dst, src, arr->descr->elsize * n); + memcpy(dst, src, PyArray_DESCR(arr)->elsize * n); } return; } @@ -1880,40 +1906,48 @@ VOID_copyswap (char *dst, char *src, int swap, PyArrayObject *arr) int offset; Py_ssize_t pos = 0; - descr = arr->descr; + descr = PyArray_DESCR(arr); while (PyDict_Next(descr->fields, &pos, &key, &value)) { if NPY_TITLE_KEY(key, value) { continue; } if (!PyArg_ParseTuple(value, "Oi|O", &new, &offset, &title)) { - arr->descr = descr; + ((PyArrayObject_fieldaccess *)arr)->descr = descr; return; } - arr->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)arr)->descr = new; new->f->copyswap(dst+offset, (src != NULL ? src+offset : NULL), swap, arr); } - arr->descr = descr; + ((PyArrayObject_fieldaccess *)arr)->descr = descr; return; } - if (swap && arr->descr->subarray != NULL) { + if (swap && PyArray_DESCR(arr)->subarray != NULL) { PyArray_Descr *descr, *new; npy_intp num; int itemsize; - descr = arr->descr; + descr = PyArray_DESCR(arr); new = descr->subarray->base; - arr->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)arr)->descr = new; itemsize = new->elsize; num = descr->elsize / itemsize; new->f->copyswapn(dst, itemsize, src, itemsize, num, swap, arr); - arr->descr = descr; + ((PyArrayObject_fieldaccess *)arr)->descr = descr; return; } if (src != NULL) { - memcpy(dst, src, arr->descr->elsize); + memcpy(dst, src, PyArray_DESCR(arr)->elsize); } return; } @@ -1928,7 +1962,7 @@ UNICODE_copyswapn (char *dst, intp dstride, char *src, intp sstride, if (arr == NULL) { return; } - itemsize = arr->descr->elsize; + itemsize = PyArray_DESCR(arr)->elsize; if (src != NULL) { if (dstride == itemsize && sstride == itemsize) { memcpy(dst, src, n * itemsize); @@ -1963,7 +1997,7 @@ static void STRING_copyswap(char *dst, char *src, int NPY_UNUSED(swap), PyArrayObject *arr) { if (src != NULL && arr != NULL) { - memcpy(dst, src, arr->descr->elsize); + memcpy(dst, src, PyArray_DESCR(arr)->elsize); } } @@ -1975,7 +2009,7 @@ UNICODE_copyswap (char *dst, char *src, int swap, PyArrayObject *arr) if (arr == NULL) { return; } - itemsize = arr->descr->elsize; + itemsize = PyArray_DESCR(arr)->elsize; if (src != NULL) { memcpy(dst, src, itemsize); } @@ -2031,7 +2065,7 @@ static Bool */ @type@ tmp; #if @isfloat@ - ap->descr->f->copyswap(&tmp, ip, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(&tmp, ip, !PyArray_ISNOTSWAPPED(ap), ap); #else memcpy(&tmp, ip, sizeof(@type@)); #endif @@ -2054,7 +2088,7 @@ static Bool } else { @type@ tmp; - ap->descr->f->copyswap(&tmp, ip, !PyArray_ISNOTSWAPPED(ap), ap); + PyArray_DESCR(ap)->f->copyswap(&tmp, ip, !PyArray_ISNOTSWAPPED(ap), ap); return (Bool) ((tmp.real != 0) || (tmp.imag != 0)); } } @@ -2083,7 +2117,7 @@ Py_STRING_ISSPACE(char ch) static Bool STRING_nonzero (char *ip, PyArrayObject *ap) { - int len = ap->descr->elsize; + int len = PyArray_DESCR(ap)->elsize; int i; Bool nonz = FALSE; @@ -2106,17 +2140,17 @@ STRING_nonzero (char *ip, PyArrayObject *ap) static Bool UNICODE_nonzero (PyArray_UCS4 *ip, PyArrayObject *ap) { - int len = ap->descr->elsize >> 2; + int len = PyArray_DESCR(ap)->elsize >> 2; int i; Bool nonz = FALSE; char *buffer = NULL; if ((!PyArray_ISNOTSWAPPED(ap)) || (!PyArray_ISALIGNED(ap))) { - buffer = PyArray_malloc(ap->descr->elsize); + buffer = PyArray_malloc(PyArray_DESCR(ap)->elsize); if (buffer == NULL) { return nonz; } - memcpy(buffer, ip, ap->descr->elsize); + memcpy(buffer, ip, PyArray_DESCR(ap)->elsize); if (!PyArray_ISNOTSWAPPED(ap)) { byte_swap_vector(buffer, len, 4); } @@ -2170,8 +2204,8 @@ VOID_nonzero (char *ip, PyArrayObject *ap) int savedflags, offset; Py_ssize_t pos = 0; - descr = ap->descr; - savedflags = ap->flags; + descr = PyArray_DESCR(ap); + savedflags = PyArray_FLAGS(ap); while (PyDict_Next(descr->fields, &pos, &key, &value)) { if NPY_TITLE_KEY(key, value) { continue; @@ -2181,24 +2215,28 @@ VOID_nonzero (char *ip, PyArrayObject *ap) PyErr_Clear(); continue; } - ap->descr = new; - ap->flags = savedflags; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)ap)->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->flags = savedflags; if ((new->alignment > 1) && !__ALIGNED(ip+offset, new->alignment)) { - ap->flags &= ~NPY_ARRAY_ALIGNED; + PyArray_CLEARFLAGS(ap, NPY_ARRAY_ALIGNED); } else { - ap->flags |= NPY_ARRAY_ALIGNED; + PyArray_ENABLEFLAGS(ap, NPY_ARRAY_ALIGNED); } if (new->f->nonzero(ip+offset, ap)) { nonz = TRUE; break; } } - ap->descr = descr; - ap->flags = savedflags; + ((PyArrayObject_fieldaccess *)ap)->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->flags = savedflags; return nonz; } - len = ap->descr->elsize; + len = PyArray_DESCR(ap)->elsize; for (i = 0; i < len; i++) { if (*ip != '\0') { nonz = TRUE; @@ -2413,7 +2451,7 @@ STRING_compare(char *ip1, char *ip2, PyArrayObject *ap) { const unsigned char *c1 = (unsigned char *)ip1; const unsigned char *c2 = (unsigned char *)ip2; - const size_t len = ap->descr->elsize; + const size_t len = PyArray_DESCR(ap)->elsize; size_t i; for(i = 0; i < len; ++i) { @@ -2431,7 +2469,7 @@ static int UNICODE_compare(PyArray_UCS4 *ip1, PyArray_UCS4 *ip2, PyArrayObject *ap) { - int itemsize = ap->descr->elsize; + int itemsize = PyArray_DESCR(ap)->elsize; if (itemsize < 0) { return 0; @@ -2469,7 +2507,7 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) if (!PyArray_HASFIELDS(ap)) { return STRING_compare(ip1, ip2, ap); } - descr = ap->descr; + descr = PyArray_DESCR(ap); /* * Compare on the first-field. If equal, then * compare on the second-field, etc. @@ -2481,7 +2519,11 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) if (!PyArg_ParseTuple(tup, "Oi|O", &new, &offset, &title)) { goto finish; } - ap->descr = new; + /* + * TODO: temporarily modifying the array like this + * is bad coding style, should be changed. + */ + ((PyArrayObject_fieldaccess *)ap)->descr = new; swap = PyArray_ISBYTESWAPPED(ap); nip1 = ip1+offset; nip2 = ip2+offset; @@ -2525,7 +2567,7 @@ VOID_compare(char *ip1, char *ip2, PyArrayObject *ap) } finish: - ap->descr = descr; + ((PyArrayObject_fieldaccess *)ap)->descr = descr; return res; } @@ -2648,7 +2690,7 @@ static int @fname@_argmax(@type@ *ip, intp n, intp *max_ind, PyArrayObject *aip) { intp i; - int elsize = aip->descr->elsize; + int elsize = PyArray_DESCR(aip)->elsize; @type@ *mp = (@type@ *)PyArray_malloc(elsize); if (mp==NULL) return 0; diff --git a/numpy/core/src/multiarray/buffer.c b/numpy/core/src/multiarray/buffer.c index eb1c028a9..75c6b9ba1 100644 --- a/numpy/core/src/multiarray/buffer.c +++ b/numpy/core/src/multiarray/buffer.c @@ -45,7 +45,7 @@ array_getreadbuf(PyArrayObject *self, Py_ssize_t segment, void **ptrptr) return -1; } if (PyArray_ISONESEGMENT(self)) { - *ptrptr = self->data; + *ptrptr = PyArray_DATA(self); return PyArray_NBYTES(self); } PyErr_SetString(PyExc_ValueError, "array is not a single segment"); @@ -154,7 +154,7 @@ _is_natively_aligned_at(PyArray_Descr *descr, { int k; - if ((Py_ssize_t)(arr->data) % descr->alignment != 0) { + if ((Py_ssize_t)(PyArray_DATA(arr)) % descr->alignment != 0) { return 0; } @@ -166,9 +166,9 @@ _is_natively_aligned_at(PyArray_Descr *descr, return 0; } - for (k = 0; k < arr->nd; ++k) { - if (arr->dimensions[k] > 1) { - if (arr->strides[k] % descr->alignment != 0) { + for (k = 0; k < PyArray_NDIM(arr); ++k) { + if (PyArray_DIM(arr, k) > 1) { + if (PyArray_STRIDE(arr, k) % descr->alignment != 0) { return 0; } } diff --git a/numpy/core/src/multiarray/calculation.c b/numpy/core/src/multiarray/calculation.c index ddb68953d..a6cb82ecb 100644 --- a/numpy/core/src/multiarray/calculation.c +++ b/numpy/core/src/multiarray/calculation.c @@ -45,7 +45,6 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) intp *rptr; intp i, n, m; int elsize; - int copyret = 0; NPY_BEGIN_THREADS_DEF; if ((ap=(PyArrayObject *)PyArray_CheckAxis(op, &axis, 0)) == NULL) { @@ -55,16 +54,16 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) * We need to permute the array so that axis is placed at the end. * And all other dimensions are shifted left. */ - if (axis != ap->nd-1) { + if (axis != PyArray_NDIM(ap)-1) { PyArray_Dims newaxes; intp dims[MAX_DIMS]; int i; newaxes.ptr = dims; - newaxes.len = ap->nd; + newaxes.len = PyArray_NDIM(ap); for (i = 0; i < axis; i++) dims[i] = i; - for (i = axis; i < ap->nd - 1; i++) dims[i] = i + 1; - dims[ap->nd - 1] = axis; + for (i = axis; i < PyArray_NDIM(ap) - 1; i++) dims[i] = i + 1; + dims[PyArray_NDIM(ap) - 1] = axis; op = (PyArrayObject *)PyArray_Transpose(ap, &newaxes); Py_DECREF(ap); if (op == NULL) { @@ -76,20 +75,19 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) } /* Will get native-byte order contiguous copy. */ - ap = (PyArrayObject *) - PyArray_ContiguousFromAny((PyObject *)op, - op->descr->type_num, 1, 0); + ap = (PyArrayObject *)PyArray_ContiguousFromAny((PyObject *)op, + PyArray_DESCR(op)->type_num, 1, 0); Py_DECREF(op); if (ap == NULL) { return NULL; } - arg_func = ap->descr->f->argmax; + arg_func = PyArray_DESCR(ap)->f->argmax; if (arg_func == NULL) { PyErr_SetString(PyExc_TypeError, "data type not ordered"); goto fail; } - elsize = ap->descr->elsize; - m = ap->dimensions[ap->nd-1]; + elsize = PyArray_DESCR(ap)->elsize; + m = PyArray_DIMS(ap)[PyArray_NDIM(ap)-1]; if (m == 0) { PyErr_SetString(PyExc_ValueError, "attempt to get argmax/argmin "\ @@ -98,8 +96,8 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) } if (!out) { - rp = (PyArrayObject *)PyArray_New(Py_TYPE(ap), ap->nd-1, - ap->dimensions, PyArray_INTP, + rp = (PyArrayObject *)PyArray_New(Py_TYPE(ap), PyArray_NDIM(ap)-1, + PyArray_DIMS(ap), PyArray_INTP, NULL, NULL, 0, 0, (PyObject *)ap); if (rp == NULL) { @@ -108,38 +106,33 @@ PyArray_ArgMax(PyArrayObject *op, int axis, PyArrayObject *out) } else { if (PyArray_SIZE(out) != - PyArray_MultiplyList(ap->dimensions, ap->nd - 1)) { + PyArray_MultiplyList(PyArray_DIMS(ap), PyArray_NDIM(ap) - 1)) { PyErr_SetString(PyExc_TypeError, "invalid shape for output array."); } - rp = (PyArrayObject *)\ - PyArray_FromArray(out, + rp = (PyArrayObject *)PyArray_FromArray(out, PyArray_DescrFromType(PyArray_INTP), NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY); if (rp == NULL) { goto fail; } - if (rp != out) { - copyret = 1; - } } - NPY_BEGIN_THREADS_DESCR(ap->descr); + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap)); n = PyArray_SIZE(ap)/m; - rptr = (intp *)rp->data; - for (ip = ap->data, i = 0; i < n; i++, ip += elsize*m) { + rptr = (intp *)PyArray_DATA(rp); + for (ip = PyArray_DATA(ap), i = 0; i < n; i++, ip += elsize*m) { arg_func(ip, m, rptr, ap); rptr += 1; } - NPY_END_THREADS_DESCR(ap->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(ap)); Py_DECREF(ap); - if (copyret) { - PyArrayObject *obj; - obj = (PyArrayObject *)rp->base; - Py_INCREF(obj); + /* Trigger the UPDATEIFCOPY if necessary */ + if (out != NULL && out != rp) { Py_DECREF(rp); - rp = obj; + rp = out; + Py_INCREF(rp); } return (PyObject *)rp; @@ -194,7 +187,7 @@ PyArray_Max(PyArrayObject *ap, int axis, PyArrayObject *out) return NULL; } ret = PyArray_GenericReduceFunction(arr, n_ops.maximum, axis, - arr->descr->type_num, out); + PyArray_DESCR(arr)->type_num, out); Py_DECREF(arr); return ret; } @@ -212,7 +205,7 @@ PyArray_Min(PyArrayObject *ap, int axis, PyArrayObject *out) return NULL; } ret = PyArray_GenericReduceFunction(arr, n_ops.minimum, axis, - arr->descr->type_num, out); + PyArray_DESCR(arr)->type_num, out); Py_DECREF(arr); return ret; } @@ -273,24 +266,26 @@ NPY_NO_EXPORT PyObject * __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, int variance, int num) { - PyObject *obj1 = NULL, *obj2 = NULL, *obj3 = NULL, *new = NULL; + PyObject *obj1 = NULL, *obj2 = NULL, *obj3 = NULL; + PyArrayObject *arr1 = NULL, *arr2 = NULL, *new = NULL; PyObject *ret = NULL, *newshape = NULL; int i, n; intp val; - if ((new = PyArray_CheckAxis(self, &axis, 0)) == NULL) { + if ((new = (PyArrayObject *)PyArray_CheckAxis(self, &axis, 0)) == NULL) { return NULL; } /* Compute and reshape mean */ - obj1 = PyArray_EnsureAnyArray(PyArray_Mean((PyArrayObject *)new, axis, rtype, NULL)); - if (obj1 == NULL) { + arr1 = (PyArrayObject *)PyArray_EnsureAnyArray( + PyArray_Mean(new, axis, rtype, NULL)); + if (arr1 == NULL) { Py_DECREF(new); return NULL; } n = PyArray_NDIM(new); newshape = PyTuple_New(n); if (newshape == NULL) { - Py_DECREF(obj1); + Py_DECREF(arr1); Py_DECREF(new); return NULL; } @@ -303,43 +298,44 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, } PyTuple_SET_ITEM(newshape, i, PyInt_FromLong((long)val)); } - obj2 = PyArray_Reshape((PyArrayObject *)obj1, newshape); - Py_DECREF(obj1); + arr2 = (PyArrayObject *)PyArray_Reshape(arr1, newshape); + Py_DECREF(arr1); Py_DECREF(newshape); - if (obj2 == NULL) { + if (arr2 == NULL) { Py_DECREF(new); return NULL; } /* Compute x = x - mx */ - obj1 = PyArray_EnsureAnyArray(PyNumber_Subtract((PyObject *)new, obj2)); - Py_DECREF(obj2); - if (obj1 == NULL) { + arr1 = (PyArrayObject *)PyArray_EnsureAnyArray( + PyNumber_Subtract((PyObject *)new, (PyObject *)arr2)); + Py_DECREF(arr2); + if (arr1 == NULL) { Py_DECREF(new); return NULL; } /* Compute x * x */ - if (PyArray_ISCOMPLEX(obj1)) { - obj3 = PyArray_Conjugate((PyArrayObject *)obj1, NULL); + if (PyArray_ISCOMPLEX(arr1)) { + obj3 = PyArray_Conjugate(arr1, NULL); } else { - obj3 = obj1; - Py_INCREF(obj1); + obj3 = (PyObject *)arr1; + Py_INCREF(arr1); } if (obj3 == NULL) { Py_DECREF(new); return NULL; } - obj2 = PyArray_EnsureAnyArray \ - (PyArray_GenericBinaryFunction((PyArrayObject *)obj1, obj3, n_ops.multiply)); - Py_DECREF(obj1); + arr2 = (PyArrayObject *)PyArray_EnsureAnyArray( + PyArray_GenericBinaryFunction(arr1, obj3, n_ops.multiply)); + Py_DECREF(arr1); Py_DECREF(obj3); - if (obj2 == NULL) { + if (arr2 == NULL) { Py_DECREF(new); return NULL; } - if (PyArray_ISCOMPLEX(obj2)) { - obj3 = PyObject_GetAttrString(obj2, "real"); + if (PyArray_ISCOMPLEX(arr2)) { + obj3 = PyObject_GetAttrString((PyObject *)arr2, "real"); switch(rtype) { case NPY_CDOUBLE: rtype = NPY_DOUBLE; @@ -353,8 +349,8 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, } } else { - obj3 = obj2; - Py_INCREF(obj2); + obj3 = (PyObject *)arr2; + Py_INCREF(arr2); } if (obj3 == NULL) { Py_DECREF(new); @@ -364,7 +360,7 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, obj1 = PyArray_GenericReduceFunction((PyArrayObject *)obj3, n_ops.add, axis, rtype, NULL); Py_DECREF(obj3); - Py_DECREF(obj2); + Py_DECREF(arr2); if (obj1 == NULL) { Py_DECREF(new); return NULL; @@ -385,10 +381,10 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, Py_DECREF(obj2); if (!variance) { - obj1 = PyArray_EnsureAnyArray(ret); + arr1 = (PyArrayObject *)PyArray_EnsureAnyArray(ret); /* sqrt() */ - ret = PyArray_GenericUnaryFunction((PyArrayObject *)obj1, n_ops.sqrt); - Py_DECREF(obj1); + ret = PyArray_GenericUnaryFunction(arr1, n_ops.sqrt); + Py_DECREF(arr1); } if (ret == NULL) { return NULL; @@ -399,12 +395,12 @@ __New_PyArray_Std(PyArrayObject *self, int axis, int rtype, PyArrayObject *out, if (PyArray_Check(self) && Py_TYPE(self) == Py_TYPE(ret)) { goto finish; } - obj1 = PyArray_EnsureArray(ret); - if (obj1 == NULL) { + arr1 = (PyArrayObject *)PyArray_EnsureArray(ret); + if (arr1 == NULL) { return NULL; } - ret = PyArray_View((PyArrayObject *)obj1, NULL, Py_TYPE(self)); - Py_DECREF(obj1); + ret = PyArray_View(arr1, NULL, Py_TYPE(self)); + Py_DECREF(arr1); finish: if (out) { @@ -599,10 +595,10 @@ PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out) my_descr = PyArray_DescrFromType(NPY_DOUBLE); } else { - Py_INCREF(a->descr); - my_descr = a->descr; + Py_INCREF(PyArray_DESCR(a)); + my_descr = PyArray_DESCR(a); } - out = (PyArrayObject *)PyArray_Empty(a->nd, a->dimensions, + out = (PyArrayObject *)PyArray_Empty(PyArray_NDIM(a), PyArray_DIMS(a), my_descr, PyArray_ISFORTRAN(a)); if (out == NULL) { @@ -639,9 +635,9 @@ PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out) Py_DECREF(f); Py_DECREF(out); if (ret_int) { - Py_INCREF(a->descr); + Py_INCREF(PyArray_DESCR(a)); tmp = PyArray_CastToType((PyArrayObject *)ret, - a->descr, PyArray_ISFORTRAN(a)); + PyArray_DESCR(a), PyArray_ISFORTRAN(a)); Py_DECREF(ret); return tmp; } @@ -655,13 +651,13 @@ PyArray_Round(PyArrayObject *a, int decimals, PyArrayObject *out) NPY_NO_EXPORT PyObject * PyArray_Mean(PyArrayObject *self, int axis, int rtype, PyArrayObject *out) { - PyObject *obj1 = NULL, *obj2 = NULL; - PyObject *new, *ret; + PyObject *obj1 = NULL, *obj2 = NULL, *ret; + PyArrayObject *new; - if ((new = PyArray_CheckAxis(self, &axis, 0)) == NULL) { + if ((new = (PyArrayObject *)PyArray_CheckAxis(self, &axis, 0)) == NULL) { return NULL; } - obj1 = PyArray_GenericReduceFunction((PyArrayObject *)new, n_ops.add, axis, + obj1 = PyArray_GenericReduceFunction(new, n_ops.add, axis, rtype, out); obj2 = PyFloat_FromDouble((double) PyArray_DIM(new,axis)); Py_DECREF(new); @@ -810,7 +806,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o return NULL; } - func = self->descr->f->fastclip; + func = PyArray_DESCR(self)->f->fastclip; if (func == NULL || (min != NULL && !PyArray_CheckAnyScalar(min)) || (max != NULL && !PyArray_CheckAnyScalar(max))) { return _slow_array_clip(self, min, max, out); @@ -844,15 +840,15 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o * type that matches both). */ if (PyArray_ScalarKind(newdescr->type_num, NULL) > - PyArray_ScalarKind(self->descr->type_num, NULL)) { - indescr = PyArray_PromoteTypes(newdescr, self->descr); + PyArray_ScalarKind(PyArray_DESCR(self)->type_num, NULL)) { + indescr = PyArray_PromoteTypes(newdescr, PyArray_DESCR(self)); func = indescr->f->fastclip; if (func == NULL) { return _slow_array_clip(self, min, max, out); } } else { - indescr = self->descr; + indescr = PyArray_DESCR(self); Py_INCREF(indescr); } Py_DECREF(newdescr); @@ -927,7 +923,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o if (PyArray_ISONESEGMENT(self) && PyArray_CHKFLAGS(self, NPY_ARRAY_ALIGNED) && PyArray_ISNOTSWAPPED(self) && - (self->descr == indescr)) { + (PyArray_DESCR(self) == indescr)) { ingood = 1; } if (!ingood) { @@ -974,8 +970,8 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o if (out == NULL) { Py_INCREF(indescr); out = (PyArrayObject*)PyArray_NewFromDescr(Py_TYPE(self), - indescr, self->nd, - self->dimensions, + indescr, PyArray_NDIM(self), + PyArray_DIMS(self), NULL, NULL, PyArray_ISFORTRAN(self), (PyObject *)self); @@ -992,7 +988,7 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o if (!outgood && PyArray_ISONESEGMENT(out) && PyArray_CHKFLAGS(out, NPY_ARRAY_ALIGNED) && PyArray_ISNOTSWAPPED(out) && - PyArray_EquivTypes(out->descr, indescr)) { + PyArray_EquivTypes(PyArray_DESCR(out), indescr)) { outgood = 1; } @@ -1024,19 +1020,19 @@ PyArray_Clip(PyArrayObject *self, PyObject *min, PyObject *max, PyArrayObject *o "same shape as the input."); goto fail; } - if (newout->data != newin->data) { - memcpy(newout->data, newin->data, PyArray_NBYTES(newin)); + if (PyArray_DATA(newout) != PyArray_DATA(newin)) { + memcpy(PyArray_DATA(newout), PyArray_DATA(newin), PyArray_NBYTES(newin)); } /* Now we can call the fast-clip function */ min_data = max_data = NULL; if (mina != NULL) { - min_data = mina->data; + min_data = PyArray_DATA(mina); } if (maxa != NULL) { - max_data = maxa->data; + max_data = PyArray_DATA(maxa); } - func(newin->data, PyArray_SIZE(newin), min_data, max_data, newout->data); + func(PyArray_DATA(newin), PyArray_SIZE(newin), min_data, max_data, PyArray_DATA(newout)); /* Clean up temporary variables */ Py_XDECREF(mina); diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c index 89d4c5f63..5b17ad28c 100644 --- a/numpy/core/src/multiarray/common.c +++ b/numpy/core/src/multiarray/common.c @@ -92,7 +92,7 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max) * is NULL. */ if (PyArray_Check(op)) { - chktype = PyArray_DESCR(op); + chktype = PyArray_DESCR((PyArrayObject *)op); Py_INCREF(chktype); if (minitype == NULL) { return chktype; @@ -211,7 +211,7 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max) if (PyObject_HasAttrString(op, "__array__")) { ip = PyObject_CallMethod(op, "__array__", NULL); if(ip && PyArray_Check(ip)) { - chktype = PyArray_DESCR(ip); + chktype = PyArray_DESCR((PyArrayObject *)ip); Py_INCREF(chktype); Py_DECREF(ip); goto finish; @@ -288,7 +288,7 @@ _array_find_type(PyObject *op, PyArray_Descr *minitype, int max) if (outtype->type_num == PyArray_VOID && minitype->type_num != PyArray_VOID) { Py_DECREF(outtype); - return PyArray_DescrFromType(PyArray_OBJECT); + return PyArray_DescrFromType(NPY_OBJECT); } return outtype; } @@ -448,19 +448,19 @@ index2ptr(PyArrayObject *mp, intp i) { intp dim0; - if (mp->nd == 0) { + if (PyArray_NDIM(mp) == 0) { PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed"); return NULL; } - dim0 = mp->dimensions[0]; + dim0 = PyArray_DIMS(mp)[0]; if (i < 0) { i += dim0; } if (i == 0 && dim0 > 0) { - return mp->data; + return PyArray_DATA(mp); } if (i > 0 && i < dim0) { - return mp->data+i*mp->strides[0]; + return PyArray_DATA(mp)+i*PyArray_STRIDES(mp)[0]; } PyErr_SetString(PyExc_IndexError,"index out of bounds"); return NULL; @@ -469,7 +469,7 @@ index2ptr(PyArrayObject *mp, intp i) NPY_NO_EXPORT int _zerofill(PyArrayObject *ret) { - if (PyDataType_REFCHK(ret->descr)) { + if (PyDataType_REFCHK(PyArray_DESCR(ret))) { PyObject *zero = PyInt_FromLong(0); PyArray_FillObjectArray(ret, zero); Py_DECREF(zero); @@ -480,7 +480,7 @@ _zerofill(PyArrayObject *ret) } else { intp n = PyArray_NBYTES(ret); - memset(ret->data, 0, n); + memset(PyArray_DATA(ret), 0, n); } return 0; } @@ -498,14 +498,14 @@ _IsAligned(PyArrayObject *ap) * PyArray_DescrConverter(), but not necessarily when using * PyArray_DescrAlignConverter(). */ - alignment = ap->descr->alignment; + alignment = PyArray_DESCR(ap)->alignment; if (alignment == 1) { return 1; } - ptr = (intp) ap->data; + ptr = (intp) PyArray_DATA(ap); aligned = (ptr % alignment) == 0; - for (i = 0; i < ap->nd; i++) { - aligned &= ((ap->strides[i] % alignment) == 0); + for (i = 0; i < PyArray_NDIM(ap); i++) { + aligned &= ((PyArray_STRIDES(ap)[i] % alignment) == 0); } return aligned != 0; } @@ -513,12 +513,12 @@ _IsAligned(PyArrayObject *ap) NPY_NO_EXPORT Bool _IsWriteable(PyArrayObject *ap) { - PyObject *base=ap->base; + PyObject *base=PyArray_BASE(ap); void *dummy; Py_ssize_t n; /* If we own our own data, then no-problem */ - if ((base == NULL) || (ap->flags & NPY_ARRAY_OWNDATA)) { + if ((base == NULL) || (PyArray_FLAGS(ap) & NPY_ARRAY_OWNDATA)) { return TRUE; } /* @@ -529,13 +529,16 @@ _IsWriteable(PyArrayObject *ap) * or a string object (for pickling support memory savings). * - this last could be removed if a proper pickleable * buffer was added to Python. + * + * MW: I think it would better to disallow switching from READONLY + * to WRITEABLE like this... */ while(PyArray_Check(base)) { - if (PyArray_CHKFLAGS(base, NPY_ARRAY_OWNDATA)) { - return (Bool) (PyArray_ISWRITEABLE(base)); + if (PyArray_CHKFLAGS((PyArrayObject *)base, NPY_ARRAY_OWNDATA)) { + return (npy_bool) (PyArray_ISWRITEABLE((PyArrayObject *)base)); } - base = PyArray_BASE(base); + base = PyArray_BASE((PyArrayObject *)base); } /* diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index c709527a3..74d7e1192 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -360,7 +360,7 @@ PyArray_PyIntAsInt(PyObject *o) long long_value = -1; PyObject *obj; static char *msg = "an integer is required"; - PyObject *arr; + PyArrayObject *arr; PyArray_Descr *descr; int ret; @@ -380,16 +380,18 @@ PyArray_PyIntAsInt(PyObject *o) descr = &INT_Descr; arr = NULL; if (PyArray_Check(o)) { - if (PyArray_SIZE(o)!=1 || !PyArray_ISINTEGER(o)) { + if (PyArray_SIZE((PyArrayObject *)o)!=1 || + !PyArray_ISINTEGER((PyArrayObject *)o)) { PyErr_SetString(PyExc_TypeError, msg); return -1; } Py_INCREF(descr); - arr = PyArray_CastToType((PyArrayObject *)o, descr, 0); + arr = (PyArrayObject *)PyArray_CastToType((PyArrayObject *)o, + descr, 0); } if (PyArray_IsScalar(o, Integer)) { Py_INCREF(descr); - arr = PyArray_FromScalar(o, descr); + arr = (PyArrayObject *)PyArray_FromScalar(o, descr); } if (arr != NULL) { ret = *((int *)PyArray_DATA(arr)); @@ -449,7 +451,7 @@ PyArray_PyIntAsIntp(PyObject *o) longlong long_value = -1; PyObject *obj; static char *msg = "an integer is required"; - PyObject *arr; + PyArrayObject *arr; PyArray_Descr *descr; npy_intp ret; @@ -475,16 +477,18 @@ PyArray_PyIntAsIntp(PyObject *o) arr = NULL; if (PyArray_Check(o)) { - if (PyArray_SIZE(o)!=1 || !PyArray_ISINTEGER(o)) { + if (PyArray_SIZE((PyArrayObject *)o)!=1 || + !PyArray_ISINTEGER((PyArrayObject *)o)) { PyErr_SetString(PyExc_TypeError, msg); return -1; } Py_INCREF(descr); - arr = PyArray_CastToType((PyArrayObject *)o, descr, 0); + arr = (PyArrayObject *)PyArray_CastToType((PyArrayObject *)o, + descr, 0); } else if (PyArray_IsScalar(o, Integer)) { Py_INCREF(descr); - arr = PyArray_FromScalar(o, descr); + arr = (PyArrayObject *)PyArray_FromScalar(o, descr); } if (arr != NULL) { ret = *((npy_intp *)PyArray_DATA(arr)); diff --git a/numpy/core/src/multiarray/convert.c b/numpy/core/src/multiarray/convert.c index 57ad8c22f..d399912a6 100644 --- a/numpy/core/src/multiarray/convert.c +++ b/numpy/core/src/multiarray/convert.c @@ -86,7 +86,7 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format) n3 = (sep ? strlen((const char *)sep) : 0); if (n3 == 0) { /* binary data */ - if (PyDataType_FLAGCHK(self->descr, NPY_LIST_PICKLE)) { + if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_LIST_PICKLE)) { PyErr_SetString(PyExc_ValueError, "cannot write " \ "object arrays to a file in " \ "binary mode"); @@ -100,15 +100,15 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format) #if defined (_MSC_VER) && defined(_WIN64) /* Workaround Win64 fwrite() bug. Ticket #1660 */ { - npy_intp maxsize = 2147483648 / self->descr->elsize; + npy_intp maxsize = 2147483648 / PyArray_DESCR(self)->elsize; npy_intp chunksize; n = 0; while (size > 0) { chunksize = (size > maxsize) ? maxsize : size; n2 = fwrite((const void *) - ((char *)self->data + (n * self->descr->elsize)), - (size_t) self->descr->elsize, + ((char *)PyArray_DATA(self) + (n * PyArray_DESCR(self)->elsize)), + (size_t) PyArray_DESCR(self)->elsize, (size_t) chunksize, fp); if (n2 < chunksize) { break; @@ -119,8 +119,8 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format) size = PyArray_SIZE(self); } #else - n = fwrite((const void *)self->data, - (size_t) self->descr->elsize, + n = fwrite((const void *)PyArray_DATA(self), + (size_t) PyArray_DESCR(self)->elsize, (size_t) size, fp); #endif NPY_END_ALLOW_THREADS; @@ -138,7 +138,7 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format) NPY_BEGIN_THREADS; while (it->index < it->size) { if (fwrite((const void *)it->dataptr, - (size_t) self->descr->elsize, + (size_t) PyArray_DESCR(self)->elsize, 1, fp) < 1) { NPY_END_THREADS; PyErr_Format(PyExc_IOError, @@ -163,7 +163,7 @@ PyArray_ToFile(PyArrayObject *self, FILE *fp, char *sep, char *format) PyArray_IterNew((PyObject *)self); n4 = (format ? strlen((const char *)format) : 0); while (it->index < it->size) { - obj = self->descr->f->getitem(it->dataptr, self); + obj = PyArray_DESCR(self)->f->getitem(it->dataptr, self); if (obj == NULL) { Py_DECREF(it); return -1; @@ -266,7 +266,7 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order) numbytes = PyArray_NBYTES(self); if ((PyArray_ISCONTIGUOUS(self) && (order == NPY_CORDER)) || (PyArray_ISFORTRAN(self) && (order == NPY_FORTRANORDER))) { - ret = PyBytes_FromStringAndSize(self->data, (Py_ssize_t) numbytes); + ret = PyBytes_FromStringAndSize(PyArray_DATA(self), (Py_ssize_t) numbytes); } else { PyObject *new; @@ -293,7 +293,7 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order) } dptr = PyBytes_AS_STRING(ret); index = it->size; - elsize = self->descr->elsize; + elsize = PyArray_DESCR(self)->elsize; while (index--) { memcpy(dptr, it->dataptr, elsize); dptr += elsize; @@ -308,14 +308,14 @@ PyArray_ToString(PyArrayObject *self, NPY_ORDER order) NPY_NO_EXPORT int PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj) { - PyObject *newarr; + PyArrayObject *newarr; int itemsize, swap; void *fromptr; PyArray_Descr *descr; intp size; PyArray_CopySwapFunc *copyswap; - itemsize = arr->descr->elsize; + itemsize = PyArray_DESCR(arr)->elsize; if (PyArray_ISOBJECT(arr)) { fromptr = &obj; swap = 0; @@ -324,7 +324,8 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj) else { descr = PyArray_DESCR(arr); Py_INCREF(descr); - newarr = PyArray_FromAny(obj, descr, 0,0, NPY_ARRAY_ALIGNED, NULL); + newarr = (PyArrayObject *)PyArray_FromAny(obj, descr, + 0,0, NPY_ARRAY_ALIGNED, NULL); if (newarr == NULL) { return -1; } @@ -332,11 +333,11 @@ PyArray_FillWithScalar(PyArrayObject *arr, PyObject *obj) swap = (PyArray_ISNOTSWAPPED(arr) != PyArray_ISNOTSWAPPED(newarr)); } size=PyArray_SIZE(arr); - copyswap = arr->descr->f->copyswap; + copyswap = PyArray_DESCR(arr)->f->copyswap; if (PyArray_ISONESEGMENT(arr)) { char *toptr=PyArray_DATA(arr); PyArray_FillWithScalarFunc* fillwithscalar = - arr->descr->f->fillwithscalar; + PyArray_DESCR(arr)->f->fillwithscalar; if (fillwithscalar && PyArray_ISALIGNED(arr)) { copyswap(fromptr, NULL, swap, newarr); fillwithscalar(toptr, size, fromptr, arr); @@ -489,7 +490,8 @@ PyArray_NewCopy(PyArrayObject *m1, NPY_ORDER order) NPY_NO_EXPORT PyObject * PyArray_View(PyArrayObject *self, PyArray_Descr *type, PyTypeObject *pytype) { - PyObject *new = NULL; + PyArrayObject *new = NULL; + PyArray_Descr *dtype; PyTypeObject *subtype; if (pytype) { @@ -498,21 +500,26 @@ PyArray_View(PyArrayObject *self, PyArray_Descr *type, PyTypeObject *pytype) else { subtype = Py_TYPE(self); } - Py_INCREF(self->descr); - new = PyArray_NewFromDescr(subtype, - self->descr, - self->nd, self->dimensions, - self->strides, - self->data, - self->flags, (PyObject *)self); + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + new = (PyArrayObject *)PyArray_NewFromDescr(subtype, + dtype, + PyArray_NDIM(self), PyArray_DIMS(self), + PyArray_STRIDES(self), + PyArray_DATA(self), + PyArray_FLAGS(self), (PyObject *)self); if (new == NULL) { return NULL; } Py_INCREF(self); - PyArray_BASE(new) = (PyObject *)self; + if (PyArray_SetBaseObject(new, (PyObject *)self) < 0) { + Py_DECREF(new); + Py_DECREF(type); + return NULL; + } if (type != NULL) { - if (PyObject_SetAttrString(new, "dtype", + if (PyObject_SetAttrString((PyObject *)new, "dtype", (PyObject *)type) < 0) { Py_DECREF(new); Py_DECREF(type); @@ -520,5 +527,5 @@ PyArray_View(PyArrayObject *self, PyArray_Descr *type, PyTypeObject *pytype) } Py_DECREF(type); } - return new; + return (PyObject *)new; } diff --git a/numpy/core/src/multiarray/convert_datatype.c b/numpy/core/src/multiarray/convert_datatype.c index da3f7205b..f4d3e8c57 100644 --- a/numpy/core/src/multiarray/convert_datatype.c +++ b/numpy/core/src/multiarray/convert_datatype.c @@ -41,8 +41,8 @@ PyArray_CastToType(PyArrayObject *arr, PyArray_Descr *dtype, int is_f_order) } out = PyArray_NewFromDescr(Py_TYPE(arr), dtype, - arr->nd, - arr->dimensions, + PyArray_NDIM(arr), + PyArray_DIMS(arr), NULL, NULL, is_f_order, (PyObject *)arr); @@ -1633,10 +1633,10 @@ PyArray_Zero(PyArrayObject *arr) int ret, storeflags; PyObject *obj; - if (_check_object_rec(arr->descr) < 0) { + if (_check_object_rec(PyArray_DESCR(arr)) < 0) { return NULL; } - zeroval = PyDataMem_NEW(arr->descr->elsize); + zeroval = PyDataMem_NEW(PyArray_DESCR(arr)->elsize); if (zeroval == NULL) { PyErr_SetNone(PyExc_MemoryError); return NULL; @@ -1648,10 +1648,10 @@ PyArray_Zero(PyArrayObject *arr) Py_DECREF(obj); return zeroval; } - storeflags = arr->flags; - arr->flags |= NPY_ARRAY_BEHAVED; - ret = arr->descr->f->setitem(obj, zeroval, arr); - arr->flags = storeflags; + storeflags = PyArray_FLAGS(arr); + PyArray_ENABLEFLAGS(arr, NPY_ARRAY_BEHAVED); + ret = PyArray_DESCR(arr)->f->setitem(obj, zeroval, arr); + ((PyArrayObject_fieldaccess *)arr)->flags = storeflags; Py_DECREF(obj); if (ret < 0) { PyDataMem_FREE(zeroval); @@ -1670,10 +1670,10 @@ PyArray_One(PyArrayObject *arr) int ret, storeflags; PyObject *obj; - if (_check_object_rec(arr->descr) < 0) { + if (_check_object_rec(PyArray_DESCR(arr)) < 0) { return NULL; } - oneval = PyDataMem_NEW(arr->descr->elsize); + oneval = PyDataMem_NEW(PyArray_DESCR(arr)->elsize); if (oneval == NULL) { PyErr_SetNone(PyExc_MemoryError); return NULL; @@ -1686,10 +1686,10 @@ PyArray_One(PyArrayObject *arr) return oneval; } - storeflags = arr->flags; - arr->flags |= NPY_ARRAY_BEHAVED; - ret = arr->descr->f->setitem(obj, oneval, arr); - arr->flags = storeflags; + storeflags = PyArray_FLAGS(arr); + PyArray_ENABLEFLAGS(arr, NPY_ARRAY_BEHAVED); + ret = PyArray_DESCR(arr)->f->setitem(obj, oneval, arr); + ((PyArrayObject_fieldaccess *)arr)->flags = storeflags; Py_DECREF(obj); if (ret < 0) { PyDataMem_FREE(oneval); @@ -1752,7 +1752,7 @@ PyArray_ConvertToCommonType(PyObject *op, int *retn) for (i = 0; i < n; i++) { mps[i] = (PyArrayObject *) array_big_item((PyArrayObject *)op, i); } - if (!PyArray_ISCARRAY(op)) { + if (!PyArray_ISCARRAY((PyArrayObject *)op)) { for (i = 0; i < n; i++) { PyObject *obj; obj = PyArray_NewCopy(mps[i], NPY_CORDER); diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 6100fe7ee..b527a1074 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -409,7 +409,7 @@ _get_array_memory_extents(PyArrayObject *arr, /* Return a half-open range */ *out_start = start; - *out_end = end + arr->descr->elsize; + *out_end = end + PyArray_DESCR(arr)->elsize; } /* Returns 1 if the arrays have overlapping data, 0 otherwise */ @@ -621,7 +621,7 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, npy_intp offset) } } - if (dim > a->nd) { + if (dim > PyArray_NDIM(a)) { PyErr_Format(PyExc_ValueError, "setArrayFromSequence: sequence/array dimensions mismatch."); goto fail; @@ -635,34 +635,34 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, npy_intp offset) * Either the dimensions match, or the sequence has length 1 and can * be broadcast to the destination. */ - if (slen != a->dimensions[dim] && slen != 1) { + if (slen != PyArray_DIMS(a)[dim] && slen != 1) { PyErr_Format(PyExc_ValueError, "cannot copy sequence with size %d to array axis " - "with dimension %d", (int)slen, (int)a->dimensions[dim]); + "with dimension %d", (int)slen, (int)PyArray_DIMS(a)[dim]); goto fail; } /* Broadcast the one element from the sequence to all the outputs */ if (slen == 1) { PyObject *o; - npy_intp alen = a->dimensions[dim]; + npy_intp alen = PyArray_DIMS(a)[dim]; o = PySequence_GetItem(s, 0); if (o == NULL) { goto fail; } for (i = 0; i < alen; i++) { - if ((a->nd - dim) > 1) { + if ((PyArray_NDIM(a) - dim) > 1) { res = setArrayFromSequence(a, o, dim+1, offset); } else { - res = a->descr->f->setitem(o, (a->data + offset), a); + res = PyArray_DESCR(a)->f->setitem(o, (PyArray_DATA(a) + offset), a); } if (res < 0) { Py_DECREF(o); goto fail; } - offset += a->strides[dim]; + offset += PyArray_STRIDES(a)[dim]; } Py_DECREF(o); } @@ -673,17 +673,17 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, npy_intp offset) if (o == NULL) { goto fail; } - if ((a->nd - dim) > 1) { + if ((PyArray_NDIM(a) - dim) > 1) { res = setArrayFromSequence(a, o, dim+1, offset); } else { - res = a->descr->f->setitem(o, (a->data + offset), a); + res = PyArray_DESCR(a)->f->setitem(o, (PyArray_DATA(a) + offset), a); } Py_DECREF(o); if (res < 0) { goto fail; } - offset += a->strides[dim]; + offset += PyArray_STRIDES(a)[dim]; } } @@ -703,7 +703,7 @@ PyArray_AssignFromSequence(PyArrayObject *self, PyObject *v) "assignment from non-sequence"); return -1; } - if (self->nd == 0) { + if (PyArray_NDIM(self) == 0) { PyErr_SetString(PyExc_ValueError, "assignment to 0-d array"); return -1; @@ -722,7 +722,7 @@ discover_itemsize(PyObject *s, int nd, int *itemsize) int n, r, i; if (PyArray_Check(s)) { - *itemsize = MAX(*itemsize, PyArray_ITEMSIZE(s)); + *itemsize = MAX(*itemsize, PyArray_ITEMSIZE((PyArrayObject *)s)); return 0; } @@ -768,7 +768,7 @@ discover_itemsize(PyObject *s, int nd, int *itemsize) * has, filling in the dimensions as we go. */ static int -discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, +discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it, int stop_at_string, int stop_at_tuple, int *out_is_object) { @@ -782,66 +782,68 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, return 0; } - /* s is an Array */ - if (PyArray_Check(s)) { - if (PyArray_NDIM(s) < *maxndim) { - *maxndim = PyArray_NDIM(s); + /* obj is an Array */ + if (PyArray_Check(obj)) { + PyArrayObject *arr = (PyArrayObject *)obj; + + if (PyArray_NDIM(arr) < *maxndim) { + *maxndim = PyArray_NDIM(arr); } for (i=0; i<*maxndim; i++) { - d[i] = PyArray_DIM(s,i); + d[i] = PyArray_DIM(arr,i); } return 0; } - /* s is a Scalar */ - if (PyArray_IsScalar(s, Generic)) { + /* obj is a Scalar */ + if (PyArray_IsScalar(obj, Generic)) { *maxndim = 0; return 0; } - /* s is not a Sequence */ - if (!PySequence_Check(s) || + /* obj is not a Sequence */ + if (!PySequence_Check(obj) || #if defined(NPY_PY3K) /* FIXME: XXX -- what is the correct thing to do here? */ #else - PyInstance_Check(s) || + PyInstance_Check(obj) || #endif - PySequence_Length(s) < 0) { + PySequence_Length(obj) < 0) { *maxndim = 0; PyErr_Clear(); return 0; } - /* s is a String */ - if (PyString_Check(s) || + /* obj is a String */ + if (PyString_Check(obj) || #if defined(NPY_PY3K) #else - PyBuffer_Check(s) || + PyBuffer_Check(obj) || #endif - PyUnicode_Check(s)) { + PyUnicode_Check(obj)) { if (stop_at_string) { *maxndim = 0; } else { - d[0] = PySequence_Length(s); + d[0] = PySequence_Length(obj); *maxndim = 1; } return 0; } - /* s is a Tuple, but tuples aren't expanded */ - if (stop_at_tuple && PyTuple_Check(s)) { + /* obj is a Tuple, but tuples aren't expanded */ + if (stop_at_tuple && PyTuple_Check(obj)) { *maxndim = 0; return 0; } - /* s is a PEP 3118 buffer */ + /* obj is a PEP 3118 buffer */ #if PY_VERSION_HEX >= 0x02060000 /* PEP 3118 buffer interface */ memset(&buffer_view, 0, sizeof(Py_buffer)); - if (PyObject_GetBuffer(s, &buffer_view, PyBUF_STRIDES) == 0 || - PyObject_GetBuffer(s, &buffer_view, PyBUF_ND) == 0) { + if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_STRIDES) == 0 || + PyObject_GetBuffer(obj, &buffer_view, PyBUF_ND) == 0) { int nd = buffer_view.ndim; if (nd < *maxndim) { *maxndim = nd; @@ -852,7 +854,7 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, PyBuffer_Release(&buffer_view); return 0; } - else if (PyObject_GetBuffer(s, &buffer_view, PyBUF_SIMPLE) == 0) { + else if (PyObject_GetBuffer(obj, &buffer_view, PyBUF_SIMPLE) == 0) { d[0] = buffer_view.len; *maxndim = 1; PyBuffer_Release(&buffer_view); @@ -863,8 +865,8 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, } #endif - /* s has the __array_struct__ interface */ - if ((e = PyObject_GetAttrString(s, "__array_struct__")) != NULL) { + /* obj has the __array_struct__ interface */ + if ((e = PyObject_GetAttrString(obj, "__array_struct__")) != NULL) { int nd = -1; if (NpyCapsule_Check(e)) { PyArrayInterface *inter; @@ -890,8 +892,8 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, PyErr_Clear(); } - /* s has the __array_interface__ interface */ - if ((e = PyObject_GetAttrString(s, "__array_interface__")) != NULL) { + /* obj has the __array_interface__ interface */ + if ((e = PyObject_GetAttrString(obj, "__array_interface__")) != NULL) { int nd = -1; if (PyDict_Check(e)) { PyObject *new; @@ -925,7 +927,7 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, PyErr_Clear(); } - n = PySequence_Size(s); + n = PySequence_Size(obj); if (n < 0) { return -1; @@ -942,7 +944,7 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, npy_intp dtmp[NPY_MAXDIMS]; int j, maxndim_m1 = *maxndim - 1; - if ((e = PySequence_GetItem(s, 0)) == NULL) { + if ((e = PySequence_GetItem(obj, 0)) == NULL) { /* * PySequence_Check detects whether an old type object is a * sequence by the presence of the __getitem__ attribute, and @@ -977,7 +979,7 @@ discover_dimensions(PyObject *s, int *maxndim, npy_intp *d, int check_it, *maxndim = maxndim_m1 + 1; for (i = 1; i < n; ++i) { /* Get the dimensions of the first item */ - if ((e = PySequence_GetItem(s, i)) == NULL) { + if ((e = PySequence_GetItem(obj, i)) == NULL) { /* see comment above */ if (PyErr_ExceptionMatches(PyExc_KeyError)) { PyErr_Clear(); @@ -1028,7 +1030,7 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj) { - PyArrayObject *self; + PyArrayObject_fieldaccess *fa; int i; size_t sd; npy_intp largest; @@ -1116,55 +1118,55 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd, largest /= dim; } - self = (PyArrayObject *) subtype->tp_alloc(subtype, 0); - if (self == NULL) { + fa = (PyArrayObject_fieldaccess *) subtype->tp_alloc(subtype, 0); + if (fa == NULL) { Py_DECREF(descr); return NULL; } - self->nd = nd; - self->dimensions = NULL; - self->data = NULL; + fa->nd = nd; + fa->dimensions = NULL; + fa->data = NULL; if (data == NULL) { - self->flags = NPY_ARRAY_DEFAULT; + fa->flags = NPY_ARRAY_DEFAULT; if (flags) { - self->flags |= NPY_ARRAY_F_CONTIGUOUS; + fa->flags |= NPY_ARRAY_F_CONTIGUOUS; if (nd > 1) { - self->flags &= ~NPY_ARRAY_C_CONTIGUOUS; + fa->flags &= ~NPY_ARRAY_C_CONTIGUOUS; } flags = NPY_ARRAY_F_CONTIGUOUS; } } else { - self->flags = (flags & ~NPY_ARRAY_UPDATEIFCOPY); + fa->flags = (flags & ~NPY_ARRAY_UPDATEIFCOPY); } - self->descr = descr; - self->base = (PyObject *)NULL; - self->weakreflist = (PyObject *)NULL; + fa->descr = descr; + fa->base = (PyObject *)NULL; + fa->weakreflist = (PyObject *)NULL; if (nd > 0) { - self->dimensions = PyDimMem_NEW(2*nd); - if (self->dimensions == NULL) { + fa->dimensions = PyDimMem_NEW(2*nd); + if (fa->dimensions == NULL) { PyErr_NoMemory(); goto fail; } - self->strides = self->dimensions + nd; - memcpy(self->dimensions, dims, sizeof(npy_intp)*nd); + fa->strides = fa->dimensions + nd; + memcpy(fa->dimensions, dims, sizeof(npy_intp)*nd); if (strides == NULL) { /* fill it in */ - sd = _array_fill_strides(self->strides, dims, nd, sd, - flags, &(self->flags)); + sd = _array_fill_strides(fa->strides, dims, nd, sd, + flags, &(fa->flags)); } else { /* * we allow strides even when we create * the memory, but be careful with this... */ - memcpy(self->strides, strides, sizeof(npy_intp)*nd); + memcpy(fa->strides, strides, sizeof(npy_intp)*nd); sd *= size; } } else { - self->dimensions = self->strides = NULL; - self->flags |= NPY_ARRAY_F_CONTIGUOUS; + fa->dimensions = fa->strides = NULL; + fa->flags |= NPY_ARRAY_F_CONTIGUOUS; } if (data == NULL) { @@ -1182,7 +1184,7 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd, PyErr_NoMemory(); goto fail; } - self->flags |= NPY_ARRAY_OWNDATA; + fa->flags |= NPY_ARRAY_OWNDATA; /* * It is bad to have unitialized OBJECT pointers @@ -1197,16 +1199,16 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd, * If data is passed in, this object won't own it by default. * Caller must arrange for this to be reset if truly desired */ - self->flags &= ~NPY_ARRAY_OWNDATA; + fa->flags &= ~NPY_ARRAY_OWNDATA; } - self->data = data; + fa->data = data; /* * If the strides were provided to the function, need to * update the flags to get the right CONTIGUOUS, ALIGN properties */ if (strides != NULL) { - PyArray_UpdateFlags(self, NPY_ARRAY_UPDATE_ALL); + PyArray_UpdateFlags((PyArrayObject *)fa, NPY_ARRAY_UPDATE_ALL); } /* @@ -1217,14 +1219,14 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd, if ((subtype != &PyArray_Type)) { PyObject *res, *func, *args; - func = PyObject_GetAttrString((PyObject *)self, "__array_finalize__"); + func = PyObject_GetAttrString((PyObject *)fa, "__array_finalize__"); if (func && func != Py_None) { if (NpyCapsule_Check(func)) { /* A C-function is stored here */ PyArray_FinalizeFunc *cfunc; cfunc = NpyCapsule_AsVoidPtr(func); Py_DECREF(func); - if (cfunc(self, obj) < 0) { + if (cfunc((PyArrayObject *)fa, obj) < 0) { goto fail; } } @@ -1248,10 +1250,10 @@ PyArray_NewFromDescr(PyTypeObject *subtype, PyArray_Descr *descr, int nd, } else Py_XDECREF(func); } - return (PyObject *)self; + return (PyObject *)fa; fail: - Py_DECREF(self); + Py_DECREF(fa); return NULL; } @@ -1455,7 +1457,7 @@ _array_from_buffer_3118(PyObject *obj, PyObject **out) r = PyArray_NewFromDescr(&PyArray_Type, descr, nd, shape, strides, view->buf, flags, NULL); - ((PyArrayObject *)r)->base = memoryview; + ((PyArrayObject_fieldaccess *)r)->base = memoryview; PyArray_UpdateFlags((PyArrayObject *)r, NPY_ARRAY_UPDATE_ALL); *out = r; @@ -1593,11 +1595,17 @@ PyArray_GetArrayParamsFromObject(PyObject *op, /* If op supports the __array_struct__ or __array_interface__ interface */ tmp = PyArray_FromStructInterface(op); + if (tmp == NULL) { + return -1; + } if (tmp == Py_NotImplemented) { tmp = PyArray_FromInterface(op); + if (tmp == NULL) { + return -1; + } } if (tmp != Py_NotImplemented) { - if (writeable && !PyArray_ISWRITEABLE(tmp)) { + if (writeable && !PyArray_ISWRITEABLE((PyArrayObject *)tmp)) { PyErr_SetString(PyExc_RuntimeError, "cannot write to array interface object"); Py_DECREF(tmp); @@ -1619,7 +1627,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op, if (!writeable) { tmp = PyArray_FromArrayAttr(op, requested_dtype, context); if (tmp != Py_NotImplemented) { - if (writeable && !PyArray_ISWRITEABLE(tmp)) { + if (writeable && !PyArray_ISWRITEABLE((PyArrayObject *)tmp)) { PyErr_SetString(PyExc_RuntimeError, "cannot write to array interface object"); Py_DECREF(tmp); @@ -1788,7 +1796,7 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth, /* If we got dimensions and dtype instead of an array */ if (arr == NULL) { - if (flags&NPY_ARRAY_UPDATEIFCOPY) { + if (flags & NPY_ARRAY_UPDATEIFCOPY) { Py_XDECREF(newtype); PyErr_SetString(PyExc_TypeError, "UPDATEIFCOPY used for non-array input."); @@ -1937,8 +1945,8 @@ PyArray_CheckFromAny(PyObject *op, PyArray_Descr *descr, int min_depth, PyObject *obj; if (requires & NPY_ARRAY_NOTSWAPPED) { if (!descr && PyArray_Check(op) && - !PyArray_ISNBO(PyArray_DESCR(op)->byteorder)) { - descr = PyArray_DescrNew(PyArray_DESCR(op)); + !PyArray_ISNBO(PyArray_DESCR((PyArrayObject *)op)->byteorder)) { + descr = PyArray_DescrNew(PyArray_DESCR((PyArrayObject *)op)); } else if (descr && !PyArray_ISNBO(descr->byteorder)) { PyArray_DESCR_REPLACE(descr); @@ -1974,8 +1982,8 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) int copy = 0; int arrflags; PyArray_Descr *oldtype; - char *msg = "cannot copy back to a read-only array"; PyTypeObject *subtype; + NPY_CASTING casting = NPY_SAFE_CASTING; oldtype = PyArray_DESCR(arr); subtype = Py_TYPE(arr); @@ -1992,41 +2000,55 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) itemsize = newtype->elsize; } - /* - * Can't cast unless ndim-0 array, NPY_ARRAY_FORCECAST is specified - * or the cast is safe. - */ - if (!(flags & NPY_ARRAY_FORCECAST) && !PyArray_NDIM(arr) == 0 && - !PyArray_CanCastTo(oldtype, newtype)) { + /* If the casting if forced, use the 'unsafe' casting rule */ + if (flags & NPY_ARRAY_FORCECAST) { + casting = NPY_UNSAFE_CASTING; + } + + /* Raise an error if the casting rule isn't followed */ + if (!PyArray_CanCastArrayTo(arr, newtype, casting)) { + PyObject *errmsg; + + errmsg = PyUString_FromString("Cannot cast array data from "); + PyUString_ConcatAndDel(&errmsg, + PyObject_Repr((PyObject *)PyArray_DESCR(arr))); + PyUString_ConcatAndDel(&errmsg, + PyUString_FromString(" to ")); + PyUString_ConcatAndDel(&errmsg, + PyObject_Repr((PyObject *)newtype)); + PyUString_ConcatAndDel(&errmsg, + PyUString_FromFormat(" according to the rule %s", + npy_casting_to_string(casting))); + PyErr_SetObject(PyExc_TypeError, errmsg); + Py_DECREF(newtype); - PyErr_SetString(PyExc_TypeError, - "array cannot be safely cast " \ - "to required type"); return NULL; } /* Don't copy if sizes are compatible */ if ((flags & NPY_ARRAY_ENSURECOPY) || PyArray_EquivTypes(oldtype, newtype)) { - arrflags = arr->flags; - if (arr->nd <= 1 && (flags & NPY_ARRAY_F_CONTIGUOUS)) { + arrflags = PyArray_FLAGS(arr); + if (PyArray_NDIM(arr) <= 1 && (flags & NPY_ARRAY_F_CONTIGUOUS)) { flags |= NPY_ARRAY_C_CONTIGUOUS; } copy = (flags & NPY_ARRAY_ENSURECOPY) || - ((flags & NPY_ARRAY_C_CONTIGUOUS) && (!(arrflags & NPY_ARRAY_C_CONTIGUOUS))) + ((flags & NPY_ARRAY_C_CONTIGUOUS) && + (!(arrflags & NPY_ARRAY_C_CONTIGUOUS))) || ((flags & NPY_ARRAY_ALIGNED) && - (!(arrflags & NPY_ARRAY_ALIGNED))) - || (arr->nd > 1 && - ((flags & NPY_ARRAY_F_CONTIGUOUS) && - (!(arrflags & NPY_ARRAY_F_CONTIGUOUS)))) + (!(arrflags & NPY_ARRAY_ALIGNED))) + || (PyArray_NDIM(arr) > 1 && + ((flags & NPY_ARRAY_F_CONTIGUOUS) && + (!(arrflags & NPY_ARRAY_F_CONTIGUOUS)))) || ((flags & NPY_ARRAY_WRITEABLE) && - (!(arrflags & NPY_ARRAY_WRITEABLE))); + (!(arrflags & NPY_ARRAY_WRITEABLE))); if (copy) { if ((flags & NPY_ARRAY_UPDATEIFCOPY) && - (!PyArray_ISWRITEABLE(arr))) { + (!PyArray_ISWRITEABLE(arr))) { Py_DECREF(newtype); - PyErr_SetString(PyExc_ValueError, msg); + PyErr_SetString(PyExc_ValueError, + "cannot copy back to a read-only array"); return NULL; } if ((flags & NPY_ARRAY_ENSUREARRAY)) { @@ -2034,23 +2056,27 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) } ret = (PyArrayObject *) PyArray_NewFromDescr(subtype, newtype, - arr->nd, - arr->dimensions, + PyArray_NDIM(arr), + PyArray_DIMS(arr), NULL, NULL, flags & NPY_ARRAY_F_CONTIGUOUS, (PyObject *)arr); if (ret == NULL) { return NULL; } - if (PyArray_CopyInto(ret, arr) == -1) { + if (PyArray_CopyInto(ret, arr) < 0) { Py_DECREF(ret); return NULL; } if (flags & NPY_ARRAY_UPDATEIFCOPY) { - ret->flags |= NPY_ARRAY_UPDATEIFCOPY; - ret->base = (PyObject *)arr; - PyArray_FLAGS(ret->base) &= ~NPY_ARRAY_WRITEABLE; + /* + * Don't use PyArray_SetBaseObject, because that compresses + * the chain of bases. + */ Py_INCREF(arr); + ((PyArrayObject_fieldaccess *)ret)->base = (PyObject *)arr; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_UPDATEIFCOPY); + PyArray_CLEARFLAGS(arr, NPY_ARRAY_WRITEABLE); } } /* @@ -2060,20 +2086,25 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) else { Py_DECREF(newtype); if ((flags & NPY_ARRAY_ENSUREARRAY) && - !PyArray_CheckExact(arr)) { - Py_INCREF(arr->descr); + !PyArray_CheckExact(arr)) { + PyArray_Descr *dtype = PyArray_DESCR(arr); + Py_INCREF(dtype); ret = (PyArrayObject *) PyArray_NewFromDescr(&PyArray_Type, - arr->descr, - arr->nd, - arr->dimensions, - arr->strides, - arr->data, - arr->flags,NULL); + dtype, + PyArray_NDIM(arr), + PyArray_DIMS(arr), + PyArray_STRIDES(arr), + PyArray_DATA(arr), + PyArray_FLAGS(arr), + NULL); if (ret == NULL) { return NULL; } - ret->base = (PyObject *)arr; + if (PyArray_SetBaseObject(ret, (PyObject *)arr)) { + Py_DECREF(ret); + return NULL; + } } else { ret = arr; @@ -2088,9 +2119,10 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) */ else { if ((flags & NPY_ARRAY_UPDATEIFCOPY) && - (!PyArray_ISWRITEABLE(arr))) { + (!PyArray_ISWRITEABLE(arr))) { Py_DECREF(newtype); - PyErr_SetString(PyExc_ValueError, msg); + PyErr_SetString(PyExc_ValueError, + "cannot copy back to a read-only array B"); return NULL; } if ((flags & NPY_ARRAY_ENSUREARRAY)) { @@ -2098,7 +2130,7 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) } ret = (PyArrayObject *) PyArray_NewFromDescr(subtype, newtype, - arr->nd, arr->dimensions, + PyArray_NDIM(arr), PyArray_DIMS(arr), NULL, NULL, flags & NPY_ARRAY_F_CONTIGUOUS, (PyObject *)arr); @@ -2110,10 +2142,14 @@ PyArray_FromArray(PyArrayObject *arr, PyArray_Descr *newtype, int flags) return NULL; } if (flags & NPY_ARRAY_UPDATEIFCOPY) { - ret->flags |= NPY_ARRAY_UPDATEIFCOPY; - ret->base = (PyObject *)arr; - PyArray_FLAGS(ret->base) &= ~NPY_ARRAY_WRITEABLE; + /* + * Don't use PyArray_SetBaseObject, because that compresses + * the chain of bases. + */ Py_INCREF(arr); + ((PyArrayObject_fieldaccess *)ret)->base = (PyObject *)arr; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_UPDATEIFCOPY); + PyArray_CLEARFLAGS(arr, NPY_ARRAY_WRITEABLE); } } return (PyObject *)ret; @@ -2126,7 +2162,8 @@ PyArray_FromStructInterface(PyObject *input) PyArray_Descr *thetype = NULL; char buf[40]; PyArrayInterface *inter; - PyObject *attr, *r; + PyObject *attr; + PyArrayObject *ret; char endian = PyArray_NATBYTE; attr = PyObject_GetAttrString(input, "__array_struct__"); @@ -2162,15 +2199,18 @@ PyArray_FromStructInterface(PyObject *input) } } - r = PyArray_NewFromDescr(&PyArray_Type, thetype, + ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, thetype, inter->nd, inter->shape, inter->strides, inter->data, inter->flags, NULL); Py_INCREF(input); - PyArray_BASE(r) = input; + if (PyArray_SetBaseObject(ret, input) < 0) { + Py_DECREF(ret); + return NULL; + } Py_DECREF(attr); - PyArray_UpdateFlags((PyArrayObject *)r, NPY_ARRAY_UPDATE_ALL); - return r; + PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); + return (PyObject *)ret; fail: PyErr_SetString(PyExc_ValueError, "invalid __array_struct__"); @@ -2208,17 +2248,23 @@ PyArray_FromInterface(PyObject *input) } if (!PyDict_Check(inter)) { Py_DECREF(inter); - return Py_NotImplemented; + PyErr_SetString(PyExc_ValueError, + "Invalid __array_interface__ value, must be a dict"); + return NULL; } shape = PyDict_GetItemString(inter, "shape"); if (shape == NULL) { Py_DECREF(inter); - return Py_NotImplemented; + PyErr_SetString(PyExc_ValueError, + "Missing __array_interface__ shape"); + return NULL; } tstr = PyDict_GetItemString(inter, "typestr"); if (tstr == NULL) { Py_DECREF(inter); - return Py_NotImplemented; + PyErr_SetString(PyExc_ValueError, + "Missing __array_interface__ typestr"); + return NULL; } attr = PyDict_GetItemString(inter, "data"); @@ -2234,7 +2280,7 @@ PyArray_FromInterface(PyObject *input) if (res < 0) { PyErr_Clear(); res = PyObject_AsReadBuffer( - item, (const void **)&data, &buffer_len); + item, (const void **)&data, &buffer_len); if (res < 0) { goto fail; } @@ -2245,7 +2291,7 @@ PyArray_FromInterface(PyObject *input) longlong num = PyLong_AsLongLong(attr); if (error_converting(num)) { PyErr_SetString(PyExc_TypeError, - "offset "\ + "__array_interface__ offset " "must be an integer"); goto fail; } @@ -2257,7 +2303,7 @@ PyArray_FromInterface(PyObject *input) PyObject *dataptr; if (PyTuple_GET_SIZE(attr) != 2) { PyErr_SetString(PyExc_TypeError, - "data must return " \ + "__array_interface__ data must be " \ "a 2-tuple with (data pointer "\ "integer, read-only flag)"); goto fail; @@ -2268,7 +2314,7 @@ PyArray_FromInterface(PyObject *input) "%p", (void **)&data); if (res < 1) { PyErr_SetString(PyExc_TypeError, - "data string cannot be " \ + "__array_interface__ data string cannot be " \ "converted"); goto fail; } @@ -2277,9 +2323,9 @@ PyArray_FromInterface(PyObject *input) data = PyLong_AsVoidPtr(dataptr); } else { - PyErr_SetString(PyExc_TypeError, "first element " \ - "of data tuple must be integer" \ - " or string."); + PyErr_SetString(PyExc_TypeError, "first element " + "of __array_interface__ data tuple " + "must be integer or string."); goto fail; } if (PyObject_IsTrue(PyTuple_GET_ITEM(attr,1))) { @@ -2294,7 +2340,8 @@ PyArray_FromInterface(PyObject *input) } #endif if (!PyBytes_Check(attr)) { - PyErr_SetString(PyExc_TypeError, "typestr must be a string"); + PyErr_SetString(PyExc_TypeError, + "__array_interface__ typestr must be a string"); goto fail; } type = _array_typedescr_fromstr(PyString_AS_STRING(attr)); @@ -2329,7 +2376,10 @@ PyArray_FromInterface(PyObject *input) return NULL; } Py_INCREF(base); - ret->base = base; + if (PyArray_SetBaseObject(ret, base) < 0) { + Py_DECREF(ret); + return NULL; + } attr = PyDict_GetItemString(inter, "strides"); if (attr != NULL && attr != Py_None) { @@ -2356,7 +2406,7 @@ PyArray_FromInterface(PyObject *input) if (PyErr_Occurred()) { PyErr_Clear(); } - memcpy(ret->strides, strides, n*sizeof(npy_intp)); + memcpy(PyArray_STRIDES(ret), strides, n*sizeof(npy_intp)); } else PyErr_Clear(); PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); @@ -2467,13 +2517,13 @@ PyArray_FromDimsAndDataAndDescr(int nd, int *d, NPY_NO_EXPORT PyObject * PyArray_FromDims(int nd, int *d, int type) { - PyObject *ret; + PyArrayObject *ret; char msg[] = "PyArray_FromDims: use PyArray_SimpleNew."; if (DEPRECATE(msg) < 0) { return NULL; } - ret = PyArray_FromDimsAndDataAndDescr(nd, d, + ret = (PyArrayObject *)PyArray_FromDimsAndDataAndDescr(nd, d, PyArray_DescrFromType(type), NULL); /* @@ -2481,10 +2531,10 @@ PyArray_FromDims(int nd, int *d, int type) * relied on that. Better keep it the same. If * Object type, then it's already been set to zero, though. */ - if (ret && (PyArray_DESCR(ret)->type_num != PyArray_OBJECT)) { + if (ret && (PyArray_DESCR(ret)->type_num != NPY_OBJECT)) { memset(PyArray_DATA(ret), 0, PyArray_NBYTES(ret)); } - return ret; + return (PyObject *)ret; } /* end old calls */ @@ -2531,7 +2581,7 @@ PyArray_EnsureAnyArray(PyObject *op) /* TODO: Put the order parameter in PyArray_CopyAnyInto and remove this */ NPY_NO_EXPORT int -PyArray_CopyAnyIntoOrdered(PyArrayObject *dst, PyArrayObject *src, +PyArray_CopyAsFlat(PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order) { PyArray_StridedTransferFn *stransfer = NULL; @@ -2571,9 +2621,9 @@ PyArray_CopyAnyIntoOrdered(PyArrayObject *dst, PyArrayObject *src, dst_size = PyArray_SIZE(dst); src_size = PyArray_SIZE(src); if (dst_size != src_size) { - PyErr_SetString(PyExc_ValueError, - "arrays must have the same number of elements" - " for copy"); + PyErr_Format(PyExc_ValueError, + "cannot copy from array of size %d into an array " + "of size %d", (int)src_size, (int)dst_size); return -1; } @@ -2724,7 +2774,7 @@ PyArray_CopyAnyIntoOrdered(PyArrayObject *dst, PyArrayObject *src, NPY_NO_EXPORT int PyArray_CopyAnyInto(PyArrayObject *dst, PyArrayObject *src) { - return PyArray_CopyAnyIntoOrdered(dst, src, NPY_CORDER); + return PyArray_CopyAsFlat(dst, src, NPY_CORDER); } /*NUMPY_API @@ -3121,7 +3171,7 @@ NPY_NO_EXPORT PyObject * PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags) { PyObject *temp1, *temp2; - int n = arr->nd; + int n = PyArray_NDIM(arr); if (*axis == MAX_DIMS || n == 0) { if (n != 1) { @@ -3131,7 +3181,7 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags) return NULL; } if (*axis == MAX_DIMS) { - *axis = PyArray_NDIM(temp1)-1; + *axis = PyArray_NDIM((PyArrayObject *)temp1)-1; } } else { @@ -3158,7 +3208,7 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags) else { temp2 = (PyObject *)temp1; } - n = PyArray_NDIM(temp2); + n = PyArray_NDIM((PyArrayObject *)temp2); if (*axis < 0) { *axis += n; } @@ -3256,7 +3306,7 @@ NPY_NO_EXPORT PyObject * PyArray_Arange(double start, double stop, double step, int type_num) { npy_intp length; - PyObject *range; + PyArrayObject *range; PyArray_ArrFuncs *funcs; PyObject *obj; int ret; @@ -3271,7 +3321,7 @@ PyArray_Arange(double start, double stop, double step, int type_num) return PyArray_New(&PyArray_Type, 1, &length, type_num, NULL, NULL, 0, 0, NULL); } - range = PyArray_New(&PyArray_Type, 1, &length, type_num, + range = (PyArrayObject *)PyArray_New(&PyArray_Type, 1, &length, type_num, NULL, NULL, 0, 0, NULL); if (range == NULL) { return NULL; @@ -3283,34 +3333,34 @@ PyArray_Arange(double start, double stop, double step, int type_num) * if length > 2, then call the inner loop, otherwise stop */ obj = PyFloat_FromDouble(start); - ret = funcs->setitem(obj, PyArray_DATA(range), (PyArrayObject *)range); + ret = funcs->setitem(obj, PyArray_DATA(range), range); Py_DECREF(obj); if (ret < 0) { goto fail; } if (length == 1) { - return range; + return (PyObject *)range; } obj = PyFloat_FromDouble(start + step); ret = funcs->setitem(obj, PyArray_BYTES(range)+PyArray_ITEMSIZE(range), - (PyArrayObject *)range); + range); Py_DECREF(obj); if (ret < 0) { goto fail; } if (length == 2) { - return range; + return (PyObject *)range; } if (!funcs->fill) { PyErr_SetString(PyExc_ValueError, "no fill-function for data-type."); Py_DECREF(range); return NULL; } - funcs->fill(PyArray_DATA(range), length, (PyArrayObject *)range); + funcs->fill(PyArray_DATA(range), length, range); if (PyErr_Occurred()) { goto fail; } - return range; + return (PyObject *)range; fail: Py_DECREF(range); @@ -3397,7 +3447,7 @@ _calc_length(PyObject *start, PyObject *stop, PyObject *step, PyObject **next, i NPY_NO_EXPORT PyObject * PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr *dtype) { - PyObject *range; + PyArrayObject *range; PyArray_ArrFuncs *funcs; PyObject *next, *err; npy_intp length; @@ -3462,10 +3512,10 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr } if (length <= 0) { length = 0; - range = PyArray_SimpleNewFromDescr(1, &length, dtype); + range = (PyArrayObject *)PyArray_SimpleNewFromDescr(1, &length, dtype); Py_DECREF(step); Py_DECREF(start); - return range; + return (PyObject *)range; } /* @@ -3481,7 +3531,7 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr swap = 0; } - range = PyArray_SimpleNewFromDescr(1, &length, native); + range = (PyArrayObject *)PyArray_SimpleNewFromDescr(1, &length, native); if (range == NULL) { goto fail; } @@ -3491,15 +3541,14 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr * if length > 2, then call the inner loop, otherwise stop */ funcs = PyArray_DESCR(range)->f; - if (funcs->setitem( - start, PyArray_DATA(range), (PyArrayObject *)range) < 0) { + if (funcs->setitem(start, PyArray_DATA(range), range) < 0) { goto fail; } if (length == 1) { goto finish; } if (funcs->setitem(next, PyArray_BYTES(range)+PyArray_ITEMSIZE(range), - (PyArrayObject *)range) < 0) { + range) < 0) { goto fail; } if (length == 2) { @@ -3510,22 +3559,24 @@ PyArray_ArangeObj(PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr Py_DECREF(range); goto fail; } - funcs->fill(PyArray_DATA(range), length, (PyArrayObject *)range); + funcs->fill(PyArray_DATA(range), length, range); if (PyErr_Occurred()) { goto fail; } finish: + /* TODO: This swapping could be handled on the fly by the nditer */ if (swap) { PyObject *new; - new = PyArray_Byteswap((PyArrayObject *)range, 1); + new = PyArray_Byteswap(range, 1); Py_DECREF(new); Py_DECREF(PyArray_DESCR(range)); - PyArray_DESCR(range) = dtype; /* steals the reference */ + /* steals the reference */ + ((PyArrayObject_fieldaccess *)range)->descr = dtype; } Py_DECREF(start); Py_DECREF(step); Py_DECREF(next); - return range; + return (PyObject *)range; fail: Py_DECREF(start); @@ -3594,7 +3645,7 @@ array_fromfile_binary(FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nrea return NULL; } NPY_BEGIN_ALLOW_THREADS; - *nread = fread(r->data, dtype->elsize, num, fp); + *nread = fread(PyArray_DATA(r), dtype->elsize, num, fp); NPY_END_ALLOW_THREADS; return r; } @@ -3630,7 +3681,7 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread, clean_sep = swab_separator(sep); NPY_BEGIN_ALLOW_THREADS; totalbytes = bytes = size * dtype->elsize; - dptr = r->data; + dptr = PyArray_DATA(r); for (i= 0; num < 0 || i < num; i++) { if (next(&stream, dptr, dtype, stream_data) < 0) { break; @@ -3640,12 +3691,12 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread, dptr += dtype->elsize; if (num < 0 && thisbuf == size) { totalbytes += bytes; - tmp = PyDataMem_RENEW(r->data, totalbytes); + tmp = PyDataMem_RENEW(PyArray_DATA(r), totalbytes); if (tmp == NULL) { err = 1; break; } - r->data = tmp; + ((PyArrayObject_fieldaccess *)r)->data = tmp; dptr = tmp + (totalbytes - bytes); thisbuf = 0; } @@ -3654,13 +3705,13 @@ array_from_text(PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread, } } if (num < 0) { - tmp = PyDataMem_RENEW(r->data, NPY_MAX(*nread,1)*dtype->elsize); + tmp = PyDataMem_RENEW(PyArray_DATA(r), NPY_MAX(*nread,1)*dtype->elsize); if (tmp == NULL) { err = 1; } else { - PyArray_DIM(r,0) = *nread; - r->data = tmp; + PyArray_DIMS(r)[0] = *nread; + ((PyArrayObject_fieldaccess *)r)->data = tmp; } } NPY_END_ALLOW_THREADS; @@ -3733,15 +3784,15 @@ PyArray_FromFile(FILE *fp, PyArray_Descr *dtype, npy_intp num, char *sep) } if (((npy_intp) nread) < num) { /* Realloc memory for smaller number of elements */ - const size_t nsize = NPY_MAX(nread,1)*ret->descr->elsize; + const size_t nsize = NPY_MAX(nread,1)*PyArray_DESCR(ret)->elsize; char *tmp; - if((tmp = PyDataMem_RENEW(ret->data, nsize)) == NULL) { + if((tmp = PyDataMem_RENEW(PyArray_DATA(ret), nsize)) == NULL) { Py_DECREF(ret); return PyErr_NoMemory(); } - ret->data = tmp; - PyArray_DIM(ret,0) = nread; + ((PyArrayObject_fieldaccess *)ret)->data = tmp; + PyArray_DIMS(ret)[0] = nread; } return (PyObject *)ret; } @@ -3848,10 +3899,13 @@ PyArray_FromBuffer(PyObject *buf, PyArray_Descr *type, } if (!writeable) { - ret->flags &= ~NPY_ARRAY_WRITEABLE; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_WRITEABLE); } /* Store a reference for decref on deallocation */ - ret->base = buf; + if (PyArray_SetBaseObject(ret, buf) < 0) { + Py_DECREF(ret); + return NULL; + } PyArray_UpdateFlags(ret, NPY_ARRAY_ALIGNED); return (PyObject *)ret; } @@ -3931,7 +3985,7 @@ PyArray_FromString(char *data, npy_intp slen, PyArray_Descr *dtype, if (ret == NULL) { return NULL; } - memcpy(ret->data, data, num*dtype->elsize); + memcpy(PyArray_DATA(ret), data, num*dtype->elsize); } else { /* read from character-based string */ @@ -4004,13 +4058,13 @@ PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count) (value = PyIter_Next(iter)); i++) { if (i >= elcount) { /* - Grow ret->data: + Grow PyArray_DATA(ret): this is similar for the strategy for PyListObject, but we use 50% overallocation => 0, 4, 8, 14, 23, 36, 56, 86 ... */ elcount = (i >> 1) + (i < 4 ? 4 : 2) + i; if (elcount <= NPY_MAX_INTP/elsize) { - new_data = PyDataMem_RENEW(ret->data, elcount * elsize); + new_data = PyDataMem_RENEW(PyArray_DATA(ret), elcount * elsize); } else { new_data = NULL; @@ -4021,12 +4075,12 @@ PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count) Py_DECREF(value); goto done; } - ret->data = new_data; + ((PyArrayObject_fieldaccess *)ret)->data = new_data; } - ret->dimensions[0] = i + 1; + PyArray_DIMS(ret)[0] = i + 1; if (((item = index2ptr(ret, i)) == NULL) - || (ret->descr->f->setitem(value, item, ret) == -1)) { + || (PyArray_DESCR(ret)->f->setitem(value, item, ret) == -1)) { Py_DECREF(value); goto done; } @@ -4045,12 +4099,12 @@ PyArray_FromIter(PyObject *obj, PyArray_Descr *dtype, npy_intp count) if (i == 0) { i = 1; } - new_data = PyDataMem_RENEW(ret->data, i * elsize); + new_data = PyDataMem_RENEW(PyArray_DATA(ret), i * elsize); if (new_data == NULL) { PyErr_SetString(PyExc_MemoryError, "cannot allocate array memory"); goto done; } - ret->data = new_data; + ((PyArrayObject_fieldaccess *)ret)->data = new_data; done: Py_XDECREF(iter); diff --git a/numpy/core/src/multiarray/ctors.h b/numpy/core/src/multiarray/ctors.h index 13f5d0da6..ed7b72980 100644 --- a/numpy/core/src/multiarray/ctors.h +++ b/numpy/core/src/multiarray/ctors.h @@ -47,7 +47,7 @@ PyArray_CheckAxis(PyArrayObject *arr, int *axis, int flags); /* TODO: Put the order parameter in PyArray_CopyAnyInto and remove this */ NPY_NO_EXPORT int -PyArray_CopyAnyIntoOrdered(PyArrayObject *dst, PyArrayObject *src, +PyArray_CopyAsFlat(PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order); /* FIXME: remove those from here */ diff --git a/numpy/core/src/multiarray/datetime.c b/numpy/core/src/multiarray/datetime.c index 7a0bafbf2..cc4fc9d63 100644 --- a/numpy/core/src/multiarray/datetime.c +++ b/numpy/core/src/multiarray/datetime.c @@ -2573,23 +2573,24 @@ convert_pyobject_to_datetime(PyArray_DatetimeMetaData *meta, PyObject *obj, } /* Datetime zero-dimensional array */ else if (PyArray_Check(obj) && - PyArray_NDIM(obj) == 0 && - PyArray_DESCR(obj)->type_num == NPY_DATETIME) { - PyArray_DatetimeMetaData *obj_meta; + PyArray_NDIM((PyArrayObject *)obj) == 0 && + PyArray_DESCR((PyArrayObject *)obj)->type_num == NPY_DATETIME) { + PyArrayObject *arr = (PyArrayObject *)obj; + PyArray_DatetimeMetaData *arr_meta; npy_datetime dt = 0; - obj_meta = get_datetime_metadata_from_dtype(PyArray_DESCR(obj)); - if (obj_meta == NULL) { + arr_meta = get_datetime_metadata_from_dtype(PyArray_DESCR(arr)); + if (arr_meta == NULL) { return -1; } - PyArray_DESCR(obj)->f->copyswap(&dt, - PyArray_DATA(obj), - !PyArray_ISNOTSWAPPED(obj), - obj); + PyArray_DESCR(arr)->f->copyswap(&dt, + PyArray_DATA(arr), + !PyArray_ISNOTSWAPPED(arr), + obj); /* Copy the value directly if units weren't specified */ if (meta->base == -1) { - *meta = *obj_meta; + *meta = *arr_meta; *out = dt; return 0; @@ -2600,11 +2601,11 @@ convert_pyobject_to_datetime(PyArray_DatetimeMetaData *meta, PyObject *obj, if (dt != NPY_DATETIME_NAT && raise_if_datetime64_metadata_cast_error( "NumPy timedelta64 scalar", - obj_meta, meta, casting) < 0) { + arr_meta, meta, casting) < 0) { return -1; } else { - return cast_datetime_to_datetime(obj_meta, meta, dt, out); + return cast_datetime_to_datetime(arr_meta, meta, dt, out); } } } @@ -2769,23 +2770,24 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj, } /* Timedelta zero-dimensional array */ else if (PyArray_Check(obj) && - PyArray_NDIM(obj) == 0 && - PyArray_DESCR(obj)->type_num == NPY_TIMEDELTA) { - PyArray_DatetimeMetaData *obj_meta; + PyArray_NDIM((PyArrayObject *)obj) == 0 && + PyArray_DESCR((PyArrayObject *)obj)->type_num == NPY_TIMEDELTA) { + PyArrayObject *arr = (PyArrayObject *)obj; + PyArray_DatetimeMetaData *arr_meta; npy_timedelta dt = 0; - obj_meta = get_datetime_metadata_from_dtype(PyArray_DESCR(obj)); - if (obj_meta == NULL) { + arr_meta = get_datetime_metadata_from_dtype(PyArray_DESCR(arr)); + if (arr_meta == NULL) { return -1; } - PyArray_DESCR(obj)->f->copyswap(&dt, - PyArray_DATA(obj), - !PyArray_ISNOTSWAPPED(obj), - obj); + PyArray_DESCR(arr)->f->copyswap(&dt, + PyArray_DATA(arr), + !PyArray_ISNOTSWAPPED(arr), + obj); /* Copy the value directly if units weren't specified */ if (meta->base == -1) { - *meta = *obj_meta; + *meta = *arr_meta; *out = dt; return 0; @@ -2796,11 +2798,11 @@ convert_pyobject_to_timedelta(PyArray_DatetimeMetaData *meta, PyObject *obj, if (dt != NPY_DATETIME_NAT && raise_if_timedelta64_metadata_cast_error( "NumPy timedelta64 scalar", - obj_meta, meta, casting) < 0) { + arr_meta, meta, casting) < 0) { return -1; } else { - return cast_timedelta_to_timedelta(obj_meta, meta, dt, out); + return cast_timedelta_to_timedelta(arr_meta, meta, dt, out); } } } @@ -3184,7 +3186,8 @@ is_any_numpy_datetime(PyObject *obj) { return (PyArray_IsScalar(obj, Datetime) || (PyArray_Check(obj) && ( - PyArray_DESCR(obj)->type_num == NPY_DATETIME)) || + PyArray_DESCR((PyArrayObject *)obj)->type_num == + NPY_DATETIME)) || PyDate_Check(obj) || PyDateTime_Check(obj)); } @@ -3197,9 +3200,9 @@ static npy_bool is_any_numpy_timedelta(PyObject *obj) { return (PyArray_IsScalar(obj, Timedelta) || - (PyArray_Check(obj) && ( - PyArray_DESCR(obj)->type_num == NPY_TIMEDELTA)) || - PyDelta_Check(obj)); + (PyArray_Check(obj) && ( + PyArray_DESCR((PyArrayObject *)obj)->type_num == NPY_TIMEDELTA)) || + PyDelta_Check(obj)); } /* @@ -3550,7 +3553,7 @@ datetime_arange(PyObject *start, PyObject *stop, PyObject *step, * Returns 0 on success, -1 on failure. */ static int -find_string_array_datetime64_type(PyObject *obj, +find_string_array_datetime64_type(PyArrayObject *arr, PyArray_DatetimeMetaData *meta) { NpyIter* iter; @@ -3565,7 +3568,7 @@ find_string_array_datetime64_type(PyObject *obj, PyArray_DatetimeMetaData tmp_meta; /* Handle zero-sized arrays specially */ - if (PyArray_SIZE(obj) == 0) { + if (PyArray_SIZE(arr) == 0) { return 0; } @@ -3575,7 +3578,7 @@ find_string_array_datetime64_type(PyObject *obj, } /* Use unsafe casting to allow unicode -> ascii string */ - iter = NpyIter_New((PyArrayObject *)obj, + iter = NpyIter_New((PyArrayObject *)arr, NPY_ITER_READONLY| NPY_ITER_EXTERNAL_LOOP| NPY_ITER_BUFFERED, @@ -3681,19 +3684,20 @@ recursive_find_object_datetime64_type(PyObject *obj, { /* Array -> use its metadata */ if (PyArray_Check(obj)) { - PyArray_Descr *obj_dtype = PyArray_DESCR(obj); + PyArrayObject *arr = (PyArrayObject *)obj; + PyArray_Descr *arr_dtype = PyArray_DESCR(arr); - if (obj_dtype->type_num == NPY_STRING || - obj_dtype->type_num == NPY_UNICODE) { - return find_string_array_datetime64_type(obj, meta); + if (arr_dtype->type_num == NPY_STRING || + arr_dtype->type_num == NPY_UNICODE) { + return find_string_array_datetime64_type(arr, meta); } /* If the array has metadata, use it */ - else if (obj_dtype->type_num == NPY_DATETIME || - obj_dtype->type_num == NPY_TIMEDELTA) { + else if (arr_dtype->type_num == NPY_DATETIME || + arr_dtype->type_num == NPY_TIMEDELTA) { PyArray_DatetimeMetaData *tmp_meta; /* Get the metadata from the type */ - tmp_meta = get_datetime_metadata_from_dtype(obj_dtype); + tmp_meta = get_datetime_metadata_from_dtype(arr_dtype); if (tmp_meta == NULL) { return -1; } @@ -3707,7 +3711,7 @@ recursive_find_object_datetime64_type(PyObject *obj, return 0; } /* If it's not an object array, stop looking */ - else if (obj_dtype->type_num != NPY_OBJECT) { + else if (arr_dtype->type_num != NPY_OBJECT) { return 0; } } @@ -3827,15 +3831,16 @@ recursive_find_object_timedelta64_type(PyObject *obj, { /* Array -> use its metadata */ if (PyArray_Check(obj)) { - PyArray_Descr *obj_dtype = PyArray_DESCR(obj); + PyArrayObject *arr = (PyArrayObject *)obj; + PyArray_Descr *arr_dtype = PyArray_DESCR(arr); /* If the array has metadata, use it */ - if (obj_dtype->type_num == NPY_DATETIME || - obj_dtype->type_num == NPY_TIMEDELTA) { + if (arr_dtype->type_num == NPY_DATETIME || + arr_dtype->type_num == NPY_TIMEDELTA) { PyArray_DatetimeMetaData *tmp_meta; /* Get the metadata from the type */ - tmp_meta = get_datetime_metadata_from_dtype(obj_dtype); + tmp_meta = get_datetime_metadata_from_dtype(arr_dtype); if (tmp_meta == NULL) { return -1; } @@ -3849,7 +3854,7 @@ recursive_find_object_timedelta64_type(PyObject *obj, return 0; } /* If it's not an object array, stop looking */ - else if (obj_dtype->type_num != NPY_OBJECT) { + else if (arr_dtype->type_num != NPY_OBJECT) { return 0; } } diff --git a/numpy/core/src/multiarray/datetime_busdaycal.c b/numpy/core/src/multiarray/datetime_busdaycal.c index 018912ee8..1d047a547 100644 --- a/numpy/core/src/multiarray/datetime_busdaycal.c +++ b/numpy/core/src/multiarray/datetime_busdaycal.c @@ -149,7 +149,8 @@ invalid_weekmask_string: /* Something like [1,1,1,1,1,0,0] */ else if (PySequence_Check(obj)) { if (PySequence_Size(obj) != 7 || - (PyArray_Check(obj) && PyArray_NDIM(obj) != 1)) { + (PyArray_Check(obj) && + PyArray_NDIM((PyArrayObject *)obj) != 1)) { PyErr_SetString(PyExc_ValueError, "A business day weekmask array must have length 7"); Py_DECREF(obj); diff --git a/numpy/core/src/multiarray/descriptor.c b/numpy/core/src/multiarray/descriptor.c index 8f3038bf2..4f832bd12 100644 --- a/numpy/core/src/multiarray/descriptor.c +++ b/numpy/core/src/multiarray/descriptor.c @@ -599,7 +599,7 @@ _convert_from_commastring(PyObject *obj, int align) } listobj = PyObject_CallMethod(_numpy_internal, "_commastring", "O", obj); Py_DECREF(_numpy_internal); - if (!listobj) { + if (listobj == NULL) { return NULL; } if (!PyList_Check(listobj) || PyList_GET_SIZE(listobj) < 1) { @@ -851,10 +851,28 @@ _convert_from_dict(PyObject *obj, int align) || (offsets && (n > PyObject_Length(offsets))) || (titles && (n > PyObject_Length(titles)))) { PyErr_SetString(PyExc_ValueError, - "all items in the dictionary must have the same length."); + "'names', 'formats', 'offsets', and 'titles' dicct " + "entries must have the same length"); goto fail; } + /* + * If a property 'aligned' is in the dict, it overrides the align flag + * to be True if it not already true. + */ + tmp = PyDict_GetItemString(obj, "aligned"); + if (tmp != NULL) { + if (tmp == Py_True) { + align = 1; + } + else if (tmp != Py_False) { + PyErr_SetString(PyExc_ValueError, + "NumPy dtype descriptor includes 'aligned' entry, " + "but its value is neither True nor False"); + return NULL; + } + } + totalsize = 0; for (i = 0; i < n; i++) { PyObject *tup, *descr, *index, *title, *name, *off; @@ -2748,14 +2766,14 @@ arraydescr_struct_list_str(PyArray_Descr *dtype) /* Special case subarray handling here */ if (PyDataType_HASSUBARRAY(fld_dtype)) { tmp = arraydescr_short_construction_repr( - fld_dtype->subarray->base); + fld_dtype->subarray->base, 0); PyUString_ConcatAndDel(&ret, tmp); PyUString_ConcatAndDel(&ret, PyUString_FromString(", ")); PyUString_ConcatAndDel(&ret, PyObject_Str(fld_dtype->subarray->shape)); } else { - tmp = arraydescr_short_construction_repr(fld_dtype); + tmp = arraydescr_short_construction_repr(fld_dtype, 0); PyUString_ConcatAndDel(&ret, tmp); } PyUString_ConcatAndDel(&ret, PyUString_FromString(")")); @@ -2773,7 +2791,7 @@ arraydescr_struct_list_str(PyArray_Descr *dtype) * in a dict format. */ static PyObject * -arraydescr_struct_dict_str(PyArray_Descr *dtype) +arraydescr_struct_dict_str(PyArray_Descr *dtype, int includealignedflag) { PyObject *names, *key, *fields, *ret, *tmp, *tup, *title; Py_ssize_t i, names_size; @@ -2813,7 +2831,7 @@ arraydescr_struct_dict_str(PyArray_Descr *dtype) if (title != NULL && title != Py_None) { has_titles = 1; } - tmp = arraydescr_short_construction_repr(fld_dtype); + tmp = arraydescr_short_construction_repr(fld_dtype, 0); PyUString_ConcatAndDel(&ret, tmp); if (i != names_size - 1) { PyUString_ConcatAndDel(&ret, PyUString_FromString(",")); @@ -2857,22 +2875,36 @@ arraydescr_struct_dict_str(PyArray_Descr *dtype) } } } - /* Finally, the itemsize */ - PyUString_ConcatAndDel(&ret, - PyUString_FromFormat("], 'itemsize':%d}", (int)dtype->elsize)); + if (includealignedflag && (dtype->flags&NPY_ALIGNED_STRUCT)) { + /* Finally, the itemsize/itemsize and aligned flag */ + PyUString_ConcatAndDel(&ret, + PyUString_FromFormat("], 'itemsize':%d, 'aligned':True}", + (int)dtype->elsize)); + } + else { + /* Finally, the itemsize/itemsize*/ + PyUString_ConcatAndDel(&ret, + PyUString_FromFormat("], 'itemsize':%d}", (int)dtype->elsize)); + } return ret; } /* Produces a string representation for a structured dtype */ static PyObject * -arraydescr_struct_str(PyArray_Descr *dtype) +arraydescr_struct_str(PyArray_Descr *dtype, int includealignflag) { - if (is_dtype_struct_simple_unaligned_layout(dtype)) { + /* + * The list str representation can't include the 'align=' flag, + * so if it is requested and the struct has the aligned flag set, + * we must use the dict str instead. + */ + if (!(includealignflag && (dtype->flags&NPY_ALIGNED_STRUCT)) && + is_dtype_struct_simple_unaligned_layout(dtype)) { return arraydescr_struct_list_str(dtype); } else { - return arraydescr_struct_dict_str(dtype); + return arraydescr_struct_dict_str(dtype, includealignflag); } } @@ -2883,7 +2915,7 @@ arraydescr_subarray_str(PyArray_Descr *dtype) PyObject *p, *ret; ret = PyUString_FromString("("); - p = arraydescr_short_construction_repr(dtype->subarray->base); + p = arraydescr_short_construction_repr(dtype->subarray->base, 0); PyUString_ConcatAndDel(&ret, p); PyUString_ConcatAndDel(&ret, PyUString_FromString(", ")); PyUString_ConcatAndDel(&ret, PyObject_Str(dtype->subarray->shape)); @@ -2898,7 +2930,7 @@ arraydescr_str(PyArray_Descr *dtype) PyObject *sub; if (PyDataType_HASFIELDS(dtype)) { - sub = arraydescr_struct_str(dtype); + sub = arraydescr_struct_str(dtype, 1); } else if (PyDataType_HASSUBARRAY(dtype)) { sub = arraydescr_subarray_str(dtype); @@ -2921,7 +2953,7 @@ arraydescr_struct_repr(PyArray_Descr *dtype) PyObject *sub, *s; s = PyUString_FromString("dtype("); - sub = arraydescr_struct_str(dtype); + sub = arraydescr_struct_str(dtype, 0); if (sub == NULL) { return NULL; } @@ -2944,20 +2976,26 @@ arraydescr_struct_repr(PyArray_Descr *dtype) * additional constructor parameters are given, will reproduce * the exact memory layout. * - * This does not preserve the 'align=True' parameter or sticky - * NPY_ALIGNED_STRUCT flag for struct arrays like the regular - * repr does, because the 'align' flag is not part of first - * dtype constructor parameter. + * If 'includealignflag' is true, this includes the 'align=True' parameter + * inside the struct dtype construction dict when needed. Use this flag + * if you want a proper repr string without the 'dtype()' part around it. + * + * If 'includealignflag' is false, this does not preserve the + * 'align=True' parameter or sticky NPY_ALIGNED_STRUCT flag for + * struct arrays like the regular repr does, because the 'align' + * flag is not part of first dtype constructor parameter. This + * mode is intended for a full 'repr', where the 'align=True' is + * provided as the second parameter. */ NPY_NO_EXPORT PyObject * -arraydescr_short_construction_repr(PyArray_Descr *dtype) +arraydescr_short_construction_repr(PyArray_Descr *dtype, int includealignflag) { PyObject *ret; PyArray_DatetimeMetaData *meta; char byteorder[2]; if (PyDataType_HASFIELDS(dtype)) { - return arraydescr_struct_str(dtype); + return arraydescr_struct_str(dtype, includealignflag); } else if (PyDataType_HASSUBARRAY(dtype)) { return arraydescr_subarray_str(dtype); @@ -3037,16 +3075,20 @@ arraydescr_short_construction_repr(PyArray_Descr *dtype) if (meta == NULL) { return NULL; } - ret = PyUString_FromFormat("%sM8", byteorder); - return append_metastr_to_string(meta, 0, ret); + ret = PyUString_FromFormat("'%sM8", byteorder); + ret = append_metastr_to_string(meta, 0, ret); + PyUString_ConcatAndDel(&ret, PyUString_FromString("'")); + return ret; case NPY_TIMEDELTA: meta = get_datetime_metadata_from_dtype(dtype); if (meta == NULL) { return NULL; } - ret = PyUString_FromFormat("%sm8", byteorder); - return append_metastr_to_string(meta, 0, ret); + ret = PyUString_FromFormat("'%sm8", byteorder); + ret = append_metastr_to_string(meta, 0, ret); + PyUString_ConcatAndDel(&ret, PyUString_FromString("'")); + return ret; default: PyErr_SetString(PyExc_RuntimeError, "Internal error: NumPy dtype " diff --git a/numpy/core/src/multiarray/descriptor.h b/numpy/core/src/multiarray/descriptor.h index 4f8a90582..d936d0b31 100644 --- a/numpy/core/src/multiarray/descriptor.h +++ b/numpy/core/src/multiarray/descriptor.h @@ -11,16 +11,25 @@ NPY_NO_EXPORT PyArray_Descr * _arraydescr_fromobj(PyObject *obj); /* - * This creates a shorter repr using the 'kind' and 'itemsize', - * instead of the longer type name. It also creates the input - * for constructing a dtype rather than the full dtype function - * call. + * This creates a shorter repr using 'kind' and 'itemsize', + * instead of the longer type name. This is the object passed + * as the first parameter to the dtype constructor, and if no + * additional constructor parameters are given, will reproduce + * the exact memory layout. * - * This does not preserve the 'align=True' parameter - * for structured arrays like the regular repr does. + * If 'includealignflag' is true, this includes the 'align=True' parameter + * inside the struct dtype construction dict when needed. Use this flag + * if you want a proper repr string without the 'dtype()' part around it. + * + * If 'includealignflag' is false, this does not preserve the + * 'align=True' parameter or sticky NPY_ALIGNED_STRUCT flag for + * struct arrays like the regular repr does, because the 'align' + * flag is not part of first dtype constructor parameter. This + * mode is intended for a full 'repr', where the 'align=True' is + * provided as the second parameter. */ NPY_NO_EXPORT PyObject * -arraydescr_short_construction_repr(PyArray_Descr *dtype); +arraydescr_short_construction_repr(PyArray_Descr *dtype, int includealignflag); #ifdef NPY_ENABLE_SEPARATE_COMPILATION extern NPY_NO_EXPORT char *_datetime_strings[]; diff --git a/numpy/core/src/multiarray/einsum.c.src b/numpy/core/src/multiarray/einsum.c.src index 9120ef59a..6447e9843 100644 --- a/numpy/core/src/multiarray/einsum.c.src +++ b/numpy/core/src/multiarray/einsum.c.src @@ -2207,7 +2207,11 @@ get_single_op_view(PyArrayObject *op, int iop, char *labels, NPY_ARRAY_ALIGNED| NPY_ARRAY_F_CONTIGUOUS); Py_INCREF(op); - PyArray_BASE(*ret) = (PyObject *)op; + if (PyArray_SetBaseObject(*ret, (PyObject *)op) < 0) { + Py_DECREF(*ret); + *ret = NULL; + return 0; + } return 1; } @@ -2308,7 +2312,10 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels) NPY_ARRAY_ALIGNED| NPY_ARRAY_F_CONTIGUOUS); Py_INCREF(op); - PyArray_BASE(ret) = (PyObject *)op; + if (PyArray_SetBaseObject(ret, (PyObject *)op) < 0) { + Py_DECREF(ret); + return NULL; + } return ret; } diff --git a/numpy/core/src/multiarray/flagsobject.c b/numpy/core/src/multiarray/flagsobject.c index cdcf60d5c..31a7d041e 100644 --- a/numpy/core/src/multiarray/flagsobject.c +++ b/numpy/core/src/multiarray/flagsobject.c @@ -31,6 +31,7 @@ PyArray_NewFlagsObject(PyObject *obj) { PyObject *flagobj; int flags; + if (obj == NULL) { flags = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_OWNDATA | @@ -38,7 +39,13 @@ PyArray_NewFlagsObject(PyObject *obj) NPY_ARRAY_ALIGNED; } else { - flags = PyArray_FLAGS(obj); + if (!PyArray_Check(obj)) { + PyErr_SetString(PyExc_ValueError, + "Need a NumPy array to create a flags object"); + return NULL; + } + + flags = PyArray_FLAGS((PyArrayObject *)obj); } flagobj = PyArrayFlags_Type.tp_alloc(&PyArrayFlags_Type, 0); if (flagobj == NULL) { @@ -59,32 +66,32 @@ PyArray_UpdateFlags(PyArrayObject *ret, int flagmask) if (flagmask & NPY_ARRAY_F_CONTIGUOUS) { if (_IsFortranContiguous(ret)) { - ret->flags |= NPY_ARRAY_F_CONTIGUOUS; - if (ret->nd > 1) { - ret->flags &= ~NPY_ARRAY_C_CONTIGUOUS; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_F_CONTIGUOUS); + if (PyArray_NDIM(ret) > 1) { + PyArray_CLEARFLAGS(ret, NPY_ARRAY_C_CONTIGUOUS); } } else { - ret->flags &= ~NPY_ARRAY_F_CONTIGUOUS; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_F_CONTIGUOUS); } } if (flagmask & NPY_ARRAY_C_CONTIGUOUS) { if (_IsContiguous(ret)) { - ret->flags |= NPY_ARRAY_C_CONTIGUOUS; - if (ret->nd > 1) { - ret->flags &= ~NPY_ARRAY_F_CONTIGUOUS; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_C_CONTIGUOUS); + if (PyArray_NDIM(ret) > 1) { + PyArray_CLEARFLAGS(ret, NPY_ARRAY_F_CONTIGUOUS); } } else { - ret->flags &= ~NPY_ARRAY_C_CONTIGUOUS; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_C_CONTIGUOUS); } } if (flagmask & NPY_ARRAY_ALIGNED) { if (_IsAligned(ret)) { - ret->flags |= NPY_ARRAY_ALIGNED; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_ALIGNED); } else { - ret->flags &= ~NPY_ARRAY_ALIGNED; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_ALIGNED); } } /* @@ -93,10 +100,10 @@ PyArray_UpdateFlags(PyArrayObject *ret, int flagmask) */ if (flagmask & NPY_ARRAY_WRITEABLE) { if (_IsWriteable(ret)) { - ret->flags |= NPY_ARRAY_WRITEABLE; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_WRITEABLE); } else { - ret->flags &= ~NPY_ARRAY_WRITEABLE; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_WRITEABLE); } } return; @@ -115,20 +122,20 @@ _IsContiguous(PyArrayObject *ap) intp dim; int i; - if (ap->nd == 0) { + if (PyArray_NDIM(ap) == 0) { return 1; } - sd = ap->descr->elsize; - if (ap->nd == 1) { - return ap->dimensions[0] == 1 || sd == ap->strides[0]; + sd = PyArray_DESCR(ap)->elsize; + if (PyArray_NDIM(ap) == 1) { + return PyArray_DIMS(ap)[0] == 1 || sd == PyArray_STRIDES(ap)[0]; } - for (i = ap->nd - 1; i >= 0; --i) { - dim = ap->dimensions[i]; + for (i = PyArray_NDIM(ap) - 1; i >= 0; --i) { + dim = PyArray_DIMS(ap)[i]; /* contiguous by definition */ if (dim == 0) { return 1; } - if (ap->strides[i] != sd) { + if (PyArray_STRIDES(ap)[i] != sd) { return 0; } sd *= dim; @@ -145,20 +152,20 @@ _IsFortranContiguous(PyArrayObject *ap) intp dim; int i; - if (ap->nd == 0) { + if (PyArray_NDIM(ap) == 0) { return 1; } - sd = ap->descr->elsize; - if (ap->nd == 1) { - return ap->dimensions[0] == 1 || sd == ap->strides[0]; + sd = PyArray_DESCR(ap)->elsize; + if (PyArray_NDIM(ap) == 1) { + return PyArray_DIMS(ap)[0] == 1 || sd == PyArray_STRIDES(ap)[0]; } - for (i = 0; i < ap->nd; ++i) { - dim = ap->dimensions[i]; + for (i = 0; i < PyArray_NDIM(ap); ++i) { + dim = PyArray_DIMS(ap)[i]; /* fortran contiguous by definition */ if (dim == 0) { return 1; } - if (ap->strides[i] != sd) { + if (PyArray_STRIDES(ap)[i] != sd) { return 0; } sd *= dim; diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c index cbb7ce6e9..e9e052683 100644 --- a/numpy/core/src/multiarray/getset.c +++ b/numpy/core/src/multiarray/getset.c @@ -23,7 +23,7 @@ static PyObject * array_ndim_get(PyArrayObject *self) { - return PyInt_FromLong(self->nd); + return PyInt_FromLong(PyArray_NDIM(self)); } static PyObject * @@ -35,7 +35,7 @@ array_flags_get(PyArrayObject *self) static PyObject * array_shape_get(PyArrayObject *self) { - return PyArray_IntTupleFromIntp(self->nd, self->dimensions); + return PyArray_IntTupleFromIntp(PyArray_NDIM(self), PyArray_DIMS(self)); } @@ -43,10 +43,10 @@ static int array_shape_set(PyArrayObject *self, PyObject *val) { int nd; - PyObject *ret; + PyArrayObject *ret; /* Assumes C-order */ - ret = PyArray_Reshape(self, val); + ret = (PyArrayObject *)PyArray_Reshape(self, val); if (ret == NULL) { return -1; } @@ -59,24 +59,24 @@ array_shape_set(PyArrayObject *self, PyObject *val) } /* Free old dimensions and strides */ - PyDimMem_FREE(self->dimensions); + PyDimMem_FREE(PyArray_DIMS(self)); nd = PyArray_NDIM(ret); - self->nd = nd; + ((PyArrayObject_fieldaccess *)self)->nd = nd; if (nd > 0) { /* create new dimensions and strides */ - self->dimensions = PyDimMem_NEW(2*nd); - if (self->dimensions == NULL) { + ((PyArrayObject_fieldaccess *)self)->dimensions = PyDimMem_NEW(2*nd); + if (PyArray_DIMS(self) == NULL) { Py_DECREF(ret); PyErr_SetString(PyExc_MemoryError,""); return -1; } - self->strides = self->dimensions + nd; - memcpy(self->dimensions, PyArray_DIMS(ret), nd*sizeof(intp)); - memcpy(self->strides, PyArray_STRIDES(ret), nd*sizeof(intp)); + ((PyArrayObject_fieldaccess *)self)->strides = PyArray_DIMS(self) + nd; + memcpy(PyArray_DIMS(self), PyArray_DIMS(ret), nd*sizeof(intp)); + memcpy(PyArray_STRIDES(self), PyArray_STRIDES(ret), nd*sizeof(intp)); } else { - self->dimensions = NULL; - self->strides = NULL; + ((PyArrayObject_fieldaccess *)self)->dimensions = NULL; + ((PyArrayObject_fieldaccess *)self)->strides = NULL; } Py_DECREF(ret); PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); @@ -87,7 +87,7 @@ array_shape_set(PyArrayObject *self, PyObject *val) static PyObject * array_strides_get(PyArrayObject *self) { - return PyArray_IntTupleFromIntp(self->nd, self->strides); + return PyArray_IntTupleFromIntp(PyArray_NDIM(self), PyArray_STRIDES(self)); } static int @@ -105,40 +105,40 @@ array_strides_set(PyArrayObject *self, PyObject *obj) PyErr_SetString(PyExc_TypeError, "invalid strides"); return -1; } - if (newstrides.len != self->nd) { + if (newstrides.len != PyArray_NDIM(self)) { PyErr_Format(PyExc_ValueError, "strides must be " \ - " same length as shape (%d)", self->nd); + " same length as shape (%d)", PyArray_NDIM(self)); goto fail; } new = self; - while(new->base && PyArray_Check(new->base)) { - new = (PyArrayObject *)(new->base); + while(PyArray_BASE(new) && PyArray_Check(PyArray_BASE(new))) { + new = (PyArrayObject *)(PyArray_BASE(new)); } /* * Get the available memory through the buffer interface on - * new->base or if that fails from the current new + * PyArray_BASE(new) or if that fails from the current new */ - if (new->base && PyObject_AsReadBuffer(new->base, + if (PyArray_BASE(new) && PyObject_AsReadBuffer(PyArray_BASE(new), (const void **)&buf, &buf_len) >= 0) { - offset = self->data - buf; + offset = PyArray_DATA(self) - buf; numbytes = buf_len + offset; } else { PyErr_Clear(); - numbytes = PyArray_MultiplyList(new->dimensions, - new->nd)*new->descr->elsize; - offset = self->data - new->data; + numbytes = PyArray_MultiplyList(PyArray_DIMS(new), + PyArray_NDIM(new))*PyArray_DESCR(new)->elsize; + offset = PyArray_DATA(self) - PyArray_DATA(new); } - if (!PyArray_CheckStrides(self->descr->elsize, self->nd, numbytes, + if (!PyArray_CheckStrides(PyArray_DESCR(self)->elsize, PyArray_NDIM(self), numbytes, offset, - self->dimensions, newstrides.ptr)) { + PyArray_DIMS(self), newstrides.ptr)) { PyErr_SetString(PyExc_ValueError, "strides is not "\ "compatible with available memory"); goto fail; } - memcpy(self->strides, newstrides.ptr, sizeof(intp)*newstrides.len); + memcpy(PyArray_STRIDES(self), newstrides.ptr, sizeof(intp)*newstrides.len); PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); PyDimMem_FREE(newstrides.ptr); return 0; @@ -164,14 +164,14 @@ array_priority_get(PyArrayObject *self) static PyObject * array_typestr_get(PyArrayObject *self) { - return arraydescr_protocol_typestr_get(self->descr); + return arraydescr_protocol_typestr_get(PyArray_DESCR(self)); } static PyObject * array_descr_get(PyArrayObject *self) { - Py_INCREF(self->descr); - return (PyObject *)self->descr; + Py_INCREF(PyArray_DESCR(self)); + return (PyObject *)PyArray_DESCR(self); } static PyObject * @@ -180,7 +180,7 @@ array_protocol_descr_get(PyArrayObject *self) PyObject *res; PyObject *dobj; - res = arraydescr_protocol_descr_get(self->descr); + res = arraydescr_protocol_descr_get(PyArray_DESCR(self)); if (res) { return res; } @@ -205,11 +205,11 @@ array_protocol_descr_get(PyArrayObject *self) static PyObject * array_protocol_strides_get(PyArrayObject *self) { - if PyArray_ISCONTIGUOUS(self) { + if (PyArray_ISCONTIGUOUS(self)) { Py_INCREF(Py_None); return Py_None; } - return PyArray_IntTupleFromIntp(self->nd, self->strides); + return PyArray_IntTupleFromIntp(PyArray_NDIM(self), PyArray_STRIDES(self)); } @@ -218,8 +218,8 @@ static PyObject * array_dataptr_get(PyArrayObject *self) { return Py_BuildValue("NO", - PyLong_FromVoidPtr(self->data), - (self->flags & NPY_ARRAY_WRITEABLE ? Py_False : + PyLong_FromVoidPtr(PyArray_DATA(self)), + (PyArray_FLAGS(self) & NPY_ARRAY_WRITEABLE ? Py_False : Py_True)); } @@ -233,7 +233,7 @@ array_ctypes_get(PyArrayObject *self) return NULL; } ret = PyObject_CallMethod(_numpy_internal, "_ctypes", "ON", self, - PyLong_FromVoidPtr(self->data)); + PyLong_FromVoidPtr(PyArray_DATA(self))); Py_DECREF(_numpy_internal); return ret; } @@ -262,7 +262,7 @@ array_interface_get(PyArrayObject *self) PyDict_SetItemString(dict, "descr", obj); Py_DECREF(obj); - obj = arraydescr_protocol_typestr_get(self->descr); + obj = arraydescr_protocol_typestr_get(PyArray_DESCR(self)); PyDict_SetItemString(dict, "typestr", obj); Py_DECREF(obj); @@ -299,6 +299,10 @@ array_data_get(PyArrayObject *self) #endif } +/* + * TODO: Given view semantics, I think this function is a really + * bad idea, and should be removed! + */ static int array_data_set(PyArrayObject *self, PyObject *op) { @@ -324,23 +328,24 @@ array_data_set(PyArrayObject *self, PyObject *op) PyErr_SetString(PyExc_AttributeError, "not enough data for array"); return -1; } - if (self->flags & NPY_ARRAY_OWNDATA) { + if (PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA) { PyArray_XDECREF(self); - PyDataMem_FREE(self->data); + PyDataMem_FREE(PyArray_DATA(self)); } - if (self->base) { - if (self->flags & NPY_ARRAY_UPDATEIFCOPY) { - ((PyArrayObject *)self->base)->flags |= NPY_ARRAY_WRITEABLE; - self->flags &= ~NPY_ARRAY_UPDATEIFCOPY; + if (PyArray_BASE(self)) { + if (PyArray_FLAGS(self) & NPY_ARRAY_UPDATEIFCOPY) { + PyArray_ENABLEFLAGS((PyArrayObject *)PyArray_BASE(self), + NPY_ARRAY_WRITEABLE); + PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY); } - Py_DECREF(self->base); + Py_DECREF(PyArray_BASE(self)); } Py_INCREF(op); - self->base = op; - self->data = buf; - self->flags = NPY_ARRAY_CARRAY; + ((PyArrayObject_fieldaccess *)self)->base = op; + ((PyArrayObject_fieldaccess *)self)->data = buf; + ((PyArrayObject_fieldaccess *)self)->flags = NPY_ARRAY_CARRAY; if (!writeable) { - self->flags &= ~NPY_ARRAY_WRITEABLE; + PyArray_CLEARFLAGS(self, ~NPY_ARRAY_WRITEABLE); } return 0; } @@ -349,7 +354,7 @@ array_data_set(PyArrayObject *self, PyObject *op) static PyObject * array_itemsize_get(PyArrayObject *self) { - return PyInt_FromLong((long) self->descr->elsize); + return PyInt_FromLong((long) PyArray_DESCR(self)->elsize); } static PyObject * @@ -409,8 +414,8 @@ array_descr_set(PyArrayObject *self, PyObject *arg) } if (PyDataType_FLAGCHK(newtype, NPY_ITEM_HASOBJECT) || PyDataType_FLAGCHK(newtype, NPY_ITEM_IS_POINTER) || - PyDataType_FLAGCHK(self->descr, NPY_ITEM_HASOBJECT) || - PyDataType_FLAGCHK(self->descr, NPY_ITEM_IS_POINTER)) { + PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_ITEM_HASOBJECT) || + PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_ITEM_IS_POINTER)) { PyErr_SetString(PyExc_TypeError, \ "Cannot change data-type for object " \ "array."); @@ -425,7 +430,7 @@ array_descr_set(PyArrayObject *self, PyObject *arg) if (newtype == NULL) { return -1; } - newtype->elsize = self->descr->elsize; + newtype->elsize = PyArray_DESCR(self)->elsize; } /* But no other flexible types */ else { @@ -437,44 +442,44 @@ array_descr_set(PyArrayObject *self, PyObject *arg) } - if ((newtype->elsize != self->descr->elsize) && - (self->nd == 0 || !PyArray_ISONESEGMENT(self) || + if ((newtype->elsize != PyArray_DESCR(self)->elsize) && + (PyArray_NDIM(self) == 0 || !PyArray_ISONESEGMENT(self) || PyDataType_HASSUBARRAY(newtype))) { goto fail; } if (PyArray_ISCONTIGUOUS(self)) { - index = self->nd - 1; + index = PyArray_NDIM(self) - 1; } else { index = 0; } - if (newtype->elsize < self->descr->elsize) { + if (newtype->elsize < PyArray_DESCR(self)->elsize) { /* * if it is compatible increase the size of the * dimension at end (or at the front for NPY_ARRAY_F_CONTIGUOUS) */ - if (self->descr->elsize % newtype->elsize != 0) { + if (PyArray_DESCR(self)->elsize % newtype->elsize != 0) { goto fail; } - newdim = self->descr->elsize / newtype->elsize; - self->dimensions[index] *= newdim; - self->strides[index] = newtype->elsize; + newdim = PyArray_DESCR(self)->elsize / newtype->elsize; + PyArray_DIMS(self)[index] *= newdim; + PyArray_STRIDES(self)[index] = newtype->elsize; } - else if (newtype->elsize > self->descr->elsize) { + else if (newtype->elsize > PyArray_DESCR(self)->elsize) { /* * Determine if last (or first if NPY_ARRAY_F_CONTIGUOUS) dimension * is compatible */ - newdim = self->dimensions[index] * self->descr->elsize; + newdim = PyArray_DIMS(self)[index] * PyArray_DESCR(self)->elsize; if ((newdim % newtype->elsize) != 0) { goto fail; } - self->dimensions[index] = newdim / newtype->elsize; - self->strides[index] = newtype->elsize; + PyArray_DIMS(self)[index] = newdim / newtype->elsize; + PyArray_STRIDES(self)[index] = newtype->elsize; } /* fall through -- adjust type*/ - Py_DECREF(self->descr); + Py_DECREF(PyArray_DESCR(self)); if (PyDataType_HASSUBARRAY(newtype)) { /* * create new array object from data and update @@ -486,25 +491,25 @@ array_descr_set(PyArrayObject *self, PyObject *arg) * temp will steal a reference to it */ temp = (PyArrayObject *) - PyArray_NewFromDescr(&PyArray_Type, newtype, self->nd, - self->dimensions, self->strides, - self->data, self->flags, NULL); + PyArray_NewFromDescr(&PyArray_Type, newtype, PyArray_NDIM(self), + PyArray_DIMS(self), PyArray_STRIDES(self), + PyArray_DATA(self), PyArray_FLAGS(self), NULL); if (temp == NULL) { return -1; } - PyDimMem_FREE(self->dimensions); - self->dimensions = temp->dimensions; - self->nd = temp->nd; - self->strides = temp->strides; - newtype = temp->descr; - Py_INCREF(temp->descr); + PyDimMem_FREE(PyArray_DIMS(self)); + ((PyArrayObject_fieldaccess *)self)->dimensions = PyArray_DIMS(temp); + ((PyArrayObject_fieldaccess *)self)->nd = PyArray_NDIM(temp); + ((PyArrayObject_fieldaccess *)self)->strides = PyArray_STRIDES(temp); + newtype = PyArray_DESCR(temp); + Py_INCREF(PyArray_DESCR(temp)); /* Fool deallocator not to delete these*/ - temp->nd = 0; - temp->dimensions = NULL; + ((PyArrayObject_fieldaccess *)temp)->nd = 0; + ((PyArrayObject_fieldaccess *)temp)->dimensions = NULL; Py_DECREF(temp); } - self->descr = newtype; + ((PyArrayObject_fieldaccess *)self)->descr = newtype; PyArray_UpdateFlags(self, NPY_ARRAY_UPDATE_ALL); return 0; @@ -525,10 +530,10 @@ array_struct_get(PyArrayObject *self) return PyErr_NoMemory(); } inter->two = 2; - inter->nd = self->nd; - inter->typekind = self->descr->kind; - inter->itemsize = self->descr->elsize; - inter->flags = self->flags; + inter->nd = PyArray_NDIM(self); + inter->typekind = PyArray_DESCR(self)->kind; + inter->itemsize = PyArray_DESCR(self)->elsize; + inter->flags = PyArray_FLAGS(self); /* reset unused flags */ inter->flags &= ~(NPY_ARRAY_UPDATEIFCOPY | NPY_ARRAY_OWNDATA); if (PyArray_ISNOTSWAPPED(self)) inter->flags |= NPY_ARRAY_NOTSWAPPED; @@ -536,23 +541,23 @@ array_struct_get(PyArrayObject *self) * Copy shape and strides over since these can be reset *when the array is "reshaped". */ - if (self->nd > 0) { - inter->shape = (intp *)_pya_malloc(2*sizeof(intp)*self->nd); + if (PyArray_NDIM(self) > 0) { + inter->shape = (intp *)_pya_malloc(2*sizeof(intp)*PyArray_NDIM(self)); if (inter->shape == NULL) { _pya_free(inter); return PyErr_NoMemory(); } - inter->strides = inter->shape + self->nd; - memcpy(inter->shape, self->dimensions, sizeof(intp)*self->nd); - memcpy(inter->strides, self->strides, sizeof(intp)*self->nd); + inter->strides = inter->shape + PyArray_NDIM(self); + memcpy(inter->shape, PyArray_DIMS(self), sizeof(intp)*PyArray_NDIM(self)); + memcpy(inter->strides, PyArray_STRIDES(self), sizeof(intp)*PyArray_NDIM(self)); } else { inter->shape = NULL; inter->strides = NULL; } - inter->data = self->data; - if (PyDataType_HASFIELDS(self->descr)) { - inter->descr = arraydescr_protocol_descr_get(self->descr); + inter->data = PyArray_DATA(self); + if (PyDataType_HASFIELDS(PyArray_DESCR(self))) { + inter->descr = arraydescr_protocol_descr_get(PyArray_DESCR(self)); if (inter->descr == NULL) { PyErr_Clear(); } @@ -571,13 +576,13 @@ array_struct_get(PyArrayObject *self) static PyObject * array_base_get(PyArrayObject *self) { - if (self->base == NULL) { + if (PyArray_BASE(self) == NULL) { Py_INCREF(Py_None); return Py_None; } else { - Py_INCREF(self->base); - return self->base; + Py_INCREF(PyArray_BASE(self)); + return PyArray_BASE(self); } } @@ -593,7 +598,7 @@ _get_part(PyArrayObject *self, int imag) PyArrayObject *ret; int offset; - switch (self->descr->type_num) { + switch (PyArray_DESCR(self)->type_num) { case PyArray_CFLOAT: float_type_num = PyArray_FLOAT; break; @@ -606,7 +611,7 @@ _get_part(PyArrayObject *self, int imag) default: PyErr_Format(PyExc_ValueError, "Cannot convert complex type number %d to float", - self->descr->type_num); + PyArray_DESCR(self)->type_num); return NULL; } @@ -614,28 +619,30 @@ _get_part(PyArrayObject *self, int imag) offset = (imag ? type->elsize : 0); - if (!PyArray_ISNBO(self->descr->byteorder)) { + if (!PyArray_ISNBO(PyArray_DESCR(self)->byteorder)) { PyArray_Descr *new; new = PyArray_DescrNew(type); - new->byteorder = self->descr->byteorder; + new->byteorder = PyArray_DESCR(self)->byteorder; Py_DECREF(type); type = new; } ret = (PyArrayObject *) PyArray_NewFromDescr(Py_TYPE(self), type, - self->nd, - self->dimensions, - self->strides, - self->data + offset, - self->flags, (PyObject *)self); + PyArray_NDIM(self), + PyArray_DIMS(self), + PyArray_STRIDES(self), + PyArray_DATA(self) + offset, + PyArray_FLAGS(self), (PyObject *)self); if (ret == NULL) { return NULL; } - ret->flags &= ~NPY_ARRAY_C_CONTIGUOUS; - ret->flags &= ~NPY_ARRAY_F_CONTIGUOUS; Py_INCREF(self); - ret->base = (PyObject *)self; + if (PyArray_SetBaseObject(ret, (PyObject *)self) < 0) { + Py_DECREF(ret); + return NULL; + } + PyArray_CLEARFLAGS(ret, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); return ret; } @@ -701,11 +708,11 @@ array_imag_get(PyArrayObject *self) ret = _get_part(self, 1); } else { - Py_INCREF(self->descr); + Py_INCREF(PyArray_DESCR(self)); ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), - self->descr, - self->nd, - self->dimensions, + PyArray_DESCR(self), + PyArray_NDIM(self), + PyArray_DIMS(self), NULL, NULL, PyArray_ISFORTRAN(self), (PyObject *)self); @@ -715,7 +722,7 @@ array_imag_get(PyArrayObject *self) if (_zerofill(ret) < 0) { return NULL; } - ret->flags &= ~NPY_ARRAY_WRITEABLE; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_WRITEABLE); } return (PyObject *) ret; } @@ -758,21 +765,21 @@ array_flat_get(PyArrayObject *self) static int array_flat_set(PyArrayObject *self, PyObject *val) { - PyObject *arr = NULL; + PyArrayObject *arr = NULL; int retval = -1; PyArrayIterObject *selfit = NULL, *arrit = NULL; PyArray_Descr *typecode; int swap; PyArray_CopySwapFunc *copyswap; - typecode = self->descr; + typecode = PyArray_DESCR(self); Py_INCREF(typecode); - arr = PyArray_FromAny(val, typecode, + arr = (PyArrayObject *)PyArray_FromAny(val, typecode, 0, 0, NPY_ARRAY_FORCECAST | FORTRAN_IF(self), NULL); if (arr == NULL) { return -1; } - arrit = (PyArrayIterObject *)PyArray_IterNew(arr); + arrit = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arr); if (arrit == NULL) { goto exit; } @@ -785,10 +792,10 @@ array_flat_set(PyArrayObject *self, PyObject *val) goto exit; } swap = PyArray_ISNOTSWAPPED(self) != PyArray_ISNOTSWAPPED(arr); - copyswap = self->descr->f->copyswap; - if (PyDataType_REFCHK(self->descr)) { + copyswap = PyArray_DESCR(self)->f->copyswap; + if (PyDataType_REFCHK(PyArray_DESCR(self))) { while (selfit->index < selfit->size) { - PyArray_Item_XDECREF(selfit->dataptr, self->descr); + PyArray_Item_XDECREF(selfit->dataptr, PyArray_DESCR(self)); PyArray_Item_INCREF(arrit->dataptr, PyArray_DESCR(arr)); memmove(selfit->dataptr, arrit->dataptr, sizeof(PyObject **)); if (swap) { @@ -805,7 +812,7 @@ array_flat_set(PyArrayObject *self, PyObject *val) } while(selfit->index < selfit->size) { - memmove(selfit->dataptr, arrit->dataptr, self->descr->elsize); + memmove(selfit->dataptr, arrit->dataptr, PyArray_DESCR(self)->elsize); if (swap) { copyswap(selfit->dataptr, NULL, swap, self); } diff --git a/numpy/core/src/multiarray/item_selection.c b/numpy/core/src/multiarray/item_selection.c index 74203774c..54de27e05 100644 --- a/numpy/core/src/multiarray/item_selection.c +++ b/numpy/core/src/multiarray/item_selection.c @@ -18,7 +18,6 @@ #include "ctors.h" #include "lowlevel_strided_loops.h" -#define PyAO PyArrayObject #define _check_axis PyArray_CheckAxis /*NUMPY_API @@ -26,18 +25,18 @@ */ NPY_NO_EXPORT PyObject * PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, - PyArrayObject *ret, NPY_CLIPMODE clipmode) + PyArrayObject *out, NPY_CLIPMODE clipmode) { + PyArray_Descr *dtype; PyArray_FastTakeFunc *func; - PyArrayObject *self, *indices; + PyArrayObject *obj = NULL, *self, *indices; intp nd, i, j, n, m, max_item, tmp, chunk, nelem; intp shape[MAX_DIMS]; char *src, *dest; - int copyret = 0; int err; indices = NULL; - self = (PyAO *)_check_axis(self0, &axis, NPY_ARRAY_CARRAY); + self = (PyArrayObject *)_check_axis(self0, &axis, NPY_ARRAY_CARRAY); if (self == NULL) { return NULL; } @@ -45,49 +44,46 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, PyArray_INTP, 1, 0); if (indices == NULL) { - Py_XINCREF(ret); goto fail; } n = m = chunk = 1; - nd = self->nd + indices->nd - 1; + nd = PyArray_NDIM(self) + PyArray_NDIM(indices) - 1; for (i = 0; i < nd; i++) { if (i < axis) { - shape[i] = self->dimensions[i]; + shape[i] = PyArray_DIMS(self)[i]; n *= shape[i]; } else { - if (i < axis+indices->nd) { - shape[i] = indices->dimensions[i-axis]; + if (i < axis+PyArray_NDIM(indices)) { + shape[i] = PyArray_DIMS(indices)[i-axis]; m *= shape[i]; } else { - shape[i] = self->dimensions[i-indices->nd+1]; + shape[i] = PyArray_DIMS(self)[i-PyArray_NDIM(indices)+1]; chunk *= shape[i]; } } } - Py_INCREF(self->descr); - if (!ret) { - ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), - self->descr, + if (!out) { + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + obj = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), + dtype, nd, shape, NULL, NULL, 0, (PyObject *)self); - if (ret == NULL) { + if (obj == NULL) { goto fail; } } else { - PyArrayObject *obj; int flags = NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY; - if ((ret->nd != nd) || - !PyArray_CompareLists(ret->dimensions, shape, nd)) { + if ((PyArray_NDIM(out) != nd) || + !PyArray_CompareLists(PyArray_DIMS(out), shape, nd)) { PyErr_SetString(PyExc_ValueError, "bad shape in output array"); - ret = NULL; - Py_DECREF(self->descr); goto fail; } @@ -99,30 +95,27 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, */ flags |= NPY_ARRAY_ENSURECOPY; } - obj = (PyArrayObject *)PyArray_FromArray(ret, self->descr, - flags); - if (obj != ret) { - copyret = 1; - } - ret = obj; - if (ret == NULL) { + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags); + if (obj == NULL) { goto fail; } } - max_item = self->dimensions[axis]; + max_item = PyArray_DIMS(self)[axis]; nelem = chunk; - chunk = chunk * ret->descr->elsize; - src = self->data; - dest = ret->data; + chunk = chunk * PyArray_DESCR(obj)->elsize; + src = PyArray_DATA(self); + dest = PyArray_DATA(obj); - func = self->descr->f->fasttake; + func = PyArray_DESCR(self)->f->fasttake; if (func == NULL) { switch(clipmode) { case NPY_RAISE: for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { - tmp = ((intp *)(indices->data))[j]; + tmp = ((intp *)(PyArray_DATA(indices)))[j]; if (tmp < 0) { tmp = tmp + max_item; } @@ -141,7 +134,7 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, case NPY_WRAP: for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { - tmp = ((intp *)(indices->data))[j]; + tmp = ((intp *)(PyArray_DATA(indices)))[j]; if (tmp < 0) { while (tmp < 0) { tmp += max_item; @@ -161,7 +154,7 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, case NPY_CLIP: for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { - tmp = ((intp *)(indices->data))[j]; + tmp = ((intp *)(PyArray_DATA(indices)))[j]; if (tmp < 0) { tmp = 0; } @@ -177,27 +170,25 @@ PyArray_TakeFrom(PyArrayObject *self0, PyObject *indices0, int axis, } } else { - err = func(dest, src, (intp *)(indices->data), + err = func(dest, src, (intp *)(PyArray_DATA(indices)), max_item, n, m, nelem, clipmode); if (err) { goto fail; } } - PyArray_INCREF(ret); + PyArray_INCREF(obj); Py_XDECREF(indices); Py_XDECREF(self); - if (copyret) { - PyObject *obj; - obj = ret->base; - Py_INCREF(obj); - Py_DECREF(ret); - ret = (PyArrayObject *)obj; + if (out != NULL && out != obj) { + Py_INCREF(out); + Py_DECREF(obj); + obj = out; } - return (PyObject *)ret; + return (PyObject *)obj; fail: - PyArray_XDECREF_ERR(ret); + PyArray_XDECREF_ERR(obj); Py_XDECREF(indices); Py_XDECREF(self); return NULL; @@ -229,25 +220,25 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, if (clipmode == NPY_RAISE) { flags |= NPY_ARRAY_ENSURECOPY; } - Py_INCREF(self->descr); + Py_INCREF(PyArray_DESCR(self)); obj = (PyArrayObject *)PyArray_FromArray(self, - self->descr, flags); + PyArray_DESCR(self), flags); if (obj != self) { copied = 1; } self = obj; } max_item = PyArray_SIZE(self); - dest = self->data; - chunk = self->descr->elsize; + dest = PyArray_DATA(self); + chunk = PyArray_DESCR(self)->elsize; indices = (PyArrayObject *)PyArray_ContiguousFromAny(indices0, PyArray_INTP, 0, 0); if (indices == NULL) { goto fail; } ni = PyArray_SIZE(indices); - Py_INCREF(self->descr); - values = (PyArrayObject *)PyArray_FromAny(values0, self->descr, 0, 0, + Py_INCREF(PyArray_DESCR(self)); + values = (PyArrayObject *)PyArray_FromAny(values0, PyArray_DESCR(self), 0, 0, NPY_ARRAY_DEFAULT | NPY_ARRAY_FORCECAST, NULL); if (values == NULL) { goto fail; @@ -256,12 +247,12 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, if (nv <= 0) { goto finish; } - if (PyDataType_REFCHK(self->descr)) { + if (PyDataType_REFCHK(PyArray_DESCR(self))) { switch(clipmode) { case NPY_RAISE: for (i = 0; i < ni; i++) { - src = values->data + chunk*(i % nv); - tmp = ((intp *)(indices->data))[i]; + src = PyArray_DATA(values) + chunk*(i % nv); + tmp = ((intp *)(PyArray_DATA(indices)))[i]; if (tmp < 0) { tmp = tmp + max_item; } @@ -271,15 +262,15 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, "range for array"); goto fail; } - PyArray_Item_INCREF(src, self->descr); - PyArray_Item_XDECREF(dest+tmp*chunk, self->descr); + PyArray_Item_INCREF(src, PyArray_DESCR(self)); + PyArray_Item_XDECREF(dest+tmp*chunk, PyArray_DESCR(self)); memmove(dest + tmp*chunk, src, chunk); } break; case NPY_WRAP: for (i = 0; i < ni; i++) { - src = values->data + chunk * (i % nv); - tmp = ((intp *)(indices->data))[i]; + src = PyArray_DATA(values) + chunk * (i % nv); + tmp = ((intp *)(PyArray_DATA(indices)))[i]; if (tmp < 0) { while (tmp < 0) { tmp += max_item; @@ -290,23 +281,23 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, tmp -= max_item; } } - PyArray_Item_INCREF(src, self->descr); - PyArray_Item_XDECREF(dest+tmp*chunk, self->descr); + PyArray_Item_INCREF(src, PyArray_DESCR(self)); + PyArray_Item_XDECREF(dest+tmp*chunk, PyArray_DESCR(self)); memmove(dest + tmp * chunk, src, chunk); } break; case NPY_CLIP: for (i = 0; i < ni; i++) { - src = values->data + chunk * (i % nv); - tmp = ((intp *)(indices->data))[i]; + src = PyArray_DATA(values) + chunk * (i % nv); + tmp = ((intp *)(PyArray_DATA(indices)))[i]; if (tmp < 0) { tmp = 0; } else if (tmp >= max_item) { tmp = max_item - 1; } - PyArray_Item_INCREF(src, self->descr); - PyArray_Item_XDECREF(dest+tmp*chunk, self->descr); + PyArray_Item_INCREF(src, PyArray_DESCR(self)); + PyArray_Item_XDECREF(dest+tmp*chunk, PyArray_DESCR(self)); memmove(dest + tmp * chunk, src, chunk); } break; @@ -316,8 +307,8 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, switch(clipmode) { case NPY_RAISE: for (i = 0; i < ni; i++) { - src = values->data + chunk * (i % nv); - tmp = ((intp *)(indices->data))[i]; + src = PyArray_DATA(values) + chunk * (i % nv); + tmp = ((intp *)(PyArray_DATA(indices)))[i]; if (tmp < 0) { tmp = tmp + max_item; } @@ -332,8 +323,8 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, break; case NPY_WRAP: for (i = 0; i < ni; i++) { - src = values->data + chunk * (i % nv); - tmp = ((intp *)(indices->data))[i]; + src = PyArray_DATA(values) + chunk * (i % nv); + tmp = ((intp *)(PyArray_DATA(indices)))[i]; if (tmp < 0) { while (tmp < 0) { tmp += max_item; @@ -349,8 +340,8 @@ PyArray_PutTo(PyArrayObject *self, PyObject* values0, PyObject *indices0, break; case NPY_CLIP: for (i = 0; i < ni; i++) { - src = values->data + chunk * (i % nv); - tmp = ((intp *)(indices->data))[i]; + src = PyArray_DATA(values) + chunk * (i % nv); + tmp = ((intp *)(PyArray_DATA(indices)))[i]; if (tmp < 0) { tmp = 0; } @@ -389,6 +380,7 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) { PyArray_FastPutmaskFunc *func; PyArrayObject *mask, *values; + PyArray_Descr *dtype; intp i, chunk, ni, max_item, nv, tmp; char *src, *dest; int copied = 0; @@ -402,17 +394,17 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) values = NULL; if (!PyArray_Check(self)) { PyErr_SetString(PyExc_TypeError, - "putmask: first argument must "\ + "putmask: first argument must " "be an array"); return NULL; } if (!PyArray_ISCONTIGUOUS(self)) { PyArrayObject *obj; - int flags = NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY; - Py_INCREF(self->descr); - obj = (PyArrayObject *)PyArray_FromArray(self, - self->descr, flags); + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + obj = (PyArrayObject *)PyArray_FromArray(self, dtype, + NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY); if (obj != self) { copied = 1; } @@ -420,24 +412,24 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) } max_item = PyArray_SIZE(self); - dest = self->data; - chunk = self->descr->elsize; - mask = (PyArrayObject *)\ - PyArray_FROM_OTF(mask0, PyArray_BOOL, - NPY_ARRAY_CARRAY | NPY_ARRAY_FORCECAST); + dest = PyArray_DATA(self); + chunk = PyArray_DESCR(self)->elsize; + mask = (PyArrayObject *)PyArray_FROM_OTF(mask0, NPY_BOOL, + NPY_ARRAY_CARRAY | NPY_ARRAY_FORCECAST); if (mask == NULL) { goto fail; } ni = PyArray_SIZE(mask); if (ni != max_item) { PyErr_SetString(PyExc_ValueError, - "putmask: mask and data must be "\ + "putmask: mask and data must be " "the same size"); goto fail; } - Py_INCREF(self->descr); - values = (PyArrayObject *)\ - PyArray_FromAny(values0, self->descr, 0, 0, NPY_ARRAY_CARRAY, NULL); + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + values = (PyArrayObject *)PyArray_FromAny(values0, dtype, + 0, 0, NPY_ARRAY_CARRAY, NULL); if (values == NULL) { goto fail; } @@ -448,30 +440,30 @@ PyArray_PutMask(PyArrayObject *self, PyObject* values0, PyObject* mask0) Py_INCREF(Py_None); return Py_None; } - if (PyDataType_REFCHK(self->descr)) { + if (PyDataType_REFCHK(PyArray_DESCR(self))) { for (i = 0; i < ni; i++) { - tmp = ((Bool *)(mask->data))[i]; + tmp = ((npy_bool *)(PyArray_DATA(mask)))[i]; if (tmp) { - src = values->data + chunk * (i % nv); - PyArray_Item_INCREF(src, self->descr); - PyArray_Item_XDECREF(dest+i*chunk, self->descr); + src = PyArray_DATA(values) + chunk * (i % nv); + PyArray_Item_INCREF(src, PyArray_DESCR(self)); + PyArray_Item_XDECREF(dest+i*chunk, PyArray_DESCR(self)); memmove(dest + i * chunk, src, chunk); } } } else { - func = self->descr->f->fastputmask; + func = PyArray_DESCR(self)->f->fastputmask; if (func == NULL) { for (i = 0; i < ni; i++) { - tmp = ((Bool *)(mask->data))[i]; + tmp = ((npy_bool *)(PyArray_DATA(mask)))[i]; if (tmp) { - src = values->data + chunk*(i % nv); + src = PyArray_DATA(values) + chunk*(i % nv); memmove(dest + i*chunk, src, chunk); } } } else { - func(dest, mask->data, ni, values->data, nv); + func(dest, PyArray_DATA(mask), ni, PyArray_DATA(values), nv); } } @@ -507,27 +499,27 @@ PyArray_Repeat(PyArrayObject *aop, PyObject *op, int axis) PyArrayObject *ret = NULL; char *new_data, *old_data; - repeats = (PyAO *)PyArray_ContiguousFromAny(op, PyArray_INTP, 0, 1); + repeats = (PyArrayObject *)PyArray_ContiguousFromAny(op, PyArray_INTP, 0, 1); if (repeats == NULL) { return NULL; } - nd = repeats->nd; - counts = (intp *)repeats->data; + nd = PyArray_NDIM(repeats); + counts = (npy_intp *)PyArray_DATA(repeats); if ((ap=_check_axis(aop, &axis, NPY_ARRAY_CARRAY))==NULL) { Py_DECREF(repeats); return NULL; } - aop = (PyAO *)ap; + aop = (PyArrayObject *)ap; if (nd == 1) { - n = repeats->dimensions[0]; + n = PyArray_DIMS(repeats)[0]; } else { /* nd == 0 */ - n = aop->dimensions[axis]; + n = PyArray_DIMS(aop)[axis]; } - if (aop->dimensions[axis] != n) { + if (PyArray_DIMS(aop)[axis] != n) { PyErr_SetString(PyExc_ValueError, "a.shape[axis] != len(repeats)"); goto fail; @@ -550,29 +542,29 @@ PyArray_Repeat(PyArrayObject *aop, PyObject *op, int axis) /* Construct new array */ - aop->dimensions[axis] = total; - Py_INCREF(aop->descr); + PyArray_DIMS(aop)[axis] = total; + Py_INCREF(PyArray_DESCR(aop)); ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(aop), - aop->descr, - aop->nd, - aop->dimensions, + PyArray_DESCR(aop), + PyArray_NDIM(aop), + PyArray_DIMS(aop), NULL, NULL, 0, (PyObject *)aop); - aop->dimensions[axis] = n; + PyArray_DIMS(aop)[axis] = n; if (ret == NULL) { goto fail; } - new_data = ret->data; - old_data = aop->data; + new_data = PyArray_DATA(ret); + old_data = PyArray_DATA(aop); - chunk = aop->descr->elsize; - for(i = axis + 1; i < aop->nd; i++) { - chunk *= aop->dimensions[i]; + chunk = PyArray_DESCR(aop)->elsize; + for(i = axis + 1; i < PyArray_NDIM(aop); i++) { + chunk *= PyArray_DIMS(aop)[i]; } n_outer = 1; for (i = 0; i < axis; i++) { - n_outer *= aop->dimensions[i]; + n_outer *= PyArray_DIMS(aop)[i]; } for (i = 0; i < n_outer; i++) { for (j = 0; j < n; j++) { @@ -600,16 +592,17 @@ PyArray_Repeat(PyArrayObject *aop, PyObject *op, int axis) /*NUMPY_API */ NPY_NO_EXPORT PyObject * -PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, +PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *out, NPY_CLIPMODE clipmode) { + PyArrayObject *obj = NULL; + PyArray_Descr *dtype; int n, elsize; intp i; char *ret_data; PyArrayObject **mps, *ap; PyArrayMultiIterObject *multi = NULL; intp mi; - int copyret = 0; ap = NULL; /* @@ -636,27 +629,27 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, goto fail; } /* Set-up return array */ - if (!ret) { - Py_INCREF(mps[0]->descr); - ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(ap), - mps[0]->descr, + if (out == NULL) { + dtype = PyArray_DESCR(mps[0]); + Py_INCREF(dtype); + obj = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(ap), + dtype, multi->nd, multi->dimensions, NULL, NULL, 0, (PyObject *)ap); } else { - PyArrayObject *obj; int flags = NPY_ARRAY_CARRAY | NPY_ARRAY_UPDATEIFCOPY | NPY_ARRAY_FORCECAST; - if ((PyArray_NDIM(ret) != multi->nd) - || !PyArray_CompareLists( - PyArray_DIMS(ret), multi->dimensions, multi->nd)) { + if ((PyArray_NDIM(out) != multi->nd) + || !PyArray_CompareLists(PyArray_DIMS(out), + multi->dimensions, + multi->nd)) { PyErr_SetString(PyExc_TypeError, - "invalid shape for output array."); - ret = NULL; + "choose: invalid shape for output array."); goto fail; } if (clipmode == NPY_RAISE) { @@ -667,19 +660,16 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, */ flags |= NPY_ARRAY_ENSURECOPY; } - Py_INCREF(mps[0]->descr); - obj = (PyArrayObject *)PyArray_FromArray(ret, mps[0]->descr, flags); - if (obj != ret) { - copyret = 1; - } - ret = obj; + dtype = PyArray_DESCR(mps[0]); + Py_INCREF(dtype); + obj = (PyArrayObject *)PyArray_FromArray(out, dtype, flags); } - if (ret == NULL) { + if (obj == NULL) { goto fail; } - elsize = ret->descr->elsize; - ret_data = ret->data; + elsize = PyArray_DESCR(obj)->elsize; + ret_data = PyArray_DATA(obj); while (PyArray_MultiIter_NOTDONE(multi)) { mi = *((intp *)PyArray_MultiIter_DATA(multi, n)); @@ -717,21 +707,19 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, PyArray_MultiIter_NEXT(multi); } - PyArray_INCREF(ret); + PyArray_INCREF(obj); Py_DECREF(multi); for (i = 0; i < n; i++) { Py_XDECREF(mps[i]); } Py_DECREF(ap); PyDataMem_FREE(mps); - if (copyret) { - PyObject *obj; - obj = ret->base; - Py_INCREF(obj); - Py_DECREF(ret); - ret = (PyArrayObject *)obj; + if (out != NULL && out != obj) { + Py_INCREF(out); + Py_DECREF(obj); + obj = out; } - return (PyObject *)ret; + return (PyObject *)obj; fail: Py_XDECREF(multi); @@ -740,7 +728,7 @@ PyArray_Choose(PyArrayObject *ip, PyObject *op, PyArrayObject *ret, } Py_XDECREF(ap); PyDataMem_FREE(mps); - PyArray_XDECREF_ERR(ret); + PyArray_XDECREF_ERR(obj); return NULL; } @@ -769,14 +757,14 @@ _new_sort(PyArrayObject *op, int axis, NPY_SORTKIND which) return -1; } - NPY_BEGIN_THREADS_DESCR(op->descr); - sort = op->descr->f->sort[which]; + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(op)); + sort = PyArray_DESCR(op)->f->sort[which]; size = it->size; - N = op->dimensions[axis]; - elsize = op->descr->elsize; - astride = op->strides[axis]; + N = PyArray_DIMS(op)[axis]; + elsize = PyArray_DESCR(op)->elsize; + astride = PyArray_STRIDES(op)[axis]; - needcopy = !(op->flags & NPY_ARRAY_ALIGNED) || + needcopy = !(PyArray_FLAGS(op) & NPY_ARRAY_ALIGNED) || (astride != (intp) elsize) || swap; if (needcopy) { char *buffer = PyDataMem_NEW(N*elsize); @@ -808,7 +796,7 @@ _new_sort(PyArrayObject *op, int axis, NPY_SORTKIND which) PyArray_ITER_NEXT(it); } } - NPY_END_THREADS_DESCR(op->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(op)); Py_DECREF(it); return 0; @@ -824,7 +812,7 @@ _new_argsort(PyArrayObject *op, int axis, NPY_SORTKIND which) PyArrayIterObject *it = NULL; PyArrayIterObject *rit = NULL; - PyObject *ret; + PyArrayObject *ret; int needcopy = 0, i; intp N, size; int elsize, swap; @@ -832,28 +820,30 @@ _new_argsort(PyArrayObject *op, int axis, NPY_SORTKIND which) PyArray_ArgSortFunc *argsort; BEGIN_THREADS_DEF; - ret = PyArray_New(Py_TYPE(op), op->nd, - op->dimensions, PyArray_INTP, - NULL, NULL, 0, 0, (PyObject *)op); + ret = (PyArrayObject *)PyArray_New(Py_TYPE(op), + PyArray_NDIM(op), + PyArray_DIMS(op), + NPY_INTP, + NULL, NULL, 0, 0, (PyObject *)op); if (ret == NULL) { return NULL; } it = (PyArrayIterObject *)PyArray_IterAllButAxis((PyObject *)op, &axis); - rit = (PyArrayIterObject *)PyArray_IterAllButAxis(ret, &axis); + rit = (PyArrayIterObject *)PyArray_IterAllButAxis((PyObject *)ret, &axis); if (rit == NULL || it == NULL) { goto fail; } swap = !PyArray_ISNOTSWAPPED(op); - NPY_BEGIN_THREADS_DESCR(op->descr); - argsort = op->descr->f->argsort[which]; + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(op)); + argsort = PyArray_DESCR(op)->f->argsort[which]; size = it->size; - N = op->dimensions[axis]; - elsize = op->descr->elsize; - astride = op->strides[axis]; + N = PyArray_DIMS(op)[axis]; + elsize = PyArray_DESCR(op)->elsize; + astride = PyArray_STRIDES(op)[axis]; rstride = PyArray_STRIDE(ret,axis); - needcopy = swap || !(op->flags & NPY_ARRAY_ALIGNED) || + needcopy = swap || !(PyArray_FLAGS(op) & NPY_ARRAY_ALIGNED) || (astride != (intp) elsize) || (rstride != sizeof(intp)); if (needcopy) { @@ -898,11 +888,11 @@ _new_argsort(PyArrayObject *op, int axis, NPY_SORTKIND which) } } - NPY_END_THREADS_DESCR(op->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(op)); Py_DECREF(it); Py_DECREF(rit); - return ret; + return (PyObject *)ret; fail: NPY_END_THREADS; @@ -919,7 +909,7 @@ static PyArrayObject *global_obj; static int qsortCompare (const void *a, const void *b) { - return global_obj->descr->f->compare(a,b,global_obj); + return PyArray_DESCR(global_obj)->f->compare(a,b,global_obj); } /* @@ -927,14 +917,14 @@ qsortCompare (const void *a, const void *b) * the array with axes swapped if local variable axis is not the * last dimension. Origin must be defined locally. */ -#define SWAPAXES(op, ap) { \ - orign = (ap)->nd-1; \ - if (axis != orign) { \ - (op) = (PyAO *)PyArray_SwapAxes((ap), axis, orign); \ - Py_DECREF((ap)); \ - if ((op) == NULL) return NULL; \ - } \ - else (op) = (ap); \ +#define SWAPAXES(op, ap) { \ + orign = PyArray_NDIM(ap)-1; \ + if (axis != orign) { \ + (op) = (PyArrayObject *)PyArray_SwapAxes((ap), axis, orign); \ + Py_DECREF((ap)); \ + if ((op) == NULL) return NULL; \ + } \ + else (op) = (ap); \ } /* @@ -944,7 +934,7 @@ qsortCompare (const void *a, const void *b) */ #define SWAPBACK(op, ap) { \ if (axis != orign) { \ - (op) = (PyAO *)PyArray_SwapAxes((ap), axis, orign); \ + (op) = (PyArrayObject *)PyArray_SwapAxes((ap), axis, orign); \ Py_DECREF((ap)); \ if ((op) == NULL) return NULL; \ } \ @@ -954,10 +944,10 @@ qsortCompare (const void *a, const void *b) /* These swap axes in-place if necessary */ #define SWAPINTP(a,b) {intp c; c=(a); (a) = (b); (b) = c;} #define SWAPAXES2(ap) { \ - orign = (ap)->nd-1; \ + orign = PyArray_NDIM(ap)-1; \ if (axis != orign) { \ - SWAPINTP(ap->dimensions[axis], ap->dimensions[orign]); \ - SWAPINTP(ap->strides[axis], ap->strides[orign]); \ + SWAPINTP(PyArray_DIMS(ap)[axis], PyArray_DIMS(ap)[orign]); \ + SWAPINTP(PyArray_STRIDES(ap)[axis], PyArray_STRIDES(ap)[orign]); \ PyArray_UpdateFlags(ap, NPY_ARRAY_C_CONTIGUOUS | \ NPY_ARRAY_F_CONTIGUOUS); \ } \ @@ -965,8 +955,8 @@ qsortCompare (const void *a, const void *b) #define SWAPBACK2(ap) { \ if (axis != orign) { \ - SWAPINTP(ap->dimensions[axis], ap->dimensions[orign]); \ - SWAPINTP(ap->strides[axis], ap->strides[orign]); \ + SWAPINTP(PyArray_DIMS(ap)[axis], PyArray_DIMS(ap)[orign]); \ + SWAPINTP(PyArray_STRIDES(ap)[axis], PyArray_STRIDES(ap)[orign]); \ PyArray_UpdateFlags(ap, NPY_ARRAY_C_CONTIGUOUS | \ NPY_ARRAY_F_CONTIGUOUS); \ } \ @@ -982,7 +972,7 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which) char *ip; int i, n, m, elsize, orign; - n = op->nd; + n = PyArray_NDIM(op); if ((n == 0) || (PyArray_SIZE(op) == 1)) { return 0; } @@ -1000,11 +990,11 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which) } /* Determine if we should use type-specific algorithm or not */ - if (op->descr->f->sort[which] != NULL) { + if (PyArray_DESCR(op)->f->sort[which] != NULL) { return _new_sort(op, axis, which); } if ((which != PyArray_QUICKSORT) - || op->descr->f->compare == NULL) { + || PyArray_DESCR(op)->f->compare == NULL) { PyErr_SetString(PyExc_TypeError, "desired sort not supported for this type"); return -1; @@ -1018,8 +1008,8 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which) if (ap == NULL) { goto fail; } - elsize = ap->descr->elsize; - m = ap->dimensions[ap->nd-1]; + elsize = PyArray_DESCR(ap)->elsize; + m = PyArray_DIMS(ap)[PyArray_NDIM(ap)-1]; if (m == 0) { goto finish; } @@ -1028,7 +1018,7 @@ PyArray_Sort(PyArrayObject *op, int axis, NPY_SORTKIND which) /* Store global -- allows re-entry -- restore before leaving*/ store_arr = global_obj; global_obj = ap; - for (ip = ap->data, i = 0; i < n; i++, ip += elsize*m) { + for (ip = PyArray_DATA(ap), i = 0; i < n; i++, ip += elsize*m) { qsort(ip, m, elsize, qsortCompare); } global_obj = store_arr; @@ -1054,10 +1044,10 @@ static char *global_data; static int argsort_static_compare(const void *ip1, const void *ip2) { - int isize = global_obj->descr->elsize; + int isize = PyArray_DESCR(global_obj)->elsize; const intp *ipa = ip1; const intp *ipb = ip2; - return global_obj->descr->f->compare(global_data + (isize * *ipa), + return PyArray_DESCR(global_obj)->f->compare(global_data + (isize * *ipa), global_data + (isize * *ipb), global_obj); } @@ -1074,32 +1064,32 @@ PyArray_ArgSort(PyArrayObject *op, int axis, NPY_SORTKIND which) int argsort_elsize; char *store_ptr; - n = op->nd; + n = PyArray_NDIM(op); if ((n == 0) || (PyArray_SIZE(op) == 1)) { - ret = (PyArrayObject *)PyArray_New(Py_TYPE(op), op->nd, - op->dimensions, + ret = (PyArrayObject *)PyArray_New(Py_TYPE(op), PyArray_NDIM(op), + PyArray_DIMS(op), PyArray_INTP, NULL, NULL, 0, 0, (PyObject *)op); if (ret == NULL) { return NULL; } - *((intp *)ret->data) = 0; + *((intp *)PyArray_DATA(ret)) = 0; return (PyObject *)ret; } /* Creates new reference op2 */ - if ((op2=(PyAO *)_check_axis(op, &axis, 0)) == NULL) { + if ((op2=(PyArrayObject *)_check_axis(op, &axis, 0)) == NULL) { return NULL; } /* Determine if we should use new algorithm or not */ - if (op2->descr->f->argsort[which] != NULL) { + if (PyArray_DESCR(op2)->f->argsort[which] != NULL) { ret = (PyArrayObject *)_new_argsort(op2, axis, which); Py_DECREF(op2); return (PyObject *)ret; } - if ((which != PyArray_QUICKSORT) || op2->descr->f->compare == NULL) { + if ((which != PyArray_QUICKSORT) || PyArray_DESCR(op2)->f->compare == NULL) { PyErr_SetString(PyExc_TypeError, "requested sort not available for type"); Py_DECREF(op2); @@ -1116,21 +1106,21 @@ PyArray_ArgSort(PyArrayObject *op, int axis, NPY_SORTKIND which) if (op == NULL) { return NULL; } - ret = (PyArrayObject *)PyArray_New(Py_TYPE(op), op->nd, - op->dimensions, PyArray_INTP, + ret = (PyArrayObject *)PyArray_New(Py_TYPE(op), PyArray_NDIM(op), + PyArray_DIMS(op), PyArray_INTP, NULL, NULL, 0, 0, (PyObject *)op); if (ret == NULL) { goto fail; } - ip = (intp *)ret->data; - argsort_elsize = op->descr->elsize; - m = op->dimensions[op->nd-1]; + ip = (intp *)PyArray_DATA(ret); + argsort_elsize = PyArray_DESCR(op)->elsize; + m = PyArray_DIMS(op)[PyArray_NDIM(op)-1]; if (m == 0) { goto finish; } n = PyArray_SIZE(op)/m; store_ptr = global_data; - global_data = op->data; + global_data = PyArray_DATA(op); store = global_obj; global_obj = op; for (i = 0; i < n; i++, ip += m, global_data += m*argsort_elsize) { @@ -1187,7 +1177,7 @@ PyArray_LexSort(PyObject *sort_keys, int axis) "need sequence of keys with len > 0 in lexsort"); return NULL; } - mps = (PyArrayObject **) _pya_malloc(n*sizeof(PyArrayObject)); + mps = (PyArrayObject **) _pya_malloc(n*NPY_SIZEOF_PYARRAYOBJECT); if (mps == NULL) { return PyErr_NoMemory(); } @@ -1209,22 +1199,22 @@ PyArray_LexSort(PyObject *sort_keys, int axis) goto fail; } if (i > 0) { - if ((mps[i]->nd != mps[0]->nd) - || (!PyArray_CompareLists(mps[i]->dimensions, - mps[0]->dimensions, - mps[0]->nd))) { + if ((PyArray_NDIM(mps[i]) != PyArray_NDIM(mps[0])) + || (!PyArray_CompareLists(PyArray_DIMS(mps[i]), + PyArray_DIMS(mps[0]), + PyArray_NDIM(mps[0])))) { PyErr_SetString(PyExc_ValueError, "all keys need to be the same shape"); goto fail; } } - if (!mps[i]->descr->f->argsort[PyArray_MERGESORT]) { + if (!PyArray_DESCR(mps[i])->f->argsort[PyArray_MERGESORT]) { PyErr_Format(PyExc_TypeError, "merge sort not available for item %d", i); goto fail; } if (!object - && PyDataType_FLAGCHK(mps[i]->descr, NPY_NEEDS_PYAPI)) { + && PyDataType_FLAGCHK(PyArray_DESCR(mps[i]), NPY_NEEDS_PYAPI)) { object = 1; } its[i] = (PyArrayIterObject *)PyArray_IterAllButAxis( @@ -1235,18 +1225,18 @@ PyArray_LexSort(PyObject *sort_keys, int axis) } /* Now we can check the axis */ - nd = mps[0]->nd; + nd = PyArray_NDIM(mps[0]); if ((nd == 0) || (PyArray_SIZE(mps[0]) == 1)) { /* single element case */ - ret = (PyArrayObject *)PyArray_New(&PyArray_Type, mps[0]->nd, - mps[0]->dimensions, + ret = (PyArrayObject *)PyArray_New(&PyArray_Type, PyArray_NDIM(mps[0]), + PyArray_DIMS(mps[0]), PyArray_INTP, NULL, NULL, 0, 0, NULL); if (ret == NULL) { goto fail; } - *((intp *)(ret->data)) = 0; + *((intp *)(PyArray_DATA(ret))) = 0; goto finish; } if (axis < 0) { @@ -1259,8 +1249,8 @@ PyArray_LexSort(PyObject *sort_keys, int axis) } /* Now do the sorting */ - ret = (PyArrayObject *)PyArray_New(&PyArray_Type, mps[0]->nd, - mps[0]->dimensions, PyArray_INTP, + ret = (PyArrayObject *)PyArray_New(&PyArray_Type, PyArray_NDIM(mps[0]), + PyArray_DIMS(mps[0]), PyArray_INTP, NULL, NULL, 0, 0, NULL); if (ret == NULL) { goto fail; @@ -1274,17 +1264,17 @@ PyArray_LexSort(PyObject *sort_keys, int axis) NPY_BEGIN_THREADS; } size = rit->size; - N = mps[0]->dimensions[axis]; + N = PyArray_DIMS(mps[0])[axis]; rstride = PyArray_STRIDE(ret, axis); - maxelsize = mps[0]->descr->elsize; + maxelsize = PyArray_DESCR(mps[0])->elsize; needcopy = (rstride != sizeof(intp)); for (j = 0; j < n; j++) { needcopy = needcopy || PyArray_ISBYTESWAPPED(mps[j]) - || !(mps[j]->flags & NPY_ARRAY_ALIGNED) - || (mps[j]->strides[axis] != (intp)mps[j]->descr->elsize); - if (mps[j]->descr->elsize > maxelsize) { - maxelsize = mps[j]->descr->elsize; + || !(PyArray_FLAGS(mps[j]) & NPY_ARRAY_ALIGNED) + || (PyArray_STRIDES(mps[j])[axis] != (intp)PyArray_DESCR(mps[j])->elsize); + if (PyArray_DESCR(mps[j])->elsize > maxelsize) { + maxelsize = PyArray_DESCR(mps[j])->elsize; } } @@ -1304,9 +1294,9 @@ PyArray_LexSort(PyObject *sort_keys, int axis) *iptr++ = i; } for (j = 0; j < n; j++) { - elsize = mps[j]->descr->elsize; - astride = mps[j]->strides[axis]; - argsort = mps[j]->descr->f->argsort[PyArray_MERGESORT]; + elsize = PyArray_DESCR(mps[j])->elsize; + astride = PyArray_STRIDES(mps[j])[axis]; + argsort = PyArray_DESCR(mps[j])->f->argsort[PyArray_MERGESORT]; _unaligned_strided_byte_copy(valbuffer, (intp) elsize, its[j]->dataptr, astride, N, elsize); if (swaps[j]) { @@ -1335,7 +1325,7 @@ PyArray_LexSort(PyObject *sort_keys, int axis) *iptr++ = i; } for (j = 0; j < n; j++) { - argsort = mps[j]->descr->f->argsort[PyArray_MERGESORT]; + argsort = PyArray_DESCR(mps[j])->f->argsort[PyArray_MERGESORT]; if (argsort(its[j]->dataptr, (intp *)rit->dataptr, N, mps[j]) < 0) { goto fail; @@ -1389,13 +1379,13 @@ PyArray_LexSort(PyObject *sort_keys, int axis) static void local_search_left(PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret) { - PyArray_CompareFunc *compare = key->descr->f->compare; - intp nelts = arr->dimensions[arr->nd - 1]; + PyArray_CompareFunc *compare = PyArray_DESCR(key)->f->compare; + intp nelts = PyArray_DIMS(arr)[PyArray_NDIM(arr) - 1]; intp nkeys = PyArray_SIZE(key); - char *parr = arr->data; - char *pkey = key->data; - intp *pret = (intp *)ret->data; - int elsize = arr->descr->elsize; + char *parr = PyArray_DATA(arr); + char *pkey = PyArray_DATA(key); + intp *pret = (intp *)PyArray_DATA(ret); + int elsize = PyArray_DESCR(arr)->elsize; intp i; for (i = 0; i < nkeys; ++i) { @@ -1432,13 +1422,13 @@ local_search_left(PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret) static void local_search_right(PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret) { - PyArray_CompareFunc *compare = key->descr->f->compare; - intp nelts = arr->dimensions[arr->nd - 1]; + PyArray_CompareFunc *compare = PyArray_DESCR(key)->f->compare; + intp nelts = PyArray_DIMS(arr)[PyArray_NDIM(arr) - 1]; intp nkeys = PyArray_SIZE(key); - char *parr = arr->data; - char *pkey = key->data; - intp *pret = (intp *)ret->data; - int elsize = arr->descr->elsize; + char *parr = PyArray_DATA(arr); + char *pkey = PyArray_DATA(key); + intp *pret = (intp *)PyArray_DATA(ret); + int elsize = PyArray_DESCR(arr)->elsize; intp i; for(i = 0; i < nkeys; ++i) { @@ -1471,7 +1461,7 @@ PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2, NPY_SEARCHSIDE side) PyArray_Descr *dtype; NPY_BEGIN_THREADS_DEF; - dtype = PyArray_DescrFromObject((PyObject *)op2, op1->descr); + dtype = PyArray_DescrFromObject((PyObject *)op2, PyArray_DESCR(op1)); /* need ap1 as contiguous array and of right type */ Py_INCREF(dtype); ap1 = (PyArrayObject *)PyArray_CheckFromAny((PyObject *)op1, dtype, @@ -1490,28 +1480,28 @@ PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2, NPY_SEARCHSIDE side) goto fail; } /* ret is a contiguous array of intp type to hold returned indices */ - ret = (PyArrayObject *)PyArray_New(Py_TYPE(ap2), ap2->nd, - ap2->dimensions, PyArray_INTP, + ret = (PyArrayObject *)PyArray_New(Py_TYPE(ap2), PyArray_NDIM(ap2), + PyArray_DIMS(ap2), PyArray_INTP, NULL, NULL, 0, 0, (PyObject *)ap2); if (ret == NULL) { goto fail; } /* check that comparison function exists */ - if (ap2->descr->f->compare == NULL) { + if (PyArray_DESCR(ap2)->f->compare == NULL) { PyErr_SetString(PyExc_TypeError, "compare not supported for type"); goto fail; } if (side == NPY_SEARCHLEFT) { - NPY_BEGIN_THREADS_DESCR(ap2->descr); + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap2)); local_search_left(ap1, ap2, ret); - NPY_END_THREADS_DESCR(ap2->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(ap2)); } else if (side == NPY_SEARCHRIGHT) { - NPY_BEGIN_THREADS_DESCR(ap2->descr); + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap2)); local_search_right(ap1, ap2, ret); - NPY_END_THREADS_DESCR(ap2->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(ap2)); } Py_DECREF(ap1); Py_DECREF(ap2); @@ -1530,7 +1520,7 @@ PyArray_SearchSorted(PyArrayObject *op1, PyObject *op2, NPY_SEARCHSIDE side) NPY_NO_EXPORT PyObject * PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) { - int n = self->nd; + int n = PyArray_NDIM(self); PyObject *new; PyArray_Dims newaxes; intp dims[MAX_DIMS]; @@ -1571,15 +1561,16 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) if (new == NULL) { return NULL; } - self = (PyAO *)new; + self = (PyArrayObject *)new; if (n == 2) { - PyObject *a = NULL, *indices= NULL, *ret = NULL; + PyObject *a = NULL, *ret = NULL; + PyArrayObject *indices = NULL; intp n1, n2, start, stop, step, count; intp *dptr; - n1 = self->dimensions[0]; - n2 = self->dimensions[1]; + n1 = PyArray_DIMS(self)[0]; + n2 = PyArray_DIMS(self)[1]; step = n2 + 1; if (offset < 0) { start = -n2 * offset; @@ -1592,7 +1583,7 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) /* count = ceil((stop-start)/step) */ count = ((stop-start) / step) + (((stop-start) % step) != 0); - indices = PyArray_New(&PyArray_Type, 1, &count, + indices = (PyArrayObject *)PyArray_New(&PyArray_Type, 1, &count, PyArray_INTP, NULL, NULL, 0, 0, NULL); if (indices == NULL) { Py_DECREF(self); @@ -1608,7 +1599,7 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) Py_DECREF(indices); return NULL; } - ret = PyObject_GetItem(a, indices); + ret = PyObject_GetItem(a, (PyObject *)indices); Py_DECREF(a); Py_DECREF(indices); return ret; @@ -1628,13 +1619,13 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) new = NULL; - typecode = self->descr; + typecode = PyArray_DESCR(self); mydiagonal = PyList_New(0); if (mydiagonal == NULL) { Py_DECREF(self); return NULL; } - n1 = self->dimensions[0]; + n1 = PyArray_DIMS(self)[0]; for (i = 0; i < n1; i++) { new = PyInt_FromLong((long) i); sel = PyArray_EnsureAnyArray(PyObject_GetItem((PyObject *)self, new)); @@ -1644,7 +1635,7 @@ PyArray_Diagonal(PyArrayObject *self, int offset, int axis1, int axis2) Py_DECREF(mydiagonal); return NULL; } - new = PyArray_Diagonal((PyAO *)sel, offset, n-3, n-2); + new = PyArray_Diagonal((PyArrayObject *)sel, offset, n-3, n-2); Py_DECREF(sel); if (new == NULL) { Py_DECREF(self); @@ -1677,11 +1668,11 @@ PyArray_Compress(PyArrayObject *self, PyObject *condition, int axis, PyArrayObject *cond; PyObject *res, *ret; - cond = (PyAO *)PyArray_FROM_O(condition); + cond = (PyArrayObject *)PyArray_FROM_O(condition); if (cond == NULL) { return NULL; } - if (cond->nd != 1) { + if (PyArray_NDIM(cond) != 1) { Py_DECREF(cond); PyErr_SetString(PyExc_ValueError, "condition must be 1-d array"); @@ -1707,7 +1698,7 @@ PyArray_Compress(PyArrayObject *self, PyObject *condition, int axis, NPY_NO_EXPORT npy_intp PyArray_CountNonzero(PyArrayObject *self) { - PyArray_NonzeroFunc *nonzero = self->descr->f->nonzero; + PyArray_NonzeroFunc *nonzero = PyArray_DESCR(self)->f->nonzero; char *data; npy_intp stride, count; npy_intp nonzero_count = 0; @@ -1791,7 +1782,7 @@ PyArray_Nonzero(PyArrayObject *self) PyArrayObject *ret = NULL; PyObject *ret_tuple; npy_intp ret_dims[2]; - PyArray_NonzeroFunc *nonzero = self->descr->f->nonzero; + PyArray_NonzeroFunc *nonzero = PyArray_DESCR(self)->f->nonzero; char *data; npy_intp stride, count; npy_intp nonzero_count = PyArray_CountNonzero(self); @@ -1888,7 +1879,7 @@ finish: /* Create views into ret, one for each dimension */ if (ndim == 1) { /* Directly switch to one dimensions (dimension 1 is 1 anyway) */ - ret->nd = 1; + ((PyArrayObject_fieldaccess *)ret)->nd = 1; PyTuple_SET_ITEM(ret_tuple, 0, (PyObject *)ret); } else { @@ -1907,7 +1898,10 @@ finish: return NULL; } Py_INCREF(ret); - view->base = (PyObject *)ret; + if (PyArray_SetBaseObject(view, (PyObject *)ret) < 0) { + Py_DECREF(ret); + Py_DECREF(ret_tuple); + } PyTuple_SET_ITEM(ret_tuple, i, (PyObject *)view); } diff --git a/numpy/core/src/multiarray/iterators.c b/numpy/core/src/multiarray/iterators.c index 3618febf2..e1d44740f 100644 --- a/numpy/core/src/multiarray/iterators.c +++ b/numpy/core/src/multiarray/iterators.c @@ -22,7 +22,8 @@ #define SingleIndex -3 NPY_NO_EXPORT npy_intp -parse_subindex(PyObject *op, npy_intp *step_size, npy_intp *n_steps, npy_intp max) +parse_subindex(PyObject *op, npy_intp *step_size, + npy_intp *n_steps, npy_intp max) { npy_intp index; @@ -116,8 +117,8 @@ parse_index(PyArrayObject *self, PyObject *op, } } start = parse_subindex(op1, &step_size, &n_steps, - nd_old < self->nd ? - self->dimensions[nd_old] : 0); + nd_old < PyArray_NDIM(self) ? + PyArray_DIMS(self)[nd_old] : 0); Py_DECREF(op1); if (start == -1) { break; @@ -135,7 +136,7 @@ parse_index(PyArrayObject *self, PyObject *op, } Py_DECREF(op1); } - n_add = self->nd-(n-i-n_pseudo-1+nd_old); + n_add = PyArray_NDIM(self)-(n-i-n_pseudo-1+nd_old); if (n_add < 0) { PyErr_SetString(PyExc_IndexError, "too many indices"); @@ -143,24 +144,24 @@ parse_index(PyArrayObject *self, PyObject *op, } for (j = 0; j < n_add; j++) { dimensions[nd_new] = \ - self->dimensions[nd_old]; + PyArray_DIMS(self)[nd_old]; strides[nd_new] = \ - self->strides[nd_old]; + PyArray_STRIDES(self)[nd_old]; nd_new++; nd_old++; } } else { - if (nd_old >= self->nd) { + if (nd_old >= PyArray_NDIM(self)) { PyErr_SetString(PyExc_IndexError, "too many indices"); return -1; } - offset += self->strides[nd_old]*start; + offset += PyArray_STRIDES(self)[nd_old]*start; nd_old++; if (n_steps != SingleIndex) { dimensions[nd_new] = n_steps; strides[nd_new] = step_size * \ - self->strides[nd_old-1]; + PyArray_STRIDES(self)[nd_old-1]; nd_new++; } } @@ -169,10 +170,10 @@ parse_index(PyArrayObject *self, PyObject *op, if (i < n) { return -1; } - n_add = self->nd-nd_old; + n_add = PyArray_NDIM(self)-nd_old; for (j = 0; j < n_add; j++) { - dimensions[nd_new] = self->dimensions[nd_old]; - strides[nd_new] = self->strides[nd_old]; + dimensions[nd_new] = PyArray_DIMS(self)[nd_old]; + strides[nd_new] = PyArray_STRIDES(self)[nd_old]; nd_new++; nd_old++; } @@ -276,9 +277,9 @@ get_ptr_simple(PyArrayIterObject* iter, npy_intp *coordinates) npy_intp i; char *ret; - ret = iter->ao->data; + ret = PyArray_DATA(iter->ao); - for(i = 0; i < iter->ao->nd; ++i) { + for(i = 0; i < PyArray_NDIM(iter->ao); ++i) { ret += coordinates[i] * iter->strides[i]; } @@ -296,7 +297,7 @@ array_iter_base_init(PyArrayIterObject *it, PyArrayObject *ao) { int nd, i; - nd = ao->nd; + nd = PyArray_NDIM(ao); PyArray_UpdateFlags(ao, NPY_ARRAY_C_CONTIGUOUS); if (PyArray_ISCONTIGUOUS(ao)) { it->contiguous = 1; @@ -310,16 +311,16 @@ array_iter_base_init(PyArrayIterObject *it, PyArrayObject *ao) it->nd_m1 = nd - 1; it->factors[nd-1] = 1; for (i = 0; i < nd; i++) { - it->dims_m1[i] = ao->dimensions[i] - 1; - it->strides[i] = ao->strides[i]; + it->dims_m1[i] = PyArray_DIMS(ao)[i] - 1; + it->strides[i] = PyArray_STRIDES(ao)[i]; it->backstrides[i] = it->strides[i] * it->dims_m1[i]; if (i > 0) { - it->factors[nd-i-1] = it->factors[nd-i] * ao->dimensions[nd-i]; + it->factors[nd-i-1] = it->factors[nd-i] * PyArray_DIMS(ao)[nd-i]; } it->bounds[i][0] = 0; - it->bounds[i][1] = ao->dimensions[i] - 1; + it->bounds[i][1] = PyArray_DIMS(ao)[i] - 1; it->limits[i][0] = 0; - it->limits[i][1] = ao->dimensions[i] - 1; + it->limits[i][1] = PyArray_DIMS(ao)[i] - 1; it->limits_sizes[i] = it->limits[i][1] - it->limits[i][0] + 1; } @@ -370,16 +371,16 @@ PyArray_BroadcastToShape(PyObject *obj, npy_intp *dims, int nd) int i, diff, j, compat, k; PyArrayObject *ao = (PyArrayObject *)obj; - if (ao->nd > nd) { + if (PyArray_NDIM(ao) > nd) { goto err; } compat = 1; - diff = j = nd - ao->nd; - for (i = 0; i < ao->nd; i++, j++) { - if (ao->dimensions[i] == 1) { + diff = j = nd - PyArray_NDIM(ao); + for (i = 0; i < PyArray_NDIM(ao); i++, j++) { + if (PyArray_DIMS(ao)[i] == 1) { continue; } - if (ao->dimensions[i] != dims[j]) { + if (PyArray_DIMS(ao)[i] != dims[j]) { compat = 0; break; } @@ -408,12 +409,12 @@ PyArray_BroadcastToShape(PyObject *obj, npy_intp *dims, int nd) for (i = 0; i < nd; i++) { it->dims_m1[i] = dims[i] - 1; k = i - diff; - if ((k < 0) || ao->dimensions[k] != dims[i]) { + if ((k < 0) || PyArray_DIMS(ao)[k] != dims[i]) { it->contiguous = 0; it->strides[i] = 0; } else { - it->strides[i] = ao->strides[k]; + it->strides[i] = PyArray_STRIDES(ao)[k]; } it->backstrides[i] = it->strides[i] * it->dims_m1[i]; if (i > 0) { @@ -441,28 +442,37 @@ PyArray_BroadcastToShape(PyObject *obj, npy_intp *dims, int nd) NPY_NO_EXPORT PyObject * PyArray_IterAllButAxis(PyObject *obj, int *inaxis) { + PyArrayObject *arr; PyArrayIterObject *it; int axis; - it = (PyArrayIterObject *)PyArray_IterNew(obj); + + if (!PyArray_Check(obj)) { + PyErr_SetString(PyExc_ValueError, + "Numpy IterAllButAxis requires an ndarray"); + return NULL; + } + arr = (PyArrayObject *)obj; + + it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arr); if (it == NULL) { return NULL; } - if (PyArray_NDIM(obj)==0) { + if (PyArray_NDIM(arr)==0) { return (PyObject *)it; } if (*inaxis < 0) { int i, minaxis = 0; npy_intp minstride = 0; i = 0; - while (minstride == 0 && i < PyArray_NDIM(obj)) { - minstride = PyArray_STRIDE(obj,i); + while (minstride == 0 && i < PyArray_NDIM(arr)) { + minstride = PyArray_STRIDE(arr,i); i++; } - for (i = 1; i < PyArray_NDIM(obj); i++) { - if (PyArray_STRIDE(obj,i) > 0 && - PyArray_STRIDE(obj, i) < minstride) { + for (i = 1; i < PyArray_NDIM(arr); i++) { + if (PyArray_STRIDE(arr,i) > 0 && + PyArray_STRIDE(arr, i) < minstride) { minaxis = i; - minstride = PyArray_STRIDE(obj,i); + minstride = PyArray_STRIDE(arr,i); } } *inaxis = minaxis; @@ -471,7 +481,7 @@ PyArray_IterAllButAxis(PyObject *obj, int *inaxis) /* adjust so that will not iterate over axis */ it->contiguous = 0; if (it->size != 0) { - it->size /= PyArray_DIM(obj,axis); + it->size /= PyArray_DIM(arr,axis); } it->dims_m1[axis] = 0; it->backstrides[axis] = 0; @@ -560,32 +570,32 @@ iter_length(PyArrayIterObject *self) } -static PyObject * +static PyArrayObject * iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind) { npy_intp index, strides; int itemsize; npy_intp count = 0; char *dptr, *optr; - PyObject *r; + PyArrayObject *ret; int swap; PyArray_CopySwapFunc *copyswap; - if (ind->nd != 1) { + if (PyArray_NDIM(ind) != 1) { PyErr_SetString(PyExc_ValueError, "boolean index array should have 1 dimension"); return NULL; } - index = ind->dimensions[0]; + index = PyArray_DIMS(ind)[0]; if (index > self->size) { PyErr_SetString(PyExc_ValueError, "too many boolean indices"); return NULL; } - strides = ind->strides[0]; - dptr = ind->data; + strides = PyArray_STRIDES(ind)[0]; + dptr = PyArray_DATA(ind); /* Get size of return array */ while (index--) { if (*((Bool *)dptr) != 0) { @@ -593,22 +603,22 @@ iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind) } dptr += strides; } - itemsize = self->ao->descr->elsize; - Py_INCREF(self->ao->descr); - r = PyArray_NewFromDescr(Py_TYPE(self->ao), - self->ao->descr, 1, &count, + itemsize = PyArray_DESCR(self->ao)->elsize; + Py_INCREF(PyArray_DESCR(self->ao)); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao), + PyArray_DESCR(self->ao), 1, &count, NULL, NULL, 0, (PyObject *)self->ao); - if (r == NULL) { + if (ret == NULL) { return NULL; } /* Set up loop */ - optr = PyArray_DATA(r); - index = ind->dimensions[0]; - dptr = ind->data; - copyswap = self->ao->descr->f->copyswap; + optr = PyArray_DATA(ret); + index = PyArray_DIMS(ind)[0]; + dptr = PyArray_DATA(ind); + copyswap = PyArray_DESCR(self->ao)->f->copyswap; /* Loop over Boolean array */ - swap = (PyArray_ISNOTSWAPPED(self->ao) != PyArray_ISNOTSWAPPED(r)); + swap = (PyArray_ISNOTSWAPPED(self->ao) != PyArray_ISNOTSWAPPED(ret)); while (index--) { if (*((Bool *)dptr) != 0) { copyswap(optr, self->dataptr, swap, self->ao); @@ -618,14 +628,14 @@ iter_subscript_Bool(PyArrayIterObject *self, PyArrayObject *ind) PyArray_ITER_NEXT(self); } PyArray_ITER_RESET(self); - return r; + return ret; } static PyObject * iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind) { npy_intp num; - PyObject *r; + PyArrayObject *ret; PyArrayIterObject *ind_it; int itemsize; int swap; @@ -633,9 +643,9 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind) npy_intp index; PyArray_CopySwapFunc *copyswap; - itemsize = self->ao->descr->elsize; - if (ind->nd == 0) { - num = *((npy_intp *)ind->data); + itemsize = PyArray_DESCR(self->ao)->elsize; + if (PyArray_NDIM(ind) == 0) { + num = *((npy_intp *)PyArray_DATA(ind)); if (num < 0) { num += self->size; } @@ -644,33 +654,37 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind) "index %"INTP_FMT" out of bounds" \ " 0<=index<%"INTP_FMT, num, self->size); - r = NULL; + PyArray_ITER_RESET(self); + return NULL; } else { + PyObject *tmp; PyArray_ITER_GOTO1D(self, num); - r = PyArray_ToScalar(self->dataptr, self->ao); + tmp = PyArray_ToScalar(self->dataptr, self->ao); + PyArray_ITER_RESET(self); + return tmp; } - PyArray_ITER_RESET(self); - return r; } - Py_INCREF(self->ao->descr); - r = PyArray_NewFromDescr(Py_TYPE(self->ao), self->ao->descr, - ind->nd, ind->dimensions, + Py_INCREF(PyArray_DESCR(self->ao)); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao), + PyArray_DESCR(self->ao), + PyArray_NDIM(ind), + PyArray_DIMS(ind), NULL, NULL, 0, (PyObject *)self->ao); - if (r == NULL) { + if (ret == NULL) { return NULL; } - optr = PyArray_DATA(r); + optr = PyArray_DATA(ret); ind_it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)ind); if (ind_it == NULL) { - Py_DECREF(r); + Py_DECREF(ret); return NULL; } index = ind_it->size; - copyswap = PyArray_DESCR(r)->f->copyswap; - swap = (PyArray_ISNOTSWAPPED(r) != PyArray_ISNOTSWAPPED(self->ao)); + copyswap = PyArray_DESCR(ret)->f->copyswap; + swap = (PyArray_ISNOTSWAPPED(ret) != PyArray_ISNOTSWAPPED(self->ao)); while (index--) { num = *((npy_intp *)(ind_it->dataptr)); if (num < 0) { @@ -682,18 +696,18 @@ iter_subscript_int(PyArrayIterObject *self, PyArrayObject *ind) " 0<=index<%"INTP_FMT, num, self->size); Py_DECREF(ind_it); - Py_DECREF(r); + Py_DECREF(ret); PyArray_ITER_RESET(self); return NULL; } PyArray_ITER_GOTO1D(self, num); - copyswap(optr, self->dataptr, swap, r); + copyswap(optr, self->dataptr, swap, ret); optr += itemsize; PyArray_ITER_NEXT(ind_it); } Py_DECREF(ind_it); PyArray_ITER_RESET(self); - return r; + return (PyObject *)ret; } /* Always returns arrays */ @@ -701,9 +715,10 @@ NPY_NO_EXPORT PyObject * iter_subscript(PyArrayIterObject *self, PyObject *ind) { PyArray_Descr *indtype = NULL; + PyArray_Descr *dtype; npy_intp start, step_size; npy_intp n_steps; - PyObject *r; + PyArrayObject *ret; char *dptr; int size; PyObject *obj = NULL; @@ -731,7 +746,7 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind) /* * Tuples >1d not accepted --- i.e. no newaxis * Could implement this with adjusted strides and dimensions in iterator - * Check for Boolean -- this is first becasue Bool is a subclass of Int + * Check for Boolean -- this is first because Bool is a subclass of Int */ PyArray_ITER_RESET(self); @@ -741,13 +756,14 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind) } else { /* empty array */ npy_intp ii = 0; - Py_INCREF(self->ao->descr); - r = PyArray_NewFromDescr(Py_TYPE(self->ao), - self->ao->descr, + dtype = PyArray_DESCR(self->ao); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao), + dtype, 1, &ii, NULL, NULL, 0, (PyObject *)self->ao); - return r; + return (PyObject *)ret; } } @@ -765,30 +781,32 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind) } PyArray_ITER_GOTO1D(self, start) if (n_steps == SingleIndex) { /* Integer */ - r = PyArray_ToScalar(self->dataptr, self->ao); + PyObject *tmp; + tmp = PyArray_ToScalar(self->dataptr, self->ao); PyArray_ITER_RESET(self); - return r; + return tmp; } - size = self->ao->descr->elsize; - Py_INCREF(self->ao->descr); - r = PyArray_NewFromDescr(Py_TYPE(self->ao), - self->ao->descr, + size = PyArray_DESCR(self->ao)->elsize; + dtype = PyArray_DESCR(self->ao); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self->ao), + dtype, 1, &n_steps, NULL, NULL, 0, (PyObject *)self->ao); - if (r == NULL) { + if (ret == NULL) { goto fail; } - dptr = PyArray_DATA(r); - copyswap = PyArray_DESCR(r)->f->copyswap; + dptr = PyArray_DATA(ret); + copyswap = PyArray_DESCR(ret)->f->copyswap; while (n_steps--) { - copyswap(dptr, self->dataptr, 0, r); + copyswap(dptr, self->dataptr, 0, ret); start += step_size; PyArray_ITER_GOTO1D(self, start) dptr += size; } PyArray_ITER_RESET(self); - return r; + return (PyObject *)ret; } /* convert to INTP array if Integer array scalar or List */ @@ -807,12 +825,12 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind) if (PyArray_Check(obj)) { /* Check for Boolean object */ - if (PyArray_TYPE(obj)==PyArray_BOOL) { - r = iter_subscript_Bool(self, (PyArrayObject *)obj); + if (PyArray_TYPE((PyArrayObject *)obj) == NPY_BOOL) { + ret = iter_subscript_Bool(self, (PyArrayObject *)obj); Py_DECREF(indtype); } /* Check for integer array */ - else if (PyArray_ISINTEGER(obj)) { + else if (PyArray_ISINTEGER((PyArrayObject *)obj)) { PyObject *new; new = PyArray_FromAny(obj, indtype, 0, 0, NPY_ARRAY_FORCECAST | NPY_ARRAY_ALIGNED, NULL); @@ -821,13 +839,15 @@ iter_subscript(PyArrayIterObject *self, PyObject *ind) } Py_DECREF(obj); obj = new; - r = iter_subscript_int(self, (PyArrayObject *)obj); + new = iter_subscript_int(self, (PyArrayObject *)obj); + Py_DECREF(obj); + return new; } else { goto fail; } Py_DECREF(obj); - return r; + return (PyObject *)ret; } else { Py_DECREF(indtype); @@ -853,24 +873,24 @@ iter_ass_sub_Bool(PyArrayIterObject *self, PyArrayObject *ind, char *dptr; PyArray_CopySwapFunc *copyswap; - if (ind->nd != 1) { + if (PyArray_NDIM(ind) != 1) { PyErr_SetString(PyExc_ValueError, "boolean index array should have 1 dimension"); return -1; } - index = ind->dimensions[0]; + index = PyArray_DIMS(ind)[0]; if (index > self->size) { PyErr_SetString(PyExc_ValueError, "boolean index array has too many values"); return -1; } - strides = ind->strides[0]; - dptr = ind->data; + strides = PyArray_STRIDES(ind)[0]; + dptr = PyArray_DATA(ind); PyArray_ITER_RESET(self); /* Loop over Boolean array */ - copyswap = self->ao->descr->f->copyswap; + copyswap = PyArray_DESCR(self->ao)->f->copyswap; while (index--) { if (*((Bool *)dptr) != 0) { copyswap(self->dataptr, val->dataptr, swap, self->ao); @@ -895,9 +915,9 @@ iter_ass_sub_int(PyArrayIterObject *self, PyArrayObject *ind, npy_intp index; PyArray_CopySwapFunc *copyswap; - copyswap = self->ao->descr->f->copyswap; - if (ind->nd == 0) { - num = *((npy_intp *)ind->data); + copyswap = PyArray_DESCR(self->ao)->f->copyswap; + if (PyArray_NDIM(ind) == 0) { + num = *((npy_intp *)PyArray_DATA(ind)); PyArray_ITER_GOTO1D(self, num); copyswap(self->dataptr, val->dataptr, swap, self->ao); return 0; @@ -935,7 +955,7 @@ iter_ass_sub_int(PyArrayIterObject *self, PyArrayObject *ind, NPY_NO_EXPORT int iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val) { - PyObject *arrval = NULL; + PyArrayObject *arrval = NULL; PyArrayIterObject *val_it = NULL; PyArray_Descr *type; PyArray_Descr *indtype = NULL; @@ -962,7 +982,7 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val) ind = PyTuple_GET_ITEM(ind, 0); } - type = self->ao->descr; + type = PyArray_DESCR(self->ao); /* * Check for Boolean -- this is first becasue @@ -1003,11 +1023,11 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val) skip: Py_INCREF(type); - arrval = PyArray_FromAny(val, type, 0, 0, 0, NULL); + arrval = (PyArrayObject *)PyArray_FromAny(val, type, 0, 0, 0, NULL); if (arrval == NULL) { return -1; } - val_it = (PyArrayIterObject *)PyArray_IterNew(arrval); + val_it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arrval); if (val_it == NULL) { goto finish; } @@ -1065,7 +1085,7 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val) if (obj != NULL && PyArray_Check(obj)) { /* Check for Boolean object */ - if (PyArray_TYPE(obj)==PyArray_BOOL) { + if (PyArray_TYPE((PyArrayObject *)obj)==PyArray_BOOL) { if (iter_ass_sub_Bool(self, (PyArrayObject *)obj, val_it, swap) < 0) { goto finish; @@ -1073,7 +1093,7 @@ iter_ass_subscript(PyArrayIterObject *self, PyObject *ind, PyObject *val) retval=0; } /* Check for integer array */ - else if (PyArray_ISINTEGER(obj)) { + else if (PyArray_ISINTEGER((PyArrayObject *)obj)) { PyObject *new; Py_INCREF(indtype); new = PyArray_CheckFromAny(obj, indtype, 0, 0, @@ -1116,11 +1136,11 @@ static PyMappingMethods iter_as_mapping = { -static PyObject * +static PyArrayObject * iter_array(PyArrayIterObject *it, PyObject *NPY_UNUSED(op)) { - PyObject *r; + PyArrayObject *ret; npy_intp size; /* Any argument ignored */ @@ -1133,37 +1153,46 @@ iter_array(PyArrayIterObject *it, PyObject *NPY_UNUSED(op)) * to copy back to the old array */ size = PyArray_SIZE(it->ao); - Py_INCREF(it->ao->descr); + Py_INCREF(PyArray_DESCR(it->ao)); if (PyArray_ISCONTIGUOUS(it->ao)) { - r = PyArray_NewFromDescr(&PyArray_Type, - it->ao->descr, + ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, + PyArray_DESCR(it->ao), 1, &size, - NULL, it->ao->data, - it->ao->flags, + NULL, PyArray_DATA(it->ao), + PyArray_FLAGS(it->ao), (PyObject *)it->ao); - if (r == NULL) { + if (ret == NULL) { + return NULL; + } + Py_INCREF(it->ao); + if (PyArray_SetBaseObject(ret, (PyObject *)it->ao) < 0) { + Py_DECREF(ret); return NULL; } } else { - r = PyArray_NewFromDescr(&PyArray_Type, - it->ao->descr, + ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, + PyArray_DESCR(it->ao), 1, &size, NULL, NULL, 0, (PyObject *)it->ao); - if (r == NULL) { + if (ret == NULL) { return NULL; } - if (PyArray_CopyAnyInto((PyArrayObject *)r, it->ao) < 0) { - Py_DECREF(r); + if (PyArray_CopyAnyInto(ret, it->ao) < 0) { + Py_DECREF(ret); return NULL; } - PyArray_FLAGS(r) |= NPY_ARRAY_UPDATEIFCOPY; - it->ao->flags &= ~NPY_ARRAY_WRITEABLE; + /* + * Don't use PyArray_SetBaseObject, because that compresses + * the chain of bases. + */ + Py_INCREF(it->ao); + ((PyArrayObject_fieldaccess *)ret)->base = (PyObject *)it->ao; + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_UPDATEIFCOPY); + PyArray_CLEARFLAGS(it->ao, NPY_ARRAY_WRITEABLE); } - Py_INCREF(it->ao); - PyArray_BASE(r) = (PyObject *)it->ao; - return r; + return ret; } @@ -1218,7 +1247,7 @@ static PyObject * iter_coords_get(PyArrayIterObject *self) { int nd; - nd = self->ao->nd; + nd = PyArray_NDIM(self->ao); if (self->contiguous) { /* * coordinates not kept track of --- @@ -1324,7 +1353,7 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit) /* Discover the broadcast number of dimensions */ for (i = 0, nd = 0; i < mit->numiter; i++) { - nd = MAX(nd, mit->iters[i]->ao->nd); + nd = MAX(nd, PyArray_NDIM(mit->iters[i]->ao)); } mit->nd = nd; @@ -1334,9 +1363,9 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit) for (j = 0; j < mit->numiter; j++) { it = mit->iters[j]; /* This prepends 1 to shapes not already equal to nd */ - k = i + it->ao->nd - nd; + k = i + PyArray_NDIM(it->ao) - nd; if (k >= 0) { - tmp = it->ao->dimensions[k]; + tmp = PyArray_DIMS(it->ao)[k]; if (tmp == 1) { continue; } @@ -1370,7 +1399,7 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit) it = mit->iters[i]; it->nd_m1 = mit->nd - 1; it->size = tmp; - nd = it->ao->nd; + nd = PyArray_NDIM(it->ao); it->factors[mit->nd-1] = 1; for (j = 0; j < mit->nd; j++) { it->dims_m1[j] = mit->dimensions[j] - 1; @@ -1380,12 +1409,12 @@ PyArray_Broadcast(PyArrayMultiIterObject *mit) * underlying array was 1 */ if ((k < 0) || - it->ao->dimensions[k] != mit->dimensions[j]) { + PyArray_DIMS(it->ao)[k] != mit->dimensions[j]) { it->contiguous = 0; it->strides[j] = 0; } else { - it->strides[j] = it->ao->strides[k]; + it->strides[j] = PyArray_STRIDES(it->ao)[k]; } it->backstrides[j] = it->strides[j] * it->dims_m1[j]; if (j > 0) @@ -1796,22 +1825,22 @@ static char* _set_constant(PyArrayNeighborhoodIterObject* iter, PyArrayIterObject *ar = iter->_internal_iter; int storeflags, st; - ret = PyDataMem_NEW(ar->ao->descr->elsize); + ret = PyDataMem_NEW(PyArray_DESCR(ar->ao)->elsize); if (ret == NULL) { PyErr_SetNone(PyExc_MemoryError); return NULL; } if (PyArray_ISOBJECT(ar->ao)) { - memcpy(ret, fill->data, sizeof(PyObject*)); + memcpy(ret, PyArray_DATA(fill), sizeof(PyObject*)); Py_INCREF(*(PyObject**)ret); } else { /* Non-object types */ - storeflags = ar->ao->flags; - ar->ao->flags |= NPY_ARRAY_BEHAVED; - st = ar->ao->descr->f->setitem((PyObject*)fill, ret, ar->ao); - ar->ao->flags = storeflags; + storeflags = PyArray_FLAGS(ar->ao); + PyArray_ENABLEFLAGS(ar->ao, NPY_ARRAY_BEHAVED); + st = PyArray_DESCR(ar->ao)->f->setitem((PyObject*)fill, ret, ar->ao); + ((PyArrayObject_fieldaccess *)ar->ao)->flags = storeflags; if (st < 0) { PyDataMem_FREE(ret); @@ -1958,10 +1987,10 @@ PyArray_NeighborhoodIterNew(PyArrayIterObject *x, npy_intp *bounds, Py_INCREF(x); ret->_internal_iter = x; - ret->nd = x->ao->nd; + ret->nd = PyArray_NDIM(x->ao); for (i = 0; i < ret->nd; ++i) { - ret->dimensions[i] = x->ao->dimensions[i]; + ret->dimensions[i] = PyArray_DIMS(x->ao)[i]; } /* Compute the neighborhood size and copy the shape */ diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index 08538e492..5cd6531d4 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -32,8 +32,8 @@ array_subscript_simple(PyArrayObject *self, PyObject *op); NPY_NO_EXPORT Py_ssize_t array_length(PyArrayObject *self) { - if (self->nd != 0) { - return self->dimensions[0]; + if (PyArray_NDIM(self) != 0) { + return PyArray_DIMS(self)[0]; } else { PyErr_SetString(PyExc_TypeError, "len() of unsized object"); return -1; @@ -46,7 +46,7 @@ array_big_item(PyArrayObject *self, intp i) char *item; PyArrayObject *r; - if(self->nd == 0) { + if(PyArray_NDIM(self) == 0) { PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed"); return NULL; @@ -54,19 +54,22 @@ array_big_item(PyArrayObject *self, intp i) if ((item = index2ptr(self, i)) == NULL) { return NULL; } - Py_INCREF(self->descr); + Py_INCREF(PyArray_DESCR(self)); r = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), - self->descr, - self->nd-1, - self->dimensions+1, - self->strides+1, item, - self->flags, + PyArray_DESCR(self), + PyArray_NDIM(self)-1, + PyArray_DIMS(self)+1, + PyArray_STRIDES(self)+1, item, + PyArray_FLAGS(self), (PyObject *)self); if (r == NULL) { return NULL; } Py_INCREF(self); - r->base = (PyObject *)self; + if (PyArray_SetBaseObject(r, (PyObject *)self) < 0) { + Py_DECREF(r); + return NULL; + } PyArray_UpdateFlags(r, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); return (PyObject *)r; } @@ -80,12 +83,12 @@ _array_ass_item(PyArrayObject *self, Py_ssize_t i, PyObject *v) NPY_NO_EXPORT PyObject * array_item_nice(PyArrayObject *self, Py_ssize_t i) { - if (self->nd == 1) { + if (PyArray_NDIM(self) == 1) { char *item; if ((item = index2ptr(self, i)) == NULL) { return NULL; } - return PyArray_Scalar(item, self->descr, (PyObject *)self); + return PyArray_Scalar(item, PyArray_DESCR(self), (PyObject *)self); } else { return PyArray_Return( @@ -110,14 +113,14 @@ array_ass_big_item(PyArrayObject *self, intp i, PyObject *v) "array is not writeable"); return -1; } - if (self->nd == 0) { + if (PyArray_NDIM(self) == 0) { PyErr_SetString(PyExc_IndexError, "0-d arrays can't be indexed."); return -1; } - if (self->nd > 1) { + if (PyArray_NDIM(self) > 1) { if((tmp = (PyArrayObject *)array_big_item(self, i)) == NULL) { return -1; } @@ -129,7 +132,7 @@ array_ass_big_item(PyArrayObject *self, intp i, PyObject *v) if ((item = index2ptr(self, i)) == NULL) { return -1; } - if (self->descr->f->setitem(v, item, self) == -1) { + if (PyArray_DESCR(self)->f->setitem(v, item, self) == -1) { return -1; } return 0; @@ -155,11 +158,11 @@ _swap_axes(PyArrayMapIterObject *mit, PyArrayObject **ret, int getmap) * and need to be reshaped first by pre-pending ones */ arr = *ret; - if (arr->nd != mit->nd) { - for (i = 1; i <= arr->nd; i++) { - permute.ptr[mit->nd-i] = arr->dimensions[arr->nd-i]; + if (PyArray_NDIM(arr) != mit->nd) { + for (i = 1; i <= PyArray_NDIM(arr); i++) { + permute.ptr[mit->nd-i] = PyArray_DIMS(arr)[PyArray_NDIM(arr)-i]; } - for (i = 0; i < mit->nd-arr->nd; i++) { + for (i = 0; i < mit->nd-PyArray_NDIM(arr); i++) { permute.ptr[i] = 1; } new = PyArray_Newshape(arr, &permute, PyArray_ANYORDER); @@ -232,10 +235,10 @@ PyArray_GetMap(PyArrayMapIterObject *mit) of the new array in nd and dimensions. */ temp = mit->ait->ao; - Py_INCREF(temp->descr); + Py_INCREF(PyArray_DESCR(temp)); ret = (PyArrayObject *) PyArray_NewFromDescr(Py_TYPE(temp), - temp->descr, + PyArray_DESCR(temp), mit->nd, mit->dimensions, NULL, NULL, PyArray_ISFORTRAN(temp), @@ -256,7 +259,7 @@ PyArray_GetMap(PyArrayMapIterObject *mit) } index = it->size; swap = (PyArray_ISNOTSWAPPED(temp) != PyArray_ISNOTSWAPPED(ret)); - copyswap = ret->descr->f->copyswap; + copyswap = PyArray_DESCR(ret)->f->copyswap; PyArray_MapIterReset(mit); while (index--) { copyswap(it->dataptr, mit->dataptr, swap, ret); @@ -277,7 +280,7 @@ PyArray_GetMap(PyArrayMapIterObject *mit) static int PyArray_SetMap(PyArrayMapIterObject *mit, PyObject *op) { - PyObject *arr = NULL; + PyArrayObject *arr = NULL; PyArrayIterObject *it; int index; int swap; @@ -288,15 +291,16 @@ PyArray_SetMap(PyArrayMapIterObject *mit, PyObject *op) if (mit->ait == NULL) { return -1; } - descr = mit->ait->ao->descr; + descr = PyArray_DESCR(mit->ait->ao); Py_INCREF(descr); - arr = PyArray_FromAny(op, descr, 0, 0, NPY_ARRAY_FORCECAST, NULL); + arr = (PyArrayObject *)PyArray_FromAny(op, descr, + 0, 0, NPY_ARRAY_FORCECAST, NULL); if (arr == NULL) { return -1; } if ((mit->subspace != NULL) && (mit->consec)) { if (mit->iteraxes[0] > 0) { /* then we need to swap */ - _swap_axes(mit, (PyArrayObject **)&arr, 0); + _swap_axes(mit, &arr, 0); if (arr == NULL) { return -1; } @@ -307,7 +311,8 @@ PyArray_SetMap(PyArrayMapIterObject *mit, PyObject *op) to shape of mit->dimensions, mit->nd */ if ((it = (PyArrayIterObject *)\ - PyArray_BroadcastToShape(arr, mit->dimensions, mit->nd))==NULL) { + PyArray_BroadcastToShape((PyObject *)arr, + mit->dimensions, mit->nd))==NULL) { Py_DECREF(arr); return -1; } @@ -391,16 +396,21 @@ add_new_axes_0d(PyArrayObject *arr, int newaxis_count) for (i = 0; i < newaxis_count; ++i) { dimensions[i] = 1; } - Py_INCREF(arr->descr); - if ((other = (PyArrayObject *) - PyArray_NewFromDescr(Py_TYPE(arr), arr->descr, - newaxis_count, dimensions, - NULL, arr->data, - arr->flags, - (PyObject *)arr)) == NULL) + Py_INCREF(PyArray_DESCR(arr)); + other = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(arr), + PyArray_DESCR(arr), + newaxis_count, dimensions, + NULL, PyArray_DATA(arr), + PyArray_FLAGS(arr), + (PyObject *)arr); + if (other == NULL) { return NULL; - other->base = (PyObject *)arr; + } Py_INCREF(arr); + if (PyArray_SetBaseObject(other, (PyObject *)arr) < 0) { + Py_DECREF(other); + return NULL; + } return (PyObject *)other; } @@ -422,8 +432,8 @@ fancy_indexing_check(PyObject *args) for (i = 0; i < n; i++) { obj = PyTuple_GET_ITEM(args,i); if (PyArray_Check(obj)) { - if (PyArray_ISINTEGER(obj) || - PyArray_ISBOOL(obj)) { + if (PyArray_ISINTEGER((PyArrayObject *)obj) || + PyArray_ISBOOL((PyArrayObject *)obj)) { retval = SOBJ_ISFANCY; } else { @@ -437,8 +447,8 @@ fancy_indexing_check(PyObject *args) } } else if (PyArray_Check(args)) { - if ((PyArray_TYPE(args)==PyArray_BOOL) || - (PyArray_ISINTEGER(args))) { + if ((PyArray_TYPE((PyArrayObject *)args)==NPY_BOOL) || + (PyArray_ISINTEGER((PyArrayObject *)args))) { return SOBJ_ISFANCY; } else { @@ -463,7 +473,8 @@ fancy_indexing_check(PyObject *args) return SOBJ_ISFANCY; } if (PyArray_Check(obj)) { - if (PyArray_ISINTEGER(obj) || PyArray_ISBOOL(obj)) { + if (PyArray_ISINTEGER((PyArrayObject *)obj) || + PyArray_ISBOOL((PyArrayObject *)obj)) { retval = SOBJ_LISTTUP; } else { @@ -503,35 +514,44 @@ fancy_indexing_check(PyObject *args) NPY_NO_EXPORT PyObject * array_subscript_simple(PyArrayObject *self, PyObject *op) { - intp dimensions[MAX_DIMS], strides[MAX_DIMS]; - intp offset; + npy_intp dimensions[MAX_DIMS], strides[MAX_DIMS]; + npy_intp offset; int nd; PyArrayObject *other; - intp value; + npy_intp value; value = PyArray_PyIntAsIntp(op); - if (!PyErr_Occurred()) { + if (value == -1 && PyErr_Occurred()) { + PyErr_Clear(); + } + else { return array_big_item(self, value); } - PyErr_Clear(); /* Standard (view-based) Indexing */ - if ((nd = parse_index(self, op, dimensions, strides, &offset)) == -1) { + nd = parse_index(self, op, dimensions, strides, &offset); + if (nd == -1) { return NULL; } + /* This will only work if new array will be a view */ - Py_INCREF(self->descr); - if ((other = (PyArrayObject *) - PyArray_NewFromDescr(Py_TYPE(self), self->descr, - nd, dimensions, - strides, self->data+offset, - self->flags, - (PyObject *)self)) == NULL) { + Py_INCREF(PyArray_DESCR(self)); + other = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), + PyArray_DESCR(self), + nd, dimensions, + strides, PyArray_DATA(self)+offset, + PyArray_FLAGS(self), + (PyObject *)self); + if (other == NULL) { return NULL; } - other->base = (PyObject *)self; Py_INCREF(self); + if (PyArray_SetBaseObject(other, (PyObject *)self) < 0) { + Py_DECREF(other); + return NULL; + } PyArray_UpdateFlags(other, NPY_ARRAY_UPDATE_ALL); + return (PyObject *)other; } @@ -546,8 +566,8 @@ array_subscript(PyArrayObject *self, PyObject *op) if (PyString_Check(op) || PyUnicode_Check(op)) { PyObject *temp; - if (PyDataType_HASFIELDS(self->descr)) { - obj = PyDict_GetItem(self->descr->fields, op); + if (PyDataType_HASFIELDS(PyArray_DESCR(self))) { + obj = PyDict_GetItem(PyArray_DESCR(self)->fields, op); if (obj != NULL) { PyArray_Descr *descr; int offset; @@ -574,7 +594,7 @@ array_subscript(PyArrayObject *self, PyObject *op) } /* Check for multiple field access */ - if (PyDataType_HASFIELDS(self->descr) && + if (PyDataType_HASFIELDS(PyArray_DESCR(self)) && PySequence_Check(op) && !PyTuple_Check(op)) { int seqlen, i; @@ -610,7 +630,7 @@ array_subscript(PyArrayObject *self, PyObject *op) return (PyObject *)self; } - if (self->nd == 0) { + if (PyArray_NDIM(self) == 0) { if (op == Py_None) { return add_new_axes_0d(self, 1); } @@ -625,17 +645,17 @@ array_subscript(PyArrayObject *self, PyObject *op) return add_new_axes_0d(self, nd); } /* Allow Boolean mask selection also */ - if ((PyArray_Check(op) && (PyArray_DIMS(op)==0) - && PyArray_ISBOOL(op))) { + if ((PyArray_Check(op) && (PyArray_DIMS((PyArrayObject *)op)==0) + && PyArray_ISBOOL((PyArrayObject *)op))) { if (PyObject_IsTrue(op)) { Py_INCREF(self); return (PyObject *)self; } else { intp oned = 0; - Py_INCREF(self->descr); + Py_INCREF(PyArray_DESCR(self)); return PyArray_NewFromDescr(Py_TYPE(self), - self->descr, + PyArray_DESCR(self), 1, &oned, NULL, NULL, NPY_ARRAY_DEFAULT, @@ -650,7 +670,7 @@ array_subscript(PyArrayObject *self, PyObject *op) if (fancy != SOBJ_NOTFANCY) { int oned; - oned = ((self->nd == 1) && + oned = ((PyArray_NDIM(self) == 1) && !(PyTuple_Check(op) && PyTuple_GET_SIZE(op) > 1)); /* wrap arguments into a mapiter object */ @@ -731,8 +751,8 @@ array_ass_sub_simple(PyArrayObject *self, PyObject *index, PyObject *op) tmp = (PyArrayObject *)tmp0; } - if (PyArray_ISOBJECT(self) && (tmp->nd == 0)) { - ret = tmp->descr->f->setitem(op, tmp->data, tmp); + if (PyArray_ISOBJECT(self) && (PyArray_NDIM(tmp) == 0)) { + ret = PyArray_DESCR(tmp)->f->setitem(op, PyArray_DATA(tmp), tmp); } else { ret = PyArray_CopyObject(tmp, op); @@ -754,7 +774,7 @@ _tuple_of_integers(PyObject *seq, intp *vals, int maxvals) for(i=0; i<maxvals; i++) { obj = PyTuple_GET_ITEM(seq, i); - if ((PyArray_Check(obj) && PyArray_NDIM(obj) > 0) + if ((PyArray_Check(obj) && PyArray_NDIM((PyArrayObject *)obj) > 0) || PyList_Check(obj)) { return -1; } @@ -800,10 +820,10 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op) } if (PyString_Check(index) || PyUnicode_Check(index)) { - if (PyDataType_HASFIELDS(self->descr)) { + if (PyDataType_HASFIELDS(PyArray_DESCR(self))) { PyObject *obj; - obj = PyDict_GetItem(self->descr->fields, index); + obj = PyDict_GetItem(PyArray_DESCR(self)->fields, index); if (obj != NULL) { PyArray_Descr *descr; int offset; @@ -841,7 +861,7 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op) } } - if (self->nd == 0) { + if (PyArray_NDIM(self) == 0) { /* * Several different exceptions to the 0-d no-indexing rule * @@ -853,13 +873,14 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op) if (index == Py_None || (PyTuple_Check(index) && (0 == PyTuple_GET_SIZE(index) || count_new_axes_0d(index) > 0))) { - return self->descr->f->setitem(op, self->data, self); + return PyArray_DESCR(self)->f->setitem(op, PyArray_DATA(self), self); } if (PyBool_Check(index) || PyArray_IsScalar(index, Bool) || - (PyArray_Check(index) && (PyArray_DIMS(index)==0) && - PyArray_ISBOOL(index))) { + (PyArray_Check(index) && + (PyArray_DIMS((PyArrayObject *)index)==0) && + PyArray_ISBOOL((PyArrayObject *)index))) { if (PyObject_IsTrue(index)) { - return self->descr->f->setitem(op, self->data, self); + return PyArray_DESCR(self)->f->setitem(op, PyArray_DATA(self), self); } else { /* don't do anything */ return 0; @@ -870,31 +891,31 @@ array_ass_sub(PyArrayObject *self, PyObject *index, PyObject *op) } /* Integer-tuple */ - if (PyTuple_Check(index) && (PyTuple_GET_SIZE(index) == self->nd) - && (_tuple_of_integers(index, vals, self->nd) >= 0)) { + if (PyTuple_Check(index) && (PyTuple_GET_SIZE(index) == PyArray_NDIM(self)) + && (_tuple_of_integers(index, vals, PyArray_NDIM(self)) >= 0)) { int i; char *item; - for (i = 0; i < self->nd; i++) { + for (i = 0; i < PyArray_NDIM(self); i++) { if (vals[i] < 0) { - vals[i] += self->dimensions[i]; + vals[i] += PyArray_DIMS(self)[i]; } - if ((vals[i] < 0) || (vals[i] >= self->dimensions[i])) { + if ((vals[i] < 0) || (vals[i] >= PyArray_DIMS(self)[i])) { PyErr_Format(PyExc_IndexError, "index (%"INTP_FMT") out of range "\ "(0<=index<%"INTP_FMT") in dimension %d", - vals[i], self->dimensions[i], i); + vals[i], PyArray_DIMS(self)[i], i); return -1; } } item = PyArray_GetPtr(self, vals); - return self->descr->f->setitem(op, item, self); + return PyArray_DESCR(self)->f->setitem(op, item, self); } PyErr_Clear(); fancy = fancy_indexing_check(index); if (fancy != SOBJ_NOTFANCY) { - oned = ((self->nd == 1) && + oned = ((PyArray_NDIM(self) == 1) && !(PyTuple_Check(index) && PyTuple_GET_SIZE(index) > 1)); mit = (PyArrayMapIterObject *) PyArray_MapIterNew(index, oned, fancy); if (mit == NULL) { @@ -951,26 +972,26 @@ array_subscript_nice(PyArrayObject *self, PyObject *op) } } /* optimization for a tuple of integers */ - if (self->nd > 1 && PyTuple_Check(op) && - (PyTuple_GET_SIZE(op) == self->nd) - && (_tuple_of_integers(op, vals, self->nd) >= 0)) { + if (PyArray_NDIM(self) > 1 && PyTuple_Check(op) && + (PyTuple_GET_SIZE(op) == PyArray_NDIM(self)) + && (_tuple_of_integers(op, vals, PyArray_NDIM(self)) >= 0)) { int i; char *item; - for (i = 0; i < self->nd; i++) { + for (i = 0; i < PyArray_NDIM(self); i++) { if (vals[i] < 0) { - vals[i] += self->dimensions[i]; + vals[i] += PyArray_DIMS(self)[i]; } - if ((vals[i] < 0) || (vals[i] >= self->dimensions[i])) { + if ((vals[i] < 0) || (vals[i] >= PyArray_DIMS(self)[i])) { PyErr_Format(PyExc_IndexError, "index (%"INTP_FMT") out of range "\ "(0<=index<%"INTP_FMT") in dimension %d", - vals[i], self->dimensions[i], i); + vals[i], PyArray_DIMS(self)[i], i); return NULL; } } item = PyArray_GetPtr(self, vals); - return PyArray_Scalar(item, self->descr, (PyObject *)self); + return PyArray_Scalar(item, PyArray_DESCR(self), (PyObject *)self); } PyErr_Clear(); @@ -993,14 +1014,15 @@ array_subscript_nice(PyArrayObject *self, PyObject *op) Py_XDECREF(mp); return NULL; } - if (PyArray_Check(mp) && mp->nd == 0) { + if (PyArray_Check(mp) && PyArray_NDIM(mp) == 0) { Bool noellipses = TRUE; if ((op == Py_Ellipsis) || PyString_Check(op) || PyUnicode_Check(op)) { noellipses = FALSE; } else if (PyBool_Check(op) || PyArray_IsScalar(op, Bool) || - (PyArray_Check(op) && (PyArray_DIMS(op)==0) && - PyArray_ISBOOL(op))) { + (PyArray_Check(op) && + (PyArray_DIMS((PyArrayObject *)op)==0) && + PyArray_ISBOOL((PyArrayObject *)op))) { noellipses = FALSE; } else if (PySequence_Check(op)) { @@ -1020,7 +1042,7 @@ array_subscript_nice(PyArrayObject *self, PyObject *op) } if (noellipses) { PyObject *ret; - ret = PyArray_ToScalar(mp->data, mp); + ret = PyArray_ToScalar(PyArray_DATA(mp), mp); Py_DECREF(mp); return ret; } @@ -1071,12 +1093,12 @@ _nonzero_indices(PyObject *myBool, PyArrayIterObject **iters) if (ba == NULL) { return -1; } - nd = ba->nd; + nd = PyArray_NDIM(ba); for (j = 0; j < nd; j++) { iters[j] = NULL; } size = PyArray_SIZE(ba); - ptr = (Bool *)ba->data; + ptr = (Bool *)PyArray_DATA(ba); count = 0; /* pre-determine how many nonzero entries there are */ @@ -1100,11 +1122,11 @@ _nonzero_indices(PyObject *myBool, PyArrayIterObject **iters) if (iters[j] == NULL) { goto fail; } - dptr[j] = (intp *)iters[j]->ao->data; + dptr[j] = (intp *)PyArray_DATA(iters[j]->ao); coords[j] = 0; - dims_m1[j] = ba->dimensions[j]-1; + dims_m1[j] = PyArray_DIMS(ba)[j]-1; } - ptr = (Bool *)ba->data; + ptr = (npy_bool *)PyArray_DATA(ba); if (count == 0) { goto finish; } @@ -1157,7 +1179,7 @@ _convert_obj(PyObject *obj, PyArrayIterObject **iter) if (PySlice_Check(obj) || (obj == Py_Ellipsis)) { return 0; } - else if (PyArray_Check(obj) && PyArray_ISBOOL(obj)) { + else if (PyArray_Check(obj) && PyArray_ISBOOL((PyArrayObject *)obj)) { return _nonzero_indices(obj, iter); } else { @@ -1185,10 +1207,10 @@ PyArray_MapIterReset(PyArrayMapIterObject *mit) mit->index = 0; - copyswap = mit->iters[0]->ao->descr->f->copyswap; + copyswap = PyArray_DESCR(mit->iters[0]->ao)->f->copyswap; if (mit->subspace != NULL) { - memcpy(coord, mit->bscoord, sizeof(intp)*mit->ait->ao->nd); + memcpy(coord, mit->bscoord, sizeof(intp)*PyArray_NDIM(mit->ait->ao)); PyArray_ITER_RESET(mit->subspace); for (i = 0; i < mit->numiter; i++) { it = mit->iters[i]; @@ -1235,13 +1257,14 @@ PyArray_MapIterNext(PyArrayMapIterObject *mit) if (mit->index >= mit->size) { return; } - copyswap = mit->iters[0]->ao->descr->f->copyswap; + copyswap = PyArray_DESCR(mit->iters[0]->ao)->f->copyswap; /* Sub-space iteration */ if (mit->subspace != NULL) { PyArray_ITER_NEXT(mit->subspace); if (mit->subspace->index >= mit->subspace->size) { /* reset coord to coordinates of beginning of the subspace */ - memcpy(coord, mit->bscoord, sizeof(intp)*mit->ait->ao->nd); + memcpy(coord, mit->bscoord, + sizeof(intp)*PyArray_NDIM(mit->ait->ao)); PyArray_ITER_RESET(mit->subspace); for (i = 0; i < mit->numiter; i++) { it = mit->iters[i]; @@ -1277,7 +1300,7 @@ PyArray_MapIterNext(PyArrayMapIterObject *mit) * 2) Create subspace iterator * 3) Update nd, dimensions, and size. * - * Subspace iteration is necessary if: arr->nd > mit->numiter + * Subspace iteration is necessary if: PyArray_NDIM(arr) > mit->numiter * * Need to check for index-errors somewhere. * @@ -1294,7 +1317,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) intp dimsize; intp *indptr; - subnd = arr->nd - mit->numiter; + subnd = PyArray_NDIM(arr) - mit->numiter; if (subnd < 0) { PyErr_SetString(PyExc_ValueError, "too many indices for array"); @@ -1307,7 +1330,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) } /* no subspace iteration needed. Finish up and Return */ if (subnd == 0) { - n = arr->nd; + n = PyArray_NDIM(arr); for (i = 0; i < n; i++) { mit->iteraxes[i] = i; } @@ -1343,9 +1366,9 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) goto fail; } /* Expand dimensions of result */ - n = mit->subspace->ao->nd; + n = PyArray_NDIM(mit->subspace->ao); for (i = 0; i < n; i++) { - mit->dimensions[mit->nd+i] = mit->subspace->ao->dimensions[i]; + mit->dimensions[mit->nd+i] = PyArray_DIMS(mit->subspace->ao)[i]; } mit->nd += n; @@ -1355,7 +1378,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) */ n = PyTuple_GET_SIZE(mit->indexobj); /* The number of dimensions an ellipsis takes up */ - ellipexp = arr->nd - n + 1; + ellipexp = PyArray_NDIM(arr) - n + 1; /* * Now fill in iteraxes -- remember indexing arrays have been * converted to 0's in mit->indexobj @@ -1364,7 +1387,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) j = 0; /* Only expand the first ellipsis */ noellip = 1; - memset(mit->bscoord, 0, sizeof(intp)*arr->nd); + memset(mit->bscoord, 0, sizeof(intp)*PyArray_NDIM(arr)); for (i = 0; i < n; i++) { /* * We need to fill in the starting coordinates for @@ -1387,7 +1410,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) } else if (!PySlice_Check(obj) || (slice_GetIndices((PySliceObject *)obj, - arr->dimensions[curraxis], + PyArray_DIMS(arr)[curraxis], &start, &stop, &step, &dimsize) < 0)) { PyErr_Format(PyExc_ValueError, @@ -1421,7 +1444,7 @@ PyArray_MapIterBind(PyArrayMapIterObject *mit, PyArrayObject *arr) intp indval; it = mit->iters[i]; PyArray_ITER_RESET(it); - dimsize = arr->dimensions[mit->iteraxes[i]]; + dimsize = PyArray_DIMS(arr)[mit->iteraxes[i]]; while (it->index < it->size) { indptr = ((intp *)it->dataptr); indval = *indptr; @@ -1455,7 +1478,7 @@ PyArray_MapIterNew(PyObject *indexobj, int oned, int fancy) { PyArrayMapIterObject *mit; PyArray_Descr *indtype; - PyObject *arr = NULL; + PyArrayObject *arr = NULL; int i, n, started, nonindex; if (fancy == SOBJ_BADARRAY) { @@ -1512,7 +1535,8 @@ PyArray_MapIterNew(PyObject *indexobj, int oned, int fancy) */ /* convert all inputs to iterators */ - if (PyArray_Check(indexobj) && (PyArray_TYPE(indexobj) == PyArray_BOOL)) { + if (PyArray_Check(indexobj) && + (PyArray_TYPE((PyArrayObject *)indexobj) == NPY_BOOL)) { mit->numiter = _nonzero_indices(indexobj, mit->iters); if (mit->numiter < 0) { goto fail; @@ -1532,12 +1556,12 @@ PyArray_MapIterNew(PyObject *indexobj, int oned, int fancy) else if (PyArray_Check(indexobj) || !PyTuple_Check(indexobj)) { mit->numiter = 1; indtype = PyArray_DescrFromType(NPY_INTP); - arr = PyArray_FromAny(indexobj, indtype, 0, 0, + arr = (PyArrayObject *)PyArray_FromAny(indexobj, indtype, 0, 0, NPY_ARRAY_FORCECAST, NULL); if (arr == NULL) { goto fail; } - mit->iters[0] = (PyArrayIterObject *)PyArray_IterNew(arr); + mit->iters[0] = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arr); if (mit->iters[0] == NULL) { Py_DECREF(arr); goto fail; diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 68f697a4d..28fb8f8cf 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -290,26 +290,29 @@ PyArray_GetField(PyArrayObject *self, PyArray_Descr *typed, int offset) { PyObject *ret = NULL; - if (offset < 0 || (offset + typed->elsize) > self->descr->elsize) { + if (offset < 0 || (offset + typed->elsize) > PyArray_DESCR(self)->elsize) { PyErr_Format(PyExc_ValueError, "Need 0 <= offset <= %d for requested type " \ "but received offset = %d", - self->descr->elsize-typed->elsize, offset); + PyArray_DESCR(self)->elsize-typed->elsize, offset); Py_DECREF(typed); return NULL; } ret = PyArray_NewFromDescr(Py_TYPE(self), typed, - self->nd, self->dimensions, - self->strides, - self->data + offset, - self->flags&(~NPY_ARRAY_F_CONTIGUOUS), + PyArray_NDIM(self), PyArray_DIMS(self), + PyArray_STRIDES(self), + PyArray_DATA(self) + offset, + PyArray_FLAGS(self)&(~NPY_ARRAY_F_CONTIGUOUS), (PyObject *)self); if (ret == NULL) { return NULL; } Py_INCREF(self); - ((PyArrayObject *)ret)->base = (PyObject *)self; + if (PyArray_SetBaseObject(((PyArrayObject *)ret), (PyObject *)self) < 0) { + Py_DECREF(ret); + return NULL; + } PyArray_UpdateFlags((PyArrayObject *)ret, NPY_ARRAY_UPDATE_ALL); return ret; @@ -344,23 +347,21 @@ PyArray_SetField(PyArrayObject *self, PyArray_Descr *dtype, PyObject *ret = NULL; int retval = 0; - if (offset < 0 || (offset + dtype->elsize) > self->descr->elsize) { + if (offset < 0 || (offset + dtype->elsize) > PyArray_DESCR(self)->elsize) { PyErr_Format(PyExc_ValueError, "Need 0 <= offset <= %d for requested type " \ "but received offset = %d", - self->descr->elsize-dtype->elsize, offset); + PyArray_DESCR(self)->elsize-dtype->elsize, offset); Py_DECREF(dtype); return -1; } ret = PyArray_NewFromDescr(Py_TYPE(self), - dtype, self->nd, self->dimensions, - self->strides, self->data + offset, - self->flags, (PyObject *)self); + dtype, PyArray_NDIM(self), PyArray_DIMS(self), + PyArray_STRIDES(self), PyArray_DATA(self) + offset, + PyArray_FLAGS(self), (PyObject *)self); if (ret == NULL) { return -1; } - Py_INCREF(self); - ((PyArrayObject *)ret)->base = (PyObject *)self; PyArray_UpdateFlags((PyArrayObject *)ret, NPY_ARRAY_UPDATE_ALL); retval = PyArray_CopyObject((PyArrayObject *)ret, val); @@ -410,7 +411,7 @@ PyArray_Byteswap(PyArrayObject *self, Bool inplace) PyArray_CopySwapNFunc *copyswapn; PyArrayIterObject *it; - copyswapn = self->descr->f->copyswapn; + copyswapn = PyArray_DESCR(self)->f->copyswapn; if (inplace) { if (!PyArray_ISWRITEABLE(self)) { PyErr_SetString(PyExc_RuntimeError, @@ -420,15 +421,15 @@ PyArray_Byteswap(PyArrayObject *self, Bool inplace) } size = PyArray_SIZE(self); if (PyArray_ISONESEGMENT(self)) { - copyswapn(self->data, self->descr->elsize, NULL, -1, size, 1, self); + copyswapn(PyArray_DATA(self), PyArray_DESCR(self)->elsize, NULL, -1, size, 1, self); } else { /* Use iterator */ int axis = -1; intp stride; it = (PyArrayIterObject *) \ PyArray_IterAllButAxis((PyObject *)self, &axis); - stride = self->strides[axis]; - size = self->dimensions[axis]; + stride = PyArray_STRIDES(self)[axis]; + size = PyArray_DIMS(self)[axis]; while (it->index < it->size) { copyswapn(it->dataptr, stride, NULL, -1, size, 1, self); PyArray_ITER_NEXT(it); @@ -549,8 +550,8 @@ array_toscalar(PyArrayObject *self, PyObject *args) { } if (n == 0) { - if (self->nd == 0 || PyArray_SIZE(self) == 1) - return self->descr->f->getitem(self->data, self); + if (PyArray_NDIM(self) == 0 || PyArray_SIZE(self) == 1) + return PyArray_DESCR(self)->f->getitem(PyArray_DATA(self), self); else { PyErr_SetString(PyExc_ValueError, "can only convert an array " \ @@ -558,7 +559,7 @@ array_toscalar(PyArrayObject *self, PyObject *args) { return NULL; } } - else if (n != self->nd && (n > 1 || self->nd == 0)) { + else if (n != PyArray_NDIM(self) && (n > 1 || PyArray_NDIM(self) == 0)) { PyErr_SetString(PyExc_ValueError, "incorrect number of indices for " \ "array"); @@ -579,25 +580,25 @@ array_toscalar(PyArrayObject *self, PyObject *args) { "index out of bounds"); return NULL; } - if (self->nd == 1) { - value *= self->strides[0]; - return self->descr->f->getitem(self->data + value, + if (PyArray_NDIM(self) == 1) { + value *= PyArray_STRIDES(self)[0]; + return PyArray_DESCR(self)->f->getitem(PyArray_DATA(self) + value, self); } - nd = self->nd; + nd = PyArray_NDIM(self); factor = 1; while (nd--) { factors[nd] = factor; - factor *= self->dimensions[nd]; + factor *= PyArray_DIMS(self)[nd]; } loc = 0; - for (nd = 0; nd < self->nd; nd++) { + for (nd = 0; nd < PyArray_NDIM(self); nd++) { index = value / factors[nd]; value = value % factors[nd]; - loc += self->strides[nd]*index; + loc += PyArray_STRIDES(self)[nd]*index; } - return self->descr->f->getitem(self->data + loc, + return PyArray_DESCR(self)->f->getitem(PyArray_DATA(self) + loc, self); } @@ -610,17 +611,17 @@ array_toscalar(PyArrayObject *self, PyObject *args) { loc = 0; while (nd--) { if (index[nd] < 0) { - index[nd] += self->dimensions[nd]; + index[nd] += PyArray_DIMS(self)[nd]; } if (index[nd] < 0 || - index[nd] >= self->dimensions[nd]) { + index[nd] >= PyArray_DIMS(self)[nd]) { PyErr_SetString(PyExc_ValueError, "index out of bounds"); return NULL; } - loc += self->strides[nd]*index[nd]; + loc += PyArray_STRIDES(self)[nd]*index[nd]; } - return self->descr->f->getitem(self->data + loc, self); + return PyArray_DESCR(self)->f->getitem(PyArray_DATA(self) + loc, self); } } @@ -638,8 +639,8 @@ array_setscalar(PyArrayObject *self, PyObject *args) { } obj = PyTuple_GET_ITEM(args, n); if (n == 0) { - if (self->nd == 0 || PyArray_SIZE(self) == 1) { - ret = self->descr->f->setitem(obj, self->data, self); + if (PyArray_NDIM(self) == 0 || PyArray_SIZE(self) == 1) { + ret = PyArray_DESCR(self)->f->setitem(obj, PyArray_DATA(self), self); } else { PyErr_SetString(PyExc_ValueError, @@ -648,7 +649,7 @@ array_setscalar(PyArrayObject *self, PyObject *args) { return NULL; } } - else if (n != self->nd && (n > 1 || self->nd == 0)) { + else if (n != PyArray_NDIM(self) && (n > 1 || PyArray_NDIM(self) == 0)) { PyErr_SetString(PyExc_ValueError, "incorrect number of indices for " \ "array"); @@ -689,26 +690,26 @@ array_setscalar(PyArrayObject *self, PyObject *args) { "index out of bounds"); return NULL; } - if (self->nd == 1) { - value *= self->strides[0]; - ret = self->descr->f->setitem(obj, self->data + value, + if (PyArray_NDIM(self) == 1) { + value *= PyArray_STRIDES(self)[0]; + ret = PyArray_DESCR(self)->f->setitem(obj, PyArray_DATA(self) + value, self); goto finish; } - nd = self->nd; + nd = PyArray_NDIM(self); factor = 1; while (nd--) { factors[nd] = factor; - factor *= self->dimensions[nd]; + factor *= PyArray_DIMS(self)[nd]; } loc = 0; - for (nd = 0; nd < self->nd; nd++) { + for (nd = 0; nd < PyArray_NDIM(self); nd++) { index = value / factors[nd]; value = value % factors[nd]; - loc += self->strides[nd]*index; + loc += PyArray_STRIDES(self)[nd]*index; } - ret = self->descr->f->setitem(obj, self->data + loc, self); + ret = PyArray_DESCR(self)->f->setitem(obj, PyArray_DATA(self) + loc, self); } else { intp loc, index[MAX_DIMS]; @@ -722,17 +723,17 @@ array_setscalar(PyArrayObject *self, PyObject *args) { loc = 0; while (nd--) { if (index[nd] < 0) { - index[nd] += self->dimensions[nd]; + index[nd] += PyArray_DIMS(self)[nd]; } if (index[nd] < 0 || - index[nd] >= self->dimensions[nd]) { + index[nd] >= PyArray_DIMS(self)[nd]) { PyErr_SetString(PyExc_ValueError, "index out of bounds"); return NULL; } - loc += self->strides[nd]*index[nd]; + loc += PyArray_STRIDES(self)[nd]*index[nd]; } - ret = self->descr->f->setitem(obj, self->data + loc, self); + ret = PyArray_DESCR(self)->f->setitem(obj, PyArray_DATA(self) + loc, self); } finish: @@ -878,28 +879,30 @@ array_astype(PyArrayObject *self, PyObject *args, PyObject *kwds) static PyObject * array_wraparray(PyArrayObject *self, PyObject *args) { - PyObject *arr; - PyObject *ret; + PyArrayObject *arr, *ret; + PyObject *obj; if (PyTuple_Size(args) < 1) { PyErr_SetString(PyExc_TypeError, "only accepts 1 argument"); return NULL; } - arr = PyTuple_GET_ITEM(args, 0); - if (arr == NULL) { + obj = PyTuple_GET_ITEM(args, 0); + if (obj == NULL) { return NULL; } - if (!PyArray_Check(arr)) { + if (!PyArray_Check(obj)) { PyErr_SetString(PyExc_TypeError, "can only be called with ndarray object"); return NULL; } + arr = (PyArrayObject *)obj; if (Py_TYPE(self) != Py_TYPE(arr)){ - Py_INCREF(PyArray_DESCR(arr)); - ret = PyArray_NewFromDescr(Py_TYPE(self), - PyArray_DESCR(arr), + PyArray_Descr *dtype = PyArray_DESCR(arr); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), + dtype, PyArray_NDIM(arr), PyArray_DIMS(arr), PyArray_STRIDES(arr), PyArray_DATA(arr), @@ -907,13 +910,16 @@ array_wraparray(PyArrayObject *self, PyObject *args) if (ret == NULL) { return NULL; } - Py_INCREF(arr); - PyArray_BASE(ret) = arr; - return ret; + Py_INCREF(obj); + if (PyArray_SetBaseObject(ret, obj) < 0) { + Py_DECREF(ret); + return NULL; + } + return (PyObject *)ret; } else { /*The type was set in __array_prepare__*/ Py_INCREF(arr); - return arr; + return (PyObject *)arr; } } @@ -921,30 +927,33 @@ array_wraparray(PyArrayObject *self, PyObject *args) static PyObject * array_preparearray(PyArrayObject *self, PyObject *args) { - PyObject *arr; - PyObject *ret; + PyObject *obj; + PyArrayObject *arr, *ret; + PyArray_Descr *dtype; if (PyTuple_Size(args) < 1) { PyErr_SetString(PyExc_TypeError, "only accepts 1 argument"); return NULL; } - arr = PyTuple_GET_ITEM(args, 0); - if (!PyArray_Check(arr)) { + obj = PyTuple_GET_ITEM(args, 0); + if (!PyArray_Check(obj)) { PyErr_SetString(PyExc_TypeError, "can only be called with ndarray object"); return NULL; } + arr = (PyArrayObject *)obj; if (Py_TYPE(self) == Py_TYPE(arr)) { /* No need to create a new view */ Py_INCREF(arr); - return arr; + return (PyObject *)arr; } - Py_INCREF(PyArray_DESCR(arr)); - ret = PyArray_NewFromDescr(Py_TYPE(self), - PyArray_DESCR(arr), + dtype = PyArray_DESCR(arr); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), + dtype, PyArray_NDIM(arr), PyArray_DIMS(arr), PyArray_STRIDES(arr), PyArray_DATA(arr), @@ -953,8 +962,11 @@ array_preparearray(PyArrayObject *self, PyObject *args) return NULL; } Py_INCREF(arr); - PyArray_BASE(ret) = arr; - return ret; + if (PyArray_SetBaseObject(ret, (PyObject *)arr) < 0) { + Py_DECREF(ret); + return NULL; + } + return (PyObject *)ret; } @@ -972,7 +984,7 @@ array_getarray(PyArrayObject *self, PyObject *args) /* convert to PyArray_Type */ if (!PyArray_CheckExact(self)) { - PyObject *new; + PyArrayObject *new; PyTypeObject *subtype = &PyArray_Type; if (!PyType_IsSubtype(Py_TYPE(self), &PyArray_Type)) { @@ -980,7 +992,7 @@ array_getarray(PyArrayObject *self, PyObject *args) } Py_INCREF(PyArray_DESCR(self)); - new = PyArray_NewFromDescr(subtype, + new = (PyArrayObject *)PyArray_NewFromDescr(subtype, PyArray_DESCR(self), PyArray_NDIM(self), PyArray_DIMS(self), @@ -991,15 +1003,14 @@ array_getarray(PyArrayObject *self, PyObject *args) return NULL; } Py_INCREF(self); - PyArray_BASE(new) = (PyObject *)self; - self = (PyArrayObject *)new; + PyArray_SetBaseObject(new, (PyObject *)self); + self = new; } else { Py_INCREF(self); } - if ((newtype == NULL) || - PyArray_EquivTypes(self->descr, newtype)) { + if ((newtype == NULL) || PyArray_EquivTypes(PyArray_DESCR(self), newtype)) { return (PyObject *)self; } else { @@ -1132,7 +1143,7 @@ array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds) if (order != NULL) { PyObject *new_name; PyObject *_numpy_internal; - saved = self->descr; + saved = PyArray_DESCR(self); if (!PyDataType_HASFIELDS(saved)) { PyErr_SetString(PyExc_ValueError, "Cannot specify " \ "order when the array has no fields."); @@ -1150,13 +1161,13 @@ array_sort(PyArrayObject *self, PyObject *args, PyObject *kwds) } newd = PyArray_DescrNew(saved); newd->names = new_name; - self->descr = newd; + ((PyArrayObject_fieldaccess *)self)->descr = newd; } val = PyArray_Sort(self, axis, sortkind); if (order != NULL) { - Py_XDECREF(self->descr); - self->descr = saved; + Py_XDECREF(PyArray_DESCR(self)); + ((PyArrayObject_fieldaccess *)self)->descr = saved; } if (val < 0) { return NULL; @@ -1186,7 +1197,7 @@ array_argsort(PyArrayObject *self, PyObject *args, PyObject *kwds) if (order != NULL) { PyObject *new_name; PyObject *_numpy_internal; - saved = self->descr; + saved = PyArray_DESCR(self); if (!PyDataType_HASFIELDS(saved)) { PyErr_SetString(PyExc_ValueError, "Cannot specify " \ "order when the array has no fields."); @@ -1204,13 +1215,13 @@ array_argsort(PyArrayObject *self, PyObject *args, PyObject *kwds) } newd = PyArray_DescrNew(saved); newd->names = new_name; - self->descr = newd; + ((PyArrayObject_fieldaccess *)self)->descr = newd; } res = PyArray_ArgSort(self, axis, sortkind); if (order != NULL) { - Py_XDECREF(self->descr); - self->descr = saved; + Py_XDECREF(PyArray_DESCR(self)); + ((PyArrayObject_fieldaccess *)self)->descr = saved; } return _ARET(res); } @@ -1276,13 +1287,14 @@ array_deepcopy(PyArrayObject *self, PyObject *args) PyObject* visit; char *optr; PyArrayIterObject *it; - PyObject *copy, *ret, *deepcopy; + PyObject *copy, *deepcopy; + PyArrayObject *ret; if (!PyArg_ParseTuple(args, "O", &visit)) { return NULL; } - ret = PyArray_Copy(self); - if (PyDataType_REFCHK(self->descr)) { + ret = (PyArrayObject *)PyArray_Copy(self); + if (PyDataType_REFCHK(PyArray_DESCR(self))) { copy = PyImport_ImportModule("copy"); if (copy == NULL) { return NULL; @@ -1299,14 +1311,14 @@ array_deepcopy(PyArrayObject *self, PyObject *args) } optr = PyArray_DATA(ret); while(it->index < it->size) { - _deepcopy_call(it->dataptr, optr, self->descr, deepcopy, visit); - optr += self->descr->elsize; + _deepcopy_call(it->dataptr, optr, PyArray_DESCR(self), deepcopy, visit); + optr += PyArray_DESCR(self)->elsize; PyArray_ITER_NEXT(it); } Py_DECREF(deepcopy); Py_DECREF(it); } - return _ARET(ret); + return PyArray_Return(ret); } /* Convert Array to flat list (using getitem) */ @@ -1318,7 +1330,7 @@ _getlist_pkl(PyArrayObject *self) PyObject *list; PyArray_GetItemFunc *getitem; - getitem = self->descr->f->getitem; + getitem = PyArray_DESCR(self)->f->getitem; iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self); if (iter == NULL) { return NULL; @@ -1344,7 +1356,7 @@ _setlist_pkl(PyArrayObject *self, PyObject *list) PyArrayIterObject *iter = NULL; PyArray_SetItemFunc *setitem; - setitem = self->descr->f->setitem; + setitem = PyArray_DESCR(self)->f->setitem; iter = (PyArrayIterObject *)PyArray_IterNew((PyObject *)self); if (iter == NULL) { return -1; @@ -1416,13 +1428,13 @@ array_reduce(PyArrayObject *self, PyObject *NPY_UNUSED(args)) PyTuple_SET_ITEM(state, 0, PyInt_FromLong(version)); PyTuple_SET_ITEM(state, 1, PyObject_GetAttrString((PyObject *)self, "shape")); - descr = self->descr; + descr = PyArray_DESCR(self); Py_INCREF(descr); PyTuple_SET_ITEM(state, 2, (PyObject *)descr); mybool = (PyArray_ISFORTRAN(self) ? Py_True : Py_False); Py_INCREF(mybool); PyTuple_SET_ITEM(state, 3, mybool); - if (PyDataType_FLAGCHK(self->descr, NPY_LIST_PICKLE)) { + if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_LIST_PICKLE)) { thestr = _getlist_pkl(self); } else { @@ -1451,6 +1463,8 @@ array_setstate(PyArrayObject *self, PyObject *args) intp size, dimensions[MAX_DIMS]; int nd; + PyArrayObject_fieldaccess *fa = (PyArrayObject_fieldaccess *)self; + /* This will free any memory associated with a and use the string in setstate as the (writeable) memory. */ @@ -1481,19 +1495,19 @@ array_setstate(PyArrayObject *self, PyObject *args) return NULL; } - Py_XDECREF(self->descr); - self->descr = typecode; + Py_XDECREF(PyArray_DESCR(self)); + fa->descr = typecode; Py_INCREF(typecode); nd = PyArray_IntpFromSequence(shape, dimensions, MAX_DIMS); if (nd < 0) { return NULL; } size = PyArray_MultiplyList(dimensions, nd); - if (self->descr->elsize == 0) { + if (PyArray_DESCR(self)->elsize == 0) { PyErr_SetString(PyExc_ValueError, "Invalid data-type size."); return NULL; } - if (size < 0 || size > MAX_INTP / self->descr->elsize) { + if (size < 0 || size > MAX_INTP / PyArray_DESCR(self)->elsize) { PyErr_NoMemory(); return NULL; } @@ -1530,7 +1544,7 @@ array_setstate(PyArrayObject *self, PyObject *args) return NULL; } - if ((len != (self->descr->elsize * size))) { + if ((len != (PyArray_DESCR(self)->elsize * size))) { PyErr_SetString(PyExc_ValueError, "buffer size does not" \ " match array size"); @@ -1539,39 +1553,39 @@ array_setstate(PyArrayObject *self, PyObject *args) } } - if ((self->flags & NPY_ARRAY_OWNDATA)) { - if (self->data != NULL) { - PyDataMem_FREE(self->data); + if ((PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA)) { + if (PyArray_DATA(self) != NULL) { + PyDataMem_FREE(PyArray_DATA(self)); } - self->flags &= ~NPY_ARRAY_OWNDATA; + PyArray_CLEARFLAGS(self, NPY_ARRAY_OWNDATA); } - Py_XDECREF(self->base); + Py_XDECREF(PyArray_BASE(self)); - self->flags &= ~NPY_ARRAY_UPDATEIFCOPY; + PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY); - if (self->dimensions != NULL) { - PyDimMem_FREE(self->dimensions); - self->dimensions = NULL; + if (PyArray_DIMS(self) != NULL) { + PyDimMem_FREE(PyArray_DIMS(self)); + fa->dimensions = NULL; } - self->flags = NPY_ARRAY_DEFAULT; + fa->flags = NPY_ARRAY_DEFAULT; - self->nd = nd; + fa->nd = nd; if (nd > 0) { - self->dimensions = PyDimMem_NEW(nd * 2); - self->strides = self->dimensions + nd; - memcpy(self->dimensions, dimensions, sizeof(intp)*nd); - (void) _array_fill_strides(self->strides, dimensions, nd, - (size_t) self->descr->elsize, - (is_f_order ? NPY_ARRAY_F_CONTIGUOUS : - NPY_ARRAY_C_CONTIGUOUS), - &(self->flags)); + fa->dimensions = PyDimMem_NEW(nd * 2); + fa->strides = PyArray_DIMS(self) + nd; + memcpy(PyArray_DIMS(self), dimensions, sizeof(intp)*nd); + _array_fill_strides(PyArray_STRIDES(self), dimensions, nd, + PyArray_DESCR(self)->elsize, + (is_f_order ? NPY_ARRAY_F_CONTIGUOUS : + NPY_ARRAY_C_CONTIGUOUS), + &(fa->flags)); } if (!PyDataType_FLAGCHK(typecode, NPY_LIST_PICKLE)) { int swap=!PyArray_ISNOTSWAPPED(self); - self->data = datastr; + fa->data = datastr; #ifndef NPY_PY3K /* Check that the string is not interned */ if (!_IsAligned(self) || swap || PyString_CHECK_INTERNED(rawdata)) { @@ -1580,58 +1594,60 @@ array_setstate(PyArrayObject *self, PyObject *args) if (!_IsAligned(self) || swap) { #endif intp num = PyArray_NBYTES(self); - self->data = PyDataMem_NEW(num); - if (self->data == NULL) { - self->nd = 0; - PyDimMem_FREE(self->dimensions); + fa->data = PyDataMem_NEW(num); + if (PyArray_DATA(self) == NULL) { + fa->nd = 0; + PyDimMem_FREE(PyArray_DIMS(self)); Py_DECREF(rawdata); return PyErr_NoMemory(); } if (swap) { /* byte-swap on pickle-read */ - intp numels = num / self->descr->elsize; - self->descr->f->copyswapn(self->data, self->descr->elsize, - datastr, self->descr->elsize, - numels, 1, self); + intp numels = num / PyArray_DESCR(self)->elsize; + PyArray_DESCR(self)->f->copyswapn(PyArray_DATA(self), + PyArray_DESCR(self)->elsize, + datastr, PyArray_DESCR(self)->elsize, + numels, 1, self); if (!PyArray_ISEXTENDED(self)) { - self->descr = PyArray_DescrFromType(self->descr->type_num); + fa->descr = PyArray_DescrFromType( + PyArray_DESCR(self)->type_num); } else { - self->descr = PyArray_DescrNew(typecode); - if (self->descr->byteorder == PyArray_BIG) { - self->descr->byteorder = PyArray_LITTLE; + fa->descr = PyArray_DescrNew(typecode); + if (PyArray_DESCR(self)->byteorder == PyArray_BIG) { + PyArray_DESCR(self)->byteorder = PyArray_LITTLE; } - else if (self->descr->byteorder == PyArray_LITTLE) { - self->descr->byteorder = PyArray_BIG; + else if (PyArray_DESCR(self)->byteorder == PyArray_LITTLE) { + PyArray_DESCR(self)->byteorder = PyArray_BIG; } } Py_DECREF(typecode); } else { - memcpy(self->data, datastr, num); + memcpy(PyArray_DATA(self), datastr, num); } - self->flags |= NPY_ARRAY_OWNDATA; - self->base = NULL; + PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA); + fa->base = NULL; Py_DECREF(rawdata); } else { - self->base = rawdata; + fa->base = rawdata; } } else { - self->data = PyDataMem_NEW(PyArray_NBYTES(self)); - if (self->data == NULL) { - self->nd = 0; - self->data = PyDataMem_NEW(self->descr->elsize); - if (self->dimensions) { - PyDimMem_FREE(self->dimensions); + fa->data = PyDataMem_NEW(PyArray_NBYTES(self)); + if (PyArray_DATA(self) == NULL) { + fa->nd = 0; + fa->data = PyDataMem_NEW(PyArray_DESCR(self)->elsize); + if (PyArray_DIMS(self)) { + PyDimMem_FREE(PyArray_DIMS(self)); } return PyErr_NoMemory(); } - if (PyDataType_FLAGCHK(self->descr, NPY_NEEDS_INIT)) { - memset(self->data, 0, PyArray_NBYTES(self)); + if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_NEEDS_INIT)) { + memset(PyArray_DATA(self), 0, PyArray_NBYTES(self)); } - self->flags |= NPY_ARRAY_OWNDATA; - self->base = NULL; + PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA); + fa->base = NULL; if (_setlist_pkl(self, rawdata) < 0) { return NULL; } @@ -1798,7 +1814,7 @@ array_mean(PyArrayObject *self, PyObject *args, PyObject *kwds) return NULL; } - num = _get_type_num_double(self->descr, dtype); + num = _get_type_num_double(PyArray_DESCR(self), dtype); Py_XDECREF(dtype); return PyArray_Mean(self, axis, num, out); } @@ -1967,7 +1983,7 @@ array_stddev(PyArrayObject *self, PyObject *args, PyObject *kwds) return NULL; } - num = _get_type_num_double(self->descr, dtype); + num = _get_type_num_double(PyArray_DESCR(self), dtype); Py_XDECREF(dtype); return __New_PyArray_Std(self, axis, num, out, 0, ddof); } @@ -1992,7 +2008,7 @@ array_variance(PyArrayObject *self, PyObject *args, PyObject *kwds) return NULL; } - num = _get_type_num_double(self->descr, dtype); + num = _get_type_num_double(PyArray_DESCR(self), dtype); Py_XDECREF(dtype); return __New_PyArray_Std(self, axis, num, out, 1, ddof); } @@ -2156,7 +2172,9 @@ array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds) PyObject *write = Py_None; PyObject *align = Py_None; PyObject *uic = Py_None; - int flagback = self->flags; + int flagback = PyArray_FLAGS(self); + + PyArrayObject_fieldaccess *fa = (PyArrayObject_fieldaccess *)self; if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOO", kwlist, &write, @@ -2166,10 +2184,10 @@ array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds) if (align != Py_None) { if (PyObject_Not(align)) { - self->flags &= ~NPY_ARRAY_ALIGNED; + PyArray_CLEARFLAGS(self, NPY_ARRAY_ALIGNED); } else if (_IsAligned(self)) { - self->flags |= NPY_ARRAY_ALIGNED; + PyArray_ENABLEFLAGS(self, NPY_ARRAY_ALIGNED); } else { PyErr_SetString(PyExc_ValueError, @@ -2181,34 +2199,36 @@ array_setflags(PyArrayObject *self, PyObject *args, PyObject *kwds) if (uic != Py_None) { if (PyObject_IsTrue(uic)) { - self->flags = flagback; + fa->flags = flagback; PyErr_SetString(PyExc_ValueError, "cannot set UPDATEIFCOPY " \ "flag to True"); return NULL; } else { - self->flags &= ~NPY_ARRAY_UPDATEIFCOPY; - Py_XDECREF(self->base); - self->base = NULL; + PyArray_CLEARFLAGS(self, NPY_ARRAY_UPDATEIFCOPY); + Py_XDECREF(fa->base); + fa->base = NULL; } } if (write != Py_None) { - if (PyObject_IsTrue(write)) + if (PyObject_IsTrue(write)) { if (_IsWriteable(self)) { - self->flags |= NPY_ARRAY_WRITEABLE; + PyArray_ENABLEFLAGS(self, NPY_ARRAY_WRITEABLE); } else { - self->flags = flagback; + fa->flags = flagback; PyErr_SetString(PyExc_ValueError, - "cannot set WRITEABLE " \ - "flag to True of this " \ - "array"); \ + "cannot set WRITEABLE " + "flag to True of this " + "array"); return NULL; } - else - self->flags &= ~NPY_ARRAY_WRITEABLE; + } + else { + PyArray_CLEARFLAGS(self, NPY_ARRAY_WRITEABLE); + } } Py_INCREF(Py_None); @@ -2226,7 +2246,7 @@ array_newbyteorder(PyArrayObject *self, PyObject *args) &endian)) { return NULL; } - new = PyArray_DescrNewByteorder(self->descr, endian); + new = PyArray_DescrNewByteorder(PyArray_DESCR(self), endian); if (!new) { return NULL; } diff --git a/numpy/core/src/multiarray/multiarray_tests.c.src b/numpy/core/src/multiarray/multiarray_tests.c.src index d6340025c..90c63ca44 100644 --- a/numpy/core/src/multiarray/multiarray_tests.c.src +++ b/numpy/core/src/multiarray/multiarray_tests.c.src @@ -1,3 +1,4 @@ +#define NPY_NO_DEPRECATED_API #include <Python.h> #include "numpy/ndarrayobject.h" @@ -28,15 +29,16 @@ static int copy_@type@(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *ni for (i = 0; i < itx->size; ++i) { PyArrayNeighborhoodIter_Reset(niterx); - for (j = 0; j < itx->ao->nd; ++j) { + for (j = 0; j < PyArray_NDIM(itx->ao); ++j) { odims[j] = bounds[2 * j + 1] - bounds[2 * j] + 1; } - aout = (PyArrayObject*)PyArray_SimpleNew(itx->ao->nd, odims, @typenum@); + aout = (PyArrayObject*)PyArray_SimpleNew( + PyArray_NDIM(itx->ao), odims, @typenum@); if (aout == NULL) { return -1; } - ptr = (@type@*)aout->data; + ptr = (@type@*)PyArray_DATA(aout); for (j = 0; j < niterx->size; ++j) { *ptr = *((@type@*)niterx->dataptr); @@ -60,7 +62,7 @@ static int copy_object(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *ni npy_intp i, j; npy_intp odims[NPY_MAXDIMS]; PyArrayObject *aout; - PyArray_CopySwapFunc *copyswap = itx->ao->descr->f->copyswap; + PyArray_CopySwapFunc *copyswap = PyArray_DESCR(itx->ao)->f->copyswap; npy_int itemsize = PyArray_ITEMSIZE(itx->ao); /* @@ -70,16 +72,16 @@ static int copy_object(PyArrayIterObject *itx, PyArrayNeighborhoodIterObject *ni for (i = 0; i < itx->size; ++i) { PyArrayNeighborhoodIter_Reset(niterx); - for (j = 0; j < itx->ao->nd; ++j) { + for (j = 0; j < PyArray_NDIM(itx->ao); ++j) { odims[j] = bounds[2 * j + 1] - bounds[2 * j] + 1; } - aout = (PyArrayObject*)PyArray_SimpleNew(itx->ao->nd, odims, NPY_OBJECT); + aout = (PyArrayObject*)PyArray_SimpleNew(PyArray_NDIM(itx->ao), odims, NPY_OBJECT); if (aout == NULL) { return -1; } for (j = 0; j < niterx->size; ++j) { - copyswap(aout->data + j * itemsize, niterx->dataptr, 0, NULL); + copyswap(PyArray_DATA(aout) + j * itemsize, niterx->dataptr, 0, NULL); PyArrayNeighborhoodIter_Next(niterx); } @@ -116,7 +118,7 @@ test_neighborhood_iterator(PyObject* NPY_UNUSED(self), PyObject* args) if (ax == NULL) { return NULL; } - if (PySequence_Size(b) != 2 * ax->nd) { + if (PySequence_Size(b) != 2 * PyArray_NDIM(ax)) { PyErr_SetString(PyExc_ValueError, "bounds sequence size not compatible with x input"); goto clean_ax; @@ -133,7 +135,7 @@ test_neighborhood_iterator(PyObject* NPY_UNUSED(self), PyObject* args) } /* Compute boundaries for the neighborhood iterator */ - for (i = 0; i < 2 * ax->nd; ++i) { + for (i = 0; i < 2 * PyArray_NDIM(ax); ++i) { PyObject* bound; bound = PySequence_GetItem(b, i); if (bounds == NULL) { @@ -220,15 +222,16 @@ copy_double_double(PyArrayNeighborhoodIterObject *itx, */ PyArrayNeighborhoodIter_Reset(itx); for (i = 0; i < itx->size; ++i) { - for (j = 0; j < itx->ao->nd; ++j) { + for (j = 0; j < PyArray_NDIM(itx->ao); ++j) { odims[j] = bounds[2 * j + 1] - bounds[2 * j] + 1; } - aout = (PyArrayObject*)PyArray_SimpleNew(itx->ao->nd, odims, NPY_DOUBLE); + aout = (PyArrayObject*)PyArray_SimpleNew( + PyArray_NDIM(itx->ao), odims, NPY_DOUBLE); if (aout == NULL) { return -1; } - ptr = (double*)aout->data; + ptr = (double*)PyArray_DATA(aout); PyArrayNeighborhoodIter_Reset(niterx); for (j = 0; j < niterx->size; ++j) { @@ -267,12 +270,12 @@ test_neighborhood_iterator_oob(PyObject* NPY_UNUSED(self), PyObject* args) if (ax == NULL) { return NULL; } - if (PySequence_Size(b1) != 2 * ax->nd) { + if (PySequence_Size(b1) != 2 * PyArray_NDIM(ax)) { PyErr_SetString(PyExc_ValueError, "bounds sequence 1 size not compatible with x input"); goto clean_ax; } - if (PySequence_Size(b2) != 2 * ax->nd) { + if (PySequence_Size(b2) != 2 * PyArray_NDIM(ax)) { PyErr_SetString(PyExc_ValueError, "bounds sequence 2 size not compatible with x input"); goto clean_ax; @@ -289,7 +292,7 @@ test_neighborhood_iterator_oob(PyObject* NPY_UNUSED(self), PyObject* args) } /* Compute boundaries for the neighborhood iterator */ - for (i = 0; i < 2 * ax->nd; ++i) { + for (i = 0; i < 2 * PyArray_NDIM(ax); ++i) { PyObject* bound; bound = PySequence_GetItem(b1, i); if (bounds == NULL) { @@ -312,7 +315,7 @@ test_neighborhood_iterator_oob(PyObject* NPY_UNUSED(self), PyObject* args) goto clean_out; } - for (i = 0; i < 2 * ax->nd; ++i) { + for (i = 0; i < 2 * PyArray_NDIM(ax); ++i) { PyObject* bound; bound = PySequence_GetItem(b2, i); if (bounds == NULL) { diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 452ddec20..2c1061656 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -137,9 +137,9 @@ PyArray_OverflowMultiplyList(npy_intp *l1, int n) NPY_NO_EXPORT void * PyArray_GetPtr(PyArrayObject *obj, npy_intp* ind) { - int n = obj->nd; - npy_intp *strides = obj->strides; - char *dptr = obj->data; + int n = PyArray_NDIM(obj); + npy_intp *strides = PyArray_STRIDES(obj); + char *dptr = PyArray_DATA(obj); while (n--) { dptr += (*strides++) * (*ind++); @@ -198,22 +198,22 @@ PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd, } switch(nd) { case 1: - *((char **)ptr) = ap->data; + *((char **)ptr) = PyArray_DATA(ap); break; case 2: - n = ap->dimensions[0]; + n = PyArray_DIMS(ap)[0]; ptr2 = (char **)_pya_malloc(n * sizeof(char *)); if (!ptr2) { goto fail; } for (i = 0; i < n; i++) { - ptr2[i] = ap->data + i*ap->strides[0]; + ptr2[i] = PyArray_DATA(ap) + i*PyArray_STRIDES(ap)[0]; } *((char ***)ptr) = ptr2; break; case 3: - n = ap->dimensions[0]; - m = ap->dimensions[1]; + n = PyArray_DIMS(ap)[0]; + m = PyArray_DIMS(ap)[1]; ptr3 = (char ***)_pya_malloc(n*(m+1) * sizeof(char *)); if (!ptr3) { goto fail; @@ -221,12 +221,12 @@ PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd, for (i = 0; i < n; i++) { ptr3[i] = ptr3[n + (m-1)*i]; for (j = 0; j < m; j++) { - ptr3[i][j] = ap->data + i*ap->strides[0] + j*ap->strides[1]; + ptr3[i][j] = PyArray_DATA(ap) + i*PyArray_STRIDES(ap)[0] + j*PyArray_STRIDES(ap)[1]; } } *((char ****)ptr) = ptr3; } - memcpy(dims, ap->dimensions, nd*sizeof(npy_intp)); + memcpy(dims, PyArray_DIMS(ap), nd*sizeof(npy_intp)); *op = (PyObject *)ap; return 0; @@ -290,10 +290,10 @@ PyArray_Free(PyObject *op, void *ptr) { PyArrayObject *ap = (PyArrayObject *)op; - if ((ap->nd < 1) || (ap->nd > 3)) { + if ((PyArray_NDIM(ap) < 1) || (PyArray_NDIM(ap) > 3)) { return -1; } - if (ap->nd >= 2) { + if (PyArray_NDIM(ap) >= 2) { _pya_free(ptr); } Py_DECREF(ap); @@ -407,17 +407,17 @@ PyArray_Concatenate(PyObject *op, int axis) goto fail; } if (i == 0) { - nd = mps[i]->nd; + nd = PyArray_NDIM(mps[i]); } else { - if (nd != mps[i]->nd) { + if (nd != PyArray_NDIM(mps[i])) { PyErr_SetString(PyExc_ValueError, "arrays must have same "\ "number of dimensions"); goto fail; } - if (!PyArray_CompareLists(mps[0]->dimensions+1, - mps[i]->dimensions+1, + if (!PyArray_CompareLists(PyArray_DIMS(mps[0])+1, + PyArray_DIMS(mps[i])+1, nd-1)) { PyErr_SetString(PyExc_ValueError, "array dimensions must "\ @@ -430,25 +430,25 @@ PyArray_Concatenate(PyObject *op, int axis) "0-d arrays can't be concatenated"); goto fail; } - new_dim += mps[i]->dimensions[0]; + new_dim += PyArray_DIMS(mps[i])[0]; } - tmp = mps[0]->dimensions[0]; - mps[0]->dimensions[0] = new_dim; - Py_INCREF(mps[0]->descr); + tmp = PyArray_DIMS(mps[0])[0]; + PyArray_DIMS(mps[0])[0] = new_dim; + Py_INCREF(PyArray_DESCR(mps[0])); ret = (PyArrayObject *)PyArray_NewFromDescr(subtype, - mps[0]->descr, nd, - mps[0]->dimensions, + PyArray_DESCR(mps[0]), nd, + PyArray_DIMS(mps[0]), NULL, NULL, 0, (PyObject *)ret); - mps[0]->dimensions[0] = tmp; + PyArray_DIMS(mps[0])[0] = tmp; if (ret == NULL) { goto fail; } - data = ret->data; + data = PyArray_DATA(ret); for (i = 0; i < n; i++) { numbytes = PyArray_NBYTES(mps[i]); - memcpy(data, mps[i]->data, numbytes); + memcpy(data, PyArray_DATA(mps[i]), numbytes); data += numbytes; } @@ -476,9 +476,9 @@ _signbit_set(PyArrayObject *arr) char byteorder; int elsize; - elsize = arr->descr->elsize; - byteorder = arr->descr->byteorder; - ptr = arr->data; + elsize = PyArray_DESCR(arr)->elsize; + byteorder = PyArray_DESCR(arr)->byteorder; + ptr = PyArray_DATA(arr); if (elsize > 1 && (byteorder == PyArray_LITTLE || (byteorder == PyArray_NATIVE && @@ -673,8 +673,8 @@ PyArray_InnerProduct(PyObject *op1, PyObject *op2) if (ap2 == NULL) { goto fail; } - if (ap1->nd == 0 || ap2->nd == 0) { - ret = (ap1->nd == 0 ? ap1 : ap2); + if (PyArray_NDIM(ap1) == 0 || PyArray_NDIM(ap2) == 0) { + ret = (PyArray_NDIM(ap1) == 0 ? ap1 : ap2); ret = (PyArrayObject *)Py_TYPE(ret)->tp_as_number->nb_multiply( (PyObject *)ap1, (PyObject *)ap2); Py_DECREF(ap1); @@ -682,19 +682,19 @@ PyArray_InnerProduct(PyObject *op1, PyObject *op2) return (PyObject *)ret; } - l = ap1->dimensions[ap1->nd - 1]; - if (ap2->dimensions[ap2->nd - 1] != l) { + l = PyArray_DIMS(ap1)[PyArray_NDIM(ap1) - 1]; + if (PyArray_DIMS(ap2)[PyArray_NDIM(ap2) - 1] != l) { PyErr_SetString(PyExc_ValueError, "matrices are not aligned"); goto fail; } - nd = ap1->nd + ap2->nd - 2; + nd = PyArray_NDIM(ap1) + PyArray_NDIM(ap2) - 2; j = 0; - for (i = 0; i < ap1->nd - 1; i++) { - dimensions[j++] = ap1->dimensions[i]; + for (i = 0; i < PyArray_NDIM(ap1) - 1; i++) { + dimensions[j++] = PyArray_DIMS(ap1)[i]; } - for (i = 0; i < ap2->nd - 1; i++) { - dimensions[j++] = ap2->dimensions[i]; + for (i = 0; i < PyArray_NDIM(ap2) - 1; i++) { + dimensions[j++] = PyArray_DIMS(ap2)[i]; } /* @@ -705,20 +705,20 @@ PyArray_InnerProduct(PyObject *op1, PyObject *op2) if (ret == NULL) { goto fail; } - dot = (ret->descr->f->dotfunc); + dot = (PyArray_DESCR(ret)->f->dotfunc); if (dot == NULL) { PyErr_SetString(PyExc_ValueError, "dot not available for this type"); goto fail; } - is1 = ap1->strides[ap1->nd - 1]; - is2 = ap2->strides[ap2->nd - 1]; - op = ret->data; os = ret->descr->elsize; - axis = ap1->nd - 1; + is1 = PyArray_STRIDES(ap1)[PyArray_NDIM(ap1) - 1]; + is2 = PyArray_STRIDES(ap2)[PyArray_NDIM(ap2) - 1]; + op = PyArray_DATA(ret); os = PyArray_DESCR(ret)->elsize; + axis = PyArray_NDIM(ap1) - 1; it1 = (PyArrayIterObject *) PyArray_IterAllButAxis((PyObject *)ap1, &axis); - axis = ap2->nd - 1; + axis = PyArray_NDIM(ap2) - 1; it2 = (PyArrayIterObject *) PyArray_IterAllButAxis((PyObject *)ap2, &axis); - NPY_BEGIN_THREADS_DESCR(ap2->descr); + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap2)); while (1) { while (it2->index < it2->size) { dot(it1->dataptr, is1, it2->dataptr, is2, op, l, ret); @@ -731,7 +731,7 @@ PyArray_InnerProduct(PyObject *op1, PyObject *op2) } PyArray_ITER_RESET(it2); } - NPY_END_THREADS_DESCR(ap2->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(ap2)); Py_DECREF(it1); Py_DECREF(it2); if (PyErr_Occurred()) { @@ -782,39 +782,39 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out) if (ap2 == NULL) { goto fail; } - if (ap1->nd == 0 || ap2->nd == 0) { - ret = (ap1->nd == 0 ? ap1 : ap2); + if (PyArray_NDIM(ap1) == 0 || PyArray_NDIM(ap2) == 0) { + ret = (PyArray_NDIM(ap1) == 0 ? ap1 : ap2); ret = (PyArrayObject *)Py_TYPE(ret)->tp_as_number->nb_multiply( (PyObject *)ap1, (PyObject *)ap2); Py_DECREF(ap1); Py_DECREF(ap2); return (PyObject *)ret; } - l = ap1->dimensions[ap1->nd - 1]; - if (ap2->nd > 1) { - matchDim = ap2->nd - 2; + l = PyArray_DIMS(ap1)[PyArray_NDIM(ap1) - 1]; + if (PyArray_NDIM(ap2) > 1) { + matchDim = PyArray_NDIM(ap2) - 2; } else { matchDim = 0; } - if (ap2->dimensions[matchDim] != l) { + if (PyArray_DIMS(ap2)[matchDim] != l) { PyErr_SetString(PyExc_ValueError, "objects are not aligned"); goto fail; } - nd = ap1->nd + ap2->nd - 2; + nd = PyArray_NDIM(ap1) + PyArray_NDIM(ap2) - 2; if (nd > NPY_MAXDIMS) { PyErr_SetString(PyExc_ValueError, "dot: too many dimensions in result"); goto fail; } j = 0; - for (i = 0; i < ap1->nd - 1; i++) { - dimensions[j++] = ap1->dimensions[i]; + for (i = 0; i < PyArray_NDIM(ap1) - 1; i++) { + dimensions[j++] = PyArray_DIMS(ap1)[i]; } - for (i = 0; i < ap2->nd - 2; i++) { - dimensions[j++] = ap2->dimensions[i]; + for (i = 0; i < PyArray_NDIM(ap2) - 2; i++) { + dimensions[j++] = PyArray_DIMS(ap2)[i]; } - if(ap2->nd > 1) { - dimensions[j++] = ap2->dimensions[ap2->nd-1]; + if(PyArray_NDIM(ap2) > 1) { + dimensions[j++] = PyArray_DIMS(ap2)[PyArray_NDIM(ap2)-1]; } /* fprintf(stderr, "nd=%d dimensions=", nd); @@ -823,7 +823,7 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out) fprintf(stderr, "\n"); */ - is1 = ap1->strides[ap1->nd-1]; is2 = ap2->strides[matchDim]; + is1 = PyArray_STRIDES(ap1)[PyArray_NDIM(ap1)-1]; is2 = PyArray_STRIDES(ap2)[matchDim]; /* Choose which subtype to return */ ret = new_array_for_sum(ap1, ap2, out, nd, dimensions, typenum); if (ret == NULL) { @@ -838,20 +838,20 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out) memset(PyArray_DATA(ret), 0, PyArray_ITEMSIZE(ret)); } - dot = ret->descr->f->dotfunc; + dot = PyArray_DESCR(ret)->f->dotfunc; if (dot == NULL) { PyErr_SetString(PyExc_ValueError, "dot not available for this type"); goto fail; } - op = ret->data; os = ret->descr->elsize; - axis = ap1->nd-1; + op = PyArray_DATA(ret); os = PyArray_DESCR(ret)->elsize; + axis = PyArray_NDIM(ap1)-1; it1 = (PyArrayIterObject *) PyArray_IterAllButAxis((PyObject *)ap1, &axis); it2 = (PyArrayIterObject *) PyArray_IterAllButAxis((PyObject *)ap2, &matchDim); - NPY_BEGIN_THREADS_DESCR(ap2->descr); + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ap2)); while (1) { while (it2->index < it2->size) { dot(it1->dataptr, is1, it2->dataptr, is2, op, l, ret); @@ -864,7 +864,7 @@ PyArray_MatrixProduct2(PyObject *op1, PyObject *op2, PyArrayObject* out) } PyArray_ITER_RESET(it2); } - NPY_END_THREADS_DESCR(ap2->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(ap2)); Py_DECREF(it1); Py_DECREF(it2); if (PyErr_Occurred()) { @@ -958,8 +958,8 @@ _pyarray_correlate(PyArrayObject *ap1, PyArrayObject *ap2, int typenum, NPY_BEGIN_THREADS_DEF; - n1 = ap1->dimensions[0]; - n2 = ap2->dimensions[0]; + n1 = PyArray_DIMS(ap1)[0]; + n2 = PyArray_DIMS(ap2)[0]; if (n1 < n2) { ret = ap1; ap1 = ap2; @@ -1002,20 +1002,20 @@ _pyarray_correlate(PyArrayObject *ap1, PyArrayObject *ap2, int typenum, if (ret == NULL) { return NULL; } - dot = ret->descr->f->dotfunc; + dot = PyArray_DESCR(ret)->f->dotfunc; if (dot == NULL) { PyErr_SetString(PyExc_ValueError, "function not available for this data type"); goto clean_ret; } - NPY_BEGIN_THREADS_DESCR(ret->descr); - is1 = ap1->strides[0]; - is2 = ap2->strides[0]; - op = ret->data; - os = ret->descr->elsize; - ip1 = ap1->data; - ip2 = ap2->data + n_left*is2; + NPY_BEGIN_THREADS_DESCR(PyArray_DESCR(ret)); + is1 = PyArray_STRIDES(ap1)[0]; + is2 = PyArray_STRIDES(ap2)[0]; + op = PyArray_DATA(ret); + os = PyArray_DESCR(ret)->elsize; + ip1 = PyArray_DATA(ap1); + ip2 = PyArray_DATA(ap2) + n_left*is2; n = n - n_left; for (i = 0; i < n_left; i++) { dot(ip1, is1, ip2, is2, op, n, ret); @@ -1035,7 +1035,7 @@ _pyarray_correlate(PyArrayObject *ap1, PyArrayObject *ap2, int typenum, op += os; } - NPY_END_THREADS_DESCR(ret->descr); + NPY_END_THREADS_DESCR(PyArray_DESCR(ret)); if (PyErr_Occurred()) { goto clean_ret; } @@ -1062,16 +1062,16 @@ _pyarray_revert(PyArrayObject *ret) npy_intp os; char *op; - length = ret->dimensions[0]; - copyswap = ret->descr->f->copyswap; + length = PyArray_DIMS(ret)[0]; + copyswap = PyArray_DESCR(ret)->f->copyswap; - tmp = PyArray_malloc(ret->descr->elsize); + tmp = PyArray_malloc(PyArray_DESCR(ret)->elsize); if (tmp == NULL) { return -1; } - os = ret->descr->elsize; - op = ret->data; + os = PyArray_DESCR(ret)->elsize; + op = PyArray_DATA(ret); sw1 = op; sw2 = op + (length - 1) * os; if (PyArray_ISFLEXIBLE(ret) || PyArray_ISOBJECT(ret)) { @@ -1513,24 +1513,33 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin) npy_intp newdims[MAX_DIMS]; npy_intp newstrides[MAX_DIMS]; int i, k, num; - PyObject *ret; + PyArrayObject *ret; + PyArray_Descr *dtype; num = ndmin - nd; for (i = 0; i < num; i++) { newdims[i] = 1; - newstrides[i] = arr->descr->elsize; + newstrides[i] = PyArray_DESCR(arr)->elsize; } for (i = num; i < ndmin; i++) { k = i - num; - newdims[i] = arr->dimensions[k]; - newstrides[i] = arr->strides[k]; + newdims[i] = PyArray_DIMS(arr)[k]; + newstrides[i] = PyArray_STRIDES(arr)[k]; + } + dtype = PyArray_DESCR(arr); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(arr), + dtype, ndmin, newdims, newstrides, + PyArray_DATA(arr), PyArray_FLAGS(arr), (PyObject *)arr); + if (ret == NULL) { + return NULL; } - Py_INCREF(arr->descr); - ret = PyArray_NewFromDescr(Py_TYPE(arr), arr->descr, ndmin, - newdims, newstrides, arr->data, arr->flags, (PyObject *)arr); /* steals a reference to arr --- so don't increment here */ - PyArray_BASE(ret) = (PyObject *)arr; - return ret; + if (PyArray_SetBaseObject(ret, (PyObject *)arr) < 0) { + Py_DECREF(ret); + return NULL; + } + return (PyObject *)ret; } @@ -1545,9 +1554,8 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin) static PyObject * _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) { - PyObject *op, *ret = NULL; - static char *kwd[]= {"object", "dtype", "copy", "order", "subok", - "ndmin", NULL}; + PyObject *op; + PyArrayObject *oparr = NULL, *ret = NULL; Bool subok = FALSE; Bool copy = TRUE; int ndmin = 0, nd; @@ -1556,6 +1564,9 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) NPY_ORDER order = NPY_ANYORDER; int flags = 0; + static char *kwd[]= {"object", "dtype", "copy", "order", "subok", + "ndmin", NULL}; + if (PyTuple_GET_SIZE(args) > 2) { PyErr_SetString(PyExc_ValueError, "only 2 non-keyword arguments accepted"); @@ -1579,33 +1590,34 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) /* fast exit if simple call */ if ((subok && PyArray_Check(op)) || (!subok && PyArray_CheckExact(op))) { + oparr = (PyArrayObject *)op; if (type == NULL) { - if (!copy && STRIDING_OK(op, order)) { + if (!copy && STRIDING_OK(oparr, order)) { Py_INCREF(op); - ret = op; + ret = oparr; goto finish; } else { - ret = PyArray_NewCopy((PyArrayObject*)op, order); + ret = (PyArrayObject *)PyArray_NewCopy(oparr, order); goto finish; } } /* One more chance */ - oldtype = PyArray_DESCR(op); + oldtype = PyArray_DESCR(oparr); if (PyArray_EquivTypes(oldtype, type)) { - if (!copy && STRIDING_OK(op, order)) { + if (!copy && STRIDING_OK(oparr, order)) { Py_INCREF(op); - ret = op; + ret = oparr; goto finish; } else { - ret = PyArray_NewCopy((PyArrayObject*)op, order); + ret = (PyArrayObject *)PyArray_NewCopy(oparr, order); if (oldtype == type) { goto finish; } Py_INCREF(oldtype); Py_DECREF(PyArray_DESCR(ret)); - PyArray_DESCR(ret) = oldtype; + ((PyArrayObject_fieldaccess *)ret)->descr = oldtype; goto finish; } } @@ -1618,8 +1630,9 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) flags |= NPY_ARRAY_C_CONTIGUOUS; } else if ((order == NPY_FORTRANORDER) - /* order == NPY_ANYORDER && */ - || (PyArray_Check(op) && PyArray_ISFORTRAN(op))) { + /* order == NPY_ANYORDER && */ + || (PyArray_Check(op) && + PyArray_ISFORTRAN((PyArrayObject *)op))) { flags |= NPY_ARRAY_F_CONTIGUOUS; } if (!subok) { @@ -1628,21 +1641,22 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) flags |= NPY_ARRAY_FORCECAST; Py_XINCREF(type); - ret = PyArray_CheckFromAny(op, type, 0, 0, flags, NULL); + ret = (PyArrayObject *)PyArray_CheckFromAny(op, type, + 0, 0, flags, NULL); finish: Py_XDECREF(type); if (!ret) { - return ret; + return (PyObject *)ret; } else if ((nd=PyArray_NDIM(ret)) >= ndmin) { - return ret; + return (PyObject *)ret; } /* * create a new array from the same data with ones in the shape * steals a reference to ret */ - return _prepend_ones((PyArrayObject *)ret, nd, ndmin); + return _prepend_ones(ret, nd, ndmin); clean_type: Py_XDECREF(type); diff --git a/numpy/core/src/multiarray/nditer_api.c b/numpy/core/src/multiarray/nditer_api.c index 4eda968db..4875c1e34 100644 --- a/numpy/core/src/multiarray/nditer_api.c +++ b/numpy/core/src/multiarray/nditer_api.c @@ -1049,7 +1049,10 @@ NpyIter_GetIterView(NpyIter *iter, npy_intp i) } /* Tell the view who owns the data */ Py_INCREF(obj); - view->base = (PyObject *)obj; + if (PyArray_SetBaseObject(view, (PyObject *)obj) < 0) { + Py_DECREF(view); + return NULL; + } /* Make sure all the flags are good */ PyArray_UpdateFlags(view, NPY_ARRAY_UPDATE_ALL); @@ -1864,7 +1867,7 @@ npyiter_copy_from_buffers(NpyIter *iter) (int)iop, (int)op_transfersize); if (op_itflags[iop] & NPY_OP_ITFLAG_WRITEMASKED) { - npy_uint8 *maskptr; + npy_mask *maskptr; /* * The mask pointer may be in the buffer or in @@ -1873,10 +1876,10 @@ npyiter_copy_from_buffers(NpyIter *iter) delta = (ptrs[maskop] - buffers[maskop]); if (0 <= delta && delta <= buffersize*dtypes[maskop]->elsize) { - maskptr = buffers[maskop]; + maskptr = (npy_mask *)buffers[maskop]; } else { - maskptr = ad_ptrs[maskop]; + maskptr = (npy_mask *)ad_ptrs[maskop]; } PyArray_TransferMaskedStridedToNDim(ndim_transfer, diff --git a/numpy/core/src/multiarray/nditer_constr.c b/numpy/core/src/multiarray/nditer_constr.c index b9f79f1d3..441f19de0 100644 --- a/numpy/core/src/multiarray/nditer_constr.c +++ b/numpy/core/src/multiarray/nditer_constr.c @@ -2702,7 +2702,10 @@ npyiter_new_temp_array(NpyIter *iter, PyTypeObject *subtype, Py_DECREF(ret); return NULL; } - newret->base = (PyObject *)ret; + if (PyArray_SetBaseObject(newret, (PyObject *)ret) < 0) { + Py_DECREF(newret); + return NULL; + } ret = newret; } @@ -2859,10 +2862,15 @@ npyiter_allocate_arrays(NpyIter *iter, } /* If the data will be written to, set UPDATEIFCOPY */ if (op_itflags[iop] & NPY_OP_ITFLAG_WRITE) { - PyArray_FLAGS(temp) |= NPY_ARRAY_UPDATEIFCOPY; - PyArray_FLAGS(op[iop]) &= ~NPY_ARRAY_WRITEABLE; + /* + * Don't use PyArray_SetBaseObject, because that compresses + * the chain of bases. + */ Py_INCREF(op[iop]); - temp->base = (PyObject *)op[iop]; + ((PyArrayObject_fieldaccess *)temp)->base = + (PyObject *)op[iop]; + PyArray_ENABLEFLAGS(temp, NPY_ARRAY_UPDATEIFCOPY); + PyArray_CLEARFLAGS(op[iop], NPY_ARRAY_WRITEABLE); } Py_DECREF(op[iop]); diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c index 1e86487f5..5e4f0ea74 100644 --- a/numpy/core/src/multiarray/nditer_pywrap.c +++ b/numpy/core/src/multiarray/nditer_pywrap.c @@ -2014,7 +2014,7 @@ npyiter_seq_length(NewNpyArrayIterObject *self) NPY_NO_EXPORT PyObject * npyiter_seq_item(NewNpyArrayIterObject *self, Py_ssize_t i) { - PyObject *ret; + PyArrayObject *ret; npy_intp ret_ndim; npy_intp nop, innerloopsize, innerstride; @@ -2072,16 +2072,19 @@ npyiter_seq_item(NewNpyArrayIterObject *self, Py_ssize_t i) } Py_INCREF(dtype); - ret = (PyObject *)PyArray_NewFromDescr(&PyArray_Type, dtype, + ret = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, dtype, ret_ndim, &innerloopsize, &innerstride, dataptr, self->writeflags[i] ? NPY_ARRAY_WRITEABLE : 0, NULL); Py_INCREF(self); - ((PyArrayObject *)ret)->base = (PyObject *)self; + if (PyArray_SetBaseObject(ret, (PyObject *)self) < 0) { + Py_DECREF(ret); + return NULL; + } - PyArray_UpdateFlags((PyArrayObject *)ret, NPY_ARRAY_UPDATE_ALL); + PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); - return ret; + return (PyObject *)ret; } NPY_NO_EXPORT PyObject * diff --git a/numpy/core/src/multiarray/number.c b/numpy/core/src/multiarray/number.c index 8faa31c16..8bb9dfc3d 100644 --- a/numpy/core/src/multiarray/number.c +++ b/numpy/core/src/multiarray/number.c @@ -27,13 +27,13 @@ NPY_NO_EXPORT NumericOps n_ops; /* NB: static objects initialized to zero */ /* FIXME - macro contains a return */ #define SET(op) temp = PyDict_GetItemString(dict, #op); \ - if (temp != NULL) { \ - if (!(PyCallable_Check(temp))) { \ - return -1; \ - } \ - Py_INCREF(temp); \ - Py_XDECREF(n_ops.op); \ - n_ops.op = temp; \ + if (temp != NULL) { \ + if (!(PyCallable_Check(temp))) { \ + return -1; \ + } \ + Py_INCREF(temp); \ + Py_XDECREF(n_ops.op); \ + n_ops.op = temp; \ } @@ -289,8 +289,8 @@ array_power_is_scalar(PyObject *o2, double* exp) return 1; } if ((PyArray_IsZeroDim(o2) && - ((PyArray_ISINTEGER(o2) || - (optimize_fpexps && PyArray_ISFLOAT(o2))))) || + ((PyArray_ISINTEGER((PyArrayObject *)o2) || + (optimize_fpexps && PyArray_ISFLOAT((PyArrayObject *)o2))))) || PyArray_IsScalar(o2, Integer) || (optimize_fpexps && PyArray_IsScalar(o2, Floating))) { temp = Py_TYPE(o2)->tp_as_number->nb_float(o2); @@ -547,7 +547,7 @@ _array_nonzero(PyArrayObject *mp) n = PyArray_SIZE(mp); if (n == 1) { - return mp->descr->f->nonzero(mp->data, mp); + return PyArray_DESCR(mp)->f->nonzero(PyArray_DATA(mp), mp); } else if (n == 0) { return 0; @@ -593,7 +593,7 @@ array_int(PyArrayObject *v) " converted to Python scalars"); return NULL; } - pv = v->descr->f->getitem(v->data, v); + pv = PyArray_DESCR(v)->f->getitem(PyArray_DATA(v), v); if (pv == NULL) { return NULL; } @@ -613,7 +613,8 @@ array_int(PyArrayObject *v) * If we still got an array which can hold references, stop * because it could point back at 'v'. */ - if (PyArray_Check(pv) && PyDataType_REFCHK(PyArray_DESCR(pv))) { + if (PyArray_Check(pv) && + PyDataType_REFCHK(PyArray_DESCR((PyArrayObject *)pv))) { PyErr_SetString(PyExc_TypeError, "object array may be self-referencing"); return NULL; @@ -633,7 +634,7 @@ array_float(PyArrayObject *v) "be converted to Python scalars"); return NULL; } - pv = v->descr->f->getitem(v->data, v); + pv = PyArray_DESCR(v)->f->getitem(PyArray_DATA(v), v); if (pv == NULL) { return NULL; } @@ -653,7 +654,8 @@ array_float(PyArrayObject *v) * If we still got an array which can hold references, stop * because it could point back at 'v'. */ - if (PyArray_Check(pv) && PyDataType_REFCHK(PyArray_DESCR(pv))) { + if (PyArray_Check(pv) && + PyDataType_REFCHK(PyArray_DESCR((PyArrayObject *)pv))) { PyErr_SetString(PyExc_TypeError, "object array may be self-referencing"); return NULL; @@ -674,7 +676,7 @@ array_long(PyArrayObject *v) "be converted to Python scalars"); return NULL; } - pv = v->descr->f->getitem(v->data, v); + pv = PyArray_DESCR(v)->f->getitem(PyArray_DATA(v), v); if (Py_TYPE(pv)->tp_as_number == 0) { PyErr_SetString(PyExc_TypeError, "cannot convert to an int; "\ "scalar object is not a number"); @@ -689,7 +691,8 @@ array_long(PyArrayObject *v) * If we still got an array which can hold references, stop * because it could point back at 'v'. */ - if (PyArray_Check(pv) && PyDataType_REFCHK(PyArray_DESCR(pv))) { + if (PyArray_Check(pv) && + PyDataType_REFCHK(PyArray_DESCR((PyArrayObject *)pv))) { PyErr_SetString(PyExc_TypeError, "object array may be self-referencing"); return NULL; @@ -708,7 +711,7 @@ array_oct(PyArrayObject *v) "be converted to Python scalars"); return NULL; } - pv = v->descr->f->getitem(v->data, v); + pv = PyArray_DESCR(v)->f->getitem(PyArray_DATA(v), v); if (Py_TYPE(pv)->tp_as_number == 0) { PyErr_SetString(PyExc_TypeError, "cannot convert to an int; "\ "scalar object is not a number"); @@ -723,7 +726,8 @@ array_oct(PyArrayObject *v) * If we still got an array which can hold references, stop * because it could point back at 'v'. */ - if (PyArray_Check(pv) && PyDataType_REFCHK(PyArray_DESCR(pv))) { + if (PyArray_Check(pv) && + PyDataType_REFCHK(PyArray_DESCR((PyArrayObject *)pv))) { PyErr_SetString(PyExc_TypeError, "object array may be self-referencing"); return NULL; @@ -742,7 +746,7 @@ array_hex(PyArrayObject *v) "be converted to Python scalars"); return NULL; } - pv = v->descr->f->getitem(v->data, v); + pv = PyArray_DESCR(v)->f->getitem(PyArray_DATA(v), v); if (Py_TYPE(pv)->tp_as_number == 0) { PyErr_SetString(PyExc_TypeError, "cannot convert to an int; "\ "scalar object is not a number"); @@ -757,7 +761,8 @@ array_hex(PyArrayObject *v) * If we still got an array which can hold references, stop * because it could point back at 'v'. */ - if (PyArray_Check(pv) && PyDataType_REFCHK(PyArray_DESCR(pv))) { + if (PyArray_Check(pv) && + PyDataType_REFCHK(PyArray_DESCR((PyArrayObject *)pv))) { PyErr_SetString(PyExc_TypeError, "object array may be self-referencing"); return NULL; @@ -784,7 +789,7 @@ array_index(PyArrayObject *v) "one element can be converted to an index"); return NULL; } - return v->descr->f->getitem(v->data, v); + return PyArray_DESCR(v)->f->getitem(PyArray_DATA(v), v); } #endif diff --git a/numpy/core/src/multiarray/refcount.c b/numpy/core/src/multiarray/refcount.c index cd1474948..409d4d30f 100644 --- a/numpy/core/src/multiarray/refcount.c +++ b/numpy/core/src/multiarray/refcount.c @@ -104,16 +104,16 @@ PyArray_INCREF(PyArrayObject *mp) PyObject *temp; PyArrayIterObject *it; - if (!PyDataType_REFCHK(mp->descr)) { + if (!PyDataType_REFCHK(PyArray_DESCR(mp))) { return 0; } - if (mp->descr->type_num != PyArray_OBJECT) { + if (PyArray_DESCR(mp)->type_num != PyArray_OBJECT) { it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)mp); if (it == NULL) { return -1; } while(it->index < it->size) { - PyArray_Item_INCREF(it->dataptr, mp->descr); + PyArray_Item_INCREF(it->dataptr, PyArray_DESCR(mp)); PyArray_ITER_NEXT(it); } Py_DECREF(it); @@ -121,7 +121,7 @@ PyArray_INCREF(PyArrayObject *mp) } if (PyArray_ISONESEGMENT(mp)) { - data = (PyObject **)mp->data; + data = (PyObject **)PyArray_DATA(mp); n = PyArray_SIZE(mp); if (PyArray_ISALIGNED(mp)) { for (i = 0; i < n; i++, data++) { @@ -162,16 +162,16 @@ PyArray_XDECREF(PyArrayObject *mp) PyObject *temp; PyArrayIterObject *it; - if (!PyDataType_REFCHK(mp->descr)) { + if (!PyDataType_REFCHK(PyArray_DESCR(mp))) { return 0; } - if (mp->descr->type_num != PyArray_OBJECT) { + if (PyArray_DESCR(mp)->type_num != PyArray_OBJECT) { it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)mp); if (it == NULL) { return -1; } while(it->index < it->size) { - PyArray_Item_XDECREF(it->dataptr, mp->descr); + PyArray_Item_XDECREF(it->dataptr, PyArray_DESCR(mp)); PyArray_ITER_NEXT(it); } Py_DECREF(it); @@ -179,7 +179,7 @@ PyArray_XDECREF(PyArrayObject *mp) } if (PyArray_ISONESEGMENT(mp)) { - data = (PyObject **)mp->data; + data = (PyObject **)PyArray_DATA(mp); n = PyArray_SIZE(mp); if (PyArray_ISALIGNED(mp)) { for (i = 0; i < n; i++, data++) Py_XDECREF(*data); @@ -214,9 +214,9 @@ PyArray_FillObjectArray(PyArrayObject *arr, PyObject *obj) { intp i,n; n = PyArray_SIZE(arr); - if (arr->descr->type_num == PyArray_OBJECT) { + if (PyArray_DESCR(arr)->type_num == PyArray_OBJECT) { PyObject **optr; - optr = (PyObject **)(arr->data); + optr = (PyObject **)(PyArray_DATA(arr)); n = PyArray_SIZE(arr); if (obj == NULL) { for (i = 0; i < n; i++) { @@ -232,10 +232,10 @@ PyArray_FillObjectArray(PyArrayObject *arr, PyObject *obj) } else { char *optr; - optr = arr->data; + optr = PyArray_DATA(arr); for (i = 0; i < n; i++) { - _fillobject(optr, obj, arr->descr); - optr += arr->descr->elsize; + _fillobject(optr, obj, PyArray_DESCR(arr)); + optr += PyArray_DESCR(arr)->elsize; } } } diff --git a/numpy/core/src/multiarray/scalarapi.c b/numpy/core/src/multiarray/scalarapi.c index 21d8c762b..fa783dabf 100644 --- a/numpy/core/src/multiarray/scalarapi.c +++ b/numpy/core/src/multiarray/scalarapi.c @@ -227,7 +227,7 @@ PyArray_CastScalarToCtype(PyObject *scalar, void *ctypeptr, Py_DECREF(ain); return -1; } - castfunc(ain->data, aout->data, 1, ain, aout); + castfunc(PyArray_DATA(ain), PyArray_DATA(aout), 1, ain, aout); Py_DECREF(ain); Py_DECREF(aout); } @@ -270,7 +270,7 @@ NPY_NO_EXPORT PyObject * PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode) { PyArray_Descr *typecode; - PyObject *r; + PyArrayObject *r; char *memptr; PyObject *ret; @@ -279,18 +279,24 @@ PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode) if ((typecode->type_num == PyArray_VOID) && !(((PyVoidScalarObject *)scalar)->flags & NPY_ARRAY_OWNDATA) && outcode == NULL) { - r = PyArray_NewFromDescr(&PyArray_Type, + r = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, typecode, 0, NULL, NULL, ((PyVoidScalarObject *)scalar)->obval, ((PyVoidScalarObject *)scalar)->flags, NULL); - PyArray_BASE(r) = (PyObject *)scalar; + if (r == NULL) { + return NULL; + } Py_INCREF(scalar); - return r; + if (PyArray_SetBaseObject(r, (PyObject *)scalar) < 0) { + Py_DECREF(r); + return NULL; + } + return (PyObject *)r; } - r = PyArray_NewFromDescr(&PyArray_Type, + r = (PyArrayObject *)PyArray_NewFromDescr(&PyArray_Type, typecode, 0, NULL, NULL, NULL, 0, NULL); @@ -327,12 +333,12 @@ PyArray_FromScalar(PyObject *scalar, PyArray_Descr *outcode) finish: if (outcode == NULL) { - return r; + return (PyObject *)r; } if (outcode->type_num == typecode->type_num) { if (!PyTypeNum_ISEXTENDED(typecode->type_num) || (outcode->elsize == typecode->elsize)) { - return r; + return (PyObject *)r; } } @@ -353,7 +359,8 @@ PyArray_ScalarFromObject(PyObject *object) { PyObject *ret=NULL; if (PyArray_IsZeroDim(object)) { - return PyArray_ToScalar(PyArray_DATA(object), object); + return PyArray_ToScalar(PyArray_DATA((PyArrayObject *)object), + (PyArrayObject *)object); } /* * Booleans in Python are implemented as a subclass of integers, @@ -762,7 +769,7 @@ PyArray_Scalar(void *data, PyArray_Descr *descr, PyObject *base) if (base) { Py_INCREF(base); vobj->base = base; - vobj->flags = PyArray_FLAGS(base); + vobj->flags = PyArray_FLAGS((PyArrayObject *)base); vobj->flags &= ~NPY_ARRAY_OWNDATA; vobj->obval = data; return obj; @@ -805,9 +812,9 @@ PyArray_Return(PyArrayObject *mp) if (!PyArray_Check(mp)) { return (PyObject *)mp; } - if (mp->nd == 0) { + if (PyArray_NDIM(mp) == 0) { PyObject *ret; - ret = PyArray_ToScalar(mp->data, mp); + ret = PyArray_ToScalar(PyArray_DATA(mp), mp); Py_DECREF(mp); return ret; } diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index e2674ac50..fc3a74fd2 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -681,7 +681,8 @@ timedeltatype_repr(PyObject *self) ret = PyUString_FromString("numpy.timedelta64('NaT'"); } else { - ret = PyUString_FromFormat("numpy.timedelta64(%lld", scal->obval); + ret = PyUString_FromFormat("numpy.timedelta64(%lld", + (long long)scal->obval); } /* The metadata unit */ if (scal->obmeta.base == NPY_FR_GENERIC) { @@ -1131,14 +1132,14 @@ gentype_struct_get(PyObject *self) inter = (PyArrayInterface *)PyArray_malloc(sizeof(PyArrayInterface)); inter->two = 2; inter->nd = 0; - inter->flags = arr->flags; + inter->flags = PyArray_FLAGS(arr); inter->flags &= ~(NPY_ARRAY_UPDATEIFCOPY | NPY_ARRAY_OWNDATA); inter->flags |= NPY_ARRAY_NOTSWAPPED; - inter->typekind = arr->descr->kind; - inter->itemsize = arr->descr->elsize; + inter->typekind = PyArray_DESCR(arr)->kind; + inter->itemsize = PyArray_DESCR(arr)->elsize; inter->strides = NULL; inter->shape = NULL; - inter->data = arr->data; + inter->data = PyArray_DATA(arr); inter->descr = NULL; ret = NpyCapsule_FromVoidPtrAndDesc(inter, arr, gentype_struct_free); @@ -1416,21 +1417,24 @@ static char doc_sc_wraparray[] = "sc.__array_wrap__(obj) return scalar from arra static PyObject * gentype_wraparray(PyObject *NPY_UNUSED(scalar), PyObject *args) { - PyObject *arr; + PyObject *obj; + PyArrayObject *arr; if (PyTuple_Size(args) < 1) { PyErr_SetString(PyExc_TypeError, "only accepts 1 argument."); return NULL; } - arr = PyTuple_GET_ITEM(args, 0); - if (!PyArray_Check(arr)) { + obj = PyTuple_GET_ITEM(args, 0); + if (!PyArray_Check(obj)) { PyErr_SetString(PyExc_TypeError, "can only be called with ndarray object"); return NULL; } + arr = (PyArrayObject *)obj; - return PyArray_Scalar(PyArray_DATA(arr), PyArray_DESCR(arr), arr); + return PyArray_Scalar(PyArray_DATA(arr), + PyArray_DESCR(arr), (PyObject *)arr); } /* @@ -1567,7 +1571,8 @@ voidtype_setfield(PyVoidScalarObject *self, PyObject *args, PyObject *kwds) { PyArray_Descr *typecode = NULL; int offset = 0; - PyObject *value, *src; + PyObject *value; + PyArrayObject *src; int mysize; char *dptr; static char *kwlist[] = {"value", "dtype", "offset", 0}; @@ -1607,7 +1612,8 @@ voidtype_setfield(PyVoidScalarObject *self, PyObject *args, PyObject *kwds) } else { /* Copy data from value to correct place in dptr */ - src = PyArray_FromAny(value, typecode, 0, 0, NPY_ARRAY_CARRAY, NULL); + src = (PyArrayObject *)PyArray_FromAny(value, typecode, + 0, 0, NPY_ARRAY_CARRAY, NULL); if (src == NULL) { return NULL; } @@ -2431,7 +2437,7 @@ static PyObject * { PyObject *obj = NULL; PyObject *robj; - PyObject *arr; + PyArrayObject *arr; PyArray_Descr *typecode = NULL; #if !(@default@ == 2) int itemsize; @@ -2477,12 +2483,13 @@ static PyObject * * It is expected at this point that robj is a PyArrayScalar * (even for Object Data Type) */ - arr = PyArray_FromAny(obj, typecode, 0, 0, NPY_ARRAY_FORCECAST, NULL); + arr = (PyArrayObject *)PyArray_FromAny(obj, typecode, + 0, 0, NPY_ARRAY_FORCECAST, NULL); if ((arr == NULL) || (PyArray_NDIM(arr) > 0)) { - return arr; + return (PyObject *)arr; } /* 0-d array */ - robj = PyArray_ToScalar(PyArray_DATA(arr), (PyArrayObject *)arr); + robj = PyArray_ToScalar(PyArray_DATA(arr), arr); Py_DECREF(arr); finish: @@ -2611,7 +2618,7 @@ static PyObject * bool_arrtype_new(PyTypeObject *NPY_UNUSED(type), PyObject *args, PyObject *NPY_UNUSED(kwds)) { PyObject *obj = NULL; - PyObject *arr; + PyArrayObject *arr; if (!PyArg_ParseTuple(args, "|O", &obj)) { return NULL; @@ -2625,9 +2632,10 @@ bool_arrtype_new(PyTypeObject *NPY_UNUSED(type), PyObject *args, PyObject *NPY_U if (obj == Py_True) { PyArrayScalar_RETURN_TRUE; } - arr = PyArray_FROM_OTF(obj, PyArray_BOOL, NPY_ARRAY_FORCECAST); + arr = (PyArrayObject *)PyArray_FROM_OTF(obj, + NPY_BOOL, NPY_ARRAY_FORCECAST); if (arr && 0 == PyArray_NDIM(arr)) { - Bool val = *((Bool *)PyArray_DATA(arr)); + npy_bool val = *((npy_bool *)PyArray_DATA(arr)); Py_DECREF(arr); PyArrayScalar_RETURN_BOOL_FROM_LONG(val); } @@ -2776,8 +2784,9 @@ void_arrtype_new(PyTypeObject *type, PyObject *args, PyObject *NPY_UNUSED(kwds)) */ if (PyLong_Check(obj) || PyInt_Check(obj) || PyArray_IsScalar(obj, Integer) || - (PyArray_Check(obj) && PyArray_NDIM(obj)==0 && - PyArray_ISINTEGER(obj))) { + (PyArray_Check(obj) && + PyArray_NDIM((PyArrayObject *)obj)==0 && + PyArray_ISINTEGER((PyArrayObject *)obj))) { #if defined(NPY_PY3K) new = Py_TYPE(obj)->tp_as_number->nb_int(obj); #else diff --git a/numpy/core/src/multiarray/sequence.c b/numpy/core/src/multiarray/sequence.c index b59c1d2b1..354dcfa2f 100644 --- a/numpy/core/src/multiarray/sequence.c +++ b/numpy/core/src/multiarray/sequence.c @@ -30,57 +30,59 @@ array_any_nonzero(PyArrayObject *mp); static PyObject * -array_slice(PyArrayObject *self, Py_ssize_t ilow, - Py_ssize_t ihigh) +array_slice(PyArrayObject *self, Py_ssize_t ilow, Py_ssize_t ihigh) { - PyArrayObject *r; - Py_ssize_t l; + PyArrayObject *ret; + PyArray_Descr *dtype; + Py_ssize_t dim0; char *data; + npy_intp shape[NPY_MAXDIMS]; - if (self->nd == 0) { + if (PyArray_NDIM(self) == 0) { PyErr_SetString(PyExc_ValueError, "cannot slice a 0-d array"); return NULL; } - l=self->dimensions[0]; + dim0 = PyArray_DIM(self, 0); if (ilow < 0) { ilow = 0; } - else if (ilow > l) { - ilow = l; + else if (ilow > dim0) { + ilow = dim0; } if (ihigh < ilow) { ihigh = ilow; } - else if (ihigh > l) { - ihigh = l; + else if (ihigh > dim0) { + ihigh = dim0; } - if (ihigh != ilow) { - data = index2ptr(self, ilow); - if (data == NULL) { - return NULL; - } - } - else { - data = self->data; + data = PyArray_DATA(self); + if (ilow < ihigh) { + data += ilow * PyArray_STRIDE(self, 0); } - self->dimensions[0] = ihigh-ilow; - Py_INCREF(self->descr); - r = (PyArrayObject *) \ - PyArray_NewFromDescr(Py_TYPE(self), self->descr, - self->nd, self->dimensions, - self->strides, data, - self->flags, (PyObject *)self); - self->dimensions[0] = l; - if (r == NULL) { + /* Same shape except dimension 0 */ + shape[0] = ihigh - ilow; + memcpy(shape+1, PyArray_DIMS(self) + 1, + (PyArray_NDIM(self)-1)*sizeof(npy_intp)); + + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), dtype, + PyArray_NDIM(self), shape, + PyArray_STRIDES(self), data, + PyArray_FLAGS(self), (PyObject *)self); + if (ret == NULL) { return NULL; } - r->base = (PyObject *)self; Py_INCREF(self); - PyArray_UpdateFlags(r, NPY_ARRAY_UPDATE_ALL); - return (PyObject *)r; + if (PyArray_SetBaseObject(ret, (PyObject *)self) < 0) { + Py_DECREF(ret); + return NULL; + } + PyArray_UpdateFlags(ret, NPY_ARRAY_UPDATE_ALL); + return (PyObject *)ret; } @@ -162,19 +164,19 @@ NPY_NO_EXPORT PySequenceMethods array_as_sequence = { /* Array evaluates as "TRUE" if any of the elements are non-zero*/ static int -array_any_nonzero(PyArrayObject *mp) +array_any_nonzero(PyArrayObject *arr) { intp index; PyArrayIterObject *it; Bool anyTRUE = FALSE; - it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)mp); + it = (PyArrayIterObject *)PyArray_IterNew((PyObject *)arr); if (it == NULL) { return anyTRUE; } index = it->size; while(index--) { - if (mp->descr->f->nonzero(it->dataptr, mp)) { + if (PyArray_DESCR(arr)->f->nonzero(it->dataptr, arr)) { anyTRUE = TRUE; break; } diff --git a/numpy/core/src/multiarray/shape.c b/numpy/core/src/multiarray/shape.c index ff022faf0..3754e6a1e 100644 --- a/numpy/core/src/multiarray/shape.c +++ b/numpy/core/src/multiarray/shape.c @@ -59,13 +59,13 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, return NULL; } - if (self->descr->elsize == 0) { + if (PyArray_DESCR(self)->elsize == 0) { PyErr_SetString(PyExc_ValueError, "Bad data-type size."); return NULL; } newsize = 1; - largest = MAX_INTP / self->descr->elsize; + largest = MAX_INTP / PyArray_DESCR(self)->elsize; for(k = 0; k < new_nd; k++) { if (new_dimensions[k] == 0) { break; @@ -83,7 +83,7 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, oldsize = PyArray_SIZE(self); if (oldsize != newsize) { - if (!(self->flags & NPY_ARRAY_OWNDATA)) { + if (!(PyArray_FLAGS(self) & NPY_ARRAY_OWNDATA)) { PyErr_SetString(PyExc_ValueError, "cannot resize this array: it does not own its data"); return NULL; @@ -96,8 +96,8 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, refcnt = 1; } if ((refcnt > 2) - || (self->base != NULL) - || (self->weakreflist != NULL)) { + || (PyArray_BASE(self) != NULL) + || (((PyArrayObject_fieldaccess *)self)->weakreflist != NULL)) { PyErr_SetString(PyExc_ValueError, "cannot resize an array references or is referenced\n"\ "by another array in this way. Use the resize function"); @@ -105,60 +105,60 @@ PyArray_Resize(PyArrayObject *self, PyArray_Dims *newshape, int refcheck, } if (newsize == 0) { - sd = self->descr->elsize; + sd = PyArray_DESCR(self)->elsize; } else { - sd = newsize*self->descr->elsize; + sd = newsize*PyArray_DESCR(self)->elsize; } /* Reallocate space if needed */ - new_data = PyDataMem_RENEW(self->data, sd); + new_data = PyDataMem_RENEW(PyArray_DATA(self), sd); if (new_data == NULL) { PyErr_SetString(PyExc_MemoryError, "cannot allocate memory for array"); return NULL; } - self->data = new_data; + ((PyArrayObject_fieldaccess *)self)->data = new_data; } if ((newsize > oldsize) && PyArray_ISWRITEABLE(self)) { /* Fill new memory with zeros */ - elsize = self->descr->elsize; - if (PyDataType_FLAGCHK(self->descr, NPY_ITEM_REFCOUNT)) { + elsize = PyArray_DESCR(self)->elsize; + if (PyDataType_FLAGCHK(PyArray_DESCR(self), NPY_ITEM_REFCOUNT)) { PyObject *zero = PyInt_FromLong(0); char *optr; - optr = self->data + oldsize*elsize; + optr = PyArray_DATA(self) + oldsize*elsize; n = newsize - oldsize; for (k = 0; k < n; k++) { - _putzero((char *)optr, zero, self->descr); + _putzero((char *)optr, zero, PyArray_DESCR(self)); optr += elsize; } Py_DECREF(zero); } else{ - memset(self->data+oldsize*elsize, 0, (newsize-oldsize)*elsize); + memset(PyArray_DATA(self)+oldsize*elsize, 0, (newsize-oldsize)*elsize); } } - if (self->nd != new_nd) { + if (PyArray_NDIM(self) != new_nd) { /* Different number of dimensions. */ - self->nd = new_nd; + ((PyArrayObject_fieldaccess *)self)->nd = new_nd; /* Need new dimensions and strides arrays */ - dimptr = PyDimMem_RENEW(self->dimensions, 2*new_nd); + dimptr = PyDimMem_RENEW(PyArray_DIMS(self), 2*new_nd); if (dimptr == NULL) { PyErr_SetString(PyExc_MemoryError, "cannot allocate memory for array"); return NULL; } - self->dimensions = dimptr; - self->strides = dimptr + new_nd; + ((PyArrayObject_fieldaccess *)self)->dimensions = dimptr; + ((PyArrayObject_fieldaccess *)self)->strides = dimptr + new_nd; } /* make new_strides variable */ - sd = (size_t) self->descr->elsize; + sd = (size_t) PyArray_DESCR(self)->elsize; sd = (size_t) _array_fill_strides(new_strides, new_dimensions, new_nd, sd, - self->flags, &(self->flags)); - memmove(self->dimensions, new_dimensions, new_nd*sizeof(intp)); - memmove(self->strides, new_strides, new_nd*sizeof(intp)); + PyArray_FLAGS(self), &(((PyArrayObject_fieldaccess *)self)->flags)); + memmove(PyArray_DIMS(self), new_dimensions, new_nd*sizeof(intp)); + memmove(PyArray_STRIDES(self), new_strides, new_nd*sizeof(intp)); Py_INCREF(Py_None); return Py_None; } @@ -190,7 +190,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, order = PyArray_ISFORTRAN(self); } /* Quick check to make sure anything actually needs to be done */ - if (n == self->nd) { + if (n == PyArray_NDIM(self)) { same = TRUE; i = 0; while (same && i < n) { @@ -216,7 +216,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, if (i == 0) { strides = newstrides; } - flags = self->flags; + flags = PyArray_FLAGS(self); if (strides == NULL) { /* @@ -236,14 +236,14 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, (((PyArray_CHKFLAGS(self, NPY_ARRAY_C_CONTIGUOUS) && order == NPY_FORTRANORDER) || (PyArray_CHKFLAGS(self, NPY_ARRAY_F_CONTIGUOUS) && - order == NPY_CORDER)) && (self->nd > 1))) { + order == NPY_CORDER)) && (PyArray_NDIM(self) > 1))) { int success = 0; success = _attempt_nocopy_reshape(self,n,dimensions, newstrides,order); if (success) { /* no need to copy the array after all */ strides = newstrides; - flags = self->flags; + flags = PyArray_FLAGS(self); } else { PyObject *new; @@ -253,7 +253,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, } incref = FALSE; self = (PyArrayObject *)new; - flags = self->flags; + flags = PyArray_FLAGS(self); } } @@ -278,7 +278,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, */ if (order == NPY_FORTRANORDER) { if (strides[0] == 0) { - strides[0] = self->descr->elsize; + strides[0] = PyArray_DESCR(self)->elsize; } for (i = 1; i < n; i++) { if (strides[i] == 0) { @@ -288,7 +288,7 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, } else { if (strides[n-1] == 0) { - strides[n-1] = self->descr->elsize; + strides[n-1] = PyArray_DESCR(self)->elsize; } for (i = n - 2; i > -1; i--) { if (strides[i] == 0) { @@ -298,12 +298,12 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, } } - Py_INCREF(self->descr); + Py_INCREF(PyArray_DESCR(self)); ret = (PyAO *)PyArray_NewFromDescr(Py_TYPE(self), - self->descr, + PyArray_DESCR(self), n, dimensions, strides, - self->data, + PyArray_DATA(self), flags, (PyObject *)self); if (ret == NULL) { @@ -312,7 +312,10 @@ PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, if (incref) { Py_INCREF(self); } - ret->base = (PyObject *)self; + if (PyArray_SetBaseObject(ret, (PyObject *)self)) { + Py_DECREF(ret); + return NULL; + } PyArray_UpdateFlags(ret, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); return (PyObject *)ret; @@ -353,12 +356,12 @@ _check_ones(PyArrayObject *self, int newnd, intp* newdims, intp *strides) Bool done=FALSE; int j, k; - nd = self->nd; - dims = self->dimensions; + nd = PyArray_NDIM(self); + dims = PyArray_DIMS(self); for (k = 0, j = 0; !done && (j < nd || k < newnd);) { if ((j<nd) && (k<newnd) && (newdims[k] == dims[j])) { - strides[k] = self->strides[j]; + strides[k] = PyArray_STRIDES(self)[j]; j++; k++; } @@ -420,7 +423,7 @@ _putzero(char *optr, PyObject *zero, PyArray_Descr *dtype) * * The "is_f_order" argument describes how the array should be viewed * during the reshape, not how it is stored in memory (that - * information is in self->strides). + * information is in PyArray_STRIDES(self)). * * If some output dimensions have length 1, the strides assigned to * them are arbitrary. In the current implementation, they are the @@ -437,10 +440,10 @@ _attempt_nocopy_reshape(PyArrayObject *self, int newnd, intp* newdims, int np, op; oldnd = 0; - for (oi = 0; oi < self->nd; oi++) { - if (self->dimensions[oi]!= 1) { - olddims[oldnd] = self->dimensions[oi]; - oldstrides[oldnd] = self->strides[oi]; + for (oi = 0; oi < PyArray_NDIM(self); oi++) { + if (PyArray_DIMS(self)[oi]!= 1) { + olddims[oldnd] = PyArray_DIMS(self)[oi]; + oldstrides[oldnd] = PyArray_STRIDES(self)[oi]; oldnd++; } } @@ -590,40 +593,45 @@ _fix_unknown_dimension(PyArray_Dims *newshape, intp s_original) NPY_NO_EXPORT PyObject * PyArray_Squeeze(PyArrayObject *self) { - int nd = self->nd; + int nd = PyArray_NDIM(self); int newnd = nd; intp dimensions[MAX_DIMS]; intp strides[MAX_DIMS]; int i, j; - PyObject *ret; + PyArrayObject *ret; + PyArray_Descr *dtype; if (nd == 0) { Py_INCREF(self); return (PyObject *)self; } for (j = 0, i = 0; i < nd; i++) { - if (self->dimensions[i] == 1) { + if (PyArray_DIMS(self)[i] == 1) { newnd -= 1; } else { - dimensions[j] = self->dimensions[i]; - strides[j++] = self->strides[i]; + dimensions[j] = PyArray_DIMS(self)[i]; + strides[j++] = PyArray_STRIDES(self)[i]; } } - Py_INCREF(self->descr); - ret = PyArray_NewFromDescr(Py_TYPE(self), - self->descr, + dtype = PyArray_DESCR(self); + Py_INCREF(dtype); + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(self), + dtype, newnd, dimensions, - strides, self->data, - self->flags, + strides, PyArray_DATA(self), + PyArray_FLAGS(self), (PyObject *)self); if (ret == NULL) { return NULL; } - PyArray_FLAGS(ret) &= ~NPY_ARRAY_OWNDATA; - PyArray_BASE(ret) = (PyObject *)self; + PyArray_CLEARFLAGS(ret, NPY_ARRAY_OWNDATA); Py_INCREF(self); + if (PyArray_SetBaseObject(ret, (PyObject *)self) < 0) { + Py_DECREF(ret); + return NULL; + } return (PyObject *)ret; } @@ -643,7 +651,7 @@ PyArray_SwapAxes(PyArrayObject *ap, int a1, int a2) return (PyObject *)ap; } - n = ap->nd; + n = PyArray_NDIM(ap); if (n <= 1) { Py_INCREF(ap); return (PyObject *)ap; @@ -696,7 +704,7 @@ PyArray_Transpose(PyArrayObject *ap, PyArray_Dims *permute) PyArrayObject *ret = NULL; if (permute == NULL) { - n = ap->nd; + n = PyArray_NDIM(ap); for (i = 0; i < n; i++) { permutation[i] = n-1-i; } @@ -704,7 +712,7 @@ PyArray_Transpose(PyArrayObject *ap, PyArray_Dims *permute) else { n = permute->len; axes = permute->ptr; - if (n != ap->nd) { + if (n != PyArray_NDIM(ap)) { PyErr_SetString(PyExc_ValueError, "axes don't match array"); return NULL; @@ -715,9 +723,9 @@ PyArray_Transpose(PyArrayObject *ap, PyArray_Dims *permute) for (i = 0; i < n; i++) { axis = axes[i]; if (axis < 0) { - axis = ap->nd + axis; + axis = PyArray_NDIM(ap) + axis; } - if (axis < 0 || axis >= ap->nd) { + if (axis < 0 || axis >= PyArray_NDIM(ap)) { PyErr_SetString(PyExc_ValueError, "invalid axis for this array"); return NULL; @@ -736,26 +744,29 @@ PyArray_Transpose(PyArrayObject *ap, PyArray_Dims *permute) /* * this allocates memory for dimensions and strides (but fills them - * incorrectly), sets up descr, and points data at ap->data. + * incorrectly), sets up descr, and points data at PyArray_DATA(ap). */ - Py_INCREF(ap->descr); + Py_INCREF(PyArray_DESCR(ap)); ret = (PyArrayObject *)\ PyArray_NewFromDescr(Py_TYPE(ap), - ap->descr, - n, ap->dimensions, - NULL, ap->data, ap->flags, + PyArray_DESCR(ap), + n, PyArray_DIMS(ap), + NULL, PyArray_DATA(ap), PyArray_FLAGS(ap), (PyObject *)ap); if (ret == NULL) { return NULL; } /* point at true owner of memory: */ - ret->base = (PyObject *)ap; Py_INCREF(ap); + if (PyArray_SetBaseObject(ret, (PyObject *)ap) < 0) { + Py_DECREF(ret); + return NULL; + } /* fix the dimensions and strides of the return-array */ for (i = 0; i < n; i++) { - ret->dimensions[i] = ap->dimensions[permutation[i]]; - ret->strides[i] = ap->strides[permutation[i]]; + PyArray_DIMS(ret)[i] = PyArray_DIMS(ap)[permutation[i]]; + PyArray_STRIDES(ret)[i] = PyArray_STRIDES(ap)[permutation[i]]; } PyArray_UpdateFlags(ret, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); return (PyObject *)ret; @@ -866,13 +877,13 @@ PyArray_Ravel(PyArrayObject *a, NPY_ORDER order) /* If all the strides matched a contiguous layout, return a view */ if (i < 0) { - PyObject *ret; + PyArrayObject *ret; npy_intp stride = PyArray_DESCR(a)->elsize; val[0] = PyArray_SIZE(a); Py_INCREF(PyArray_DESCR(a)); - ret = PyArray_NewFromDescr(Py_TYPE(a), + ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(a), PyArray_DESCR(a), 1, val, &stride, @@ -881,12 +892,15 @@ PyArray_Ravel(PyArrayObject *a, NPY_ORDER order) (PyObject *)a); if (ret != NULL) { - PyArray_UpdateFlags((PyArrayObject *)ret, + PyArray_UpdateFlags(ret, NPY_ARRAY_C_CONTIGUOUS|NPY_ARRAY_F_CONTIGUOUS); Py_INCREF(a); - PyArray_BASE(ret) = (PyObject *)a; + if (PyArray_SetBaseObject(ret, (PyObject *)a) < 0) { + Py_DECREF(ret); + ret = NULL; + } } - return ret; + return (PyObject *)ret; } } @@ -908,9 +922,9 @@ PyArray_Flatten(PyArrayObject *a, NPY_ORDER order) } size = PyArray_SIZE(a); - Py_INCREF(a->descr); + Py_INCREF(PyArray_DESCR(a)); ret = (PyArrayObject *)PyArray_NewFromDescr(Py_TYPE(a), - a->descr, + PyArray_DESCR(a), 1, &size, NULL, NULL, @@ -919,7 +933,7 @@ PyArray_Flatten(PyArrayObject *a, NPY_ORDER order) if (ret == NULL) { return NULL; } - if (PyArray_CopyAnyIntoOrdered(ret, a, order) < 0) { + if (PyArray_CopyAsFlat(ret, a, order) < 0) { Py_DECREF(ret); return NULL; } diff --git a/numpy/core/src/npymath/halffloat.c b/numpy/core/src/npymath/halffloat.c index cea9a3bd7..d5ef57d7b 100644 --- a/numpy/core/src/npymath/halffloat.c +++ b/numpy/core/src/npymath/halffloat.c @@ -145,7 +145,7 @@ npy_half npy_half_nextafter(npy_half x, npy_half y) return ret; } - + int npy_half_eq_nonan(npy_half h1, npy_half h2) { return (h1 == h2 || ((h1 | h2) & 0x7fff) == 0); @@ -239,7 +239,7 @@ npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f) h_sgn = (npy_uint16) ((f&0x80000000u) >> 16); f_exp = (f&0x7f800000u); - + /* Exponent overflow/NaN converts to signed inf/NaN */ if (f_exp >= 0x47800000u) { if (f_exp == 0x7f800000u) { @@ -265,15 +265,15 @@ npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f) return (npy_uint16) (h_sgn + 0x7c00u); } } - + /* Exponent underflow converts to a subnormal half or signed zero */ if (f_exp <= 0x38000000u) { - /* + /* * Signed zeros, subnormal floats, and floats with small * exponents all convert to signed zero halfs. */ if (f_exp < 0x33000000u) { -#if NPY_HALF_GENERATE_UNDERFLOW +#if NPY_HALF_GENERATE_UNDERFLOW /* If f != 0, it underflowed to 0 */ if ((f&0x7fffffff) != 0) { npy_set_floatstatus_underflow(); @@ -284,7 +284,7 @@ npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f) /* Make the subnormal significand */ f_exp >>= 23; f_sig = (0x00800000u + (f&0x007fffffu)); -#if NPY_HALF_GENERATE_UNDERFLOW +#if NPY_HALF_GENERATE_UNDERFLOW /* If it's not exactly represented, it underflowed */ if ((f_sig&(((npy_uint32)1 << (126 - f_exp)) - 1)) != 0) { npy_set_floatstatus_underflow(); @@ -292,7 +292,7 @@ npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f) #endif f_sig >>= (113 - f_exp); /* Handle rounding by adding 1 to the bit beyond half precision */ -#if NPY_HALF_ROUND_TIES_TO_EVEN +#if NPY_HALF_ROUND_TIES_TO_EVEN /* * If the last bit in the half significand is 0 (already even), and * the remaining bit pattern is 1000...0, then we do not add one @@ -317,7 +317,7 @@ npy_uint16 npy_floatbits_to_halfbits(npy_uint32 f) h_exp = (npy_uint16) ((f_exp - 0x38000000u) >> 13); /* Handle rounding by adding 1 to the bit beyond half precision */ f_sig = (f&0x007fffffu); -#if NPY_HALF_ROUND_TIES_TO_EVEN +#if NPY_HALF_ROUND_TIES_TO_EVEN /* * If the last bit in the half significand is 0 (already even), and * the remaining bit pattern is 1000...0, then we do not add one @@ -354,7 +354,7 @@ npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d) h_sgn = (d&0x8000000000000000ULL) >> 48; d_exp = (d&0x7ff0000000000000ULL); - + /* Exponent overflow/NaN converts to signed inf/NaN */ if (d_exp >= 0x40f0000000000000ULL) { if (d_exp == 0x7ff0000000000000ULL) { @@ -380,15 +380,15 @@ npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d) return h_sgn + 0x7c00u; } } - + /* Exponent underflow converts to subnormal half or signed zero */ if (d_exp <= 0x3f00000000000000ULL) { - /* + /* * Signed zeros, subnormal floats, and floats with small * exponents all convert to signed zero halfs. */ if (d_exp < 0x3e60000000000000ULL) { -#if NPY_HALF_GENERATE_UNDERFLOW +#if NPY_HALF_GENERATE_UNDERFLOW /* If d != 0, it underflowed to 0 */ if ((d&0x7fffffffffffffffULL) != 0) { npy_set_floatstatus_underflow(); @@ -399,7 +399,7 @@ npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d) /* Make the subnormal significand */ d_exp >>= 52; d_sig = (0x0010000000000000ULL + (d&0x000fffffffffffffULL)); -#if NPY_HALF_GENERATE_UNDERFLOW +#if NPY_HALF_GENERATE_UNDERFLOW /* If it's not exactly represented, it underflowed */ if ((d_sig&(((npy_uint64)1 << (1051 - d_exp)) - 1)) != 0) { npy_set_floatstatus_underflow(); @@ -407,7 +407,7 @@ npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d) #endif d_sig >>= (1009 - d_exp); /* Handle rounding by adding 1 to the bit beyond half precision */ -#if NPY_HALF_ROUND_TIES_TO_EVEN +#if NPY_HALF_ROUND_TIES_TO_EVEN /* * If the last bit in the half significand is 0 (already even), and * the remaining bit pattern is 1000...0, then we do not add one @@ -432,7 +432,7 @@ npy_uint16 npy_doublebits_to_halfbits(npy_uint64 d) h_exp = (npy_uint16) ((d_exp - 0x3f00000000000000ULL) >> 42); /* Handle rounding by adding 1 to the bit beyond half precision */ d_sig = (d&0x000fffffffffffffULL); -#if NPY_HALF_ROUND_TIES_TO_EVEN +#if NPY_HALF_ROUND_TIES_TO_EVEN /* * If the last bit in the half significand is 0 (already even), and * the remaining bit pattern is 1000...0, then we do not add one @@ -527,4 +527,4 @@ npy_uint64 npy_halfbits_to_doublebits(npy_uint16 h) return d_sgn + (((npy_uint64)(h&0x7fffu) + 0xfc000u) << 42); } } - + diff --git a/numpy/core/src/npymath/ieee754.c.src b/numpy/core/src/npymath/ieee754.c.src index 71be7b8ce..a81cabe5b 100644 --- a/numpy/core/src/npymath/ieee754.c.src +++ b/numpy/core/src/npymath/ieee754.c.src @@ -128,7 +128,7 @@ float _nextf(float x, int p) #ifdef HAVE_LDOUBLE_DOUBLE_DOUBLE_BE -/* +/* * FIXME: this is ugly and untested. The asm part only works with gcc, and we * should consolidate the GET_LDOUBLE* / SET_LDOUBLE macros */ @@ -561,7 +561,7 @@ npy_longdouble npy_nextafterl(npy_longdouble x, npy_longdouble y) void npy_set_floatstatus_divbyzero(void) { fpsetsticky(FP_X_DZ); -} +} void npy_set_floatstatus_overflow(void) { @@ -593,7 +593,7 @@ void npy_set_floatstatus_invalid(void) void npy_set_floatstatus_divbyzero(void) { feraiseexcept(FE_DIVBYZERO); -} +} void npy_set_floatstatus_overflow(void) { @@ -617,7 +617,7 @@ void npy_set_floatstatus_invalid(void) void npy_set_floatstatus_divbyzero(void) { fp_raise_xcp(FP_DIV_BY_ZERO); -} +} void npy_set_floatstatus_overflow(void) { diff --git a/numpy/core/src/npysort/sort.c.src b/numpy/core/src/npysort/sort.c.src index e42597384..17f734e11 100644 --- a/numpy/core/src/npysort/sort.c.src +++ b/numpy/core/src/npysort/sort.c.src @@ -469,7 +469,7 @@ mergesort0_@suff@(@type@ *pl, @type@ *pr, @type@ *pw, @type@ *vp, size_t len) int mergesort_@suff@(@type@ *start, npy_intp num, PyArrayObject *arr) { - const size_t elsize = arr->descr->elsize; + const size_t elsize = PyArray_DESCR(arr)->elsize; const size_t len = elsize / sizeof(@type@); @type@ *pl, *pr, *pw, *vp; int err = 0; @@ -500,8 +500,8 @@ fail_0: int quicksort_@suff@(@type@ *start, npy_intp num, PyArrayObject *arr) { - const size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *vp = malloc(arr->descr->elsize); + const size_t len = PyArray_DESCR(arr)->elsize/sizeof(@type@); + @type@ *vp = malloc(PyArray_DESCR(arr)->elsize); @type@ *pl = start; @type@ *pr = start + (num - 1)*len; @type@ *stack[PYA_QS_STACK], **sptr = stack, *pm, *pi, *pj, *pk; @@ -567,8 +567,8 @@ quicksort_@suff@(@type@ *start, npy_intp num, PyArrayObject *arr) int heapsort_@suff@(@type@ *start, npy_intp n, PyArrayObject *arr) { - size_t len = arr->descr->elsize/sizeof(@type@); - @type@ *tmp = malloc(arr->descr->elsize); + size_t len = PyArray_DESCR(arr)->elsize/sizeof(@type@); + @type@ *tmp = malloc(PyArray_DESCR(arr)->elsize); @type@ *a = start - len; npy_intp i,j,l; @@ -616,7 +616,7 @@ heapsort_@suff@(@type@ *start, npy_intp n, PyArrayObject *arr) int aheapsort_@suff@(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) { - size_t len = arr->descr->elsize/sizeof(@type@); + size_t len = PyArray_DESCR(arr)->elsize/sizeof(@type@); npy_intp *a, i,j,l, tmp; /* The array needs to be offset by one for heapsort indexing */ @@ -665,7 +665,7 @@ aheapsort_@suff@(@type@ *v, npy_intp *tosort, npy_intp n, PyArrayObject *arr) int aquicksort_@suff@(@type@ *v, npy_intp* tosort, npy_intp num, PyArrayObject *arr) { - size_t len = arr->descr->elsize/sizeof(@type@); + size_t len = PyArray_DESCR(arr)->elsize/sizeof(@type@); @type@ *vp; npy_intp *pl = tosort; npy_intp *pr = tosort + num - 1; @@ -775,7 +775,7 @@ amergesort0_@suff@(npy_intp *pl, npy_intp *pr, @type@ *v, npy_intp *pw, int len) int amergesort_@suff@(@type@ *v, npy_intp *tosort, npy_intp num, PyArrayObject *arr) { - const size_t elsize = arr->descr->elsize; + const size_t elsize = PyArray_DESCR(arr)->elsize; const size_t len = elsize / sizeof(@type@); npy_intp *pl, *pr, *pw; diff --git a/numpy/core/src/private/lowlevel_strided_loops.h b/numpy/core/src/private/lowlevel_strided_loops.h index b5b8159e6..b6b53ba45 100644 --- a/numpy/core/src/private/lowlevel_strided_loops.h +++ b/numpy/core/src/private/lowlevel_strided_loops.h @@ -19,7 +19,7 @@ * The 'transferdata' parameter is slightly special, following a * generic auxiliary data pattern defined in ndarraytypes.h * Use NPY_AUXDATA_CLONE and NPY_AUXDATA_FREE to deal with this data. - * + * */ typedef void (PyArray_StridedTransferFn)(char *dst, npy_intp dst_stride, char *src, npy_intp src_stride, @@ -47,10 +47,10 @@ typedef void (PyArray_MaskedStridedTransferFn)(char *dst, npy_intp dst_stride, * aligned: * Should be 1 if the src and dst pointers are always aligned, * 0 otherwise. - * src_stride: + * src_stride: * Should be the src stride if it will always be the same, * NPY_MAX_INTP otherwise. - * dst_stride: + * dst_stride: * Should be the dst stride if it will always be the same, * NPY_MAX_INTP otherwise. * itemsize: @@ -143,10 +143,10 @@ PyArray_GetDTypeCopySwapFn(int aligned, * aligned: * Should be 1 if the src and dst pointers are always aligned, * 0 otherwise. - * src_stride: + * src_stride: * Should be the src stride if it will always be the same, * NPY_MAX_INTP otherwise. - * dst_stride: + * dst_stride: * Should be the dst stride if it will always be the same, * NPY_MAX_INTP otherwise. * src_dtype: @@ -363,9 +363,9 @@ PyArray_TransferMaskedStridedToNDim(npy_intp ndim, PyArray_CompareLists(PyArray_DIMS(arr1), \ PyArray_DIMS(arr2), \ PyArray_NDIM(arr1)) && \ - (arr1->flags&(NPY_ARRAY_C_CONTIGUOUS| \ + (PyArray_FLAGS(arr1)&(NPY_ARRAY_C_CONTIGUOUS| \ NPY_ARRAY_F_CONTIGUOUS)) == \ - (arr2->flags&(NPY_ARRAY_C_CONTIGUOUS| \ + (PyArray_FLAGS(arr2)&(NPY_ARRAY_C_CONTIGUOUS| \ NPY_ARRAY_F_CONTIGUOUS)) \ ) diff --git a/numpy/core/src/private/npy_config.h b/numpy/core/src/private/npy_config.h index b4842b832..237dc94ab 100644 --- a/numpy/core/src/private/npy_config.h +++ b/numpy/core/src/private/npy_config.h @@ -10,7 +10,7 @@ #endif /* Safe to use ldexp and frexp for long double for MSVC builds */ -#if (SIZEOF_LONG_DOUBLE == SIZEOF_DOUBLE) || defined(_MSC_VER) +#if (SIZEOF_LONG_DOUBLE == SIZEOF_DOUBLE) || defined(_MSC_VER) #ifdef HAVE_LDEXP #define HAVE_LDEXPL 1 #endif @@ -24,7 +24,7 @@ #undef HAVE_ATAN2 #endif -/* +/* * On Mac OS X, because there is only one configuration stage for all the archs * in universal builds, any macro which depends on the arch needs to be * harcoded diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 2ffca63d2..1aca37bc7 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -61,7 +61,6 @@ /********************/ #define USE_USE_DEFAULTS 1 -#define USE_NEW_ITERATOR_GENFUNC 1 /********************/ /* ---------------------------------------------------------------- */ @@ -798,7 +797,7 @@ static int get_ufunc_arguments(PyUFuncObject *self, } /* If it's an array, can use it */ if (PyArray_Check(obj)) { - if (!PyArray_ISWRITEABLE(obj)) { + if (!PyArray_ISWRITEABLE((PyArrayObject *)obj)) { PyErr_SetString(PyExc_ValueError, "return array is not writeable"); return -1; @@ -890,7 +889,7 @@ static int get_ufunc_arguments(PyUFuncObject *self, } if (PyArray_Check(value)) { - if (!PyArray_ISWRITEABLE(value)) { + if (!PyArray_ISWRITEABLE((PyArrayObject *)value)) { PyErr_SetString(PyExc_ValueError, "return array is not writeable"); goto fail; @@ -1116,6 +1115,7 @@ prepare_ufunc_output(PyUFuncObject *self, { if (arr_prep != NULL && arr_prep != Py_None) { PyObject *res; + PyArrayObject *arr; res = PyObject_CallFunction(arr_prep, "O(OOi)", *op, self, arr_prep_args, i); @@ -1128,32 +1128,33 @@ prepare_ufunc_output(PyUFuncObject *self, Py_XDECREF(res); return -1; } + arr = (PyArrayObject *)res; /* If the same object was returned, nothing to do */ - if (res == (PyObject *)*op) { - Py_DECREF(res); + if (arr == *op) { + Py_DECREF(arr); } /* If the result doesn't match, throw an error */ - else if (PyArray_NDIM(res) != PyArray_NDIM(*op) || - !PyArray_CompareLists(PyArray_DIMS(res), + else if (PyArray_NDIM(arr) != PyArray_NDIM(*op) || + !PyArray_CompareLists(PyArray_DIMS(arr), PyArray_DIMS(*op), - PyArray_NDIM(res)) || - !PyArray_CompareLists(PyArray_STRIDES(res), + PyArray_NDIM(arr)) || + !PyArray_CompareLists(PyArray_STRIDES(arr), PyArray_STRIDES(*op), - PyArray_NDIM(res)) || - !PyArray_EquivTypes(PyArray_DESCR(res), + PyArray_NDIM(arr)) || + !PyArray_EquivTypes(PyArray_DESCR(arr), PyArray_DESCR(*op))) { PyErr_SetString(PyExc_TypeError, "__array_prepare__ must return an " "ndarray or subclass thereof which is " "otherwise identical to its input"); - Py_DECREF(res); + Py_DECREF(arr); return -1; } /* Replace the op value */ else { Py_DECREF(*op); - *op = (PyArrayObject *)res; + *op = arr; } } @@ -3468,7 +3469,7 @@ PyUFunc_GenericReduction(PyUFuncObject *self, PyObject *args, return NULL; } /* Check to see if input is zero-dimensional */ - if (mp->nd == 0) { + if (PyArray_NDIM(mp) == 0) { PyErr_Format(PyExc_TypeError, "cannot %s on a scalar", _reduce_type[operation]); Py_XDECREF(otype); @@ -3487,9 +3488,9 @@ PyUFunc_GenericReduction(PyUFuncObject *self, PyObject *args, } if (axis < 0) { - axis += mp->nd; + axis += PyArray_NDIM(mp); } - if (axis < 0 || axis >= mp->nd) { + if (axis < 0 || axis >= PyArray_NDIM(mp)) { PyErr_SetString(PyExc_ValueError, "axis not in array"); Py_XDECREF(otype); Py_DECREF(mp); @@ -3500,7 +3501,7 @@ PyUFunc_GenericReduction(PyUFuncObject *self, PyObject *args, * unless otype already specified. */ if (otype == NULL && out != NULL) { - otype = out->descr; + otype = PyArray_DESCR(out); Py_INCREF(otype); } if (otype == NULL) { @@ -3515,7 +3516,7 @@ PyUFunc_GenericReduction(PyUFuncObject *self, PyObject *args, if (PyTypeNum_ISBOOL(typenum)) { typenum = PyArray_LONG; } - else if ((size_t)mp->descr->elsize < sizeof(long)) { + else if ((size_t)PyArray_DESCR(mp)->elsize < sizeof(long)) { if (PyTypeNum_ISUNSIGNED(typenum)) { typenum = PyArray_ULONG; } @@ -4322,16 +4323,16 @@ ufunc_outer(PyUFuncObject *self, PyObject *args, PyObject *kwds) return NULL; } /* Construct new shape tuple */ - shape1 = PyTuple_New(ap1->nd); + shape1 = PyTuple_New(PyArray_NDIM(ap1)); if (shape1 == NULL) { goto fail; } - for (i = 0; i < ap1->nd; i++) { + for (i = 0; i < PyArray_NDIM(ap1); i++) { PyTuple_SET_ITEM(shape1, i, - PyLong_FromLongLong((longlong)ap1->dimensions[i])); + PyLong_FromLongLong((longlong)PyArray_DIMS(ap1)[i])); } - shape2 = PyTuple_New(ap2->nd); - for (i = 0; i < ap2->nd; i++) { + shape2 = PyTuple_New(PyArray_NDIM(ap2)); + for (i = 0; i < PyArray_NDIM(ap2); i++) { PyTuple_SET_ITEM(shape2, i, PyInt_FromLong((long) 1)); } if (shape2 == NULL) { diff --git a/numpy/core/src/umath/ufunc_type_resolution.c b/numpy/core/src/umath/ufunc_type_resolution.c index b08a7f165..fb7352070 100644 --- a/numpy/core/src/umath/ufunc_type_resolution.c +++ b/numpy/core/src/umath/ufunc_type_resolution.c @@ -665,7 +665,7 @@ timedelta_dtype_with_copied_meta(PyArray_Descr *dtype) * m8[<A>] + M8[<B>] => m8[gcd(<A>,<B>)] + M8[gcd(<A>,<B>)] * TODO: Non-linear time unit cases require highly special-cased loops * M8[<A>] + m8[Y|M|B] - * m8[Y|M|B] + M8[<A>] + * m8[Y|M|B] + M8[<A>] */ NPY_NO_EXPORT int PyUFunc_AdditionTypeResolution(PyUFuncObject *ufunc, @@ -1360,7 +1360,7 @@ static NpyAuxData * ufunc_masker_data_clone(NpyAuxData *data) { _ufunc_masker_data *n; - + /* Allocate a new one */ n = (_ufunc_masker_data *)PyArray_malloc(sizeof(_ufunc_masker_data)); if (n == NULL) { diff --git a/numpy/core/tests/test_datetime.py b/numpy/core/tests/test_datetime.py index f5673b61f..a624768b6 100644 --- a/numpy/core/tests/test_datetime.py +++ b/numpy/core/tests/test_datetime.py @@ -514,7 +514,7 @@ class TestDateTime(TestCase): formatter={'datetime': lambda x : "'%s'" % np.datetime_as_string(x, timezone='UTC')}), "['2011-03-16T13:55Z', '1920-01-01T03:12Z']") - + def test_pickle(self): # Check that pickle roundtripping works diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index ff13dcad6..6dc3e7554 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -185,6 +185,12 @@ class TestRecord(TestCase): 'formats':['i1', 'O'], 'offsets':[np.dtype('intp').itemsize, 0]}) + def test_comma_datetime(self): + dt = np.dtype('M8[D],datetime64[Y],i8') + assert_equal(dt, np.dtype([('f0', 'M8[D]'), + ('f1', 'datetime64[Y]'), + ('f2', 'i8')])) + class TestSubarray(TestCase): def test_single_subarray(self): a = np.dtype((np.int, (2))) @@ -291,6 +297,25 @@ class TestString(TestCase): "('bottom', [('bleft', ('>f4', (8, 64)), (1,)), " "('bright', '>f4', (8, 36))])]") + # If the sticky aligned flag is set to True, it makes the + # str() function use a dict representation with an 'aligned' flag + dt = np.dtype([('top', [('tiles', ('>f4', (64, 64)), (1,)), + ('rtile', '>f4', (64, 36))], + (3,)), + ('bottom', [('bleft', ('>f4', (8, 64)), (1,)), + ('bright', '>f4', (8, 36))])], + align=True) + assert_equal(str(dt), + "{'names':['top','bottom'], " + "'formats':[([('tiles', ('>f4', (64, 64)), (1,)), " + "('rtile', '>f4', (64, 36))], (3,))," + "[('bleft', ('>f4', (8, 64)), (1,)), " + "('bright', '>f4', (8, 36))]], " + "'offsets':[0,76800], " + "'itemsize':80000, " + "'aligned':True}") + assert_equal(np.dtype(eval(str(dt))), dt) + dt = np.dtype({'names': ['r','g','b'], 'formats': ['u1', 'u1', 'u1'], 'offsets': [0, 1, 2], 'titles': ['Red pixel', 'Green pixel', 'Blue pixel']}) @@ -322,6 +347,10 @@ class TestString(TestCase): " 'titles':['Red pixel','Blue pixel']," " 'itemsize':3}") + dt = np.dtype([('a', '<m8[D]'), ('b', '<M8[us]')]) + assert_equal(str(dt), + "[('a', '<m8[D]'), ('b', '<M8[us]')]") + def test_complex_dtype_repr(self): dt = np.dtype([('top', [('tiles', ('>f4', (64, 64)), (1,)), ('rtile', '>f4', (64, 36))], (3,)), @@ -366,5 +395,10 @@ class TestString(TestCase): "'titles':['Red pixel','Blue pixel'], " "'itemsize':4})") + dt = np.dtype([('a', '<M8[D]'), ('b', '<m8[us]')]) + assert_equal(repr(dt), + "dtype([('a', '<M8[D]'), ('b', '<m8[us]')])") + + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index d0161b366..d7136db51 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -71,7 +71,8 @@ class TestEinSum(TestCase): def test_einsum_views(self): # pass-through - a = np.arange(6).reshape(2,3) + a = np.arange(6) + a.shape = (2,3) b = np.einsum("...", a) assert_(b.base is a) @@ -88,7 +89,8 @@ class TestEinSum(TestCase): assert_equal(b, a) # transpose - a = np.arange(6).reshape(2,3) + a = np.arange(6) + a.shape = (2,3) b = np.einsum("ji", a) assert_(b.base is a) @@ -99,7 +101,8 @@ class TestEinSum(TestCase): assert_equal(b, a.T) # diagonal - a = np.arange(9).reshape(3,3) + a = np.arange(9) + a.shape = (3,3) b = np.einsum("ii->i", a) assert_(b.base is a) @@ -110,7 +113,8 @@ class TestEinSum(TestCase): assert_equal(b, [a[i,i] for i in range(3)]) # diagonal with various ways of broadcasting an additional dimension - a = np.arange(27).reshape(3,3,3) + a = np.arange(27) + a.shape = (3,3,3) b = np.einsum("...ii->...i", a) assert_(b.base is a) @@ -173,7 +177,8 @@ class TestEinSum(TestCase): for x in a.transpose(1,0,2)]) # triple diagonal - a = np.arange(27).reshape(3,3,3) + a = np.arange(27) + a.shape = (3,3,3) b = np.einsum("iii->i", a) assert_(b.base is a) @@ -184,7 +189,8 @@ class TestEinSum(TestCase): assert_equal(b, [a[i,i,i] for i in range(3)]) # swap axes - a = np.arange(24).reshape(2,3,4) + a = np.arange(24) + a.shape = (2,3,4) b = np.einsum("ijk->jik", a) assert_(b.base is a) diff --git a/numpy/core/tests/test_half.py b/numpy/core/tests/test_half.py index 37fce6460..8f1aa92c2 100644 --- a/numpy/core/tests/test_half.py +++ b/numpy/core/tests/test_half.py @@ -221,7 +221,7 @@ class TestHalf(TestCase): a = np.zeros((5,), dtype=float16) a.fill(1) assert_equal(a, np.ones((5,), dtype=float16)) - + # nonzero and copyswap a = np.array([0,0,-1,-1/1e20,0,2.0**-24, 7.629e-6], dtype=float16) assert_equal(a.nonzero()[0], @@ -235,7 +235,7 @@ class TestHalf(TestCase): b = np.ones((20,), dtype=float16) assert_equal(np.dot(a,b), 95) - + # argmax a = np.array([0, -np.inf, -2, 0.5, 12.55, 7.3, 2.1, 12.4], dtype=float16) assert_equal(a.argmax(), @@ -271,7 +271,7 @@ class TestHalf(TestCase): assert_equal(np.nextafter(a_f16[0], hinf), -a_f16[1]) assert_equal(np.nextafter(a_f16[1:], hinf), a_f16[:-1]) assert_equal(np.nextafter(a_f16[:-1], -hinf), a_f16[1:]) - + def test_half_ufuncs(self): """Test the various ufuncs""" @@ -302,7 +302,7 @@ class TestHalf(TestCase): assert_equal(np.signbit(b), [True,False,False,False,False]) assert_equal(np.copysign(b,a), [2,5,1,4,3]) - + assert_equal(np.maximum(a,b), [0,5,2,4,3]) x = np.maximum(b,c) assert_(np.isnan(x[3])) @@ -365,7 +365,7 @@ class TestHalf(TestCase): bx16 = np.array((1e4,),dtype=float16) sy16 = float16(1e-4) by16 = float16(1e4) - + # Underflow errors assert_raises_fpe('underflow', lambda a,b:a*b, sx16, sx16) assert_raises_fpe('underflow', lambda a,b:a*b, sx16, sy16) diff --git a/numpy/core/tests/test_memmap.py b/numpy/core/tests/test_memmap.py index 18b356ce2..fa4eb9de0 100644 --- a/numpy/core/tests/test_memmap.py +++ b/numpy/core/tests/test_memmap.py @@ -4,7 +4,7 @@ import warnings from numpy import memmap from numpy import arange, allclose -from numpy.testing import TestCase, assert_, assert_array_equal +from numpy.testing import * class TestMemmap(TestCase): def setUp(self): @@ -69,23 +69,22 @@ class TestMemmap(TestCase): fp = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape) fp[:] = self.data[:] + assert_equal(fp[0], self.data[0]) fp.flush() def test_del(self): # Make sure a view does not delete the underlying mmap fp_base = memmap(self.tmpfp, dtype=self.dtype, mode='w+', shape=self.shape) - fp_view = fp_base[:] - class ViewCloseError(Exception): - pass - _close = memmap._close - def replace_close(self): - raise ViewCloseError('View should not call _close on memmap') - try: - memmap._close = replace_close - del fp_view - finally: - memmap._close = _close + fp_base[0] = 5 + fp_view = fp_base[0:1] + assert_equal(fp_view[0], 5) + del fp_view + # Should still be able to access and assign values after + # deleting the view + assert_equal(fp_base[0], 5) + fp_base[0] = 6 + assert_equal(fp_base[0], 6) if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 6821fbe49..03f1b10c6 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1056,8 +1056,12 @@ class TestPutmask(object): dtype=[('x', '<f8'), ('y', '>f8'), ('z', '<f8')]) np.putmask(rec['x'],[True,False],10) assert_array_equal(rec['x'],[10,5]) - np.putmask(rec['y'],[True,False],10) - assert_array_equal(rec['y'],[10,4]) + assert_array_equal(rec['y'],[2,4]) + assert_array_equal(rec['z'],[3,3]) + np.putmask(rec['y'],[True,False],11) + assert_array_equal(rec['x'],[10,5]) + assert_array_equal(rec['y'],[11,4]) + assert_array_equal(rec['z'],[3,3]) def test_masked_array(self): ## x = np.array([1,2,3]) diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 974a6d6f8..773ce9a3b 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -299,7 +299,7 @@ class TestUfunc(TestCase): b[:] = 0 np.add(a,0.5,sig=('i4','i4','i4'),out=b, casting='unsafe') assert_equal(b, [0, 0, 1]) - + def test_inner1d(self): a = np.arange(6).reshape((2,3)) @@ -458,8 +458,8 @@ class TestUfunc(TestCase): for s2 in slice_3: a1 = d1.transpose(p1)[s1] a2 = d2.transpose(p2)[s2] - ref = ref and a1.base != None and a1.base.base != None - ref = ref and a2.base != None and a2.base.base != None + ref = ref and a1.base != None + ref = ref and a2.base != None if broadcastable(a1.shape[-1], a2.shape[-2]) and \ broadcastable(a1.shape[0], a2.shape[0]): assert_array_almost_equal( diff --git a/numpy/f2py/doc/multiarray/array_from_pyobj.c b/numpy/f2py/doc/multiarray/array_from_pyobj.c index 7e0de9a74..5a700eecf 100644 --- a/numpy/f2py/doc/multiarray/array_from_pyobj.c +++ b/numpy/f2py/doc/multiarray/array_from_pyobj.c @@ -1,14 +1,14 @@ -/* - * File: array_from_pyobj.c +/* + * File: array_from_pyobj.c * * Description: - * ------------ + * ------------ * Provides array_from_pyobj function that returns a contigious array * object with the given dimensions and required storage order, either * in row-major (C) or column-major (Fortran) order. The function * array_from_pyobj is very flexible about its Python object argument * that can be any number, list, tuple, or array. - * + * * array_from_pyobj is used in f2py generated Python extension * modules. * @@ -80,8 +80,8 @@ PyArrayObject* array_from_pyobj(const int type_num, if (intent & F2PY_INTENT_CACHE) { /* Don't expect correct storage order or anything reasonable when - returning cache array. */ - if ((intent & F2PY_INTENT_HIDE) + returning cache array. */ + if ((intent & F2PY_INTENT_HIDE) || (obj==Py_None)) { PyArrayObject *arr = NULL; CHECK_DIMS_DEFINED(rank,dims,"optional,intent(cache) must" @@ -92,7 +92,7 @@ PyArrayObject* array_from_pyobj(const int type_num, Py_INCREF(arr); return arr; } - if (PyArray_Check(obj) + if (PyArray_Check(obj) && ISCONTIGUOUS((PyArrayObject *)obj) && HAS_PROPER_ELSIZE((PyArrayObject *)obj,type_num) ) { @@ -124,7 +124,7 @@ PyArrayObject* array_from_pyobj(const int type_num, intent(inout) */ PyArrayObject *arr = (PyArrayObject *)obj; - int is_cont = (intent & F2PY_INTENT_C) ? + int is_cont = (intent & F2PY_INTENT_C) ? (ISCONTIGUOUS(arr)) : (array_has_column_major_storage(arr)); if (check_and_fix_dimensions(arr,rank,dims)) @@ -166,7 +166,7 @@ PyArrayObject* array_from_pyobj(const int type_num, if ((obj==Py_None) && (intent & F2PY_OPTIONAL)) { PyArrayObject *arr = NULL; - CHECK_DIMS_DEFINED(rank,dims,"optional must have defined dimensions.\n"); + CHECK_DIMS_DEFINED(rank,dims,"optional must have defined dimensions.\n"); arr = (PyArrayObject *)PyArray_FromDims(rank,dims,type_num); ARR_IS_NULL(arr,"FromDims failed: optional.\n"); if (intent & F2PY_INTENT_OUT) { @@ -240,8 +240,8 @@ void lazy_transpose(PyArrayObject* arr) { Note that this function is assumed to be used even times for a given array. Otherwise, the caller should set flags &= ~CONTIGUOUS. */ - int rank,i,s,j; - rank = arr->nd; + int rank,i,s,j; + rank = arr->nd; if (rank < 2) return; for(i=0,j=rank-1;i<rank/2;++i,--j) { @@ -268,7 +268,7 @@ int check_and_fix_dimensions(const PyArrayObject* arr,const int rank,int *dims) int free_axe = -1; int i; /* Fill dims where -1 or 0; check dimensions; calc new_size; */ - for(i=0;i<arr->nd;++i) { + for(i=0;i<arr->nd;++i) { if (dims[i] >= 0) { if (dims[i]!=arr->dimensions[i]) { fprintf(stderr,"%d-th dimension must be fixed to %d but got %d\n", @@ -311,7 +311,7 @@ int check_and_fix_dimensions(const PyArrayObject* arr,const int rank,int *dims) if (arr->dimensions[i]!=dims[i]) { fprintf(stderr,"%d-th dimension must be fixed to %d but got %d\n", i,dims[i],arr->dimensions[i]); - return 1; + return 1; } if (!dims[i]) dims[i] = 1; } else diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 13f659d70..f7cde270d 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -627,7 +627,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. - Otherwise mono-dimensional axes will be squeezed. + Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 @@ -803,7 +803,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, fh.close() X = np.array(X, dtype) - # Multicolumn data are returned with shape (1, N, M), i.e. + # Multicolumn data are returned with shape (1, N, M), i.e. # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) diff --git a/numpy/lib/src/_compiled_base.c b/numpy/lib/src/_compiled_base.c index 066519bf1..ea7205a8a 100644 --- a/numpy/lib/src/_compiled_base.c +++ b/numpy/lib/src/_compiled_base.c @@ -1,12 +1,13 @@ +#define NPY_NO_DEPRECATED_API #include "Python.h" #include "structmember.h" #include "numpy/noprefix.h" #include "npy_config.h" -static intp -incr_slot_(double x, double *bins, intp lbins) +static npy_intp +incr_slot_(double x, double *bins, npy_intp lbins) { - intp i; + npy_intp i; for ( i = 0; i < lbins; i ++ ) { if ( x < bins [i] ) { @@ -16,10 +17,10 @@ incr_slot_(double x, double *bins, intp lbins) return lbins; } -static intp -decr_slot_(double x, double * bins, intp lbins) +static npy_intp +decr_slot_(double x, double * bins, npy_intp lbins) { - intp i; + npy_intp i; for ( i = lbins - 1; i >= 0; i -- ) { if (x < bins [i]) { @@ -57,11 +58,11 @@ monotonic_(double * a, int lena) /* find the index of the maximum element of an integer array */ -static intp -mxx (intp *i , intp len) +static npy_intp +mxx (npy_intp *i , npy_intp len) { - intp mx = 0, max = i[0]; - intp j; + npy_intp mx = 0, max = i[0]; + npy_intp j; for ( j = 1; j < len; j ++ ) { if ( i [j] > max ) { @@ -73,11 +74,11 @@ mxx (intp *i , intp len) } /* find the index of the minimum element of an integer array */ -static intp -mnx (intp *i , intp len) +static npy_intp +mnx (npy_intp *i , npy_intp len) { - intp mn = 0, min = i [0]; - intp j; + npy_intp mn = 0, min = i [0]; + npy_intp j; for ( j = 1; j < len; j ++ ) if ( i [j] < min ) @@ -105,8 +106,8 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) { PyArray_Descr *type; PyObject *list = NULL, *weight=Py_None, *mlength=Py_None; - PyObject *lst=NULL, *ans=NULL, *wts=NULL; - intp *numbers, *ians, len , mxi, mni, ans_size, minlength; + PyArrayObject *lst=NULL, *ans=NULL, *wts=NULL; + npy_intp *numbers, *ians, len , mxi, mni, ans_size, minlength; int i; double *weights , *dans; static char *kwlist[] = {"list", "weights", "minlength", NULL}; @@ -115,8 +116,9 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) kwlist, &list, &weight, &mlength)) { goto fail; } - if (!(lst = PyArray_ContiguousFromAny(list, PyArray_INTP, 1, 1))) { - goto fail; + lst = (PyArrayObject *)PyArray_ContiguousFromAny(list, NPY_INTP, 1, 1); + if (lst == NULL) { + goto fail; } len = PyArray_SIZE(lst); if (len < 1) { @@ -124,7 +126,7 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) "The first argument cannot be empty."); goto fail; } - numbers = (intp *) PyArray_DATA(lst); + numbers = (npy_intp *) PyArray_DATA(lst); mxi = mxx(numbers, len); mni = mnx(numbers, len); if (numbers[mni] < 0) { @@ -147,18 +149,21 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) ans_size = minlength; } } - type = PyArray_DescrFromType(PyArray_INTP); + type = PyArray_DescrFromType(NPY_INTP); if (weight == Py_None) { - if (!(ans = PyArray_Zeros(1, &ans_size, type, 0))) { + ans = (PyArrayObject *)PyArray_Zeros(1, &ans_size, type, 0); + if (ans == NULL) { goto fail; } - ians = (intp *)(PyArray_DATA(ans)); + ians = (npy_intp *)(PyArray_DATA(ans)); for (i = 0; i < len; i++) ians [numbers [i]] += 1; Py_DECREF(lst); } else { - if (!(wts = PyArray_ContiguousFromAny(weight, PyArray_DOUBLE, 1, 1))) { + wts = (PyArrayObject *)PyArray_ContiguousFromAny( + weight, NPY_DOUBLE, 1, 1); + if (wts == NULL) { goto fail; } weights = (double *)PyArray_DATA (wts); @@ -167,18 +172,19 @@ arr_bincount(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) "The weights and list don't have the same length."); goto fail; } - type = PyArray_DescrFromType(PyArray_DOUBLE); - if (!(ans = PyArray_Zeros(1, &ans_size, type, 0))) { + type = PyArray_DescrFromType(NPY_DOUBLE); + ans = (PyArrayObject *)PyArray_Zeros(1, &ans_size, type, 0); + if (ans == NULL) { goto fail; } - dans = (double *)PyArray_DATA (ans); + dans = (double *)PyArray_DATA(ans); for (i = 0; i < len; i++) { dans[numbers[i]] += weights[i]; } Py_DECREF(lst); Py_DECREF(wts); } - return ans; + return (PyObject *)ans; fail: Py_XDECREF(lst); @@ -200,10 +206,10 @@ arr_digitize(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) { /* self is not used */ PyObject *ox, *obins; - PyObject *ax = NULL, *abins = NULL, *aret = NULL; + PyArrayObject *ax = NULL, *abins = NULL, *aret = NULL; double *dx, *dbins; - intp lbins, lx; /* lengths */ - intp *iret; + npy_intp lbins, lx; /* lengths */ + npy_intp *iret; int m, i; static char *kwlist[] = {"x", "bins", NULL}; PyArray_Descr *type; @@ -211,12 +217,16 @@ arr_digitize(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO", kwlist, &ox, &obins)) { goto fail; } - type = PyArray_DescrFromType(PyArray_DOUBLE); - if (!(ax = PyArray_FromAny(ox, type, 1, 1, CARRAY, NULL))) { + type = PyArray_DescrFromType(NPY_DOUBLE); + ax = (PyArrayObject *)PyArray_FromAny(ox, type, + 1, 1, NPY_ARRAY_CARRAY, NULL); + if (ax == NULL) { goto fail; } Py_INCREF(type); - if (!(abins = PyArray_FromAny(obins, type, 1, 1, CARRAY, NULL))) { + abins = (PyArrayObject *)PyArray_FromAny(obins, type, + 1, 1, NPY_ARRAY_CARRAY, NULL); + if (abins == NULL) { goto fail; } @@ -224,10 +234,11 @@ arr_digitize(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) dx = (double *)PyArray_DATA(ax); lbins = PyArray_SIZE(abins); dbins = (double *)PyArray_DATA(abins); - if (!(aret = PyArray_SimpleNew(1, &lx, PyArray_INTP))) { + aret = (PyArrayObject *)PyArray_SimpleNew(1, &lx, NPY_INTP); + if (aret == NULL) { goto fail; } - iret = (intp *)PyArray_DATA(aret); + iret = (npy_intp *)PyArray_DATA(aret); if (lx <= 0 || lbins < 0) { PyErr_SetString(PyExc_ValueError, @@ -266,7 +277,7 @@ arr_digitize(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds) Py_DECREF(ax); Py_DECREF(abins); - return aret; + return (PyObject *)aret; fail: Py_XDECREF(ax); @@ -291,7 +302,7 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) int numvals, totmask, sameshape; char *input_data, *mptr, *vptr, *zero = NULL; int melsize, delsize, copied, nd; - intp *instrides, *inshape; + npy_intp *instrides, *inshape; int mindx, rem_indx, indx, i, k, objarray; static char *kwlist[] = {"input", "mask", "vals", NULL}; @@ -302,13 +313,13 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) goto fail; } - amask = (PyArrayObject *) PyArray_FROM_OF(mask, CARRAY); + amask = (PyArrayObject *)PyArray_FROM_OF(mask, NPY_ARRAY_CARRAY); if (amask == NULL) { goto fail; } /* Cast an object array */ - if (amask->descr->type_num == PyArray_OBJECT) { - tmp = (PyArrayObject *)PyArray_Cast(amask, PyArray_INTP); + if (PyArray_DESCR(amask)->type_num == NPY_OBJECT) { + tmp = (PyArrayObject *)PyArray_Cast(amask, NPY_INTP); if (tmp == NULL) { goto fail; } @@ -317,16 +328,16 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) } sameshape = 1; - if (amask->nd == ainput->nd) { - for (k = 0; k < amask->nd; k++) { - if (amask->dimensions[k] != ainput->dimensions[k]) { + if (PyArray_NDIM(amask) == PyArray_NDIM(ainput)) { + for (k = 0; k < PyArray_NDIM(amask); k++) { + if (PyArray_DIMS(amask)[k] != PyArray_DIMS(ainput)[k]) { sameshape = 0; } } } else { /* Test to see if amask is 1d */ - if (amask->nd != 1) { + if (PyArray_NDIM(amask) != 1) { sameshape = 0; } else if ((PyArray_SIZE(ainput)) != PyArray_SIZE(amask)) { @@ -339,22 +350,23 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) goto fail; } - avals = (PyArrayObject *)PyArray_FromObject(vals, ainput->descr->type_num, 0, 1); + avals = (PyArrayObject *)PyArray_FromObject(vals, + PyArray_DESCR(ainput)->type_num, 0, 1); if (avals == NULL) { goto fail; } numvals = PyArray_SIZE(avals); - nd = ainput->nd; - input_data = ainput->data; - mptr = amask->data; - melsize = amask->descr->elsize; - vptr = avals->data; - delsize = avals->descr->elsize; + nd = PyArray_NDIM(ainput); + input_data = PyArray_DATA(ainput); + mptr = PyArray_DATA(amask); + melsize = PyArray_DESCR(amask)->elsize; + vptr = PyArray_DATA(avals); + delsize = PyArray_DESCR(avals)->elsize; zero = PyArray_Zero(amask); if (zero == NULL) { goto fail; } - objarray = (ainput->descr->type_num == PyArray_OBJECT); + objarray = (PyArray_DESCR(ainput)->type_num == NPY_OBJECT); /* Handle zero-dimensional case separately */ if (nd == 0) { @@ -380,8 +392,8 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) */ totmask = (int) PyArray_SIZE(amask); copied = 0; - instrides = ainput->strides; - inshape = ainput->dimensions; + instrides = PyArray_STRIDES(ainput); + inshape = PyArray_DIMS(ainput); for (mindx = 0; mindx < totmask; mindx++) { if (memcmp(mptr,zero,melsize) != 0) { /* compute indx into input array */ @@ -402,7 +414,7 @@ arr_insert(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) copied += 1; /* If we move past value data. Reset */ if (copied >= numvals) { - vptr = avals->data; + vptr = PyArray_DATA(avals); } } mptr += melsize; @@ -476,31 +488,32 @@ arr_interp(PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict) return NULL; } - afp = (NPY_AO*)PyArray_ContiguousFromAny(fp, NPY_DOUBLE, 1, 1); + afp = (PyArrayObject *)PyArray_ContiguousFromAny(fp, NPY_DOUBLE, 1, 1); if (afp == NULL) { return NULL; } - axp = (NPY_AO*)PyArray_ContiguousFromAny(xp, NPY_DOUBLE, 1, 1); + axp = (PyArrayObject *)PyArray_ContiguousFromAny(xp, NPY_DOUBLE, 1, 1); if (axp == NULL) { goto fail; } - ax = (NPY_AO*)PyArray_ContiguousFromAny(x, NPY_DOUBLE, 1, 0); + ax = (PyArrayObject *)PyArray_ContiguousFromAny(x, NPY_DOUBLE, 1, 0); if (ax == NULL) { goto fail; } - lenxp = axp->dimensions[0]; + lenxp = PyArray_DIMS(axp)[0]; if (lenxp == 0) { PyErr_SetString(PyExc_ValueError, "array of sample points is empty"); goto fail; } - if (afp->dimensions[0] != lenxp) { + if (PyArray_DIMS(afp)[0] != lenxp) { PyErr_SetString(PyExc_ValueError, "fp and xp are not of the same length."); goto fail; } - af = (NPY_AO*)PyArray_SimpleNew(ax->nd, ax->dimensions, NPY_DOUBLE); + af = (PyArrayObject *)PyArray_SimpleNew(PyArray_NDIM(ax), + PyArray_DIMS(ax), NPY_DOUBLE); if (af == NULL) { goto fail; } @@ -1038,7 +1051,10 @@ arr_unravel_index(PyObject *self, PyObject *args, PyObject *kwds) goto fail; } Py_INCREF(ret_arr); - view->base = (PyObject *)ret_arr; + if (PyArray_SetBaseObject(view, (PyObject *)ret_arr) < 0) { + Py_DECREF(view); + goto fail; + } PyTuple_SET_ITEM(ret_tuple, i, PyArray_Return(view)); } @@ -1246,8 +1262,8 @@ static PyObject * pack_or_unpack_bits(PyObject *input, int axis, int unpack) { PyArrayObject *inp; - PyObject *new = NULL; - PyObject *out = NULL; + PyArrayObject *new = NULL; + PyArrayObject *out = NULL; npy_intp outdims[MAX_DIMS]; int i; void (*thefunc)(void *, int, npy_intp, npy_intp, void *, npy_intp, npy_intp); @@ -1271,25 +1287,25 @@ pack_or_unpack_bits(PyObject *input, int axis, int unpack) goto fail; } - new = PyArray_CheckAxis(inp, &axis, 0); + new = (PyArrayObject *)PyArray_CheckAxis(inp, &axis, 0); Py_DECREF(inp); if (new == NULL) { return NULL; } /* Handle zero-dim array separately */ if (PyArray_SIZE(new) == 0) { - return PyArray_Copy((PyArrayObject *)new); + return PyArray_Copy(new); } if (PyArray_NDIM(new) == 0) { if (unpack) { /* Handle 0-d array by converting it to a 1-d array */ - PyObject *temp; + PyArrayObject *temp; PyArray_Dims newdim = {NULL, 1}; npy_intp shape = 1; newdim.ptr = &shape; - temp = PyArray_Newshape((PyArrayObject *)new, &newdim, NPY_CORDER); + temp = (PyArrayObject *)PyArray_Newshape(new, &newdim, NPY_CORDER); if (temp == NULL) { goto fail; } @@ -1297,8 +1313,8 @@ pack_or_unpack_bits(PyObject *input, int axis, int unpack) new = temp; } else { - ubyte *optr, *iptr; - out = PyArray_New(new->ob_type, 0, NULL, NPY_UBYTE, + char *optr, *iptr; + out = (PyArrayObject *)PyArray_New(new->ob_type, 0, NULL, NPY_UBYTE, NULL, NULL, 0, 0, NULL); if (out == NULL) { goto fail; @@ -1338,8 +1354,9 @@ pack_or_unpack_bits(PyObject *input, int axis, int unpack) } /* Create output array */ - out = PyArray_New(new->ob_type, PyArray_NDIM(new), outdims, PyArray_UBYTE, - NULL, NULL, 0, PyArray_ISFORTRAN(new), NULL); + out = (PyArrayObject *)PyArray_New(new->ob_type, + PyArray_NDIM(new), outdims, NPY_UBYTE, + NULL, NULL, 0, PyArray_ISFORTRAN(new), NULL); if (out == NULL) { goto fail; } @@ -1365,7 +1382,7 @@ pack_or_unpack_bits(PyObject *input, int axis, int unpack) finish: Py_DECREF(new); - return out; + return (PyObject *)out; fail: Py_XDECREF(new); diff --git a/numpy/lib/tests/test_index_tricks.py b/numpy/lib/tests/test_index_tricks.py index f0190937b..e4c0bde93 100644 --- a/numpy/lib/tests/test_index_tricks.py +++ b/numpy/lib/tests/test_index_tricks.py @@ -48,7 +48,7 @@ class TestRavelUnravelIndex(TestCase): uncoords = coords[0]+5*coords[1] assert_equal(np.ravel_multi_index(coords, shape, order='F'), uncoords) assert_equal(coords, np.unravel_index(uncoords, shape, order='F')) - + coords = np.array([[1,0,1,2,3,4],[1,6,1,3,2,0],[1,3,1,0,9,5]], dtype=dtype) shape = (5,8,10) diff --git a/numpy/lib/tests/test_recfunctions.py b/numpy/lib/tests/test_recfunctions.py index c6befa5f6..0a3f1e3e3 100644 --- a/numpy/lib/tests/test_recfunctions.py +++ b/numpy/lib/tests/test_recfunctions.py @@ -671,9 +671,10 @@ class TestJoinBy2(TestCase): (10, 2, 54, 69, 14, 4), (11, 2, 55, 70, 15, 5), (10, 3, 56, 71, 16, 6), (11, 3, 57, 72, 17, 7), (10, 4, 58, 73, 18, 8), (11, 4, 59, 74, 19, 9)], - dtype=[('k', '<i8'), ('a', '<i8'), ('b1', '<i8'), - ('b2', '<i8'), ('c1', '<i8'), ('c2', '<i8')]) + dtype=[('k', int), ('a', int), ('b1', int), + ('b2', int), ('c1', int), ('c2', int)]) test = join_by(['a','k'], a, b, r1postfix='1', r2postfix='2', jointype='inner') + assert_equal(test.dtype, control.dtype) assert_equal(test, control) diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index d7cf114f7..814f2d614 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -11,7 +11,7 @@ def assert_shapes_correct(input_shapes, expected_shape): outarrays = broadcast_arrays(*inarrays) outshapes = [a.shape for a in outarrays] expected = [expected_shape] * len(inarrays) - assert_(outshapes == expected) + assert_equal(outshapes, expected) def assert_incompatible_shapes_raise(input_shapes): """ Broadcast a list of arrays with the given (incompatible) input shapes @@ -76,13 +76,13 @@ def test_same_input_shapes(): for shape in data: input_shapes = [shape] # Single input. - yield assert_shapes_correct, input_shapes, shape + assert_shapes_correct(input_shapes, shape) # Double input. input_shapes2 = [shape, shape] - yield assert_shapes_correct, input_shapes2, shape + assert_shapes_correct(input_shapes2, shape) # Triple input. input_shapes3 = [shape, shape, shape] - yield assert_shapes_correct, input_shapes3, shape + assert_shapes_correct(input_shapes3, shape) def test_two_compatible_by_ones_input_shapes(): """ Check that two different input shapes (of the same length but some have @@ -104,9 +104,9 @@ def test_two_compatible_by_ones_input_shapes(): [[(1,1), (0,1)], (0,1)], ] for input_shapes, expected_shape in data: - yield assert_shapes_correct, input_shapes, expected_shape + assert_shapes_correct(input_shapes, expected_shape) # Reverse the input shapes since broadcasting should be symmetric. - yield assert_shapes_correct, input_shapes[::-1], expected_shape + assert_shapes_correct(input_shapes[::-1], expected_shape) def test_two_compatible_by_prepending_ones_input_shapes(): """ Check that two different input shapes (of different lengths) broadcast @@ -135,9 +135,9 @@ def test_two_compatible_by_prepending_ones_input_shapes(): [[(), (0,1)], (0,1)], ] for input_shapes, expected_shape in data: - yield assert_shapes_correct, input_shapes, expected_shape + assert_shapes_correct(input_shapes, expected_shape) # Reverse the input shapes since broadcasting should be symmetric. - yield assert_shapes_correct, input_shapes[::-1], expected_shape + assert_shapes_correct(input_shapes[::-1], expected_shape) def test_incompatible_shapes_raise_valueerror(): """ Check that a ValueError is raised for incompatible shapes. @@ -149,9 +149,9 @@ def test_incompatible_shapes_raise_valueerror(): [(1,3,4), (2,3,3)], ] for input_shapes in data: - yield assert_incompatible_shapes_raise, input_shapes + assert_incompatible_shapes_raise(input_shapes) # Reverse the input shapes since broadcasting should be symmetric. - yield assert_incompatible_shapes_raise, input_shapes[::-1] + assert_incompatible_shapes_raise(input_shapes[::-1]) def test_same_as_ufunc(): """ Check that the data layout is the same as if a ufunc did the operation. @@ -192,16 +192,17 @@ def test_same_as_ufunc(): [[(), (0,1)], (0,1)], ] for input_shapes, expected_shape in data: - yield assert_same_as_ufunc, input_shapes[0], input_shapes[1] + assert_same_as_ufunc(input_shapes[0], input_shapes[1], + "Shapes: %s %s" % (input_shapes[0], input_shapes[1])) # Reverse the input shapes since broadcasting should be symmetric. - yield assert_same_as_ufunc, input_shapes[1], input_shapes[0] + assert_same_as_ufunc(input_shapes[1], input_shapes[0]) # Try them transposed, too. - yield assert_same_as_ufunc, input_shapes[0], input_shapes[1], True + assert_same_as_ufunc(input_shapes[0], input_shapes[1], True) # ... and flipped for non-rank-0 inputs in order to test negative # strides. if () not in input_shapes: - yield assert_same_as_ufunc, input_shapes[0], input_shapes[1], False, True - yield assert_same_as_ufunc, input_shapes[0], input_shapes[1], True, True + assert_same_as_ufunc(input_shapes[0], input_shapes[1], False, True) + assert_same_as_ufunc(input_shapes[0], input_shapes[1], True, True) if __name__ == "__main__": diff --git a/numpy/numarray/_capi.c b/numpy/numarray/_capi.c index 446594d99..deb2cd478 100644 --- a/numpy/numarray/_capi.c +++ b/numpy/numarray/_capi.c @@ -1,5 +1,6 @@ #include <Python.h> +#define NPY_NO_DEPRECATED_API #define _libnumarray_MODULE #include "include/numpy/libnumarray.h" #include "numpy/npy_3kcompat.h" @@ -347,9 +348,9 @@ NA_NewAll(int ndim, maybelong *shape, NumarrayType type, result = NULL; } else { if (buffer) { - memcpy(result->data, buffer, NA_NBYTES(result)); + memcpy(PyArray_DATA(result), buffer, NA_NBYTES(result)); } else { - memset(result->data, 0, NA_NBYTES(result)); + memset(PyArray_DATA(result), 0, NA_NBYTES(result)); } } } @@ -366,7 +367,7 @@ NA_NewAllStrides(int ndim, maybelong *shape, maybelong *strides, byteoffset, 0, byteorder, aligned, writeable); for(i=0; i<ndim; i++) - result->strides[i] = strides[i]; + PyArray_STRIDES(result)[i] = strides[i]; return result; } @@ -703,16 +704,16 @@ _NA_callStridingHelper(PyObject *aux, long dim, { int i, j, status=0; dim -= 1; - for(i=0; i<numarray[0]->dimensions[dim]; i++) { + for(i=0; i<PyArray_DIMS(numarray[0])[dim]; i++) { for (j=0; j<nnumarray; j++) - data[j] += numarray[j]->strides[dim]*i; + data[j] += PyArray_STRIDES(numarray[j])[dim]*i; if (dim == 0) status |= f(aux, nnumarray, numarray, data); else status |= _NA_callStridingHelper( aux, dim, nnumarray, numarray, data, f); for (j=0; j<nnumarray; j++) - data[j] -= numarray[j]->strides[dim]*i; + data[j] -= PyArray_STRIDES(numarray[j])[dim]*i; } return status; } @@ -746,7 +747,7 @@ callStridingCFunc(PyObject *self, PyObject *args) { "%s arg[%d] is not an array.", me->descr.name, i); numarray[i] = (PyArrayObject *) otemp; - data[i] = numarray[i]->data; + data[i] = PyArray_DATA(numarray[i]); Py_DECREF(otemp); if (!NA_updateDataPtr(numarray[i])) return NULL; @@ -755,7 +756,7 @@ callStridingCFunc(PyObject *self, PyObject *args) { /* Cast function pointer and perform stride operation */ f = (CFUNC_STRIDED_FUNC) me->descr.fptr; - if (_NA_callStridingHelper(aux, numarray[0]->nd, + if (_NA_callStridingHelper(aux, PyArray_NDIM(numarray[0]), nnumarray, numarray, data, f)) { return NULL; } else { @@ -1052,7 +1053,7 @@ NA_InputArray(PyObject *a, NumarrayType t, int requires) static int satisfies(PyArrayObject *a, int requirements, NumarrayType t) { - int type_ok = (a->descr->type_num == t) || (t == tAny); + int type_ok = (PyArray_DESCR(a)->type_num == t) || (t == tAny); if (PyArray_ISCARRAY(a)) return type_ok; @@ -1076,7 +1077,7 @@ NA_OutputArray(PyObject *a, NumarrayType t, int requires) PyArray_Descr *dtype; PyArrayObject *ret; - if (!PyArray_Check(a) || !PyArray_ISWRITEABLE(a)) { + if (!PyArray_Check(a) || !PyArray_ISWRITEABLE((PyArrayObject *)a)) { PyErr_Format(PyExc_TypeError, "NA_OutputArray: only writeable arrays work for output."); return NULL; @@ -1087,18 +1088,22 @@ NA_OutputArray(PyObject *a, NumarrayType t, int requires) return (PyArrayObject *)a; } if (t == tAny) { - dtype = PyArray_DESCR(a); + dtype = PyArray_DESCR((PyArrayObject *)a); Py_INCREF(dtype); } else { dtype = PyArray_DescrFromType(t); } - ret = (PyArrayObject *)PyArray_Empty(PyArray_NDIM(a), PyArray_DIMS(a), - dtype, 0); - ret->flags |= NPY_UPDATEIFCOPY; - ret->base = a; - PyArray_FLAGS(a) &= ~NPY_WRITEABLE; + ret = (PyArrayObject *)PyArray_Empty(PyArray_NDIM((PyArrayObject *)a), + PyArray_DIMS((PyArrayObject *)a), + dtype, 0); Py_INCREF(a); + if (PyArray_SetBaseObject(ret, a) < 0) { + Py_DECREF(ret); + return NULL; + } + PyArray_ENABLEFLAGS(ret, NPY_ARRAY_UPDATEIFCOPY); + PyArray_CLEARFLAGS((PyArrayObject *)a, NPY_ARRAY_WRITEABLE); return ret; } @@ -1114,7 +1119,8 @@ NA_OutputArray(PyObject *a, NumarrayType t, int requires) static PyArrayObject * NA_IoArray(PyObject *a, NumarrayType t, int requires) { - PyArrayObject *shadow = NA_InputArray(a, t, requires | NPY_UPDATEIFCOPY ); + PyArrayObject *shadow = NA_InputArray(a, t, + requires | NPY_ARRAY_UPDATEIFCOPY ); if (!shadow) return NULL; @@ -1159,7 +1165,7 @@ Complex64 NA_get_Complex64(PyArrayObject *a, long offset) Complex32 v0; Complex64 v; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tComplex32: v0 = NA_GETP(a, Complex32, (NA_PTR(a)+offset)); v.r = v0.r; @@ -1180,7 +1186,7 @@ void NA_set_Complex64(PyArrayObject *a, long offset, Complex64 v) { Complex32 v0; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tComplex32: v0.r = v.r; v0.i = v.i; @@ -1197,7 +1203,7 @@ void NA_set_Complex64(PyArrayObject *a, long offset, Complex64 v) Int64 NA_get_Int64(PyArrayObject *a, long offset) { - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: return NA_GETP(a, Bool, (NA_PTR(a)+offset)) != 0; case tInt8: @@ -1227,7 +1233,7 @@ Int64 NA_get_Int64(PyArrayObject *a, long offset) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_get_Int64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); } return 0; /* suppress warning */ @@ -1237,7 +1243,7 @@ void NA_set_Int64(PyArrayObject *a, long offset, Int64 v) { Bool b; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: b = (v != 0); NA_SETP(a, Bool, (NA_PTR(a)+offset), b); @@ -1275,7 +1281,7 @@ void NA_set_Int64(PyArrayObject *a, long offset, Int64 v) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_set_Int64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); } } @@ -1293,11 +1299,11 @@ long NA_get_offset(PyArrayObject *a, int N, ...) va_start(ap, N); if (N > 0) { /* compute offset of "outer" indices. */ for(i=0; i<N; i++) - offset += va_arg(ap, long) * a->strides[i]; + offset += va_arg(ap, long) * PyArray_STRIDES(a)[i]; } else { /* compute offset of "inner" indices. */ N = -N; for(i=0; i<N; i++) - offset += va_arg(ap, long) * a->strides[a->nd-N+i]; + offset += va_arg(ap, long) * PyArray_STRIDES(a)[PyArray_NDIM(a)-N+i]; } va_end(ap); return offset; @@ -1305,7 +1311,7 @@ long NA_get_offset(PyArrayObject *a, int N, ...) Float64 NA_get_Float64(PyArrayObject *a, long offset) { - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: return NA_GETP(a, Bool, (NA_PTR(a)+offset)) != 0; case tInt8: @@ -1337,7 +1343,7 @@ Float64 NA_get_Float64(PyArrayObject *a, long offset) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_get_Float64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); } return 0; /* suppress warning */ } @@ -1346,7 +1352,7 @@ void NA_set_Float64(PyArrayObject *a, long offset, Float64 v) { Bool b; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: b = (v != 0); NA_SETP(a, Bool, (NA_PTR(a)+offset), b); @@ -1388,7 +1394,7 @@ void NA_set_Float64(PyArrayObject *a, long offset, Float64 v) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_set_Float64", - a->descr->type_num ); + PyArray_DESCR(a)->type_num ); PyErr_Print(); } } @@ -1396,127 +1402,127 @@ void NA_set_Float64(PyArrayObject *a, long offset, Float64 v) Float64 NA_get1_Float64(PyArrayObject *a, long i) { - long offset = i * a->strides[0]; + long offset = i * PyArray_STRIDES(a)[0]; return NA_get_Float64(a, offset); } Float64 NA_get2_Float64(PyArrayObject *a, long i, long j) { - long offset = i * a->strides[0] - + j * a->strides[1]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1]; return NA_get_Float64(a, offset); } Float64 NA_get3_Float64(PyArrayObject *a, long i, long j, long k) { - long offset = i * a->strides[0] - + j * a->strides[1] - + k * a->strides[2]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1] + + k * PyArray_STRIDES(a)[2]; return NA_get_Float64(a, offset); } void NA_set1_Float64(PyArrayObject *a, long i, Float64 v) { - long offset = i * a->strides[0]; + long offset = i * PyArray_STRIDES(a)[0]; NA_set_Float64(a, offset, v); } void NA_set2_Float64(PyArrayObject *a, long i, long j, Float64 v) { - long offset = i * a->strides[0] - + j * a->strides[1]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1]; NA_set_Float64(a, offset, v); } void NA_set3_Float64(PyArrayObject *a, long i, long j, long k, Float64 v) { - long offset = i * a->strides[0] - + j * a->strides[1] - + k * a->strides[2]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1] + + k * PyArray_STRIDES(a)[2]; NA_set_Float64(a, offset, v); } Complex64 NA_get1_Complex64(PyArrayObject *a, long i) { - long offset = i * a->strides[0]; + long offset = i * PyArray_STRIDES(a)[0]; return NA_get_Complex64(a, offset); } Complex64 NA_get2_Complex64(PyArrayObject *a, long i, long j) { - long offset = i * a->strides[0] - + j * a->strides[1]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1]; return NA_get_Complex64(a, offset); } Complex64 NA_get3_Complex64(PyArrayObject *a, long i, long j, long k) { - long offset = i * a->strides[0] - + j * a->strides[1] - + k * a->strides[2]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1] + + k * PyArray_STRIDES(a)[2]; return NA_get_Complex64(a, offset); } void NA_set1_Complex64(PyArrayObject *a, long i, Complex64 v) { - long offset = i * a->strides[0]; + long offset = i * PyArray_STRIDES(a)[0]; NA_set_Complex64(a, offset, v); } void NA_set2_Complex64(PyArrayObject *a, long i, long j, Complex64 v) { - long offset = i * a->strides[0] - + j * a->strides[1]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1]; NA_set_Complex64(a, offset, v); } void NA_set3_Complex64(PyArrayObject *a, long i, long j, long k, Complex64 v) { - long offset = i * a->strides[0] - + j * a->strides[1] - + k * a->strides[2]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1] + + k * PyArray_STRIDES(a)[2]; NA_set_Complex64(a, offset, v); } Int64 NA_get1_Int64(PyArrayObject *a, long i) { - long offset = i * a->strides[0]; + long offset = i * PyArray_STRIDES(a)[0]; return NA_get_Int64(a, offset); } Int64 NA_get2_Int64(PyArrayObject *a, long i, long j) { - long offset = i * a->strides[0] - + j * a->strides[1]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1]; return NA_get_Int64(a, offset); } Int64 NA_get3_Int64(PyArrayObject *a, long i, long j, long k) { - long offset = i * a->strides[0] - + j * a->strides[1] - + k * a->strides[2]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1] + + k * PyArray_STRIDES(a)[2]; return NA_get_Int64(a, offset); } void NA_set1_Int64(PyArrayObject *a, long i, Int64 v) { - long offset = i * a->strides[0]; + long offset = i * PyArray_STRIDES(a)[0]; NA_set_Int64(a, offset, v); } void NA_set2_Int64(PyArrayObject *a, long i, long j, Int64 v) { - long offset = i * a->strides[0] - + j * a->strides[1]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1]; NA_set_Int64(a, offset, v); } void NA_set3_Int64(PyArrayObject *a, long i, long j, long k, Int64 v) { - long offset = i * a->strides[0] - + j * a->strides[1] - + k * a->strides[2]; + long offset = i * PyArray_STRIDES(a)[0] + + j * PyArray_STRIDES(a)[1] + + k * PyArray_STRIDES(a)[2]; NA_set_Int64(a, offset, v); } @@ -1525,7 +1531,7 @@ void NA_set3_Int64(PyArrayObject *a, long i, long j, long k, Int64 v) #define NA_SET_CMPLX(a, type, base, cnt, in) \ { \ int i; \ - int stride = a->strides[ a->nd - 1]; \ + int stride = PyArray_STRIDES(a)[ PyArray_NDIM(a) - 1]; \ NA_SET1D(a, type, base, cnt, in); \ base = NA_PTR(a) + offset + sizeof(type); \ for(i=0; i<cnt; i++) { \ @@ -1539,7 +1545,7 @@ NA_get1D_Float64(PyArrayObject *a, long offset, int cnt, Float64*out) { char *base = NA_PTR(a) + offset; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: NA_GET1D(a, Bool, base, cnt, out); break; @@ -1584,7 +1590,7 @@ NA_get1D_Float64(PyArrayObject *a, long offset, int cnt, Float64*out) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_get1D_Float64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); return -1; } @@ -1608,7 +1614,7 @@ NA_set1D_Float64(PyArrayObject *a, long offset, int cnt, Float64*in) { char *base = NA_PTR(a) + offset; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: NA_SET1D(a, Bool, base, cnt, in); break; @@ -1653,7 +1659,7 @@ NA_set1D_Float64(PyArrayObject *a, long offset, int cnt, Float64*in) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_set1D_Float64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); return -1; } @@ -1665,7 +1671,7 @@ NA_get1D_Int64(PyArrayObject *a, long offset, int cnt, Int64*out) { char *base = NA_PTR(a) + offset; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: NA_GET1D(a, Bool, base, cnt, out); break; @@ -1708,7 +1714,7 @@ NA_get1D_Int64(PyArrayObject *a, long offset, int cnt, Int64*out) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_get1D_Int64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); return -1; } @@ -1732,7 +1738,7 @@ NA_set1D_Int64(PyArrayObject *a, long offset, int cnt, Int64*in) { char *base = NA_PTR(a) + offset; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: NA_SET1D(a, Bool, base, cnt, in); break; @@ -1775,7 +1781,7 @@ NA_set1D_Int64(PyArrayObject *a, long offset, int cnt, Int64*in) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_set1D_Int64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); return -1; } @@ -1787,14 +1793,14 @@ NA_get1D_Complex64(PyArrayObject *a, long offset, int cnt, Complex64*out) { char *base = NA_PTR(a) + offset; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tComplex64: NA_GET1D(a, Complex64, base, cnt, out); break; default: PyErr_Format( PyExc_TypeError, "Unsupported type %d in NA_get1D_Complex64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); return -1; } @@ -1806,14 +1812,14 @@ NA_set1D_Complex64(PyArrayObject *a, long offset, int cnt, Complex64*in) { char *base = NA_PTR(a) + offset; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tComplex64: NA_SET1D(a, Complex64, base, cnt, in); break; default: PyErr_Format( PyExc_TypeError, "Unsupported type %d in NA_set1D_Complex64", - a->descr->type_num); + PyArray_DESCR(a)->type_num); PyErr_Print(); return -1; } @@ -1835,10 +1841,10 @@ NA_ShapeEqual(PyArrayObject *a, PyArrayObject *b) "NA_ShapeEqual: non-array as parameter."); return -1; } - if (a->nd != b->nd) + if (PyArray_NDIM(a) != PyArray_NDIM(b)) return 0; - for(i=0; i<a->nd; i++) - if (a->dimensions[i] != b->dimensions[i]) + for(i=0; i<PyArray_NDIM(a); i++) + if (PyArray_DIMS(a)[i] != PyArray_DIMS(b)[i]) return 0; return 1; } @@ -1858,11 +1864,11 @@ NA_ShapeLessThan(PyArrayObject *a, PyArrayObject *b) "NA_ShapeLessThan: non-array as parameter."); return -1; } - mindim = MIN(a->nd, b->nd); - aoff = a->nd - mindim; - boff = b->nd - mindim; + mindim = MIN(PyArray_NDIM(a), PyArray_NDIM(b)); + aoff = PyArray_NDIM(a) - mindim; + boff = PyArray_NDIM(b) - mindim; for(i=0; i<mindim; i++) - if (a->dimensions[i+aoff] >= b->dimensions[i+boff]) + if (PyArray_DIMS(a)[i+aoff] >= PyArray_DIMS(b)[i+boff]) return 0; return 1; } @@ -2059,7 +2065,7 @@ getShape(PyObject *a, maybelong *shape, int dims) } if (!PySequence_Check(a) || - (NA_NDArrayCheck(a) && (PyArray(a)->nd == 0))) + (NA_NDArrayCheck(a) && (PyArray_NDIM(PyArray(a)) == 0))) return dims; slen = PySequence_Length(a); if (slen < 0) { @@ -2103,13 +2109,13 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, long offset) SequenceConstraint mustbe = NOTHING; int i, seqlen=-1, slen = PySequence_Length(s); - if (dim > a->nd) { + if (dim > PyArray_NDIM(a)) { PyErr_Format(PyExc_ValueError, "setArrayFromSequence: sequence/array dimensions mismatch."); return -1; } - if (slen != a->dimensions[dim]) { + if (slen != PyArray_DIMS(a)[dim]) { PyErr_Format(PyExc_ValueError, "setArrayFromSequence: sequence/array shape mismatch."); return -1; @@ -2122,7 +2128,7 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, long offset) "setArrayFromSequence: Can't get a sequence item"); return -1; } else if ((NA_isPythonScalar(o) || - (NA_NumArrayCheck(o) && PyArray(o)->nd == 0)) && + (NA_NumArrayCheck(o) && PyArray_NDIM(PyArray(o)) == 0)) && ((mustbe == NOTHING) || (mustbe == NUMBER))) { if (NA_setFromPythonScalar(a, offset, o) < 0) return -2; @@ -2154,7 +2160,7 @@ setArrayFromSequence(PyArrayObject *a, PyObject *s, int dim, long offset) return -6; } Py_DECREF(o); - offset += a->strides[dim]; + offset += PyArray_STRIDES(a)[dim]; } return 0; } @@ -2199,7 +2205,7 @@ _NA_maxType(PyObject *seq, int limit) return -1; } if (NA_NumArrayCheck(seq)) { - switch(PyArray(seq)->descr->type_num) { + switch(PyArray_DESCR(PyArray(seq))->type_num) { case tBool: return BOOL_SCALAR; case tInt8: @@ -2302,7 +2308,7 @@ NA_isPythonScalar(PyObject *o) static PyObject * NA_getPythonScalar(PyArrayObject *a, long offset) { - int type = a->descr->type_num; + int type = PyArray_DESCR(a)->type_num; PyObject *rval = NULL; switch(type) { @@ -2355,9 +2361,9 @@ NA_getPythonScalar(PyArrayObject *a, long offset) static int NA_overflow(PyArrayObject *a, Float64 v) { - if ((a->flags & CHECKOVERFLOW) == 0) return 0; + if ((PyArray_FLAGS(a) & CHECKOVERFLOW) == 0) return 0; - switch(a->descr->type_num) { + switch(PyArray_DESCR(a)->type_num) { case tBool: return 0; case tInt8: @@ -2402,7 +2408,7 @@ NA_overflow(PyArrayObject *a, Float64 v) default: PyErr_Format( PyExc_TypeError, "Unknown type %d in NA_overflow", - a->descr->type_num ); + PyArray_DESCR(a)->type_num ); PyErr_Print(); return -1; } @@ -2425,11 +2431,11 @@ _setFromPythonScalarCore(PyArrayObject *a, long offset, PyObject*value, int entr return -1; NA_set_Int64(a, offset, v); } else if (PyLong_Check(value)) { - if (a->descr->type_num == tInt64) { + if (PyArray_DESCR(a)->type_num == tInt64) { v = (Int64) PyLong_AsLongLong( value ); - } else if (a->descr->type_num == tUInt64) { + } else if (PyArray_DESCR(a)->type_num == tUInt64) { v = (UInt64) PyLong_AsUnsignedLongLong( value ); - } else if (a->descr->type_num == tUInt32) { + } else if (PyArray_DESCR(a)->type_num == tUInt32) { v = PyLong_AsUnsignedLong(value); } else { v = PyLong_AsLongLong(value); @@ -2455,7 +2461,7 @@ _setFromPythonScalarCore(PyArrayObject *a, long offset, PyObject*value, int entr NA_set_Complex64(a, offset, vc); } else if (PyObject_HasAttrString(value, "__tonumtype__")) { int rval; - PyObject *type = NA_typeNoToTypeObject(a->descr->type_num); + PyObject *type = NA_typeNoToTypeObject(PyArray_DESCR(a)->type_num); if (!type) return -1; value = PyObject_CallMethod( value, "__tonumtype__", "(N)", type); @@ -2482,7 +2488,7 @@ _setFromPythonScalarCore(PyArrayObject *a, long offset, PyObject*value, int entr static int NA_setFromPythonScalar(PyArrayObject *a, long offset, PyObject *value) { - if (a->flags & WRITABLE) + if (PyArray_FLAGS(a) & NPY_ARRAY_WRITEABLE) return _setFromPythonScalarCore(a, offset, value, 0); else { PyErr_Format( @@ -2508,7 +2514,7 @@ NA_ComplexArrayCheck(PyObject *a) int rval = NA_NumArrayCheck(a); if (rval > 0) { PyArrayObject *arr = (PyArrayObject *) a; - switch(arr->descr->type_num) { + switch(PyArray_DESCR(arr)->type_num) { case tComplex64: case tComplex32: return 1; default: @@ -2523,8 +2529,8 @@ NA_elements(PyArrayObject *a) { int i; unsigned long n = 1; - for(i = 0; i<a->nd; i++) - n *= a->dimensions[i]; + for(i = 0; i<PyArray_NDIM(a); i++) + n *= PyArray_DIMS(a)[i]; return n; } @@ -2725,27 +2731,27 @@ NA_swapAxes(PyArrayObject *array, int x, int y) if (((PyObject *) array) == Py_None) return 0; - if (array->nd < 2) return 0; + if (PyArray_NDIM(array) < 2) return 0; - if (x < 0) x += array->nd; - if (y < 0) y += array->nd; + if (x < 0) x += PyArray_NDIM(array); + if (y < 0) y += PyArray_NDIM(array); - if ((x < 0) || (x >= array->nd) || - (y < 0) || (y >= array->nd)) { + if ((x < 0) || (x >= PyArray_NDIM(array)) || + (y < 0) || (y >= PyArray_NDIM(array))) { PyErr_Format(PyExc_ValueError, "Specified dimension does not exist"); return -1; } - temp = array->dimensions[x]; - array->dimensions[x] = array->dimensions[y]; - array->dimensions[y] = temp; + temp = PyArray_DIMS(array)[x]; + PyArray_DIMS(array)[x] = PyArray_DIMS(array)[y]; + PyArray_DIMS(array)[y] = temp; - temp = array->strides[x]; - array->strides[x] = array->strides[y]; - array->strides[y] = temp; + temp = PyArray_STRIDES(array)[x]; + PyArray_STRIDES(array)[x] = PyArray_STRIDES(array)[y]; + PyArray_STRIDES(array)[y] = temp; - PyArray_UpdateFlags(array, NPY_UPDATE_ALL); + PyArray_UpdateFlags(array, NPY_ARRAY_UPDATE_ALL); return 0; } @@ -2861,20 +2867,20 @@ NA_NewAllFromBuffer(int ndim, maybelong *shape, NumarrayType type, static void NA_updateAlignment(PyArrayObject *self) { - PyArray_UpdateFlags(self, NPY_ALIGNED); + PyArray_UpdateFlags(self, NPY_ARRAY_ALIGNED); } static void NA_updateContiguous(PyArrayObject *self) { - PyArray_UpdateFlags(self, NPY_CONTIGUOUS | NPY_FORTRAN); + PyArray_UpdateFlags(self, NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_F_CONTIGUOUS); } static void NA_updateStatus(PyArrayObject *self) { - PyArray_UpdateFlags(self, NPY_UPDATE_ALL); + PyArray_UpdateFlags(self, NPY_ARRAY_UPDATE_ALL); } static int @@ -2915,7 +2921,7 @@ NA_getArrayData(PyArrayObject *obj) PyErr_Format(PyExc_TypeError, "expected an NDArray"); } - return obj->data; + return PyArray_DATA(obj); } /* Byteswap is not a flag of the array --- it is implicit in the data-type */ diff --git a/numpy/numarray/include/numpy/arraybase.h b/numpy/numarray/include/numpy/arraybase.h index a964979ce..32f9948f4 100644 --- a/numpy/numarray/include/numpy/arraybase.h +++ b/numpy/numarray/include/numpy/arraybase.h @@ -12,7 +12,7 @@ typedef npy_uint8 UInt8; typedef npy_int16 Int16; typedef npy_uint16 UInt16; typedef npy_int32 Int32; -typedef npy_uint32 UInt32; +typedef npy_uint32 UInt32; typedef npy_int64 Int64; typedef npy_uint64 UInt64; typedef npy_float32 Float32; @@ -65,7 +65,7 @@ typedef struct { Float64 r, i; } Complex64; #define PyArray(m) ((PyArrayObject *)(m)) #define PyArray_ISFORTRAN_CONTIGUOUS(m) (((PyArray(m))->flags & FORTRAN_CONTIGUOUS) != 0) -#define PyArray_ISWRITABLE PyArray_ISWRITEABLE +#define PyArray_ISWRITABLE PyArray_ISWRITEABLE -#endif +#endif diff --git a/numpy/numarray/include/numpy/cfunc.h b/numpy/numarray/include/numpy/cfunc.h index b581be08f..1739290ae 100644 --- a/numpy/numarray/include/numpy/cfunc.h +++ b/numpy/numarray/include/numpy/cfunc.h @@ -6,11 +6,11 @@ typedef int (*UFUNC)(long, long, long, void **, long*); /* typedef void (*CFUNC_2ARG)(long, void *, void *); */ /* typedef void (*CFUNC_3ARG)(long, void *, void *, void *); */ typedef int (*CFUNCfromPyValue)(PyObject *, void *); -typedef int (*CFUNC_STRIDE_CONV_FUNC)(long, long, maybelong *, +typedef int (*CFUNC_STRIDE_CONV_FUNC)(long, long, maybelong *, void *, long, maybelong*, void *, long, maybelong *); -typedef int (*CFUNC_STRIDED_FUNC)(PyObject *, long, PyArrayObject **, - char **data); +typedef int (*CFUNC_STRIDED_FUNC)(PyObject *, long, PyArrayObject **, + char **data); #define MAXARRAYS 16 diff --git a/numpy/numarray/include/numpy/numcomplex.h b/numpy/numarray/include/numpy/numcomplex.h index 9ed4198c7..7b4960e40 100644 --- a/numpy/numarray/include/numpy/numcomplex.h +++ b/numpy/numarray/include/numpy/numcomplex.h @@ -68,7 +68,7 @@ typedef struct { Float64 a, theta; } PolarComplex64; #define NUM_CLE(p, q) ((p).r <= (q).r) #define NUM_CGE(p, q) ((p).r >= (q).r) -/* e**z = e**x * (cos(y)+ i*sin(y)) where z = x + i*y +/* e**z = e**x * (cos(y)+ i*sin(y)) where z = x + i*y so e**z = e**x * cos(y) + i * e**x * sin(y) */ #define NUM_CEXP(p, s) \ @@ -79,7 +79,7 @@ typedef struct { Float64 a, theta; } PolarComplex64; /* e**w = z; w = u + i*v; z = r * e**(i*theta); -e**u * e**(i*v) = r * e**(i*theta); +e**u * e**(i*v) = r * e**(i*theta); log(z) = w; log(z) = log(r) + i*theta; */ @@ -110,7 +110,7 @@ log(z) = w; log(z) = log(r) + i*theta; NUM_CEXP(s, s); \ } \ } - + #define NUM_CSQRT(p, s) { Complex64 temp; temp.r = 0.5; temp.i=0; \ NUM_CPOW(p, temp, s); \ } @@ -201,7 +201,7 @@ log(z) = w; log(z) = log(r) + i*theta; NUM_CIMUL(s, s); \ NUM_CRMUL(s, 0.5, s); \ } - + /* asinh(z) = log( z + (z**2 + 1)**0.5 ) */ #define NUM_CASINH(p, s) { Complex64 p1; NUM_CASS(p, p1); \ NUM_CMUL(p, p, s); \ diff --git a/numpy/numarray/include/numpy/nummacro.h b/numpy/numarray/include/numpy/nummacro.h index e9acd6e31..0f87dfb84 100644 --- a/numpy/numarray/include/numpy/nummacro.h +++ b/numpy/numarray/include/numpy/nummacro.h @@ -6,7 +6,7 @@ /* The structs defined here are private implementation details of numarray which are subject to change w/o notice. */ - + #define PY_BOOL_CHAR "b" #define PY_INT8_CHAR "b" #define PY_INT16_CHAR "h" @@ -44,7 +44,7 @@ typedef enum #define UNCONVERTED 0 #define C_ARRAY (NUM_CONTIGUOUS | NUM_NOTSWAPPED | NUM_ALIGNED) -#define MUST_BE_COMPUTED 2 +#define MUST_BE_COMPUTED 2 #define NUM_FLOORDIVIDE(a,b,out) (out) = floor((a)/(b)) @@ -87,15 +87,15 @@ typedef enum /* from here down, type("ai") is NDInfo* */ #define NA_PTR(ai) ((char *) NA_OFFSETDATA((ai))) -#define NA_PTR1(ai, i) (NA_PTR(ai) + \ - (i)*(ai)->strides[0]) -#define NA_PTR2(ai, i, j) (NA_PTR(ai) + \ - (i)*(ai)->strides[0] + \ - (j)*(ai)->strides[1]) -#define NA_PTR3(ai, i, j, k) (NA_PTR(ai) + \ - (i)*(ai)->strides[0] + \ - (j)*(ai)->strides[1] + \ - (k)*(ai)->strides[2]) +#define NA_PTR1(ai, i) (NA_PTR(ai) + \ + (i)*PyArray_STRIDES(ai)[0]) +#define NA_PTR2(ai, i, j) (NA_PTR(ai) + \ + (i)*PyArray_STRIDES(ai)[0] + \ + (j)*PyArray_STRIDES(ai)[1]) +#define NA_PTR3(ai, i, j, k) (NA_PTR(ai) + \ + (i)*PyArray_STRIDES(ai)[0] + \ + (j)*PyArray_STRIDES(ai)[1] + \ + (k)*PyArray_STRIDES(ai)[2]) #define NA_SET_TEMP(ai, type, v) (((type *) &__temp__)[0] = v) @@ -238,10 +238,10 @@ _makeSetPa(Bool) /* fast (aligned, !byteswapped) */ #define NA_GETPf(ai, type, ptr) (*((type *) (ptr))) -#define NA_GETP(ai, type, ptr) \ - (PyArray_ISCARRAY(ai) ? NA_GETPf(ai, type, ptr) \ - : (PyArray_ISBYTESWAPPED(ai) ? \ - NA_GETPb(ai, type, ptr) \ +#define NA_GETP(ai, type, ptr) \ + (PyArray_ISCARRAY(ai) ? NA_GETPf(ai, type, ptr) \ + : (PyArray_ISBYTESWAPPED(ai) ? \ + NA_GETPb(ai, type, ptr) \ : NA_GETPa(ai, type, ptr))) /* NOTE: NA_SET* macros cannot be used as values. */ @@ -255,12 +255,12 @@ _makeSetPa(Bool) /* fast (aligned, !byteswapped) */ #define NA_SETPf(ai, type, ptr, v) ((*((type *) ptr)) = (v)) -#define NA_SETP(ai, type, ptr, v) \ - if (PyArray_ISCARRAY(ai)) { \ - NA_SETPf((ai), type, (ptr), (v)); \ - } else if (PyArray_ISBYTESWAPPED(ai)) { \ - NA_SETPb((ai), type, (ptr), (v)); \ - } else \ +#define NA_SETP(ai, type, ptr, v) \ + if (PyArray_ISCARRAY(ai)) { \ + NA_SETPf((ai), type, (ptr), (v)); \ + } else if (PyArray_ISBYTESWAPPED(ai)) { \ + NA_SETPb((ai), type, (ptr), (v)); \ + } else \ NA_SETPa((ai), type, (ptr), (v)) /* ========================== 1 index get/set ============================ */ @@ -315,42 +315,42 @@ _makeSetPa(Bool) #define NA_GET3(ai, type, i, j, k) NA_GETP(ai, type, NA_PTR3(ai, i, j, k)) /* byteswapping */ -#define NA_SET3b(ai, type, i, j, k, v) \ +#define NA_SET3b(ai, type, i, j, k, v) \ NA_SETPb(ai, type, NA_PTR3(ai, i, j, k), v) /* aligning */ -#define NA_SET3a(ai, type, i, j, k, v) \ +#define NA_SET3a(ai, type, i, j, k, v) \ NA_SETPa(ai, type, NA_PTR3(ai, i, j, k), v) /* fast (aligned, !byteswapped) */ -#define NA_SET3f(ai, type, i, j, k, v) \ +#define NA_SET3f(ai, type, i, j, k, v) \ NA_SETPf(ai, type, NA_PTR3(ai, i, j, k), v) -#define NA_SET3(ai, type, i, j, k, v) \ +#define NA_SET3(ai, type, i, j, k, v) \ NA_SETP(ai, type, NA_PTR3(ai, i, j, k), v) /* ========================== 1D get/set ================================== */ -#define NA_GET1Db(ai, type, base, cnt, out) \ - { int i, stride = ai->strides[ai->nd-1]; \ - for(i=0; i<cnt; i++) { \ - out[i] = NA_GETPb(ai, type, base); \ - base += stride; \ - } \ - } +#define NA_GET1Db(ai, type, base, cnt, out) \ + { int i, stride = PyArray_STRIDES(ai)[PyArray_NDIM(ai)-1]; \ + for(i=0; i<cnt; i++) { \ + out[i] = NA_GETPb(ai, type, base); \ + base += stride; \ + } \ + } #define NA_GET1Da(ai, type, base, cnt, out) \ - { int i, stride = ai->strides[ai->nd-1]; \ + { int i, stride = PyArray_STRIDES(ai)[PyArray_NDIM(ai)-1]; \ for(i=0; i<cnt; i++) { \ out[i] = NA_GETPa(ai, type, base); \ base += stride; \ } \ - } + } #define NA_GET1Df(ai, type, base, cnt, out) \ - { int i, stride = ai->strides[ai->nd-1]; \ + { int i, stride = PyArray_STRIDES(ai)[PyArray_NDIM(ai)-1]; \ for(i=0; i<cnt; i++) { \ out[i] = NA_GETPf(ai, type, base); \ base += stride; \ } \ - } + } #define NA_GET1D(ai, type, base, cnt, out) \ if (PyArray_ISCARRAY(ai)) { \ @@ -360,9 +360,9 @@ _makeSetPa(Bool) } else { \ NA_GET1Da(ai, type, base, cnt, out); \ } - + #define NA_SET1Db(ai, type, base, cnt, in) \ - { int i, stride = ai->strides[ai->nd-1]; \ + { int i, stride = PyArray_STRIDES(ai)[PyArray_NDIM(ai)-1]; \ for(i=0; i<cnt; i++) { \ NA_SETPb(ai, type, base, in[i]); \ base += stride; \ @@ -370,7 +370,7 @@ _makeSetPa(Bool) } #define NA_SET1Da(ai, type, base, cnt, in) \ - { int i, stride = ai->strides[ai->nd-1]; \ + { int i, stride = PyArray_STRIDES(ai)[PyArray_NDIM(ai)-1]; \ for(i=0; i<cnt; i++) { \ NA_SETPa(ai, type, base, in[i]); \ base += stride; \ @@ -378,7 +378,7 @@ _makeSetPa(Bool) } #define NA_SET1Df(ai, type, base, cnt, in) \ - { int i, stride = ai->strides[ai->nd-1]; \ + { int i, stride = PyArray_STRIDES(ai)[PyArray_NDIM(ai)-1]; \ for(i=0; i<cnt; i++) { \ NA_SETPf(ai, type, base, in[i]); \ base += stride; \ @@ -393,7 +393,7 @@ _makeSetPa(Bool) } else { \ NA_SET1Da(ai, type, base, cnt, out); \ } - + /* ========================== utilities ================================== */ #if !defined(MIN) @@ -412,7 +412,7 @@ _makeSetPa(Bool) #define BOOLEAN_BITWISE_NOT(x) ((x) ^ 1) -#define NA_NBYTES(a) (a->descr->elsize * NA_elements(a)) +#define NA_NBYTES(a) (PyArray_DESCR(a)->elsize * NA_elements(a)) #if defined(NA_SMP) #define BEGIN_THREADS Py_BEGIN_ALLOW_THREADS |