gh-133677: Fix tests when running in non-UTF-8 locale (GH-133865)

This commit is contained in:
Serhiy Storchaka 2025-05-12 19:09:11 +03:00 committed by GitHub
parent d1533115ba
commit 14305a83d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 16 additions and 12 deletions

View file

@ -38,7 +38,7 @@ class StraceResult:
This assumes the program under inspection doesn't print any non-utf8 This assumes the program under inspection doesn't print any non-utf8
strings which would mix into the strace output.""" strings which would mix into the strace output."""
decoded_events = self.event_bytes.decode('utf-8') decoded_events = self.event_bytes.decode('utf-8', 'surrogateescape')
matches = [ matches = [
_syscall_regex.match(event) _syscall_regex.match(event)
for event in decoded_events.splitlines() for event in decoded_events.splitlines()

View file

@ -6973,7 +6973,7 @@ class TestProgName(TestCase):
def check_usage(self, expected, *args, **kwargs): def check_usage(self, expected, *args, **kwargs):
res = script_helper.assert_python_ok('-Xutf8', *args, '-h', **kwargs) res = script_helper.assert_python_ok('-Xutf8', *args, '-h', **kwargs)
self.assertEqual(res.out.splitlines()[0].decode(), self.assertEqual(os.fsdecode(res.out.splitlines()[0]),
f'usage: {expected} [-h]') f'usage: {expected} [-h]')
def test_script(self, compiled=False): def test_script(self, compiled=False):

View file

@ -791,21 +791,21 @@ class TestAsyncioToolsBasic(unittest.TestCase):
class TestAsyncioToolsEdgeCases(unittest.TestCase): class TestAsyncioToolsEdgeCases(unittest.TestCase):
def test_task_awaits_self(self): def test_task_awaits_self(self):
"""A task directly awaits itself should raise a cycle.""" """A task directly awaits itself - should raise a cycle."""
input_ = [(1, [(1, "Self-Awaiter", [[["loopback"], 1]])])] input_ = [(1, [(1, "Self-Awaiter", [[["loopback"], 1]])])]
with self.assertRaises(tools.CycleFoundException) as ctx: with self.assertRaises(tools.CycleFoundException) as ctx:
tools.build_async_tree(input_) tools.build_async_tree(input_)
self.assertIn([1, 1], ctx.exception.cycles) self.assertIn([1, 1], ctx.exception.cycles)
def test_task_with_missing_awaiter_id(self): def test_task_with_missing_awaiter_id(self):
"""Awaiter ID not in task list should not crash, just show 'Unknown'.""" """Awaiter ID not in task list - should not crash, just show 'Unknown'."""
input_ = [(1, [(1, "Task-A", [[["coro"], 999]])])] # 999 not defined input_ = [(1, [(1, "Task-A", [[["coro"], 999]])])] # 999 not defined
table = tools.build_task_table(input_) table = tools.build_task_table(input_)
self.assertEqual(len(table), 1) self.assertEqual(len(table), 1)
self.assertEqual(table[0][4], "Unknown") self.assertEqual(table[0][4], "Unknown")
def test_duplicate_coroutine_frames(self): def test_duplicate_coroutine_frames(self):
"""Same coroutine frame repeated under a parent should deduplicate.""" """Same coroutine frame repeated under a parent - should deduplicate."""
input_ = [ input_ = [
( (
1, 1,
@ -829,7 +829,7 @@ class TestAsyncioToolsEdgeCases(unittest.TestCase):
self.assertIn("Task-1", flat) self.assertIn("Task-1", flat)
def test_task_with_no_name(self): def test_task_with_no_name(self):
"""Task with no name in id2name should still render with fallback.""" """Task with no name in id2name - should still render with fallback."""
input_ = [(1, [(1, "root", [[["f1"], 2]]), (2, None, [])])] input_ = [(1, [(1, "root", [[["f1"], 2]]), (2, None, [])])]
# If name is None, fallback to string should not crash # If name is None, fallback to string should not crash
tree = tools.build_async_tree(input_) tree = tools.build_async_tree(input_)

View file

@ -20,7 +20,7 @@ from test.support import cpython_only
from test.support import is_emscripten, is_wasi from test.support import is_emscripten, is_wasi
from test.support import infinite_recursion from test.support import infinite_recursion
from test.support import os_helper from test.support import os_helper
from test.support.os_helper import TESTFN, FakePath from test.support.os_helper import TESTFN, FS_NONASCII, FakePath
try: try:
import fcntl import fcntl
except ImportError: except ImportError:
@ -770,12 +770,16 @@ class PurePathTest(unittest.TestCase):
self.assertEqual(self.make_uri(P('c:/')), 'file:///c:/') self.assertEqual(self.make_uri(P('c:/')), 'file:///c:/')
self.assertEqual(self.make_uri(P('c:/a/b.c')), 'file:///c:/a/b.c') self.assertEqual(self.make_uri(P('c:/a/b.c')), 'file:///c:/a/b.c')
self.assertEqual(self.make_uri(P('c:/a/b%#c')), 'file:///c:/a/b%25%23c') self.assertEqual(self.make_uri(P('c:/a/b%#c')), 'file:///c:/a/b%25%23c')
self.assertEqual(self.make_uri(P('c:/a/b\xe9')), 'file:///c:/a/b%C3%A9')
self.assertEqual(self.make_uri(P('//some/share/')), 'file://some/share/') self.assertEqual(self.make_uri(P('//some/share/')), 'file://some/share/')
self.assertEqual(self.make_uri(P('//some/share/a/b.c')), self.assertEqual(self.make_uri(P('//some/share/a/b.c')),
'file://some/share/a/b.c') 'file://some/share/a/b.c')
self.assertEqual(self.make_uri(P('//some/share/a/b%#c\xe9')),
'file://some/share/a/b%25%23c%C3%A9') from urllib.parse import quote_from_bytes
QUOTED_FS_NONASCII = quote_from_bytes(os.fsencode(FS_NONASCII))
self.assertEqual(self.make_uri(P('c:/a/b' + FS_NONASCII)),
'file:///c:/a/b' + QUOTED_FS_NONASCII)
self.assertEqual(self.make_uri(P('//some/share/a/b%#c' + FS_NONASCII)),
'file://some/share/a/b%25%23c' + QUOTED_FS_NONASCII)
@needs_windows @needs_windows
def test_ordering_windows(self): def test_ordering_windows(self):

View file

@ -109,7 +109,7 @@ class urlopen_FileTests(unittest.TestCase):
finally: finally:
f.close() f.close()
self.pathname = os_helper.TESTFN self.pathname = os_helper.TESTFN
self.quoted_pathname = urllib.parse.quote(self.pathname) self.quoted_pathname = urllib.parse.quote(os.fsencode(self.pathname))
self.returned_obj = urllib.request.urlopen("file:%s" % self.quoted_pathname) self.returned_obj = urllib.request.urlopen("file:%s" % self.quoted_pathname)
def tearDown(self): def tearDown(self):

View file

@ -3642,7 +3642,7 @@ class EncodedMetadataTests(unittest.TestCase):
except OSError: except OSError:
pass pass
except UnicodeEncodeError: except UnicodeEncodeError:
self.skipTest(f'cannot encode file name {fn!r}') self.skipTest(f'cannot encode file name {fn!a}')
zipfile.main(["--metadata-encoding=shift_jis", "-e", TESTFN, TESTFN2]) zipfile.main(["--metadata-encoding=shift_jis", "-e", TESTFN, TESTFN2])
listing = os.listdir(TESTFN2) listing = os.listdir(TESTFN2)