Mailing List Archive

gh-116608: Ignore UTF-16 BOM in importlib.resources._functional tests (GH-117569)
https://github.com/python/cpython/commit/4d4a6f1b6aea6dae131ac116f1735a38c3e32cd1
commit: 4d4a6f1b6aea6dae131ac116f1735a38c3e32cd1
branch: main
author: Petr Viktorin <encukou@gmail.com>
committer: encukou <encukou@gmail.com>
date: 2024-04-05T17:00:29+02:00
summary:

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.

files:
M Lib/test/test_importlib/resources/test_functional.py

diff --git a/Lib/test/test_importlib/resources/test_functional.py b/Lib/test/test_importlib/resources/test_functional.py
index fd02fc7c0e7b15..d60a2bedd89798 100644
--- a/Lib/test/test_importlib/resources/test_functional.py
+++ b/Lib/test/test_importlib/resources/test_functional.py
@@ -32,6 +32,12 @@ def _gen_resourcetxt_path_parts(self):
with self.subTest(path_parts=path_parts):
yield path_parts

+ def assertEndsWith(self, string, suffix):
+ """Assert that `string` ends with `suffix`.
+
+ Used to ignore an architecture-specific UTF-16 byte-order mark."""
+ self.assertEqual(string[-len(suffix):], suffix)
+
def test_read_text(self):
self.assertEqual(
resources.read_text(self.anchor01, 'utf-8.file'),
@@ -65,12 +71,12 @@ def test_read_text(self):
),
'\x00\x01\x02\x03',
)
- self.assertEqual(
+ self.assertEndsWith( # ignore the BOM
resources.read_text(
self.anchor01, 'utf-16.file',
errors='backslashreplace',
),
- 'Hello, UTF-16 world!\n'.encode('utf-16').decode(
+ 'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
errors='backslashreplace',
),
)
@@ -112,9 +118,9 @@ def test_open_text(self):
self.anchor01, 'utf-16.file',
errors='backslashreplace',
) as f:
- self.assertEqual(
+ self.assertEndsWith( # ignore the BOM
f.read(),
- 'Hello, UTF-16 world!\n'.encode('utf-16').decode(
+ 'Hello, UTF-16 world!\n'.encode('utf-16-le').decode(
errors='backslashreplace',
),
)

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-leave@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: list-python-checkins@lists.gossamer-threads.com