@@ -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."""
0 commit comments