wxPython Assertion Failures and output redirection

Hi, i have a following question.

I’m redirecting a stdout output on the start of the app in order to store the debug information.
As well, I set AssertMode to wx.APP_ASSERT_DIALOG.

The code looks as follows:

        current_date = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
        stdout_filename = f"{stdout_filename_prefix}_{current_date}.out"
        print(f"Storing stdout to {stdout_filename}")
        app = wx.App(redirect=True, filename=stdout_filename)
        app.SetAssertMode(wx.APP_ASSERT_DIALOG)

However, in the case, when assertion failure happens, if one says to stop app the all stdout output is lost (and in cases, which I have seen, if one tries to press continue, then assertion failure happens again, and after several time one is forced to select stop option).

In this case, unfortunately, the stdout output is lost (and in such a way, one is losing the debug information).

Is there a way to preserve both stdout information and information about assertion failures?

The assertions are coming from C++, typically several layers deeper than the wrapper code. If you use the stop option on the assertion dialog then the process will exit immediately, with an abort() call if I remember correctly, so it’s probable that any buffered output waiting in Python output files will not get flushed.

You might have some luck with overriding wx.App.RedirectStdio and changing it to use an unbuffered file, or perhaps some other file-like object that flushes after every write. You will probably still not be able to get the final traceback triggered by the assert however, as that would not happen until control returns from the C++ to Python, and if there was an abort that will never happen.

Hi, Robin.

Thank you very much for the clarification.