8000 gh-116608: Ignore UTF-16 BOM in importlib.resources._functional tests… · python/importlib_resources@8c274e5 · GitHub
[go: up one dir, main page]

Skip to content
10000

Commit 8c274e5

Browse files
encukoujaraco
authored andcommitted
gh-116608: Ignore UTF-16 BOM in importlib.resources._functional tests (GH-117569)
gh-116609: Ignore UTF-16 BOM in importlib.resources._functional tests To test the `errors` argument, we read a UTF-16 file as UTF-8 with "backslashreplace" error handling. However, the utf-16 codec adds an endian-specific byte-order mark, so on big-endian machines the expectation doesn't match the test file (which was saved on a little-endian machine). Use endswith to ignore the BOM.
1 parent 1f4d3f1 commit 8c274e5

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

importlib_resources/tests/test_functional.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ def _gen_resourcetxt_path_parts(self):
3636
with self.subTest(path_parts=path_parts):
3737
yield path_parts
3838

39+
def assertEndsWith(self, string, suffix):
40+
"""Assert that `string` ends with `suffix`.
41+
42+
Used to ignore an architecture-specific UTF-16 byte-order mark."""
43+
self.assertEqual(string[-len(suffix):], suffix)
44+
3945
def test_read_text(self):
4046
self.assertEqual(
4147
resources.read_text(self.anchor01, 'utf-8.file'),
@@ -76,13 +82,13 @@ def test_read_text(self):
7682
),
7783
'\x00\x01\x02\x03',
7884
)
79-
self.assertEqual(
85+
self.assertEndsWith( # ignore the BOM
8086
resources.read_text(
8187
self.anchor01,
8288
'utf-16.file',
8389
errors='backslashreplace',
8490
),
85-
'Hello, UTF-16 world!\n'.encode('utf-16').decode(
91+
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
8692
errors='backslashreplace',
8793
),
8894
)
@@ -128,9 +134,9 @@ def test_open_text(self):
128134
'utf-16.file',
129135
errors='backslashreplace',
130136
) as f:
131-
self.assertEqual(
137+
self.assertEndsWith( # ignore the BOM
132138
f.read(),
133-
'Hello, UTF-16 world!\n'.encode('utf-16').decode(
139+
'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
134140
errors='backslashreplace',
135141
),
136142
)

0 commit comments

Comments
 (0)
0