Skip to content

Commit de4fe01

Browse files
Fix tests on Windows.
1 parent 6772f2d commit de4fe01

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

Lib/test/test_ntpath.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,34 @@ def test_realpath_strict(self):
437437
# gh-106242: Embedded nulls should raise OSError (not ValueError)
438438
self.assertRaises(OSError, ntpath.realpath, ABSTFN + "\0spam", strict=True)
439439

440+
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
441+
def test_realpath_embedded_null(self):
442+
realpath = ntpath.realpath
443+
ABSTFN = ntpath.abspath(os_helper.TESTFN)
444+
path = ABSTFN + '\x00'
445+
self.assertEqual(realpath(path, strict=False), path)
446+
self.assertRaises(OSError, realpath, path, strict=True)
447+
path = os.fsencode(ABSTFN) + b'\x00'
448+
self.assertEqual(realpath(path, strict=False), path)
449+
self.assertRaises(OSError, realpath, path, strict=True)
450+
path = ABSTFN + '\\nonexistent\\x\x00'
451+
self.assertEqual(realpath(path, strict=False), path)
452+
self.assertRaises(OSError, realpath, path, strict=True)
453+
path = os.fsencode(ABSTFN) + b'\\nonexistent\\x\x00'
454+
self.assertEqual(realpath(path, strict=False), path)
455+
self.assertRaises(OSError, realpath, path, strict=True)
456+
457+
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
458+
def test_realpath_undecodable(self):
459+
realpath = ntpath.realpath
460+
ABSTFN = ntpath.abspath(os_helper.TESTFN)
461+
path = os.fsencode(ABSTFN) + b'\xff'
462+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
463+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
464+
path = os.fsencode(ABSTFN) + b'\\nonexistent\\\xff'
465+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
466+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
467+
440468
@os_helper.skip_unless_symlink
441469
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
442470
def test_realpath_relative(self):

Lib/test/test_posixpath.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -489,11 +489,37 @@ def test_realpath_strict(self):
489489
finally:
490490
os_helper.unlink(ABSTFN)
491491

492+
def test_realpath_embedded_null(self):
493+
path = '/\x00'
494+
self.assertRaises(ValueError, realpath, path, strict=False)
495+
self.assertRaises(ValueError, realpath, path, strict=True)
496+
path = b'/\x00'
497+
self.assertRaises(ValueError, realpath, path, strict=False)
498+
self.assertRaises(ValueError, realpath, path, strict=True)
499+
path = '/nonexistent/x\x00'
500+
self.assertRaises(ValueError, realpath, path, strict=False)
501+
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
502+
path = b'/nonexistent/x\x00'
503+
self.assertRaises(ValueError, realpath, path, strict=False)
504+
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
505+
506+
@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
492507
def test_realpath_unencodable(self):
493-
self.assertRaises(ValueError, realpath, 'test\0', strict=False)
494-
self.assertRaises(ValueError, realpath, 'test\0', strict=True)
495-
self.assertRaises(UnicodeEncodeError, realpath, '\ud800', strict=False)
496-
self.assertRaises(UnicodeEncodeError, realpath, '\ud800', strict=True)
508+
path = '/\ud800'
509+
self.assertRaises(UnicodeEncodeError, realpath, path, strict=False)
510+
self.assertRaises(UnicodeEncodeError, realpath, path, strict=True)
511+
path = '/nonexistent/\ud800'
512+
self.assertRaises(UnicodeEncodeError, realpath, path, strict=False)
513+
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
514+
515+
@unittest.skipUnless(sys.platform == 'win32', 'requires native Unicode paths')
516+
def test_realpath_undecodable(self):
517+
path = b'/\xff'
518+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
519+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=True)
520+
path = b'/nonexistent/\xff'
521+
self.assertRaises(UnicodeDecodeError, realpath, path, strict=False)
522+
self.assertRaises(FileNotFoundError, realpath, path, strict=True)
497523

498524
@os_helper.skip_unless_symlink
499525
@skip_if_ABSTFN_contains_backslash

Lib/test/test_tarfile.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4093,6 +4093,7 @@ def test_data_filter(self):
40934093
self.assertIs(filtered.name, tarinfo.name)
40944094
self.assertIs(filtered.type, tarinfo.type)
40954095

4096+
@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
40964097
def test_filter_unencodable(self):
40974098
# Sanity check using a valid path.
40984099
tarinfo = tarfile.TarInfo(os_helper.TESTFN)
@@ -4101,14 +4102,17 @@ def test_filter_unencodable(self):
41014102
filtered = tarfile.data_filter(tarinfo, '')
41024103
self.assertIs(filtered.name, tarinfo.name)
41034104

4104-
tarinfo = tarfile.TarInfo('test\0')
4105+
tarinfo = tarfile.TarInfo('test\x00')
41054106
self.assertRaises(ValueError, tarfile.tar_filter, tarinfo, '')
41064107
self.assertRaises(ValueError, tarfile.data_filter, tarinfo, '')
41074108
tarinfo = tarfile.TarInfo('\ud800')
41084109
self.assertRaises(UnicodeEncodeError, tarfile.tar_filter, tarinfo, '')
41094110
self.assertRaises(UnicodeEncodeError, tarfile.data_filter, tarinfo, '')
41104111

4111-
def test_extract_encode_error(self):
4112+
@unittest.skipIf(sys.platform == 'win32', 'requires native bytes paths')
4113+
def test_extract_unencodable(self):
4114+
# Create a member with name \xed\xa0\x80 which is UTF-8 encoded
4115+
# lone surrogate \ud800.
41124116
with ArchiveMaker(encoding='ascii', errors='surrogateescape') as arc:
41134117
arc.add('\udced\udca0\udc80')
41144118
with os_helper.temp_cwd() as tmp:

0 commit comments

Comments
 (0)