mirror of
https://github.com/python/cpython
synced 2024-09-16 00:17:02 +00:00
GH-77621: Delay some imports from pathlib (#112244)
Import `contextlib`, `glob` and `re` only as required. Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
fbb9027a03
commit
bbb4367b55
|
@ -5,14 +5,11 @@
|
||||||
operating systems.
|
operating systems.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import contextlib
|
|
||||||
import functools
|
import functools
|
||||||
import glob
|
|
||||||
import io
|
import io
|
||||||
import ntpath
|
import ntpath
|
||||||
import os
|
import os
|
||||||
import posixpath
|
import posixpath
|
||||||
import re
|
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
import warnings
|
||||||
from _collections_abc import Sequence
|
from _collections_abc import Sequence
|
||||||
|
@ -75,17 +72,23 @@ def _is_case_sensitive(pathmod):
|
||||||
# Globbing helpers
|
# Globbing helpers
|
||||||
#
|
#
|
||||||
|
|
||||||
|
re = glob = None
|
||||||
|
|
||||||
|
|
||||||
@functools.lru_cache(maxsize=256)
|
@functools.lru_cache(maxsize=256)
|
||||||
def _compile_pattern(pat, sep, case_sensitive):
|
def _compile_pattern(pat, sep, case_sensitive):
|
||||||
"""Compile given glob pattern to a re.Pattern object (observing case
|
"""Compile given glob pattern to a re.Pattern object (observing case
|
||||||
sensitivity)."""
|
sensitivity)."""
|
||||||
|
global re, glob
|
||||||
|
if re is None:
|
||||||
|
import re, glob
|
||||||
|
|
||||||
flags = re.NOFLAG if case_sensitive else re.IGNORECASE
|
flags = re.NOFLAG if case_sensitive else re.IGNORECASE
|
||||||
regex = glob.translate(pat, recursive=True, include_hidden=True, seps=sep)
|
regex = glob.translate(pat, recursive=True, include_hidden=True, seps=sep)
|
||||||
# The string representation of an empty path is a single dot ('.'). Empty
|
# The string representation of an empty path is a single dot ('.'). Empty
|
||||||
# paths shouldn't match wildcards, so we consume it with an atomic group.
|
# paths shouldn't match wildcards, so we consume it with an atomic group.
|
||||||
regex = r'(\.\Z)?+' + regex
|
regex = r'(\.\Z)?+' + regex
|
||||||
return re.compile(regex, flags).match
|
return re.compile(regex, flags=flags).match
|
||||||
|
|
||||||
|
|
||||||
def _select_children(parent_paths, dir_only, follow_symlinks, match):
|
def _select_children(parent_paths, dir_only, follow_symlinks, match):
|
||||||
|
@ -981,7 +984,8 @@ def iterdir(self):
|
||||||
def _scandir(self):
|
def _scandir(self):
|
||||||
# Emulate os.scandir(), which returns an object that can be used as a
|
# Emulate os.scandir(), which returns an object that can be used as a
|
||||||
# context manager. This method is called by walk() and glob().
|
# context manager. This method is called by walk() and glob().
|
||||||
return contextlib.nullcontext(self.iterdir())
|
from contextlib import nullcontext
|
||||||
|
return nullcontext(self.iterdir())
|
||||||
|
|
||||||
def _make_child_relpath(self, name):
|
def _make_child_relpath(self, name):
|
||||||
path_str = str(self)
|
path_str = str(self)
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Slightly improve the import time of the :mod:`pathlib` module by deferring
|
||||||
|
some imports. Patch by Barney Gale.
|
Loading…
Reference in a new issue