GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: avoid segfaults when legacy copyswap slot is not defined by ngoldbaum · Pull Request #32151 · numpy/numpy

Conversation

@ngoldbaum

@ngoldbaum ngoldbaum commented Jul 30, 2026

Copy link
Copy Markdown
Member

PR summary

Towards addressing #24859 (see also #30795 ping @SwayamInSync). This avoids segfaults in many operations if copyswap or copyswapn isn't defined. Also see #32150 which does the same thing along with a small behavior change in np.byteswap.

AI Disclosure

I used an AI model to search the codebase and debug/test the fixes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ngoldbaum
BUG: avoid segfaults when legacy copyswap slot is not defined
599cbed
@ngoldbaum ngoldbaum added 00 - Bug 09 - Backport-Candidate PRs tagged should be backported component: numpy.dtype labels Jul 30, 2026
@SwayamInSync

SwayamInSync commented Jul 30, 2026

Copy link
Copy Markdown
Member

This was needed (I had it in my next todos too) I think prevention is good for backporting, and maybe for future releases should we also think about adding a slot?
@seberg has thoughts that user-dtypes having byteorder parameter might be more useful but he is also okay with adding new slot

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum

ngoldbaum commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

maybe for future releases should we also think about adding a slot?

Sounds good to me if you want to take that on!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum

ngoldbaum commented Jul 30, 2026

Copy link
Copy Markdown
Member Author

And can you give this an official code review pass? We're a bit short on reviewers right now so every review helps.

SwayamInSync reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

SwayamInSync
SwayamInSync suggested changes Jul 30, 2026
View reviewed changes

@SwayamInSync SwayamInSync left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Maybe need to tight some areas, specially around introducing an error-producing recursive VOID_copyswap without updating this success-returning caller.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/arraytypes.c.src Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/src/multiarray/methods.c
PyArray_CopySwapNFunc *copyswapn;
PyArrayIterObject *it;

copyswapn = PyDataType_GetArrFuncs(PyArray_DESCR(self))->copyswapn;

@SwayamInSync SwayamInSync Jul 30, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The recursive structured-dtype paths are fixed here, but the top-level PyArray_Byteswap path still calls a potentially NULL copyswapn. I verified that direct SF array byteswap still exits with SIGSEGV:

SF = _get_sfloat_dtype()
np.array([1., 2.]).view(SF(1.)).byteswap()

I see that #32150 handles this case. Since this PR is marked as a backport candidate while #32150 is a separate enhancement, should they be treated as dependent for release/backport purposes? Otherwise, should the title or summary clarify that this PR covers the structured/recursive call paths rather than all missing-copyswap[n] cases?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ngoldbaum ngoldbaum Jul 30, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I can do a second backport-only version that doesn't change any behavior and unconditionally raises an error. But that can land directly on the 2.5 branch.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ngoldbaum ngoldbaum Jul 31, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Actually on second thought, to make the backport easier, I'll add just the null-check and TypeError on this branch.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/tests/test_custom_dtypes.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/tests/test_custom_dtypes.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum
MAINT: address review comments
cb863ac
ngoldbaum
ngoldbaum commented Jul 30, 2026
View reviewed changes

@ngoldbaum ngoldbaum left a comment

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I addressed three of @SwayamInSync's comments and left a comment about how to handle backporting the fix in #32150

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c
PyArray_CopySwapNFunc *copyswapn;
PyArrayIterObject *it;

copyswapn = PyDataType_GetArrFuncs(PyArray_DESCR(self))->copyswapn;

@ngoldbaum ngoldbaum Jul 30, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I can do a second backport-only version that doesn't change any behavior and unconditionally raises an error. But that can land directly on the 2.5 branch.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
SwayamInSync
SwayamInSync approved these changes Jul 31, 2026
View reviewed changes

@SwayamInSync SwayamInSync left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ngoldbaum
MAINT: Make all operations that segfaulted raise TypeError
dec685f
@ngoldbaum ngoldbaum mentioned this pull request Jul 31, 2026
ENH: add no-op fallback in np.byteswap for types with no byte order #32150
Merged
@ngoldbaum

ngoldbaum commented Jul 31, 2026

Copy link
Copy Markdown
Member Author

