Add optional buffer size to pythonbuf::d_buffer constructor #1687
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.
In some cases the user of pythonbuf needs to allocate the internal buffer to a specific size e.g. for performance or to enable synchronous writes to the buffer. By changing
pythonbuf::d_buffer
to be dynamically allocated, we can now enable these use-cases while still providing the default behavior of allocating a 1024 byte internal buffer (through a default parameter).In my case, I am binding a C++ class that writes short strings to a stringstream that is attached on the python side to an io.TextIOWrapper. With the current implementation I was not able to read these strings before either flushing on the C++ side, or destroying the object altogether. In this case setting the internal buffer size to 1, rendering the stream synchronous, I'm able to read out the strings in python at any time (without modifying the C++ class itself).