summaryrefslogtreecommitdiff
path: root/numpy
diff options
context:
space:
mode:
authorSebastian Berg <sebastian@sipsolutions.net>2020-06-27 11:25:28 -0500
committerGitHub <noreply@github.com>2020-06-27 11:25:28 -0500
commitf156d37e63cf1f99635153e0022695285fc4a58d (patch)
tree8be86a160161e80c543cf0afc03cadaf3839ea0a /numpy
parentfafd123410f1ad5c47341c66d8ea4cf541165e77 (diff)
parent8ac7ac94ab868473e2276902be656946745c641f (diff)
downloadnumpy-f156d37e63cf1f99635153e0022695285fc4a58d.tar.gz
Merge pull request #16570 from vrakesh/concat_sequence
ENH: Throw TypeError on operator concat on Numpy Arrays
Diffstat (limited to 'numpy')
-rw-r--r--numpy/core/src/multiarray/sequence.c17
-rw-r--r--numpy/core/tests/test_shape_base.py15
2 files changed, 30 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/sequence.c b/numpy/core/src/multiarray/sequence.c
index 1efdd204f..1c74f1719 100644
--- a/numpy/core/src/multiarray/sequence.c
+++ b/numpy/core/src/multiarray/sequence.c
@@ -50,9 +50,24 @@ array_contains(PyArrayObject *self, PyObject *el)
return ret;
}
+static PyObject *
+array_concat(PyObject *self, PyObject *other)
+{
+ /*
+ * Throw a type error, when trying to concat NDArrays
+ * NOTE: This error is not Thrown when running with PyPy
+ */
+ PyErr_SetString(PyExc_TypeError,
+ "Concatenation operation is not implemented for NumPy arrays, "
+ "use np.concatenate() instead. Please do not rely on this error; "
+ "it may not be given on all Python implementations.");
+ return NULL;
+}
+
+
NPY_NO_EXPORT PySequenceMethods array_as_sequence = {
(lenfunc)array_length, /*sq_length*/
- (binaryfunc)NULL, /*sq_concat is handled by nb_add*/
+ (binaryfunc)array_concat, /*sq_concat for operator.concat*/
(ssizeargfunc)NULL,
(ssizeargfunc)array_item,
(ssizessizeargfunc)NULL,
diff --git a/numpy/core/tests/test_shape_base.py b/numpy/core/tests/test_shape_base.py
index 546ecf001..94a916193 100644
--- a/numpy/core/tests/test_shape_base.py
+++ b/numpy/core/tests/test_shape_base.py
@@ -8,7 +8,7 @@ from numpy.core.shape_base import (_block_dispatcher, _block_setup,
_block_concatenate, _block_slicing)
from numpy.testing import (
assert_, assert_raises, assert_array_equal, assert_equal,
- assert_raises_regex, assert_warns
+ assert_raises_regex, assert_warns, IS_PYPY
)
@@ -320,6 +320,19 @@ class TestConcatenate:
assert_(out is rout)
assert_equal(res, rout)
+ @pytest.mark.skipif(IS_PYPY, reason="PYPY handles sq_concat, nb_add differently than cpython")
+ def test_operator_concat(self):
+ import operator
+ a = array([1, 2])
+ b = array([3, 4])
+ n = [1,2]
+ res = array([1, 2, 3, 4])
+ assert_raises(TypeError, operator.concat, a, b)
+ assert_raises(TypeError, operator.concat, a, n)
+ assert_raises(TypeError, operator.concat, n, a)
+ assert_raises(TypeError, operator.concat, a, 1)
+ assert_raises(TypeError, operator.concat, 1, a)
+
def test_bad_out_shape(self):
a = array([1, 2])
b = array([3, 4])