Skip to content

[cppyy] Add std::span converter with Python buffer support#21650

Merged
guitargeek merged 2 commits intoroot-project:masterfrom
guitargeek:std_span_converter
Mar 23, 2026
Merged

[cppyy] Add std::span converter with Python buffer support#21650
guitargeek merged 2 commits intoroot-project:masterfrom
guitargeek:std_span_converter

Conversation

@guitargeek
Copy link
Contributor

@guitargeek guitargeek commented Mar 20, 2026

Add a dedicated converter for std::span (C++20) to enable passing Python buffer-compatible objects to C++ functions expecting std::span<T>.

The new StdSpanConverter supports:

  • Python buffer protocol (e.g. NumPy arrays, array.array)
  • Existing C++ objects (passed through as-is)
  • Automatic conversion from other C++ classes like std::vector
  • Both std::span<T> and std::span<const T>

A typecode mapping utility is introduced to validate buffer formats against the expected C++ value type. Incompatible buffers now raise a descriptive TypeError indicating the expected and actual formats.

A new unit test to validate these conversions is also implemented.

This addresses an item in the ROOT plan of work 2026 (https://cern.ch/root-pow).

@guitargeek guitargeek self-assigned this Mar 20, 2026
@guitargeek guitargeek requested a review from aaronj0 March 20, 2026 16:21
@guitargeek guitargeek force-pushed the std_span_converter branch 2 times, most recently from cbaacba to c756a0d Compare March 20, 2026 16:57
@guitargeek guitargeek marked this pull request as ready for review March 20, 2026 19:46
@guitargeek guitargeek requested a review from vepadulano as a code owner March 20, 2026 19:46
@github-actions
Copy link

github-actions bot commented Mar 20, 2026

Test Results

    22 files      22 suites   3d 4h 27m 38s ⏱️
 3 835 tests  3 833 ✅ 1 💤 1 ❌
76 598 runs  76 588 ✅ 9 💤 1 ❌

For more details on these failures, see this check.

Results for commit 8cd7282.

♻️ This comment has been updated with latest results.

Copy link
Contributor

@aaronj0 aaronj0 left a comment

Choose a reason for hiding this comment

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

Few comments, but LGTM!

The `PyBuffer_Release` function has its current interface since Python
3.1, which is long end-of-life, so we don't need to be backwards
compatible for that. This also makes the code easier to understand
because no unused arguments are passed around.
@guitargeek guitargeek force-pushed the std_span_converter branch 4 times, most recently from 3dd5d77 to f4221c0 Compare March 22, 2026 12:31
Add a dedicated converter for `std::span` (C++20) to enable passing Python
buffer-compatible objects to C++ functions expecting `std::span<T>`.

The new StdSpanConverter supports:
- Python buffer protocol (e.g. NumPy arrays, array.array)
- Existing C++ objects (passed through as-is)
- Automatic conversion from other C++ classes like `std::vector`
- Both `std::span<T>` and `std::span<const T>`

A typecode mapping utility is introduced to validate buffer formats
against the expected C++ value type. Incompatible buffers now raise a
descriptive TypeError indicating the expected and actual formats.

A new unit test to validate these conversions is also implemented.
@guitargeek guitargeek merged commit 170ae7b into root-project:master Mar 23, 2026
26 of 30 checks passed
@guitargeek guitargeek deleted the std_span_converter branch March 23, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants