diff options
Diffstat (limited to 'numpy')
49 files changed, 695 insertions, 351 deletions
diff --git a/numpy/compat/setup.py b/numpy/compat/setup.py index 26161f330..882857428 100644 --- a/numpy/compat/setup.py +++ b/numpy/compat/setup.py @@ -1,10 +1,10 @@ -#!/usr/bin/env python from __future__ import division, print_function - def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration + config = Configuration('compat', parent_package, top_path) + config.add_data_dir('tests') return config if __name__ == '__main__': diff --git a/numpy/conftest.py b/numpy/conftest.py index 15985a75b..ce985d079 100644 --- a/numpy/conftest.py +++ b/numpy/conftest.py @@ -8,7 +8,7 @@ import pytest import numpy import importlib -from numpy.core.multiarray_tests import get_fpu_mode +from numpy.core._multiarray_tests import get_fpu_mode _old_fpu_mode = None @@ -21,7 +21,7 @@ def pytest_itemcollected(item): Check FPU precision mode was not changed during test collection. The clumsy way we do it here is mainly necessary because numpy - still uses yield tests, which can execute code at test collection + still uses yield tests, which can execute code at test collection time. """ global _old_fpu_mode diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index d2348f364..d154206c5 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -1,7 +1,7 @@ from __future__ import division, absolute_import, print_function try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: @@ -1089,7 +1089,7 @@ def outer(a, b, out=None): References ---------- - .. [1] : G. H. Golub and C. F. van Loan, *Matrix Computations*, 3rd + .. [1] : G. H. Golub and C. F. Van Loan, *Matrix Computations*, 3rd ed., Baltimore, MD, Johns Hopkins University Press, 1996, pg. 8. diff --git a/numpy/core/setup.py b/numpy/core/setup.py index 11b1acb07..d519e0eb8 100644 --- a/numpy/core/setup.py +++ b/numpy/core/setup.py @@ -924,29 +924,29 @@ def configuration(parent_package='',top_path=None): # umath_tests module # ####################################################################### - config.add_extension('umath_tests', - sources=[join('src', 'umath', 'umath_tests.c.src')]) + config.add_extension('_umath_tests', + sources=[join('src', 'umath', '_umath_tests.c.src')]) ####################################################################### # custom rational dtype module # ####################################################################### - config.add_extension('test_rational', - sources=[join('src', 'umath', 'test_rational.c.src')]) + config.add_extension('_rational_tests', + sources=[join('src', 'umath', '_rational_tests.c.src')]) ####################################################################### # struct_ufunc_test module # ####################################################################### - config.add_extension('struct_ufunc_test', - sources=[join('src', 'umath', 'struct_ufunc_test.c.src')]) + config.add_extension('_struct_ufunc_tests', + sources=[join('src', 'umath', '_struct_ufunc_tests.c.src')]) ####################################################################### # multiarray_tests module # ####################################################################### - config.add_extension('multiarray_tests', - sources=[join('src', 'multiarray', 'multiarray_tests.c.src'), + config.add_extension('_multiarray_tests', + sources=[join('src', 'multiarray', '_multiarray_tests.c.src'), join('src', 'private', 'mem_overlap.c')], depends=[join('src', 'private', 'mem_overlap.h'), join('src', 'private', 'npy_extint128.h')], @@ -956,8 +956,8 @@ def configuration(parent_package='',top_path=None): # operand_flag_tests module # ####################################################################### - config.add_extension('operand_flag_tests', - sources=[join('src', 'umath', 'operand_flag_tests.c.src')]) + config.add_extension('_operand_flag_tests', + sources=[join('src', 'umath', '_operand_flag_tests.c.src')]) config.add_data_dir('tests') config.add_data_dir('tests/data') diff --git a/numpy/core/src/multiarray/multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src index d63349560..afc6db1aa 100644 --- a/numpy/core/src/multiarray/multiarray_tests.c.src +++ b/numpy/core/src/multiarray/_multiarray_tests.c.src @@ -1867,7 +1867,7 @@ static PyMethodDef Multiarray_TestsMethods[] = { #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, - "multiarray_tests", + "_multiarray_tests", NULL, -1, Multiarray_TestsMethods, @@ -1880,11 +1880,11 @@ static struct PyModuleDef moduledef = { #if defined(NPY_PY3K) #define RETVAL m -PyMODINIT_FUNC PyInit_multiarray_tests(void) +PyMODINIT_FUNC PyInit__multiarray_tests(void) #else #define RETVAL PyMODINIT_FUNC -initmultiarray_tests(void) +init_multiarray_tests(void) #endif { PyObject *m; @@ -1892,7 +1892,7 @@ initmultiarray_tests(void) #if defined(NPY_PY3K) m = PyModule_Create(&moduledef); #else - m = Py_InitModule("multiarray_tests", Multiarray_TestsMethods); + m = Py_InitModule("_multiarray_tests", Multiarray_TestsMethods); #endif if (m == NULL) { return RETVAL; @@ -1900,7 +1900,7 @@ initmultiarray_tests(void) import_array(); if (PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, - "cannot load umath_tests module."); + "cannot load _multiarray_tests module."); } return RETVAL; } diff --git a/numpy/core/src/multiarray/buffer.c b/numpy/core/src/multiarray/buffer.c index 4aa25a196..21dbdefd6 100644 --- a/numpy/core/src/multiarray/buffer.c +++ b/numpy/core/src/multiarray/buffer.c @@ -15,6 +15,7 @@ #include "common.h" #include "numpyos.h" #include "arrayobject.h" +#include "scalartypes.h" /************************************************************************* **************** Implement Buffer Protocol **************************** @@ -176,7 +177,7 @@ _is_natively_aligned_at(PyArray_Descr *descr, static int _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, - PyArrayObject* arr, Py_ssize_t *offset, + PyObject* obj, Py_ssize_t *offset, char *active_byteorder) { int k; @@ -223,7 +224,7 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, Py_DECREF(subarray_tuple); old_offset = *offset; - ret = _buffer_format_string(descr->subarray->base, str, arr, offset, + ret = _buffer_format_string(descr->subarray->base, str, obj, offset, active_byteorder); *offset = old_offset + (*offset - old_offset) * total_count; return ret; @@ -265,7 +266,7 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, } /* Insert child item */ - _buffer_format_string(child, str, arr, offset, + _buffer_format_string(child, str, obj, offset, active_byteorder); /* Insert field name */ @@ -302,6 +303,7 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, } else { int is_standard_size = 1; + int is_natively_aligned; int is_native_only_type = (descr->type_num == NPY_LONGDOUBLE || descr->type_num == NPY_CLONGDOUBLE); if (sizeof(npy_longlong) != 8) { @@ -312,8 +314,16 @@ _buffer_format_string(PyArray_Descr *descr, _tmp_string_t *str, *offset += descr->elsize; - if (descr->byteorder == '=' && - _is_natively_aligned_at(descr, arr, *offset)) { + if (PyArray_IsScalar(obj, Generic)) { + /* scalars are always natively aligned */ + is_natively_aligned = 1; + } + else { + is_natively_aligned = _is_natively_aligned_at(descr, + (PyArrayObject*)obj, *offset); + } + + if (descr->byteorder == '=' && is_natively_aligned) { /* Prefer native types, to cater for Cython */ is_standard_size = 0; if (*active_byteorder != '@') { @@ -448,43 +458,61 @@ static PyObject *_buffer_info_cache = NULL; /* Fill in the info structure */ static _buffer_info_t* -_buffer_info_new(PyArrayObject *arr) +_buffer_info_new(PyObject *obj) { _buffer_info_t *info; _tmp_string_t fmt = {NULL, 0, 0}; int k; + PyArray_Descr *descr = NULL; + int err = 0; info = malloc(sizeof(_buffer_info_t)); if (info == NULL) { goto fail; } - /* Fill in format */ - if (_buffer_format_string(PyArray_DESCR(arr), &fmt, arr, NULL, NULL) != 0) { - free(fmt.s); - goto fail; - } - _append_char(&fmt, '\0'); - info->format = fmt.s; - - /* Fill in shape and strides */ - info->ndim = PyArray_NDIM(arr); - - if (info->ndim == 0) { + if (PyArray_IsScalar(obj, Generic)) { + descr = PyArray_DescrFromScalar(obj); + if (descr == NULL) { + goto fail; + } + info->ndim = 0; info->shape = NULL; info->strides = NULL; } else { - info->shape = malloc(sizeof(Py_ssize_t) * PyArray_NDIM(arr) * 2 + 1); - if (info->shape == NULL) { - goto fail; + PyArrayObject * arr = (PyArrayObject *)obj; + descr = PyArray_DESCR(arr); + /* Fill in shape and strides */ + info->ndim = PyArray_NDIM(arr); + + if (info->ndim == 0) { + info->shape = NULL; + info->strides = NULL; } - info->strides = info->shape + PyArray_NDIM(arr); - for (k = 0; k < PyArray_NDIM(arr); ++k) { - info->shape[k] = PyArray_DIMS(arr)[k]; - info->strides[k] = PyArray_STRIDES(arr)[k]; + else { + info->shape = malloc(sizeof(Py_ssize_t) * PyArray_NDIM(arr) * 2 + 1); + if (info->shape == NULL) { + goto fail; + } + info->strides = info->shape + PyArray_NDIM(arr); + for (k = 0; k < PyArray_NDIM(arr); ++k) { + info->shape[k] = PyArray_DIMS(arr)[k]; + info->strides[k] = PyArray_STRIDES(arr)[k]; + } } + Py_INCREF(descr); + } + + /* Fill in format */ + err = _buffer_format_string(descr, &fmt, obj, NULL, NULL); + Py_DECREF(descr); + if (err != 0) { + free(fmt.s); + goto fail; } + _append_char(&fmt, '\0'); + info->format = fmt.s; return info; @@ -530,7 +558,7 @@ _buffer_info_free(_buffer_info_t *info) /* Get buffer info from the global dictionary */ static _buffer_info_t* -_buffer_get_info(PyObject *arr) +_buffer_get_info(PyObject *obj) { PyObject *key = NULL, *item_list = NULL, *item = NULL; _buffer_info_t *info = NULL, *old_info = NULL; @@ -543,13 +571,13 @@ _buffer_get_info(PyObject *arr) } /* Compute information */ - info = _buffer_info_new((PyArrayObject*)arr); + info = _buffer_info_new(obj); if (info == NULL) { return NULL; } /* Check if it is identical with an old one; reuse old one, if yes */ - key = PyLong_FromVoidPtr((void*)arr); + key = PyLong_FromVoidPtr((void*)obj); if (key == NULL) { goto fail; } @@ -627,9 +655,8 @@ _buffer_clear_info(PyObject *arr) } /* - * Retrieving buffers + * Retrieving buffers for ndarray */ - static int array_getbuffer(PyObject *obj, Py_buffer *view, int flags) { @@ -751,6 +778,62 @@ fail: return -1; } +/* + * Retrieving buffers for scalars + */ +int +gentype_getbuffer(PyObject *self, Py_buffer *view, int flags) +{ + _buffer_info_t *info = NULL; + PyArray_Descr *descr = NULL; + int elsize; + + if (flags & PyBUF_WRITABLE) { + PyErr_SetString(PyExc_BufferError, "scalar buffer is readonly"); + goto fail; + } + + /* Fill in information */ + info = _buffer_get_info(self); + if (info == NULL) { + PyErr_SetString(PyExc_BufferError, + "could not get scalar buffer information"); + goto fail; + } + + view->ndim = info->ndim; + view->shape = info->shape; + view->strides = info->strides; + + if ((flags & PyBUF_FORMAT) == PyBUF_FORMAT) { + view->format = info->format; + } else { + view->format = NULL; + } + + descr = PyArray_DescrFromScalar(self); + view->buf = (void *)scalar_value(self, descr); + elsize = descr->elsize; +#ifndef Py_UNICODE_WIDE + if (descr->type_num == NPY_UNICODE) { + elsize >>= 1; + } +#endif + view->len = elsize; + view->itemsize = elsize; + + Py_DECREF(descr); + + view->readonly = 1; + view->suboffsets = NULL; + view->obj = self; + Py_INCREF(self); + return 0; + +fail: + view->obj = NULL; + return -1; +} /* * NOTE: for backward compatibility (esp. with PyArg_ParseTuple("s#", ...)) diff --git a/numpy/core/src/multiarray/buffer.h b/numpy/core/src/multiarray/buffer.h index d2ea01b34..d5da8f440 100644 --- a/numpy/core/src/multiarray/buffer.h +++ b/numpy/core/src/multiarray/buffer.h @@ -9,4 +9,7 @@ _array_dealloc_buffer_info(PyArrayObject *self); NPY_NO_EXPORT PyArray_Descr* _descriptor_from_pep3118_format(char *s); +NPY_NO_EXPORT int +gentype_getbuffer(PyObject *obj, Py_buffer *view, int flags); + #endif diff --git a/numpy/core/src/multiarray/einsum.c.src b/numpy/core/src/multiarray/einsum.c.src index 7db606194..5dbc30aa9 100644 --- a/numpy/core/src/multiarray/einsum.c.src +++ b/numpy/core/src/multiarray/einsum.c.src @@ -1829,9 +1829,10 @@ parse_operand_subscripts(char *subscripts, int length, break; } else { - PyErr_SetString(PyExc_ValueError, + PyErr_Format(PyExc_ValueError, "einstein sum subscripts string contains a " - "'.' that is not part of an ellipsis ('...')"); + "'.' that is not part of an ellipsis ('...') in " + "operand %d", iop); return 0; } @@ -1888,6 +1889,12 @@ parse_operand_subscripts(char *subscripts, int length, return 0; } } + else if (label == '.') { + PyErr_Format(PyExc_ValueError, + "einstein sum subscripts string contains a " + "'.' that is not part of an ellipsis ('...') in " + "operand %d", iop); + } else if (label != ' ') { PyErr_Format(PyExc_ValueError, "invalid subscript '%c' in einstein sum " @@ -2011,7 +2018,8 @@ parse_output_subscripts(char *subscripts, int length, else { PyErr_SetString(PyExc_ValueError, "einstein sum subscripts string contains a " - "'.' that is not part of an ellipsis ('...')"); + "'.' that is not part of an ellipsis ('...') " + "in the output"); return -1; } @@ -2037,8 +2045,15 @@ parse_output_subscripts(char *subscripts, int length, if (i > 0) { for (i = 0; i < length; ++i) { label = subscripts[i]; + if (label == '.') { + PyErr_SetString(PyExc_ValueError, + "einstein sum subscripts string contains a " + "'.' that is not part of an ellipsis ('...') " + "in the output"); + return -1; + } /* A label for an axis */ - if (label != '.' && label != ' ') { + else if (label != ' ') { if (idim < ndim_left) { out_labels[idim++] = label; } @@ -2049,12 +2064,6 @@ parse_output_subscripts(char *subscripts, int length, return -1; } } - else { - PyErr_SetString(PyExc_ValueError, - "einstein sum subscripts string contains a " - "'.' that is not part of an ellipsis ('...')"); - return -1; - } } } @@ -2185,7 +2194,7 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels) { npy_intp new_strides[NPY_MAXDIMS]; npy_intp new_dims[NPY_MAXDIMS]; - int i, idim, ndim, icombine, combineoffset, label; + int idim, ndim, icombine, combineoffset; int icombinemap[NPY_MAXDIMS]; PyArrayObject *ret = NULL; @@ -2205,7 +2214,7 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels) * The char type may be either signed or unsigned, we * need it to be signed here. */ - label = (signed char)labels[idim]; + int label = (signed char)labels[idim]; /* If this label says to merge axes, get the actual label */ if (label < 0) { combineoffset = label; @@ -2225,7 +2234,7 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels) } else { /* Update the combined axis dimensions and strides */ - i = idim + combineoffset; + int i = icombinemap[idim + combineoffset]; if (combineoffset < 0 && new_dims[i] != 0 && new_dims[i] != PyArray_DIM(op, idim)) { PyErr_Format(PyExc_ValueError, @@ -2235,7 +2244,6 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels) (int)PyArray_DIM(op, idim)); return NULL; } - i = icombinemap[i]; new_dims[i] = PyArray_DIM(op, idim); new_strides[i] += PyArray_STRIDE(op, idim); } diff --git a/numpy/core/src/multiarray/scalartypes.c.src b/numpy/core/src/multiarray/scalartypes.c.src index 9ea26e5e9..e101c3779 100644 --- a/numpy/core/src/multiarray/scalartypes.c.src +++ b/numpy/core/src/multiarray/scalartypes.c.src @@ -28,6 +28,7 @@ #include "npy_import.h" #include "dragon4.h" #include "npy_longdouble.h" +#include "buffer.h" #include <stdlib.h> @@ -2526,22 +2527,6 @@ gentype_getcharbuf(PyObject *self, Py_ssize_t segment, constchar **ptrptr) } #endif /* !defined(NPY_PY3K) */ - -static int -gentype_getbuffer(PyObject *self, Py_buffer *view, int flags) -{ - Py_ssize_t len; - void *buf; - - /* FIXME: XXX: the format is not implemented! -- this needs more work */ - - len = gentype_getreadbuf(self, 0, &buf); - return PyBuffer_FillInfo(view, self, buf, len, 1, flags); -} - -/* releasebuffer is not needed */ - - static PyBufferProcs gentype_as_buffer = { #if !defined(NPY_PY3K) gentype_getreadbuf, /* bf_getreadbuffer*/ diff --git a/numpy/core/src/npymath/npy_math_complex.c.src b/numpy/core/src/npymath/npy_math_complex.c.src index ea784ec5b..cf427dad8 100644 --- a/numpy/core/src/npymath/npy_math_complex.c.src +++ b/numpy/core/src/npymath/npy_math_complex.c.src @@ -1526,9 +1526,13 @@ const npy_float SQRT_MIN = 1.0842022e-19f; const npy_double SQRT_MIN = 1.4916681462400413e-154; /* sqrt(DBL_MIN) */ #endif #if @precision@ == 3 +#if NPY_SIZEOF_LONGDOUBLE == NPY_SIZEOF_DOUBLE +const npy_longdouble SQRT_MIN = 1.4916681462400413e-154; /* sqrt(DBL_MIN) */ +#else /* this is correct for 80 bit long doubles */ const npy_longdouble SQRT_MIN = 1.8336038675548471656e-2466l; #endif +#endif /* Avoid underflow when y is small. */ if (y < SQRT_MIN) { return (x * x); diff --git a/numpy/core/src/umath/operand_flag_tests.c.src b/numpy/core/src/umath/_operand_flag_tests.c.src index 046c37595..551a9c632 100644 --- a/numpy/core/src/umath/operand_flag_tests.c.src +++ b/numpy/core/src/umath/_operand_flag_tests.c.src @@ -42,7 +42,7 @@ static void *data[1] = {NULL}; #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, - "operand_flag_tests", + "_operand_flag_tests", NULL, -1, TestMethods, @@ -53,11 +53,11 @@ static struct PyModuleDef moduledef = { }; #define RETVAL m -PyMODINIT_FUNC PyInit_operand_flag_tests(void) +PyMODINIT_FUNC PyInit__operand_flag_tests(void) { #else #define RETVAL -PyMODINIT_FUNC initoperand_flag_tests(void) +PyMODINIT_FUNC init_operand_flag_tests(void) { #endif PyObject *m = NULL; @@ -66,7 +66,7 @@ PyMODINIT_FUNC initoperand_flag_tests(void) #if defined(NPY_PY3K) m = PyModule_Create(&moduledef); #else - m = Py_InitModule("operand_flag_tests", TestMethods); + m = Py_InitModule("_operand_flag_tests", TestMethods); #endif if (m == NULL) { goto fail; @@ -92,7 +92,7 @@ PyMODINIT_FUNC initoperand_flag_tests(void) fail: if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, - "cannot load operand_flag_tests module."); + "cannot load _operand_flag_tests module."); } #if defined(NPY_PY3K) if (m) { diff --git a/numpy/core/src/umath/test_rational.c.src b/numpy/core/src/umath/_rational_tests.c.src index ffc92b732..9e74845df 100644 --- a/numpy/core/src/umath/test_rational.c.src +++ b/numpy/core/src/umath/_rational_tests.c.src @@ -1129,7 +1129,7 @@ PyMethodDef module_methods[] = { #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, - "test_rational", + "_rational_tests", NULL, -1, module_methods, @@ -1142,10 +1142,10 @@ static struct PyModuleDef moduledef = { #if defined(NPY_PY3K) #define RETVAL m -PyMODINIT_FUNC PyInit_test_rational(void) { +PyMODINIT_FUNC PyInit__rational_tests(void) { #else #define RETVAL -PyMODINIT_FUNC inittest_rational(void) { +PyMODINIT_FUNC init_rational_tests(void) { #endif PyObject *m = NULL; @@ -1295,7 +1295,7 @@ PyMODINIT_FUNC inittest_rational(void) { #if defined(NPY_PY3K) m = PyModule_Create(&moduledef); #else - m = Py_InitModule("test_rational", module_methods); + m = Py_InitModule("_rational_tests", module_methods); #endif if (!m) { @@ -1397,7 +1397,7 @@ PyMODINIT_FUNC inittest_rational(void) { fail: if (!PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, - "cannot load test_rational module."); + "cannot load _rational_tests module."); } #if defined(NPY_PY3K) if (m) { diff --git a/numpy/core/src/umath/struct_ufunc_test.c.src b/numpy/core/src/umath/_struct_ufunc_tests.c.src index 9a6318f47..b831d5c2a 100644 --- a/numpy/core/src/umath/struct_ufunc_test.c.src +++ b/numpy/core/src/umath/_struct_ufunc_tests.c.src @@ -56,7 +56,7 @@ static void add_uint64_triplet(char **args, npy_intp *dimensions, #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, - "struct_ufunc_test", + "_struct_ufunc_tests", NULL, -1, StructUfuncTestMethods, @@ -68,9 +68,9 @@ static struct PyModuleDef moduledef = { #endif #if defined(NPY_PY3K) -PyMODINIT_FUNC PyInit_struct_ufunc_test(void) +PyMODINIT_FUNC PyInit__struct_ufunc_tests(void) #else -PyMODINIT_FUNC initstruct_ufunc_test(void) +PyMODINIT_FUNC init_struct_ufunc_tests(void) #endif { PyObject *m, *add_triplet, *d; @@ -81,7 +81,7 @@ PyMODINIT_FUNC initstruct_ufunc_test(void) #if defined(NPY_PY3K) m = PyModule_Create(&moduledef); #else - m = Py_InitModule("struct_ufunc_test", StructUfuncTestMethods); + m = Py_InitModule("_struct_ufunc_tests", StructUfuncTestMethods); #endif if (m == NULL) { diff --git a/numpy/core/src/umath/umath_tests.c.src b/numpy/core/src/umath/_umath_tests.c.src index 8d9009a1a..120ce0332 100644 --- a/numpy/core/src/umath/umath_tests.c.src +++ b/numpy/core/src/umath/_umath_tests.c.src @@ -360,7 +360,7 @@ static PyMethodDef UMath_TestsMethods[] = { #if defined(NPY_PY3K) static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, - "umath_tests", + "_umath_tests", NULL, -1, UMath_TestsMethods, @@ -373,11 +373,11 @@ static struct PyModuleDef moduledef = { #if defined(NPY_PY3K) #define RETVAL m -PyMODINIT_FUNC PyInit_umath_tests(void) +PyMODINIT_FUNC PyInit__umath_tests(void) #else #define RETVAL PyMODINIT_FUNC -initumath_tests(void) +init_umath_tests(void) #endif { PyObject *m; @@ -387,7 +387,7 @@ initumath_tests(void) #if defined(NPY_PY3K) m = PyModule_Create(&moduledef); #else - m = Py_InitModule("umath_tests", UMath_TestsMethods); + m = Py_InitModule("_umath_tests", UMath_TestsMethods); #endif if (m == NULL) return RETVAL; @@ -406,7 +406,7 @@ initumath_tests(void) if (PyErr_Occurred()) { PyErr_SetString(PyExc_RuntimeError, - "cannot load umath_tests module."); + "cannot load _umath_tests module."); } return RETVAL; diff --git a/numpy/core/tests/_locales.py b/numpy/core/tests/_locales.py new file mode 100644 index 000000000..28eebb14d --- /dev/null +++ b/numpy/core/tests/_locales.py @@ -0,0 +1,76 @@ +"""Provide class for testing in French locale + +""" +from __future__ import division, absolute_import, print_function + +import sys +import locale + +from numpy.testing import SkipTest + +__ALL__ = ['CommaDecimalPointLocale'] + + +def find_comma_decimal_point_locale(): + """See if platform has a decimal point as comma locale. + + Find a locale that uses a comma instead of a period as the + decimal point. + + Returns + ------- + old_locale: str + Locale when the function was called. + new_locale: {str, None) + First French locale found, None if none found. + + """ + if sys.platform == 'win32': + locales = ['FRENCH'] + else: + locales = ['fr_FR', 'fr_FR.UTF-8', 'fi_FI', 'fi_FI.UTF-8'] + + old_locale = locale.getlocale(locale.LC_NUMERIC) + new_locale = None + try: + for loc in locales: + try: + locale.setlocale(locale.LC_NUMERIC, loc) + new_locale = loc + break + except locale.Error: + pass + finally: + locale.setlocale(locale.LC_NUMERIC, locale=old_locale) + return old_locale, new_locale + + +class CommaDecimalPointLocale(object): + """Sets LC_NUMERIC to a locale with comma as decimal point. + + Classes derived from this class have setup and teardown methods that run + tests with locale.LC_NUMERIC set to a locale where commas (',') are used as + the decimal point instead of periods ('.'). On exit the locale is restored + to the initial locale. It also serves as context manager with the same + effect. If no such locale is available, it raises SkipTest in both cases. + + .. versionadded:: 1.15.0 + + """ + (cur_locale, tst_locale) = find_comma_decimal_point_locale() + + def setup(self): + if self.tst_locale is None: + raise SkipTest("No French locale available") + locale.setlocale(locale.LC_NUMERIC, locale=self.tst_locale) + + def teardown(self): + locale.setlocale(locale.LC_NUMERIC, locale=self.cur_locale) + + def __enter__(self): + if self.tst_locale is None: + raise SkipTest("No French locale available") + locale.setlocale(locale.LC_NUMERIC, locale=self.tst_locale) + + def __exit__(self, type, value, traceback): + locale.setlocale(locale.LC_NUMERIC, locale=self.cur_locale) diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py index fe0c7cc5f..2c2900e6c 100644 --- a/numpy/core/tests/test_deprecations.py +++ b/numpy/core/tests/test_deprecations.py @@ -429,7 +429,7 @@ class TestNonNumericConjugate(_DeprecationTestCase): class TestNPY_CHAR(_DeprecationTestCase): # 2017-05-03, 1.13.0 def test_npy_char_deprecation(self): - from numpy.core.multiarray_tests import npy_char_deprecation + from numpy.core._multiarray_tests import npy_char_deprecation self.assert_deprecated(npy_char_deprecation) assert_(npy_char_deprecation() == 'S1') @@ -440,11 +440,11 @@ class Test_UPDATEIFCOPY(_DeprecationTestCase): WRITEBACKIFCOPY instead """ def test_npy_updateifcopy_deprecation(self): - from numpy.core.multiarray_tests import npy_updateifcopy_deprecation + from numpy.core._multiarray_tests import npy_updateifcopy_deprecation arr = np.arange(9).reshape(3, 3) v = arr.T self.assert_deprecated(npy_updateifcopy_deprecation, args=(v,)) - + class TestDatetimeEvent(_DeprecationTestCase): # 2017-08-11, 1.14.0 diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 2f997b4f7..c924e6f43 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -5,7 +5,7 @@ import sys import operator import numpy as np -from numpy.core.test_rational import rational +from numpy.core._rational_tests import rational from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_raises, dec diff --git a/numpy/core/tests/test_einsum.py b/numpy/core/tests/test_einsum.py index 9bd85fdb9..406b10eab 100644 --- a/numpy/core/tests/test_einsum.py +++ b/numpy/core/tests/test_einsum.py @@ -1,5 +1,7 @@ from __future__ import division, absolute_import, print_function +import itertools + import numpy as np from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_array_equal, @@ -791,6 +793,12 @@ class TestEinSum(object): self.optimize_compare('dba,ead,cad->bce') self.optimize_compare('aef,fbc,dca->bde') + def test_combined_views_mapping(self): + # gh-10792 + a = np.arange(9).reshape(1, 1, 3, 1, 3) + b = np.einsum('bbcdc->d', a) + assert_equal(b, [12]) + class TestEinSumPath(object): def build_operands(self, string, size_dict=global_size_dict): @@ -918,6 +926,13 @@ class TestEinSumPath(object): opt = np.einsum(*path_test, optimize=exp_path) assert_almost_equal(noopt, opt) + def test_spaces(self): + #gh-10794 + arr = np.array([[1]]) + for sp in itertools.product(['', ' '], repeat=4): + # no error for any spacing + np.einsum('{}...a{}->{}...a{}'.format(*sp), arr) + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_extint128.py b/numpy/core/tests/test_extint128.py index d87585dcf..31786124d 100644 --- a/numpy/core/tests/test_extint128.py +++ b/numpy/core/tests/test_extint128.py @@ -6,7 +6,7 @@ import contextlib import operator import numpy as np -import numpy.core.multiarray_tests as mt +import numpy.core._multiarray_tests as mt from numpy.compat import long from numpy.testing import assert_raises, assert_equal, dec diff --git a/numpy/core/tests/test_indexing.py b/numpy/core/tests/test_indexing.py index 3a02c9fce..082ecb496 100644 --- a/numpy/core/tests/test_indexing.py +++ b/numpy/core/tests/test_indexing.py @@ -6,7 +6,7 @@ import functools import operator import numpy as np -from numpy.core.multiarray_tests import array_indexing +from numpy.core._multiarray_tests import array_indexing from itertools import product from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_raises, diff --git a/numpy/core/tests/test_longdouble.py b/numpy/core/tests/test_longdouble.py index 625d40c1b..7cd5b04d8 100644 --- a/numpy/core/tests/test_longdouble.py +++ b/numpy/core/tests/test_longdouble.py @@ -7,7 +7,7 @@ from numpy.testing import ( run_module_suite, assert_, assert_equal, dec, assert_raises, assert_array_equal, temppath, ) -from .test_print import in_foreign_locale +from ._locales import CommaDecimalPointLocale LD_INFO = np.finfo(np.longdouble) longdouble_longer_than_double = (LD_INFO.eps < np.finfo(np.double).eps) @@ -50,25 +50,12 @@ def test_bytes(): np.longdouble(b"1.2") -@in_foreign_locale -def test_fromstring_foreign_repr(): - f = 1.234 - a = np.fromstring(repr(f), dtype=float, sep=" ") - assert_equal(a[0], f) - - @dec.knownfailureif(string_to_longdouble_inaccurate, "Need strtold_l") def test_repr_roundtrip_bytes(): o = 1 + LD_INFO.eps assert_equal(np.longdouble(repr(o).encode("ascii")), o) -@in_foreign_locale -def test_repr_roundtrip_foreign(): - o = 1.5 - assert_equal(o, np.longdouble(repr(o))) - - def test_bogus_string(): assert_raises(ValueError, np.longdouble, "spam") assert_raises(ValueError, np.longdouble, "1.0 flub") @@ -83,18 +70,6 @@ def test_fromstring(): err_msg="reading '%s'" % s) -@in_foreign_locale -def test_fromstring_best_effort_float(): - assert_equal(np.fromstring("1,234", dtype=float, sep=" "), - np.array([1.])) - - -@in_foreign_locale -def test_fromstring_best_effort(): - assert_equal(np.fromstring("1,234", dtype=np.longdouble, sep=" "), - np.array([1.])) - - def test_fromstring_bogus(): assert_equal(np.fromstring("1. 2. 3. flop 4.", dtype=float, sep=" "), np.array([1., 2., 3.])) @@ -155,26 +130,6 @@ class TestFileBased(object): assert_equal(res, self.tgt) -@in_foreign_locale -def test_fromstring_foreign(): - s = "1.234" - a = np.fromstring(s, dtype=np.longdouble, sep=" ") - assert_equal(a[0], np.longdouble(s)) - - -@in_foreign_locale -def test_fromstring_foreign_sep(): - a = np.array([1, 2, 3, 4]) - b = np.fromstring("1,2,3,4,", dtype=np.longdouble, sep=",") - assert_array_equal(a, b) - - -@in_foreign_locale -def test_fromstring_foreign_value(): - b = np.fromstring("1,234", dtype=np.longdouble, sep=" ") - assert_array_equal(b[0], 1) - - # Conversions long double -> string @@ -207,6 +162,43 @@ def test_array_repr(): raise ValueError("precision loss creating arrays") assert_(repr(a) != repr(b)) +# +# Locale tests: scalar types formatting should be independent of the locale +# + +class TestCommaDecimalPointLocale(CommaDecimalPointLocale): + + def test_repr_roundtrip_foreign(self): + o = 1.5 + assert_equal(o, np.longdouble(repr(o))) + + def test_fromstring_foreign_repr(self): + f = 1.234 + a = np.fromstring(repr(f), dtype=float, sep=" ") + assert_equal(a[0], f) + + def test_fromstring_best_effort_float(self): + assert_equal(np.fromstring("1,234", dtype=float, sep=" "), + np.array([1.])) + + def test_fromstring_best_effort(self): + assert_equal(np.fromstring("1,234", dtype=np.longdouble, sep=" "), + np.array([1.])) + + def test_fromstring_foreign(self): + s = "1.234" + a = np.fromstring(s, dtype=np.longdouble, sep=" ") + assert_equal(a[0], np.longdouble(s)) + + def test_fromstring_foreign_sep(self): + a = np.array([1, 2, 3, 4]) + b = np.fromstring("1,2,3,4,", dtype=np.longdouble, sep=",") + assert_array_equal(a, b) + + def test_fromstring_foreign_value(self): + b = np.fromstring("1,234", dtype=np.longdouble, sep=" ") + assert_array_equal(b[0], 1) + if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_mem_overlap.py b/numpy/core/tests/test_mem_overlap.py index 9c17ed210..92baa0896 100644 --- a/numpy/core/tests/test_mem_overlap.py +++ b/numpy/core/tests/test_mem_overlap.py @@ -7,8 +7,8 @@ import numpy as np from numpy.testing import (run_module_suite, assert_, assert_raises, assert_equal, assert_array_equal, assert_allclose, dec) -from numpy.core.multiarray_tests import solve_diophantine, internal_overlap -from numpy.core import umath_tests +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 @@ -749,7 +749,7 @@ class TestUFunc(object): def test_unary_gufunc_fuzz(self): shapes = [7, 13, 8, 21, 29, 32] - gufunc = umath_tests.euclidean_pdist + gufunc = _umath_tests.euclidean_pdist rng = np.random.RandomState(1234) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 940072238..d861da4b6 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1,7 +1,7 @@ from __future__ import division, absolute_import, print_function try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: @@ -26,18 +26,14 @@ from decimal import Decimal import numpy as np from numpy.compat import strchar, unicode -from numpy.core.tests.test_print import in_foreign_locale -from numpy.core.multiarray_tests import ( - test_neighborhood_iterator, test_neighborhood_iterator_oob, - test_pydatamem_seteventhook_start, test_pydatamem_seteventhook_end, - test_inplace_increment, get_buffer_info, test_as_c_array, - ) +import numpy.core._multiarray_tests as _multiarray_tests from numpy.testing import ( run_module_suite, assert_, assert_raises, assert_warns, assert_equal, assert_almost_equal, assert_array_equal, assert_raises_regex, assert_array_almost_equal, assert_allclose, IS_PYPY, HAS_REFCOUNT, assert_array_less, runstring, dec, SkipTest, temppath, suppress_warnings ) +from ._locales import CommaDecimalPointLocale # Need to test an object that does not fully implement math interface from datetime import timedelta, datetime @@ -195,7 +191,7 @@ class TestAttributes(object): assert_equal(isinstance(numpy_int, int), True) # ... and fast-path checks on C-API level should also work - from numpy.core.multiarray_tests import test_int_subclass + from numpy.core._multiarray_tests import test_int_subclass assert_equal(test_int_subclass(numpy_int), True) def test_stridesattr(self): @@ -3346,7 +3342,7 @@ class TestTemporaryElide(object): # def incref_elide(a): # d = input.copy() # refcount 1 # return d, d + d # PyNumber_Add without increasing refcount - from numpy.core.multiarray_tests import incref_elide + from numpy.core._multiarray_tests import incref_elide d = np.ones(100000) orig, res = incref_elide(d) d + d @@ -3361,7 +3357,7 @@ class TestTemporaryElide(object): # # def incref_elide_l(d): # return l[4] + l[4] # PyNumber_Add without increasing refcount - from numpy.core.multiarray_tests import incref_elide_l + from numpy.core._multiarray_tests import incref_elide_l # padding with 1 makes sure the object on the stack is not overwritten l = [1, 1, 1, 1, np.ones(100000)] res = incref_elide_l(l) @@ -3440,7 +3436,7 @@ class TestTemporaryElide(object): class TestCAPI(object): def test_IsPythonScalar(self): - from numpy.core.multiarray_tests import IsPythonScalar + from numpy.core._multiarray_tests import IsPythonScalar assert_(IsPythonScalar(b'foobar')) assert_(IsPythonScalar(1)) assert_(IsPythonScalar(2**80)) @@ -4477,14 +4473,15 @@ class TestIO(object): assert_equal(s, '1.51,2.00,3.51,4.00') def test_locale(self): - in_foreign_locale(self.test_numbers)() - in_foreign_locale(self.test_nan)() - in_foreign_locale(self.test_inf)() - in_foreign_locale(self.test_counted_string)() - in_foreign_locale(self.test_ascii)() - in_foreign_locale(self.test_malformed)() - in_foreign_locale(self.test_tofile_sep)() - in_foreign_locale(self.test_tofile_format)() + with CommaDecimalPointLocale(): + self.test_numbers() + self.test_nan() + self.test_inf() + self.test_counted_string() + self.test_ascii() + self.test_malformed() + self.test_tofile_sep() + self.test_tofile_format() class TestFromBuffer(object): @@ -5808,24 +5805,24 @@ class TestNeighborhoodIter(object): np.array([[0, 0, 0], [0, 1, 0]], dtype=dt), np.array([[0, 0, 1], [0, 2, 3]], dtype=dt), np.array([[0, 1, 0], [2, 3, 0]], dtype=dt)] - l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], - NEIGH_MODE['zero']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 0, -1, 1], x[0], NEIGH_MODE['zero']) assert_array_equal(l, r) r = [np.array([[1, 1, 1], [1, 0, 1]], dtype=dt), np.array([[1, 1, 1], [0, 1, 1]], dtype=dt), np.array([[1, 0, 1], [1, 2, 3]], dtype=dt), np.array([[0, 1, 1], [2, 3, 1]], dtype=dt)] - l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], - NEIGH_MODE['one']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 0, -1, 1], x[0], NEIGH_MODE['one']) assert_array_equal(l, r) r = [np.array([[4, 4, 4], [4, 0, 1]], dtype=dt), np.array([[4, 4, 4], [0, 1, 4]], dtype=dt), np.array([[4, 0, 1], [4, 2, 3]], dtype=dt), np.array([[0, 1, 4], [2, 3, 4]], dtype=dt)] - l = test_neighborhood_iterator(x, [-1, 0, -1, 1], 4, - NEIGH_MODE['constant']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 0, -1, 1], 4, NEIGH_MODE['constant']) assert_array_equal(l, r) def test_simple2d(self): @@ -5840,8 +5837,8 @@ class TestNeighborhoodIter(object): np.array([[0, 1, 1], [0, 1, 1]], dtype=dt), np.array([[0, 0, 1], [2, 2, 3]], dtype=dt), np.array([[0, 1, 1], [2, 3, 3]], dtype=dt)] - l = test_neighborhood_iterator(x, [-1, 0, -1, 1], x[0], - NEIGH_MODE['mirror']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 0, -1, 1], x[0], NEIGH_MODE['mirror']) assert_array_equal(l, r) def test_mirror2d(self): @@ -5855,15 +5852,18 @@ class TestNeighborhoodIter(object): # Test padding with constant values x = np.linspace(1, 5, 5).astype(dt) r = [[0, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 0]] - l = test_neighborhood_iterator(x, [-1, 1], x[0], NEIGH_MODE['zero']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 1], x[0], NEIGH_MODE['zero']) assert_array_equal(l, r) r = [[1, 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 1]] - l = test_neighborhood_iterator(x, [-1, 1], x[0], NEIGH_MODE['one']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 1], x[0], NEIGH_MODE['one']) assert_array_equal(l, r) r = [[x[4], 1, 2], [1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, x[4]]] - l = test_neighborhood_iterator(x, [-1, 1], x[4], NEIGH_MODE['constant']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-1, 1], x[4], NEIGH_MODE['constant']) assert_array_equal(l, r) def test_simple_float(self): @@ -5877,7 +5877,8 @@ class TestNeighborhoodIter(object): x = np.linspace(1, 5, 5).astype(dt) r = np.array([[2, 1, 1, 2, 3], [1, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 5], [3, 4, 5, 5, 4]], dtype=dt) - l = test_neighborhood_iterator(x, [-2, 2], x[1], NEIGH_MODE['mirror']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-2, 2], x[1], NEIGH_MODE['mirror']) assert_([i.dtype == dt for i in l]) assert_array_equal(l, r) @@ -5892,7 +5893,8 @@ class TestNeighborhoodIter(object): x = np.linspace(1, 5, 5).astype(dt) r = np.array([[4, 5, 1, 2, 3], [5, 1, 2, 3, 4], [1, 2, 3, 4, 5], [2, 3, 4, 5, 1], [3, 4, 5, 1, 2]], dtype=dt) - l = test_neighborhood_iterator(x, [-2, 2], x[0], NEIGH_MODE['circular']) + l = _multiarray_tests.test_neighborhood_iterator( + x, [-2, 2], x[0], NEIGH_MODE['circular']) assert_array_equal(l, r) def test_circular(self): @@ -5915,8 +5917,8 @@ class TestStackedNeighborhoodIter(object): np.array([3], dtype=dt), np.array([0], dtype=dt), np.array([0], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-2, 4], NEIGH_MODE['zero'], - [0, 0], NEIGH_MODE['zero']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-2, 4], NEIGH_MODE['zero'], [0, 0], NEIGH_MODE['zero']) assert_array_equal(l, r) r = [np.array([1, 0, 1], dtype=dt), @@ -5924,8 +5926,8 @@ class TestStackedNeighborhoodIter(object): np.array([1, 2, 3], dtype=dt), np.array([2, 3, 0], dtype=dt), np.array([3, 0, 1], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [-1, 1], NEIGH_MODE['one']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [-1, 1], NEIGH_MODE['one']) assert_array_equal(l, r) # 2nd simple, 1d test: stacking 2 neigh iterators, mixing const padding and @@ -5939,8 +5941,8 @@ class TestStackedNeighborhoodIter(object): np.array([1, 2, 3], dtype=dt), np.array([2, 3, 3], dtype=dt), np.array([3, 3, 0], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['mirror'], - [-1, 1], NEIGH_MODE['zero']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['mirror'], [-1, 1], NEIGH_MODE['zero']) assert_array_equal(l, r) # Stacking mirror on top of zero @@ -5950,8 +5952,8 @@ class TestStackedNeighborhoodIter(object): np.array([0, 1, 2], dtype=dt), np.array([1, 2, 3], dtype=dt), np.array([2, 3, 0], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [-2, 0], NEIGH_MODE['mirror']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [-2, 0], NEIGH_MODE['mirror']) assert_array_equal(l, r) # Stacking mirror on top of zero: 2nd @@ -5961,8 +5963,8 @@ class TestStackedNeighborhoodIter(object): np.array([2, 3, 0], dtype=dt), np.array([3, 0, 0], dtype=dt), np.array([0, 0, 3], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [0, 2], NEIGH_MODE['mirror']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [0, 2], NEIGH_MODE['mirror']) assert_array_equal(l, r) # Stacking mirror on top of zero: 3rd @@ -5972,8 +5974,8 @@ class TestStackedNeighborhoodIter(object): np.array([0, 1, 2, 3, 0], dtype=dt), np.array([1, 2, 3, 0, 0], dtype=dt), np.array([2, 3, 0, 0, 3], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [-2, 2], NEIGH_MODE['mirror']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [-2, 2], NEIGH_MODE['mirror']) assert_array_equal(l, r) # 3rd simple, 1d test: stacking 2 neigh iterators, mixing const padding and @@ -5987,8 +5989,8 @@ class TestStackedNeighborhoodIter(object): np.array([1, 2, 3], dtype=dt), np.array([2, 3, 1], dtype=dt), np.array([3, 1, 0], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['circular'], - [-1, 1], NEIGH_MODE['zero']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['circular'], [-1, 1], NEIGH_MODE['zero']) assert_array_equal(l, r) # Stacking mirror on top of zero @@ -5998,8 +6000,8 @@ class TestStackedNeighborhoodIter(object): np.array([0, 1, 2], dtype=dt), np.array([1, 2, 3], dtype=dt), np.array([2, 3, 0], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [-2, 0], NEIGH_MODE['circular']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [-2, 0], NEIGH_MODE['circular']) assert_array_equal(l, r) # Stacking mirror on top of zero: 2nd @@ -6009,8 +6011,8 @@ class TestStackedNeighborhoodIter(object): np.array([2, 3, 0], dtype=dt), np.array([3, 0, 0], dtype=dt), np.array([0, 0, 1], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [0, 2], NEIGH_MODE['circular']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [0, 2], NEIGH_MODE['circular']) assert_array_equal(l, r) # Stacking mirror on top of zero: 3rd @@ -6020,8 +6022,8 @@ class TestStackedNeighborhoodIter(object): np.array([0, 1, 2, 3, 0], dtype=dt), np.array([1, 2, 3, 0, 0], dtype=dt), np.array([2, 3, 0, 0, 1], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [-1, 3], NEIGH_MODE['zero'], - [-2, 2], NEIGH_MODE['circular']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [-1, 3], NEIGH_MODE['zero'], [-2, 2], NEIGH_MODE['circular']) assert_array_equal(l, r) # 4th simple, 1d test: stacking 2 neigh iterators, but with lower iterator @@ -6032,24 +6034,24 @@ class TestStackedNeighborhoodIter(object): # array x = np.array([1, 2, 3], dtype=dt) r = [np.array([1, 2, 3, 0], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [1, 1], NEIGH_MODE['zero'], - [-1, 2], NEIGH_MODE['zero']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['zero']) assert_array_equal(l, r) # Stacking mirror on top of zero, first neighborhood strictly inside the # array x = np.array([1, 2, 3], dtype=dt) r = [np.array([1, 2, 3, 3], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [1, 1], NEIGH_MODE['zero'], - [-1, 2], NEIGH_MODE['mirror']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['mirror']) assert_array_equal(l, r) # Stacking mirror on top of zero, first neighborhood strictly inside the # array x = np.array([1, 2, 3], dtype=dt) r = [np.array([1, 2, 3, 1], dtype=dt)] - l = test_neighborhood_iterator_oob(x, [1, 1], NEIGH_MODE['zero'], - [-1, 2], NEIGH_MODE['circular']) + l = _multiarray_tests.test_neighborhood_iterator_oob( + x, [1, 1], NEIGH_MODE['zero'], [-1, 2], NEIGH_MODE['circular']) assert_array_equal(l, r) class TestWarnings(object): @@ -6433,7 +6435,9 @@ class TestNewBufferProtocol(object): def test_export_flags(self): # Check SIMPLE flag, see also gh-3613 (exception should be BufferError) - assert_raises(ValueError, get_buffer_info, np.arange(5)[::2], ('SIMPLE',)) + assert_raises(ValueError, + _multiarray_tests.get_buffer_info, + np.arange(5)[::2], ('SIMPLE',)) def test_padding(self): for j in range(8): @@ -6489,10 +6493,12 @@ class TestNewBufferProtocol(object): arr = np.ones((1, 10)) if arr.flags.f_contiguous: - shape, strides = get_buffer_info(arr, ['F_CONTIGUOUS']) + shape, strides = _multiarray_tests.get_buffer_info( + arr, ['F_CONTIGUOUS']) assert_(strides[0] == 8) arr = np.ones((10, 1), order='F') - shape, strides = get_buffer_info(arr, ['C_CONTIGUOUS']) + shape, strides = _multiarray_tests.get_buffer_info( + arr, ['C_CONTIGUOUS']) assert_(strides[-1] == 8) def test_out_of_order_fields(self): @@ -6634,26 +6640,26 @@ def test_scalar_element_deletion(): class TestMemEventHook(object): def test_mem_seteventhook(self): # The actual tests are within the C code in - # multiarray/multiarray_tests.c.src - test_pydatamem_seteventhook_start() + # multiarray/_multiarray_tests.c.src + _multiarray_tests.test_pydatamem_seteventhook_start() # force an allocation and free of a numpy array # needs to be larger then limit of small memory cacher in ctors.c a = np.zeros(1000) del a gc.collect() - test_pydatamem_seteventhook_end() + _multiarray_tests.test_pydatamem_seteventhook_end() class TestMapIter(object): def test_mapiter(self): # The actual tests are within the C code in - # multiarray/multiarray_tests.c.src + # multiarray/_multiarray_tests.c.src a = np.arange(12).reshape((3, 4)).astype(float) index = ([1, 1, 2, 0], [0, 0, 2, 3]) vals = [50, 50, 30, 16] - test_inplace_increment(a, index, vals) + _multiarray_tests.test_inplace_increment(a, index, vals) assert_equal(a, [[0.00, 1., 2.0, 19.], [104., 5., 6.0, 7.0], [8.00, 9., 40., 11.]]) @@ -6661,24 +6667,24 @@ class TestMapIter(object): b = np.arange(6).astype(float) index = (np.array([1, 2, 0]),) vals = [50, 4, 100.1] - test_inplace_increment(b, index, vals) + _multiarray_tests.test_inplace_increment(b, index, vals) assert_equal(b, [100.1, 51., 6., 3., 4., 5.]) class TestAsCArray(object): def test_1darray(self): array = np.arange(24, dtype=np.double) - from_c = test_as_c_array(array, 3) + from_c = _multiarray_tests.test_as_c_array(array, 3) assert_equal(array[3], from_c) def test_2darray(self): array = np.arange(24, dtype=np.double).reshape(3, 8) - from_c = test_as_c_array(array, 2, 4) + from_c = _multiarray_tests.test_as_c_array(array, 2, 4) assert_equal(array[2, 4], from_c) def test_3darray(self): array = np.arange(24, dtype=np.double).reshape(2, 3, 4) - from_c = test_as_c_array(array, 1, 2, 3) + from_c = _multiarray_tests.test_as_c_array(array, 1, 2, 3) assert_equal(array[1, 2, 3], from_c) @@ -7238,7 +7244,7 @@ class TestWritebackIfCopy(object): assert_equal(b, np.array([[15, 18, 21], [42, 54, 66], [69, 90, 111]])) def test_view_assign(self): - from numpy.core.multiarray_tests import npy_create_writebackifcopy, npy_resolve + from numpy.core._multiarray_tests import npy_create_writebackifcopy, npy_resolve arr = np.arange(9).reshape(3, 3).T arr_wb = npy_create_writebackifcopy(arr) assert_(arr_wb.flags.writebackifcopy) @@ -7308,7 +7314,7 @@ def test_equal_override(): def test_npymath_complex(): # Smoketest npymath functions - from numpy.core.multiarray_tests import ( + from numpy.core._multiarray_tests import ( npy_cabs, npy_carg) funcs = {npy_cabs: np.absolute, @@ -7327,7 +7333,7 @@ def test_npymath_complex(): def test_npymath_real(): # Smoketest npymath functions - from numpy.core.multiarray_tests import ( + from numpy.core._multiarray_tests import ( npy_log10, npy_cosh, npy_sinh, npy_tan, npy_tanh) funcs = {npy_log10: np.log10, diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index 1b2485a87..9d8f4f06a 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -4,8 +4,8 @@ import sys import warnings import numpy as np +import numpy.core._multiarray_tests as _multiarray_tests from numpy import array, arange, nditer, all -from numpy.core.multiarray_tests import test_nditer_too_large from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_array_equal, assert_raises, assert_warns, dec, HAS_REFCOUNT, suppress_warnings @@ -2697,18 +2697,19 @@ def test_iter_too_large_with_multiindex(): # arrays are now too large to be broadcast. The different modes test # different nditer functionality with or without GIL. for mode in range(6): - assert_raises(ValueError, test_nditer_too_large, arrays, -1, mode) + with assert_raises(ValueError): + _multiarray_tests.test_nditer_too_large(arrays, -1, mode) # but if we do nothing with the nditer, it can be constructed: - test_nditer_too_large(arrays, -1, 7) + _multiarray_tests.test_nditer_too_large(arrays, -1, 7) # When an axis is removed, things should work again (half the time): for i in range(num): for mode in range(6): # an axis with size 1024 is removed: - test_nditer_too_large(arrays, i*2, mode) + _multiarray_tests.test_nditer_too_large(arrays, i*2, mode) # an axis with size 1 is removed: - assert_raises(ValueError, test_nditer_too_large, - arrays, i*2 + 1, mode) + with assert_raises(ValueError): + _multiarray_tests.test_nditer_too_large(arrays, i*2 + 1, mode) if __name__ == "__main__": diff --git a/numpy/core/tests/test_print.py b/numpy/core/tests/test_print.py index 6ebb4733c..f432711a9 100644 --- a/numpy/core/tests/test_print.py +++ b/numpy/core/tests/test_print.py @@ -2,12 +2,14 @@ from __future__ import division, absolute_import, print_function import sys import locale +import contextlib import nose import numpy as np from numpy.testing import ( - run_module_suite, assert_, assert_equal, SkipTest + run_module_suite, assert_, assert_equal, SkipTest, dec ) +from ._locales import CommaDecimalPointLocale if sys.version_info[0] >= 3: @@ -201,46 +203,21 @@ def test_scalar_format(): (fmat, repr(val), repr(valtype), str(e))) +# # Locale tests: scalar types formatting should be independent of the locale -def in_foreign_locale(func): - """ - Swap LC_NUMERIC locale to one in which the decimal point is ',' and not '.' - If not possible, raise SkipTest +# - """ - if sys.platform == 'win32': - locales = ['FRENCH'] - else: - locales = ['fr_FR', 'fr_FR.UTF-8', 'fi_FI', 'fi_FI.UTF-8'] +class TestCommaDecimalPointLocale(CommaDecimalPointLocale): + + def test_locale_single(self): + assert_equal(str(np.float32(1.2)), str(float(1.2))) + + def test_locale_double(self): + assert_equal(str(np.double(1.2)), str(float(1.2))) + + def test_locale_longdouble(self): + assert_equal(str(np.longdouble('1.2')), str(float(1.2))) - def wrapper(*args, **kwargs): - curloc = locale.getlocale(locale.LC_NUMERIC) - try: - for loc in locales: - try: - locale.setlocale(locale.LC_NUMERIC, loc) - break - except locale.Error: - pass - else: - raise SkipTest("Skipping locale test, because " - "French locale not found") - return func(*args, **kwargs) - finally: - locale.setlocale(locale.LC_NUMERIC, locale=curloc) - return nose.tools.make_decorator(func)(wrapper) - -@in_foreign_locale -def test_locale_single(): - assert_equal(str(np.float32(1.2)), str(float(1.2))) - -@in_foreign_locale -def test_locale_double(): - assert_equal(str(np.double(1.2)), str(float(1.2))) - -@in_foreign_locale -def test_locale_longdouble(): - assert_equal(str(np.longdouble('1.2')), str(float(1.2))) if __name__ == "__main__": run_module_suite() diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index 32ce5dc42..8e261d226 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -2,7 +2,7 @@ from __future__ import division, absolute_import, print_function import sys try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: diff --git a/numpy/core/tests/test_scalarbuffer.py b/numpy/core/tests/test_scalarbuffer.py new file mode 100644 index 000000000..cd887f2fb --- /dev/null +++ b/numpy/core/tests/test_scalarbuffer.py @@ -0,0 +1,84 @@ +""" +Test scalar buffer interface adheres to PEP 3118 +""" +import sys +import numpy as np +from numpy.testing import run_module_suite, assert_, assert_equal, dec + +# PEP3118 format strings for native (standard alignment and byteorder) types +scalars_and_codes = [ + (np.bool_, '?'), + (np.byte, 'b'), + (np.short, 'h'), + (np.intc, 'i'), + (np.int_, 'l'), + (np.longlong, 'q'), + (np.ubyte, 'B'), + (np.ushort, 'H'), + (np.uintc, 'I'), + (np.uint, 'L'), + (np.ulonglong, 'Q'), + (np.half, 'e'), + (np.single, 'f'), + (np.double, 'd'), + (np.longdouble, 'g'), + (np.csingle, 'Zf'), + (np.cdouble, 'Zd'), + (np.clongdouble, 'Zg'), +] + + +class TestScalarPEP3118(object): + skip_if_no_buffer_interface = dec.skipif(sys.version_info.major < 3, + "scalars do not implement buffer interface in Python 2") + + @skip_if_no_buffer_interface + def test_scalar_match_array(self): + for scalar, _ in scalars_and_codes: + x = scalar() + a = np.array([], dtype=np.dtype(scalar)) + mv_x = memoryview(x) + mv_a = memoryview(a) + assert_equal(mv_x.format, mv_a.format) + + @skip_if_no_buffer_interface + def test_scalar_dim(self): + for scalar, _ in scalars_and_codes: + x = scalar() + mv_x = memoryview(x) + assert_equal(mv_x.itemsize, np.dtype(scalar).itemsize) + assert_equal(mv_x.ndim, 0) + assert_equal(mv_x.shape, ()) + assert_equal(mv_x.strides, ()) + assert_equal(mv_x.suboffsets, ()) + + @skip_if_no_buffer_interface + def test_scalar_known_code(self): + for scalar, code in scalars_and_codes: + x = scalar() + mv_x = memoryview(x) + assert_equal(mv_x.format, code) + + @skip_if_no_buffer_interface + def test_void_scalar_structured_data(self): + dt = np.dtype([('name', np.unicode_, 16), ('grades', np.float64, (2,))]) + x = np.array(('ndarray_scalar', (1.2, 3.0)), dtype=dt)[()] + assert_(isinstance(x, np.void)) + mv_x = memoryview(x) + expected_size = 16 * np.dtype((np.unicode_, 1)).itemsize + expected_size += 2 * np.dtype((np.float64, 1)).itemsize + assert_equal(mv_x.itemsize, expected_size) + assert_equal(mv_x.ndim, 0) + assert_equal(mv_x.shape, ()) + assert_equal(mv_x.strides, ()) + assert_equal(mv_x.suboffsets, ()) + + # check scalar format string against ndarray format string + a = np.array([('Sarah', (8.0, 7.0)), ('John', (6.0, 7.0))], dtype=dt) + assert_(isinstance(a, np.ndarray)) + mv_a = memoryview(a) + assert_equal(mv_x.itemsize, mv_a.itemsize) + assert_equal(mv_x.format, mv_a.format) + +if __name__ == "__main__": + run_module_suite() diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py index 7e1bfbdbe..576ecaf0d 100644 --- a/numpy/core/tests/test_ufunc.py +++ b/numpy/core/tests/test_ufunc.py @@ -4,9 +4,9 @@ import warnings import itertools import numpy as np -import numpy.core.umath_tests as umt -import numpy.core.operand_flag_tests as opflag_tests -from numpy.core.test_rational import rational, test_add, test_add_rationals +import numpy.core._umath_tests as umt +import numpy.core._operand_flag_tests as opflag_tests +import numpy.core._rational_tests as _rational_tests from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_raises, assert_array_equal, assert_almost_equal, assert_array_almost_equal, @@ -42,8 +42,9 @@ class TestUfunc(object): assert_(pickle.loads(pickle.dumps(np.sin)) is np.sin) # Check that ufunc not defined in the top level numpy namespace such as - # numpy.core.test_rational.test_add can also be pickled - assert_(pickle.loads(pickle.dumps(test_add)) is test_add) + # numpy.core._rational_tests.test_add can also be pickled + res = pickle.loads(pickle.dumps(_rational_tests.test_add)) + assert_(res is _rational_tests.test_add) def test_pickle_withstring(self): import pickle @@ -625,7 +626,7 @@ class TestUfunc(object): assert_array_equal(c, (a * b).sum(0)) c = in1d(a, b, axes=[0, 2]) assert_array_equal(c, (a.transpose(1, 2, 0) * b).sum(-1)) - # Check errors for inproperly constructed axes arguments. + # Check errors for improperly constructed axes arguments. # should have list. assert_raises(TypeError, in1d, a, b, axes=-1) # needs enough elements @@ -670,7 +671,7 @@ class TestUfunc(object): d = mm(a, b, out=c, axes=[(-2, -1), (-2, -1), (3, 0)]) assert_(c is d) assert_array_equal(c, np.matmul(a, b).transpose(3, 0, 1, 2)) - # Check errors for inproperly constructed axes arguments. + # Check errors for improperly constructed axes arguments. # wrong argument assert_raises(TypeError, mm, a, b, axis=1) # axes should be list @@ -1143,15 +1144,17 @@ class TestUfunc(object): a = np.array([0, 1, 2], dtype='i8') b = np.array([0, 1, 2], dtype='i8') - c = np.empty(3, dtype=rational) + c = np.empty(3, dtype=_rational_tests.rational) # Output must be specified so numpy knows what # ufunc signature to look for - result = test_add(a, b, c) - assert_equal(result, np.array([0, 2, 4], dtype=rational)) + result = _rational_tests.test_add(a, b, c) + target = np.array([0, 2, 4], dtype=_rational_tests.rational) + assert_equal(result, target) # no output type should raise TypeError - assert_raises(TypeError, test_add, a, b) + with assert_raises(TypeError): + _rational_tests.test_add(a, b) def test_operand_flags(self): a = np.arange(16, dtype='l').reshape(4, 4) @@ -1167,7 +1170,7 @@ class TestUfunc(object): assert_equal(a, 10) def test_struct_ufunc(self): - import numpy.core.struct_ufunc_test as struct_ufunc + import numpy.core._struct_ufunc_tests as struct_ufunc a = np.array([(1, 2, 3)], dtype='u8,u8,u8') b = np.array([(1, 2, 3)], dtype='u8,u8,u8') @@ -1176,20 +1179,30 @@ class TestUfunc(object): assert_equal(result, np.array([(2, 4, 6)], dtype='u8,u8,u8')) def test_custom_ufunc(self): - a = np.array([rational(1, 2), rational(1, 3), rational(1, 4)], - dtype=rational) - b = np.array([rational(1, 2), rational(1, 3), rational(1, 4)], - dtype=rational) - - result = test_add_rationals(a, b) - expected = np.array([rational(1), rational(2, 3), rational(1, 2)], - dtype=rational) + a = np.array( + [_rational_tests.rational(1, 2), + _rational_tests.rational(1, 3), + _rational_tests.rational(1, 4)], + dtype=_rational_tests.rational) + b = np.array( + [_rational_tests.rational(1, 2), + _rational_tests.rational(1, 3), + _rational_tests.rational(1, 4)], + dtype=_rational_tests.rational) + + result = _rational_tests.test_add_rationals(a, b) + expected = np.array( + [_rational_tests.rational(1), + _rational_tests.rational(2, 3), + _rational_tests.rational(1, 2)], + dtype=_rational_tests.rational) assert_equal(result, expected) def test_custom_ufunc_forced_sig(self): # gh-9351 - looking for a non-first userloop would previously hang - assert_raises(TypeError, - np.multiply, rational(1), 1, signature=(rational, int, None)) + with assert_raises(TypeError): + np.multiply(_rational_tests.rational(1), 1, + signature=(_rational_tests.rational, int, None)) def test_custom_array_like(self): diff --git a/numpy/core/tests/test_umath.py b/numpy/core/tests/test_umath.py index 4b6b26cbf..fe7768e53 100644 --- a/numpy/core/tests/test_umath.py +++ b/numpy/core/tests/test_umath.py @@ -7,7 +7,7 @@ import fnmatch import itertools import numpy.core.umath as ncu -from numpy.core import umath_tests as ncu_tests +from numpy.core import _umath_tests as ncu_tests import numpy as np from numpy.testing import ( run_module_suite, assert_, assert_equal, assert_raises, diff --git a/numpy/distutils/__init__.py b/numpy/distutils/__init__.py index 0450334ff..d5921b399 100644 --- a/numpy/distutils/__init__.py +++ b/numpy/distutils/__init__.py @@ -17,7 +17,7 @@ try: # Normally numpy is installed if the above import works, but an interrupted # in-place build could also have left a __config__.py. In that case the # next import may still fail, so keep it inside the try block. - from numpy.testing.nosetester import _numpy_tester + from numpy.testing import _numpy_tester test = _numpy_tester().test except ImportError: pass @@ -26,7 +26,7 @@ except ImportError: def customized_fcompiler(plat=None, compiler=None): from numpy.distutils.fcompiler import new_fcompiler c = new_fcompiler(plat=plat, compiler=compiler) - c.customize() + c.customize() return c def customized_ccompiler(plat=None, compiler=None): diff --git a/numpy/doc/subclassing.py b/numpy/doc/subclassing.py index 467e31cea..3be3d94b3 100644 --- a/numpy/doc/subclassing.py +++ b/numpy/doc/subclassing.py @@ -562,7 +562,7 @@ pass on to ``A.__array_ufunc__``, the ``super`` call in ``A`` would go to Prior to numpy 1.13, the behaviour of ufuncs could only be tuned using ``__array_wrap__`` and ``__array_prepare__``. These two allowed one to -change the output type of a ufunc, but, in constrast to +change the output type of a ufunc, but, in contrast to ``__array_ufunc__``, did not allow one to make any changes to the inputs. It is hoped to eventually deprecate these, but ``__array_wrap__`` is also used by other numpy functions and methods, such as ``squeeze``, so at the diff --git a/numpy/f2py/crackfortran.py b/numpy/f2py/crackfortran.py index dc560f98e..19ce8c145 100755 --- a/numpy/f2py/crackfortran.py +++ b/numpy/f2py/crackfortran.py @@ -612,7 +612,7 @@ multilinepattern = re.compile( def split_by_unquoted(line, characters): """ Splits the line into (line[:i], line[i:]), - where i is the index of first occurence of one of the characters + where i is the index of first occurrence of one of the characters not within quotes, or len(line) if no such index exists """ assert not (set('"\'') & set(characters)), "cannot split by unquoted quotes" diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 0599324d7..475119481 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -1,7 +1,7 @@ from __future__ import division, absolute_import, print_function import numpy as np -from numpy.core.test_rational import rational +from numpy.core._rational_tests import rational from numpy.testing import ( run_module_suite, assert_equal, assert_array_equal, assert_raises, assert_ diff --git a/numpy/ma/extras.py b/numpy/ma/extras.py index e247fe170..8272dced9 100644 --- a/numpy/ma/extras.py +++ b/numpy/ma/extras.py @@ -1620,7 +1620,10 @@ def flatnotmasked_contiguous(a): Returns ------- slice_list : list - A sorted sequence of slices (start index, end index). + A sorted sequence of `slice` objects (start index, end index). + + ..versionchanged:: 1.15.0 + Now returns an empty list instead of None for a fully masked array See Also -------- @@ -1635,7 +1638,7 @@ def flatnotmasked_contiguous(a): -------- >>> a = np.ma.arange(10) >>> np.ma.flatnotmasked_contiguous(a) - slice(0, 10, None) + [slice(0, 10, None)] >>> mask = (a < 3) | (a > 8) | (a == 5) >>> a[mask] = np.ma.masked @@ -1645,13 +1648,13 @@ def flatnotmasked_contiguous(a): >>> np.ma.flatnotmasked_contiguous(a) [slice(3, 5, None), slice(6, 9, None)] >>> a[:] = np.ma.masked - >>> print(np.ma.flatnotmasked_edges(a)) - None + >>> np.ma.flatnotmasked_contiguous(a) + [] """ m = getmask(a) if m is nomask: - return slice(0, a.size, None) + return [slice(0, a.size)] i = 0 result = [] for (k, g) in itertools.groupby(m.ravel()): @@ -1659,7 +1662,7 @@ def flatnotmasked_contiguous(a): if not k: result.append(slice(i, i + n)) i += n - return result or None + return result def notmasked_contiguous(a, axis=None): """ @@ -1671,7 +1674,8 @@ def notmasked_contiguous(a, axis=None): The input array. axis : int, optional Axis along which to perform the operation. - If None (default), applies to a flattened version of the array. + If None (default), applies to a flattened version of the array, and this + is the same as `flatnotmasked_contiguous`. Returns ------- @@ -1679,6 +1683,8 @@ def notmasked_contiguous(a, axis=None): A list of slices (start and end indexes) of unmasked indexes in the array. + If the input is 2d and axis is specified, the result is a list of lists. + See Also -------- flatnotmasked_edges, flatnotmasked_contiguous, notmasked_edges, @@ -1690,17 +1696,35 @@ def notmasked_contiguous(a, axis=None): Examples -------- - >>> a = np.arange(9).reshape((3, 3)) + >>> a = np.arange(12).reshape((3, 4)) >>> mask = np.zeros_like(a) - >>> mask[1:, 1:] = 1 - + >>> mask[1:, :-1] = 1; mask[0, 1] = 1; mask[-1, 0] = 0 >>> ma = np.ma.array(a, mask=mask) + >>> ma + masked_array( + data=[[0, --, 2, 3], + [--, --, --, 7], + [8, --, --, 11]], + mask=[[False, True, False, False], + [ True, True, True, False], + [False, True, True, False]], + fill_value=999999) >>> np.array(ma[~ma.mask]) - array([0, 1, 2, 3, 6]) + array([ 0, 2, 3, 7, 8, 11]) >>> np.ma.notmasked_contiguous(ma) - [slice(0, 4, None), slice(6, 7, None)] + [slice(0, 1, None), slice(2, 4, None), slice(7, 9, None), slice(11, 12, None)] + + >>> np.ma.notmasked_contiguous(ma, axis=0) + [[slice(0, 1, None), slice(2, 3, None)], # column broken into two segments + [], # fully masked column + [slice(0, 1, None)], + [slice(0, 3, None)]] + >>> np.ma.notmasked_contiguous(ma, axis=1) + [[slice(0, 1, None), slice(2, 4, None)], # row broken into two segments + [slice(3, 4, None)], + [slice(0, 1, None), slice(3, 4, None)]] """ a = asarray(a) nd = a.ndim @@ -1717,7 +1741,7 @@ def notmasked_contiguous(a, axis=None): # for i in range(a.shape[other]): idx[other] = i - result.append(flatnotmasked_contiguous(a[tuple(idx)]) or None) + result.append(flatnotmasked_contiguous(a[tuple(idx)])) return result diff --git a/numpy/ma/tests/test_extras.py b/numpy/ma/tests/test_extras.py index d1c1aa63e..95319eb65 100644 --- a/numpy/ma/tests/test_extras.py +++ b/numpy/ma/tests/test_extras.py @@ -128,7 +128,10 @@ class TestGeneric(object): a = arange(10) # No mask test = flatnotmasked_contiguous(a) - assert_equal(test, slice(0, a.size)) + assert_equal(test, [slice(0, a.size)]) + # mask of all false + a.mask = np.zeros(10, dtype=bool) + assert_equal(test, [slice(0, a.size)]) # Some mask a[(a < 3) | (a > 8) | (a == 5)] = masked test = flatnotmasked_contiguous(a) @@ -136,7 +139,7 @@ class TestGeneric(object): # a[:] = masked test = flatnotmasked_contiguous(a) - assert_equal(test, None) + assert_equal(test, []) class TestAverage(object): @@ -368,23 +371,32 @@ class TestNotMasked(object): a = masked_array(np.arange(24).reshape(3, 8), mask=[[0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], - [0, 0, 0, 0, 0, 0, 1, 0], ]) + [0, 0, 0, 0, 0, 0, 1, 0]]) tmp = notmasked_contiguous(a, None) - assert_equal(tmp[-1], slice(23, 24, None)) - assert_equal(tmp[-2], slice(16, 22, None)) - assert_equal(tmp[-3], slice(0, 4, None)) - # + assert_equal(tmp, [ + slice(0, 4, None), + slice(16, 22, None), + slice(23, 24, None) + ]) + tmp = notmasked_contiguous(a, 0) - assert_(len(tmp[-1]) == 1) - assert_(tmp[-2] is None) - assert_equal(tmp[-3], tmp[-1]) - assert_(len(tmp[0]) == 2) + assert_equal(tmp, [ + [slice(0, 1, None), slice(2, 3, None)], + [slice(0, 1, None), slice(2, 3, None)], + [slice(0, 1, None), slice(2, 3, None)], + [slice(0, 1, None), slice(2, 3, None)], + [slice(2, 3, None)], + [slice(2, 3, None)], + [], + [slice(2, 3, None)] + ]) # tmp = notmasked_contiguous(a, 1) - assert_equal(tmp[0][-1], slice(0, 4, None)) - assert_(tmp[1] is None) - assert_equal(tmp[2][-1], slice(7, 8, None)) - assert_equal(tmp[2][-2], slice(0, 6, None)) + assert_equal(tmp, [ + [slice(0, 4, None)], + [], + [slice(0, 6, None), slice(7, 8, None)] + ]) class TestCompressFunctions(object): diff --git a/numpy/matrixlib/tests/test_defmatrix.py b/numpy/matrixlib/tests/test_defmatrix.py index 9575f9c8a..d5435a1a3 100644 --- a/numpy/matrixlib/tests/test_defmatrix.py +++ b/numpy/matrixlib/tests/test_defmatrix.py @@ -1,7 +1,7 @@ from __future__ import division, absolute_import, print_function try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: diff --git a/numpy/testing/__init__.py b/numpy/testing/__init__.py index 9485b455e..f4970b06b 100644 --- a/numpy/testing/__init__.py +++ b/numpy/testing/__init__.py @@ -9,7 +9,12 @@ from __future__ import division, absolute_import, print_function from unittest import TestCase -from . import decorators as dec -from .nosetester import run_module_suite, NoseTester as Tester, _numpy_tester -from .utils import * +from ._private.utils import * +from ._private import decorators as dec +from ._private.nosetester import ( + run_module_suite, NoseTester as Tester, _numpy_tester, + ) + +__all__ = _private.utils.__all__ + ['TestCase', 'run_module_suite'] + test = _numpy_tester().test diff --git a/numpy/testing/nose_tools/__init__.py b/numpy/testing/_private/__init__.py index e69de29bb..e69de29bb 100644 --- a/numpy/testing/nose_tools/__init__.py +++ b/numpy/testing/_private/__init__.py diff --git a/numpy/testing/nose_tools/decorators.py b/numpy/testing/_private/decorators.py index dee832404..60d3f968f 100644 --- a/numpy/testing/nose_tools/decorators.py +++ b/numpy/testing/_private/decorators.py @@ -16,7 +16,7 @@ function name, setup and teardown functions and so on - see from __future__ import division, absolute_import, print_function try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: diff --git a/numpy/testing/nose_tools/noseclasses.py b/numpy/testing/_private/noseclasses.py index 9756b9b45..08dec0ca9 100644 --- a/numpy/testing/nose_tools/noseclasses.py +++ b/numpy/testing/_private/noseclasses.py @@ -325,7 +325,7 @@ class FPUModeCheckPlugin(Plugin): """ def prepareTestCase(self, test): - from numpy.core.multiarray_tests import get_fpu_mode + from numpy.core._multiarray_tests import get_fpu_mode def run(result): old_mode = get_fpu_mode() diff --git a/numpy/testing/nose_tools/nosetester.py b/numpy/testing/_private/nosetester.py index c2cf58377..c2cf58377 100644 --- a/numpy/testing/nose_tools/nosetester.py +++ b/numpy/testing/_private/nosetester.py diff --git a/numpy/testing/nose_tools/parameterized.py b/numpy/testing/_private/parameterized.py index 53e67517d..53e67517d 100644 --- a/numpy/testing/nose_tools/parameterized.py +++ b/numpy/testing/_private/parameterized.py diff --git a/numpy/testing/nose_tools/utils.py b/numpy/testing/_private/utils.py index ff3cf841c..507ecb1e2 100644 --- a/numpy/testing/nose_tools/utils.py +++ b/numpy/testing/_private/utils.py @@ -1156,10 +1156,54 @@ def rundocs(filename=None, raise_on_error=True): raise AssertionError("Some doctests failed:\n%s" % "\n".join(msg)) -def raises(*args,**kwargs): +def raises(*args): + """Decorator to check for raised exceptions. + + The decorated test function must raise one of the passed exceptions to + pass. If you want to test many assertions about exceptions in a single + test, you may want to use `assert_raises` instead. + + .. warning:: + This decorator is nose specific, do not use it if you are using a + different test framework. + + Parameters + ---------- + args : exceptions + The test passes if any of the passed exceptions is raised. + + Raises + ------ + AssertionError + + Examples + -------- + + Usage:: + + @raises(TypeError, ValueError) + def test_raises_type_error(): + raise TypeError("This test passes") + + @raises(Exception) + def test_that_fails_by_passing(): + pass + + """ nose = import_nose() - return nose.tools.raises(*args,**kwargs) + return nose.tools.raises(*args) +# +# assert_raises and assert_raises_regex are taken from unittest. +# +import unittest + + +class _Dummy(unittest.TestCase): + def nop(self): + pass + +_d = _Dummy('nop') def assert_raises(*args, **kwargs): """ @@ -1187,8 +1231,7 @@ def assert_raises(*args, **kwargs): """ __tracebackhide__ = True # Hide traceback for py.test - nose = import_nose() - return nose.tools.assert_raises(*args,**kwargs) + return _d.assertRaises(*args,**kwargs) def assert_raises_regex(exception_class, expected_regexp, *args, **kwargs): @@ -1212,13 +1255,12 @@ def assert_raises_regex(exception_class, expected_regexp, *args, **kwargs): """ __tracebackhide__ = True # Hide traceback for py.test - nose = import_nose() if sys.version_info.major >= 3: - funcname = nose.tools.assert_raises_regex + funcname = _d.assertRaisesRegex else: # Only present in Python 2.7, missing from unittest in 2.6 - funcname = nose.tools.assert_raises_regexp + funcname = _d.assertRaisesRegexp return funcname(exception_class, expected_regexp, *args, **kwargs) diff --git a/numpy/testing/decorators.py b/numpy/testing/decorators.py index 21bcdd798..1988afd29 100644 --- a/numpy/testing/decorators.py +++ b/numpy/testing/decorators.py @@ -3,6 +3,9 @@ Back compatibility decorators module. It will import the appropriate set of tools """ -import os +import warnings -from .nose_tools.decorators import * +warnings.warn("Import from numpy.testing, not numpy.testing.decorators", + ImportWarning) + +from ._private.decorators import * diff --git a/numpy/testing/noseclasses.py b/numpy/testing/noseclasses.py index 144c4e7e4..dde62e825 100644 --- a/numpy/testing/noseclasses.py +++ b/numpy/testing/noseclasses.py @@ -2,4 +2,10 @@ Back compatibility noseclasses module. It will import the appropriate set of tools """ -from .nose_tools.noseclasses import *
\ No newline at end of file +import warnings + +warnings.warn("Import from numpy.testing, not numpy.testing.noseclasses", + ImportWarning) + +from ._private.noseclasses import * + diff --git a/numpy/testing/nosetester.py b/numpy/testing/nosetester.py index 949fae03e..772bff305 100644 --- a/numpy/testing/nosetester.py +++ b/numpy/testing/nosetester.py @@ -3,10 +3,12 @@ Back compatibility nosetester module. It will import the appropriate set of tools """ -import os +import warnings -from .nose_tools.nosetester import * +warnings.warn("Import from numpy.testing, not numpy.testing.nosetester", + ImportWarning) +from ._private.nosetester import * __all__ = ['get_package_name', 'run_module_suite', 'NoseTester', '_numpy_tester', 'get_package_name', 'import_nose', diff --git a/numpy/testing/pytest_tools/decorators.py b/numpy/testing/pytest_tools/decorators.py index f8addb9c8..e72b1eb0b 100644 --- a/numpy/testing/pytest_tools/decorators.py +++ b/numpy/testing/pytest_tools/decorators.py @@ -13,7 +13,7 @@ function name, setup and teardown functions and so on. from __future__ import division, absolute_import, print_function try: - # Accessing collections abstact classes from collections + # Accessing collections abstract classes from collections # has been deprecated since Python 3.3 import collections.abc as collections_abc except ImportError: diff --git a/numpy/testing/setup.py b/numpy/testing/setup.py index 5a0f977d9..b00e5e029 100755 --- a/numpy/testing/setup.py +++ b/numpy/testing/setup.py @@ -6,7 +6,7 @@ def configuration(parent_package='',top_path=None): from numpy.distutils.misc_util import Configuration config = Configuration('testing', parent_package, top_path) - config.add_subpackage('nose_tools') + config.add_subpackage('_private') config.add_subpackage('pytest_tools') config.add_data_dir('tests') return config diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py index a0218c4e6..3cd89e639 100644 --- a/numpy/testing/utils.py +++ b/numpy/testing/utils.py @@ -3,9 +3,12 @@ Back compatibility utils module. It will import the appropriate set of tools """ -import os +import warnings -from .nose_tools.utils import * +warnings.warn("Import from numpy.testing, not numpy.testing.utils", + ImportWarning) + +from ._private.utils import * __all__ = [ 'assert_equal', 'assert_almost_equal', 'assert_approx_equal', |