Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 268e61c

Browse files
committed
Bug #1213894: os.path.realpath didn't resolve symlinks that were the first
component of the path.
1 parent 5661699 commit 268e61c

3 files changed

Lines changed: 24 additions & 1 deletion

File tree

Lib/posixpath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ def realpath(filename):
414414
if isabs(filename):
415415
bits = ['/'] + filename.split('/')[1:]
416416
else:
417-
bits = filename.split('/')
417+
bits = [''] + filename.split('/')
418418

419419
for i in range(2, len(bits)+1):
420420
component = join(*bits[0:i])

Lib/test/test_posixpath.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,26 @@ def test_realpath_resolve_before_normalizing(self):
476476
self.safe_rmdir(ABSTFN + "/k/y")
477477
self.safe_rmdir(ABSTFN + "/k")
478478
self.safe_rmdir(ABSTFN)
479+
480+
def test_realpath_resolve_first(self):
481+
# Bug #1213894: The first component of the path, if not absolute,
482+
# must be resolved too.
483+
484+
try:
485+
old_path = abspath('.')
486+
os.mkdir(ABSTFN)
487+
os.mkdir(ABSTFN + "/k")
488+
os.symlink(ABSTFN, ABSTFN + "link")
489+
os.chdir(dirname(ABSTFN))
490+
491+
base = basename(ABSTFN)
492+
self.assertEqual(realpath(base + "link"), ABSTFN)
493+
self.assertEqual(realpath(base + "link/k"), ABSTFN + "/k")
494+
finally:
495+
os.chdir(old_path)
496+
self.safe_remove(ABSTFN + "link")
497+
self.safe_rmdir(ABSTFN + "/k")
498+
self.safe_rmdir(ABSTFN)
479499

480500
# Convenience functions for removing temporary files.
481501
def pass_os_error(self, func, filename):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ Extension Modules
136136
Library
137137
-------
138138

139+
- Bug #1213894: os.path.realpath didn't resolve symlinks that were the first
140+
component of the path.
141+
139142
- Patch #1120353: The xmlrpclib module provides better, more transparent,
140143
support for datetime.{datetime,date,time} objects. With use_datetime set
141144
to True, applications shouldn't have to fiddle with the DateTime wrapper

0 commit comments

Comments
 (0)