Skip to content

Commit

Permalink
pythonGH-77621: Delay some imports from pathlib
Browse files Browse the repository at this point in the history
Import `contextlib`, `glob` and `re` only as required.
  • Loading branch information
barneygale committed Nov 18, 2023
1 parent 607b5e3 commit 90c3018
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
operating systems.
"""

import contextlib
import functools
import glob
import io
import ntpath
import os
import posixpath
import re
import sys
import warnings
from _collections_abc import Sequence
Expand Down Expand Up @@ -80,12 +77,13 @@ def _is_case_sensitive(pathmod):
def _compile_pattern(pat, sep, case_sensitive):
"""Compile given glob pattern to a re.Pattern object (observing case
sensitivity)."""
flags = re.NOFLAG if case_sensitive else re.IGNORECASE
regex = glob.translate(pat, recursive=True, include_hidden=True, seps=sep)
from glob import translate
regex = translate(pat, recursive=True, include_hidden=True, seps=sep)
# 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.
regex = r'(\.\Z)?+' + regex
return re.compile(regex, flags).match
from re import compile, NOFLAG, IGNORECASE
return compile(regex, flags=NOFLAG if case_sensitive else IGNORECASE).match


def _select_children(parent_paths, dir_only, follow_symlinks, match):
Expand Down Expand Up @@ -989,7 +987,8 @@ def iterdir(self):
def _scandir(self):
# Emulate os.scandir(), which returns an object that can be used as a
# 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):
path_str = str(self)
Expand Down

0 comments on commit 90c3018

Please sign in to comment.