aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/pathlib/_abc.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-06-07 17:59:34 +0100
committerGitHub <noreply@github.com>2024-06-07 17:59:34 +0100
commit242c7498e5a889b47847fb6f0f133ce461fa7e24 (patch)
tree8ddfc753a1fe1d4f6ba2fd32d5f4b6ebbf3e6c0f /Lib/pathlib/_abc.py
parent90b75405260467814c93738a3325645918d4ea51 (diff)
downloadcpython-242c7498e5a889b47847fb6f0f133ce461fa7e24.tar.gz
cpython-242c7498e5a889b47847fb6f0f133ce461fa7e24.zip
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.
Diffstat (limited to 'Lib/pathlib/_abc.py')
-rw-r--r--Lib/pathlib/_abc.py32
1 files changed, 30 insertions, 2 deletions
diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py
index 1a74f457c3f..ecea8e88d1a 100644
--- a/Lib/pathlib/_abc.py
+++ b/Lib/pathlib/_abc.py
@@ -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