Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/pybind11/detail/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@
# define PYBIND11_HAS_STRING_VIEW 1
#endif

#if defined(PYBIND11_CPP20) && defined(__has_include) && __has_include(<span>)
# define PYBIND11_HAS_SPAN 1
#endif

#if (defined(PYPY_VERSION) || defined(GRAALVM_PYTHON)) && !defined(PYBIND11_SIMPLE_GIL_MANAGEMENT)
# define PYBIND11_SIMPLE_GIL_MANAGEMENT
#endif
Expand Down
18 changes: 18 additions & 0 deletions include/pybind11/numpy.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
#include <utility>
#include <vector>

#ifdef PYBIND11_HAS_SPAN
# include <span>
#endif

#if defined(PYBIND11_NUMPY_1_ONLY)
# error "PYBIND11_NUMPY_1_ONLY is no longer supported (see PR #5595)."
#endif
Expand Down Expand Up @@ -1143,6 +1147,13 @@ class array : public buffer {
/// Dimensions of the array
const ssize_t *shape() const { return detail::array_proxy(m_ptr)->dimensions; }

#ifdef PYBIND11_HAS_SPAN
/// Dimensions of the array as a span
std::span<const ssize_t, std::dynamic_extent> shape_span() const {
return std::span(shape(), static_cast<std::size_t>(ndim()));
}
#endif

/// Dimension along a given axis
ssize_t shape(ssize_t dim) const {
if (dim >= ndim()) {
Expand All @@ -1154,6 +1165,13 @@ class array : public buffer {
/// Strides of the array
const ssize_t *strides() const { return detail::array_proxy(m_ptr)->strides; }

#ifdef PYBIND11_HAS_SPAN
/// Strides of the array as a span
std::span<const ssize_t, std::dynamic_extent> strides_span() const {
return std::span(strides(), static_cast<std::size_t>(ndim()));
}
#endif

/// Stride along a given axis
ssize_t strides(ssize_t dim) const {
if (dim >= ndim()) {
Expand Down
Loading