Skip to content

Commit 2fb631e

Browse files
committed
fix buffer
1 parent 88b6f92 commit 2fb631e

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/extensions/pdffit2module/PyFileStreambuf.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,21 @@ class PyFileStreambuf : public std::streambuf
6767

6868
virtual int_type overflow( int_type c)
6969
{
70-
PyObject* rv;
71-
rv = PyObject_CallMethod(py_file, "write", "(s#)", &c, 1);
70+
char ch = static_cast<char>(c);
71+
PyObject* py_str = PyUnicode_FromStringAndSize(&ch, 1);
72+
if (!py_str) { return traits_type::eof(); }
73+
PyObject* rv = PyObject_CallMethod(py_file, "write", "O", py_str);
74+
Py_DECREF(py_str);
7275
if (rv) { Py_DECREF(rv); }
7376
return c;
7477
}
7578

7679
virtual std::streamsize xsputn(const char_type* s, std::streamsize n)
7780
{
78-
PyObject* rv;
79-
rv = PyObject_CallMethod(py_file, "write", "(s#)", s, n);
81+
PyObject* py_str = PyUnicode_DecodeUTF8(s, n, "replace");
82+
if (!py_str) { return 0; }
83+
PyObject* rv = PyObject_CallMethod(py_file, "write", "O", py_str);
84+
Py_DECREF(py_str);
8085
if (rv) { Py_DECREF(rv); }
8186
return n;
8287
}

src/extensions/pdffit2module/misc.cc

+4-5
Original file line numberDiff line numberDiff line change
@@ -2176,15 +2176,14 @@ char pypdffit2_redirect_stdout__name__[] = "redirect_stdout";
21762176
PyObject * pypdffit2_redirect_stdout(PyObject *, PyObject *args)
21772177
{
21782178
// instance of PyFileStreambuf which takes care of redirection
2179-
PyObject *py_file = 0;
2180-
int ok = PyArg_ParseTuple(args, "O", &py_file);
2181-
if (!ok) return 0;
2179+
PyObject *py_file = nullptr;
2180+
if (!PyArg_ParseTuple(args, "O", &py_file)) return nullptr;
21822181
// check if py_file has write and flush attributes
21832182
if ( !PyObject_HasAttrString(py_file, "write") ||
21842183
!PyObject_HasAttrString(py_file, "flush") )
21852184
{
21862185
PyErr_SetString(PyExc_TypeError, "expected file-like argument");
2187-
return 0;
2186+
return nullptr;
21882187
}
21892188
// create py_stdout_streambuf if necessary
21902189
if (!py_stdout_streambuf)
@@ -2195,7 +2194,7 @@ PyObject * pypdffit2_redirect_stdout(PyObject *, PyObject *args)
21952194
// on first redirection we need to assign new ostream to NS_PDFFIT2::pout
21962195
if (NS_PDFFIT2::pout == &std::cout)
21972196
{
2198-
NS_PDFFIT2::pout = new ostream(py_stdout_streambuf);
2197+
NS_PDFFIT2::pout = new std::ostream(py_stdout_streambuf);
21992198
}
22002199
Py_INCREF(Py_None);
22012200
return Py_None;

0 commit comments

Comments
 (0)