[3.14] gh-135124: Change stdout errors in regrtest worker process (GH-135138) (#135168)

gh-135124: Change stdout errors in regrtest worker process (GH-135138)

Set sys.stdout encoder error handler to backslashreplace in regrtest
workers to avoid UnicodeEncodeError when printing a traceback
or any other non-encodable character.

Move the code from the Regrtest class to setup_process().

Call setup_process() earlier, before displaying regrtest headers.
(cherry picked from commit 3d396ab759)

Co-authored-by: Victor Stinner <vstinner@python.org>
This commit is contained in:
Miss Islington (bot) 2025-06-05 11:42:38 +02:00 committed by GitHub
parent 07921d4169
commit 285c69ea38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 51 additions and 12 deletions

View file

@ -1,5 +1,6 @@
import faulthandler
import gc
import io
import os
import random
import signal
@ -52,6 +53,14 @@ def setup_process() -> None:
support.record_original_stdout(sys.stdout)
# Set sys.stdout encoder error handler to backslashreplace,
# similar to sys.stderr error handler, to avoid UnicodeEncodeError
# when printing a traceback or any other non-encodable character.
#
# Use an assertion to fix mypy error.
assert isinstance(sys.stdout, io.TextIOWrapper)
sys.stdout.reconfigure(errors="backslashreplace")
# Some times __path__ and __file__ are not absolute (e.g. while running from
# Lib/) and, if we change the CWD to run the tests in a temporary dir, some
# imports might fail. This affects only the modules imported before os.chdir().