@@ -1741,11 +1741,6 @@ def find(root, files, recursive=True, max_depth: Optional[int] = None):
17411741 return result
17421742
17431743
1744- def _log_file_access_issue (e : OSError , path : str ) -> None :
1745- errno_name = errno .errorcode .get (e .errno , "UNKNOWN" )
1746- tty .debug (f"find must skip { path } : { errno_name } { e } " )
1747-
1748-
17491744@system_path_filter (arg_slice = slice (1 ))
17501745def find_max_depth (root , globs , max_depth : Optional [int ] = None ):
17511746 """Given a set of non-recursive glob file patterns, finds all
@@ -1759,10 +1754,19 @@ def find_max_depth(root, globs, max_depth: Optional[int] = None):
17591754 If ``globs`` is a list, files matching earlier entries are placed
17601755 in the return value before files matching later entries.
17611756 """
1757+ # If root doesn't exist, then we say we found nothing. If it
1758+ # exists but is not a dir, we assume the user would want to
1759+ # know; likewise if it exists but we do not have permission to
1760+ # access it.
17621761 try :
17631762 stat_root = os .stat (root )
1764- except OSError :
1765- return []
1763+ except OSError as e :
1764+ if e .errno == errno .ENOENT :
1765+ return []
1766+ else :
1767+ raise
1768+ if not stat .S_ISDIR (stat_root .st_mode ):
1769+ raise ValueError (f"{ root } is not a directory" )
17661770
17671771 if max_depth is None :
17681772 max_depth = sys .maxsize
@@ -1786,6 +1790,10 @@ def _dir_id(stat_info):
17861790 # https://github.com/python/cpython/blob/3.9/Python/fileutils.c
17871791 return (stat_info .st_ino , stat_info .st_dev )
17881792
1793+ def _log_file_access_issue (e ):
1794+ errno_name = errno .errorcode .get (e .errno , "UNKNOWN" )
1795+ tty .debug (f"find must skip { dir_entry .path } : { errno_name } { str (e )} " )
1796+
17891797 visited_dirs = set ([_dir_id (stat_root )])
17901798
17911799 # Each queue item stores the depth and path
@@ -1800,8 +1808,9 @@ def _dir_id(stat_info):
18001808 depth , next_dir = dir_queue .pop ()
18011809 try :
18021810 dir_iter = os .scandir (next_dir )
1803- except OSError as e :
1804- _log_file_access_issue (e , next_dir )
1811+ except OSError :
1812+ # Most commonly, this would be a permissions issue, for
1813+ # example if we are scanning an external directory like /usr
18051814 continue
18061815
18071816 with dir_iter :
@@ -1812,7 +1821,7 @@ def _dir_id(stat_info):
18121821 except OSError as e :
18131822 # Possible permission issue, or a symlink that cannot
18141823 # be resolved (ELOOP).
1815- _log_file_access_issue (e , dir_entry . path )
1824+ _log_file_access_issue (e )
18161825 continue
18171826
18181827 if it_is_a_dir and (depth < max_depth ):
@@ -1828,7 +1837,7 @@ def _dir_id(stat_info):
18281837 else :
18291838 stat_info = dir_entry .stat (follow_symlinks = True )
18301839 except OSError as e :
1831- _log_file_access_issue (e , dir_entry . path )
1840+ _log_file_access_issue (e )
18321841 continue
18331842
18341843 dir_id = _dir_id (stat_info )
0 commit comments