summaryrefslogtreecommitdiff
path: root/numpy/core
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/core')
-rw-r--r--numpy/core/__init__.py7
-rw-r--r--numpy/core/_internal.py2
-rw-r--r--numpy/core/arrayprint.py15
-rw-r--r--numpy/core/code_generators/generate_umath.py20
-rw-r--r--numpy/core/defchararray.py4
-rw-r--r--numpy/core/numeric.py6
-rw-r--r--numpy/core/numerictypes.py8
-rw-r--r--numpy/core/setup_common.py6
-rw-r--r--numpy/core/src/multiarray/common.c2
-rw-r--r--numpy/core/src/multiarray/common.h3
-rw-r--r--numpy/core/src/multiarray/ctors.c40
-rw-r--r--numpy/core/src/multiarray/einsum.c.src8
-rw-r--r--numpy/core/src/multiarray/multiarraymodule.c1
-rw-r--r--numpy/core/src/umath/simd.inc.src2
-rw-r--r--numpy/core/tests/test_deprecations.py13
-rw-r--r--numpy/core/tests/test_multiarray.py52
-rw-r--r--numpy/core/tests/test_numeric.py2
-rw-r--r--numpy/core/tests/test_regression.py11
-rw-r--r--numpy/core/tests/test_ufunc.py8
19 files changed, 128 insertions, 82 deletions
diff --git a/numpy/core/__init__.py b/numpy/core/__init__.py
index 815c61924..c2d53fe3e 100644
--- a/numpy/core/__init__.py
+++ b/numpy/core/__init__.py
@@ -135,16 +135,11 @@ def _ufunc_reduce(func):
return _ufunc_reconstruct, (whichmodule(func, name), name)
-import sys
-if sys.version_info[0] >= 3:
- import copyreg
-else:
- import copy_reg as copyreg
+import copyreg
copyreg.pickle(ufunc, _ufunc_reduce, _ufunc_reconstruct)
# Unclutter namespace (must keep _ufunc_reconstruct for unpickling)
del copyreg
-del sys
del _ufunc_reduce
from numpy._pytesttester import PytestTester
diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py
index 2a750cc24..f21774cb6 100644
--- a/numpy/core/_internal.py
+++ b/numpy/core/_internal.py
@@ -838,7 +838,7 @@ def npy_ctypes_check(cls):
# # (..., _ctypes._CData, object)
ctype_base = cls.__mro__[-2]
# right now, they're part of the _ctypes module
- return 'ctypes' in ctype_base.__module__
+ return '_ctypes' in ctype_base.__module__
except Exception:
return False
diff --git a/numpy/core/arrayprint.py b/numpy/core/arrayprint.py
index 918da4a72..ec7e4261f 100644
--- a/numpy/core/arrayprint.py
+++ b/numpy/core/arrayprint.py
@@ -22,19 +22,12 @@ __docformat__ = 'restructuredtext'
# scalars are printed inside an ndarray. Only the latter strs are currently
# user-customizable.
-import sys
import functools
import numbers
-if sys.version_info[0] >= 3:
- try:
- from _thread import get_ident
- except ImportError:
- from _dummy_thread import get_ident
-else:
- try:
- from thread import get_ident
- except ImportError:
- from dummy_thread import get_ident
+try:
+ from _thread import get_ident
+except ImportError:
+ from _dummy_thread import get_ident
import numpy as np
from . import numerictypes as _nt
diff --git a/numpy/core/code_generators/generate_umath.py b/numpy/core/code_generators/generate_umath.py
index f5691d950..7599360f5 100644
--- a/numpy/core/code_generators/generate_umath.py
+++ b/numpy/core/code_generators/generate_umath.py
@@ -148,12 +148,8 @@ class Ufunc:
# String-handling utilities to avoid locale-dependence.
import string
-if sys.version_info[0] < 3:
- UPPER_TABLE = string.maketrans(string.ascii_lowercase,
- string.ascii_uppercase)
-else:
- UPPER_TABLE = bytes.maketrans(bytes(string.ascii_lowercase, "ascii"),
- bytes(string.ascii_uppercase, "ascii"))
+UPPER_TABLE = bytes.maketrans(bytes(string.ascii_lowercase, "ascii"),
+ bytes(string.ascii_uppercase, "ascii"))
def english_upper(s):
""" Apply English case rules to convert ASCII strings to all upper case.
@@ -1076,15 +1072,9 @@ def make_ufuncs(funcdict):
uf = funcdict[name]
mlist = []
docstring = textwrap.dedent(uf.docstring).strip()
- if sys.version_info[0] < 3:
- docstring = docstring.encode('string-escape')
- docstring = docstring.replace(r'"', r'\"')
- else:
- docstring = docstring.encode('unicode-escape').decode('ascii')
- docstring = docstring.replace(r'"', r'\"')
- # XXX: I don't understand why the following replace is not
- # necessary in the python 2 case.
- docstring = docstring.replace(r"'", r"\'")
+ docstring = docstring.encode('unicode-escape').decode('ascii')
+ docstring = docstring.replace(r'"', r'\"')
+ docstring = docstring.replace(r"'", r"\'")
# Split the docstring because some compilers (like MS) do not like big
# string literal in C code. We split at endlines because textwrap.wrap
# do not play well with \n
diff --git a/numpy/core/defchararray.py b/numpy/core/defchararray.py
index 4d7781317..1cfdc55c0 100644
--- a/numpy/core/defchararray.py
+++ b/numpy/core/defchararray.py
@@ -1953,8 +1953,8 @@ class chararray(ndarray):
# strings in the new array.
itemsize = long(itemsize)
- if sys.version_info[0] >= 3 and isinstance(buffer, str):
- # On Py3, unicode objects do not have the buffer interface
+ if isinstance(buffer, str):
+ # unicode objects do not have the buffer interface
filler = buffer
buffer = None
else:
diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py
index 5a1cbe9fc..72c6089b8 100644
--- a/numpy/core/numeric.py
+++ b/numpy/core/numeric.py
@@ -35,12 +35,6 @@ bitwise_not = invert
ufunc = type(sin)
newaxis = None
-if sys.version_info[0] >= 3:
- import builtins
-else:
- import __builtin__ as builtins
-
-
array_function_dispatch = functools.partial(
overrides.array_function_dispatch, module='numpy')
diff --git a/numpy/core/numerictypes.py b/numpy/core/numerictypes.py
index c63ea08c7..c06552c4e 100644
--- a/numpy/core/numerictypes.py
+++ b/numpy/core/numerictypes.py
@@ -80,7 +80,6 @@ Exported symbols include:
"""
import types as _types
-import sys
import numbers
import warnings
@@ -120,11 +119,8 @@ from ._dtype import _kind_name
# we don't export these for import *, but we do want them accessible
# as numerictypes.bool, etc.
-if sys.version_info[0] >= 3:
- from builtins import bool, int, float, complex, object, str
- unicode = str
-else:
- from __builtin__ import bool, int, float, complex, object, unicode, str
+from builtins import bool, int, float, complex, object, str
+unicode = str
# We use this later
diff --git a/numpy/core/setup_common.py b/numpy/core/setup_common.py
index a947f7a3d..6d8e603a7 100644
--- a/numpy/core/setup_common.py
+++ b/numpy/core/setup_common.py
@@ -244,9 +244,9 @@ def check_long_double_representation(cmd):
# Disable multi-file interprocedural optimization in the Intel compiler on Linux
# which generates intermediary object files and prevents checking the
# float representation.
- elif (sys.platform != "win32"
- and cmd.compiler.compiler_type.startswith('intel')
- and '-ipo' in cmd.compiler.cc_exe):
+ elif (sys.platform != "win32"
+ and cmd.compiler.compiler_type.startswith('intel')
+ and '-ipo' in cmd.compiler.cc_exe):
newcompiler = cmd.compiler.cc_exe.replace(' -ipo', '')
cmd.compiler.set_executables(
compiler=newcompiler,
diff --git a/numpy/core/src/multiarray/common.c b/numpy/core/src/multiarray/common.c
index 39916a14d..c9ec32268 100644
--- a/numpy/core/src/multiarray/common.c
+++ b/numpy/core/src/multiarray/common.c
@@ -890,5 +890,3 @@ new_array_for_sum(PyArrayObject *ap1, PyArrayObject *ap2, PyArrayObject* out,
}
}
-
-
diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h
index e77e51f42..4913eb202 100644
--- a/numpy/core/src/multiarray/common.h
+++ b/numpy/core/src/multiarray/common.h
@@ -235,7 +235,7 @@ npy_uint_alignment(int itemsize)
default:
break;
}
-
+
return alignment;
}
@@ -343,3 +343,4 @@ new_array_for_sum(PyArrayObject *ap1, PyArrayObject *ap2, PyArrayObject* out,
int nd, npy_intp dimensions[], int typenum, PyArrayObject **result);
#endif
+
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c
index bf90142e1..0616bed65 100644
--- a/numpy/core/src/multiarray/ctors.c
+++ b/numpy/core/src/multiarray/ctors.c
@@ -678,6 +678,12 @@ discover_itemsize(PyObject *s, int nd, int *itemsize, int string_type)
return 0;
}
+typedef enum {
+ DISCOVERED_OK = 0,
+ DISCOVERED_RAGGED = 1,
+ DISCOVERED_OBJECT = 2
+} discovered_t;
+
/*
* Take an arbitrary object and discover how many dimensions it
* has, filling in the dimensions as we go.
@@ -685,7 +691,7 @@ discover_itemsize(PyObject *s, int nd, int *itemsize, int string_type)
static int
discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
int stop_at_string, int stop_at_tuple,
- int *out_is_object)
+ discovered_t *out_is_object)
{
PyObject *e;
npy_intp n, i;
@@ -871,7 +877,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
if (PyErr_ExceptionMatches(PyExc_KeyError)) {
PyErr_Clear();
*maxndim = 0;
- *out_is_object = 1;
+ *out_is_object = DISCOVERED_OBJECT;
return 0;
}
else {
@@ -930,7 +936,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it,
*maxndim = all_elems_maxndim + 1;
if (!all_dimensions_match) {
/* typically results in an array containing variable-length lists */
- *out_is_object = 1;
+ *out_is_object = DISCOVERED_RAGGED;
}
}
@@ -1741,7 +1747,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
/* Try to treat op as a list of lists */
if (!writeable && PySequence_Check(op)) {
- int check_it, stop_at_string, stop_at_tuple, is_object;
+ int check_it, stop_at_string, stop_at_tuple;
int type_num, type;
/*
@@ -1791,7 +1797,7 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
((*out_dtype)->names || (*out_dtype)->subarray));
*out_ndim = NPY_MAXDIMS;
- is_object = 0;
+ discovered_t is_object = DISCOVERED_OK;
if (discover_dimensions(
op, out_ndim, out_dims, check_it,
stop_at_string, stop_at_tuple, &is_object) < 0) {
@@ -1808,7 +1814,27 @@ PyArray_GetArrayParamsFromObject(PyObject *op,
return 0;
}
/* If object arrays are forced */
- if (is_object) {
+ if (is_object != DISCOVERED_OK) {
+ static PyObject *visibleDeprecationWarning = NULL;
+ npy_cache_import(
+ "numpy", "VisibleDeprecationWarning",
+ &visibleDeprecationWarning);
+ if (visibleDeprecationWarning == NULL) {
+ return -1;
+ }
+ if (is_object == DISCOVERED_RAGGED && requested_dtype == NULL) {
+ /* NumPy 1.19, 2019-11-01 */
+ if (PyErr_WarnEx(visibleDeprecationWarning, "Creating an "
+ "ndarray from ragged nested sequences (which is a "
+ "list-or-tuple of lists-or-tuples-or ndarrays with "
+ "different lengths or shapes) is deprecated. If you "
+ "meant to do this, you must specify 'dtype=object' "
+ "when creating the ndarray", 1) < 0)
+ {
+ return -1;
+ }
+ }
+ /* either DISCOVERED_OBJECT or there is a requested_dtype */
Py_DECREF(*out_dtype);
*out_dtype = PyArray_DescrFromType(NPY_OBJECT);
if (*out_dtype == NULL) {
@@ -2059,6 +2085,8 @@ PyArray_FromAny(PyObject *op, PyArray_Descr *newtype, int min_depth,
*
* NPY_ARRAY_FORCECAST will cause a cast to occur regardless of whether or not
* it is safe.
+ *
+ * context is passed through to PyArray_GetArrayParamsFromObject
*/
/*NUMPY_API
diff --git a/numpy/core/src/multiarray/einsum.c.src b/numpy/core/src/multiarray/einsum.c.src
index 70af3fef9..1cc557825 100644
--- a/numpy/core/src/multiarray/einsum.c.src
+++ b/numpy/core/src/multiarray/einsum.c.src
@@ -2152,6 +2152,11 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels)
}
/* A repeated label, find the original one and merge them. */
else {
+#ifdef __GNUC__
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wuninitialized"
+#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
+#endif
int i = icombinemap[idim + label];
icombinemap[idim] = -1;
@@ -2164,6 +2169,9 @@ get_combined_dims_view(PyArrayObject *op, int iop, char *labels)
return NULL;
}
new_strides[i] += stride;
+#ifdef __GNUC__
+#pragma GCC diagnostic pop
+#endif
}
}
diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c
index af0cef15b..11e0bc44d 100644
--- a/numpy/core/src/multiarray/multiarraymodule.c
+++ b/numpy/core/src/multiarray/multiarraymodule.c
@@ -1559,7 +1559,6 @@ _prepend_ones(PyArrayObject *arr, int nd, int ndmin, NPY_ORDER order)
return ret;
}
-
#define STRIDING_OK(op, order) \
((order) == NPY_ANYORDER || \
(order) == NPY_KEEPORDER || \
diff --git a/numpy/core/src/umath/simd.inc.src b/numpy/core/src/umath/simd.inc.src
index 448aa3e72..121597579 100644
--- a/numpy/core/src/umath/simd.inc.src
+++ b/numpy/core/src/umath/simd.inc.src
@@ -1134,7 +1134,7 @@ sse2_@kind@_@TYPE@(@type@ * ip, @type@ * op, const npy_intp n)
/* Order of operations important for MSVC 2015 */
*op = (*op @OP@ ip[i] || npy_isnan(*op)) ? *op : ip[i];
}
- assert((npy_uintp)n < (stride) || npy_is_aligned(&ip[i], VECTOR_SIZE_BYTES));
+ assert(n < stride || npy_is_aligned(&ip[i], VECTOR_SIZE_BYTES));
if (i + 3 * stride <= n) {
/* load the first elements */
@vtype@ c1 = @vpre@_load_@vsuf@((@type@*)&ip[i]);
diff --git a/numpy/core/tests/test_deprecations.py b/numpy/core/tests/test_deprecations.py
index 9d68167fa..252133d7b 100644
--- a/numpy/core/tests/test_deprecations.py
+++ b/numpy/core/tests/test_deprecations.py
@@ -536,3 +536,16 @@ class TestNonZero(_DeprecationTestCase):
def test_zerod(self):
self.assert_deprecated(lambda: np.nonzero(np.array(0)))
self.assert_deprecated(lambda: np.nonzero(np.array(1)))
+
+
+def test_deprecate_ragged_arrays():
+ # 2019-11-29 1.19.0
+ #
+ # NEP 34 deprecated automatic object dtype when creating ragged
+ # arrays. Also see the "ragged" tests in `test_multiarray`
+ #
+ # emits a VisibleDeprecationWarning
+ arg = [1, [2, 3]]
+ with assert_warns(np.VisibleDeprecationWarning):
+ np.array(arg)
+
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
index 503566b63..600941cfd 100644
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -424,7 +424,7 @@ class TestArrayConstruction:
assert_equal(r, np.ones((2, 6, 6)))
d = np.ones((6, ))
- r = np.array([[d, d + 1], d + 2])
+ r = np.array([[d, d + 1], d + 2], dtype=object)
assert_equal(len(r), 2)
assert_equal(r[0], [d, d + 1])
assert_equal(r[1], d + 2)
@@ -1020,34 +1020,60 @@ class TestCreation:
assert_raises(ValueError, np.ndarray, buffer=buf, strides=(0,),
shape=(max_bytes//itemsize + 1,), dtype=dtype)
- def test_jagged_ndim_object(self):
- # Lists of mismatching depths are treated as object arrays
- a = np.array([[1], 2, 3])
- assert_equal(a.shape, (3,))
- assert_equal(a.dtype, object)
+ def _ragged_creation(self, seq):
+ # without dtype=object, the ragged object should raise
+ with assert_warns(np.VisibleDeprecationWarning):
+ a = np.array(seq)
+ b = np.array(seq, dtype=object)
+ assert_equal(a, b)
+ return b
- a = np.array([1, [2], 3])
+ def test_ragged_ndim_object(self):
+ # Lists of mismatching depths are treated as object arrays
+ a = self._ragged_creation([[1], 2, 3])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
- a = np.array([1, 2, [3]])
+ a = self._ragged_creation([1, [2], 3])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
- def test_jagged_shape_object(self):
- # The jagged dimension of a list is turned into an object array
- a = np.array([[1, 1], [2], [3]])
+ a = self._ragged_creation([1, 2, [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
- a = np.array([[1], [2, 2], [3]])
+ def test_ragged_shape_object(self):
+ # The ragged dimension of a list is turned into an object array
+ a = self._ragged_creation([[1, 1], [2], [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
- a = np.array([[1], [2], [3, 3]])
+ a = self._ragged_creation([[1], [2, 2], [3]])
assert_equal(a.shape, (3,))
assert_equal(a.dtype, object)
+ a = self._ragged_creation([[1], [2], [3, 3]])
+ assert a.shape == (3,)
+ assert a.dtype == object
+
+ def test_array_of_ragged_array(self):
+ outer = np.array([None, None])
+ outer[0] = outer[1] = np.array([1, 2, 3])
+ assert np.array(outer).shape == (2,)
+ assert np.array([outer]).shape == (1, 2)
+
+ outer_ragged = np.array([None, None])
+ outer_ragged[0] = np.array([1, 2, 3])
+ outer_ragged[1] = np.array([1, 2, 3, 4])
+ # should both of these emit deprecation warnings?
+ assert np.array(outer_ragged).shape == (2,)
+ assert np.array([outer_ragged]).shape == (1, 2,)
+
+ def test_deep_nonragged_object(self):
+ # None of these should raise, even though they are missing dtype=object
+ a = np.array([[[Decimal(1)]]])
+ a = np.array([1, Decimal(1)])
+ a = np.array([[1], [Decimal(1)]])
class TestStructured:
def test_subarray_field_access(self):
diff --git a/numpy/core/tests/test_numeric.py b/numpy/core/tests/test_numeric.py
index 79f331121..c4956c298 100644
--- a/numpy/core/tests/test_numeric.py
+++ b/numpy/core/tests/test_numeric.py
@@ -1209,7 +1209,7 @@ class TestNonzero:
def test_nonzero_invalid_object(self):
# gh-9295
- a = np.array([np.array([1, 2]), 3])
+ a = np.array([np.array([1, 2]), 3], dtype=object)
assert_raises(ValueError, np.nonzero, a)
class BoolErrors:
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py
index 1d268d3fc..3a9b96886 100644
--- a/numpy/core/tests/test_regression.py
+++ b/numpy/core/tests/test_regression.py
@@ -1354,13 +1354,13 @@ class TestRegression:
def test_array_from_sequence_scalar_array(self):
# Ticket #1078: segfaults when creating an array with a sequence of
# 0d arrays.
- a = np.array((np.ones(2), np.array(2)))
+ a = np.array((np.ones(2), np.array(2)), dtype=object)
assert_equal(a.shape, (2,))
assert_equal(a.dtype, np.dtype(object))
assert_equal(a[0], np.ones(2))
assert_equal(a[1], np.array(2))
- a = np.array(((1,), np.array(1)))
+ a = np.array(((1,), np.array(1)), dtype=object)
assert_equal(a.shape, (2,))
assert_equal(a.dtype, np.dtype(object))
assert_equal(a[0], (1,))
@@ -1368,7 +1368,7 @@ class TestRegression:
def test_array_from_sequence_scalar_array2(self):
# Ticket #1081: weird array with strange input...
- t = np.array([np.array([]), np.array(0, object)])
+ t = np.array([np.array([]), np.array(0, object)], dtype=object)
assert_equal(t.shape, (2,))
assert_equal(t.dtype, np.dtype(object))
@@ -2256,9 +2256,10 @@ class TestRegression:
x[0], x[-1] = x[-1], x[0]
uf = np.frompyfunc(f, 1, 0)
- a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]])
+ a = np.array([[1, 2, 3], [4, 5], [6, 7, 8, 9]], dtype=object)
assert_equal(uf(a), ())
- assert_array_equal(a, [[3, 2, 1], [5, 4], [9, 7, 8, 6]])
+ expected = np.array([[3, 2, 1], [5, 4], [9, 7, 8, 6]], dtype=object)
+ assert_array_equal(a, expected)
@pytest.mark.skipif(not HAS_REFCOUNT, reason="Python lacks refcounts")
def test_leak_in_structured_dtype_comparison(self):
diff --git a/numpy/core/tests/test_ufunc.py b/numpy/core/tests/test_ufunc.py
index a299bcf28..abdaeeb93 100644
--- a/numpy/core/tests/test_ufunc.py
+++ b/numpy/core/tests/test_ufunc.py
@@ -1160,14 +1160,18 @@ class TestUfunc:
# Twice reproduced also for tuples:
np.add.accumulate(arr, out=arr)
np.add.accumulate(arr, out=arr)
- assert_array_equal(arr, np.array([[1]*i for i in [1, 3, 6, 10]]))
+ assert_array_equal(arr,
+ np.array([[1]*i for i in [1, 3, 6, 10]], dtype=object),
+ )
# And the same if the axis argument is used
arr = np.ones((2, 4), dtype=object)
arr[0, :] = [[2] for i in range(4)]
np.add.accumulate(arr, out=arr, axis=-1)
np.add.accumulate(arr, out=arr, axis=-1)
- assert_array_equal(arr[0, :], np.array([[2]*i for i in [1, 3, 6, 10]]))
+ assert_array_equal(arr[0, :],
+ np.array([[2]*i for i in [1, 3, 6, 10]], dtype=object),
+ )
def test_object_array_reduceat_inplace(self):
# Checks that in-place reduceats work, see also gh-7465