[8.0.x] BUG: fix an edge case where ExceptionInfo._stringify_exception could crash pytest.raises

This commit is contained in:
Clément Robert
2024-01-30 16:20:30 +01:00
committed by pytest bot
parent 3e48ef64cd
commit eb698a64a0
4 changed files with 27 additions and 1 deletions

View File

@@ -698,10 +698,21 @@ class ExceptionInfo(Generic[E]):
return fmt.repr_excinfo(self)
def _stringify_exception(self, exc: BaseException) -> str:
try:
notes = getattr(exc, "__notes__", [])
except KeyError:
# Workaround for https://github.com/python/cpython/issues/98778 on
# Python <= 3.9, and some 3.10 and 3.11 patch versions.
HTTPError = getattr(sys.modules.get("urllib.error", None), "HTTPError", ())
if sys.version_info[:2] <= (3, 11) and isinstance(exc, HTTPError):
notes = []
else:
raise
return "\n".join(
[
str(exc),
*getattr(exc, "__notes__", []),
*notes,
]
)