| 36 | } |
| 37 | |
| 38 | std::streamsize write(const char* s, std::streamsize n) { |
| 39 | if (len_ == 0) { |
| 40 | *pstr_ = PyBytes_FromStringAndSize(s, n); |
| 41 | if (*pstr_ == nullptr) // no point trying to recover from allocation error |
| 42 | std::terminate(); |
| 43 | len_ = n; |
| 44 | } else { |
| 45 | if (pos_ + n > len_) { |
| 46 | len_ = pos_ + n; |
| 47 | if (_PyBytes_Resize(pstr_, len_) == -1) |
| 48 | std::terminate(); // no point trying to recover from allocation error |
| 49 | } |
| 50 | char* b = PyBytes_AS_STRING(*pstr_); |
| 51 | std::copy(s, s + n, b + pos_); |
| 52 | } |
| 53 | pos_ += n; |
| 54 | return n; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | PyObject** pstr_; |