Skip to content

Commit 95f750a

Browse files
Omar Awilewjakob
Omar Awile
authored andcommitted
Add optional buffer size to pythonbuf::d_buffer constructor (#1687)
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).
1 parent 38f408f commit 95f750a

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

include/pybind11/iostream.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class pythonbuf : public std::streambuf {
2525
private:
2626
using traits_type = std::streambuf::traits_type;
2727

28-
char d_buffer[1024];
28+
const size_t buf_size;
29+
std::unique_ptr<char[]> d_buffer;
2930
object pywrite;
3031
object pyflush;
3132

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

5354
public:
54-
pythonbuf(object pyostream)
55-
: pywrite(pyostream.attr("write")),
55+
56+
pythonbuf(object pyostream, size_t buffer_size = 1024)
57+
: buf_size(buffer_size),
58+
d_buffer(new char[buf_size]),
59+
pywrite(pyostream.attr("write")),
5660
pyflush(pyostream.attr("flush")) {
57-
setp(d_buffer, d_buffer + sizeof(d_buffer) - 1);
61+
setp(d_buffer.get(), d_buffer.get() + buf_size - 1);
5862
}
5963

6064
/// Sync before destroy

0 commit comments

Comments
 (0)