Skip to content
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
12 changes: 4 additions & 8 deletions Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def __init__(self, sep, case_sensitive, case_pedantic=False, recursive=False):

# Low-level methods

lstat = staticmethod(os.lstat)
lexists = staticmethod(os.path.lexists)
scandir = staticmethod(os.scandir)
parse_entry = operator.attrgetter('path')
concat_path = operator.add
Expand Down Expand Up @@ -510,14 +510,10 @@ def select_exists(self, path, exists=False):
"""
if exists:
# Optimization: this path is already known to exist, e.g. because
# it was returned from os.scandir(), so we skip calling lstat().
# it was returned from os.scandir(), so we skip calling lexists().
yield path
elif self.lexists(path):
yield path
else:
try:
self.lstat(path)
yield path
except OSError:
pass

@classmethod
def walk(cls, root, top_down, on_error, follow_symlinks):
Expand Down
10 changes: 9 additions & 1 deletion Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,17 @@ def _is_case_sensitive(parser):


class Globber(glob._Globber):
lstat = operator.methodcaller('lstat')
add_slash = operator.methodcaller('joinpath', '')

@staticmethod
def lexists(path):
# Emulate os.path.lexists(), which never raises OSError.
try:
path.stat(follow_symlinks=False)
except (OSError, ValueError):
return False
return True

@staticmethod
def scandir(path):
# Emulate os.scandir(), which returns an object that can be used as a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Speed up :meth:`pathlib.Path.glob` on Windows by using fast implementation
of :func:`os.path.lexists`.