aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Lib/glob.py
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.ru>2022-04-18 12:39:32 +0300
committershadchin <shadchin@yandex-team.ru>2022-04-18 12:39:32 +0300
commitd4be68e361f4258cf0848fc70018dfe37a2acc24 (patch)
tree153e294cd97ac8b5d7a989612704a0c1f58e8ad4 /contrib/tools/python3/src/Lib/glob.py
parent260c02f5ccf242d9d9b8a873afaf6588c00237d6 (diff)
downloadydb-d4be68e361f4258cf0848fc70018dfe37a2acc24.tar.gz
IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4
ref:9f96be6d02ee8044fdd6f124b799b270c20ce641
Diffstat (limited to 'contrib/tools/python3/src/Lib/glob.py')
-rw-r--r--contrib/tools/python3/src/Lib/glob.py151
1 files changed, 105 insertions, 46 deletions
diff --git a/contrib/tools/python3/src/Lib/glob.py b/contrib/tools/python3/src/Lib/glob.py
index 1237061130..9fc08f45df 100644
--- a/contrib/tools/python3/src/Lib/glob.py
+++ b/contrib/tools/python3/src/Lib/glob.py
@@ -4,11 +4,13 @@ import contextlib
import os
import re
import fnmatch
+import itertools
+import stat
import sys
__all__ = ["glob", "iglob", "escape"]
-def glob(pathname, *, recursive=False):
+def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
"""Return a list of paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
@@ -19,9 +21,9 @@ def glob(pathname, *, recursive=False):
If recursive is true, the pattern '**' will match any files and
zero or more directories and subdirectories.
"""
- return list(iglob(pathname, recursive=recursive))
+ return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive))
-def iglob(pathname, *, recursive=False):
+def iglob(pathname, *, root_dir=None, dir_fd=None, recursive=False):
"""Return an iterator which yields the paths matching a pathname pattern.
The pattern may contain simple shell-style wildcards a la
@@ -33,35 +35,44 @@ def iglob(pathname, *, recursive=False):
zero or more directories and subdirectories.
"""
sys.audit("glob.glob", pathname, recursive)
- it = _iglob(pathname, recursive, False)
- if recursive and _isrecursive(pathname):
- s = next(it) # skip empty string
- assert not s
+ sys.audit("glob.glob/2", pathname, recursive, root_dir, dir_fd)
+ if root_dir is not None:
+ root_dir = os.fspath(root_dir)
+ else:
+ root_dir = pathname[:0]
+ it = _iglob(pathname, root_dir, dir_fd, recursive, False)
+ if not pathname or recursive and _isrecursive(pathname[:2]):
+ try:
+ s = next(it) # skip empty string
+ if s:
+ it = itertools.chain((s,), it)
+ except StopIteration:
+ pass
return it
-def _iglob(pathname, recursive, dironly):
+def _iglob(pathname, root_dir, dir_fd, recursive, dironly):
dirname, basename = os.path.split(pathname)
if not has_magic(pathname):
assert not dironly
if basename:
- if os.path.lexists(pathname):
+ if _lexists(_join(root_dir, pathname), dir_fd):
yield pathname
else:
# Patterns ending with a slash should match only directories
- if os.path.isdir(dirname):
+ if _isdir(_join(root_dir, dirname), dir_fd):
yield pathname
return
if not dirname:
if recursive and _isrecursive(basename):
- yield from _glob2(dirname, basename, dironly)
+ yield from _glob2(root_dir, basename, dir_fd, dironly)
else:
- yield from _glob1(dirname, basename, dironly)
+ yield from _glob1(root_dir, basename, dir_fd, dironly)
return
# `os.path.split()` returns the argument itself as a dirname if it is a
# drive or UNC path. Prevent an infinite recursion if a drive or UNC path
# contains magic characters (i.e. r'\\?\C:').
if dirname != pathname and has_magic(dirname):
- dirs = _iglob(dirname, recursive, True)
+ dirs = _iglob(dirname, root_dir, dir_fd, recursive, True)
else:
dirs = [dirname]
if has_magic(basename):
@@ -72,80 +83,125 @@ def _iglob(pathname, recursive, dironly):
else:
glob_in_dir = _glob0
for dirname in dirs:
- for name in glob_in_dir(dirname, basename, dironly):
+ for name in glob_in_dir(_join(root_dir, dirname), basename, dir_fd, dironly):
yield os.path.join(dirname, name)
# These 2 helper functions non-recursively glob inside a literal directory.
# They return a list of basenames. _glob1 accepts a pattern while _glob0
# takes a literal basename (so it only has to check for its existence).
-def _glob1(dirname, pattern, dironly):
- names = _listdir(dirname, dironly)
+def _glob1(dirname, pattern, dir_fd, dironly):
+ names = _listdir(dirname, dir_fd, dironly)
if not _ishidden(pattern):
names = (x for x in names if not _ishidden(x))
return fnmatch.filter(names, pattern)
-def _glob0(dirname, basename, dironly):
- if not basename:
- # `os.path.split()` returns an empty basename for paths ending with a
- # directory separator. 'q*x/' should match only directories.
- if os.path.isdir(dirname):
+def _glob0(dirname, basename, dir_fd, dironly):
+ if basename:
+ if _lexists(_join(dirname, basename), dir_fd):
return [basename]
else:
- if os.path.lexists(os.path.join(dirname, basename)):
+ # `os.path.split()` returns an empty basename for paths ending with a
+ # directory separator. 'q*x/' should match only directories.
+ if _isdir(dirname, dir_fd):
return [basename]
return []
# Following functions are not public but can be used by third-party code.
def glob0(dirname, pattern):
- return _glob0(dirname, pattern, False)
+ return _glob0(dirname, pattern, None, False)
def glob1(dirname, pattern):
- return _glob1(dirname, pattern, False)
+ return _glob1(dirname, pattern, None, False)
# This helper function recursively yields relative pathnames inside a literal
# directory.
-def _glob2(dirname, pattern, dironly):
+def _glob2(dirname, pattern, dir_fd, dironly):
assert _isrecursive(pattern)
yield pattern[:0]
- yield from _rlistdir(dirname, dironly)
+ yield from _rlistdir(dirname, dir_fd, dironly)
# If dironly is false, yields all file names inside a directory.
# If dironly is true, yields only directory names.
-def _iterdir(dirname, dironly):
- if not dirname:
- if isinstance(dirname, bytes):
- dirname = bytes(os.curdir, 'ASCII')
- else:
- dirname = os.curdir
+def _iterdir(dirname, dir_fd, dironly):
try:
- with os.scandir(dirname) as it:
- for entry in it:
- try:
- if not dironly or entry.is_dir():
- yield entry.name
- except OSError:
- pass
+ fd = None
+ fsencode = None
+ if dir_fd is not None:
+ if dirname:
+ fd = arg = os.open(dirname, _dir_open_flags, dir_fd=dir_fd)
+ else:
+ arg = dir_fd
+ if isinstance(dirname, bytes):
+ fsencode = os.fsencode
+ elif dirname:
+ arg = dirname
+ elif isinstance(dirname, bytes):
+ arg = bytes(os.curdir, 'ASCII')
+ else:
+ arg = os.curdir
+ try:
+ with os.scandir(arg) as it:
+ for entry in it:
+ try:
+ if not dironly or entry.is_dir():
+ if fsencode is not None:
+ yield fsencode(entry.name)
+ else:
+ yield entry.name
+ except OSError:
+ pass
+ finally:
+ if fd is not None:
+ os.close(fd)
except OSError:
return
-def _listdir(dirname, dironly):
- with contextlib.closing(_iterdir(dirname, dironly)) as it:
+def _listdir(dirname, dir_fd, dironly):
+ with contextlib.closing(_iterdir(dirname, dir_fd, dironly)) as it:
return list(it)
# Recursively yields relative pathnames inside a literal directory.
-def _rlistdir(dirname, dironly):
- names = _listdir(dirname, dironly)
+def _rlistdir(dirname, dir_fd, dironly):
+ names = _listdir(dirname, dir_fd, dironly)
for x in names:
if not _ishidden(x):
yield x
- path = os.path.join(dirname, x) if dirname else x
- for y in _rlistdir(path, dironly):
- yield os.path.join(x, y)
+ path = _join(dirname, x) if dirname else x
+ for y in _rlistdir(path, dir_fd, dironly):
+ yield _join(x, y)
+def _lexists(pathname, dir_fd):
+ # Same as os.path.lexists(), but with dir_fd
+ if dir_fd is None:
+ return os.path.lexists(pathname)
+ try:
+ os.lstat(pathname, dir_fd=dir_fd)
+ except (OSError, ValueError):
+ return False
+ else:
+ return True
+
+def _isdir(pathname, dir_fd):
+ # Same as os.path.isdir(), but with dir_fd
+ if dir_fd is None:
+ return os.path.isdir(pathname)
+ try:
+ st = os.stat(pathname, dir_fd=dir_fd)
+ except (OSError, ValueError):
+ return False
+ else:
+ return stat.S_ISDIR(st.st_mode)
+
+def _join(dirname, basename):
+ # It is common if dirname or basename is empty
+ if not dirname or not basename:
+ return dirname or basename
+ return os.path.join(dirname, basename)
+
magic_check = re.compile('([*?[])')
magic_check_bytes = re.compile(b'([*?[])')
@@ -176,3 +232,6 @@ def escape(pathname):
else:
pathname = magic_check.sub(r'[\1]', pathname)
return drive + pathname
+
+
+_dir_open_flags = os.O_RDONLY | getattr(os, 'O_DIRECTORY', 0)