From b908dd4a09efb8d33fec9516404c808a3332eeeb Mon Sep 17 00:00:00 2001 From: Nour Yosri Date: Wed, 6 Aug 2025 20:07:18 -0700 Subject: [PATCH 1/2] Add gaurds to redirect C++'s stdout/stderr streams to python's stdout/stderr streams --- src/simplex.pybind.h | 7 +++++-- src/tesseract.pybind.cc | 11 +++++++++++ src/tesseract.pybind.h | 10 +++++++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/simplex.pybind.h b/src/simplex.pybind.h index 6dd2c0a..a6c780f 100644 --- a/src/simplex.pybind.h +++ b/src/simplex.pybind.h @@ -15,6 +15,7 @@ #ifndef _SIMPLEX_PYBIND_H #define _SIMPLEX_PYBIND_H +#include #include #include #include @@ -63,7 +64,8 @@ void add_simplex_module(py::module& root) { .def_readwrite("end_time_to_errors", &SimplexDecoder::end_time_to_errors) .def_readonly("low_confidence_flag", &SimplexDecoder::low_confidence_flag) .def("init_ilp", &SimplexDecoder::init_ilp) - .def("decode_to_errors", &SimplexDecoder::decode_to_errors, py::arg("detections")) + .def("decode_to_errors", &SimplexDecoder::decode_to_errors, py::arg("detections"), + py::call_guard()) .def( "get_observables_from_errors", [](SimplexDecoder& self, const std::vector& predicted_errors) { @@ -88,6 +90,7 @@ void add_simplex_module(py::module& root) { } return result; }, - py::arg("detections")); + py::arg("detections"), + py::call_guard()); } #endif diff --git a/src/tesseract.pybind.cc b/src/tesseract.pybind.cc index 4c35029..c47f035 100644 --- a/src/tesseract.pybind.cc +++ b/src/tesseract.pybind.cc @@ -14,6 +14,7 @@ #include "tesseract.pybind.h" +#include #include #include "common.pybind.h" @@ -23,8 +24,18 @@ PYBIND11_MODULE(tesseract_decoder, tesseract) { py::module::import("stim"); + add_common_module(tesseract); add_utils_module(tesseract); add_simplex_module(tesseract); add_tesseract_module(tesseract); + + // Adds a context manager to the python library that can be used to redirect C++'s stdout/stderr + // to python's stdout/stderr at run time like + // with tesseract_decoder.ostream_redirect(stdout=..., stderr=...): + // do_work() + // This is only needed if the C++ function's stdout/stderr which is not redirected to python's + // stdout/stderr using the py::call_guard() statement. + py::add_ostream_redirect(tesseract, "ostream_redirect"); } diff --git a/src/tesseract.pybind.h b/src/tesseract.pybind.h index e33df6e..73d0860 100644 --- a/src/tesseract.pybind.h +++ b/src/tesseract.pybind.h @@ -15,6 +15,7 @@ #ifndef _TESSERACT_PYBIND_H #define _TESSERACT_PYBIND_H +#include #include #include #include @@ -71,11 +72,13 @@ void add_tesseract_module(py::module& root) { .def(py::init(), py::arg("config")) .def("decode_to_errors", py::overload_cast&>(&TesseractDecoder::decode_to_errors), - py::arg("detections")) + py::arg("detections"), + py::call_guard()) .def("decode_to_errors", py::overload_cast&, size_t, size_t>( &TesseractDecoder::decode_to_errors), - py::arg("detections"), py::arg("det_order"), py::arg("det_beam")) + py::arg("detections"), py::arg("det_order"), py::arg("det_beam"), + py::call_guard()) .def( "get_observables_from_errors", [](TesseractDecoder& self, const std::vector& predicted_errors) { @@ -100,7 +103,8 @@ void add_tesseract_module(py::module& root) { } return result; }, - py::arg("detections")) + py::arg("detections"), + py::call_guard()) .def_readwrite("low_confidence_flag", &TesseractDecoder::low_confidence_flag) .def_readwrite("predicted_errors_buffer", &TesseractDecoder::predicted_errors_buffer) .def_readwrite("errors", &TesseractDecoder::errors); From 154ae03ba884d2cb96d14d7926344de36119b880 Mon Sep 17 00:00:00 2001 From: Nour Yosri Date: Wed, 6 Aug 2025 20:16:15 -0700 Subject: [PATCH 2/2] typo --- src/tesseract.pybind.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tesseract.pybind.cc b/src/tesseract.pybind.cc index c47f035..b133285 100644 --- a/src/tesseract.pybind.cc +++ b/src/tesseract.pybind.cc @@ -34,7 +34,7 @@ PYBIND11_MODULE(tesseract_decoder, tesseract) { // to python's stdout/stderr at run time like // with tesseract_decoder.ostream_redirect(stdout=..., stderr=...): // do_work() - // This is only needed if the C++ function's stdout/stderr which is not redirected to python's + // This is only needed if the C++ function's stdout/stderr is not redirected to python's // stdout/stderr using the py::call_guard() statement. py::add_ostream_redirect(tesseract, "ostream_redirect");