gh-158377: Fix unittest.TestLoader.discover() leaking _top_level_dir on error - #158378
Open
christianaurichzm wants to merge 1 commit into
Open
christianaurichzm wants to merge 1 commit into
christianaurichzm wants to merge 1 commit into
Conversation
…l_dir on error TestLoader.discover() restores _top_level_dir at the end of the call, but only when discovery succeeds. If it raises, for example because the start directory is not importable, the loader keeps the top-level directory of the failed call, and a later discover() on the same loader without an explicit top_level_dir resolves against it. Restore _top_level_dir in a finally block. Two assertions in test_discover checked the leaked value and now expect None.
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.
Fixes #158377
TestLoader.discover()restores_top_level_dirat the end of the call (gh-59215), but only on the success path. If discovery raises, the loader keeps the top-level directory of the failed call, and a laterdiscover()on the same loader without an explicittop_level_dirresolves against it. This moves the restore into afinallyblock. Most of the diff is reindentation, so it is easier to review with whitespace changes hidden.This is reachable without touching the loader directly:
unittest.main()uses the sharedunittest.defaultTestLoader, so callingunittest.main(argv=[..., "discover", "-s", <typo>], exit=False)and then again with the right directory in the same process (for example from a notebook or REPL) fails on the second call too.Two assertions in
test_discoverchecked_top_level_dirafter anImportErrorfromdiscover()and after the successful call that follows it, so they encoded the leaked value. They now expectNone, the value of a fresh loader.