ENH: Implement PEP 688 buffer protocol for ITK Python#5673
ENH: Implement PEP 688 buffer protocol for ITK Python#5673
Conversation
Co-authored-by: thewtex <25432+thewtex@users.noreply.github.com>
Co-authored-by: thewtex <25432+thewtex@users.noreply.github.com>
Co-authored-by: thewtex <25432+thewtex@users.noreply.github.com>
Co-authored-by: thewtex <25432+thewtex@users.noreply.github.com>
…king Co-authored-by: thewtex <25432+thewtex@users.noreply.github.com>
Co-authored-by: thewtex <25432+thewtex@users.noreply.github.com>
|
|
||
| # Get the memoryview from PyBuffer using the existing C++ method | ||
| # This returns a 1-D memoryview of the raw buffer | ||
| raw_memview = PyBufferType._GetArrayViewFromImage(self) |
There was a problem hiding this comment.
The original PR, got a buffer from the import image container. This added a daisy chained reference to the object which actually holds the C buffer. This may help keep the Python buffer valid in some cases.
If this is not used, then perhaps the ImportImageContainer buffer interface should be removed. As I recall it was not properly wrapped for complex/composite pixel types.
Status Update and Analysis (2026-04-07)Current StateThis PR (copilot-generated, based on @blowekamp's #5665) implements PEP 688 buffer protocol support for ITK Python. It has been open since 2025-12-05 with no human review activity. The upstream `main` branch has no PEP 688 support — `buffer` does not exist anywhere in the codebase today. Relationship to Other PRs
Review of @blowekamp's Feedbackblowekamp commented on this PR:
This is a valid concern about memory lifetime safety. The key difference:
SimpleITK's implementation (SimpleITK#2447) solved this properly by creating a CPython ImageBuffer class that maintains a reference to the Python Image instance, preventing garbage collection of the underlying image while buffers are active. Key Issues in This PR
PEP 688 Requirements (Python 3.12+)Per PEP 688:
Recommendations
|
|
Summary of recommendation: some human should implement this 😄 |
|
Superseded by #6020 which addresses the memory safety concerns raised here. The new implementation uses existing |
Description
Implements PEP 688 buffer protocol support for ITK Images and ImportImageContainers, enabling zero-copy NumPy interoperability in Python 3.12+. Based on #5665 by @blowekamp with fixes for test failures and implementation refinements.
Key changes:
PyBuffer::_GetMemoryViewFromImportImageContainer()with overflow checking__buffer__()methods: Implemented forImageandImportImageContainerclassesPyBuffer._GetArrayViewFromImage()_get_formatstring()for robustnessImportImageContainer, CMake macro application__array__()uses buffer protocol for Python 3.12+, falls back toarray_from_image()otherwiseUsage:
PR Checklist
Related:
Co-authored-by: Bradley Lowekamp blowekamp@users.noreply.github.com
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.