cpython: cb7ee9d9cddd (original) (raw)

--- a/Doc/library/glob.rst +++ b/Doc/library/glob.rst @@ -14,7 +14,7 @@ The :mod:glob module finds all the pat according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched. This is done by using -the :func:os.listdir and :func:fnmatch.fnmatch functions in concert, and +the :func:os.scandir and :func:fnmatch.fnmatch functions in concert, and not by actually invoking a subshell. Note that unlike :func:fnmatch.fnmatch, :mod:glob treats filenames beginning with a dot (.) as special cases. (For tilde and shell variable expansion, use :func:os.path.expanduser and

--- a/Doc/whatsnew/3.6.rst +++ b/Doc/whatsnew/3.6.rst @@ -767,6 +767,10 @@ Optimizations Argument Clinic this overhead is significantly decreased. (Contributed by Serhiy Storchaka in :issue:27574). +* Optimized :func:~glob.glob and :func:~glob.iglob functions in the

--- a/Lib/glob.py +++ b/Lib/glob.py @@ -30,15 +30,16 @@ def iglob(pathname, *, recursive=False): If recursive is true, the pattern '**' will match any files and zero or more directories and subdirectories. """

@@ -49,47 +50,39 @@ def _iglob(pathname, recursive): return if not dirname: if recursive and _isrecursive(basename):

These 2 helper functions non-recursively glob inside a literal directory.

-# They return a list of basenames. glob1 accepts a pattern while glob0 +# 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):

+def _glob1(dirname, pattern, dironly):

@@ -100,30 +93,49 @@ def glob0(dirname, basename): return [basename] return [] +# Following functions are not public but can be used by third-party code. + +def glob0(dirname, pattern):

+ +def glob1(dirname, pattern):

+

This helper function recursively yields relative pathnames inside a literal

directory.

-def glob2(dirname, pattern): +def _glob2(dirname, pattern, dironly): assert _isrecursive(pattern) yield pattern[:0]

-# Recursively yields relative pathnames inside a literal directory. -def _rlistdir(dirname): +# 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 try:

+ +# Recursively yields relative pathnames inside a literal directory. +def _rlistdir(dirname, dironly):

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -89,6 +89,9 @@ Core and Builtins Library ------- +- Issue #25596: Optimized glob() and iglob() functions in the