diff options
Diffstat (limited to 'numpy')
58 files changed, 423 insertions, 324 deletions
diff --git a/numpy/__init__.py b/numpy/__init__.py index 1d8570f71..2d3423c56 100644 --- a/numpy/__init__.py +++ b/numpy/__init__.py @@ -153,13 +153,12 @@ else: from . import ma from . import matrixlib as _mat from .matrixlib import * - from .compat import long # Make these accessible from numpy name-space # but not imported in from numpy import * # TODO[gh-6103]: Deprecate these from builtins import bool, int, float, complex, object, str - unicode = str + from .compat import long, unicode from .core import round, abs, max, min # now that numpy modules are imported, can initialize limits diff --git a/numpy/core/_add_newdocs.py b/numpy/core/_add_newdocs.py index ba5c0fa68..18ab10078 100644 --- a/numpy/core/_add_newdocs.py +++ b/numpy/core/_add_newdocs.py @@ -152,6 +152,8 @@ add_newdoc('numpy.core', 'flatiter', ('copy', add_newdoc('numpy.core', 'nditer', """ + nditer(op, flags=None, op_flags=None, op_dtypes=None, order='K', casting='safe', op_axes=None, itershape=None, buffersize=0) + Efficient multi-dimensional iterator object to iterate over arrays. To get started using this object, see the :ref:`introductory guide to array iteration <arrays.nditer>`. @@ -784,7 +786,7 @@ add_newdoc('numpy.core', 'broadcast', ('reset', add_newdoc('numpy.core.multiarray', 'array', """ - array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0) + array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0) Create an array. @@ -3930,8 +3932,8 @@ add_newdoc('numpy.core.multiarray', 'ndarray', ('tolist', """)) -tobytesdoc = """ - a.{name}(order='C') +add_newdoc('numpy.core.multiarray', 'ndarray', ('tobytes', """ + a.tobytes(order='C') Construct Python bytes containing the raw data bytes in the array. @@ -3941,11 +3943,11 @@ tobytesdoc = """ unless the F_CONTIGUOUS flag in the array is set, in which case it means 'Fortran' order. - {deprecated} + .. versionadded:: 1.9.0 Parameters ---------- - order : {{'C', 'F', None}}, optional + order : {'C', 'F', None}, optional Order of the data for multidimensional arrays: C, Fortran, or the same as for the original array. @@ -3964,18 +3966,19 @@ tobytesdoc = """ >>> x.tobytes('F') b'\\x00\\x00\\x02\\x00\\x01\\x00\\x03\\x00' - """ + """)) + + +add_newdoc('numpy.core.multiarray', 'ndarray', ('tostring', r""" + a.tostring(order='C') + + A compatibility alias for `tobytes`, with exactly the same behavior. + + Despite its name, it returns `bytes` not `str`\ s. + + .. deprecated:: 1.19.0 + """)) -add_newdoc('numpy.core.multiarray', 'ndarray', - ('tostring', tobytesdoc.format(name='tostring', - deprecated= - 'This function is a compatibility ' - 'alias for tobytes. Despite its ' - 'name it returns bytes not ' - 'strings.'))) -add_newdoc('numpy.core.multiarray', 'ndarray', - ('tobytes', tobytesdoc.format(name='tobytes', - deprecated='.. versionadded:: 1.9.0'))) add_newdoc('numpy.core.multiarray', 'ndarray', ('trace', """ diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index f21774cb6..1378497bb 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -9,7 +9,6 @@ import re import sys import platform -from numpy.compat import unicode from .multiarray import dtype, array, ndarray try: import ctypes @@ -365,7 +364,7 @@ def _newnames(datatype, order): """ oldnames = datatype.names nameslist = list(oldnames) - if isinstance(order, (str, unicode)): + if isinstance(order, str): order = [order] seen = set() if isinstance(order, (list, tuple)): diff --git a/numpy/core/_methods.py b/numpy/core/_methods.py index 6785683a3..86ddf4d17 100644 --- a/numpy/core/_methods.py +++ b/numpy/core/_methods.py @@ -63,7 +63,7 @@ def _count_reduce_items(arr, axis): axis = (axis,) items = 1 for ax in axis: - items *= arr.shape[ax] + items *= arr.shape[mu.normalize_axis_index(ax, arr.ndim)] return items # Numpy 1.17.0, 2019-02-24 diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py index 1292b738c..cd01c0e77 100644 --- a/numpy/core/defchararray.py +++ b/numpy/core/defchararray.py @@ -24,7 +24,7 @@ from .numeric import array as narray from numpy.core.multiarray import _vec_string from numpy.core.overrides import set_module from numpy.core import overrides -from numpy.compat import asbytes, long +from numpy.compat import asbytes import numpy __all__ = [ @@ -340,7 +340,7 @@ def multiply(a, i): i_arr = numpy.asarray(i) if not issubclass(i_arr.dtype.type, integer): raise ValueError("Can only multiply by integers") - out_size = _get_num_chars(a_arr) * max(long(i_arr.max()), 0) + out_size = _get_num_chars(a_arr) * max(int(i_arr.max()), 0) return _vec_string( a_arr, (a_arr.dtype.type, out_size), '__mul__', (i_arr,)) @@ -450,7 +450,7 @@ def center(a, width, fillchar=' '): """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) + size = int(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( @@ -995,7 +995,7 @@ def ljust(a, width, fillchar=' '): """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) + size = int(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( @@ -1265,7 +1265,7 @@ def rjust(a, width, fillchar=' '): """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) + size = int(numpy.max(width_arr.flat)) if numpy.issubdtype(a_arr.dtype, numpy.string_): fillchar = asbytes(fillchar) return _vec_string( @@ -1733,7 +1733,7 @@ def zfill(a, width): """ a_arr = numpy.asarray(a) width_arr = numpy.asarray(width) - size = long(numpy.max(width_arr.flat)) + size = int(numpy.max(width_arr.flat)) return _vec_string( a_arr, (a_arr.dtype.type, size), 'zfill', (width_arr,)) @@ -1952,10 +1952,10 @@ class chararray(ndarray): else: dtype = string_ - # force itemsize to be a Python long, since using NumPy integer + # force itemsize to be a Python int, since using NumPy integer # types results in itemsize.itemsize being used as the size of # strings in the new array. - itemsize = long(itemsize) + itemsize = int(itemsize) if isinstance(buffer, str): # unicode objects do not have the buffer interface @@ -2720,7 +2720,7 @@ def array(obj, itemsize=None, copy=True, unicode=None, order=None): (itemsize != obj.itemsize) or (not unicode and isinstance(obj, unicode_)) or (unicode and isinstance(obj, string_))): - obj = obj.astype((dtype, long(itemsize))) + obj = obj.astype((dtype, int(itemsize))) return obj if isinstance(obj, ndarray) and issubclass(obj.dtype.type, object): diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 61b8ba3ac..ad66446c2 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -1,7 +1,7 @@ import numpy as np from .numeric import uint8, ndarray, dtype from numpy.compat import ( - long, os_fspath, contextlib_nullcontext, is_pathlib_path + os_fspath, contextlib_nullcontext, is_pathlib_path ) from numpy.core.overrides import set_module @@ -242,7 +242,7 @@ class memmap(ndarray): for k in shape: size *= k - bytes = long(offset + size*_dbytes) + bytes = int(offset + size*_dbytes) if mode in ('w+', 'r+') and flen < bytes: fid.seek(bytes - 1, 0) diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 5e8151e68..83d985a7c 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -384,12 +384,12 @@ def full_like(a, fill_value, dtype=None, order='K', subok=True, shape=None): return res -def _count_nonzero_dispatcher(a, axis=None): +def _count_nonzero_dispatcher(a, axis=None, *, keepdims=None): return (a,) @array_function_dispatch(_count_nonzero_dispatcher) -def count_nonzero(a, axis=None): +def count_nonzero(a, axis=None, *, keepdims=False): """ Counts the number of non-zero values in the array ``a``. @@ -414,6 +414,13 @@ def count_nonzero(a, axis=None): .. versionadded:: 1.12.0 + keepdims : bool, optional + If this is set to True, the axes that are counted are left + in the result as dimensions with size one. With this option, + the result will broadcast correctly against the input array. + + .. versionadded:: 1.19.0 + Returns ------- count : int or array of int @@ -429,15 +436,19 @@ def count_nonzero(a, axis=None): -------- >>> np.count_nonzero(np.eye(4)) 4 - >>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]]) + >>> a = np.array([[0, 1, 7, 0], + ... [3, 0, 2, 19]]) + >>> np.count_nonzero(a) 5 - >>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=0) - array([1, 1, 1, 1, 1]) - >>> np.count_nonzero([[0,1,7,0,0],[3,0,0,2,19]], axis=1) + >>> np.count_nonzero(a, axis=0) + array([1, 1, 2, 1]) + >>> np.count_nonzero(a, axis=1) array([2, 3]) - + >>> np.count_nonzero(a, axis=1, keepdims=True) + array([[2], + [3]]) """ - if axis is None: + if axis is None and not keepdims: return multiarray.count_nonzero(a) a = asanyarray(a) @@ -448,7 +459,7 @@ def count_nonzero(a, axis=None): else: a_bool = a.astype(np.bool_, copy=False) - return a_bool.sum(axis=axis, dtype=np.intp) + return a_bool.sum(axis=axis, dtype=np.intp, keepdims=keepdims) @set_module('numpy') diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py index 9d7bbde93..aac741612 100644 --- a/numpy/core/numerictypes.py +++ b/numpy/core/numerictypes.py @@ -83,7 +83,6 @@ import types as _types import numbers import warnings -from numpy.compat import bytes, long from numpy.core.multiarray import ( typeinfo, ndarray, array, empty, dtype, datetime_data, datetime_as_string, busday_offset, busday_count, is_busday, @@ -119,8 +118,8 @@ from ._dtype import _kind_name # we don't export these for import *, but we do want them accessible # as numerictypes.bool, etc. -from builtins import bool, int, float, complex, object, str -unicode = str +from builtins import bool, int, float, complex, object, str, bytes +from numpy.compat import long, unicode # We use this later diff --git a/numpy/core/records.py b/numpy/core/records.py index ddefa5881..b1ee1aa11 100644 --- a/numpy/core/records.py +++ b/numpy/core/records.py @@ -40,7 +40,7 @@ from collections import Counter, OrderedDict from . import numeric as sb from . import numerictypes as nt from numpy.compat import ( - isfileobj, bytes, long, unicode, os_fspath, contextlib_nullcontext + isfileobj, os_fspath, contextlib_nullcontext ) from numpy.core.overrides import set_module from .arrayprint import get_printoptions @@ -188,7 +188,7 @@ class format_parser: if names: if type(names) in [list, tuple]: pass - elif isinstance(names, (str, unicode)): + elif isinstance(names, str): names = names.split(',') else: raise NameError("illegal input names %s" % repr(names)) @@ -703,7 +703,7 @@ def fromrecords(recList, dtype=None, shape=None, formats=None, names=None, shape = _deprecate_shape_0_as_None(shape) if shape is None: shape = len(recList) - if isinstance(shape, (int, long)): + if isinstance(shape, int): shape = (shape,) if len(shape) > 1: raise ValueError("Can only deal with 1-d array.") @@ -792,7 +792,7 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None, if shape is None: shape = (-1,) - elif isinstance(shape, (int, long)): + elif isinstance(shape, int): shape = (shape,) if isfileobj(fd): diff --git a/numpy/core/src/multiarray/arrayobject.c b/numpy/core/src/multiarray/arrayobject.c index 16896aa12..dedaf38eb 100644 --- a/numpy/core/src/multiarray/arrayobject.c +++ b/numpy/core/src/multiarray/arrayobject.c @@ -41,6 +41,7 @@ maintainer email: oliphant.travis@ieee.org #include "arraytypes.h" #include "scalartypes.h" #include "arrayobject.h" +#include "conversion_utils.h" #include "ctors.h" #include "methods.h" #include "descriptor.h" @@ -1624,7 +1625,7 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) PyArray_Descr *descr = NULL; int itemsize; PyArray_Dims dims = {NULL, 0}; - PyArray_Dims strides = {NULL, 0}; + PyArray_Dims strides = {NULL, -1}; PyArray_Chunk buffer; npy_longlong offset = 0; NPY_ORDER order = NPY_CORDER; @@ -1645,7 +1646,7 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) PyArray_BufferConverter, &buffer, &offset, - &PyArray_IntpConverter, + &PyArray_OptionalIntpConverter, &strides, &PyArray_OrderConverter, &order)) { @@ -1660,7 +1661,7 @@ array_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) itemsize = descr->elsize; - if (strides.ptr != NULL) { + if (strides.len != -1) { npy_intp nb, off; if (strides.len != dims.len) { PyErr_SetString(PyExc_ValueError, diff --git a/numpy/core/src/multiarray/conversion_utils.c b/numpy/core/src/multiarray/conversion_utils.c index 484a13134..260ae7080 100644 --- a/numpy/core/src/multiarray/conversion_utils.c +++ b/numpy/core/src/multiarray/conversion_utils.c @@ -137,6 +137,20 @@ PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq) return NPY_SUCCEED; } +/* + * Like PyArray_IntpConverter, but leaves `seq` untouched if `None` is passed + * rather than treating `None` as `()`. + */ +NPY_NO_EXPORT int +PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq) +{ + if (obj == Py_None) { + return NPY_SUCCEED; + } + + return PyArray_IntpConverter(obj, seq); +} + /*NUMPY_API * Get buffer chunk from object * diff --git a/numpy/core/src/multiarray/conversion_utils.h b/numpy/core/src/multiarray/conversion_utils.h index 9bf712c3b..bee0c6064 100644 --- a/numpy/core/src/multiarray/conversion_utils.h +++ b/numpy/core/src/multiarray/conversion_utils.h @@ -7,6 +7,9 @@ NPY_NO_EXPORT int PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq); NPY_NO_EXPORT int +PyArray_OptionalIntpConverter(PyObject *obj, PyArray_Dims *seq); + +NPY_NO_EXPORT int PyArray_BufferConverter(PyObject *obj, PyArray_Chunk *buf); NPY_NO_EXPORT int diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index a23510c8b..12bf9eace 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -1211,8 +1211,8 @@ PyArray_NewFromDescrAndBase( * NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise. * NPY_KEEPORDER - Keeps the axis ordering of prototype. * dtype - If not NULL, overrides the data type of the result. - * ndim - If not 0 and dims not NULL, overrides the shape of the result. - * dims - If not NULL and ndim not 0, overrides the shape of the result. + * ndim - If not -1, overrides the shape of the result. + * dims - If ndim is not -1, overrides the shape of the result. * subok - If 1, use the prototype's array subtype, otherwise * always create a base-class array. * @@ -1225,7 +1225,7 @@ PyArray_NewLikeArrayWithShape(PyArrayObject *prototype, NPY_ORDER order, { PyObject *ret = NULL; - if (dims == NULL) { + if (ndim == -1) { ndim = PyArray_NDIM(prototype); dims = PyArray_DIMS(prototype); } @@ -1322,7 +1322,7 @@ NPY_NO_EXPORT PyObject * PyArray_NewLikeArray(PyArrayObject *prototype, NPY_ORDER order, PyArray_Descr *dtype, int subok) { - return PyArray_NewLikeArrayWithShape(prototype, order, dtype, 0, NULL, subok); + return PyArray_NewLikeArrayWithShape(prototype, order, dtype, -1, NULL, subok); } /*NUMPY_API diff --git a/numpy/core/src/multiarray/dtype_transfer.c b/numpy/core/src/multiarray/dtype_transfer.c index b26d5ac89..ecaa680ec 100644 --- a/numpy/core/src/multiarray/dtype_transfer.c +++ b/numpy/core/src/multiarray/dtype_transfer.c @@ -312,6 +312,7 @@ typedef struct { NpyAuxData *wrappeddata, *todata, *fromdata; npy_intp src_itemsize, dst_itemsize; char *bufferin, *bufferout; + npy_bool init_dest, out_needs_api; } _align_wrap_data; /* transfer data free function */ @@ -372,6 +373,9 @@ static NpyAuxData *_align_wrap_data_clone(NpyAuxData *data) } } + newdata->init_dest = d->init_dest; + newdata->out_needs_api = d->out_needs_api; + return (NpyAuxData *)newdata; } @@ -391,57 +395,26 @@ _strided_to_strided_contig_align_wrap(char *dst, npy_intp dst_stride, *todata = d->todata, *fromdata = d->fromdata; char *bufferin = d->bufferin, *bufferout = d->bufferout; + npy_bool init_dest = d->init_dest, out_needs_api = d->out_needs_api; for(;;) { - if (N > NPY_LOWLEVEL_BUFFER_BLOCKSIZE) { - tobuffer(bufferin, inner_src_itemsize, src, src_stride, - NPY_LOWLEVEL_BUFFER_BLOCKSIZE, - src_itemsize, todata); - wrapped(bufferout, dst_itemsize, bufferin, inner_src_itemsize, - NPY_LOWLEVEL_BUFFER_BLOCKSIZE, - inner_src_itemsize, wrappeddata); - frombuffer(dst, dst_stride, bufferout, dst_itemsize, - NPY_LOWLEVEL_BUFFER_BLOCKSIZE, - dst_itemsize, fromdata); - N -= NPY_LOWLEVEL_BUFFER_BLOCKSIZE; - src += NPY_LOWLEVEL_BUFFER_BLOCKSIZE*src_stride; - dst += NPY_LOWLEVEL_BUFFER_BLOCKSIZE*dst_stride; - } - else { - tobuffer(bufferin, inner_src_itemsize, src, src_stride, N, - src_itemsize, todata); - wrapped(bufferout, dst_itemsize, bufferin, inner_src_itemsize, N, - inner_src_itemsize, wrappeddata); - frombuffer(dst, dst_stride, bufferout, dst_itemsize, N, - dst_itemsize, fromdata); + /* + * The caller does not know if a previous call resulted in a Python + * exception. Much of the Python API is unsafe while an exception is in + * flight, so just skip all the work. Someone higher in the call stack + * will check for errors and propagate them. + */ + if (out_needs_api && PyErr_Occurred()) { return; } - } -} - -static void -_strided_to_strided_contig_align_wrap_init_dest(char *dst, npy_intp dst_stride, - char *src, npy_intp src_stride, - npy_intp N, npy_intp src_itemsize, - NpyAuxData *data) -{ - _align_wrap_data *d = (_align_wrap_data *)data; - PyArray_StridedUnaryOp *wrapped = d->wrapped, - *tobuffer = d->tobuffer, - *frombuffer = d->frombuffer; - npy_intp inner_src_itemsize = d->src_itemsize, - dst_itemsize = d->dst_itemsize; - NpyAuxData *wrappeddata = d->wrappeddata, - *todata = d->todata, - *fromdata = d->fromdata; - char *bufferin = d->bufferin, *bufferout = d->bufferout; - - for(;;) { if (N > NPY_LOWLEVEL_BUFFER_BLOCKSIZE) { tobuffer(bufferin, inner_src_itemsize, src, src_stride, NPY_LOWLEVEL_BUFFER_BLOCKSIZE, src_itemsize, todata); - memset(bufferout, 0, dst_itemsize*NPY_LOWLEVEL_BUFFER_BLOCKSIZE); + if (init_dest) { + memset(bufferout, 0, + dst_itemsize*NPY_LOWLEVEL_BUFFER_BLOCKSIZE); + } wrapped(bufferout, dst_itemsize, bufferin, inner_src_itemsize, NPY_LOWLEVEL_BUFFER_BLOCKSIZE, inner_src_itemsize, wrappeddata); @@ -455,7 +428,9 @@ _strided_to_strided_contig_align_wrap_init_dest(char *dst, npy_intp dst_stride, else { tobuffer(bufferin, inner_src_itemsize, src, src_stride, N, src_itemsize, todata); - memset(bufferout, 0, dst_itemsize*N); + if (init_dest) { + memset(bufferout, 0, dst_itemsize*N); + } wrapped(bufferout, dst_itemsize, bufferin, inner_src_itemsize, N, inner_src_itemsize, wrappeddata); frombuffer(dst, dst_stride, bufferout, dst_itemsize, N, @@ -477,6 +452,7 @@ _strided_to_strided_contig_align_wrap_init_dest(char *dst, npy_intp dst_stride, * wrapped - contig to contig transfer function being wrapped * wrappeddata - data for wrapped * init_dest - 1 means to memset the dest buffer to 0 before calling wrapped. + * out_needs_api - if NPY_TRUE, check for (and break on) Python API errors. * * Returns NPY_SUCCEED or NPY_FAIL. */ @@ -487,6 +463,7 @@ wrap_aligned_contig_transfer_function( PyArray_StridedUnaryOp *frombuffer, NpyAuxData *fromdata, PyArray_StridedUnaryOp *wrapped, NpyAuxData *wrappeddata, int init_dest, + int out_needs_api, PyArray_StridedUnaryOp **out_stransfer, NpyAuxData **out_transferdata) { @@ -519,14 +496,11 @@ wrap_aligned_contig_transfer_function( data->bufferin = (char *)data + basedatasize; data->bufferout = data->bufferin + NPY_LOWLEVEL_BUFFER_BLOCKSIZE*src_itemsize; + data->init_dest = (npy_bool) init_dest; + data->out_needs_api = (npy_bool) out_needs_api; /* Set the function and data */ - if (init_dest) { - *out_stransfer = &_strided_to_strided_contig_align_wrap_init_dest; - } - else { - *out_stransfer = &_strided_to_strided_contig_align_wrap; - } + *out_stransfer = &_strided_to_strided_contig_align_wrap; *out_transferdata = (NpyAuxData *)data; return NPY_SUCCEED; @@ -1171,6 +1145,7 @@ get_datetime_to_unicode_transfer_function(int aligned, frombuffer, fromdata, caststransfer, castdata, PyDataType_FLAGCHK(str_dtype, NPY_NEEDS_INIT), + *out_needs_api, out_stransfer, out_transferdata) != NPY_SUCCEED) { NPY_AUXDATA_FREE(castdata); NPY_AUXDATA_FREE(todata); @@ -1293,6 +1268,7 @@ get_unicode_to_datetime_transfer_function(int aligned, frombuffer, fromdata, caststransfer, castdata, PyDataType_FLAGCHK(dst_dtype, NPY_NEEDS_INIT), + *out_needs_api, out_stransfer, out_transferdata) != NPY_SUCCEED) { Py_DECREF(str_dtype); NPY_AUXDATA_FREE(castdata); @@ -1613,6 +1589,7 @@ get_cast_transfer_function(int aligned, frombuffer, fromdata, caststransfer, castdata, PyDataType_FLAGCHK(dst_dtype, NPY_NEEDS_INIT), + *out_needs_api, out_stransfer, out_transferdata) != NPY_SUCCEED) { NPY_AUXDATA_FREE(castdata); NPY_AUXDATA_FREE(todata); diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c index 8c1b7f943..80a1cd4a1 100644 --- a/numpy/core/src/multiarray/getset.c +++ b/numpy/core/src/multiarray/getset.c @@ -13,6 +13,7 @@ #include "npy_import.h" #include "common.h" +#include "conversion_utils.h" #include "ctors.h" #include "scalartypes.h" #include "descriptor.h" @@ -110,7 +111,7 @@ array_strides_get(PyArrayObject *self) static int array_strides_set(PyArrayObject *self, PyObject *obj) { - PyArray_Dims newstrides = {NULL, 0}; + PyArray_Dims newstrides = {NULL, -1}; PyArrayObject *new; npy_intp numbytes = 0; npy_intp offset = 0; @@ -123,8 +124,8 @@ array_strides_set(PyArrayObject *self, PyObject *obj) "Cannot delete array strides"); return -1; } - if (!PyArray_IntpConverter(obj, &newstrides) || - newstrides.ptr == NULL) { + if (!PyArray_OptionalIntpConverter(obj, &newstrides) || + newstrides.len == -1) { PyErr_SetString(PyExc_TypeError, "invalid strides"); return -1; } diff --git a/numpy/core/src/multiarray/mapping.c b/numpy/core/src/multiarray/mapping.c index 8a90d1767..7047304eb 100644 --- a/numpy/core/src/multiarray/mapping.c +++ b/numpy/core/src/multiarray/mapping.c @@ -727,8 +727,11 @@ prepare_index(PyArrayObject *self, PyObject *index, } } else if (used_ndim > PyArray_NDIM(self)) { - PyErr_SetString(PyExc_IndexError, - "too many indices for array"); + PyErr_Format(PyExc_IndexError, + "too many indices for array: " + "array is %d-dimensional, but %d were indexed", + PyArray_NDIM(self), + used_ndim); goto failed_building_indices; } else if (index_ndim == 0) { diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index ebdf8a4cd..7bfbeca15 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -566,6 +566,23 @@ array_tobytes(PyArrayObject *self, PyObject *args, PyObject *kwds) return PyArray_ToString(self, order); } +static PyObject * +array_tostring(PyArrayObject *self, PyObject *args, PyObject *kwds) +{ + NPY_ORDER order = NPY_CORDER; + static char *kwlist[] = {"order", NULL}; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O&:tostring", kwlist, + PyArray_OrderConverter, &order)) { + return NULL; + } + /* 2020-03-30, NumPy 1.19 */ + if (DEPRECATE("tostring() is deprecated. Use tobytes() instead.") < 0) { + return NULL; + } + return PyArray_ToString(self, order); +} + /* This should grow an order= keyword to be consistent */ @@ -2844,7 +2861,7 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = { (PyCFunction)array_tolist, METH_VARARGS, NULL}, {"tostring", - (PyCFunction)array_tobytes, + (PyCFunction)array_tostring, METH_VARARGS | METH_KEYWORDS, NULL}, {"trace", (PyCFunction)array_trace, diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 7792fcdcb..9e8022abd 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -1582,8 +1582,9 @@ _array_fromobject(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kws) "ndmin", NULL}; if (PyTuple_GET_SIZE(args) > 2) { - PyErr_SetString(PyExc_ValueError, - "only 2 non-keyword arguments accepted"); + PyErr_Format(PyExc_TypeError, + "array() takes from 1 to 2 positional arguments but " + "%zd were given", PyTuple_GET_SIZE(args)); return NULL; } @@ -1856,14 +1857,15 @@ array_empty_like(PyObject *NPY_UNUSED(ignored), PyObject *args, PyObject *kwds) NPY_ORDER order = NPY_KEEPORDER; PyArrayObject *ret = NULL; int subok = 1; - PyArray_Dims shape = {NULL, 0}; + /* -1 is a special value meaning "not specified" */ + PyArray_Dims shape = {NULL, -1}; if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&O&iO&:empty_like", kwlist, &PyArray_Converter, &prototype, &PyArray_DescrConverter2, &dtype, &PyArray_OrderConverter, &order, &subok, - &PyArray_IntpConverter, &shape)) { + &PyArray_OptionalIntpConverter, &shape)) { goto fail; } /* steals the reference to dtype if it's not NULL */ diff --git a/numpy/core/src/multiarray/nditer_pywrap.c b/numpy/core/src/multiarray/nditer_pywrap.c index add40f460..505c5a841 100644 --- a/numpy/core/src/multiarray/nditer_pywrap.c +++ b/numpy/core/src/multiarray/nditer_pywrap.c @@ -17,6 +17,7 @@ #include "npy_pycompat.h" #include "alloc.h" #include "common.h" +#include "conversion_utils.h" #include "ctors.h" /* Functions not part of the public NumPy C API */ @@ -748,7 +749,7 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) int oa_ndim = -1; int op_axes_arrays[NPY_MAXARGS][NPY_MAXDIMS]; int *op_axes[NPY_MAXARGS]; - PyArray_Dims itershape = {NULL, 0}; + PyArray_Dims itershape = {NULL, -1}; int buffersize = 0; if (self->iter != NULL) { @@ -765,7 +766,7 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) npyiter_order_converter, &order, PyArray_CastingConverter, &casting, &op_axes_in, - PyArray_IntpConverter, &itershape, + PyArray_OptionalIntpConverter, &itershape, &buffersize)) { npy_free_cache_dim_obj(itershape); return -1; @@ -800,7 +801,7 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) } } - if (itershape.len > 0) { + if (itershape.len != -1) { if (oa_ndim == -1) { oa_ndim = itershape.len; memset(op_axes, 0, sizeof(op_axes[0]) * nop); @@ -812,10 +813,6 @@ npyiter_init(NewNpyArrayIterObject *self, PyObject *args, PyObject *kwds) goto fail; } } - else if (itershape.ptr != NULL) { - npy_free_cache_dim_obj(itershape); - itershape.ptr = NULL; - } self->iter = NpyIter_AdvancedNew(nop, op, flags, order, casting, op_flags, op_request_dtypes, diff --git a/numpy/core/tests/test_arrayprint.py b/numpy/core/tests/test_arrayprint.py index 008ca20e6..e29217461 100644 --- a/numpy/core/tests/test_arrayprint.py +++ b/numpy/core/tests/test_arrayprint.py @@ -475,9 +475,7 @@ class TestPrintOptions: assert_equal(repr(x), "array([0., 1., 2.])") def test_0d_arrays(self): - unicode = type(u'') - - assert_equal(unicode(np.array(u'café', '<U4')), u'café') + assert_equal(str(np.array(u'café', '<U4')), u'café') assert_equal(repr(np.array('café', '<U4')), "array('café', dtype='<U4')") diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index d2cf315a9..5d35bde6c 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -8,6 +8,7 @@ import operator import warnings import pytest import tempfile +import re import numpy as np from numpy.testing import ( @@ -548,6 +549,22 @@ def test_deprecate_ragged_arrays(): np.array(arg) +class TestToString(_DeprecationTestCase): + # 2020-03-06 1.19.0 + message = re.escape("tostring() is deprecated. Use tobytes() instead.") + + def test_tostring(self): + arr = np.array(list(b"test\xFF"), dtype=np.uint8) + self.assert_deprecated(arr.tostring) + + def test_tostring_matches_tobytes(self): + arr = np.array(list(b"test\xFF"), dtype=np.uint8) + b = arr.tobytes() + with assert_warns(DeprecationWarning): + s = arr.tostring() + assert s == b + + class TestDTypeCoercion(_DeprecationTestCase): # 2020-02-06 1.19.0 message = "Converting .* to a dtype .*is deprecated" diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index 6ec61fb1d..fd0de8732 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -605,6 +605,10 @@ class TestEinsum: [[[1, 3], [3, 9], [5, 15], [7, 21]], [[8, 16], [16, 32], [24, 48], [32, 64]]]) + # Ensure explicitly setting out=None does not cause an error + # see issue gh-15776 and issue gh-15256 + assert_equal(np.einsum('i,j', [1], [2], out=None), [[2]]) + def test_subscript_range(self): # Issue #7741, make sure that all letters of Latin alphabet (both uppercase & lowercase) can be used # when creating a subscript from arrays diff --git a/numpy/core/tests/test_indexerrors.py b/numpy/core/tests/test_indexerrors.py index 9d2433fc5..a0e9a8c55 100644 --- a/numpy/core/tests/test_indexerrors.py +++ b/numpy/core/tests/test_indexerrors.py @@ -1,5 +1,8 @@ import numpy as np -from numpy.testing import assert_raises +from numpy.testing import ( + assert_raises, assert_raises_regex, + ) + class TestIndexErrors: '''Tests to exercise indexerrors not covered by other tests.''' @@ -110,6 +113,15 @@ class TestIndexErrors: assert_raises(IndexError, lambda: a[(1, [0, 1])]) assert_raises(IndexError, lambda: assign(a, (1, [0, 1]), 1)) + def test_mapping_error_message(self): + a = np.zeros((3, 5)) + index = (1, 2, 3, 4, 5) + assert_raises_regex( + IndexError, + "too many indices for array: " + "array is 2-dimensional, but 5 were indexed", + lambda: a[index]) + def test_methods(self): "cases from methods.c" diff --git a/numpy/core/tests/test_mem_overlap.py b/numpy/core/tests/test_mem_overlap.py index 44ebf1cd2..675613de4 100644 --- a/numpy/core/tests/test_mem_overlap.py +++ b/numpy/core/tests/test_mem_overlap.py @@ -5,7 +5,6 @@ import numpy as np from numpy.core._multiarray_tests import solve_diophantine, internal_overlap from numpy.core import _umath_tests from numpy.lib.stride_tricks import as_strided -from numpy.compat import long from numpy.testing import ( assert_, assert_raises, assert_equal, assert_array_equal ) @@ -387,7 +386,6 @@ def test_shares_memory_api(): assert_equal(np.shares_memory(a, b), True) assert_equal(np.shares_memory(a, b, max_work=None), True) assert_raises(np.TooHardError, np.shares_memory, a, b, max_work=1) - assert_raises(np.TooHardError, np.shares_memory, a, b, max_work=long(1)) def test_may_share_memory_bad_max_work(): diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 829679dab..5515ff446 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -16,19 +16,12 @@ from contextlib import contextmanager from numpy.compat import pickle -try: - import pathlib -except ImportError: - try: - import pathlib2 as pathlib - except ImportError: - pathlib = None - +import pathlib import builtins from decimal import Decimal import numpy as np -from numpy.compat import strchar, unicode +from numpy.compat import strchar import numpy.core._multiarray_tests as _multiarray_tests from numpy.testing import ( assert_, assert_raises, assert_warns, assert_equal, assert_almost_equal, @@ -363,6 +356,11 @@ class TestAttributes: a.strides = 1 a[::2].strides = 2 + # test 0d + arr_0d = np.array(0) + arr_0d.strides = () + assert_raises(TypeError, set_strides, arr_0d, None) + def test_fill(self): for t in "?bhilqpBHILQPfdgFDGO": x = np.empty((3, 2, 1), t) @@ -686,6 +684,12 @@ class TestZeroRank: y[()] = 6 assert_equal(x[()], 6) + # strides and shape must be the same length + with pytest.raises(ValueError): + np.ndarray((2,), strides=()) + with pytest.raises(ValueError): + np.ndarray((), strides=(2,)) + def test_output(self): x = np.array(2) assert_raises(ValueError, np.add, x, [1], x) @@ -1464,12 +1468,12 @@ class TestZeroSizeFlexible: assert_equal(zs.itemsize, 0) zs = self._zeros(10, np.void) assert_equal(zs.itemsize, 0) - zs = self._zeros(10, unicode) + zs = self._zeros(10, str) assert_equal(zs.itemsize, 0) def _test_sort_partition(self, name, kinds, **kwargs): # Previously, these would all hang - for dt in [bytes, np.void, unicode]: + for dt in [bytes, np.void, str]: zs = self._zeros(10, dt) sort_method = getattr(zs, name) sort_func = getattr(np, name) @@ -1491,13 +1495,13 @@ class TestZeroSizeFlexible: def test_resize(self): # previously an error - for dt in [bytes, np.void, unicode]: + for dt in [bytes, np.void, str]: zs = self._zeros(10, dt) zs.resize(25) zs.resize((10, 10)) def test_view(self): - for dt in [bytes, np.void, unicode]: + for dt in [bytes, np.void, str]: zs = self._zeros(10, dt) # viewing as itself should be allowed @@ -1512,7 +1516,7 @@ class TestZeroSizeFlexible: def test_pickle(self): for proto in range(2, pickle.HIGHEST_PROTOCOL + 1): - for dt in [bytes, np.void, unicode]: + for dt in [bytes, np.void, str]: zs = self._zeros(10, dt) p = pickle.dumps(zs, protocol=proto) zs2 = pickle.loads(p) @@ -4456,7 +4460,7 @@ class TestPutmask: assert_equal(x[mask], np.array(val, T)) def test_ip_types(self): - unchecked_types = [bytes, unicode, np.void] + unchecked_types = [bytes, str, np.void] x = np.random.random(1000)*100 mask = x < 40 @@ -4510,7 +4514,7 @@ class TestTake: assert_array_equal(x.take(ind, axis=0), x) def test_ip_types(self): - unchecked_types = [bytes, unicode, np.void] + unchecked_types = [bytes, str, np.void] x = np.random.random(24)*100 x.shape = 2, 3, 4 @@ -4680,14 +4684,12 @@ class TestIO: y = np.fromfile(self.filename, dtype=self.dtype) assert_array_equal(y, self.x.flat) - @pytest.mark.skipif(pathlib is None, reason="pathlib not found") def test_roundtrip_pathlib(self): p = pathlib.Path(self.filename) self.x.tofile(p) y = np.fromfile(p, dtype=self.dtype) assert_array_equal(y, self.x.flat) - @pytest.mark.skipif(pathlib is None, reason="pathlib not found") def test_roundtrip_dump_pathlib(self): p = pathlib.Path(self.filename) self.x.dump(p) @@ -5498,6 +5500,12 @@ class TestStats: # of float32. assert_(_mean(np.ones(100000, dtype='float16')) == 1) + def test_mean_axis_error(self): + # Ensure that AxisError is raised instead of IndexError when axis is + # out of bounds, see gh-15817. + with assert_raises(np.core._exceptions.AxisError): + np.arange(10).mean(axis=2) + def test_var_values(self): for mat in [self.rmat, self.cmat, self.omat]: for axis in [0, 1, None]: @@ -5540,6 +5548,12 @@ class TestStats: cmat_swapped = cmat.astype(cmat.dtype.newbyteorder()) assert_almost_equal(cmat.var(), cmat_swapped.var()) + def test_var_axis_error(self): + # Ensure that AxisError is raised instead of IndexError when axis is + # out of bounds, see gh-15817. + with assert_raises(np.core._exceptions.AxisError): + np.arange(10).var(axis=2) + def test_std_values(self): for mat in [self.rmat, self.cmat, self.omat]: for axis in [0, 1, None]: diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index 24272bb0d..c106c528d 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -2688,7 +2688,15 @@ def test_0d_iter(): 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 + + i = nditer(np.arange(5), ['multi_index'], [['readonly']], + op_axes=[()], itershape=()) + assert_equal(i.ndim, 0) + assert_equal(len(i), 1) + + # passing an itershape alone is not enough, the op_axes are also needed + with assert_raises(ValueError): + nditer(np.arange(5), ['multi_index'], [['readonly']], itershape=()) # Test a more complex buffered casting case (same as another test above) sdt = [('a', 'f4'), ('b', 'i8'), ('c', 'c8', (2, 3)), ('d', 'O')] diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py index 05f59d9dc..1bcfe50a4 100644 --- a/numpy/core/tests/test_numeric.py +++ b/numpy/core/tests/test_numeric.py @@ -1201,6 +1201,17 @@ class TestNonzero: a = np.array([[0, 0, 1], [1, 0, 1]]) assert_equal(np.count_nonzero(a, axis=()), a.astype(bool)) + def test_countnonzero_keepdims(self): + a = np.array([[0, 0, 1, 0], + [0, 3, 5, 0], + [7, 9, 2, 0]]) + assert_equal(np.count_nonzero(a, axis=0, keepdims=True), + [[1, 2, 3, 0]]) + assert_equal(np.count_nonzero(a, axis=1, keepdims=True), + [[1], [2], [3]]) + assert_equal(np.count_nonzero(a, keepdims=True), + [[6]]) + def test_array_method(self): # Tests that the array method # call to nonzero works @@ -2457,7 +2468,7 @@ class TestLikeFuncs: (np.arange(24).reshape(2, 3, 4).swapaxes(0, 1), None), (np.arange(24).reshape(4, 3, 2).swapaxes(0, 1), '?'), ] - self.shapes = [(5,), (5,6,), (5,6,7,)] + self.shapes = [(), (5,), (5,6,), (5,6,7,)] def compare_array_value(self, dz, value, fill_value): if value is not None: diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 97898cc20..4d7639e43 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -15,7 +15,7 @@ from numpy.testing import ( _assert_valid_refcount, HAS_REFCOUNT, ) from numpy.testing._private.utils import _no_tracing -from numpy.compat import asbytes, asunicode, long, pickle +from numpy.compat import asbytes, asunicode, pickle try: RecursionError @@ -1408,6 +1408,13 @@ class TestRegression: dtype='U') assert_raises(UnicodeEncodeError, np.array, a, 'S4') + def test_unicode_to_string_cast_error(self): + # gh-15790 + a = np.array([u'\x80'] * 129, dtype='U3') + assert_raises(UnicodeEncodeError, np.array, a, 'S') + b = a.reshape(3, 43)[:-1, :-1] + assert_raises(UnicodeEncodeError, np.array, b, 'S') + def test_mixed_string_unicode_array_creation(self): a = np.array(['1234', u'123']) assert_(a.itemsize == 16) @@ -1497,14 +1504,11 @@ class TestRegression: min //= -1 with np.errstate(divide="ignore"): - for t in (np.int8, np.int16, np.int32, np.int64, int, np.compat.long): + for t in (np.int8, np.int16, np.int32, np.int64, int): test_type(t) def test_buffer_hashlib(self): - try: - from hashlib import md5 - except ImportError: - from md5 import new as md5 + from hashlib import md5 x = np.array([1, 2, 3], dtype=np.dtype('<i4')) assert_equal(md5(x).hexdigest(), '2a1dd1e1e59d0a384c26951e316cd7e6') @@ -1799,7 +1803,6 @@ class TestRegression: a = np.array(0, dtype=object) a[()] = a assert_raises(RecursionError, int, a) - assert_raises(RecursionError, long, a) assert_raises(RecursionError, float, a) a[()] = None @@ -1825,7 +1828,6 @@ class TestRegression: b = np.array(0, dtype=object) a[()] = b assert_equal(int(a), int(0)) - assert_equal(long(a), long(0)) assert_equal(float(a), float(0)) def test_object_array_self_copy(self): @@ -2481,4 +2483,3 @@ class TestRegression: assert arr.size * arr.itemsize > 2 ** 31 c_arr = np.ctypeslib.as_ctypes(arr) assert_equal(c_arr._length_, arr.size) - diff --git a/numpy/core/tests/test_shape_base.py b/numpy/core/tests/test_shape_base.py index 738260327..546ecf001 100644 --- a/numpy/core/tests/test_shape_base.py +++ b/numpy/core/tests/test_shape_base.py @@ -11,7 +11,6 @@ from numpy.testing import ( assert_raises_regex, assert_warns ) -from numpy.compat import long class TestAtleast1d: def test_0D_array(self): @@ -49,7 +48,6 @@ class TestAtleast1d: """ assert_(atleast_1d(3).shape == (1,)) assert_(atleast_1d(3j).shape == (1,)) - assert_(atleast_1d(long(3)).shape == (1,)) assert_(atleast_1d(3.0).shape == (1,)) assert_(atleast_1d([[2, 3], [4, 5]]).shape == (2, 2)) diff --git a/numpy/core/tests/test_unicode.py b/numpy/core/tests/test_unicode.py index ac065d5d6..8e0dd47cb 100644 --- a/numpy/core/tests/test_unicode.py +++ b/numpy/core/tests/test_unicode.py @@ -1,10 +1,8 @@ import numpy as np -from numpy.compat import unicode from numpy.testing import assert_, assert_equal, assert_array_equal def buffer_length(arr): - if isinstance(arr, unicode): - arr = str(arr) + if isinstance(arr, str): if not arr: charmax = 0 else: diff --git a/numpy/doc/indexing.py b/numpy/doc/indexing.py index aa84e2b11..6b15a7a9e 100644 --- a/numpy/doc/indexing.py +++ b/numpy/doc/indexing.py @@ -372,8 +372,7 @@ exceptions (assigning complex to floats or ints): :: >>> x[1] 1 >>> x[1] = 1.2j - <type 'exceptions.TypeError'>: can't convert complex to long; use - long(abs(z)) + TypeError: can't convert complex to int Unlike some of the references (such as array and mask indices) diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py index 894af3437..3d2f97a56 100755 --- a/numpy/f2py/crackfortran.py +++ b/numpy/f2py/crackfortran.py @@ -1749,10 +1749,12 @@ def setattrspec(decl, attr, force=0): decl['attrspec'].append(attr) elif attr == 'automatic' and 'static' not in decl['attrspec']: decl['attrspec'].append(attr) - elif attr == 'public' and 'private' not in decl['attrspec']: - decl['attrspec'].append(attr) - elif attr == 'private' and 'public' not in decl['attrspec']: - decl['attrspec'].append(attr) + elif attr == 'public': + if 'private' not in decl['attrspec']: + decl['attrspec'].append(attr) + elif attr == 'private': + if 'public' not in decl['attrspec']: + decl['attrspec'].append(attr) else: decl['attrspec'].append(attr) return decl diff --git a/numpy/f2py/tests/test_crackfortran.py b/numpy/f2py/tests/test_crackfortran.py index 18a1474b0..735804024 100644 --- a/numpy/f2py/tests/test_crackfortran.py +++ b/numpy/f2py/tests/test_crackfortran.py @@ -1,6 +1,9 @@ import numpy as np from numpy.testing import assert_array_equal from . import util +from numpy.f2py import crackfortran +import tempfile +import textwrap class TestNoSpace(util.F2PyTest): @@ -33,3 +36,53 @@ class TestNoSpace(util.F2PyTest): self.module.subc([w, k]) assert_array_equal(k, w + 1) assert self.module.t0(23) == b'2' + +class TestPublicPrivate(): + def test_defaultPrivate(self, tmp_path): + f_path = tmp_path / "mod.f90" + with f_path.open('w') as ff: + ff.write(textwrap.dedent("""\ + module foo + private + integer :: a + public :: setA + integer :: b + contains + subroutine setA(v) + integer, intent(in) :: v + a = v + end subroutine setA + end module foo + """)) + mod = crackfortran.crackfortran([str(f_path)]) + assert len(mod) == 1 + mod = mod[0] + assert 'private' in mod['vars']['a']['attrspec'] + assert 'public' not in mod['vars']['a']['attrspec'] + assert 'private' in mod['vars']['b']['attrspec'] + assert 'public' not in mod['vars']['b']['attrspec'] + assert 'private' not in mod['vars']['seta']['attrspec'] + assert 'public' in mod['vars']['seta']['attrspec'] + + def test_defaultPublic(self, tmp_path): + f_path = tmp_path / "mod.f90" + with f_path.open('w') as ff: + ff.write(textwrap.dedent("""\ + module foo + public + integer, private :: a + public :: setA + contains + subroutine setA(v) + integer, intent(in) :: v + a = v + end subroutine setA + end module foo + """)) + mod = crackfortran.crackfortran([str(f_path)]) + assert len(mod) == 1 + mod = mod[0] + assert 'private' in mod['vars']['a']['attrspec'] + assert 'public' not in mod['vars']['a']['attrspec'] + assert 'private' not in mod['vars']['seta']['attrspec'] + assert 'public' in mod['vars']['seta']['attrspec'] diff --git a/numpy/f2py/tests/test_return_complex.py b/numpy/f2py/tests/test_return_complex.py index 38b23e1f3..3d2e2b94f 100644 --- a/numpy/f2py/tests/test_return_complex.py +++ b/numpy/f2py/tests/test_return_complex.py @@ -1,7 +1,6 @@ import pytest from numpy import array -from numpy.compat import long from numpy.testing import assert_, assert_raises from . import util @@ -15,7 +14,7 @@ class TestReturnComplex(util.F2PyTest): err = 0.0 assert_(abs(t(234j) - 234.0j) <= err) assert_(abs(t(234.6) - 234.6) <= err) - assert_(abs(t(long(234)) - 234.0) <= err) + assert_(abs(t(234) - 234.0) <= err) assert_(abs(t(234.6 + 3j) - (234.6 + 3j)) <= err) #assert_( abs(t('234')-234.)<=err) #assert_( abs(t('234.6')-234.6)<=err) diff --git a/numpy/f2py/tests/test_return_integer.py b/numpy/f2py/tests/test_return_integer.py index c95835454..0a8121dc1 100644 --- a/numpy/f2py/tests/test_return_integer.py +++ b/numpy/f2py/tests/test_return_integer.py @@ -1,7 +1,6 @@ import pytest from numpy import array -from numpy.compat import long from numpy.testing import assert_, assert_raises from . import util @@ -11,7 +10,6 @@ class TestReturnInteger(util.F2PyTest): def check_function(self, t, tname): assert_(t(123) == 123, repr(t(123))) assert_(t(123.6) == 123) - assert_(t(long(123)) == 123) assert_(t('123') == 123) assert_(t(-123) == -123) assert_(t([123]) == 123) diff --git a/numpy/f2py/tests/test_return_logical.py b/numpy/f2py/tests/test_return_logical.py index 3139e0df7..9db939c7e 100644 --- a/numpy/f2py/tests/test_return_logical.py +++ b/numpy/f2py/tests/test_return_logical.py @@ -1,7 +1,6 @@ import pytest from numpy import array -from numpy.compat import long from numpy.testing import assert_, assert_raises from . import util @@ -18,7 +17,6 @@ class TestReturnLogical(util.F2PyTest): assert_(t(1j) == 1) assert_(t(234) == 1) assert_(t(234.6) == 1) - assert_(t(long(234)) == 1) assert_(t(234.6 + 3j) == 1) assert_(t('234') == 1) assert_(t('aaa') == 1) diff --git a/numpy/f2py/tests/test_return_real.py b/numpy/f2py/tests/test_return_real.py index 258cf8d28..8e5022a8e 100644 --- a/numpy/f2py/tests/test_return_real.py +++ b/numpy/f2py/tests/test_return_real.py @@ -2,7 +2,6 @@ import platform import pytest from numpy import array -from numpy.compat import long from numpy.testing import assert_, assert_raises from . import util @@ -16,7 +15,6 @@ class TestReturnReal(util.F2PyTest): err = 0.0 assert_(abs(t(234) - 234.0) <= err) assert_(abs(t(234.6) - 234.6) <= err) - assert_(abs(t(long(234)) - 234.0) <= err) assert_(abs(t('234') - 234) <= err) assert_(abs(t('234.6') - 234.6) <= err) assert_(abs(t(-234) + 234) <= err) diff --git a/numpy/f2py/tests/util.py b/numpy/f2py/tests/util.py index 6dcc2ed12..c5b06697d 100644 --- a/numpy/f2py/tests/util.py +++ b/numpy/f2py/tests/util.py @@ -19,10 +19,7 @@ from numpy.compat import asbytes, asstr from numpy.testing import temppath from importlib import import_module -try: - from hashlib import md5 -except ImportError: - from md5 import new as md5 # noqa: F401 +from hashlib import md5 # # Maintaining a temporary module directory diff --git a/numpy/lib/arrayterator.py b/numpy/lib/arrayterator.py index 924092995..b9ea21f8e 100644 --- a/numpy/lib/arrayterator.py +++ b/numpy/lib/arrayterator.py @@ -10,8 +10,6 @@ a user-specified number of elements. from operator import mul from functools import reduce -from numpy.compat import long - __all__ = ['Arrayterator'] @@ -108,7 +106,7 @@ class Arrayterator: if slice_ is Ellipsis: fixed.extend([slice(None)] * (dims-length+1)) length = len(fixed) - elif isinstance(slice_, (int, long)): + elif isinstance(slice_, int): fixed.append(slice(slice_, slice_+1, 1)) else: fixed.append(slice_) diff --git a/numpy/lib/format.py b/numpy/lib/format.py index e2696c286..2afa4ac10 100644 --- a/numpy/lib/format.py +++ b/numpy/lib/format.py @@ -166,7 +166,7 @@ import io import warnings from numpy.lib.utils import safe_eval from numpy.compat import ( - isfileobj, long, os_fspath, pickle + isfileobj, os_fspath, pickle ) @@ -594,7 +594,7 @@ def _read_array_header(fp, version): # Sanity-check the values. if (not isinstance(d['shape'], tuple) or - not numpy.all([isinstance(x, (int, long)) for x in d['shape']])): + not numpy.all([isinstance(x, int) for x in d['shape']])): msg = "shape is not valid: {!r}" raise ValueError(msg.format(d['shape'])) if not isinstance(d['fortran_order'], bool): diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index a49c34741..bfcf0d316 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -29,7 +29,6 @@ from numpy.core.multiarray import ( interp as compiled_interp, interp_complex as compiled_interp_complex ) from numpy.core.umath import _add_newdoc_ufunc as add_newdoc_ufunc -from numpy.compat import long import builtins @@ -4207,12 +4206,17 @@ def delete(arr, obj, axis=None): Parameters ---------- arr : array_like - Input array. + Input array. obj : slice, int or array of ints - Indicate indices of sub-arrays to remove along the specified axis. + Indicate indices of sub-arrays to remove along the specified axis. + + .. versionchanged:: 1.19.0 + Boolean indices are now treated as a mask of elements to remove, + rather than being cast to the integers 0 and 1. + axis : int, optional - The axis along which to delete the subarray defined by `obj`. - If `axis` is None, `obj` is applied to the flattened array. + The axis along which to delete the subarray defined by `obj`. + If `axis` is None, `obj` is applied to the flattened array. Returns ------- @@ -4273,15 +4277,6 @@ def delete(arr, obj, axis=None): # needed for np.matrix, which is still not 1d after being ravelled ndim = arr.ndim axis = ndim - 1 - elif ndim == 0: - # 2013-09-24, 1.9 - warnings.warn( - "in the future the special handling of scalars will be removed " - "from delete and raise an error", DeprecationWarning, stacklevel=3) - if wrap: - return wrap(arr) - else: - return arr.copy(order=arrorder) else: axis = normalize_axis_index(axis, ndim) @@ -4339,19 +4334,8 @@ def delete(arr, obj, axis=None): else: return new - _obj = obj - obj = np.asarray(obj) - # After removing the special handling of booleans and out of - # bounds values, the conversion to the array can be removed. - if obj.dtype == bool: - # 2012-10-11, NumPy 1.8 - warnings.warn("in the future insert will treat boolean arrays and " - "array-likes as boolean index instead of casting it " - "to integer", FutureWarning, stacklevel=3) - obj = obj.astype(intp) - if isinstance(_obj, (int, long, integer)): + if isinstance(obj, (int, integer)) and not isinstance(obj, bool): # optimization for a single value - obj = obj.item() if (obj < -N or obj >= N): raise IndexError( "index %i is out of bounds for axis %i with " @@ -4367,11 +4351,23 @@ def delete(arr, obj, axis=None): slobj2[axis] = slice(obj+1, None) new[tuple(slobj)] = arr[tuple(slobj2)] else: + _obj = obj + obj = np.asarray(obj) if obj.size == 0 and not isinstance(_obj, np.ndarray): obj = obj.astype(intp) - keep = ones(N, dtype=bool) - keep[obj, ] = False + if obj.dtype == bool: + if obj.shape != (N,): + raise ValueError('boolean array argument obj to delete ' + 'must be one dimensional and match the axis ' + 'length of {}'.format(N)) + + # optimization, the other branch is slower + keep = ~obj + else: + keep = ones(N, dtype=bool) + keep[obj,] = False + slobj[axis] = keep new = arr[tuple(slobj)] @@ -4490,17 +4486,6 @@ def insert(arr, obj, values, axis=None): # needed for np.matrix, which is still not 1d after being ravelled ndim = arr.ndim axis = ndim - 1 - elif ndim == 0: - # 2013-09-24, 1.9 - warnings.warn( - "in the future the special handling of scalars will be removed " - "from insert and raise an error", DeprecationWarning, stacklevel=3) - arr = arr.copy(order=arrorder) - arr[...] = values - if wrap: - return wrap(arr) - else: - return arr else: axis = normalize_axis_index(axis, ndim) slobj = [slice(None)]*ndim diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index 5358c6846..32d7df117 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -229,21 +229,21 @@ def _hist_bin_fd(x, range): def _hist_bin_auto(x, range): """ Histogram bin estimator that uses the minimum width of the - Freedman-Diaconis and Sturges estimators if the FD bandwidth is non zero - and the Sturges estimator if the FD bandwidth is 0. + Freedman-Diaconis and Sturges estimators if the FD bin width is non-zero. + If the bin width from the FD estimator is 0, the Sturges estimator is used. The FD estimator is usually the most robust method, but its width estimate tends to be too large for small `x` and bad for data with limited variance. The Sturges estimator is quite good for small (<1000) datasets - and is the default in the R language. This method gives good off the shelf + and is the default in the R language. This method gives good off-the-shelf behaviour. .. versionchanged:: 1.15.0 If there is limited variance the IQR can be 0, which results in the FD bin width being 0 too. This is not a valid bin width, so ``np.histogram_bin_edges`` chooses 1 bin instead, which may not be optimal. - If the IQR is 0, it's unlikely any variance based estimators will be of - use, so we revert to the sturges estimator, which only uses the size of the + If the IQR is 0, it's unlikely any variance-based estimators will be of + use, so we revert to the Sturges estimator, which only uses the size of the dataset in its calculation. Parameters diff --git a/numpy/lib/index_tricks.py b/numpy/lib/index_tricks.py index 0560bd36d..b4118814d 100644 --- a/numpy/lib/index_tricks.py +++ b/numpy/lib/index_tricks.py @@ -597,8 +597,6 @@ class ndenumerate: def __iter__(self): return self - next = __next__ - @set_module('numpy') class ndindex: @@ -665,8 +663,6 @@ class ndindex: next(self._it) return self._it.multi_index - next = __next__ - # You can do all this with slice() plus a few special objects, # but there's a lot to remember. This version is simpler because diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index 1d69d869e..6964c1128 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -9,7 +9,6 @@ from numpy.lib._iotools import ( LineSplitter, NameValidator, StringConverter, has_nested_fields, easy_dtype, flatten_dtype ) -from numpy.compat import unicode class TestLineSplitter: @@ -179,10 +178,10 @@ class TestStringConverter: # note that the longdouble type has been skipped, so the # _status increases by 2. Everything should succeed with # unicode conversion (5). - for s in ['a', u'a', b'a']: + for s in ['a', b'a']: res = converter.upgrade(s) - assert_(type(res) is unicode) - assert_equal(res, u'a') + assert_(type(res) is str) + assert_equal(res, 'a') assert_equal(converter._status, 5 + status_offset) def test_missing(self): diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 04b280038..23bf3296d 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -21,7 +21,6 @@ from numpy.lib import ( select, setxor1d, sinc, trapz, trim_zeros, unwrap, unique, vectorize ) -from numpy.compat import long def get_mat(n): data = np.arange(n) @@ -509,12 +508,11 @@ class TestInsert: insert(a, 1, a[:, 2, :], axis=1)) def test_0d(self): - # This is an error in the future a = np.array(1) - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', '', DeprecationWarning) - assert_equal(insert(a, [], 2, axis=0), np.array(2)) - assert_(w[0].category is DeprecationWarning) + with pytest.raises(np.AxisError): + insert(a, [], 2, axis=0) + with pytest.raises(TypeError): + insert(a, [], 2, axis="nonsense") def test_subclass(self): class SubClass(np.ndarray): @@ -810,9 +808,6 @@ class TestDelete: a_del = delete(self.a, indices) nd_a_del = delete(self.nd_a, indices, axis=1) msg = 'Delete failed for obj: %r' % indices - # NOTE: The cast should be removed after warning phase for bools - if not isinstance(indices, (slice, int, long, np.integer)): - indices = np.asarray(indices, dtype=np.intp) assert_array_equal(setxor1d(a_del, self.a[indices, ]), self.a, err_msg=msg) xor = setxor1d(nd_a_del[0,:, 0], self.nd_a[0, indices, 0]) @@ -828,7 +823,6 @@ class TestDelete: self._check_inverse_of_slicing(s) def test_fancy(self): - # Deprecation/FutureWarning tests should be kept after change. self._check_inverse_of_slicing(np.array([[0, 1], [2, 1]])) with pytest.raises(IndexError): delete(self.a, [100]) @@ -837,14 +831,17 @@ class TestDelete: self._check_inverse_of_slicing([0, -1, 2, 2]) - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', category=FutureWarning) - obj = np.array([True, False, False], dtype=bool) - self._check_inverse_of_slicing(obj) - # _check_inverse_of_slicing operates on two arrays, so warns twice - assert len(w) == 2 - assert_(w[0].category is FutureWarning) - assert_(w[1].category is FutureWarning) + self._check_inverse_of_slicing([True, False, False, True, False]) + + # not legal, indexing with these would change the dimension + with pytest.raises(ValueError): + delete(self.a, True) + with pytest.raises(ValueError): + delete(self.a, False) + + # not enough items + with pytest.raises(ValueError): + delete(self.a, [False]*4) def test_single(self): self._check_inverse_of_slicing(0) @@ -852,10 +849,10 @@ class TestDelete: def test_0d(self): a = np.array(1) - with warnings.catch_warnings(record=True) as w: - warnings.filterwarnings('always', '', DeprecationWarning) - assert_equal(delete(a, [], axis=0), a) - assert_(w[0].category is DeprecationWarning) + with pytest.raises(np.AxisError): + delete(a, [], axis=0) + with pytest.raises(TypeError): + delete(a, [], axis="nonsense") def test_subclass(self): class SubClass(np.ndarray): diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 6812d8d68..8ce20a116 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -13,6 +13,7 @@ from tempfile import NamedTemporaryFile from io import BytesIO, StringIO from datetime import datetime import locale +from multiprocessing import Process import numpy as np import numpy.ma as ma @@ -569,16 +570,20 @@ class TestSaveTxt: assert_equal(s.read(), b"%f\n" % 1.) @pytest.mark.skipif(sys.platform=='win32', reason="files>4GB may not work") - @pytest.mark.skipif(IS_PYPY, - reason="GC problems after test, gc.collect does not help. see gh-15775") @pytest.mark.slow @requires_memory(free_bytes=7e9) def test_large_zip(self): - # The test takes at least 6GB of memory, writes a file larger than 4GB - test_data = np.asarray([np.random.rand(np.random.randint(50,100),4) - for i in range(800000)], dtype=object) - with tempdir() as tmpdir: - np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data) + def check_large_zip(): + # The test takes at least 6GB of memory, writes a file larger than 4GB + test_data = np.asarray([np.random.rand(np.random.randint(50,100),4) + for i in range(800000)], dtype=object) + with tempdir() as tmpdir: + np.savez(os.path.join(tmpdir, 'test.npz'), test_data=test_data) + # run in a subprocess to ensure memory is released on PyPy, see gh-15775 + p = Process(target=check_large_zip) + p.start() + p.join() + assert p.exitcode == 0 class LoadTxtBase: def check_compressed(self, fopen, suffixes): diff --git a/numpy/lib/tests/test_regression.py b/numpy/lib/tests/test_regression.py index a2598990b..55df2a675 100644 --- a/numpy/lib/tests/test_regression.py +++ b/numpy/lib/tests/test_regression.py @@ -5,7 +5,6 @@ from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_array_almost_equal, assert_raises, _assert_valid_refcount, ) -from numpy.compat import unicode class TestRegression: @@ -180,7 +179,7 @@ class TestRegression: # related to ticket #1405. include_dirs = [np.get_include()] for path in include_dirs: - assert_(isinstance(path, (str, unicode))) + assert_(isinstance(path, str)) assert_(path != '') def test_polyder_return_type(self): diff --git a/numpy/lib/tests/test_type_check.py b/numpy/lib/tests/test_type_check.py index 47685550a..3f4ca6309 100644 --- a/numpy/lib/tests/test_type_check.py +++ b/numpy/lib/tests/test_type_check.py @@ -1,5 +1,4 @@ import numpy as np -from numpy.compat import long from numpy.testing import ( assert_, assert_equal, assert_array_equal, assert_raises ) @@ -86,7 +85,6 @@ class TestIsscalar: assert_(not np.isscalar([3])) assert_(not np.isscalar((3,))) assert_(np.isscalar(3j)) - assert_(np.isscalar(long(10))) assert_(np.isscalar(4.0)) diff --git a/numpy/lib/user_array.py b/numpy/lib/user_array.py index 9c266fd6b..0e96b477e 100644 --- a/numpy/lib/user_array.py +++ b/numpy/lib/user_array.py @@ -11,7 +11,6 @@ from numpy.core import ( bitwise_xor, invert, less, less_equal, not_equal, equal, greater, greater_equal, shape, reshape, arange, sin, sqrt, transpose ) -from numpy.compat import long class container: @@ -196,9 +195,6 @@ class container: def __int__(self): return self._scalarfunc(int) - def __long__(self): - return self._scalarfunc(long) - def __hex__(self): return self._scalarfunc(hex) @@ -231,6 +227,10 @@ class container: "" return self.array.tostring() + def tobytes(self): + "" + return self.array.tobytes() + def byteswap(self): "" return self._rc(self.array.byteswap()) diff --git a/numpy/linalg/lapack_lite/fortran.py b/numpy/linalg/lapack_lite/fortran.py index 388c88daa..3aaefb92f 100644 --- a/numpy/linalg/lapack_lite/fortran.py +++ b/numpy/linalg/lapack_lite/fortran.py @@ -44,8 +44,6 @@ class LineIterator: line = line.rstrip() return line - next = __next__ - class PushbackIterator: """PushbackIterator(iterable) @@ -71,8 +69,6 @@ class PushbackIterator: def pushback(self, item): self.buffer.append(item) - next = __next__ - def fortranSourceLines(fo): """Return an iterator over statement lines of a Fortran source file. diff --git a/numpy/ma/core.py b/numpy/ma/core.py index e24cb956c..41af5cc70 100644 --- a/numpy/ma/core.py +++ b/numpy/ma/core.py @@ -2699,8 +2699,6 @@ class MaskedIterator: return masked return d - next = __next__ - class MaskedArray(ndarray): """ @@ -4327,17 +4325,6 @@ class MaskedArray(ndarray): raise MaskError('Cannot convert masked element to a Python int.') return int(self.item()) - def __long__(self): - """ - Convert to long. - """ - if self.size > 1: - raise TypeError("Only length-1 arrays can be converted " - "to Python scalars") - elif self._mask: - raise MaskError('Cannot convert masked element to a Python long.') - return long(self.item()) - @property def imag(self): """ @@ -5954,10 +5941,17 @@ class MaskedArray(ndarray): return result.tolist() def tostring(self, fill_value=None, order='C'): + r""" + A compatibility alias for `tobytes`, with exactly the same behavior. + + Despite its name, it returns `bytes` not `str`\ s. + + .. deprecated:: 1.19.0 """ - This function is a compatibility alias for tobytes. Despite its name it - returns bytes not strings. - """ + # 2020-03-30, Numpy 1.19.0 + warnings.warn( + "tostring() is deprecated. Use tobytes() instead.", + DeprecationWarning, stacklevel=2) return self.tobytes(fill_value, order=order) @@ -6385,6 +6379,21 @@ class MaskedConstant(MaskedArray): # it's a subclass, or something is wrong, make it obvious return object.__repr__(self) + def __format__(self, format_spec): + # Replace ndarray.__format__ with the default, which supports no format characters. + # Supporting format characters is unwise here, because we do not know what type + # the user was expecting - better to not guess. + try: + return object.__format__(self, format_spec) + except TypeError: + # 2020-03-23, NumPy 1.19.0 + warnings.warn( + "Format strings passed to MaskedConstant are ignored, but in future may " + "error or produce different behavior", + FutureWarning, stacklevel=2 + ) + return object.__format__(self, "") + def __reduce__(self): """Override of MaskedArray's __reduce__. """ diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 3bfb42555..98fc7dd97 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -447,6 +447,21 @@ class TestMaskedArray: assert_equal(copied.mask, [0, 0, 0]) assert_equal(a.mask, [0, 1, 0]) + def test_format(self): + a = array([0, 1, 2], mask=[False, True, False]) + assert_equal(format(a), "[0 -- 2]") + assert_equal(format(masked), "--") + assert_equal(format(masked, ""), "--") + + # Postponed from PR #15410, perhaps address in the future. + # assert_equal(format(masked, " >5"), " --") + # assert_equal(format(masked, " <5"), "-- ") + + # Expect a FutureWarning for using format_spec with MaskedElement + with assert_warns(FutureWarning): + with_format_string = format(masked, " >5") + assert_equal(with_format_string, "--") + def test_str_repr(self): a = array([0, 1, 2], mask=[False, True, False]) assert_equal(str(a), '[0 -- 2]') diff --git a/numpy/ma/tests/test_regression.py b/numpy/ma/tests/test_regression.py index 9f3368489..7e76eb054 100644 --- a/numpy/ma/tests/test_regression.py +++ b/numpy/ma/tests/test_regression.py @@ -86,6 +86,6 @@ class TestRegression: ma = np.ma.MaskedArray([(1, 1.), (2, 2.), (3, 3.)], dtype='i4,f4') assert_array_equal(ma[[]], ma[:0]) - def test_masked_array_tostring_fortran(self): + def test_masked_array_tobytes_fortran(self): ma = np.ma.arange(4).reshape((2,2)) - assert_array_equal(ma.tostring(order='F'), ma.T.tostring()) + assert_array_equal(ma.tobytes(order='F'), ma.T.tobytes()) diff --git a/numpy/ma/tests/test_subclassing.py b/numpy/ma/tests/test_subclassing.py index 02aeebd17..caa746740 100644 --- a/numpy/ma/tests/test_subclassing.py +++ b/numpy/ma/tests/test_subclassing.py @@ -105,8 +105,6 @@ class CSAIterator: def __next__(self): return next(self._dataiter).__array__().view(type(self._original)) - next = __next__ - class ComplicatedSubArray(SubArray): diff --git a/numpy/polynomial/tests/test_classes.py b/numpy/polynomial/tests/test_classes.py index a2682bd36..e9f256cf8 100644 --- a/numpy/polynomial/tests/test_classes.py +++ b/numpy/polynomial/tests/test_classes.py @@ -13,7 +13,6 @@ from numpy.polynomial import ( from numpy.testing import ( assert_almost_equal, assert_raises, assert_equal, assert_, ) -from numpy.compat import long from numpy.polynomial.polyutils import RankWarning # @@ -315,7 +314,7 @@ def test_truediv(Poly): s = stype(5) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) - for stype in (int, long, float): + for stype in (int, float): s = stype(5) assert_poly_almost_equal(op.truediv(p2, s), p1) assert_raises(TypeError, op.truediv, s, p2) diff --git a/numpy/random/tests/test_generator_mt19937_regressions.py b/numpy/random/tests/test_generator_mt19937_regressions.py index f0b502d06..456c932d4 100644 --- a/numpy/random/tests/test_generator_mt19937_regressions.py +++ b/numpy/random/tests/test_generator_mt19937_regressions.py @@ -1,5 +1,4 @@ from numpy.testing import (assert_, assert_array_equal) -from numpy.compat import long import numpy as np import pytest from numpy.random import Generator, MT19937 @@ -41,13 +40,6 @@ class TestRegression: msg = "Frequency was %f, should be < 0.23" % freq assert_(freq < 0.23, msg) - def test_permutation_longs(self): - mt19937 = Generator(MT19937(1234)) - a = mt19937.permutation(12) - mt19937 = Generator(MT19937(1234)) - b = mt19937.permutation(long(12)) - assert_array_equal(a, b) - def test_shuffle_mixed_dimension(self): # Test for trac ticket #2074 for t in [[1, 2, 3, None], diff --git a/numpy/random/tests/test_randomstate_regression.py b/numpy/random/tests/test_randomstate_regression.py index 1d8a0ed5a..4eb82fc4c 100644 --- a/numpy/random/tests/test_randomstate_regression.py +++ b/numpy/random/tests/test_randomstate_regression.py @@ -5,7 +5,6 @@ import pytest from numpy.testing import ( assert_, assert_array_equal, assert_raises, ) -from numpy.compat import long import numpy as np from numpy import random @@ -52,13 +51,6 @@ class TestRegression: msg = "Frequency was %f, should be < 0.23" % freq assert_(freq < 0.23, msg) - def test_permutation_longs(self): - random.seed(1234) - a = random.permutation(12) - random.seed(1234) - b = random.permutation(long(12)) - assert_array_equal(a, b) - def test_shuffle_mixed_dimension(self): # Test for trac ticket #2074 for t in [[1, 2, 3, None], diff --git a/numpy/random/tests/test_regression.py b/numpy/random/tests/test_regression.py index cd8f3891c..278622287 100644 --- a/numpy/random/tests/test_regression.py +++ b/numpy/random/tests/test_regression.py @@ -3,7 +3,6 @@ from numpy.testing import ( assert_, assert_array_equal, assert_raises, ) from numpy import random -from numpy.compat import long import numpy as np @@ -48,13 +47,6 @@ class TestRegression: msg = "Frequency was %f, should be < 0.23" % freq assert_(freq < 0.23, msg) - def test_permutation_longs(self): - np.random.seed(1234) - a = np.random.permutation(12) - np.random.seed(1234) - b = np.random.permutation(long(12)) - assert_array_equal(a, b) - def test_shuffle_mixed_dimension(self): # Test for trac ticket #2074 for t in [[1, 2, 3, None], |