@@ -45,9 +45,15 @@ def _is_case_sensitive(parser):
45
45
46
46
class Globber (glob ._Globber ):
47
47
lstat = operator .methodcaller ('lstat' )
48
- scandir = operator .methodcaller ('_scandir' )
49
48
add_slash = operator .methodcaller ('joinpath' , '' )
50
49
50
+ @staticmethod
51
+ def scandir (path ):
52
+ # Emulate os.scandir(), which returns an object that can be used as a
53
+ # context manager. This method is called by walk() and glob().
54
+ from contextlib import nullcontext
55
+ return nullcontext (path .iterdir ())
56
+
51
57
@staticmethod
52
58
def concat_path (path , text ):
53
59
"""Appends text to the given path.
@@ -677,20 +683,6 @@ def iterdir(self):
677
683
"""
678
684
raise UnsupportedOperation (self ._unsupported_msg ('iterdir()' ))
679
685
680
- def _scandir (self ):
681
- # Emulate os.scandir(), which returns an object that can be used as a
682
- # context manager. This method is called by walk() and glob().
683
- from contextlib import nullcontext
684
- return nullcontext (self .iterdir ())
685
-
686
- def _make_child_direntry (self , entry ):
687
- # Transform an entry yielded from _scandir() into a path object.
688
- # PathBase._scandir() yields PathBase objects, so this is a no-op.
689
- return entry
690
-
691
- def _make_child_relpath (self , name ):
692
- return self .joinpath (name )
693
-
694
686
def _glob_selector (self , parts , case_sensitive , recurse_symlinks ):
695
687
if case_sensitive is None :
696
688
case_sensitive = _is_case_sensitive (self .parser )
@@ -724,48 +716,7 @@ def rglob(self, pattern, *, case_sensitive=None, recurse_symlinks=True):
724
716
725
717
def walk (self , top_down = True , on_error = None , follow_symlinks = False ):
726
718
"""Walk the directory tree from this directory, similar to os.walk()."""
727
- paths = [self ]
728
-
729
- while paths :
730
- path = paths .pop ()
731
- if isinstance (path , tuple ):
732
- yield path
733
- continue
734
-
735
- # We may not have read permission for self, in which case we can't
736
- # get a list of the files the directory contains. os.walk()
737
- # always suppressed the exception in that instance, rather than
738
- # blow up for a minor reason when (say) a thousand readable
739
- # directories are still left to visit. That logic is copied here.
740
- try :
741
- scandir_obj = path ._scandir ()
742
- except OSError as error :
743
- if on_error is not None :
744
- on_error (error )
745
- continue
746
-
747
- with scandir_obj as scandir_it :
748
- dirnames = []
749
- filenames = []
750
- if not top_down :
751
- paths .append ((path , dirnames , filenames ))
752
- for entry in scandir_it :
753
- try :
754
- is_dir = entry .is_dir (follow_symlinks = follow_symlinks )
755
- except OSError :
756
- # Carried over from os.path.isdir().
757
- is_dir = False
758
-
759
- if is_dir :
760
- if not top_down :
761
- paths .append (path ._make_child_direntry (entry ))
762
- dirnames .append (entry .name )
763
- else :
764
- filenames .append (entry .name )
765
-
766
- if top_down :
767
- yield path , dirnames , filenames
768
- paths += [path ._make_child_relpath (d ) for d in reversed (dirnames )]
719
+ return self ._globber .walk (self , top_down , on_error , follow_symlinks )
769
720
770
721
def absolute (self ):
771
722
"""Return an absolute version of this path
0 commit comments