mirror of
https://github.com/python/cpython.git
synced 2025-07-23 03:05:38 +00:00
GH-116380: Move pathlib-specific code from glob
to pathlib._abc
. (#120011)
In `glob._Globber`, move pathlib-specific methods to `pathlib._abc.PathGlobber` and replace them with abstract methods. Rename `glob._Globber` to `glob._GlobberBase`. As a result, the `glob` module is no longer befouled by code that can only ever apply to pathlib. No change of behaviour.
This commit is contained in:
parent
90b7540526
commit
242c7498e5
2 changed files with 52 additions and 14 deletions
|
@ -12,8 +12,9 @@ resemble pathlib's PurePath and Path respectively.
|
|||
"""
|
||||
|
||||
import functools
|
||||
import operator
|
||||
import posixpath
|
||||
from glob import _Globber, _no_recurse_symlinks
|
||||
from glob import _GlobberBase, _no_recurse_symlinks
|
||||
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
|
||||
|
||||
|
||||
|
@ -84,6 +85,33 @@ class ParserBase:
|
|||
raise UnsupportedOperation(self._unsupported_msg('isabs()'))
|
||||
|
||||
|
||||
class PathGlobber(_GlobberBase):
|
||||
"""
|
||||
Class providing shell-style globbing for path objects.
|
||||
"""
|
||||
|
||||
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())
|
||||
|
||||
@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.
|
||||
|
||||
|
@ -104,7 +132,7 @@ class PurePathBase:
|
|||
'_resolving',
|
||||
)
|
||||
parser = ParserBase()
|
||||
_globber = _Globber
|
||||
_globber = PathGlobber
|
||||
|
||||
def __init__(self, path, *paths):
|
||||
self._raw_path = self.parser.join(path, *paths) if paths else path
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue