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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions include/pybind11/iostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class pythonbuf : public std::streambuf {
private:
using traits_type = std::streambuf::traits_type;

char d_buffer[1024];
const size_t buf_size;
std::unique_ptr<char[]> d_buffer;
object pywrite;
object pyflush;

Expand All @@ -51,10 +52,13 @@ class pythonbuf : public std::streambuf {
}

public:
pythonbuf(object pyostream)
: pywrite(pyostream.attr("write")),

pythonbuf(object pyostream, size_t buffer_size = 1024)
: buf_size(buffer_size),
d_buffer(new char[buf_size]),
pywrite(pyostream.attr("write")),
pyflush(pyostream.attr("flush")) {
setp(d_buffer, d_buffer + sizeof(d_buffer) - 1);
setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
}

/// Sync before destroy
Expand Down