Skip to content

BUG: fix deepcopying StringDType arrays #28643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion numpy/_core/src/multiarray/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -1581,7 +1581,7 @@ _deepcopy_call(char *iptr, char *optr, PyArray_Descr *dtype,
}
}
}
else {
else if (PyDataType_ISOBJECT(dtype)) {
PyObject *itemp, *otemp;
PyObject *res;
memcpy(&itemp, iptr, sizeof(itemp));
Expand Down
21 changes: 14 additions & 7 deletions numpy/_core/tests/test_stringdtype.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import copy
import itertools
import os
import pickle
import string
import sys
import tempfile

Expand Down Expand Up @@ -371,6 +371,13 @@ def test_pickle(dtype, string_list):
os.remove(f.name)


def test_stdlib_copy(dtype, string_list):
arr = np.array(string_list, dtype=dtype)

assert_array_equal(copy.copy(arr), arr)
assert_array_equal(copy.deepcopy(arr), arr)


@pytest.mark.parametrize(
"strings",
[
Expand Down Expand Up @@ -1674,12 +1681,12 @@ def test_zeros(self):
assert_array_equal(z, "")

def test_copy(self):
c = self.a.copy()
assert_array_equal(self.get_flags(c), self.get_flags(self.a))
assert_array_equal(c, self.a)
offsets = self.get_view(c)['offset']
assert offsets[2] == 1
assert offsets[3] == 1 + len(self.s_medium) + self.sizeofstr // 2
for c in [self.a.copy(), copy.copy(self.a), copy.deepcopy(self.a)]:
assert_array_equal(self.get_flags(c), self.get_flags(self.a))
assert_array_equal(c, self.a)
offsets = self.get_view(c)['offset']
assert offsets[2] == 1
assert offsets[3] == 1 + len(self.s_medium) + self.sizeofstr // 2

def test_arena_use_with_setting(self):
c = np.zeros_like(self.a)
Expand Down
Loading