8000 Merge pull request #26603 from mtsokol/array-copy-str · mathomp4/numpy@7f6ac72 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f6ac72

Browse files
authored
Merge pull request numpy#26603 from mtsokol/array-copy-str
BUG: Disallow string inputs for ``copy`` keyword in ``np.array`` and ``np.asarray``
2 parents c41621b + 923d2ad commit 7f6ac72

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

numpy/_core/src/multiarray/conversion_utils.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,12 @@ PyArray_CopyConverter(PyObject *obj, NPY_COPYMODE *copymode) {
249249
return NPY_FAIL;
250250
}
251251
}
252+
else if(PyUnicode_Check(obj)) {
253+
PyErr_SetString(PyExc_ValueError,
254+
"strings are not allowed for 'copy' keyword. "
255+
"Use True/False/None instead.");
256+
return NPY_FAIL;
257+
}
252258
else {
253259
npy_bool bool_copymode;
254260
if (!PyArray_BoolConverter(obj, &bool_copymode)) {

numpy/_core/tests/test_multiarray.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,14 @@ def test_array_copy_true(self):
503503
assert_array_equal(e, [[1, 3, 7], [1, 2, 3]])
504504
assert_array_equal(d, [[1, 5, 3], [1,2,3]])
505505

506+
def test_array_copy_str(self):
507+
with pytest.raises(
508+
ValueError,
509+
match="strings are not allowed for 'copy' keyword. "
510+
"Use True/False/None instead."
511+
):
512+
np.array([1, 2, 3], copy="always")
513+
506514
def test_array_cont(self):
507515
d = np.ones(10)[::2]
508516
assert_(np.ascontiguousarray(d).flags.c_contiguous)

0 commit comments

Comments
 (0)
0