Skip to content
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.

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

8.1.1 removed ability to introspect teardown exceptions in pytest_fixture_post_finalizer #12306

Open
mchase-nasuni opened this issue May 10, 2024 · 2 comments

Comments

@mchase-nasuni
Copy link

mchase-nasuni commented May 10, 2024

From several years ago until recently (works in 8.0.2, doesn't work in 8.1.1), it was possible to get teardown exception information in pytest_fixture_post_finalizer by calling sys.exc_info(). This appears to have changed with the following commit: 434282e#diff-49027cfd80e14edac9b0fae71f7228a408a09599f66a7815839ce8c3ae2ab84fL1020-R1043, which moved the hook call from inside the finally block to before the exception is (re)raised.

Perhaps there's another way to get at this information, but I wasn't able to find it by reading documentation, searching the web, or trying to read the source code. I was able to make things work again by somewhat rearranging the new code.

This was first encountered in CI images that I believe were based off of Ubuntu 22.04, but I was able to reproduce on macOS Monterey.

Here is the code for my attempt at a minimal example:

conftest.py

import sys

import pytest


@pytest.fixture(autouse=True)
def _fail_eventually():
    yield
    raise RuntimeError


def pytest_fixture_post_finalizer():
    exc_info = sys.exc_info()
    if exc_info != (None, None, None):
        print("Saw exception!")
    else:
        print("Didn't see exception")

test_repro.py

def test_pass():
    pass

Against 8.0.2, this prints Saw exception!, and pip list has the following output:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.0.2
setuptools 65.5.0

Against 8.1.1, this prints Didn't see exception, and pip list has the following output:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.1.1
setuptools 65.5.0

Against 8.2.0, this still prints Didn't see exception, and pip list has the following output:

Package    Version
---------- -------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     8.2.0
setuptools 65.5.0

Against a branch I created in a fork of pytest to check that I'm right about this issue (main...mchase-nasuni:pytest:mchase-nasuni-internal-testing), this prints Saw exception!, and pip list has the following output:

Package    Version
---------- ---------------------
iniconfig  2.0.0
packaging  24.0
pip        24.0
pluggy     1.5.0
pytest     0.1.dev15909+g0692015
setuptools 65.5.0
@axelande

This comment was marked as off-topic.

@The-Compiler
Copy link
Member

@axelande This is completely unrelated, and happens because PTVS is accessing private pytest API which was moved in 3ba4095.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants