diff options
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/code_generators/ufunc_docstrings.py | 4 | ||||
-rw-r--r-- | numpy/core/src/common/lowlevel_strided_loops.h | 3 | ||||
-rw-r--r-- | numpy/core/src/multiarray/dtype_transfer.c | 325 | ||||
-rw-r--r-- | numpy/distutils/ccompiler.py | 6 | ||||
-rw-r--r-- | numpy/distutils/misc_util.py | 5 | ||||
-rw-r--r-- | numpy/lib/histograms.py | 3 | ||||
-rw-r--r-- | numpy/lib/shape_base.py | 2 | ||||
-rwxr-xr-x | numpy/linalg/lapack_lite/make_lite.py | 4 | ||||
-rw-r--r-- | numpy/linalg/tests/test_build.py | 4 | ||||
-rw-r--r-- | numpy/linalg/tests/test_linalg.py | 4 | ||||
-rw-r--r-- | numpy/random/_common.pyx | 4 | ||||
-rw-r--r-- | numpy/random/_generator.pyx | 10 | ||||
-rw-r--r-- | numpy/random/_philox.pyx | 5 | ||||
-rw-r--r-- | numpy/random/bit_generator.pyx | 9 | ||||
-rw-r--r-- | numpy/random/tests/test_generator_mt19937.py | 8 | ||||
-rw-r--r-- | numpy/typing/_char_codes.py | 4 |
16 files changed, 39 insertions, 361 deletions
diff --git a/numpy/core/code_generators/ufunc_docstrings.py b/numpy/core/code_generators/ufunc_docstrings.py index 04181fbc2..2f75cb41f 100644 --- a/numpy/core/code_generators/ufunc_docstrings.py +++ b/numpy/core/code_generators/ufunc_docstrings.py @@ -2808,8 +2808,8 @@ add_newdoc('numpy.core.umath', 'matmul', (9, 5, 7, 3) >>> # n is 7, k is 4, m is 3 - The matmul function implements the semantics of the `@` operator introduced - in Python 3.5 following PEP465. + The matmul function implements the semantics of the ``@`` operator introduced + in Python 3.5 following :pep:`465`. Examples -------- diff --git a/numpy/core/src/common/lowlevel_strided_loops.h b/numpy/core/src/common/lowlevel_strided_loops.h index bda9bb5e4..014103f13 100644 --- a/numpy/core/src/common/lowlevel_strided_loops.h +++ b/numpy/core/src/common/lowlevel_strided_loops.h @@ -159,8 +159,7 @@ PyArray_GetDTypeCopySwapFn(int aligned, * Should be the dst stride if it will always be the same, * NPY_MAX_INTP otherwise. * src_dtype: - * The data type of source data. If this is NULL, a transfer - * function which sets the destination to zeros is produced. + * The data type of source data. Must not be NULL. * dst_dtype: * The data type of destination data. If this is NULL and * move_references is 1, a transfer function which decrements diff --git a/numpy/core/src/multiarray/dtype_transfer.c b/numpy/core/src/multiarray/dtype_transfer.c index fa17caced..ae3834e15 100644 --- a/numpy/core/src/multiarray/dtype_transfer.c +++ b/numpy/core/src/multiarray/dtype_transfer.c @@ -81,29 +81,6 @@ get_decsrcref_transfer_function(int aligned, NpyAuxData **out_transferdata, int *out_needs_api); -/* - * Returns a transfer function which zeros out the dest values. - * - * Returns NPY_SUCCEED or NPY_FAIL. - */ -static int -get_setdstzero_transfer_function(int aligned, - npy_intp dst_stride, - PyArray_Descr *dst_dtype, - PyArray_StridedUnaryOp **out_stransfer, - NpyAuxData **out_transferdata, - int *out_needs_api); - -/* - * Returns a transfer function which sets a boolean type to ones. - * - * Returns NPY_SUCCEED or NPY_FAIL. - */ -NPY_NO_EXPORT int -get_bool_setdstone_transfer_function(npy_intp dst_stride, - PyArray_StridedUnaryOp **out_stransfer, - NpyAuxData **out_transferdata, - int *NPY_UNUSED(out_needs_api)); /*************************** COPY REFERENCES *******************************/ @@ -3125,69 +3102,6 @@ get_decsrcref_fields_transfer_function(int aligned, return NPY_SUCCEED; } -static int -get_setdestzero_fields_transfer_function(int aligned, - npy_intp dst_stride, - PyArray_Descr *dst_dtype, - PyArray_StridedUnaryOp **out_stransfer, - NpyAuxData **out_transferdata, - int *out_needs_api) -{ - PyObject *names, *key, *tup, *title; - PyArray_Descr *dst_fld_dtype; - npy_int i, names_size, field_count, structsize; - int dst_offset; - _field_transfer_data *data; - _single_field_transfer *fields; - - names = dst_dtype->names; - names_size = PyTuple_GET_SIZE(dst_dtype->names); - - field_count = names_size; - structsize = sizeof(_field_transfer_data) + - field_count * sizeof(_single_field_transfer); - /* Allocate the data and populate it */ - data = (_field_transfer_data *)PyArray_malloc(structsize); - if (data == NULL) { - PyErr_NoMemory(); - return NPY_FAIL; - } - data->base.free = &_field_transfer_data_free; - data->base.clone = &_field_transfer_data_clone; - fields = &data->fields; - - for (i = 0; i < names_size; ++i) { - key = PyTuple_GET_ITEM(names, i); - tup = PyDict_GetItem(dst_dtype->fields, key); - if (!PyArg_ParseTuple(tup, "Oi|O", &dst_fld_dtype, - &dst_offset, &title)) { - PyArray_free(data); - return NPY_FAIL; - } - if (get_setdstzero_transfer_function(0, - dst_stride, - dst_fld_dtype, - &fields[i].stransfer, - &fields[i].data, - out_needs_api) != NPY_SUCCEED) { - for (i = i-1; i >= 0; --i) { - NPY_AUXDATA_FREE(fields[i].data); - } - PyArray_free(data); - return NPY_FAIL; - } - fields[i].src_offset = 0; - fields[i].dst_offset = dst_offset; - fields[i].src_itemsize = 0; - } - - data->field_count = field_count; - - *out_stransfer = &_strided_to_strided_field_transfer; - *out_transferdata = (NpyAuxData *)data; - - return NPY_SUCCEED; -} /************************* MASKED TRANSFER WRAPPER *************************/ @@ -3341,228 +3255,7 @@ _strided_masked_wrapper_transfer_function( } -/************************* DEST BOOL SETONE *******************************/ - -static int -_null_to_strided_set_bool_one(char *dst, - npy_intp dst_stride, - char *NPY_UNUSED(src), npy_intp NPY_UNUSED(src_stride), - npy_intp N, npy_intp NPY_UNUSED(src_itemsize), - NpyAuxData *NPY_UNUSED(data)) -{ - /* bool type is one byte, so can just use the char */ - - while (N > 0) { - *dst = 1; - - dst += dst_stride; - --N; - } - return 0; -} - -static int -_null_to_contig_set_bool_one(char *dst, - npy_intp NPY_UNUSED(dst_stride), - char *NPY_UNUSED(src), npy_intp NPY_UNUSED(src_stride), - npy_intp N, npy_intp NPY_UNUSED(src_itemsize), - NpyAuxData *NPY_UNUSED(data)) -{ - /* bool type is one byte, so can just use the char */ - - memset(dst, 1, N); - return 0; -} - -/* Only for the bool type, sets the destination to 1 */ -NPY_NO_EXPORT int -get_bool_setdstone_transfer_function(npy_intp dst_stride, - PyArray_StridedUnaryOp **out_stransfer, - NpyAuxData **out_transferdata, - int *NPY_UNUSED(out_needs_api)) -{ - if (dst_stride == 1) { - *out_stransfer = &_null_to_contig_set_bool_one; - } - else { - *out_stransfer = &_null_to_strided_set_bool_one; - } - *out_transferdata = NULL; - - return NPY_SUCCEED; -} - -/*************************** DEST SETZERO *******************************/ - -/* Sets dest to zero */ -typedef struct { - NpyAuxData base; - npy_intp dst_itemsize; -} _dst_memset_zero_data; - -/* zero-padded data copy function */ -static NpyAuxData *_dst_memset_zero_data_clone(NpyAuxData *data) -{ - _dst_memset_zero_data *newdata = - (_dst_memset_zero_data *)PyArray_malloc( - sizeof(_dst_memset_zero_data)); - if (newdata == NULL) { - return NULL; - } - - memcpy(newdata, data, sizeof(_dst_memset_zero_data)); - - return (NpyAuxData *)newdata; -} - -static int -_null_to_strided_memset_zero(char *dst, - npy_intp dst_stride, - char *NPY_UNUSED(src), npy_intp NPY_UNUSED(src_stride), - npy_intp N, npy_intp NPY_UNUSED(src_itemsize), - NpyAuxData *data) -{ - _dst_memset_zero_data *d = (_dst_memset_zero_data *)data; - npy_intp dst_itemsize = d->dst_itemsize; - - while (N > 0) { - memset(dst, 0, dst_itemsize); - dst += dst_stride; - --N; - } - return 0; -} - -static int -_null_to_contig_memset_zero(char *dst, - npy_intp dst_stride, - char *NPY_UNUSED(src), npy_intp NPY_UNUSED(src_stride), - npy_intp N, npy_intp NPY_UNUSED(src_itemsize), - NpyAuxData *data) -{ - _dst_memset_zero_data *d = (_dst_memset_zero_data *)data; - npy_intp dst_itemsize = d->dst_itemsize; - - memset(dst, 0, N*dst_itemsize); - return 0; -} - -static int -_null_to_strided_reference_setzero(char *dst, - npy_intp dst_stride, - char *NPY_UNUSED(src), npy_intp NPY_UNUSED(src_stride), - npy_intp N, npy_intp NPY_UNUSED(src_itemsize), - NpyAuxData *NPY_UNUSED(data)) -{ - PyObject *dst_ref = NULL; - - while (N > 0) { - memcpy(&dst_ref, dst, sizeof(dst_ref)); - - /* Release the reference in dst and set it to NULL */ - NPY_DT_DBG_REFTRACE("dec dest ref (to set zero)", dst_ref); - Py_XDECREF(dst_ref); - memset(dst, 0, sizeof(PyObject *)); - - dst += dst_stride; - --N; - } - return 0; -} - -NPY_NO_EXPORT int -get_setdstzero_transfer_function(int aligned, - npy_intp dst_stride, - PyArray_Descr *dst_dtype, - PyArray_StridedUnaryOp **out_stransfer, - NpyAuxData **out_transferdata, - int *out_needs_api) -{ - _dst_memset_zero_data *data; - - /* If there are no references, just set the whole thing to zero */ - if (!PyDataType_REFCHK(dst_dtype)) { - data = (_dst_memset_zero_data *) - PyArray_malloc(sizeof(_dst_memset_zero_data)); - if (data == NULL) { - PyErr_NoMemory(); - return NPY_FAIL; - } - - data->base.free = (NpyAuxData_FreeFunc *)(&PyArray_free); - data->base.clone = &_dst_memset_zero_data_clone; - data->dst_itemsize = dst_dtype->elsize; - - if (dst_stride == data->dst_itemsize) { - *out_stransfer = &_null_to_contig_memset_zero; - } - else { - *out_stransfer = &_null_to_strided_memset_zero; - } - *out_transferdata = (NpyAuxData *)data; - } - /* If it's exactly one reference, use the decref function */ - else if (dst_dtype->type_num == NPY_OBJECT) { - if (out_needs_api) { - *out_needs_api = 1; - } - - *out_stransfer = &_null_to_strided_reference_setzero; - *out_transferdata = NULL; - } - /* If there are subarrays, need to wrap it */ - else if (PyDataType_HASSUBARRAY(dst_dtype)) { - PyArray_Dims dst_shape = {NULL, -1}; - npy_intp dst_size = 1; - PyArray_StridedUnaryOp *contig_stransfer; - NpyAuxData *contig_data; - - if (out_needs_api) { - *out_needs_api = 1; - } - - if (!(PyArray_IntpConverter(dst_dtype->subarray->shape, - &dst_shape))) { - PyErr_SetString(PyExc_ValueError, - "invalid subarray shape"); - return NPY_FAIL; - } - dst_size = PyArray_MultiplyList(dst_shape.ptr, dst_shape.len); - npy_free_cache_dim_obj(dst_shape); - - /* Get a function for contiguous dst of the subarray type */ - if (get_setdstzero_transfer_function(aligned, - dst_dtype->subarray->base->elsize, - dst_dtype->subarray->base, - &contig_stransfer, &contig_data, - out_needs_api) != NPY_SUCCEED) { - return NPY_FAIL; - } - - if (wrap_transfer_function_n_to_n(contig_stransfer, contig_data, - 0, dst_stride, - 0, dst_dtype->subarray->base->elsize, - dst_size, - out_stransfer, out_transferdata) != NPY_SUCCEED) { - NPY_AUXDATA_FREE(contig_data); - return NPY_FAIL; - } - } - /* If there are fields, need to do each field */ - else if (PyDataType_HASFIELDS(dst_dtype)) { - if (out_needs_api) { - *out_needs_api = 1; - } - - return get_setdestzero_fields_transfer_function(aligned, - dst_stride, dst_dtype, - out_stransfer, - out_transferdata, - out_needs_api); - } - - return NPY_SUCCEED; -} +/*************************** CLEAR SRC *******************************/ static int _dec_src_ref_nop(char *NPY_UNUSED(dst), @@ -3775,13 +3468,6 @@ PyArray_LegacyGetDTypeTransferFunction(int aligned, return NPY_SUCCEED; } } - else if (src_dtype == NULL) { - return get_setdstzero_transfer_function(aligned, - dst_dtype->elsize, - dst_dtype, - out_stransfer, out_transferdata, - out_needs_api); - } src_itemsize = src_dtype->elsize; dst_itemsize = dst_dtype->elsize; @@ -4468,6 +4154,8 @@ PyArray_GetDTypeTransferFunction(int aligned, NpyAuxData **out_transferdata, int *out_needs_api) { + assert(src_dtype != NULL); + #if NPY_USE_NEW_CASTINGIMPL /* * If one of the dtypes is NULL, we give back either a src decref @@ -4492,13 +4180,6 @@ PyArray_GetDTypeTransferFunction(int aligned, return NPY_SUCCEED; } } - else if (src_dtype == NULL) { - return get_setdstzero_transfer_function(aligned, - dst_dtype->elsize, - dst_dtype, - out_stransfer, out_transferdata, - out_needs_api); - } if (get_transferfunction_for_descrs(aligned, src_stride, dst_stride, diff --git a/numpy/distutils/ccompiler.py b/numpy/distutils/ccompiler.py index 106436e64..f025c8904 100644 --- a/numpy/distutils/ccompiler.py +++ b/numpy/distutils/ccompiler.py @@ -26,10 +26,8 @@ from numpy.distutils.misc_util import cyg2win32, is_sequence, mingw32, \ _commandline_dep_string # globals for parallel build management -try: - import threading -except ImportError: - import dummy_threading as threading +import threading + _job_semaphore = None _global_lock = threading.Lock() _processing_files = set() diff --git a/numpy/distutils/misc_util.py b/numpy/distutils/misc_util.py index 5392663d6..37e120072 100644 --- a/numpy/distutils/misc_util.py +++ b/numpy/distutils/misc_util.py @@ -10,13 +10,10 @@ import shutil import multiprocessing import textwrap import importlib.util +from threading import local as tlocal import distutils from distutils.errors import DistutilsError -try: - from threading import local as tlocal -except ImportError: - from dummy_threading import local as tlocal # stores temporary directory of each thread to only create one per thread _tdata = tlocal() diff --git a/numpy/lib/histograms.py b/numpy/lib/histograms.py index 7af67a7ee..b6909bc1d 100644 --- a/numpy/lib/histograms.py +++ b/numpy/lib/histograms.py @@ -562,7 +562,8 @@ def histogram_bin_edges(a, bins=10, range=None, weights=None): below, :math:`h` is the binwidth and :math:`n_h` is the number of bins. All estimators that compute bin counts are recast to bin width using the `ptp` of the data. The final bin count is obtained from - ``np.round(np.ceil(range / h))``. + ``np.round(np.ceil(range / h))``. The final bin width is often less + than what is returned by the estimators below. 'auto' (maximum of the 'sturges' and 'fd' estimators) A compromise to get a good value. For small datasets the Sturges diff --git a/numpy/lib/shape_base.py b/numpy/lib/shape_base.py index 9dfeee527..d19bfb8f8 100644 --- a/numpy/lib/shape_base.py +++ b/numpy/lib/shape_base.py @@ -775,7 +775,7 @@ def array_split(ary, indices_or_sections, axis=0): # indices_or_sections is a scalar, not an array. Nsections = int(indices_or_sections) if Nsections <= 0: - raise ValueError('number sections must be larger than 0.') + raise ValueError('number sections must be larger than 0.') from None Neach_section, extras = divmod(Ntotal, Nsections) section_sizes = ([0] + extras * [Neach_section+1] + diff --git a/numpy/linalg/lapack_lite/make_lite.py b/numpy/linalg/lapack_lite/make_lite.py index cf15b2541..b145f6c4f 100755 --- a/numpy/linalg/lapack_lite/make_lite.py +++ b/numpy/linalg/lapack_lite/make_lite.py @@ -261,8 +261,8 @@ def runF2C(fortran_filename, output_dir): subprocess.check_call( ["f2c"] + F2C_ARGS + ['-d', output_dir, fortran_filename] ) - except subprocess.CalledProcessError: - raise F2CError + except subprocess.CalledProcessError as e: + raise F2CError from e def scrubF2CSource(c_file): with open(c_file) as fo: diff --git a/numpy/linalg/tests/test_build.py b/numpy/linalg/tests/test_build.py index 4859226d9..868341ff2 100644 --- a/numpy/linalg/tests/test_build.py +++ b/numpy/linalg/tests/test_build.py @@ -15,8 +15,8 @@ class FindDependenciesLdd: try: p = Popen(self.cmd, stdout=PIPE, stderr=PIPE) stdout, stderr = p.communicate() - except OSError: - raise RuntimeError(f'command {self.cmd} cannot be run') + except OSError as e: + raise RuntimeError(f'command {self.cmd} cannot be run') from e def get_dependencies(self, lfile): p = Popen(self.cmd + [lfile], stdout=PIPE, stderr=PIPE) diff --git a/numpy/linalg/tests/test_linalg.py b/numpy/linalg/tests/test_linalg.py index 21fab58e1..8a270f194 100644 --- a/numpy/linalg/tests/test_linalg.py +++ b/numpy/linalg/tests/test_linalg.py @@ -348,10 +348,10 @@ class LinalgTestCase: try: case.check(self.do) - except Exception: + except Exception as e: msg = f'In test case: {case!r}\n\n' msg += traceback.format_exc() - raise AssertionError(msg) + raise AssertionError(msg) from e class LinalgSquareTestCase(LinalgTestCase): diff --git a/numpy/random/_common.pyx b/numpy/random/_common.pyx index 6d77aed03..19fb34d4d 100644 --- a/numpy/random/_common.pyx +++ b/numpy/random/_common.pyx @@ -25,11 +25,11 @@ cdef uint64_t MAXSIZE = <uint64_t>sys.maxsize cdef object benchmark(bitgen_t *bitgen, object lock, Py_ssize_t cnt, object method): """Benchmark command used by BitGenerator""" cdef Py_ssize_t i - if method==u'uint64': + if method=='uint64': with lock, nogil: for i in range(cnt): bitgen.next_uint64(bitgen.state) - elif method==u'double': + elif method=='double': with lock, nogil: for i in range(cnt): bitgen.next_double(bitgen.state) diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx index 1c4689a70..cc7991eb1 100644 --- a/numpy/random/_generator.pyx +++ b/numpy/random/_generator.pyx @@ -386,7 +386,7 @@ cdef class Generator: 0.0, '', CONS_NONE, None) - def standard_exponential(self, size=None, dtype=np.float64, method=u'zig', out=None): + def standard_exponential(self, size=None, dtype=np.float64, method='zig', out=None): """ standard_exponential(size=None, dtype=np.float64, method='zig', out=None) @@ -426,12 +426,12 @@ cdef class Generator: """ _dtype = np.dtype(dtype) if _dtype == np.float64: - if method == u'zig': + if method == 'zig': return double_fill(&random_standard_exponential_fill, &self._bitgen, size, self.lock, out) else: return double_fill(&random_standard_exponential_inv_fill, &self._bitgen, size, self.lock, out) elif _dtype == np.float32: - if method == u'zig': + if method == 'zig': return float_fill(&random_standard_exponential_fill_f, &self._bitgen, size, self.lock, out) else: return float_fill(&random_standard_exponential_inv_fill_f, &self._bitgen, size, self.lock, out) @@ -4398,7 +4398,9 @@ cdef class Generator: char* x_ptr char* buf_ptr - axis = normalize_axis_index(axis, np.ndim(x)) + if isinstance(x, np.ndarray): + # Only call ndim on ndarrays, see GH 18142 + axis = normalize_axis_index(axis, np.ndim(x)) if type(x) is np.ndarray and x.ndim == 1 and x.size: # Fast, statically typed path: shuffle the underlying buffer. diff --git a/numpy/random/_philox.pyx b/numpy/random/_philox.pyx index 7e8880490..0fe8ebd7c 100644 --- a/numpy/random/_philox.pyx +++ b/numpy/random/_philox.pyx @@ -1,10 +1,5 @@ from cpython.pycapsule cimport PyCapsule_New -try: - from threading import Lock -except ImportError: - from dummy_threading import Lock - import numpy as np cimport numpy as np diff --git a/numpy/random/bit_generator.pyx b/numpy/random/bit_generator.pyx index 9b0c363ef..123d77b40 100644 --- a/numpy/random/bit_generator.pyx +++ b/numpy/random/bit_generator.pyx @@ -43,10 +43,7 @@ except ImportError: from random import SystemRandom randbits = SystemRandom().getrandbits -try: - from threading import Lock -except ImportError: - from dummy_threading import Lock +from threading import Lock from cpython.pycapsule cimport PyCapsule_New @@ -587,8 +584,8 @@ cdef class BitGenerator(): """ return random_raw(&self._bitgen, self.lock, size, output) - def _benchmark(self, Py_ssize_t cnt, method=u'uint64'): - '''Used in tests''' + def _benchmark(self, Py_ssize_t cnt, method='uint64'): + """Used in tests""" return benchmark(&self._bitgen, self.lock, cnt, method) @property diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py index 47c81584c..d68bcd38b 100644 --- a/numpy/random/tests/test_generator_mt19937.py +++ b/numpy/random/tests/test_generator_mt19937.py @@ -2526,3 +2526,11 @@ def test_broadcast_size_scalar(): random.normal(mu, sigma, size=3) with pytest.raises(ValueError): random.normal(mu, sigma, size=2) + + +def test_ragged_shuffle(): + # GH 18142 + seq = [[], [], 1] + gen = Generator(MT19937(0)) + assert_no_warnings(gen.shuffle, seq) + assert seq == [1, [], []] diff --git a/numpy/typing/_char_codes.py b/numpy/typing/_char_codes.py index 143644e88..6b6f7ae88 100644 --- a/numpy/typing/_char_codes.py +++ b/numpy/typing/_char_codes.py @@ -48,11 +48,11 @@ if TYPE_CHECKING or HAVE_LITERAL: _HalfCodes = Literal["half", "e", "=e", "<e", ">e"] _SingleCodes = Literal["single", "f", "=f", "<f", ">f"] - _DoubleCodes = Literal["double" "float", "float_", "d", "=d", "<d", ">d"] + _DoubleCodes = Literal["double", "float", "float_", "d", "=d", "<d", ">d"] _LongDoubleCodes = Literal["longdouble", "longfloat", "g", "=g", "<g", ">g"] _CSingleCodes = Literal["csingle", "singlecomplex", "F", "=F", "<F", ">F"] - _CDoubleCodes = Literal["cdouble" "complex", "complex_", "cfloat", "D", "=D", "<D", ">D"] + _CDoubleCodes = Literal["cdouble", "complex", "complex_", "cfloat", "D", "=D", "<D", ">D"] _CLongDoubleCodes = Literal["clongdouble", "clongfloat", "longcomplex", "G", "=G", "<G", ">G"] _StrCodes = Literal["str", "str_", "str0", "unicode", "unicode_", "U", "=U", "<U", ">U"] |