def get_child_details(cls, env_vars): """Retrieves fsencoding and standard stream details from a child process Returns (encoding_details, stderr_lines): - encoding_details: EncodingDetails for eager decoding - stderr_lines: result of calling splitlines() on the stderr output The child is run in isolated mode if the current interpreter supports that. """ result, py_cmd = run_python_until_end("-X", "utf8=0", "-c", cls.CHILD_PROCESS_SCRIPT, __isolated=True, **env_vars) if not result.rc == 0: result.fail(py_cmd) # All subprocess outputs in this test case should be pure ASCII adjusted_output = cls._handle_output_variations(result.out) stdout_lines = adjusted_output.decode("ascii").splitlines() child_encoding_details = dict(cls(*stdout_lines)._asdict()) stderr_lines = result.err.decode("ascii").rstrip().splitlines() return child_encoding_details, stderr_lines
def _set_locale_in_subprocess(locale_name): cmd_fmt = "import locale; print(locale.setlocale(locale.LC_CTYPE, '{}'))" if _check_nl_langinfo_CODESET: # If there's no valid CODESET, we expect coercion to be skipped cmd_fmt += "; import sys; sys.exit(not locale.nl_langinfo(locale.CODESET))" cmd = cmd_fmt.format(locale_name) result, py_cmd = run_python_until_end("-c", cmd, __isolated=True) return result.rc == 0
def _set_locale_in_subprocess(locale_name): cmd_fmt = "import locale; print(locale.setlocale(locale.LC_CTYPE, '{}'))" if _check_nl_langinfo_CODESET: # If there's no valid CODESET, we expect coercion to be skipped cmd_fmt += "; import sys; sys.exit(not locale.nl_langinfo(locale.CODESET))" cmd = cmd_fmt.format(locale_name) result, py_cmd = run_python_until_end("-c", cmd, PYTHONCOERCECLOCALE='') return result.rc == 0
def test_yet_more_evil_still_undecodable(self): src = b'#\x00\n#\xfd\n' with tempfile.TemporaryDirectory() as tmpd: fn = os.path.join(tmpd, 'bad.py') with open(fn, 'wb') as fp: fp.write(src) res = script_helper.run_python_until_end(fn)[0] self.assertIn(b'Non-UTF-8', res.err)
def test_yet_more_evil_still_undecodable(self): # Issue #25388 src = b"#\x00\n#\xfd\n" with tempfile.TemporaryDirectory() as tmpd: fn = os.path.join(tmpd, "bad.py") with open(fn, "wb") as fp: fp.write(src) res = script_helper.run_python_until_end(fn)[0] self.assertIn(b"Non-UTF-8", res.err)
def test_particularly_evil_undecodable(self): # Issue 24022 src = b'0000\x00\n00000000000\n\x00\n\x9e\n' with tempfile.TemporaryDirectory() as tmpd: fn = os.path.join(tmpd, "bad.py") with open(fn, "wb") as fp: fp.write(src) res = script_helper.run_python_until_end(fn)[0] self.assertIn(b"Non-UTF-8", res.err)
def test_particularly_evil_undecodable(self): # Issue 24022 src = b'0000\x00\n00000000000\n\x00\n\x9e\n' with tempfile.TemporaryDirectory() as tmpd: fn = os.path.join(tmpd, "bad.py") with open(fn, "wb") as fp: fp.write(src) res = script_helper.run_python_until_end(fn)[0] # PyPy change: we have a different error here self.assertIn(b"SyntaxError", res.err)
def python_run(self, m, lazy=True, warmup=False): args = [] if lazy: args += [ "-X", "lazyimportsall", ] if warmup: args += [ "-X", "lazyimportswarmup", ] args += [ "-m", m, ] res, cmd_line = run_python_until_end(*args) return ( res.rc, res.out.decode("ascii", "replace").rstrip(), res.err.decode("ascii", "replace").rstrip(), )
def get_child_details(cls, env_vars): """Retrieves fsencoding and standard stream details from a child process Returns (encoding_details, stderr_lines): - encoding_details: EncodingDetails for eager decoding - stderr_lines: result of calling splitlines() on the stderr output The child is run in isolated mode if the current interpreter supports that. """ result, py_cmd = run_python_until_end( "-X", "utf8=0", "-c", cls.CHILD_PROCESS_SCRIPT, **env_vars ) if not result.rc == 0: result.fail(py_cmd) # All subprocess outputs in this test case should be pure ASCII adjusted_output = cls._handle_output_variations(result.out) stdout_lines = adjusted_output.decode("ascii").splitlines() child_encoding_details = dict(cls(*stdout_lines)._asdict()) stderr_lines = result.err.decode("ascii").rstrip().splitlines() return child_encoding_details, stderr_lines
def _set_locale_in_subprocess(locale_name): cmd_fmt = "import locale; print(locale.setlocale(locale.LC_CTYPE, '{}'))" cmd = cmd_fmt.format(locale_name) result, py_cmd = run_python_until_end("-c", cmd, __isolated=True) return result.rc == 0