Skip to content

Add optional buffer size to pythonbuf::d_buffer constructor #1687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2019
Merged

Add optional buffer size to pythonbuf::d_buffer constructor #1687

merged 1 commit into from
Jun 11, 2019

Conversation

ohm314
Copy link
Contributor

@ohm314 ohm314 commented Feb 8, 2019

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).

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).
@wjakob
Copy link
Member

wjakob commented Jun 11, 2019

LGTM.

@wjakob wjakob merged commit e8e9726 into pybind:master Jun 11, 2019
wjakob pushed a commit that referenced this pull request Jun 11, 2019
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).
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.

2 participants