Fix pytest capture import in TTY session

Guard stream reconfiguration so pytest capture objects without reconfigure support can import the code execution TTY helper while preserving UTF-8 error handling for normal streams.
This commit is contained in:
Alessandro 2026-06-24 11:13:42 +02:00
parent 528c33b7ef
commit a18a35977a

View file

@ -6,9 +6,16 @@ if _IS_WIN:
import msvcrt
def _reconfigure_stream_errors(stream) -> None:
reconfigure = getattr(stream, "reconfigure", None)
if not callable(reconfigure):
return
reconfigure(errors="replace")
# Make stdin / stdout tolerant to broken UTF-8 so input() never aborts
sys.stdin.reconfigure(errors="replace") # type: ignore
sys.stdout.reconfigure(errors="replace") # type: ignore
_reconfigure_stream_errors(sys.stdin)
_reconfigure_stream_errors(sys.stdout)
# ──────────────────────────── PUBLIC CLASS ────────────────────────────