Skip to content

FIX use safe_getattr instead of getattr for Python 3.9 compat #11883

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ Gregory Lee
Grig Gheorghiu
Grigorii Eremeev (budulianin)
Guido Wesdorp
Guillaume Lemaitre
Guoqiang Zhang
Harald Armin Massa
Harshna
Expand Down
1 change: 1 addition & 0 deletions changelog/11872.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Correctly handle `HTTPError` in :func:`pytest.raises` for Python <= 3.9.
3 changes: 2 additions & 1 deletion src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
from _pytest._io.saferepr import safeformat
from _pytest._io.saferepr import saferepr
from _pytest.compat import get_real_func
from _pytest.compat import safe_getattr
from _pytest.deprecated import check_ispytest
from _pytest.pathlib import absolutepath
from _pytest.pathlib import bestrelpath
Expand Down Expand Up @@ -702,7 +703,7 @@ def _stringify_exception(self, exc: BaseException) -> str:
return "\n".join(
[
str(exc),
*getattr(exc, "__notes__", []),
*safe_getattr(exc, "__notes__", []),
]
)

Expand Down
12 changes: 12 additions & 0 deletions testing/code/test_excinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from pathlib import Path
from typing import Any
from typing import TYPE_CHECKING
from urllib.error import HTTPError

import _pytest._code
import pytest
Expand Down Expand Up @@ -1790,3 +1791,14 @@ def test_check_error_notes_failure(
with pytest.raises(AssertionError):
with pytest.raises(type(error), match=match):
raise error


def test_notes_getattr_keyerror():
"""Non-regression test for #11872.

HTTPError in Python < 3.10 was subclassing tempfile._TemporaryFileWrapper that
was not properly initialize and not exposing a `__file__` attribute:
https://github.com/python/cpython/issues/98778
"""
with pytest.raises(HTTPError, match="Not Found"):
raise HTTPError(code=404, msg="Not Found", fp=None, hdrs=None, url="")