feat(python): support Buffer initialization and reading from stream-like objects#3410
Closed
ulekarsiddhant0-boop wants to merge 3 commits intoapache:mainfrom
Closed
feat(python): support Buffer initialization and reading from stream-like objects#3410ulekarsiddhant0-boop wants to merge 3 commits intoapache:mainfrom
ulekarsiddhant0-boop wants to merge 3 commits intoapache:mainfrom
Conversation
Collaborator
|
Finished in #3307 |
Author
|
Hi @chaokunyang ,I am currently researching the CI failures and will return with a fix shortly. In the meantime, I am starting work on the [Ruby/Lua] serialization components to broaden the project's scope. Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Currently, the Python Buffer implementation assumes that the input data always supports the len() function. When a user tries to initialize a Buffer with a stream-like object (such as io.BytesIO), it raises a TypeError. This PR enables Buffer to handle stream-like objects and provides a method to read data from them incrementally.
Modifications
Buffer Initialization: Updated Buffer.init in buffer.pyx to check for the existence of a .read() method. If detected, it bypasses the len() check to prevent crashes with stream-like objects.
New Method read_from_stream: Added a Cython method to allow reading a specific number of bytes from a Python stream into the underlying C++ CBuffer.
Memory Safety: Implemented a mechanism to store a reference to the data chunks read from the stream (using self.data_chunk), ensuring the memory remains valid for the lifetime of the Buffer object and preventing dangling pointers in the C++ layer.
Verification
Build: Successfully verified the changes using bazel build //:serialization on Windows.
Compilation: Confirmed that Cython correctly generates the C++ source and that the MSVC compiler handles the CBuffer interface without errors.
Manual Test: Verified that io.BytesIO can now be passed to the Buffer without triggering a TypeError.