Skip to content

Conversation

@seberg
Copy link
Member

@seberg seberg commented Jul 8, 2025

This fixes the scalar handling in the array-interface and also a bug in the shape handling error path.

In theory it breaks code that used NULL to trigger the scalar path, but this path was:

  1. Completely undocumented, and
  2. correctly triggered by ommitting the data field instead.

I didn't remove/deprecate the scalar path in this PR, maybe we should. But, I do think we can ignore that theoretical use-case since it is nonsensical.

Closes gh-26037

goto fail;
}
if (PyArray_SETITEM(ret, PyArray_DATA(ret), origin) < 0) {
if (PyArray_Pack(PyArray_DESCR(ret), PyArray_DATA(ret), origin) < 0) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This probably doesn't matter in practice, but could matter for new dtypes in principle. It's just the right way now.

}
else if (result == 1) {
Py_DECREF(iface);
Py_DECREF(attr);
Copy link
Member Author

@seberg seberg Jul 8, 2025

Choose a reason for hiding this comment

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

This is a plain bug-fix, it could (and maybe should) be backported. In this branch attr is explicitly still NULL, since result == 0.

Copy link
Member

Choose a reason for hiding this comment

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

I agree. This must have been a bug from the GetItemRef migration. Care to make a backport PR for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, done in #29350

This fixes the scalar handling in the array-interface and also a bug
in the shape handling error path.

In theory it breaks code that used NULL to trigger the scalar path,
but this path was:
1. Completely undocumented, and
2. correctly triggered by *ommitting* the data field instead.

I didn't remove/deprecate the scalar path in this PR, maybe we should.
But, I do think we can ignore that theoretical use-case since it is
nonsensical.

Signed-off-by: Sebastian Berg <[email protected]>
@seberg seberg force-pushed the array-interface-null branch from 9ec8b22 to 10af699 Compare July 8, 2025 11:23
Copy link
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

}
else if (result == 1) {
Py_DECREF(iface);
Py_DECREF(attr);
Copy link
Member

Choose a reason for hiding this comment

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

I agree. This must have been a bug from the GetItemRef migration. Care to make a backport PR for this?

@seberg
Copy link
Member Author

seberg commented Jul 9, 2025

OK, one approval seems good enough. If anyone thinks that this weird NULL scalar path is actually used out there, let me know and I am happy to revert if needed.
(Although, even if it is used by one user, they should probably just adapt.)

@seberg seberg merged commit 34776f4 into numpy:main Jul 9, 2025
76 checks passed
@seberg seberg deleted the array-interface-null branch July 9, 2025 08:33
IndifferentArea pushed a commit to IndifferentArea/numpy that referenced this pull request Dec 7, 2025
numpy#29338)

This fixes the scalar handling in the array-interface and also a bug
in the shape handling error path.

In theory it breaks code that used NULL to trigger the scalar path,
but this path was:
1. Completely undocumented, and
2. correctly triggered by *ommitting* the data field instead.

I didn't remove/deprecate the scalar path in this PR, maybe we should.
But, I do think we can ignore that theoretical use-case since it is
nonsensical.

Signed-off-by: Sebastian Berg <[email protected]>
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: Cannot create zero-length array-like object via __array_interface__ providing null pointer as data

2 participants