Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

GH-114847: Speed upposixpath.realpath()#114848

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
barneygale merged 23 commits intopython:mainfrombarneygale:gh-114847
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
23 commits
Select commitHold shift + click to select a range
0a4f60a
GH-114847: Speed up `posixpath.realpath()`
barneygaleFeb 1, 2024
5aa9c51
Tiny tweak
barneygaleFeb 1, 2024
b758710
Slightly speed up handling of empty/dot parts
barneygaleFeb 1, 2024
eb079b4
Merge branch 'main' into gh-114847
barneygaleMar 31, 2024
60d3bcb
Simplify '..' handling.
barneygaleMar 31, 2024
b849308
A few more perf improvements and simplifications.
barneygaleMar 31, 2024
085e105
Simplify symlink resolution signalling
barneygaleMar 31, 2024
69dabd2
Tiny tweaks
barneygaleMar 31, 2024
197c871
Spacing
barneygaleMar 31, 2024
b03e474
Merge branch 'main' into gh-114847
barneygaleApr 1, 2024
ecd1fe7
Comments
barneygaleApr 1, 2024
4b83c97
Final tweaks
barneygaleApr 1, 2024
77712d2
Further explain 'rest'.
barneygaleApr 1, 2024
15199a8
Update Lib/posixpath.py
barneygaleApr 2, 2024
8b3b808
Merge branch 'main' into gh-114847
barneygaleApr 2, 2024
01127e3
Merge branch 'main' into gh-114847
barneygaleApr 3, 2024
31955ee
Undo re-ordering of lstat() and seen lookup
barneygaleApr 3, 2024
211167e
Merge branch 'main' into gh-114847
barneygaleApr 3, 2024
fe62fef
Re-optimise link check
barneygaleApr 4, 2024
ebccd2b
Stop querying when `lstat()` fails.
barneygaleApr 4, 2024
d2a0248
Keep querying if we hit an OSError from lstat()
barneygaleApr 4, 2024
ecbb27c
Merge branch 'main' into gh-114847
barneygaleApr 4, 2024
eed739a
Update Lib/posixpath.py
barneygaleApr 5, 2024
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
NextNext commit
GH-114847: Speed upposixpath.realpath()
Apply the following optimizations to `posixpath.realpath()`:- Remove use of recursion- Directly construct child paths rather than using `join()`- Use `os.getcwd[b]()` rather than `abspath()`- Use `startswith(sep)` rather than `isabs()`
  • Loading branch information
@barneygale
barneygale committedFeb 1, 2024
commit0a4f60a77e85af697d536717454a101cadc870d3
83 changes: 48 additions & 35 deletionsLib/posixpath.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -432,49 +432,46 @@ 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, {})
return abspath(path)

# Join two paths, normalizing and eliminating any symbolic links
# encountered in the second path.
def _joinrealpath(path, rest, strict, seen):
if isinstance(path, bytes):
if isinstance(filename, bytes):
sep = b'/'
curdir = b'.'
pardir = b'..'
getcwd = os.getcwdb
else:
sep = '/'
curdir = '.'
pardir = '..'

if isabs(rest):
rest = rest[1:]
path = sep

while rest:
name, _, rest = rest.partition(sep)
getcwd = os.getcwd

seen = {}
stack = []
querying = True
path = sep if filename.startswith(sep) else getcwd()
for part in reversed(filename.split(sep)):
stack.append((False, part))

while stack:
is_symlink, name = stack.pop()
if is_symlink:
# resolved symlink
seen[name] = path
continue
if not name or name == curdir:
# current dir
continue
if name == pardir:
# parent dir
if path:
path, name = split(path)
if name == pardir:
path = join(path, pardir, pardir)
newpath, name = split(path)
if name == pardir:
path = path + sep + pardir
else:
path =pardir
path =newpath
continue
newpath = join(path, name)
try:
st = os.lstat(newpath)
except OSError:
if strict:
raise
is_link = False
if len(path) == 1:
newpath = path + name
else:
is_link =stat.S_ISLNK(st.st_mode)
if notis_link:
newpath =path + sep + name
if notquerying:
path = newpath
continue
# Resolve the symbolic link
Expand All@@ -490,14 +487,30 @@ def _joinrealpath(path, rest, strict, seen):
os.stat(newpath)
else:
# Return already resolved part + rest of the path unchanged.
return join(newpath, rest), False
seen[newpath] = None # not resolved symlink
path, ok = _joinrealpath(path, os.readlink(newpath), strict, seen)
if not ok:
return join(path, rest), False
seen[newpath] = path # resolved symlink
path = newpath
querying = False
continue
try:
st = os.lstat(newpath)
if not stat.S_ISLNK(st.st_mode):
path = newpath
continue
target = os.readlink(newpath)
except OSError:
if strict:
raise
else:
path = newpath
querying = False
continue

return path, True
seen[newpath] = None # not resolved symlink
if target.startswith(sep):
path = sep
stack.append((True, newpath))
for part in reversed(target.split(sep)):
stack.append((False, part))
return path


supports_unicode_filenames = (sys.platform == 'darwin')
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Speed up :func:`os.path.realpath` on non-Windows platforms.

[8]ページ先頭

©2009-2025 Movatter.jp