Skip to content

Conversation

@NoureldinYosri
Copy link
Contributor

@NoureldinYosri NoureldinYosri commented Aug 7, 2025

C++'s stdout/stderr and Python's stdout/stderr streams are two different streams.. normally this is not a problem but colab ignores C++'s streams. To make colab see C++'s streams they need to be redirected to Python's streams

if the statement py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>() is added to a function's definition then its stdout/stderr gets redirected. I do that for the decoding methods for simplex and tesseract.

so statements like this with verbose=True will have their stdout redirected

>> tesseract_dec.decode(...)
len(pq) = 0 num_pq_pushed = 1
num_detectors = 3 max_num_detectors = 65538 cost = 6.811496946171
activated_errors = 
activated_detectors = 19, 20, 25, 
activated_errors = 880, 
activated_detectors = 
Decoding complete. Cost: 6.811496946171 num_pq_pushed = 100

I also added a context manager that can be used to do this from inside python

import tesseract_decoder
with tesseract_decoder.ostream_redirect(stdout=..., stderr=...):
    # since the call happens inside the context manager its streams get captured  
    a_function_whose_stdout_isnot_redirected()

@NoureldinYosri NoureldinYosri requested a review from LalehB August 7, 2025 03:14
@NoureldinYosri NoureldinYosri requested a review from noajshu August 7, 2025 03:18
@NoureldinYosri NoureldinYosri enabled auto-merge (squash) August 7, 2025 04:53
Copy link
Collaborator

@LalehB LalehB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Thank you so much Nour! 👏🏻

@NoureldinYosri NoureldinYosri merged commit 8da6fbe into quantumlib:main Aug 7, 2025
4 checks passed
@NoureldinYosri NoureldinYosri mentioned this pull request Sep 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants