[3.12] gh-122400: Handle ValueError in filecmp (GH-122401) (GH-122442)

(cherry picked from commit 3a9b2aae61)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Miss Islington (bot) 2024-07-30 11:07:06 +02:00 committed by GitHub
parent 6b4abdee91
commit 9491d18975
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 42 additions and 4 deletions

View file

@ -111,6 +111,39 @@ class DirCompareTestCase(unittest.TestCase):
(['file'], ['file2'], []),
"Comparing mismatched directories fails")
def test_cmpfiles_invalid_names(self):
# See https://github.com/python/cpython/issues/122400.
for file, desc in [
('\x00', 'NUL bytes filename'),
(__file__ + '\x00', 'filename with embedded NUL bytes'),
("\uD834\uDD1E.py", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
('a' * 1_000_000, 'very long filename'),
]:
for other_dir in [self.dir, self.dir_same, self.dir_diff]:
with self.subTest(f'cmpfiles: {desc}', other_dir=other_dir):
res = filecmp.cmpfiles(self.dir, other_dir, [file])
self.assertTupleEqual(res, ([], [], [file]))
def test_dircmp_invalid_names(self):
for bad_dir, desc in [
('\x00', 'NUL bytes dirname'),
(f'Top{os.sep}Mid\x00', 'dirname with embedded NUL bytes'),
("\uD834\uDD1E", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
('a' * 1_000_000, 'very long dirname'),
]:
d1 = filecmp.dircmp(self.dir, bad_dir)
d2 = filecmp.dircmp(bad_dir, self.dir)
for target in [
# attributes where os.listdir() raises OSError or ValueError
'left_list', 'right_list',
'left_only', 'right_only', 'common',
]:
with self.subTest(f'dircmp(ok, bad): {desc}', target=target):
with self.assertRaises((OSError, ValueError)):
getattr(d1, target)
with self.subTest(f'dircmp(bad, ok): {desc}', target=target):
with self.assertRaises((OSError, ValueError)):
getattr(d2, target)
def _assert_lists(self, actual, expected):
"""Assert that two lists are equal, up to ordering."""