Skip to content

Stop using atexit and add integration tests for tmpdir fixture#10546

Merged
bluetech merged 1 commit intopytest-dev:mainfrom
ykadowak:refactor_atexit
May 3, 2026
Merged

Stop using atexit and add integration tests for tmpdir fixture#10546
bluetech merged 1 commit intopytest-dev:mainfrom
ykadowak:refactor_atexit

Conversation

@ykadowak
Copy link
Copy Markdown
Contributor

Closes #10545

Copy link
Copy Markdown
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it ts a good idea to make this boundary more clear 👍

lets try to go with existing tools from the stdlib and make sure we trigger all we need on errors as well

Comment thread src/_pytest/pathlib.py Outdated
Comment thread src/_pytest/tmpdir.py Outdated
# permissions, etc, in which case we ignore it.
rmtree(passed_dir, ignore_errors=True)

tmp_path_factory._lock_and_dir_removal_callback.execute()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i recall the atexit registration was done to ensure cleanup even when sessionfinish fails (rare, but can happen)

if we go with a exit-stack, we can still register its cleanup for atexit, but also enure we cleanly unwind it at session finish when possible

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i recall the atexit registration was done to ensure cleanup even when sessionfinish fails (rare, but can happen)

I wasn't aware of that situation. Now it's good I think.
58c5d50

Comment thread testing/test_tmpdir.py Outdated
Copy link
Copy Markdown
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with the exitstack this looks good to me now 👍

lets register a followup to turn make_numbered_dir_with_cleanup into a actual context manager that one can push into the exitstack

but before we do more exit stacks, i think we want to take a look for pytest-core about where and how to use exit stacks / context managers in future/for code cleanup

Copy link
Copy Markdown
Contributor Author

@ykadowak ykadowak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored some test code.
5e6ce8f

Comment thread testing/test_tmpdir.py
Comment on lines -122 to -140
def test_policy_failed_removes_basedir_when_all_passed(
self, pytester: Pytester
) -> None:
p = pytester.makepyfile(
"""
def test_1(tmp_path):
assert 0 == 0
"""
)

pytester.inline_run(p)
root = pytester._test_tmproot
for child in root.iterdir():
# This symlink will be deleted by cleanup_numbered_dir **after**
# the test finishes because it's triggered by atexit.
# So it has to be ignored here.
base_dir = filter(lambda x: not x.is_symlink(), child.iterdir())
# Check the base dir itself is gone
assert len(list(base_dir)) == 0
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this because it's now a duplicate of test_retention_count

@ykadowak
Copy link
Copy Markdown
Contributor Author

ykadowak commented Dec 3, 2022

lets register a followup to turn make_numbered_dir_with_cleanup into a actual context manager that one can push into the exitstack

I'm not really understanding the motivation of using context manager for it.
Does it change anything in this case to use ExitStack.enter_context over ExitStack.callback?

@bluetech
Copy link
Copy Markdown
Member

bluetech commented May 3, 2026

I like the idea of removing reliance on atexit. I rebased the PR on current main.

However, @RonnyPfannschmidt's first review mentioned that atexit was used as a fallback to remove the locks when even sessionfinish fails, an IIUC Ronny meant to use both the explicit ExitStack context manager, and atexit. But currently the PR only does the with _exit_stack.

@RonnyPfannschmidt did you mean the code should also register an exit stack close with atexit as a safety, or are you fine without it?

@RonnyPfannschmidt
Copy link
Copy Markdown
Member

I recall that sometimes its necessary

But registring the close for atexit is way cleaner than what we had

…ixture

Fix pytest-dev#10545

Co-authored-by: Ran Benita <ran@unusedvar.com>
@bluetech bluetech added skip news used on prs to opt out of the changelog requirement labels May 3, 2026
@bluetech bluetech force-pushed the refactor_atexit branch from 138ddf8 to 0263914 Compare May 3, 2026 19:01
@bluetech
Copy link
Copy Markdown
Member

bluetech commented May 3, 2026

Alright, added back a last-resort atexit, and made a few other tweaks to the PR. Can merge this now.

@bluetech bluetech merged commit 8f81c76 into pytest-dev:main May 3, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news used on prs to opt out of the changelog requirement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop using atexit and add integration tests for tmpdir fixture

3 participants