From cc50f326738c9d68dd4dcd69d6d3268e89ab3351 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 7 Sep 2022 23:44:29 +0100 Subject: [PATCH] gh-95027: Ensure test runner uses utf-8:surrogateescape for communicating with subprocesses --- Lib/test/libregrtest/main.py | 4 ++++ Lib/test/libregrtest/runtest_mp.py | 4 +++- .../next/Tests/2022-09-07-23-44-06.gh-issue-95027.H5xs9o.rst | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Tests/2022-09-07-23-44-06.gh-issue-95027.H5xs9o.rst diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 655e4d2e56f8f0..e94e5eb86b2d20 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -681,6 +681,10 @@ def main(self, tests=None, **kwargs): self.fix_umask() + # We may have been launched with a certain IO encoding, but we do not + # want children to inherit it, so clear it out now. + os.unsetenv('PYTHONIOENCODING') + if self.ns.cleanup: self.cleanup() sys.exit(0) diff --git a/Lib/test/libregrtest/runtest_mp.py b/Lib/test/libregrtest/runtest_mp.py index a4d3e5c3146a8c..5bab4e580b74c7 100644 --- a/Lib/test/libregrtest/runtest_mp.py +++ b/Lib/test/libregrtest/runtest_mp.py @@ -66,7 +66,9 @@ def run_test_in_subprocess(testname: str, ns: Namespace, tmp_dir: str, stdout_fh '-m', 'test.regrtest', '--worker-args', worker_args] + env = dict(os.environ) + env['PYTHONIOENCODING'] = 'utf-8:surrogateescape' if tmp_dir is not None: env['TMPDIR'] = tmp_dir env['TEMP'] = tmp_dir @@ -270,7 +272,7 @@ def _runtest(self, test_name: str) -> MultiprocessResult: # gh-94026: Write stdout+stderr to a tempfile as workaround for # non-blocking pipes on Emscripten with NodeJS. with tempfile.TemporaryFile( - 'w+', encoding=sys.stdout.encoding + 'w+', encoding=sys.stdout.encoding, errors="surrogateescape", ) as stdout_fh: # gh-93353: Check for leaked temporary files in the parent process, # since the deletion of temporary files can happen late during diff --git a/Misc/NEWS.d/next/Tests/2022-09-07-23-44-06.gh-issue-95027.H5xs9o.rst b/Misc/NEWS.d/next/Tests/2022-09-07-23-44-06.gh-issue-95027.H5xs9o.rst new file mode 100644 index 00000000000000..0453978818153d --- /dev/null +++ b/Misc/NEWS.d/next/Tests/2022-09-07-23-44-06.gh-issue-95027.H5xs9o.rst @@ -0,0 +1,2 @@ +Ensures test runner uses UTF-8 and surrogateescape encoding when +communicating between multiple processes.