Add helpers to array that return the size and strides as a std::span#5974
Add helpers to array that return the size and strides as a std::span#5974dbs4261 wants to merge 6 commits intopybind:masterfrom
Conversation
…es as a std::span. These functions are hidden with macros unless PYBIND11_CPP20 is defined and the <span> include has been found.
Add comprehensive unit tests for the new std::span helper functions: - Test 0D, 1D, 2D, and 3D arrays - Verify spans match regular shape()/strides() methods - Test that spans can be used to construct new arrays - Tests are conditionally compiled only when PYBIND11_HAS_SPAN is defined
|
My prompt for Cursor was merely:
Next thing I know, it had generated the missing unit tests. This is the full response: |
include/pybind11/detail/common.h
Outdated
| # define PYBIND11_HAS_STRING_VIEW 1 | ||
| #endif | ||
|
|
||
| #if defined(PYBIND11_CPP20) && defined(__has_include) && __has_include(<span>) |
There was a problem hiding this comment.
This isn't correct. Use the cpp feature label for this instead: https://en.cppreference.com/w/cpp/experimental/feature_test.html#cpp_lib_span this resolves the ambiguity of some pre CPP20 happen to having a global header called span somewhere
Replace __has_include(<span>) check with __cpp_lib_span feature test macro to resolve ambiguity where some pre-C++20 systems might have a global header called <span> that isn't the C++20 std::span. The check is moved after <version> is included, consistent with how __cpp_lib_char8_t is handled. Co-authored-by: Cursor <cursoragent@cursor.com>
|
For easy reference, these were the failures before pushing more commits: CI / 🐍 (windows-latest, 3.14, -DCMAKE_CXX_STANDARD=20) / 🧪 (pull_request) Failing after 4m CI / 🐍 (windows-latest, 3.14t, -DCMAKE_CXX_STANDARD=23) / 🧪 (pull_request) Failing after 5m CI / 🐍 3 • GCC 10 • C++20 • x64 (pull_request) Failing after 26s CI / 🐍 3.10 • MSVC 2022 • x86 -DCMAKE_CXX_STANDARD=20 (pull_request) Failing after 3m CI / 🐍 3.13 • windows-11-arm • clang-msvc (pull_request) Failing after 3m CI / windows-11-arm • clang-msys2 (pull_request) Failing after 4m |
On Windows/MSVC, ssize_t is not available in the standard namespace without proper includes. Use py::ssize_t (the pybind11 typedef) instead to ensure cross-platform compatibility. Fixes compilation errors on: - Windows/MSVC 2022 (C++20) - GCC 10 (C++20) Co-authored-by: Cursor <cursoragent@cursor.com>
|
@seberg Could you please help reviewing this (very small) PR? It looks good to me, but it'd be great to get someone more closely associated with the numpy community to take a look, too. (Please ignore the Manylinux 3.14t CI failure. It's definitely unrelated to this PR.) |
|
Looks good to me. Seems like the only question would be if there is a nicer naming/API pattern. But I can't think of a nicer alternative. (Not sure I quite follow the motivation, for creating a new array, I would different approaches are nicer, e.g. having an |
|
The actual motivation was to use them with ranges but it was easier to just describe the constructor. I would also like to see empty_like and others like it. |
Description
Add convenience functions to pybind11::array to return the shape and strides as a std::span. These functions are hidden with macros unless PYBIND11_CPP20 is defined and the include has been found. By returning an iterable container, a new array could be created more simply with
array(other.dtype(), other.size_span(), other.strides_span()).Suggested changelog entry:
Add convenience functions to pybind11::array to return the shape and strides as a std::span.