Skip to content
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Speed up posixpath.ismount
  • Loading branch information
nineteendo committed Mar 28, 2024
commit fb1c91aa228c00d66c8b59a886d1804c20d1dd9f
44 changes: 16 additions & 28 deletions Lib/posixpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,31 +217,19 @@ def ismount(path):
except (OSError, ValueError):
# It doesn't exist -- so not a mount point. :-)
return False
else:
# A symlink can never be a mount point
if stat.S_ISLNK(s1.st_mode):
return False

path = os.fspath(path)
if isinstance(path, bytes):
parent = join(path, b'..')
else:
parent = join(path, '..')
parent = realpath(parent)
# A symlink can never be a mount point
if stat.S_ISLNK(s1.st_mode):
return False

parent = realpath(dirname(path))
try:
s2 = os.lstat(parent)
except (OSError, ValueError):
return False

dev1 = s1.st_dev
dev2 = s2.st_dev
if dev1 != dev2:
return True # path/.. on a different device as path
ino1 = s1.st_ino
ino2 = s2.st_ino
if ino1 == ino2:
return True # path/.. is the same i-node as path
return False
# path/.. on a different device as path or the same i-node as path
return s1.st_dev != s2.st_dev or s1.st_ino == s2.st_ino


# Expand paths beginning with '~' or '~user'.
Expand Down Expand Up @@ -380,29 +368,29 @@ def normpath(path):
if isinstance(path, bytes):
sep = b'/'
empty = b''
dot = b'.'
dotdot = b'..'
curdir = b'.'
pardir = b'..'
else:
sep = '/'
empty = ''
dot = '.'
dotdot = '..'
curdir = '.'
pardir = '..'
if path == empty:
return dot
return curdir
_, initial_slashes, path = splitroot(path)
comps = path.split(sep)
new_comps = []
for comp in comps:
if comp in (empty, dot):
if comp in (empty, curdir):
continue
if (comp != dotdot or (not initial_slashes and not new_comps) or
(new_comps and new_comps[-1] == dotdot)):
if (comp != pardir or (not initial_slashes and not new_comps) or
(new_comps and new_comps[-1] == pardir)):
new_comps.append(comp)
elif new_comps:
new_comps.pop()
comps = new_comps
path = initial_slashes + sep.join(comps)
return path or dot
return path or curdir

else:
def normpath(path):
Expand Down