Skip to content

BUG: Fix searchsorted and CheckFromAny byte-swapping logic #28418

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 2 commits into from
Mar 5, 2025

Conversation

seberg
Copy link
Member

@seberg seberg commented Mar 3, 2025

This closes gh-28190 and fixes another issue in the initial code that triggered the regression.

Note that we may still want to avoid this, since this does lead to constructing (view compatible) structured dtypes unnecessarily here.

It would also compactify the dtype. For building unnecessary dtypes, the better solution may be to just introduce a "canonical" flag to the dtypes (now that we have the space).


Actually, the bug may be going back to when the _int version was added (and started using borrowed references rather than stealing). Somehow, it seems we got away with that for a suprisingly long time.

@charris charris added the 09 - Backport-Candidate PRs tagged should be backported label Mar 3, 2025
This closes numpygh-28190 and fixes another issue in the initial code
that triggered the regression.

Note that we may still want to avoid this, since this does lead to
constructing (view compatible) structured dtypes unnecessarily here.

It would also compactify the dtype.  For building unnecessary dtypes,
the better solution may be to just introduce a "canonical" flag to
the dtypes (now that we have the space).
Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. The simplification in item_selection is worth it even if there hadn't been a bug!

Inline some suggestions that may help clarity.

/* refs to dtype we own = 1 */
Py_INCREF(dtype);
/* refs to dtype we own = 2 */
/* need ap2 as contiguous array and of right dtype (steals and may be replace it) */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace it -> replace dtype (since "it" could also refer to ap2, the array).

Actually, with my comment below, I think this can just be

/* need ap2 as contiguous array and of right dtype (note: steals dtype reference) */

return NULL;
}
/* dtype was stolen, replace it in case the array creation replaced it. */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine with l.2162? And write there,

/*
 * The dtype reference we had was used for creating ap2, which may have
 * replaced it with another. So here we copy the dtype of ap2 and use it for `ap1`.
 */
dtype = (PyArray_Descr *)Py_NewRef(PyArray_DESCR(ap2));

Copy link
Contributor

@mhvk mhvk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks all good to me, although still a small query of the just-in-case variety.

@mhvk
Copy link
Contributor

mhvk commented Mar 5, 2025

Might as well get this in, I think!

@mhvk mhvk merged commit 01e98f1 into numpy:main Mar 5, 2025
68 checks passed
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: np.searchsorted segfaults on structured arrays in 2.2.2
4 participants