GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — runner: correctly cleanup item _request/funcargs if an exception was reraised during call (e.g. KeyboardInterrupt) by mon · Pull Request #13626 · pytest-dev/pytest

runner: correctly cleanup item _request/funcargs if an exception was reraised during call (e.g. KeyboardInterrupt) #13626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Sign up for GitHub

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
bluetech merged 1 commit into pytest-dev:main from mon:insufficient_cleanup_on_keyboardinterrupt
Apr 12, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

runner: correctly cleanup item _request/funcargs if an exception was reraised during call (e.g. KeyboardInterrupt) #13626

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
1 commit
Select commit
338851a
runner: correctly cleanup item _request/funcargs if an exception was …
mon Jul 30, 2025
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view

There are no files selected for viewing

2 changes: 2 additions & 0 deletions 2 changelog/13626.bugfix.rst
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed function-scoped fixture values being kept alive after a test was interrupted by ``KeyboardInterrupt`` or early exit,
allowing them to potentially be released more promptly.
36 changes: 19 additions & 17 deletions 36 src/_pytest/runner.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -128,23 +128,25 @@ def runtestprotocol(
# This only happens if the item is re-run, as is done by
# pytest-rerunfailures.
item._initrequest() # type: ignore[attr-defined]
rep = call_and_report(item, "setup", log)
reports = [rep]
if rep.passed:
if item.config.getoption("setupshow", False):
show_test_item(item)
if not item.config.getoption("setuponly", False):
reports.append(call_and_report(item, "call", log))
# If the session is about to fail or stop, teardown everything - this is
# necessary to correctly report fixture teardown errors (see #11706)
if item.session.shouldfail or item.session.shouldstop:
nextitem = None
reports.append(call_and_report(item, "teardown", log, nextitem=nextitem))
# After all teardown hooks have been called
# want funcargs and request info to go away.
if hasrequest:
item._request = False # type: ignore[attr-defined]
item.funcargs = None # type: ignore[attr-defined]
try:
rep = call_and_report(item, "setup", log)
reports = [rep]
if rep.passed:
if item.config.getoption("setupshow", False):
show_test_item(item)
if not item.config.getoption("setuponly", False):
reports.append(call_and_report(item, "call", log))
# If the session is about to fail or stop, teardown everything - this is
# necessary to correctly report fixture teardown errors (see #11706)
if item.session.shouldfail or item.session.shouldstop:
nextitem = None
reports.append(call_and_report(item, "teardown", log, nextitem=nextitem))
finally:
# After all teardown hooks have been called (or an exception was reraised)
# want funcargs and request info to go away.
if hasrequest:
item._request = False # type: ignore[attr-defined]
item.funcargs = None # type: ignore[attr-defined]
return reports


Expand Down
32 changes: 32 additions & 0 deletions 32 testing/test_runner.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
import sys
import types
from typing import cast

from _pytest import outcomes
from _pytest import reports
Expand Down Expand Up @@ -494,6 +495,37 @@ def test_func():
else:
assert False, "did not raise"

def test_keyboardinterrupt_clears_request_and_funcargs(
self, pytester: Pytester
) -> None:
"""Ensure that an item's fixtures are cleared quickly even if exiting
early due to a keyboard interrupt (#13626)."""
item = pytester.getitem(
"""
import pytest

@pytest.fixture
def resource():
return object()

def test_func(resource):
raise KeyboardInterrupt("fake")
"""
)
assert isinstance(item, pytest.Function)
assert item._request
assert item.funcargs == {}

try:
runner.runtestprotocol(item, log=False)
except KeyboardInterrupt:
pass
else:
assert False, "did not raise"

assert not cast(object, item._request)
assert not item.funcargs


class TestSessionReports:
def test_collect_result(self, pytester: Pytester) -> None:
Expand Down
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.