Skip to content

Commit 154691f

Browse files
committed
implement restore_stdout
1 parent fdc2e16 commit 154691f

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/extensions/pdffit2module/bindings.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ struct PyMethodDef pypdffit2_methods[] = {
330330
{pypdffit2_redirect_stdout__name__, pypdffit2_redirect_stdout,
331331
METH_VARARGS, pypdffit2_redirect_stdout__doc__},
332332

333+
//restore_stdout
334+
{pypdffit2_restore_stdout__name__, pypdffit2_restore_stdout,
335+
METH_VARARGS, pypdffit2_restore_stdout__doc__},
336+
333337
//is_element
334338
{pypdffit2_is_element__name__, pypdffit2_is_element,
335339
METH_VARARGS, pypdffit2_is_element__doc__},

src/extensions/pdffit2module/misc.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,6 +2201,36 @@ PyObject * pypdffit2_redirect_stdout(PyObject *, PyObject *args)
22012201
return Py_None;
22022202
}
22032203

2204+
// restore_stdout
2205+
char pypdffit2_restore_stdout__doc__[] =
2206+
"Restore engine output to the default stream (std::cout).";
2207+
char pypdffit2_restore_stdout__name__[] =
2208+
"restore_stdout";
2209+
2210+
PyObject * pypdffit2_restore_stdout(PyObject *, PyObject *args)
2211+
{
2212+
// no arguments.
2213+
if (!PyArg_ParseTuple(args, ""))
2214+
return 0;
2215+
2216+
// If the global output stream pointer is not std::cout, then delete the custom stream.
2217+
if (NS_PDFFIT2::pout != &std::cout)
2218+
{
2219+
delete NS_PDFFIT2::pout;
2220+
NS_PDFFIT2::pout = &std::cout;
2221+
}
2222+
2223+
// Clean up the custom stream buffer
2224+
if (py_stdout_streambuf)
2225+
{
2226+
delete py_stdout_streambuf;
2227+
py_stdout_streambuf = nullptr;
2228+
}
2229+
2230+
Py_INCREF(Py_None);
2231+
return Py_None;
2232+
}
2233+
22042234
// is_element
22052235
char pypdffit2_is_element__doc__[] = "Check if element or isotope is defined in the built-in periodic table.";
22062236
char pypdffit2_is_element__name__[] = "is_element";

src/extensions/pdffit2module/misc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,12 @@ extern char pypdffit2_redirect_stdout__name__[];
477477
extern "C"
478478
PyObject * pypdffit2_redirect_stdout(PyObject *, PyObject *);
479479

480+
// restore_stdout
481+
extern char pypdffit2_restore_stdout__doc__[];
482+
extern char pypdffit2_restore_stdout__name__[];
483+
extern "C"
484+
PyObject * pypdffit2_restore_stdout(PyObject *, PyObject *);
485+
480486
// is_element
481487
extern char pypdffit2_is_element__doc__[];
482488
extern char pypdffit2_is_element__name__[];

0 commit comments

Comments
 (0)