Ill-formed condition check
There is a line:
if (bout := buff.getvalue().strip("\r\n")) and bout != out:
that may fail as out
is not necessarily defined properly (if an exception was thrown and caught for instance).
As I am not very clear about the rationale of the second part of the condition I prefer let the author fix it himself