diff options
65 files changed, 469 insertions, 296 deletions
diff --git a/doc/neps/missing-data.rst b/doc/neps/missing-data.rst index 6f124890b..338a8da96 100644 --- a/doc/neps/missing-data.rst +++ b/doc/neps/missing-data.rst @@ -320,7 +320,7 @@ A manual loop through a masked array like:: >>> a[3] = np.NA >>> a array([ 0., 1., 2., NA, 4.], maskna=True) - >>> for i in xrange(len(a)): + >>> for i in range(len(a)): ... a[i] = np.log(a[i]) ... __main__:2: RuntimeWarning: divide by zero encountered in log diff --git a/doc/release/1.8.0-notes.rst b/doc/release/1.8.0-notes.rst index f41c8e716..1750b5d14 100644 --- a/doc/release/1.8.0-notes.rst +++ b/doc/release/1.8.0-notes.rst @@ -37,6 +37,13 @@ compiler, then it's possible you will encounter problems. If so, please file a bug and as a temporary workaround you can re-enable the old build system by exporting the shell variable NPY_SEPARATE_COMPILATION=0. +For the AdvancedNew iterator the ``oa_ndim`` flag should now be -1 to indicate +that no ``op_axes`` and ``itershape`` are passed in. The ``oa_ndim == 0`` +case, now indicates a 0-D iteration and ``op_axes`` being NULL and the old +usage is deprecated. This does not effect the ``NpyIter_New`` or +``NpyIter_MultiNew`` functions. + + New features ============ diff --git a/doc/source/reference/arrays.classes.rst b/doc/source/reference/arrays.classes.rst index b35e36545..5cdadd40e 100644 --- a/doc/source/reference/arrays.classes.rst +++ b/doc/source/reference/arrays.classes.rst @@ -340,7 +340,7 @@ The default iterator of an ndarray object is the default Python iterator of a sequence type. Thus, when the array object itself is used as an iterator. The default behavior is equivalent to:: - for i in xrange(arr.shape[0]): + for i in range(arr.shape[0]): val = arr[i] This default iterator selects a sub-array of dimension :math:`N-1` diff --git a/doc/source/reference/c-api.iterator.rst b/doc/source/reference/c-api.iterator.rst index 7e2900bcc..1e3565bc1 100644 --- a/doc/source/reference/c-api.iterator.rst +++ b/doc/source/reference/c-api.iterator.rst @@ -634,12 +634,12 @@ Construction and Destruction Extends :cfunc:`NpyIter_MultiNew` with several advanced options providing more control over broadcasting and buffering. - If 0/NULL values are passed to ``oa_ndim``, ``op_axes``, ``itershape``, + If -1/NULL values are passed to ``oa_ndim``, ``op_axes``, ``itershape``, and ``buffersize``, it is equivalent to :cfunc:`NpyIter_MultiNew`. - The parameter ``oa_ndim``, when non-zero, specifies the number of + The parameter ``oa_ndim``, when not zero or -1, specifies the number of dimensions that will be iterated with customized broadcasting. - If it is provided, ``op_axes`` and/or ``itershape`` must also be provided. + If it is provided, ``op_axes`` must and ``itershape`` can also be provided. The ``op_axes`` parameter let you control in detail how the axes of the operand arrays get matched together and iterated. In ``op_axes``, you must provide an array of ``nop`` pointers @@ -649,6 +649,11 @@ Construction and Destruction -1 which means ``newaxis``. Within each ``op_axes[j]`` array, axes may not be repeated. The following example is how normal broadcasting applies to a 3-D array, a 2-D array, a 1-D array and a scalar. + + **Note**: Before NumPy 1.8 ``oa_ndim == 0` was used for signalling that + that ``op_axes`` and ``itershape`` are unused. This is deprecated and + should be replaced with -1. Better backward compatibility may be + achieved by using :cfunc:`NpyIter_MultiNew` for this case. .. code-block:: c diff --git a/numpy/add_newdocs.py b/numpy/add_newdocs.py index b6ace230f..9303fe77c 100644 --- a/numpy/add_newdocs.py +++ b/numpy/add_newdocs.py @@ -5405,7 +5405,7 @@ add_newdoc('numpy.core', 'ufunc', ('reduce', :: r = op.identity # op = ufunc - for i in xrange(len(A)): + for i in range(len(A)): r = op(r, A[i]) return r @@ -5486,7 +5486,7 @@ add_newdoc('numpy.core', 'ufunc', ('accumulate', r = np.empty(len(A)) t = op.identity # op = the ufunc being applied to A's elements - for i in xrange(len(A)): + for i in range(len(A)): t = op(t, A[i]) r[i] = t return r @@ -5666,8 +5666,8 @@ add_newdoc('numpy.core', 'ufunc', ('outer', For `A` and `B` one-dimensional, this is equivalent to:: r = empty(len(A),len(B)) - for i in xrange(len(A)): - for j in xrange(len(B)): + for i in range(len(A)): + for j in range(len(B)): r[i,j] = op(A[i], B[j]) # op = ufunc in question Parameters diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 7ba28f993..6b448528e 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -414,7 +414,7 @@ def _dtype_from_pep3118(spec, byteorder='@', is_subdtype=False): itemsize = 1 if spec[0].isdigit(): j = 1 - for j in xrange(1, len(spec)): + for j in range(1, len(spec)): if not spec[j].isdigit(): break itemsize = int(spec[:j]) diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index a7f9ccd44..e0eaecb95 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -35,7 +35,7 @@ def _all(a, axis=None, dtype=None, out=None, keepdims=False): def _count_reduce_items(arr, axis): if axis is None: - axis = tuple(xrange(arr.ndim)) + axis = tuple(range(arr.ndim)) if not isinstance(axis, tuple): axis = (axis,) items = 1 diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py index fa91a4799..c665cec0e 100644 --- a/numpy/core/arrayprint.py +++ b/numpy/core/arrayprint.py @@ -480,14 +480,14 @@ def _formatArray(a, format_function, rank, max_line_len, if rank == 1: s = "" line = next_line_prefix - for i in xrange(leading_items): + for i in range(leading_items): word = format_function(a[i]) + separator s, line = _extendLine(s, line, word, max_line_len, next_line_prefix) if summary_insert1: s, line = _extendLine(s, line, summary_insert1, max_line_len, next_line_prefix) - for i in xrange(trailing_items, 1, -1): + for i in range(trailing_items, 1, -1): word = format_function(a[-i]) + separator s, line = _extendLine(s, line, word, max_line_len, next_line_prefix) @@ -498,7 +498,7 @@ def _formatArray(a, format_function, rank, max_line_len, else: s = '[' sep = separator.rstrip() - for i in xrange(leading_items): + for i in range(leading_items): if i > 0: s += next_line_prefix s += _formatArray(a[i], format_function, rank-1, max_line_len, @@ -509,7 +509,7 @@ def _formatArray(a, format_function, rank, max_line_len, if summary_insert1: s += next_line_prefix + summary_insert1 + "\n" - for i in xrange(trailing_items, 1, -1): + for i in range(trailing_items, 1, -1): if leading_items or i != trailing_items: s += next_line_prefix s += _formatArray(a[-i], format_function, rank-1, max_line_len, diff --git a/numpy/core/code_generators/genapi.py b/numpy/core/code_generators/genapi.py index 164232e6e..a1fb9e641 100644 --- a/numpy/core/code_generators/genapi.py +++ b/numpy/core/code_generators/genapi.py @@ -203,7 +203,7 @@ def find_functions(filename, tag='API'): function_name = None function_args = [] doclist = [] - SCANNING, STATE_DOC, STATE_RETTYPE, STATE_NAME, STATE_ARGS = range(5) + SCANNING, STATE_DOC, STATE_RETTYPE, STATE_NAME, STATE_ARGS = list(range(5)) state = SCANNING tagcomment = '/*' + tag for lineno, line in enumerate(fo): diff --git a/numpy/core/machar.py b/numpy/core/machar.py index b7e64290e..d44d17499 100644 --- a/numpy/core/machar.py +++ b/numpy/core/machar.py @@ -123,7 +123,7 @@ class MachAr(object): # Do we really need to do this? Aren't they 2 and 2.0? # Determine ibeta and beta a = one - for _ in xrange(max_iterN): + for _ in range(max_iterN): a = a + a temp = a + one temp1 = temp - a @@ -132,7 +132,7 @@ class MachAr(object): else: raise RuntimeError(msg % (_, one.dtype)) b = one - for _ in xrange(max_iterN): + for _ in range(max_iterN): b = b + b temp = a + b itemp = int_conv(temp-a) @@ -146,7 +146,7 @@ class MachAr(object): # Determine it and irnd it = -1 b = one - for _ in xrange(max_iterN): + for _ in range(max_iterN): it = it + 1 b = b * beta temp = b + one @@ -158,7 +158,7 @@ class MachAr(object): betah = beta / two a = one - for _ in xrange(max_iterN): + for _ in range(max_iterN): a = a + a temp = a + one temp1 = temp - a @@ -182,7 +182,7 @@ class MachAr(object): for i in range(negep): a = a * betain b = a - for _ in xrange(max_iterN): + for _ in range(max_iterN): temp = one - a if any(temp-one != zero): break @@ -201,7 +201,7 @@ class MachAr(object): machep = - it - 3 a = b - for _ in xrange(max_iterN): + for _ in range(max_iterN): temp = one + a if any(temp-one != zero): break @@ -223,7 +223,7 @@ class MachAr(object): z = betain t = one + eps nxres = 0 - for _ in xrange(max_iterN): + for _ in range(max_iterN): y = z z = y*y a = z*one # Check here for underflow @@ -249,7 +249,7 @@ class MachAr(object): mx = iz + iz - 1 # Determine minexp and xmin - for _ in xrange(max_iterN): + for _ in range(max_iterN): xmin = y y = y * betain a = y * one diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index a114e4bb5..57e366efb 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1040,8 +1040,8 @@ def tensordot(a, b, axes=2): try: iter(axes) except: - axes_a = range(-axes,0) - axes_b = range(0,axes) + axes_a = list(range(-axes,0)) + axes_b = list(range(0,axes)) else: axes_a, axes_b = axes try: @@ -1065,7 +1065,7 @@ def tensordot(a, b, axes=2): equal = True if (na != nb): equal = False else: - for k in xrange(na): + for k in range(na): if as_[axes_a[k]] != bs[axes_b[k]]: equal = False break @@ -1213,7 +1213,7 @@ def rollaxis(a, axis, start=0): start -= 1 if axis==start: return a - axes = range(0,n) + axes = list(range(0,n)) axes.remove(axis) axes.insert(start, axis) return a.transpose(axes) diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index b069b5426..f89fa994d 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -112,7 +112,7 @@ if sys.version_info[0] >= 3: # "import string" is costly to import! # Construct the translation tables directly # "A" = chr(65), "a" = chr(97) -_all_chars = map(chr, range(256)) +_all_chars = map(chr, list(range(256))) _ascii_upper = _all_chars[65:65+26] _ascii_lower = _all_chars[97:97+26] LOWER_TABLE="".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:]) diff --git a/numpy/core/records.py b/numpy/core/records.py index ff5d98d3a..761b1015a 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -603,7 +603,7 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None, nfields = len(recList[0]) if formats is None and dtype is None: # slower obj = sb.array(recList, dtype=object) - arrlist = [sb.array(obj[..., i].tolist()) for i in xrange(nfields)] + arrlist = [sb.array(obj[..., i].tolist()) for i in range(nfields)] return fromarrays(arrlist, formats=formats, shape=shape, names=names, titles=titles, aligned=aligned, byteorder=byteorder) @@ -622,7 +622,7 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None, if len(shape) > 1: raise ValueError("Can only deal with 1-d array.") _array = recarray(shape, descr) - for k in xrange(_array.size): + for k in range(_array.size): _array[k] = tuple(recList[k]) return _array else: diff --git a/numpy/core/src/multiarray/nditer_api.c b/numpy/core/src/multiarray/nditer_api.c index eef7815af..40043648d 100644 --- a/numpy/core/src/multiarray/nditer_api.c +++ b/numpy/core/src/multiarray/nditer_api.c @@ -134,12 +134,10 @@ NpyIter_RemoveAxis(NpyIter *iter, int axis) axisdata = NIT_INDEX_AXISDATA(axisdata_del, 1); memmove(axisdata_del, axisdata, (ndim-1-xdim)*sizeof_axisdata); - /* If there is more than one dimension, shrink the iterator */ - if (ndim > 1) { - NIT_NDIM(iter) = ndim-1; - } - /* Otherwise convert it to a singleton dimension */ - else { + /* Shrink the iterator */ + NIT_NDIM(iter) = ndim - 1; + /* If it is now 0-d fill the singleton dimension */ + if (ndim == 1) { npy_intp *strides = NAD_STRIDES(axisdata_del); NAD_SHAPE(axisdata_del) = 1; for (iop = 0; iop < nop; ++iop) { @@ -642,6 +640,9 @@ NpyIter_GetIterIndex(NpyIter *iter) npy_intp sizeof_axisdata; iterindex = 0; + if (ndim == 0) { + return 0; + } sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop); axisdata = NIT_INDEX_AXISDATA(NIT_AXISDATA(iter), ndim-1); @@ -1750,6 +1751,8 @@ npyiter_goto_iterindex(NpyIter *iter, npy_intp iterindex) NIT_ITERINDEX(iter) = iterindex; + ndim = ndim ? ndim : 1; + if (iterindex == 0) { dataptr = NIT_RESETDATAPTR(iter); diff --git a/numpy/core/src/multiarray/nditer_constr.c b/numpy/core/src/multiarray/nditer_constr.c index cfbaea321..a40cbc7bc 100644 --- a/numpy/core/src/multiarray/nditer_constr.c +++ b/numpy/core/src/multiarray/nditer_constr.c @@ -54,8 +54,7 @@ static int npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itflags, char **op_dataptr, npy_uint32 *op_flags, int **op_axes, - npy_intp *itershape, - int output_scalars); + npy_intp *itershape); static void npyiter_replace_axisdata(NpyIter *iter, int iop, PyArrayObject *op, @@ -75,7 +74,7 @@ static PyArray_Descr * npyiter_get_common_dtype(int nop, PyArrayObject **op, npyiter_opitflags *op_itflags, PyArray_Descr **op_dtype, PyArray_Descr **op_request_dtypes, - int only_inputs, int output_scalars); + int only_inputs); static PyArrayObject * npyiter_new_temp_array(NpyIter *iter, PyTypeObject *subtype, npy_uint32 flags, npyiter_opitflags *op_itflags, @@ -86,7 +85,7 @@ npyiter_allocate_arrays(NpyIter *iter, npy_uint32 flags, PyArray_Descr **op_dtype, PyTypeObject *subtype, npy_uint32 *op_flags, npyiter_opitflags *op_itflags, - int **op_axes, int output_scalars); + int **op_axes); static void npyiter_get_priority_subtype(int nop, PyArrayObject **op, npyiter_opitflags *op_itflags, @@ -122,8 +121,7 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags, npy_int8 *perm; NpyIter_BufferData *bufferdata = NULL; - int any_allocate = 0, any_missing_dtypes = 0, - output_scalars = 0, need_subtype = 0; + int any_allocate = 0, any_missing_dtypes = 0, need_subtype = 0; /* The subtype for automatically allocated outputs */ double subtype_priority = NPY_PRIORITY; @@ -158,6 +156,22 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags, return NULL; } + /* + * Before 1.8, if `oa_ndim == 0`, this meant `op_axes != NULL` was an error. + * With 1.8, `oa_ndim == -1` takes this role, while op_axes in that case + * enforces a 0-d iterator. Using `oa_ndim == 0` with `op_axes == NULL` + * is thus deprecated with version 1.8. + */ + if ((oa_ndim == 0) && (op_axes == NULL)) { + char* mesg = "using `oa_ndim == 0` when `op_axes` is NULL is " + "deprecated. Use `oa_ndim == -1` or the MultiNew " + "iterator for NumPy <1.8 compatibility"; + if (DEPRECATE(mesg) < 0) { + return NULL; + } + oa_ndim = -1; + } + /* Error check 'oa_ndim' and 'op_axes', which must be used together */ if (!npyiter_check_op_axes(nop, oa_ndim, op_axes, itershape)) { return NULL; @@ -175,12 +189,6 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags, /* Calculate how many dimensions the iterator should have */ ndim = npyiter_calculate_ndim(nop, op_in, oa_ndim); - /* If 'ndim' is zero, any outputs should be scalars */ - if (ndim == 0) { - output_scalars = 1; - ndim = 1; - } - NPY_IT_TIME_POINT(c_calculate_ndim); /* Allocate memory for the iterator */ @@ -231,8 +239,7 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags, /* Fill in the AXISDATA arrays and set the ITERSIZE field */ if (!npyiter_fill_axisdata(iter, flags, op_itflags, op_dataptr, - op_flags, op_axes, itershape, - output_scalars)) { + op_flags, op_axes, itershape)) { NpyIter_Deallocate(iter); return NULL; } @@ -338,8 +345,7 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags, dtype = npyiter_get_common_dtype(nop, op, op_itflags, op_dtype, op_request_dtypes, - only_inputs, - output_scalars); + only_inputs); if (dtype == NULL) { NpyIter_Deallocate(iter); return NULL; @@ -389,7 +395,7 @@ NpyIter_AdvancedNew(int nop, PyArrayObject **op_in, npy_uint32 flags, * done now using a memory layout matching the iterator. */ if (!npyiter_allocate_arrays(iter, flags, op_dtype, subtype, op_flags, - op_itflags, op_axes, output_scalars)) { + op_itflags, op_axes)) { NpyIter_Deallocate(iter); return NULL; } @@ -504,7 +510,7 @@ NpyIter_MultiNew(int nop, PyArrayObject **op_in, npy_uint32 flags, { return NpyIter_AdvancedNew(nop, op_in, flags, order, casting, op_flags, op_request_dtypes, - 0, NULL, NULL, 0); + -1, NULL, NULL, 0); } /*NUMPY_API @@ -521,7 +527,7 @@ NpyIter_New(PyArrayObject *op, npy_uint32 flags, return NpyIter_AdvancedNew(1, &op, flags, order, casting, &op_flags, &dtype, - 0, NULL, NULL, 0); + -1, NULL, NULL, 0); } /*NUMPY_API @@ -758,53 +764,60 @@ npyiter_check_op_axes(int nop, int oa_ndim, int **op_axes, char axes_dupcheck[NPY_MAXDIMS]; int iop, idim; - if (oa_ndim == 0 && (op_axes != NULL || itershape != NULL)) { - PyErr_Format(PyExc_ValueError, - "If 'op_axes' or 'itershape' is not NULL in the" - "iterator constructor, 'oa_ndim' must be greater than zero"); - return 0; - } - else if (oa_ndim > 0) { - if (oa_ndim > NPY_MAXDIMS) { + if (oa_ndim < 0) { + /* + * If `oa_ndim < 0`, `op_axes` and `itershape` are signalled to + * be unused and should be NULL. (Before NumPy 1.8 this was + * signalled by `oa_ndim == 0`.) + */ + if (op_axes != NULL || itershape != NULL) { PyErr_Format(PyExc_ValueError, + "If 'op_axes' or 'itershape' is not NULL in the iterator " + "constructor, 'oa_ndim' must be zero or greater"); + return 0; + } + return 1; + } + if (oa_ndim > NPY_MAXDIMS) { + PyErr_Format(PyExc_ValueError, "Cannot construct an iterator with more than %d dimensions " "(%d were requested for op_axes)", (int)NPY_MAXDIMS, oa_ndim); - return 0; - } - else if (op_axes == NULL) { - PyErr_Format(PyExc_ValueError, - "If 'oa_ndim' is greater than zero in the iterator " - "constructor, then op_axes cannot be NULL"); - return 0; - } + return 0; + } + if (op_axes == NULL) { + PyErr_Format(PyExc_ValueError, + "If 'oa_ndim' is zero or greater in the iterator " + "constructor, then op_axes cannot be NULL"); + return 0; + } - /* Check that there are no duplicates in op_axes */ - for (iop = 0; iop < nop; ++iop) { - int *axes = op_axes[iop]; - if (axes != NULL) { - memset(axes_dupcheck, 0, NPY_MAXDIMS); - for (idim = 0; idim < oa_ndim; ++idim) { - npy_intp i = axes[idim]; - if (i >= 0) { - if (i >= NPY_MAXDIMS) { - PyErr_Format(PyExc_ValueError, - "The 'op_axes' provided to the iterator " - "constructor for operand %d " - "contained invalid " - "values %d", (int)iop, (int)i); - return 0; - } else if(axes_dupcheck[i] == 1) { - PyErr_Format(PyExc_ValueError, - "The 'op_axes' provided to the iterator " - "constructor for operand %d " - "contained duplicate " - "value %d", (int)iop, (int)i); - return 0; - } - else { - axes_dupcheck[i] = 1; - } + /* Check that there are no duplicates in op_axes */ + for (iop = 0; iop < nop; ++iop) { + int *axes = op_axes[iop]; + if (axes != NULL) { + memset(axes_dupcheck, 0, NPY_MAXDIMS); + for (idim = 0; idim < oa_ndim; ++idim) { + npy_intp i = axes[idim]; + if (i >= 0) { + if (i >= NPY_MAXDIMS) { + PyErr_Format(PyExc_ValueError, + "The 'op_axes' provided to the iterator " + "constructor for operand %d " + "contained invalid " + "values %d", (int)iop, (int)i); + return 0; + } + else if (axes_dupcheck[i] == 1) { + PyErr_Format(PyExc_ValueError, + "The 'op_axes' provided to the iterator " + "constructor for operand %d " + "contained duplicate " + "value %d", (int)iop, (int)i); + return 0; + } + else { + axes_dupcheck[i] = 1; } } } @@ -819,7 +832,7 @@ npyiter_calculate_ndim(int nop, PyArrayObject **op_in, int oa_ndim) { /* If 'op_axes' is being used, force 'ndim' */ - if (oa_ndim > 0 ) { + if (oa_ndim >= 0 ) { return oa_ndim; } /* Otherwise it's the maximum 'ndim' from the operands */ @@ -1439,8 +1452,7 @@ static int npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itflags, char **op_dataptr, npy_uint32 *op_flags, int **op_axes, - npy_intp *itershape, - int output_scalars) + npy_intp *itershape) { npy_uint32 itflags = NIT_ITFLAGS(iter); int idim, ndim = NIT_NDIM(iter); @@ -1540,6 +1552,13 @@ npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itf axisdata = NIT_AXISDATA(iter); sizeof_axisdata = NIT_AXISDATA_SIZEOF(itflags, ndim, nop); + if (ndim == 0) { + /* Need to fill the first axisdata, even if the iterator is 0-d */ + NAD_SHAPE(axisdata) = 1; + NAD_INDEX(axisdata) = 0; + memcpy(NAD_PTRS(axisdata), op_dataptr, NPY_SIZEOF_INTP*nop); + } + /* Now process the operands, filling in the axisdata */ for (idim = 0; idim < ndim; ++idim) { npy_intp bshape = broadcast_shape[ndim-idim-1]; @@ -1560,7 +1579,7 @@ npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itf ondim = PyArray_NDIM(op_cur); if (bshape == 1) { strides[iop] = 0; - if (idim >= ondim && !output_scalars && + if (idim >= ondim && (op_flags[iop] & NPY_ITER_NO_BROADCAST)) { goto operand_different_than_broadcast; } @@ -1681,8 +1700,8 @@ npyiter_fill_axisdata(NpyIter *iter, npy_uint32 flags, npyiter_opitflags *op_itf } /* Now fill in the ITERSIZE member */ - NIT_ITERSIZE(iter) = broadcast_shape[0]; - for (idim = 1; idim < ndim; ++idim) { + NIT_ITERSIZE(iter) = 1; + for (idim = 0; idim < ndim; ++idim) { NIT_ITERSIZE(iter) *= broadcast_shape[idim]; } /* The range defaults to everything */ @@ -2003,7 +2022,10 @@ npyiter_replace_axisdata(NpyIter *iter, int iop, NIT_RESETDATAPTR(iter)[iop] = op_dataptr; NIT_BASEOFFSETS(iter)[iop] = baseoffset; axisdata = axisdata0; - for (idim = 0; idim < ndim; ++idim, NIT_ADVANCE_AXISDATA(axisdata, 1)) { + /* Fill at least one axisdata, for the 0-d case */ + NAD_PTRS(axisdata)[iop] = op_dataptr; + NIT_ADVANCE_AXISDATA(axisdata, 1); + for (idim = 1; idim < ndim; ++idim, NIT_ADVANCE_AXISDATA(axisdata, 1)) { NAD_PTRS(axisdata)[iop] = op_dataptr; } } @@ -2029,7 +2051,7 @@ npyiter_compute_index_strides(NpyIter *iter, npy_uint32 flags) /* * If there is only one element being iterated, we just have * to touch the first AXISDATA because nothing will ever be - * incremented. + * incremented. This also initializes the data for the 0-d case. */ if (NIT_ITERSIZE(iter) == 1) { if (itflags & NPY_ITFLAG_HASINDEX) { @@ -2399,7 +2421,7 @@ static PyArray_Descr * npyiter_get_common_dtype(int nop, PyArrayObject **op, npyiter_opitflags *op_itflags, PyArray_Descr **op_dtype, PyArray_Descr **op_request_dtypes, - int only_inputs, int output_scalars) + int only_inputs) { int iop; npy_intp narrs = 0, ndtypes = 0; @@ -2698,7 +2720,7 @@ npyiter_allocate_arrays(NpyIter *iter, npy_uint32 flags, PyArray_Descr **op_dtype, PyTypeObject *subtype, npy_uint32 *op_flags, npyiter_opitflags *op_itflags, - int **op_axes, int output_scalars) + int **op_axes) { npy_uint32 itflags = NIT_ITFLAGS(iter); int idim, ndim = NIT_NDIM(iter); @@ -2729,7 +2751,7 @@ npyiter_allocate_arrays(NpyIter *iter, if (op[iop] == NULL) { PyArrayObject *out; PyTypeObject *op_subtype; - int ondim = output_scalars ? 0 : ndim; + int ondim = ndim; /* Check whether the subtype was disabled */ op_subtype = (op_flags[iop] & NPY_ITER_NO_SUBTYPE) ? @@ -2902,7 +2924,7 @@ npyiter_allocate_arrays(NpyIter *iter, if ((itflags & NPY_ITFLAG_BUFFER) && !(op_itflags[iop] & NPY_OP_ITFLAG_CAST)) { NpyIter_AxisData *axisdata = NIT_AXISDATA(iter); - if (ndim == 1) { + if (ndim <= 1) { op_itflags[iop] |= NPY_OP_ITFLAG_BUFNEVER; NBF_STRIDES(bufferdata)[iop] = NAD_STRIDES(axisdata)[iop]; } diff --git a/numpy/core/src/multiarray/nditer_impl.h b/numpy/core/src/multiarray/nditer_impl.h index 1251baa6e..ae24f46e6 100644 --- a/numpy/core/src/multiarray/nditer_impl.h +++ b/numpy/core/src/multiarray/nditer_impl.h @@ -294,7 +294,7 @@ struct NpyIter_AD { #define NIT_SIZEOF_ITERATOR(itflags, ndim, nop) ( \ sizeof(struct NpyIter_InternalOnly) + \ NIT_AXISDATA_OFFSET(itflags, ndim, nop) + \ - NIT_AXISDATA_SIZEOF(itflags, ndim, nop)*(ndim)) + NIT_AXISDATA_SIZEOF(itflags, ndim, nop)*(ndim ? ndim : 1)) /* Internal helper functions shared between implementation files */ NPY_NO_EXPORT void diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c index 4621491a3..61f0c42b6 100644 --- a/numpy/core/src/multiarray/nditer_pywrap.c +++ b/numpy/core/src/multiarray/nditer_pywrap.c @@ -95,7 +95,6 @@ NpyIter_GlobalFlagsConverter(PyObject *flags_in, npy_uint32 *flags) npy_uint32 flag; if (flags_in == NULL || flags_in == Py_None) { - *flags = 0; return 1; } @@ -526,7 +525,7 @@ npyiter_convert_op_axes(PyObject *op_axes_in, npy_intp nop, return 0; } - *oa_ndim = 0; + *oa_ndim = -1; /* Copy the tuples into op_axes */ for (iop = 0; iop < nop; ++iop) { @@ -545,13 +544,8 @@ npyiter_convert_op_axes(PyObject *op_axes_in, npy_intp nop, Py_DECREF(a); return 0; } - if (*oa_ndim == 0) { + if (*oa_ndim == -1) { *oa_ndim = PySequence_Size(a); - if (*oa_ndim == 0) { - PyErr_SetString(PyExc_ValueError, - "op_axes must have at least one dimension"); - return 0; - } if (*oa_ndim > NPY_MAXDIMS) { PyErr_SetString(PyExc_ValueError, "Too many dimensions in op_axes"); @@ -575,7 +569,7 @@ npyiter_convert_op_axes(PyObject *op_axes_in, npy_intp nop, op_axes[iop][idim] = -1; } else { - op_axes[iop][idim] = PyInt_AsLong(v); + op_axes[iop][idim] = PyArray_PyIntAsInt(v); if (op_axes[iop][idim]==-1 && PyErr_Occurred()) { Py_DECREF(a); @@ -589,7 +583,7 @@ npyiter_convert_op_axes(PyObject *op_axes_in, npy_intp nop, } } - if (*oa_ndim == 0) { + if (*oa_ndim == -1) { PyErr_SetString(PyExc_ValueError, "If op_axes is provided, at least one list of axes " "must be contained within it"); @@ -726,7 +720,7 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) NPY_CASTING casting = NPY_SAFE_CASTING; npy_uint32 op_flags[NPY_MAXARGS]; PyArray_Descr *op_request_dtypes[NPY_MAXARGS]; - int oa_ndim = 0; + int oa_ndim = -1; int op_axes_arrays[NPY_MAXARGS][NPY_MAXDIMS]; int *op_axes[NPY_MAXARGS]; PyArray_Dims itershape = {NULL, 0}; @@ -784,7 +778,7 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) } if (itershape.len > 0) { - if (oa_ndim == 0) { + if (oa_ndim == -1) { oa_ndim = itershape.len; memset(op_axes, 0, sizeof(op_axes[0]) * nop); } @@ -800,10 +794,9 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) itershape.ptr = NULL; } - self->iter = NpyIter_AdvancedNew(nop, op, flags, order, casting, op_flags, op_request_dtypes, - oa_ndim, oa_ndim > 0 ? op_axes : NULL, + oa_ndim, oa_ndim >= 0 ? op_axes : NULL, itershape.ptr, buffersize); @@ -860,7 +853,7 @@ NpyIter_NestedIters(PyObject *NPY_UNUSED(self), int iop, nop = 0, inest, nnest = 0; PyArrayObject *op[NPY_MAXARGS]; - npy_uint32 flags = 0, flags_inner = 0; + npy_uint32 flags = 0, flags_inner; NPY_ORDER order = NPY_KEEPORDER; NPY_CASTING casting = NPY_SAFE_CASTING; npy_uint32 op_flags[NPY_MAXARGS], op_flags_inner[NPY_MAXARGS]; diff --git a/numpy/core/src/private/lowlevel_strided_loops.h b/numpy/core/src/private/lowlevel_strided_loops.h index 94c6a2121..c9fd1248f 100644 --- a/numpy/core/src/private/lowlevel_strided_loops.h +++ b/numpy/core/src/private/lowlevel_strided_loops.h @@ -256,6 +256,7 @@ PyArray_CastRawArrays(npy_intp count, * 'stransfer' with the provided dst_stride/src_stride and * dst_strides[0]/src_strides[0], so the caller can use those values to * specialize the function. + * Note that even if ndim == 0, everything needs to be set as if ndim == 1. * * The return value is the number of elements it couldn't copy. A return value * of 0 means all elements were copied, a larger value means the end of diff --git a/numpy/core/src/umath/reduction.c b/numpy/core/src/umath/reduction.c index e6ed04e99..f69aea2d0 100644 --- a/numpy/core/src/umath/reduction.c +++ b/numpy/core/src/umath/reduction.c @@ -513,7 +513,7 @@ PyUFunc_ReduceWrapper(PyArrayObject *operand, PyArrayObject *out, NPY_KEEPORDER, casting, op_flags, op_dtypes, - 0, NULL, NULL, buffersize); + -1, NULL, NULL, buffersize); if (iter == NULL) { goto fail; } diff --git a/numpy/core/src/umath/ufunc_object.c b/numpy/core/src/umath/ufunc_object.c index 124185bfd..9c499d322 100644 --- a/numpy/core/src/umath/ufunc_object.c +++ b/numpy/core/src/umath/ufunc_object.c @@ -1211,7 +1211,7 @@ iterator_loop(PyUFuncObject *ufunc, NPY_ITER_DELAY_BUFALLOC, order, NPY_UNSAFE_CASTING, op_flags, dtype, - 0, NULL, NULL, buffersize); + -1, NULL, NULL, buffersize); if (iter == NULL) { return -1; } @@ -1509,7 +1509,7 @@ execute_fancy_ufunc_loop(PyUFuncObject *ufunc, NPY_ITER_GROWINNER, order, NPY_UNSAFE_CASTING, op_flags, dtypes, - 0, NULL, NULL, buffersize); + -1, NULL, NULL, buffersize); if (iter == NULL) { return -1; } @@ -1976,18 +1976,6 @@ PyUFunc_GeneralizedFunction(PyUFuncObject *ufunc, NPY_ITER_NO_BROADCAST; } - /* - * If there are no iteration dimensions, create a fake one - * so that the scalar edge case works right. - */ - if (iter_ndim == 0) { - iter_ndim = 1; - iter_shape[0] = 1; - for (i = 0; i < nop; ++i) { - op_axes[i][0] = -1; - } - } - /* Create the iterator */ iter = NpyIter_AdvancedNew(nop, op, NPY_ITER_MULTI_INDEX| NPY_ITER_REFS_OK| diff --git a/numpy/core/tests/test_blasdot.py b/numpy/core/tests/test_blasdot.py index ec80840c5..d6abacfc1 100644 --- a/numpy/core/tests/test_blasdot.py +++ b/numpy/core/tests/test_blasdot.py @@ -49,7 +49,7 @@ def test_dot_3args(): v = np.random.random_sample((16, 32)) r = np.empty((1024, 32)) - for i in xrange(12): + for i in range(12): np.dot(f,v,r) assert_equal(sys.getrefcount(r), 2) r2 = np.dot(f,v,out=None) diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index 4fba533db..a60d9a415 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -241,6 +241,7 @@ class TestEinSum(TestCase): assert_equal(np.einsum(a, [0,0]), np.trace(a).astype(dtype)) # multiply(a, b) + assert_equal(np.einsum("..., ...", 3, 4), 12) # scalar case for n in range(1,17): a = np.arange(3*n, dtype=dtype).reshape(3,n) b = np.arange(2*3*n, dtype=dtype).reshape(2,3,n) diff --git a/numpy/core/tests/test_item_selection.py b/numpy/core/tests/test_item_selection.py index 6da27175b..ab10b476c 100644 --- a/numpy/core/tests/test_item_selection.py +++ b/numpy/core/tests/test_item_selection.py @@ -47,7 +47,7 @@ class TestTake(TestCase): def test_refcounting(self): - objects = [object() for i in xrange(10)] + objects = [object() for i in range(10)] for mode in ('raise', 'clip', 'wrap'): a = np.array(objects) b = np.array([2, 2, 4, 5, 3, 5]) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 25cc8ced8..a9a79d38c 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -579,7 +579,7 @@ class TestMethods(TestCase): # test object array sorts. a = np.empty((101,), dtype=np.object) - a[:] = range(101) + a[:] = list(range(101)) b = a[::-1] for kind in ['q', 'h', 'm'] : msg = "object sort, kind=%s" % kind @@ -729,7 +729,7 @@ class TestMethods(TestCase): # test object array argsorts. a = np.empty((101,), dtype=np.object) - a[:] = range(101) + a[:] = list(range(101)) b = a[::-1] r = np.arange(101) rr = r[::-1] @@ -1059,7 +1059,7 @@ class TestMethods(TestCase): # Regression test for a bug that crept in at one point a = np.zeros((100, 100)) assert_(sys.getrefcount(a) < 50) - for i in xrange(100): + for i in range(100): a.diagonal() assert_(sys.getrefcount(a) < 50) @@ -1311,10 +1311,10 @@ class TestArgmax(TestCase): def test_all(self): a = np.random.normal(0,1,(4,5,6,7,8)) - for i in xrange(a.ndim): + for i in range(a.ndim): amax = a.max(i) aargmax = a.argmax(i) - axes = range(a.ndim) + axes = list(range(a.ndim)) axes.remove(i) assert_(all(amax == aargmax.choose(*a.transpose(i,*axes)))) @@ -1379,10 +1379,10 @@ class TestArgmin(TestCase): def test_all(self): a = np.random.normal(0,1,(4,5,6,7,8)) - for i in xrange(a.ndim): + for i in range(a.ndim): amin = a.min(i) aargmin = a.argmin(i) - axes = range(a.ndim) + axes = list(range(a.ndim)) axes.remove(i) assert_(all(amin == aargmin.choose(*a.transpose(i,*axes)))) @@ -1541,7 +1541,7 @@ class TestPutmask(object): class TestTake(object): def tst_basic(self,x): - ind = range(x.shape[0]) + ind = list(range(x.shape[0])) assert_array_equal(x.take(ind, axis=0), x) def test_ip_types(self): @@ -2109,7 +2109,7 @@ class TestDot(TestCase): v = np.random.random_sample((16, 32)) r = np.empty((1024, 32)) - for i in xrange(12): + for i in range(12): dot(f,v,r) assert_equal(sys.getrefcount(r), 2) r2 = dot(f,v,out=None) @@ -2532,7 +2532,7 @@ if sys.version_info >= (2, 6): def test_native_padding(self): align = np.dtype('i').alignment - for j in xrange(8): + for j in range(8): if j == 0: s = 'bi' else: @@ -2793,7 +2793,7 @@ if sys.version_info >= (2, 6): assert_equal(y.format, '<i') def test_padding(self): - for j in xrange(8): + for j in range(8): x = np.array([(1,),(2,)], dtype={'f0': (int, j)}) self._check_roundtrip(x) diff --git a/numpy/core/tests/test_multiarray_assignment.py b/numpy/core/tests/test_multiarray_assignment.py index 555de8c4a..d5e506249 100644 --- a/numpy/core/tests/test_multiarray_assignment.py +++ b/numpy/core/tests/test_multiarray_assignment.py @@ -45,7 +45,7 @@ def _indices(ndims): # no itertools.product available in Py2.4 res = [[]] - for i in xrange(ndims): + for i in range(ndims): newres = [] for elem in ind: for others in res: diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index 30d4cd640..9227d89f9 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -1435,32 +1435,32 @@ def test_iter_iterindex(): a = arange(24).reshape(4,3,2) for flags in ([], ['buffered']): i = nditer(a, flags, buffersize=buffersize) - assert_equal(iter_iterindices(i), range(24)) + assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 2 - assert_equal(iter_iterindices(i), range(2,24)) + assert_equal(iter_iterindices(i), list(range(2,24))) i = nditer(a, flags, order='F', buffersize=buffersize) - assert_equal(iter_iterindices(i), range(24)) + assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 5 - assert_equal(iter_iterindices(i), range(5,24)) + assert_equal(iter_iterindices(i), list(range(5,24))) i = nditer(a[::-1], flags, order='F', buffersize=buffersize) - assert_equal(iter_iterindices(i), range(24)) + assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 9 - assert_equal(iter_iterindices(i), range(9,24)) + assert_equal(iter_iterindices(i), list(range(9,24))) i = nditer(a[::-1,::-1], flags, order='C', buffersize=buffersize) - assert_equal(iter_iterindices(i), range(24)) + assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 13 - assert_equal(iter_iterindices(i), range(13,24)) + assert_equal(iter_iterindices(i), list(range(13,24))) i = nditer(a[::1,::-1], flags, buffersize=buffersize) - assert_equal(iter_iterindices(i), range(24)) + assert_equal(iter_iterindices(i), list(range(24))) i.iterindex = 23 - assert_equal(iter_iterindices(i), range(23,24)) + assert_equal(iter_iterindices(i), list(range(23,24))) i.reset() i.iterindex = 2 - assert_equal(iter_iterindices(i), range(2,24)) + assert_equal(iter_iterindices(i), list(range(2,24))) def test_iter_iterrange(): # Make sure getting and resetting the iterrange works @@ -1572,7 +1572,7 @@ def test_iter_buffering_delayed_alloc(): assert_equal(i[0], 0) i[1] = 1 assert_equal(i[0:2], [0,1]) - assert_equal([[x[0][()],x[1][()]] for x in i], zip(range(6), [1]*6)) + assert_equal([[x[0][()],x[1][()]] for x in i], zip(list(range(6)), [1]*6)) def test_iter_buffered_cast_simple(): # Test that buffering can handle a simple cast @@ -1804,7 +1804,7 @@ def test_iter_buffered_cast_subarray(): casting='unsafe', op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) - for x, count in zip(i, range(6)): + for x, count in zip(i, list(range(6))): assert_(np.all(x['a'] == count)) # one element -> many -> back (copies it to all) @@ -2523,5 +2523,59 @@ def test_iter_allocated_array_dtypes(): c[1,1] = a / b assert_equal(it.operands[2], [[8, 12], [20, 5]]) + +def test_0d_iter(): + # Basic test for iteration of 0-d arrays: + i = nditer([2, 3], ['multi_index'], [['readonly']]*2) + assert_equal(i.ndim, 0) + assert_equal(i.next(), (2, 3)) + assert_equal(i.multi_index, ()) + assert_equal(i.iterindex, 0) + assert_raises(StopIteration, i.next) + # test reset: + i.reset() + assert_equal(i.next(), (2, 3)) + assert_raises(StopIteration, i.next) + + # test forcing to 0-d + i = nditer(np.arange(5), ['multi_index'], [['readonly']], op_axes=[()]) + assert_equal(i.ndim, 0) + assert_equal(len(i), 1) + # note that itershape=(), still behaves like None due to the conversions + + # Test a more complex buffered casting case (same as another test above) + sdt = [('a', 'f4'), ('b', 'i8'), ('c', 'c8', (2,3)), ('d', 'O')] + a = np.array(0.5, dtype='f4') + i = nditer(a, ['buffered','refs_ok'], ['readonly'], + casting='unsafe', op_dtypes=sdt) + vals = i.next() + assert_equal(vals['a'], 0.5) + assert_equal(vals['b'], 0) + assert_equal(vals['c'], [[(0.5)]*3]*2) + assert_equal(vals['d'], 0.5) + + +def test_0d_nested_iter(): + a = np.arange(12).reshape(2,3,2) + i, j = np.nested_iters(a, [[],[1,0,2]]) + vals = [] + for x in i: + vals.append([y for y in j]) + assert_equal(vals, [[0,1,2,3,4,5,6,7,8,9,10,11]]) + + i, j = np.nested_iters(a, [[1,0,2],[]]) + vals = [] + for x in i: + vals.append([y for y in j]) + assert_equal(vals, [[0],[1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11]]) + + i, j, k = np.nested_iters(a, [[2,0], [] ,[1]]) + vals = [] + for x in i: + for y in j: + vals.append([z for z in k]) + assert_equal(vals, [[0,2,4],[1,3,5],[6,8,10],[7,9,11]]) + + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 6d0ca4efc..6d3cbe923 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -520,7 +520,7 @@ class TestTypes(TestCase): class TestFromiter(TestCase): def makegen(self): - for x in xrange(24): + for x in range(24): yield x**2 def test_types(self): diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 5c7cba936..57d128b11 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -54,11 +54,11 @@ class TestFromrecords(TestCase): b = np.zeros(count, dtype='f8') c = np.zeros(count, dtype='f8') for i in range(len(a)): - a[i] = range(1, 10) + a[i] = list(range(1, 10)) mine = np.rec.fromarrays([a, b, c], names='date,data1,data2') for i in range(len(a)): - assert_((mine.date[i] == range(1, 10))) + assert_((mine.date[i] == list(range(1, 10)))) assert_((mine.data1[i] == 0.0)) assert_((mine.data2[i] == 0.0)) @@ -81,7 +81,7 @@ class TestFromrecords(TestCase): names='c1, c2, c3, c4') assert_(ra.dtype == pa.dtype) assert_(ra.shape == pa.shape) - for k in xrange(len(ra)): + for k in range(len(ra)): assert_(ra[k].item() == pa[k].item()) def test_recarray_conflict_fields(self): diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 95df4a113..69138647e 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -240,7 +240,7 @@ class TestRegression(TestCase): def test_argmax(self,level=rlevel): """Ticket #119""" a = np.random.normal(0,1,(4,5,6,7,8)) - for i in xrange(a.ndim): + for i in range(a.ndim): aargmax = a.argmax(i) def test_mem_divmod(self,level=rlevel): @@ -675,7 +675,7 @@ class TestRegression(TestCase): def test_arr_transpose(self, level=rlevel): """Ticket #516""" x = np.random.rand(*(2,)*16) - y = x.transpose(range(16)) + y = x.transpose(list(range(16))) def test_string_mergesort(self, level=rlevel): """Ticket #540""" @@ -1188,7 +1188,7 @@ class TestRegression(TestCase): """Ticket #950""" for m in [0, 1, 2]: for n in [0, 1, 2]: - for k in xrange(3): + for k in range(3): # Try to ensure that x->data contains non-zero floats x = np.array([123456789e199], dtype=np.float64) x.resize((m, 0)) @@ -1229,8 +1229,8 @@ class TestRegression(TestCase): def test_fromiter_bytes(self): """Ticket #1058""" - a = np.fromiter(range(10), dtype='b') - b = np.fromiter(range(10), dtype='B') + a = np.fromiter(list(range(10)), dtype='b') + b = np.fromiter(list(range(10)), dtype='B') assert_(np.alltrue(a == np.array([0,1,2,3,4,5,6,7,8,9]))) assert_(np.alltrue(b == np.array([0,1,2,3,4,5,6,7,8,9]))) @@ -1413,8 +1413,8 @@ class TestRegression(TestCase): assert_(np.isfinite(np.log1p(np.exp2(-53)))) def test_fromiter_comparison(self, level=rlevel): - a = np.fromiter(range(10), dtype='b') - b = np.fromiter(range(10), dtype='B') + a = np.fromiter(list(range(10)), dtype='b') + b = np.fromiter(list(range(10)), dtype='B') assert_(np.alltrue(a == np.array([0,1,2,3,4,5,6,7,8,9]))) assert_(np.alltrue(b == np.array([0,1,2,3,4,5,6,7,8,9]))) @@ -1550,7 +1550,7 @@ class TestRegression(TestCase): assert_equal(int(np.uint64(x)), x) def test_duplicate_field_names_assign(self): - ra = np.fromiter(((i*3, i*2) for i in xrange(10)), dtype='i8,f8') + ra = np.fromiter(((i*3, i*2) for i in range(10)), dtype='i8,f8') ra.dtype.names = ('f1', 'f2') rep = repr(ra) # should not cause a segmentation fault assert_raises(ValueError, setattr, ra.dtype, 'names', ('f1', 'f1')) diff --git a/numpy/core/tests/test_shape_base.py b/numpy/core/tests/test_shape_base.py index ccfb18ef7..e0c8197de 100644 --- a/numpy/core/tests/test_shape_base.py +++ b/numpy/core/tests/test_shape_base.py @@ -151,7 +151,7 @@ class TestVstack(TestCase): def test_concatenate_axis_None(): a = np.arange(4, dtype=np.float64).reshape((2,2)) - b = range(3) + b = list(range(3)) c = ['x'] r = np.concatenate((a, a), axis=None) assert_equal(r.dtype, a.dtype) diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index a0fbb4bba..963b2aae7 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -449,7 +449,7 @@ class TestUfunc(TestCase): ret = () base = permute_n(n-1) for perm in base: - for i in xrange(n): + for i in range(n): new = perm + [n-1] new[n-1] = new[i] new[i] = n-1 diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 78aa32234..244ba59a5 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -158,9 +158,9 @@ class TestPower(TestCase): assert_complex_equal(np.power(zero, -p), cnan) assert_complex_equal(np.power(zero, -1+0.2j), cnan) - def test_fast_power(self): - x=np.array([1,2,3], np.int16) - assert (x**2.00001).dtype is (x**2.0).dtype + def test_fast_power(self): + x = np.array([1,2,3], np.int16) + assert_((x**2.00001).dtype is (x**2.0).dtype) class TestLog2(TestCase): def test_log2_values(self) : diff --git a/numpy/core/tests/test_umath_complex.py b/numpy/core/tests/test_umath_complex.py index 9500056bc..34977e683 100644 --- a/numpy/core/tests/test_umath_complex.py +++ b/numpy/core/tests/test_umath_complex.py @@ -400,7 +400,7 @@ class TestCpow(TestCase): def test_scalar(self): x = np.array([1, 1j, 2, 2.5+.37j, np.inf, np.nan]) y = np.array([1, 1j, -0.5+1.5j, -0.5+1.5j, 2, 3]) - lx = range(len(x)) + lx = list(range(len(x))) # Compute the values for complex type in python p_r = [complex(x[i]) ** complex(y[i]) for i in lx] # Substitute a result allowed by C99 standard @@ -413,7 +413,7 @@ class TestCpow(TestCase): def test_array(self): x = np.array([1, 1j, 2, 2.5+.37j, np.inf, np.nan]) y = np.array([1, 1j, -0.5+1.5j, -0.5+1.5j, 2, 3]) - lx = range(len(x)) + lx = list(range(len(x))) # Compute the values for complex type in python p_r = [complex(x[i]) ** complex(y[i]) for i in lx] # Substitute a result allowed by C99 standard diff --git a/numpy/distutils/exec_command.py b/numpy/distutils/exec_command.py index f05b56429..a469fc5e0 100644 --- a/numpy/distutils/exec_command.py +++ b/numpy/distutils/exec_command.py @@ -141,6 +141,19 @@ def _update_environment( **env ): for name,value in env.items(): os.environ[name] = value or '' +def _supports_fileno(stream): + """ + Returns True if 'stream' supports the file descriptor and allows fileno(). + """ + if hasattr(stream, 'fileno'): + try: + r = stream.fileno() + return True + except IOError: + return False + else: + return False + def exec_command( command, execute_in='', use_shell=None, use_tee = None, _with_python = 1, @@ -195,7 +208,8 @@ def exec_command( command, # _exec_command_posix uses os.system and is faster # but not on all platforms os.system will return # a correct status. - if _with_python and sys.stdout.fileno() == -1: + if (_with_python and _supports_fileno(sys.stdout) and + sys.stdout.fileno() == -1): st = _exec_command_python(command, exec_command_dir = exec_dir, **env) @@ -349,12 +363,16 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): argv = [os.environ['COMSPEC'],'/C'] + argv using_command = 1 - so_fileno = sys.stdout.fileno() - se_fileno = sys.stderr.fileno() + _so_has_fileno = _supports_fileno(sys.stdout) + _se_has_fileno = _supports_fileno(sys.stderr) so_flush = sys.stdout.flush se_flush = sys.stderr.flush - so_dup = os.dup(so_fileno) - se_dup = os.dup(se_fileno) + if _so_has_fileno: + so_fileno = sys.stdout.fileno() + so_dup = os.dup(so_fileno) + if _se_has_fileno: + se_fileno = sys.stderr.fileno() + se_dup = os.dup(se_fileno) outfile = temp_file_name() fout = open(outfile,'w') @@ -371,13 +389,16 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): so_flush() se_flush() - os.dup2(fout.fileno(),so_fileno) - if using_command: - #XXX: disabled for now as it does not work from cmd under win32. - # Tests fail on msys - os.dup2(ferr.fileno(),se_fileno) - else: - os.dup2(fout.fileno(),se_fileno) + if _so_has_fileno: + os.dup2(fout.fileno(),so_fileno) + + if _se_has_fileno: + if using_command: + #XXX: disabled for now as it does not work from cmd under win32. + # Tests fail on msys + os.dup2(ferr.fileno(),se_fileno) + else: + os.dup2(fout.fileno(),se_fileno) try: status = spawn_command(os.P_WAIT,argv0,argv,os.environ) except OSError: @@ -387,8 +408,10 @@ def _exec_command( command, use_shell=None, use_tee = None, **env ): so_flush() se_flush() - os.dup2(so_dup,so_fileno) - os.dup2(se_dup,se_fileno) + if _so_has_fileno: + os.dup2(so_dup,so_fileno) + if _se_has_fileno: + os.dup2(se_dup,se_fileno) fout.close() fout = open_latin1(outfile,'r') diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index 556668264..20cacb759 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -1047,7 +1047,7 @@ class Configuration(object): pattern_list = allpath(d).split(os.sep) pattern_list.reverse() # /a/*//b/ -> /a/*/b - rl = range(len(pattern_list)-1); rl.reverse() + rl = list(range(len(pattern_list)-1)); rl.reverse() for i in rl: if not pattern_list[i]: del pattern_list[i] diff --git a/numpy/distutils/tests/test_exec_command.py b/numpy/distutils/tests/test_exec_command.py new file mode 100644 index 000000000..9301a2097 --- /dev/null +++ b/numpy/distutils/tests/test_exec_command.py @@ -0,0 +1,85 @@ +import os +import sys +import StringIO +from tempfile import TemporaryFile + +from numpy.distutils import exec_command + + +class redirect_stdout(object): + """Context manager to redirect stdout for exec_command test.""" + def __init__(self, stdout=None): + self._stdout = stdout or sys.stdout + + def __enter__(self): + self.old_stdout = sys.stdout + sys.stdout = self._stdout + + def __exit__(self, exc_type, exc_value, traceback): + self._stdout.flush() + sys.stdout = self.old_stdout + # note: closing sys.stdout won't close it. + self._stdout.close() + +class redirect_stderr(object): + """Context manager to redirect stderr for exec_command test.""" + def __init__(self, stderr=None): + self._stderr = stderr or sys.stderr + + def __enter__(self): + self.old_stderr = sys.stderr + sys.stderr = self._stderr + + def __exit__(self, exc_type, exc_value, traceback): + self._stderr.flush() + sys.stderr = self.old_stderr + # note: closing sys.stderr won't close it. + self._stderr.close() + +class emulate_nonposix(object): + """Context manager to emulate os.name != 'posix' """ + def __init__(self, osname='non-posix'): + self._new_name = osname + + def __enter__(self): + self._old_name = os.name + os.name = self._new_name + + def __exit__(self, exc_type, exc_value, traceback): + os.name = self._old_name + + +def test_exec_command_stdout(): + # Regression test for gh-2999 and gh-2915. + # There are several packages (nose, scipy.weave.inline, Sage inline + # Fortran) that replace stdout, in which case it doesn't have a fileno + # method. This is tested here, with a do-nothing command that fails if the + # presence of fileno() is assumed in exec_command. + + # The code has a special case for posix systems, so if we are on posix test + # both that the special case works and that the generic code works. + + # Test posix version: + with redirect_stdout(StringIO.StringIO()): + with redirect_stderr(TemporaryFile()): + exec_command.exec_command("cd '.'") + + if os.name == 'posix': + # Test general (non-posix) version: + with emulate_nonposix(): + with redirect_stdout(StringIO.StringIO()): + with redirect_stderr(TemporaryFile()): + exec_command.exec_command("cd '.'") + +def test_exec_command_stderr(): + # Test posix version: + with redirect_stdout(TemporaryFile(mode='w+')): + with redirect_stderr(StringIO.StringIO()): + exec_command.exec_command("cd '.'") + + if os.name == 'posix': + # Test general (non-posix) version: + with emulate_nonposix(): + with redirect_stdout(TemporaryFile()): + with redirect_stderr(StringIO.StringIO()): + exec_command.exec_command("cd '.'") diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py index 76e113a67..95ebcaee4 100755 --- a/numpy/f2py/crackfortran.py +++ b/numpy/f2py/crackfortran.py @@ -292,7 +292,7 @@ def readfortrancode(ffile,dowithline=show,istop=1): mline_mark = re.compile(r".*?'''") if istop: dowithline('',-1) ll,l1='','' - spacedigits=[' ']+map(str,range(10)) + spacedigits=[' ']+map(str,list(range(10))) filepositiontext='' fin=fileinput.FileInput(ffile) while 1: diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py index 0584ae188..215df9553 100644 --- a/numpy/f2py/tests/util.py +++ b/numpy/f2py/tests/util.py @@ -58,7 +58,7 @@ def get_module_dir(): def get_temp_module_name(): # Assume single-threaded, and the module dir usable only by this thread d = get_module_dir() - for j in xrange(5403, 9999999): + for j in range(5403, 9999999): name = "_test_ext_module_%d" % j fn = os.path.join(d, name) if name not in sys.modules and not os.path.isfile(fn+'.py'): diff --git a/numpy/fft/fftpack.py b/numpy/fft/fftpack.py index 54d071884..d229f0702 100644 --- a/numpy/fft/fftpack.py +++ b/numpy/fft/fftpack.py @@ -511,7 +511,7 @@ def _cook_nd_args(a, s=None, axes=None, invreal=0): shapeless = 0 s = list(s) if axes is None: - axes = range(-len(s), 0) + axes = list(range(-len(s), 0)) if len(s) != len(axes): raise ValueError("Shape and axes have different lengths.") if invreal and shapeless: @@ -522,7 +522,7 @@ def _cook_nd_args(a, s=None, axes=None, invreal=0): def _raw_fftnd(a, s=None, axes=None, function=fft): a = asarray(a) s, axes = _cook_nd_args(a, s, axes) - itl = range(len(axes)) + itl = list(range(len(axes))) itl.reverse() for ii in itl: a = function(a, n=s[ii], axis=axes[ii]) diff --git a/numpy/fft/helper.py b/numpy/fft/helper.py index 763d6684b..c22c92d76 100644 --- a/numpy/fft/helper.py +++ b/numpy/fft/helper.py @@ -60,7 +60,7 @@ def fftshift(x, axes=None): tmp = asarray(x) ndim = len(tmp.shape) if axes is None: - axes = range(ndim) + axes = list(range(ndim)) elif isinstance(axes, (int, nt.integer)): axes = (axes,) y = tmp @@ -108,7 +108,7 @@ def ifftshift(x, axes=None): tmp = asarray(x) ndim = len(tmp.shape) if axes is None: - axes = range(ndim) + axes = list(range(ndim)) elif isinstance(axes, (int, nt.integer)): axes = (axes,) y = tmp diff --git a/numpy/lib/arraypad.py b/numpy/lib/arraypad.py index e6ebd97e4..bfb96fc24 100644 --- a/numpy/lib/arraypad.py +++ b/numpy/lib/arraypad.py @@ -760,7 +760,7 @@ def pad(array, pad_width, mode=None, **kwargs): function = mode # Create a new padded array - rank = range(len(narray.shape)) + rank = list(range(len(narray.shape))) total_dim_increase = [np.sum(pad_width[i]) for i in rank] offset_slices = [slice(pad_width[i][0], pad_width[i][0] + narray.shape[i]) diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index cd79dd67f..540092d45 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -2589,7 +2589,7 @@ def _chbevl(x, vals): b0 = vals[0] b1 = 0.0 - for i in xrange(1,len(vals)): + for i in range(1,len(vals)): b2 = b1 b1 = b0 b0 = x*b1 - b2 + vals[i] @@ -3467,7 +3467,7 @@ def delete(arr, obj, axis=None): new[slobj] = arr[slobj2] elif isinstance(obj, slice): start, stop, step = obj.indices(N) - numtodel = len(xrange(start, stop, step)) + numtodel = len(list(range(start, stop, step))) if numtodel <= 0: if wrap: return wrap(new) diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 9c58bf747..dc57c1048 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -307,7 +307,7 @@ class AxisConcatenator(object): k2 = ndmin-tempobj.ndim if (trans1d < 0): trans1d += k2 + 1 - defaxes = range(ndmin) + defaxes = list(range(ndmin)) k1 = trans1d axes = defaxes[:k1] + defaxes[k2:] + \ defaxes[k1:k2] @@ -532,30 +532,7 @@ class ndindex(object): (2, 0, 0) (2, 1, 0) - """ - # This is a hack to handle 0-d arrays correctly. - # Fixing nditer would be more work but should be done eventually, - # and then this entire __new__ method can be removed. - def __new__(cls, *shape): - if len(shape) == 1 and isinstance(shape[0], tuple): - shape = shape[0] - if len(shape) == 0: - class zero_dim_iter(object): - def __init__(self): - self._N = 1 - def __iter__(self): - return self - def ndincr(self): - self.next() - def next(self): - if self._N > 0: - self._N -= 1 - return () - raise StopIteration - return zero_dim_iter() - else: - return super(ndindex, cls).__new__(cls) - + """ def __init__(self, *shape): if len(shape) == 1 and isinstance(shape[0], tuple): shape = shape[0] diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index ca410a24a..bd96a17f6 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -780,7 +780,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, defconv = _getconv(dtype) # Skip the first `skiprows` lines - for i in xrange(skiprows): + for i in range(skiprows): fh.next() # Read until we find a line with some values, and use @@ -804,7 +804,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters = [_getconv(dt) for dt in dtype_types] else: # All fields have the same dtype - converters = [defconv for i in xrange(N)] + converters = [defconv for i in range(N)] if N > 1: packing = [(N, tuple)] @@ -1339,7 +1339,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, DeprecationWarning) skip_header = skiprows # Skip the first `skip_header` rows - for i in xrange(skip_header): + for i in range(skip_header): fhd.next() # Keep on until we find the first valid values diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index 575f6149d..1aa8f686c 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -74,7 +74,7 @@ def apply_along_axis(func1d,axis,arr,*args): % (axis,nd)) ind = [0]*(nd-1) i = zeros(nd,'O') - indlist = range(nd) + indlist = list(range(nd)) indlist.remove(axis) i[axis] = slice(None,None) outshape = asarray(arr.shape).take(indlist) @@ -754,7 +754,7 @@ def kron(a,b): nd = nda result = outer(a,b).reshape(as_+bs) axis = nd-1 - for _ in xrange(nd): + for _ in range(nd): result = concatenate(result, axis=axis) wrapper = get_array_prepare(a, b) if wrapper is not None: diff --git a/numpy/lib/tests/test_financial.py b/numpy/lib/tests/test_financial.py index e06c50ef6..d11195f0d 100644 --- a/numpy/lib/tests/test_financial.py +++ b/numpy/lib/tests/test_financial.py @@ -124,15 +124,15 @@ class TestFinancial(TestCase): assert_almost_equal(np.nper(0.075,-2000,0,100000.,[0,1]), [ 21.5449442 , 20.76156441], 4) - assert_almost_equal(np.ipmt(0.1/12,range(5), 24, 2000), + assert_almost_equal(np.ipmt(0.1/12,list(range(5)), 24, 2000), [-17.29165168, -16.66666667, -16.03647345, -15.40102862, -14.76028842], 4) - assert_almost_equal(np.ppmt(0.1/12,range(5), 24, 2000), + assert_almost_equal(np.ppmt(0.1/12,list(range(5)), 24, 2000), [-74.998201 , -75.62318601, -76.25337923, -76.88882405, -77.52956425], 4) - assert_almost_equal(np.ppmt(0.1/12,range(5), 24, 2000, 0, + assert_almost_equal(np.ppmt(0.1/12,list(range(5)), 24, 2000, 0, [0,0,1,'end','begin']), [-74.998201 , -75.62318601, -75.62318601, -76.88882405, -76.88882405], 4) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 25c498312..73af90386 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -887,7 +887,7 @@ class TestHistogramdd(TestCase): assert_array_equal(H, answer) Z = np.zeros((5, 5, 5)) - Z[range(5), range(5), range(5)] = 1. + Z[list(range(5)), list(range(5)), list(range(5))] = 1. H, edges = histogramdd([np.arange(5), np.arange(5), np.arange(5)], 5) assert_array_equal(H, Z) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 9144138d1..ba28cf73a 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -160,7 +160,7 @@ class TestSavezLoad(RoundtripTest, TestCase): errors = [] threads = [threading.Thread(target=writer, args=(errors,)) - for j in xrange(3)] + for j in range(3)] for t in threads: t.start() for t in threads: diff --git a/numpy/lib/tests/test_twodim_base.py b/numpy/lib/tests/test_twodim_base.py index 702f9a6f5..f6b4ba4c3 100644 --- a/numpy/lib/tests/test_twodim_base.py +++ b/numpy/lib/tests/test_twodim_base.py @@ -191,7 +191,7 @@ class TestHistogram2d(TestCase): assert_array_equal(H.T, answer) H = histogram2d(x, y, xedges)[0] assert_array_equal(H.T, answer) - H,xedges,yedges = histogram2d(range(10),range(10)) + H,xedges,yedges = histogram2d(list(range(10)),list(range(10))) assert_array_equal(H, eye(10,10)) assert_array_equal(xedges, np.linspace(0,9,11)) assert_array_equal(yedges, np.linspace(0,9,11)) diff --git a/numpy/linalg/linalg.py b/numpy/linalg/linalg.py index 167e733fd..7e7833c26 100644 --- a/numpy/linalg/linalg.py +++ b/numpy/linalg/linalg.py @@ -226,7 +226,7 @@ def tensorsolve(a, b, axes=None): an = a.ndim if axes is not None: - allaxes = range(0, an) + allaxes = list(range(0, an)) for k in axes: allaxes.remove(k) allaxes.insert(an, k) diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index 857d72249..77d2dbb36 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -337,7 +337,7 @@ def apply_along_axis(func1d, axis, arr, *args, **kwargs): % (axis, nd)) ind = [0] * (nd - 1) i = np.zeros(nd, 'O') - indlist = range(nd) + indlist = list(range(nd)) indlist.remove(axis) i[axis] = slice(None, None) outshape = np.asarray(arr.shape).take(indlist) @@ -729,7 +729,7 @@ def compress_rowcols(x, axis=None): if m.all(): return nxarray([]) # Builds a list of rows/columns indices - (idxr, idxc) = (range(len(x)), range(x.shape[1])) + (idxr, idxc) = (list(range(len(x))), list(range(x.shape[1]))) masked = m.nonzero() if not axis: for i in np.unique(masked[0]): diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index cf8f1111d..a11bf6943 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -381,7 +381,7 @@ class TestMaskedArray(TestCase): def test_pickling_subbaseclass(self): "Test pickling w/ a subclass of ndarray" import cPickle - a = array(np.matrix(range(10)), mask=[1, 0, 1, 0, 0] * 2) + a = array(np.matrix(list(range(10))), mask=[1, 0, 1, 0, 0] * 2) a_pickled = cPickle.loads(a.dumps()) assert_equal(a_pickled._mask, a._mask) assert_equal(a_pickled, a) @@ -2983,11 +2983,11 @@ class TestMaskedArrayFunctions(TestCase): def test_masked_otherfunctions(self): - assert_equal(masked_inside(range(5), 1, 3), [0, 199, 199, 199, 4]) - assert_equal(masked_outside(range(5), 1, 3), [199, 1, 2, 3, 199]) - assert_equal(masked_inside(array(range(5), mask=[1, 0, 0, 0, 0]), 1, 3).mask, [1, 1, 1, 1, 0]) - assert_equal(masked_outside(array(range(5), mask=[0, 1, 0, 0, 0]), 1, 3).mask, [1, 1, 0, 0, 1]) - assert_equal(masked_equal(array(range(5), mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 0]) + assert_equal(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4]) + assert_equal(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199]) + assert_equal(masked_inside(array(list(range(5)), mask=[1, 0, 0, 0, 0]), 1, 3).mask, [1, 1, 1, 1, 0]) + assert_equal(masked_outside(array(list(range(5)), mask=[0, 1, 0, 0, 0]), 1, 3).mask, [1, 1, 0, 0, 1]) + assert_equal(masked_equal(array(list(range(5)), mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 0]) assert_equal(masked_not_equal(array([2, 2, 1, 2, 1], mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 1]) diff --git a/numpy/ma/tests/test_old_ma.py b/numpy/ma/tests/test_old_ma.py index c9dbe4d4a..b04e8ab37 100644 --- a/numpy/ma/tests/test_old_ma.py +++ b/numpy/ma/tests/test_old_ma.py @@ -388,13 +388,13 @@ class TestMa(TestCase): assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2))) assert_(eq(masked_where(equal(x, 2), x), masked_equal(x, 2))) assert_(eq(masked_where(not_equal(x, 2), x), masked_not_equal(x, 2))) - assert_(eq(masked_inside(range(5), 1, 3), [0, 199, 199, 199, 4])) - assert_(eq(masked_outside(range(5), 1, 3), [199, 1, 2, 3, 199])) - assert_(eq(masked_inside(array(range(5), mask=[1, 0, 0, 0, 0]), 1, 3).mask, + assert_(eq(masked_inside(list(range(5)), 1, 3), [0, 199, 199, 199, 4])) + assert_(eq(masked_outside(list(range(5)), 1, 3), [199, 1, 2, 3, 199])) + assert_(eq(masked_inside(array(list(range(5)), mask=[1, 0, 0, 0, 0]), 1, 3).mask, [1, 1, 1, 1, 0])) - assert_(eq(masked_outside(array(range(5), mask=[0, 1, 0, 0, 0]), 1, 3).mask, + assert_(eq(masked_outside(array(list(range(5)), mask=[0, 1, 0, 0, 0]), 1, 3).mask, [1, 1, 0, 0, 1])) - assert_(eq(masked_equal(array(range(5), mask=[1, 0, 0, 0, 0]), 2).mask, + assert_(eq(masked_equal(array(list(range(5)), mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 0])) assert_(eq(masked_not_equal(array([2, 2, 1, 2, 1], mask=[1, 0, 0, 0, 0]), 2).mask, [1, 0, 1, 0, 1])) diff --git a/numpy/matrixlib/tests/test_defmatrix.py b/numpy/matrixlib/tests/test_defmatrix.py index 74c379d77..28b563977 100644 --- a/numpy/matrixlib/tests/test_defmatrix.py +++ b/numpy/matrixlib/tests/test_defmatrix.py @@ -211,13 +211,13 @@ class TestAlgebra(TestCase): mA = matrix(A) B = identity(2) - for i in xrange(6): + for i in range(6): assert_(allclose((mA ** i).A, B)) B = dot(B, A) Ainv = linalg.inv(A) B = identity(2) - for i in xrange(6): + for i in range(6): assert_(allclose((mA ** -i).A, B)) B = dot(B, Ainv) diff --git a/numpy/numarray/functions.py b/numpy/numarray/functions.py index 7242f2870..2e12a4149 100644 --- a/numpy/numarray/functions.py +++ b/numpy/numarray/functions.py @@ -155,7 +155,7 @@ class FileSeekWarning(Warning): pass -STRICT, SLOPPY, WARN = range(3) +STRICT, SLOPPY, WARN = list(range(3)) _BLOCKSIZE=1024 @@ -204,8 +204,8 @@ def fromfile(infile, type=None, shape=None, sizing=STRICT, ##file whose size may be determined before allocation, should be ##quick -- only one allocation will be needed. - recsize = dtype.itemsize * np.product([i for i in shape if i != -1]) - blocksize = max(_BLOCKSIZE/recsize, 1)*recsize + recsize = int(dtype.itemsize * np.product([i for i in shape if i != -1])) + blocksize = max(_BLOCKSIZE//recsize, 1)*recsize ##try to estimate file size try: @@ -216,7 +216,7 @@ def fromfile(infile, type=None, shape=None, sizing=STRICT, except (AttributeError, IOError): initsize=blocksize else: - initsize=max(1,(endpos-curpos)/recsize)*recsize + initsize=max(1,(endpos-curpos)//recsize)*recsize buf = np.newbuffer(initsize) @@ -247,20 +247,33 @@ def fromfile(infile, type=None, shape=None, sizing=STRICT, except IOError: _warnings.warn("Could not rewind (IOError in seek)", FileSeekWarning) - datasize = (len(data)/recsize) * recsize + datasize = (len(data)//recsize) * recsize if len(buf) != bytesread+datasize: buf=_resizebuf(buf,bytesread+datasize) buf[bytesread:bytesread+datasize]=data[:datasize] ##deduce shape from len(buf) shape = list(shape) uidx = shape.index(-1) - shape[uidx]=len(buf) / recsize + shape[uidx]=len(buf) // recsize a = np.ndarray(shape=shape, dtype=type, buffer=buf) if a.dtype.char == '?': np.not_equal(a, 0, a) return a + +# this function is referenced in the code above but not defined. adding +# it back. - phensley +def _resizebuf(buf,newsize): + "Return a copy of BUF of size NEWSIZE." + newbuf = np.newbuffer(newsize) + if newsize > len(buf): + newbuf[:len(buf)]=buf + else: + newbuf[:]=buf[:len(newbuf)] + return newbuf + + def fromstring(datastring, type=None, shape=None, typecode=None, dtype=None): dtype = type2dtype(typecode, type, dtype, True) if shape is None: @@ -405,7 +418,7 @@ def put(array, indices, values, axis=0, clipmode=RAISE): work[indices] = values work = work.swapaxes(0, axis) else: - def_axes = range(work.ndim) + def_axes = list(range(work.ndim)) for x in axis: def_axes.remove(x) axis = list(axis)+def_axes @@ -441,7 +454,7 @@ def take(array, indices, axis=0, outarr=None, clipmode=RAISE): return res return else: - def_axes = range(array.ndim) + def_axes = list(range(array.ndim)) for x in axis: def_axes.remove(x) axis = list(axis) + def_axes diff --git a/numpy/oldnumeric/__init__.py b/numpy/oldnumeric/__init__.py index 5fc8f7c76..68bd39e45 100644 --- a/numpy/oldnumeric/__init__.py +++ b/numpy/oldnumeric/__init__.py @@ -11,7 +11,7 @@ def _move_axis_to_0(a, axis): n = len(a.shape) if axis < 0: axis += n - axes = range(1, axis+1) + [0,] + range(axis+1, n) + axes = list(range(1, axis+1)) + [0,] + list(range(axis+1, n)) return transpose(a, axes) # Add these diff --git a/numpy/polynomial/tests/test_chebyshev.py b/numpy/polynomial/tests/test_chebyshev.py index 95da83631..b5f1d5672 100644 --- a/numpy/polynomial/tests/test_chebyshev.py +++ b/numpy/polynomial/tests/test_chebyshev.py @@ -265,7 +265,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1, k=[k]) - res = cheb.chebint(pol, m=j, k=range(j)) + res = cheb.chebint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd @@ -275,7 +275,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1, k=[k], lbnd=-1) - res = cheb.chebint(pol, m=j, k=range(j), lbnd=-1) + res = cheb.chebint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling @@ -285,7 +285,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = cheb.chebint(tgt, m=1, k=[k], scl=2) - res = cheb.chebint(pol, m=j, k=range(j), scl=2) + res = cheb.chebint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) def test_chebint_axis(self): diff --git a/numpy/polynomial/tests/test_hermite.py b/numpy/polynomial/tests/test_hermite.py index a0ef3b515..36fa6f0e4 100644 --- a/numpy/polynomial/tests/test_hermite.py +++ b/numpy/polynomial/tests/test_hermite.py @@ -255,7 +255,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = herm.hermint(tgt, m=1, k=[k]) - res = herm.hermint(pol, m=j, k=range(j)) + res = herm.hermint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd @@ -265,7 +265,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = herm.hermint(tgt, m=1, k=[k], lbnd=-1) - res = herm.hermint(pol, m=j, k=range(j), lbnd=-1) + res = herm.hermint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling @@ -275,7 +275,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = herm.hermint(tgt, m=1, k=[k], scl=2) - res = herm.hermint(pol, m=j, k=range(j), scl=2) + res = herm.hermint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) def test_hermint_axis(self): diff --git a/numpy/polynomial/tests/test_hermite_e.py b/numpy/polynomial/tests/test_hermite_e.py index f6bfe5e5e..2fcef55da 100644 --- a/numpy/polynomial/tests/test_hermite_e.py +++ b/numpy/polynomial/tests/test_hermite_e.py @@ -252,7 +252,7 @@ class TestIntegral(TestCase): tgt = pol[:] for k in range(j) : tgt = herme.hermeint(tgt, m=1, k=[k]) - res = herme.hermeint(pol, m=j, k=range(j)) + res = herme.hermeint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd @@ -262,7 +262,7 @@ class TestIntegral(TestCase): tgt = pol[:] for k in range(j) : tgt = herme.hermeint(tgt, m=1, k=[k], lbnd=-1) - res = herme.hermeint(pol, m=j, k=range(j), lbnd=-1) + res = herme.hermeint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling @@ -272,7 +272,7 @@ class TestIntegral(TestCase): tgt = pol[:] for k in range(j) : tgt = herme.hermeint(tgt, m=1, k=[k], scl=2) - res = herme.hermeint(pol, m=j, k=range(j), scl=2) + res = herme.hermeint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) def test_hermeint_axis(self): diff --git a/numpy/polynomial/tests/test_laguerre.py b/numpy/polynomial/tests/test_laguerre.py index b6e0376a2..915234b93 100644 --- a/numpy/polynomial/tests/test_laguerre.py +++ b/numpy/polynomial/tests/test_laguerre.py @@ -250,7 +250,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = lag.lagint(tgt, m=1, k=[k]) - res = lag.lagint(pol, m=j, k=range(j)) + res = lag.lagint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd @@ -260,7 +260,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = lag.lagint(tgt, m=1, k=[k], lbnd=-1) - res = lag.lagint(pol, m=j, k=range(j), lbnd=-1) + res = lag.lagint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling @@ -270,7 +270,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = lag.lagint(tgt, m=1, k=[k], scl=2) - res = lag.lagint(pol, m=j, k=range(j), scl=2) + res = lag.lagint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) def test_lagint_axis(self): diff --git a/numpy/polynomial/tests/test_legendre.py b/numpy/polynomial/tests/test_legendre.py index 3180db907..500269484 100644 --- a/numpy/polynomial/tests/test_legendre.py +++ b/numpy/polynomial/tests/test_legendre.py @@ -254,7 +254,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k]) - res = leg.legint(pol, m=j, k=range(j)) + res = leg.legint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd @@ -264,7 +264,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k], lbnd=-1) - res = leg.legint(pol, m=j, k=range(j), lbnd=-1) + res = leg.legint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling @@ -274,7 +274,7 @@ class TestIntegral(TestCase) : tgt = pol[:] for k in range(j) : tgt = leg.legint(tgt, m=1, k=[k], scl=2) - res = leg.legint(pol, m=j, k=range(j), scl=2) + res = leg.legint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) def test_legint_axis(self): diff --git a/numpy/polynomial/tests/test_polynomial.py b/numpy/polynomial/tests/test_polynomial.py index bd09c07f6..e8268d25f 100644 --- a/numpy/polynomial/tests/test_polynomial.py +++ b/numpy/polynomial/tests/test_polynomial.py @@ -253,7 +253,7 @@ class TestIntegral(TestCase): tgt = pol[:] for k in range(j) : tgt = poly.polyint(tgt, m=1, k=[k]) - res = poly.polyint(pol, m=j, k=range(j)) + res = poly.polyint(pol, m=j, k=list(range(j))) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with lbnd @@ -263,7 +263,7 @@ class TestIntegral(TestCase): tgt = pol[:] for k in range(j) : tgt = poly.polyint(tgt, m=1, k=[k], lbnd=-1) - res = poly.polyint(pol, m=j, k=range(j), lbnd=-1) + res = poly.polyint(pol, m=j, k=list(range(j)), lbnd=-1) assert_almost_equal(trim(res), trim(tgt)) # check multiple integrations with scaling @@ -273,7 +273,7 @@ class TestIntegral(TestCase): tgt = pol[:] for k in range(j) : tgt = poly.polyint(tgt, m=1, k=[k], scl=2) - res = poly.polyint(pol, m=j, k=range(j), scl=2) + res = poly.polyint(pol, m=j, k=list(range(j)), scl=2) assert_almost_equal(trim(res), trim(tgt)) def test_polyint_axis(self): diff --git a/numpy/testing/tests/test_decorators.py b/numpy/testing/tests/test_decorators.py index e2dc2bd7d..22d39e9a6 100644 --- a/numpy/testing/tests/test_decorators.py +++ b/numpy/testing/tests/test_decorators.py @@ -88,7 +88,7 @@ def test_skip_functions_callable(): def test_skip_generators_hardcoded(): @dec.knownfailureif(True, "This test is known to fail") def g1(x): - for i in xrange(x): + for i in range(x): yield i try: @@ -102,7 +102,7 @@ def test_skip_generators_hardcoded(): @dec.knownfailureif(False, "This test is NOT known to fail") def g2(x): - for i in xrange(x): + for i in range(x): yield i raise DidntSkipException('FAIL') @@ -121,7 +121,7 @@ def test_skip_generators_callable(): @dec.knownfailureif(skip_tester, "This test is known to fail") def g1(x): - for i in xrange(x): + for i in range(x): yield i try: @@ -136,7 +136,7 @@ def test_skip_generators_callable(): @dec.knownfailureif(skip_tester, "This test is NOT known to fail") def g2(x): - for i in xrange(x): + for i in range(x): yield i raise DidntSkipException('FAIL') diff --git a/numpy/testing/tests/test_utils.py b/numpy/testing/tests/test_utils.py index 9722c583c..4dd5a8121 100644 --- a/numpy/testing/tests/test_utils.py +++ b/numpy/testing/tests/test_utils.py @@ -378,7 +378,7 @@ class TestArrayAlmostEqualNulp(unittest.TestCase): @dec.knownfailureif(True, "Github issue #347") def test_simple(self): np.random.seed(12345) - for i in xrange(100): + for i in range(100): dev = np.random.randn(10) x = np.ones(10) y = x + dev * np.finfo(np.float64).eps diff --git a/tools/py3tool.py b/tools/py3tool.py index 5e2ffbe58..ef175ebfd 100755 --- a/tools/py3tool.py +++ b/tools/py3tool.py @@ -79,7 +79,8 @@ FIXES_TO_SKIP = [ 'apply', 'input', 'raw_input', - 'xreadlines' + 'xreadlines', + 'xrange' ] skip_fixes= [] |