@@ -62,29 +62,46 @@ def _splitext(path):
6262 return os .path .splitext (path )[0 ]
6363
6464
65+ # What _find_tests() asks _find_test_path() to load: an entry on the file
66+ # system, which _find_test_path() classifies itself, or a module or package
67+ # inside an archive on sys.path (a zip file, say), which the archive's path
68+ # entry finder has already classified.
69+ _FILESYSTEM_ENTRY = 'filesystem entry'
70+ _FILESYSTEM_MODULE = 'filesystem module'
71+ _FILESYSTEM_PACKAGE = 'filesystem package'
72+ _ARCHIVE_MODULE = 'archive module'
73+ _ARCHIVE_PACKAGE = 'archive package'
74+
75+
76+ def _is_archive_dir (path ):
77+ """Is *path* a directory inside an archive on sys.path?
78+
79+ A test package can live inside an archive such as a zip file, where
80+ os.listdir() does not work but the path entry finder for the archive
81+ still knows the contents. The path hooks are probed directly rather
82+ than with pkgutil.get_importer(), which would cache a negative result
83+ in sys.path_importer_cache and so hide a directory created at that
84+ path later on.
85+ """
86+ if os .path .isdir (path ):
87+ return False
88+ if sys .path_importer_cache .get (path ) is not None :
89+ return True
90+ for hook in sys .path_hooks :
91+ try :
92+ hook (path )
93+ except ImportError :
94+ continue
95+ return True
96+ return False
97+
98+
6599def _list_archive_dir (path ):
66- """List the modules in *path* when it is not a directory on disk .
100+ """The sorted (name, is_package) pairs of the modules in *path*.
67101
68- A test package can live inside an archive on sys.path, such as a zip
69- file, where os.listdir() does not work but the path entry finder for
70- the archive still knows the contents (see pkgutil.iter_modules()).
71- Return a sorted list of (name, is_package) pairs, or None if *path* is
72- a directory on the file system or nothing can import from it.
102+ *path* is a directory inside an archive on sys.path; its path entry
103+ finder lists it (see pkgutil.iter_modules()).
73104 """
74- if os .path .isdir (path ):
75- return None
76- # Probe the path hooks directly rather than with pkgutil.get_importer(),
77- # which would cache a negative result in sys.path_importer_cache and so
78- # hide a directory created at that path later on.
79- if sys .path_importer_cache .get (path ) is None :
80- for hook in sys .path_hooks :
81- try :
82- hook (path )
83- except ImportError :
84- continue
85- break
86- else :
87- return None
88105 return sorted ((name , ispkg )
89106 for _ , name , ispkg in pkgutil .iter_modules ([path ]))
90107
@@ -316,13 +333,13 @@ def discover(self, start_dir, pattern='test*.py', top_level_dir=None):
316333 start_dir = os .path .abspath (start_dir )
317334 if start_dir != top_level_dir :
318335 is_not_importable = not os .path .isfile (os .path .join (start_dir , '__init__.py' ))
319- elif _list_archive_dir (os .path .abspath (start_dir )) is not None :
336+ elif _is_archive_dir (os .path .abspath (start_dir )):
320337 # a directory inside an archive on sys.path, such as a zip file
321338 start_dir = os .path .abspath (start_dir )
322339 if start_dir != top_level_dir :
323340 parent , name = os .path .split (start_dir )
324- is_not_importable = (
325- (name , True ) not in ( _list_archive_dir ( parent ) or ()))
341+ siblings = _list_archive_dir ( parent )
342+ is_not_importable = (name , True ) not in siblings
326343 else :
327344 # support for discovery from dotted module names
328345 try :
@@ -418,7 +435,7 @@ def _find_tests(self, start_dir, pattern, namespace=False):
418435 """Used by discovery. Yields test suites it loads."""
419436 # Inside an archive the import system lists the entries and tells
420437 # modules and packages apart; on disk the file system does.
421- entries = _list_archive_dir (start_dir )
438+ in_archive = _is_archive_dir (start_dir )
422439 # Handle the __init__ in this package
423440 name = self ._get_name_from_path (start_dir )
424441 # name is '.' when start_dir == top_level_dir (and top_level_dir is by
@@ -428,24 +445,25 @@ def _find_tests(self, start_dir, pattern, namespace=False):
428445 # loadTestsFromModule with name.
429446 tests , should_recurse = self ._find_test_path (
430447 start_dir , pattern , namespace ,
431- kind = None if entries is None else 'package' )
448+ _ARCHIVE_PACKAGE if in_archive else _FILESYSTEM_ENTRY )
432449 if tests is not None :
433450 yield tests
434451 if not should_recurse :
435452 # Either an error occurred, or load_tests was used by the
436453 # package.
437454 return
438455 # Handle the contents.
439- if entries is None :
440- paths = [(path , None ) for path in sorted (os .listdir (start_dir ))]
441- else :
456+ if in_archive :
442457 paths = [(name if ispkg else f'{ name } .py' ,
443- 'package' if ispkg else 'module' )
444- for name , ispkg in entries ]
458+ _ARCHIVE_PACKAGE if ispkg else _ARCHIVE_MODULE )
459+ for name , ispkg in _list_archive_dir (start_dir )]
460+ else :
461+ paths = [(path , _FILESYSTEM_ENTRY )
462+ for path in sorted (os .listdir (start_dir ))]
445463 for path , kind in paths :
446464 full_path = os .path .join (start_dir , path )
447465 tests , should_recurse = self ._find_test_path (
448- full_path , pattern , False , kind = kind )
466+ full_path , pattern , False , kind )
449467 if tests is not None :
450468 yield tests
451469 if should_recurse :
@@ -458,25 +476,26 @@ def _find_tests(self, start_dir, pattern, namespace=False):
458476 self ._loading_packages .discard (name )
459477
460478 def _find_test_path (self , full_path , pattern , namespace = False ,
461- kind = None ):
479+ kind = _FILESYSTEM_ENTRY ):
462480 """Used by discovery.
463481
464482 Loads tests from a single file, or a directories' __init__.py when
465- passed the directory. *kind* is 'module' or 'package' for an entry
466- inside an archive, or None to consult the file system.
483+ passed the directory. *kind* says whether full_path is a module or
484+ a package inside an archive (_ARCHIVE_MODULE, _ARCHIVE_PACKAGE) or
485+ an entry on the file system (_FILESYSTEM_ENTRY) to be classified.
467486
468487 Returns a tuple (None_or_tests_from_file, should_recurse).
469488 """
470489 basename = os .path .basename (full_path )
471- if kind is None :
490+ if kind == _FILESYSTEM_ENTRY :
472491 if os .path .isfile (full_path ):
473- kind = 'module'
492+ kind = _FILESYSTEM_MODULE
474493 elif os .path .isdir (full_path ):
475494 init = os .path .join (full_path , '__init__.py' )
476495 if not namespace and not os .path .isfile (init ):
477496 return None , False
478- kind = 'package'
479- if kind == 'module' :
497+ kind = _FILESYSTEM_PACKAGE
498+ if kind in ( _FILESYSTEM_MODULE , _ARCHIVE_MODULE ) :
480499 if not VALID_MODULE_NAME .match (basename ):
481500 # valid Python identifiers only
482501 return None , False
@@ -510,7 +529,7 @@ def _find_test_path(self, full_path, pattern, namespace=False,
510529 raise ImportError (
511530 msg % (mod_name , module_dir , expected_dir ))
512531 return self .loadTestsFromModule (module , pattern = pattern ), False
513- elif kind == 'package' :
532+ elif kind in ( _FILESYSTEM_PACKAGE , _ARCHIVE_PACKAGE ) :
514533 load_tests = None
515534 tests = None
516535 name = self ._get_name_from_path (full_path )
0 commit comments