pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.8k stars 2.12k forks source link

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

Closed MaartenBaert closed 6 days ago

MaartenBaert commented 3 weeks ago

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>
MaartenBaert commented 6 days ago

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

rwgk commented 6 days ago

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 commented 6 days ago

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

I added one commit: a1b7094a5507ef243dad2deea1cb44bef993bc0d

I also git merged master.

Waiting for GitHub Actions to finish.

rwgk commented 6 days ago

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