Repository navigation
Conversation
When the standard library is a zip archive on sys.path, the test package is not a directory: findtests() failed with NotADirectoryError from os.listdir() and load_package_tests() failed with "Start directory is not importable" from unittest discovery. Fall back to listing the test modules with pkgutil.iter_modules() through the import system.
gpshead
commented
Sep 8, 2026
… regrtest Make unittest.TestLoader.discover() work when the start directory is inside an archive on sys.path, such as a zip file: the archive's path entry finder lists the entries through pkgutil.iter_modules() and tells modules and packages apart, and the rest of discovery (module naming, importing, the load_tests protocol) already works on such paths. With that, test.support.load_package_tests() needs no special case and goes back to plain discovery. libregrtest's findtests() keeps a small fallback for listing the top level test directory, now keyed on os.path.isdir() rather than on the exception os.listdir() raises, which is NotADirectoryError on POSIX but FileNotFoundError on Windows.
Documentation build overview
|
gpshead
commented
Sep 8, 2026
gpshead
commented
Sep 8, 2026
Address review: describe what _find_tests() hands _find_test_path() with module-level constants (_FILESYSTEM_ENTRY, _FILESYSTEM_MODULE, _FILESYSTEM_PACKAGE, _ARCHIVE_MODULE, _ARCHIVE_PACKAGE) instead of inline strings and None, and split the archive helper into a predicate and a listing function so that neither returns None to mean anything. Drop the regrtest NEWS entry; the unittest one covers the change.
serhiy-storchaka
self-requested a review
September 9, 2026 14:07
…in discovery Address review of unittest discovery inside archives: * A path hook such as zipimport claims every path below its archive, so a mistyped start directory there gave an empty suite. discover() now raises ImportError unless a top level directory in an archive is the archive itself or lists a module, and regrtest's findtests() lets os.listdir() raise when the archive lists nothing. * Leave out names which the archive's finder lists but cannot import, such as extension modules in a zip file, instead of reporting them as failed imports. * Pass *kind* to _find_test_path() for archive entries only, so that subclasses overriding it with the previous signature keep working for file system discovery. * Use pkgutil.get_importer() in _is_archive_dir(); it does not cache a negative result, contrary to what the docstring said. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the
testpackage lives inside an archive onsys.path(a zipped standard library, a zipapp), the test suite cannot run:unittest.TestLoader.discover()requires a real directory and raises "Start directory is not importable", which breaks every test package whoseload_tests()usestest.support.load_package_tests().libregrtest.findtests()callsos.listdir()on it, which raisesNotADirectoryError(FileNotFoundErroron Windows).Fix this in
unittestrather than in regrtest: when the start directory is not on the file system but a path hook claims it,discover()lists it through the archive's path entry finder withpkgutil.iter_modules(), which also distinguishes modules from packages. Module naming, importing and theload_testsprotocol already work on such paths.findtests()gets the samepkgutilfallback for the top-level listing, keyed onos.path.isdir().(thank you Claude Fable 5.1)