summaryrefslogtreecommitdiffstats
path: root/Lib/test/support
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-09-24 08:01:37 (GMT)
committerGitHub <noreply@github.com>2024-09-24 08:01:37 (GMT)
commite2f710792b0418b8ca1ca3b8cdf39588c7268495 (patch)
tree5fa743b6be184b1384cea36bc58e591b4568f52e /Lib/test/support
parent3c83f9958c14cd62ad8951c53536f7788745b0ba (diff)
downloadcpython-e2f710792b0418b8ca1ca3b8cdf39588c7268495.zip
cpython-e2f710792b0418b8ca1ca3b8cdf39588c7268495.tar.gz
cpython-e2f710792b0418b8ca1ca3b8cdf39588c7268495.tar.bz2
gh-124188: Fix PyErr_ProgramTextObject() (GH-124189)
* Detect source file encoding. * Use the "replace" error handler even for UTF-8 (default) encoding. * Remove the BOM. * Fix detection of too long lines if they contain NUL. * Return the head rather than the tail for truncated long lines.
Diffstat (limited to 'Lib/test/support')
-rw-r--r--Lib/test/support/script_helper.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/Lib/test/support/script_helper.py b/Lib/test/support/script_helper.py
index d0be317..46ce950 100644
--- a/Lib/test/support/script_helper.py
+++ b/Lib/test/support/script_helper.py
@@ -234,9 +234,13 @@ def make_script(script_dir, script_basename, source, omit_suffix=False):
if not omit_suffix:
script_filename += os.extsep + 'py'
script_name = os.path.join(script_dir, script_filename)
- # The script should be encoded to UTF-8, the default string encoding
- with open(script_name, 'w', encoding='utf-8') as script_file:
- script_file.write(source)
+ if isinstance(source, str):
+ # The script should be encoded to UTF-8, the default string encoding
+ with open(script_name, 'w', encoding='utf-8') as script_file:
+ script_file.write(source)
+ else:
+ with open(script_name, 'wb') as script_file:
+ script_file.write(source)
importlib.invalidate_caches()
return script_name