|
5 | 5 | operating systems.
|
6 | 6 | """
|
7 | 7 |
|
8 |
| -import contextlib |
9 | 8 | import functools
|
10 |
| -import glob |
11 | 9 | import io
|
12 | 10 | import ntpath
|
13 | 11 | import os
|
14 | 12 | import posixpath
|
15 |
| -import re |
16 | 13 | import sys
|
17 | 14 | import warnings
|
18 | 15 | from _collections_abc import Sequence
|
@@ -75,17 +72,23 @@ def _is_case_sensitive(pathmod):
|
75 | 72 | # Globbing helpers
|
76 | 73 | #
|
77 | 74 |
|
| 75 | +re = glob = None |
| 76 | + |
78 | 77 |
|
79 | 78 | @functools.lru_cache(maxsize=256)
|
80 | 79 | def _compile_pattern(pat, sep, case_sensitive):
|
81 | 80 | """Compile given glob pattern to a re.Pattern object (observing case
|
82 | 81 | sensitivity)."""
|
| 82 | + global re, glob |
| 83 | + if re is None: |
| 84 | + import re, glob |
| 85 | + |
83 | 86 | flags = re.NOFLAG if case_sensitive else re.IGNORECASE
|
84 | 87 | regex = glob.translate(pat, recursive=True, include_hidden=True, seps=sep)
|
85 | 88 | # The string representation of an empty path is a single dot ('.'). Empty
|
86 | 89 | # paths shouldn't match wildcards, so we consume it with an atomic group.
|
87 | 90 | regex = r'(\.\Z)?+' + regex
|
88 |
| - return re.compile(regex, flags).match |
| 91 | + return re.compile(regex, flags=flags).match |
89 | 92 |
|
90 | 93 |
|
91 | 94 | def _select_children(parent_paths, dir_only, follow_symlinks, match):
|
@@ -981,7 +984,8 @@ def iterdir(self):
|
981 | 984 | def _scandir(self):
|
982 | 985 | # Emulate os.scandir(), which returns an object that can be used as a
|
983 | 986 | # context manager. This method is called by walk() and glob().
|
984 |
| - return contextlib.nullcontext(self.iterdir()) |
| 987 | + from contextlib import nullcontext |
| 988 | + return nullcontext(self.iterdir()) |
985 | 989 |
|
986 | 990 | def _make_child_relpath(self, name):
|
987 | 991 | path_str = str(self)
|
|
0 commit comments