@SwayamInSync sorry to bother you but can I get you to do one more pass over this? I made it so the helper always errors but I left in-place the infrastructure for relaxing that (in #32150). Does that make sense to you? Do you see any issues in how I did it?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

SwayamInSync
SwayamInSync reviewed Aug 1, 2026
View reviewed changes

@SwayamInSync SwayamInSync left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Some nits, would be quick to resolve.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Outdated
PyArrayIterObject *it;

copyswapn = PyDataType_GetArrFuncs(PyArray_DESCR(self))->copyswapn;
if (copyswapn == NULL) {

@SwayamInSync SwayamInSync Aug 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Could the existing in-place writeability check remain before this missing-slot check?

A read-only new-style array previously returned from PyArray_FailUnlessWriteable before reaching the NULL copyswapn call. With this ordering it now raises the dtype TypeError instead:

arr = np.zeros(2, dtype=SF(1.))
arr.flags.writeable = False
arr.byteswap(inplace=True)

Previously this raised ValueError: array to be byte-swapped is read-only this branch now raises TypeError: dtype ... does not implement copyswap
Moving the inplace && PyArray_FailUnlessWriteable(...) check before the slot check would preserve that existing behavior. It would also ensure that the retained no-op path still rejects read-only arrays when is relaxed in #32150.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ngoldbaum reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@ngoldbaum ngoldbaum Aug 3, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'll take a look at this suggestion

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/scalartypes.c.src Outdated
Py_DECREF(descr);
return PyErr_NoMemory();
}
else if (copyswap == NULL) {

@SwayamInSync SwayamInSync Aug 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Since this branch is being retained for the later relaxation in #32150, I think scalar reconstruction will need different handling when check_missing_copyswap starts returning success.

For a normal new-style scalar, this branch reaches:

PyArray_Scalar(newmem, descr, NULL)

That dispatches through legacy_getitem_using_DType, which rejects the NULL array argument. I reproduced this with a quaddtype scalar when the earlier helper treated byteorder == '|' as a no-op:

RuntimeError: Using legacy SETITEM with NULL array object is only supported for basic NumPy DTypes.

The current always-error policy does not reach this code, so this is not a blocker for the backportable fix. But #32150 should either handle the immutable scalar as a direct no-op or reconstruct it through the new-style getitem slot rather than activating this path unchanged.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ngoldbaum ngoldbaum Aug 3, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'll just delete this dead branch in this PR and deal with this issue in #32150, thanks for pointing it out.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/arraytypes.c.src
}
continue;
}
copyswapn(dst+offset, dstride,

@SwayamInSync SwayamInSync Aug 1, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

One non-blocking behavior question: with the new always-error policy, an in-place structured byteswap can modify supported fields encountered before an unsupported field and then raise TypeError, leaving the array partially byteswapped.

For example:

arr = np.zeros(
    2, dtype=[("before", "i4"), ("v", SF(1.)), ("after", "i4")]
)
arr["before"] = [1, 2]
arr["after"] = [3, 4]

arr.byteswap(inplace=True)

After the TypeError, before has been byteswapped to [16777216, 33554432], while after remains [3, 4].

Is partial mutation acceptable for this in-place API, or should the fields be preflighted for missing copyswap slots before mutation? I do not know of an atomicity guarantee here, so this is non-blocking.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@seberg seberg Aug 3, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I guess I can live this it... Would be nicer if we just checked things ahead of time, but that seems tedious. (The point of the PR is in the end to avoid a segfault, so...)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ngoldbaum ngoldbaum Aug 3, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'll take a look at whether checking first makes the diff complicated, if it does I'll defer this to the followup PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@SwayamInSync

SwayamInSync commented Aug 1, 2026

Copy link
Copy Markdown
Member

@SwayamInSync sorry to bother you but can I get you to do one more pass over this?

Hey no worries, I will be happy to review more in the case we are being short on reviewers

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg
seberg reviewed Aug 3, 2026
View reviewed changes

@seberg seberg left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks, generally seems fine and should also be fine to backport.

Part of me wonders if we should abuse the _former_flags field or so to flag support for void compare and copyswap (others?) at creation time, but I dunno if that's worth it.

I would be tempted to delete at least the setitem fast-path, but for the "just get this fixed" part, probably not worth it, even if the try/except logic is ugly.

I guess my only real request is to rename check_missing_copyswap and I don't really see the point of the swap_only (at least call it inplace_swap)?!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/arraytypes.c.src Outdated

/* See common.h for the contract of this helper. */
NPY_NO_EXPORT int
check_missing_copyswap(PyArray_Descr *dtype, int swap_only)

@seberg seberg Aug 3, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can we rename this or actually make it check?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ngoldbaum ngoldbaum Aug 3, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The check is coming in a followup (#32150). I'm setting up the internal API just to make the diff of the followup smaller.

But I will change the name to inplace_swap, you're right it's better.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

seberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@MaanasArora MaanasArora Aug 6, 2026

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry, late and not blocking - but if this checks in #32150, would we always keep chaining with == NULL &&? But then it is already found missing, and the function should not need to check?

Since we're restructuring all the call sites, we could just output copyswap, as in get_copyswap_or_raise(PyArray_Descr *dtype, int inplace_swap, PyArray_CopySwapFunc** copyswap)? Seems a bit 'out there', but this is a narrow use-case. Probably too much for this PR except for a rename if needed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/methods.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread numpy/_core/src/multiarray/arraytypes.c.src
}
}
if (i == names_size) {
return 0;

@seberg seberg Aug 3, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I almost wonder if we should just hope that TRIVIALLY_COPYABLE s now typically set (on the destination I guess), add a fast-path that makes use of that, and just delete this whole fast-path... (In part, because this try/except style s annoying as well, but also because the fast-path should be hit often and actually be fast as promised.)

But yeah, looks OK here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@ngoldbaum ngoldbaum Aug 3, 2026

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks! I'll think about folding this into #32150.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/_core/src/multiarray/arraytypes.c.src
}
continue;
}
copyswapn(dst+offset, dstride,

@seberg seberg Aug 3, 2026

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I guess I can live this it... Would be nicer if we just checked things ahead of time, but that seems tedious. (The point of the PR is in the end to avoid a segfault, so...)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
ngoldbaum added 3 commits August 3, 2026 16:22
@ngoldbaum
MAINT: address review comments and delete dead code branches
2b44ffe
@ngoldbaum
DOC: strengthen comment
72d44c4
@ngoldbaum
BUG: early-exit on error for .flat and .place
fbabf73
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Aug 4, 2026
@ngoldbaum
ENH: add no-op fallback in np.byteswap for types with no byte order
1dceb7b
[2.4.x backport: rebased onto the numpy#32151 backport — replaces the strict
check_missing_copyswap() guard in PyArray_Byteswap with the no-op
fallback, and removes test_byteswap_raises, whose TypeError expectation
for byteorder-less dtypes this commit deliberately changes to a no-op;
the read-only inplace ValueError path stays covered by the StringDType
test_byteswap.]

(cherry picked from commit 18f6dfa)
@MaanasArora

MaanasArora commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Looks good and correct, thanks! Also just minor question about the work check_missing_copyswap is doing (comment in thread).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum
MAINT: call it raise_missing_copyswap
6af11ac
@ngoldbaum

ngoldbaum commented Aug 6, 2026

Copy link
Copy Markdown
Member Author

OK, let's get this in so it ends up in the bugfix release this weekend.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ngoldbaum
ngoldbaum merged commit e117b3c into numpy:main Aug 6, 2026
115 of 128 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@charris charris mentioned this pull request Aug 7, 2026
BUG: avoid segfaults when legacy copyswap slot is not defined (#32151) #32221
Merged
@charris charris removed the 09 - Backport-Candidate PRs tagged should be backported label Aug 7, 2026
charris pushed a commit that referenced this pull request Aug 7, 2026
@ngoldbaum @charris
BUG: avoid segfaults when legacy copyswap slot is not defined (#32151)
23b30f4
charris added a commit that referenced this pull request Aug 7, 2026
@charris
Merge pull request #32221 from charris/backport-32151
ecf599c
BUG: avoid segfaults when legacy copyswap slot is not defined (#32151)
ngoldbaum added a commit to ngoldbaum/numpy that referenced this pull request Sep 8, 2026
@ngoldbaum
BUG: avoid segfaults when legacy copyswap slot is not defined (numpy#…
a7828c8
…32151)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@seberg seberg seberg left review comments
@MaanasArora MaanasArora MaanasArora left review comments
+1 more reviewer
@SwayamInSync SwayamInSync SwayamInSync approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

00 - Bug component: numpy.dtype

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@ngoldbaum @SwayamInSync @MaanasArora @seberg @charris
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.