Skip to content

gh-117338: Handle leading // for posixpath.realpath #117340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
31 changes: 16 additions & 15 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def realpath(filename, *, strict=False):
"""Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path."""
filename = os.fspath(filename)
path, ok = _joinrealpath(filename[:0], filename, strict, {})
path, _ = _joinrealpath(filename[:0], filename, strict, {})
return abspath(path)

# Join two paths, normalizing and eliminating any symbolic links
Expand All @@ -427,9 +427,9 @@ def _joinrealpath(path, rest, strict, seen):
curdir = '.'
pardir = '..'

if isabs(rest):
rest = rest[1:]
path = sep
_, root, rest = splitroot(rest)
if root:
path = root

while rest:
name, _, rest = rest.partition(sep)
Expand All @@ -438,22 +438,24 @@ def _joinrealpath(path, rest, strict, seen):
continue
if name == pardir:
# parent dir
if path:
path, name = split(path)
if name == pardir:
path = join(path, pardir, pardir)
else:
if not path:
# ..
path = pardir
elif basename(path) == pardir:
# ../..
path = join(path, pardir)
else:
# foo/bar/.. -> foo
path = dirname(path)
continue
newpath = join(path, name)
try:
st = os.lstat(newpath)
is_link = stat.S_ISLNK(st.st_mode)
except OSError:
if strict:
raise
is_link = False
else:
is_link = stat.S_ISLNK(st.st_mode)
if not is_link:
path = newpath
continue
Expand All @@ -465,12 +467,11 @@ def _joinrealpath(path, rest, strict, seen):
# use cached value
continue
# The symlink is not resolved, so we must have a symlink loop.
if strict:
# Raise OSError(errno.ELOOP)
os.stat(newpath)
else:
if not strict:
# Return already resolved part + rest of the path unchanged.
return join(newpath, rest), False
# Raise OSError(errno.ELOOP)
os.stat(newpath)
seen[newpath] = None # not resolved symlink
path, ok = _joinrealpath(path, os.readlink(newpath), strict, seen)
if not ok:
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,11 @@ def test_realpath_resolve_first(self):
safe_rmdir(ABSTFN + "/k")
safe_rmdir(ABSTFN)

@skip_if_ABSTFN_contains_backslash
def test_realpath_double_slash(self):
# gh-117338: Handle leading `//` for `posixpath.realpath`
self.assertEqual(realpath("//foo"), "//foo")

def test_relpath(self):
(real_getcwd, os.getcwd) = (os.getcwd, lambda: r"/home/user/bar")
try:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle leading ``//`` for :func:`posixpath.realpath` using :func:`posixpath.splitroot`.