mirror of
https://github.com/python/cpython.git
synced 2025-08-09 19:38:42 +00:00
GH-125413: pathlib ABCs: use scandir()
to speed up glob()
(#126261)
Use the new `PathBase.scandir()` method in `PathBase.glob()`, which greatly reduces the number of `PathBase.stat()` calls needed when globbing. There are no user-facing changes, because the pathlib ABCs are still private and `Path.glob()` doesn't use the implementation in its superclass.
This commit is contained in:
parent
464a7a91d0
commit
68a51e0178
3 changed files with 10 additions and 25 deletions
|
@ -94,25 +94,13 @@ class PathGlobber(_GlobberBase):
|
|||
|
||||
lexists = operator.methodcaller('exists', follow_symlinks=False)
|
||||
add_slash = operator.methodcaller('joinpath', '')
|
||||
|
||||
@staticmethod
|
||||
def scandir(path):
|
||||
"""Emulates os.scandir(), which returns an object that can be used as
|
||||
a context manager. This method is called by walk() and glob().
|
||||
"""
|
||||
import contextlib
|
||||
return contextlib.nullcontext(path.iterdir())
|
||||
scandir = operator.methodcaller('scandir')
|
||||
|
||||
@staticmethod
|
||||
def concat_path(path, text):
|
||||
"""Appends text to the given path."""
|
||||
return path.with_segments(path._raw_path + text)
|
||||
|
||||
@staticmethod
|
||||
def parse_entry(entry):
|
||||
"""Returns the path of an entry yielded from scandir()."""
|
||||
return entry
|
||||
|
||||
|
||||
class PurePathBase:
|
||||
"""Base class for pure path objects.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue