Skip to content

Add PyMemoryView::from_owned_buffer for zero-copy memoryview creation#5937

Open
alex wants to merge 1 commit intoPyO3:mainfrom
alex:claude/implement-pyo3-api-5871-kzVaG
Open

Add PyMemoryView::from_owned_buffer for zero-copy memoryview creation#5937
alex wants to merge 1 commit intoPyO3:mainfrom
alex:claude/implement-pyo3-api-5871-kzVaG

Conversation

@alex
Copy link
Copy Markdown
Member

@alex alex commented Apr 2, 2026

Adds a new method to create a Python memoryview that exposes a read-only view of byte data owned by a frozen PyClass instance without copying. This is useful for libraries like pyca/cryptography that need to expose internal buffers efficiently.

The method uses PyBuffer_FillInfo + PyMemoryView_FromBuffer to create a memoryview backed by the owner's data, with the owner kept alive via the buffer's obj reference.

Safety is enforced at compile time:

  • T: PyClass<Frozen = True> prevents mutation that could invalidate pointers
  • for<'a> FnOnce(&'a T) -> &'a [u8] ensures the slice borrows from T or is 'static

Closes #5871

https://claude.ai/code/session_01EEP1DaqJwHGCoNufi2JT9H

@alex alex force-pushed the claude/implement-pyo3-api-5871-kzVaG branch 2 times, most recently from ac672ac to 800c7dc Compare April 2, 2026 13:05
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Apr 2, 2026

Merging this PR will not alter performance

✅ 105 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing alex:claude/implement-pyo3-api-5871-kzVaG (7f86fc8) with main (df36c71)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports.

@alex alex force-pushed the claude/implement-pyo3-api-5871-kzVaG branch 2 times, most recently from 710f6b2 to 2353d5d Compare April 2, 2026 23:12
@alex
Copy link
Copy Markdown
Member Author

alex commented Apr 2, 2026

netlify stuff looks unrelated?

@davidhewitt
Copy link
Copy Markdown
Member

Indeed, hopefully #5939 will resolve that.

@davidhewitt
Copy link
Copy Markdown
Member

#5941 might make the call_method_one_arg benchmark more stable...

@alex alex force-pushed the claude/implement-pyo3-api-5871-kzVaG branch from 2353d5d to fba259b Compare April 3, 2026 12:27
@alex
Copy link
Copy Markdown
Member Author

alex commented Apr 4, 2026

No luck with netlify...

@davidhewitt
Copy link
Copy Markdown
Member

#5945 has finally got things green, one more rebase / merge should fix.

@alex alex force-pushed the claude/implement-pyo3-api-5871-kzVaG branch from fba259b to 4de5647 Compare April 4, 2026 12:13
Adds a new method to create a Python memoryview that exposes a read-only
view of byte data owned by a frozen PyClass instance without copying.
This is useful for libraries like pyca/cryptography that need to expose
internal buffers efficiently.

The method uses PyBuffer_FillInfo + PyMemoryView_FromBuffer to create a
memoryview backed by the owner's data, with the owner kept alive via
the buffer's obj reference.

Safety is enforced at compile time:
- T: PyClass<Frozen = True> prevents mutation that could invalidate pointers
- for<'a> FnOnce(&'a T) -> &'a [u8] ensures the slice borrows from T or is 'static

Closes PyO3#5871

https://claude.ai/code/session_01EEP1DaqJwHGCoNufi2JT9H
@alex alex force-pushed the claude/implement-pyo3-api-5871-kzVaG branch from 4de5647 to 7f86fc8 Compare April 4, 2026 18:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API for returning a memoryview pointing at memory in a Py<T>

3 participants