Skip to content
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

Add support for array_t<handle> and array_t<object> #5427

Merged
merged 13 commits into from
Nov 16, 2024

Conversation

MaartenBaert
Copy link
Contributor

Description

This commit adds improved support for NumPy arrays with object dtype, by allowing users to access such arrays as array_t<handle> or array_t<object>, which are more convenient than array_t<PyObject*> (which was introduced in PR #4674). In particular, array_t<object> provides automatic memory management.

This feature relies on the fact that object and handle have the same memory layout as PyObject*, i.e. sizeof(object) == sizeof(handle) == sizeof(PyObject*). If this is somehow not the case, the enable_if_t test should automatically disable this feature.

I have also extended and improved the tests related to this feature with additional tests to detect memory leaks due to incorrect refcounts.

Suggested changelog entry:

Add support for array_t<handle> and array_t<object>

tests/test_numpy_array.py Outdated Show resolved Hide resolved
tests/test_numpy_array.cpp Outdated Show resolved Hide resolved
include/pybind11/numpy.h Outdated Show resolved Hide resolved
@MaartenBaert
Copy link
Contributor Author

It looks like "CI / 🐍 3 • Clang 15 • C++20 • x64" was a spurious failure unrelated to my change.

@rwgk
Copy link
Collaborator

rwgk commented Nov 16, 2024

It looks like "CI / 🐍 3 • Clang 15 • C++20 • x64" was a spurious failure unrelated to my change.

Yes, certainly unrelated. Similarly, "🐍 3 • Clang dev • C++11 • x64", it seems to be stuck on a wget for 2+ hours.

rwgk added 2 commits November 16, 2024 13:14
This was meant to further stress-test correctness of refcount handling.

All modified test functions were manually leak-checked (`while True`, top command, Python 3.12.3, Ubuntu 24.01, gcc 13.2.0).
@rwgk
Copy link
Collaborator

rwgk commented Nov 16, 2024

Looks good, thanks for the great work on the tests!

I added one commit: a1b7094

I also git merged master.

Waiting for GitHub Actions to finish.

@rwgk
Copy link
Collaborator

rwgk commented Nov 16, 2024

The "CI / 🐍 3 • windows-latest • mingw32 (pull_request)" is also definitely unrelated. Everything else works. Merging. Thanks @MaartenBaert!

@rwgk rwgk merged commit b9fb316 into pybind:master Nov 16, 2024
80 of 81 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Nov 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs changelog Possibly needs a changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants