8000 gh-122400: Handle ValueError in filecmp (GH-122401) · python/cpython@3a9b2aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 3a9b2aa

Browse files
authored
gh-122400: Handle ValueError in filecmp (GH-122401)
1 parent 3833d27 commit 3a9b2aa

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

Lib/filecmp.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ def phase2(self): # Distinguish files, directories, funnies
164164
ok = True
165165
try:
166166
a_stat = os.stat(a_path)
167-
except OSError:
167+
except (OSError, ValueError):
168+
# See https://github.com/python/cpython/issues/122400
169+
# for the rationale for protecting against ValueError.
168170
# print('Can\'t stat', a_path, ':', why.args[1])
169171
ok = False
170172
try:
171173
b_stat = os.stat(b_path)
172-
except OSError:
174+
except (OSError, ValueError):
173175
# print('Can\'t stat', b_path, ':', why.args[1])
174176
ok = False
175177

@@ -285,12 +287,12 @@ def cmpfiles(a, b, common, shallow=True):
285287
# Return:
286288
# 0 for equal
287289
# 1 for different
288-
# 2 for funny cases (can't stat, etc.)
290+
# 2 for funny cases (can't stat, NUL bytes, etc.)
289291
#
290292
def _cmp(a, b, sh, abs=abs, cmp=cmp):
291293
try:
292294
return not abs(cmp(a, b, sh))
293-
except OSError:
295+
except (OSError, ValueError):
294296
return 2
295297

296298

Lib/test/test_filecmp.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,39 @@ def test_cmpfiles(self):
156156
(['file'], ['file2'], []),
157157
"Comparing mismatched directories fails")
158158

159+
def test_cmpfiles_invalid_names(self):
160+
# See https://github.com/python/cpython/issues/122400.
161+
for file, desc in [
162+
('\x00', 'NUL bytes filename'),
163+
(__file__ + '\x00', 'filename with embedded NUL bytes'),
164+
("\uD834\uDD1E.py", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
165+
('a' * 1_000_000, 'very long filename'),
166+
]:
167+
for other_dir in [self.dir, self.dir_same, self.dir_diff]:
168+
with self.subTest(f'cmpfiles: {desc}', other_dir=other_dir):
169+
res = filecmp.cmpfiles(self.dir, other_dir, [file])
170+
self.assertTupleEqual(res, ([], [], [file]))
171+
172+
def test_dircmp_invalid_names(self):
173+
for bad_dir, desc in [
174+
('\x00', 'NUL bytes dirname'),
175+
(f'Top{os.sep}Mid\x00', 'dirname with embedded NUL bytes'),
176+
("\uD834\uDD1E", 'surrogate codes (MUSICAL SYMBOL G CLEF)'),
177+
('a' * 1_000_000, 'very long dirname'),
178+
]:
179+
d1 = filecmp.dircmp(self.dir, bad_dir)
180+
d2 = filecmp.dircmp(bad_dir, self.dir)
181+
for target in [
182+
# attributes where os.listdir() raises OSError or ValueError
183+
'left_list', 'right_list',
184+
'left_only', 'right_only', 'common',
185+
]:
186+
with self.subTest(f'dircmp(ok, bad): {desc}', target=target):
187+
with self.assertRaises((OSError, ValueError)):
188+
getattr(d1, target)
189+
with self.subTest(f'dircmp(bad, ok): {desc}', target=target):
190+
with self.assertRaises((OSError, ValueError)):
191+
getattr(d2, target)
159192

160193
def _assert_lists(self, actual, expected):
161194
"""Assert that two lists are equal, up to ordering."""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Handle :exc:`ValueError`\s raised by :func:`os.stat` in
2+
:class:`filecmp.dircmp` and :func:`filecmp.cmpfiles`.
3+
Patch by Bénédikt Tran.

0 commit comments

Comments
 (0)
0