Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: creating an array that shares memory with a stringdtype array can fail #26140

Copy link
Copy link
Closed
@ngoldbaum

Description

@ngoldbaum
Issue body actions

The following test case will fail on main:

import numpy as np
# strings long enough to live in the arena
string_list = ["abc", "def", "ghi" * 10, "A¢☃€ 😊" * 100, "Abc" * 1000, "DEF"]
arr = np.array(string_list, dtype=np.dtypes.StringDType())
arr_rev = np.array(string_list[::-1], dtype=np.dtypes.StringDType())

arr_view = np.array(arr, copy=None, dtype=arr_rev.dtype)
np.testing.assert_array_equal(arr, arr_view)

This is happening because of the code path here:

if (PyArray_EquivTypes(oldtype, dtype)) {
if (copy != NPY_COPY_ALWAYS && STRIDING_OK(oparr, order)) {
if (oldtype == dtype) {
Py_INCREF(op);
ret = oparr;
}
else {
/* Create a new PyArrayObject from the caller's
* PyArray_Descr. Use the reference `op` as the base
* object. */
Py_INCREF(dtype);
ret = (PyArrayObject *)PyArray_NewFromDescrAndBase(
Py_TYPE(op),
dtype,
PyArray_NDIM(oparr),
PyArray_DIMS(oparr),
PyArray_STRIDES(oparr),
PyArray_DATA(oparr),
PyArray_FLAGS(oparr),
op,
op
);
}
goto finish;
}

In the test case, oldtype != dtype, so we enter the path that calls NewFromDescrAndBase with the user-provided dtype. The problem is the user-provided dtype doesn't know anything about the arena that lives on the original dtype, so blindly accepting the user-provided dtype and passing it to NewFromDescrAndBase corrupts the arena.

I think a way to fix this would be to call finalize_descr on the user-provided DType to get a fresh descriptor if its already owned by the array, and then force the user-provided dtype to share allocators with the original dtype. We could also copy the arena, but that goes against the user desire to not have a copy.

Another way to handle this is to make stringdtype always copy in this situation, and error if someone passes copy=False.

Originally posted by @ngoldbaum in #26082 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.