gh-102475: Fix os.path.realpath() for names which look like a drive by serhiy-storchaka · Pull Request #155399 · python/cpython · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions Lib/ntpath.py
29 changes: 29 additions & 0 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,35 @@ def test_isjunction(self):
self.assertFalse(ntpath.isjunction('tmpdir'))
self.assertPathEqual(ntpath.realpath('testjunc'), ntpath.realpath('tmpdir'))

@unittest.skipIf(sys.platform != 'win32', "Can only test on win32.")
def test_realpath_drive_like_names(self):
# gh-102475: the unresolved tail is appended, not joined, so a name
# which looks like a drive does not reset the path.
drive = ntpath.splitroot(os.getcwd())[0]
for path, expected in [
('C:/spam:eggs', 'C:\\spam:eggs'),
('C:/nonexistent/spam:eggs', 'C:\\nonexistent\\spam:eggs'),
('C:/spam:eggs/ham', 'C:\\spam:eggs\\ham'),
('C:/nonexistent/spam:eggs/ham', 'C:\\nonexistent\\spam:eggs\\ham'),
]:
with self.subTest(path=path):
self.assertEqual(ntpath.realpath(path), expected)
self.assertEqual(ntpath.realpath(os.fsencode(path)),
os.fsencode(expected))

@unittest.skipIf(sys.platform != 'win32', "Can only test on win32.")
def test_realpath_drive_relative(self):
# gh-102475: the working directory of a drive which does not exist
# is its root directory.
for drive in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
if not ntpath.exists(drive + ':'):
break
else:
raise unittest.SkipTest('all drives exist')
self.assertEqual(ntpath.realpath(drive + ':spam'),
drive + ':\\spam')
self.assertEqual(ntpath.realpath(drive + ':'), drive + ':\\')

def test_isfile_invalid_paths(self):
isfile = ntpath.isfile
self.assertIs(isfile('/tmp\udfffabcds'), False)
Expand Down
Loading