mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-22 05:05:11 +00:00
6de6191a0c
* Try using `std::hash<std::type_index>`, `std::equal_to<std::type_index>` everywhere.
From PR #4316 we know that types in the unnamed namespace in different translation units do not compare equal, as desired.
But do types in named namespaces compare equal, as desired?
* Revert "Try using `std::hash<std::type_index>`, `std::equal_to<std::type_index>` everywhere."
This reverts commit a06949a926
.
* Use "our own name-based hash and equality functions" for `std::type_index` only under macOS, based on results shown under https://github.com/pybind/pybind11/pull/4316#issuecomment-1305097879
* Patch in PR #4313: Minimal reproducer for clash when binding types defined in the unnamed namespace.
* test_unnamed_namespace_b xfail for clang
* `PYBIND11_INTERNALS_VERSION 5`
* Add a note to docs/classes.rst
* For compatibility with Google-internal testing, test_unnamed_namespace_a & test_unnamed_namespace_b need to work when imported in any order.
* Trying "__GLIBCXX__ or Windows", based on observations from Google-internal testing.
* Try _LIBCPP_VERSION
* Account for libc++ behavior in tests and documentation.
* Adjust expectations for Windows Clang (and make code less redundant).
* Add WindowsClang to ci.yml
Added block transferred from PR #4321
* Add clang-latest to name that appears in the GitHub Actions web view.
* Tweak the note in classes.rst again.
* Add `pip install --upgrade pip`, Show env, cosmetic changes
Already tested under PR #4321
* Add macos_brew_install_llvm to ci.yml
Added block transferred from PR #4324
* `test_cross_module_exception_translator` xfail 'Homebrew Clang'
* Revert back to base version of .github/workflows/ci.yml (the ci.yml changes were merged under #4323 and #4326)
* Fixes for ruff
* Make updated condition in internals.h dependent on ABI version.
* Remove PYBIND11_TEST_OVERRIDE when testing with PYBIND11_INTERNALS_VERSION=10000000
* Selectively exercise cmake `-DPYBIND11_TEST_OVERRIDE`: ubuntu, macos, windows
Extra work added to quick jobs, based on timings below, to not increase the GHA start-to-last-job-finished time.
```
Duration
^ Number of pytest runs
^ ^ Job identifier
^ ^ ^
0:03:48.024227 1 1___3___Clang_3.6___C++11___x64.txt
0:03:58.992814 1 2___3___Clang_3.7___C++11___x64.txt
0:04:25.758942 1 1___3.7___Debian___x86____Install.txt
0:04:50.148276 1 4___3___Clang_7___C++11___x64.txt
0:04:55.784558 1 13___3___Clang_15___C++20___x64.txt
0:04:57.048754 1 6___3___Clang_dev___C++11___x64.txt
0:05:00.485181 1 7___3___Clang_5___C++14___x64.txt
0:05:03.744964 1 2___3___almalinux8___x64.txt
0:05:06.222752 1 5___3___Clang_9___C++11___x64.txt
0:05:11.767022 1 2___3___GCC_7___C++17__x64.txt
0:05:18.634930 1 2___3.11__deadsnakes____x64.txt
0:05:22.810995 1 1___3___GCC_7___C++11__x64.txt
0:05:25.275317 1 12___3___Clang_14___C++20___x64.txt
0:05:32.058174 1 5___3___GCC_10___C++17__x64.txt
0:05:39.381351 1 7___3___GCC_12___C++20__x64.txt
0:05:40.502252 1 8___3___Clang_10___C++17___x64.txt
0:05:59.344905 1 3___3___Clang_3.9___C++11___x64.txt
0:06:10.825147 1 6___3___GCC_11___C++20__x64.txt
0:06:20.655443 1 3___3___almalinux9___x64.txt
0:06:22.472061 1 3___3___GCC_8___C++14__x64.txt
0:06:42.647406 1 11___3___Clang_13___C++20___x64.txt
0:06:53.352720 1 1___3.10___CUDA_11.7___Ubuntu_22.04.txt
0:07:07.357801 1 2___3.7___MSVC_2019___x86_-DCMAKE_CXX_STANDARD=14.txt
0:07:09.057603 1 1___3___centos7___x64.txt
0:07:15.546282 1 1___3.8___MSVC_2019__Debug____x86_-DCMAKE_CXX_STANDARD=17.txt
0:07:22.566022 1 4___3___GCC_8___C++17__x64.txt
0:08:13.592674 1 2___3.9___MSVC_2019__Debug____x86_-DCMAKE_CXX_STANDARD=20.txt
0:08:16.422768 1 9___3___Clang_11___C++20___x64.txt
0:08:21.168457 1 3___3.8___MSVC_2019___x86_-DCMAKE_CXX_STANDARD=17.txt
0:08:27.129468 1 10___3___Clang_12___C++20___x64.txt
0:09:35.045470 1 1___3.10___windows-latest___clang-latest.txt
0:09:57.361843 1 1___3.9___MSVC_2022_C++20___x64.txt
0:10:35.187767 1 1___3.6___MSVC_2019___x86.txt
0:11:14.691200 4 2___3.9___ubuntu-20.04___x64.txt
0:11:37.701167 1 1_macos-latest___brew_install_llvm.txt
0:11:38.688299 4 4___3.11___ubuntu-20.04___x64.txt
0:11:52.720216 1 4___3.9___MSVC_2019___x86_-DCMAKE_CXX_STANDARD=20.txt
0:13:23.456591 4 6___pypy-3.8___ubuntu-20.04___x64_-DPYBIND11_FINDPYTHON=ON.txt
0:13:25.863592 2 1___3___ICC_latest___x64.txt
0:13:32.411758 3 9___3.9___windows-2022___x64.txt
0:13:45.473377 4 3___3.10___ubuntu-20.04___x64.txt
0:13:55.366447 4 5___pypy-3.7___ubuntu-20.04___x64.txt
0:13:57.969502 3 10___3.10___windows-2022___x64.txt
0:14:19.837475 3 11___3.11___windows-2022___x64.txt
0:14:33.316770 4 1___3.6___ubuntu-20.04___x64_-DPYBIND11_FINDPYTHON=ON_-DCMA.txt
0:15:34.449278 4 22___3.6___windows-2019___x64_-DPYBIND11_FINDPYTHON=ON.txt
0:16:25.189055 2 1___3.9-dbg__deadsnakes____Valgrind___x64.txt
0:17:20.956667 4 15___3.6___macos-latest___x64.txt
0:17:27.513891 4 23___3.9___windows-2019___x64.txt
0:17:58.783286 3 8___3.6___windows-2022___x64.txt
0:18:25.917828 4 7___pypy-3.9___ubuntu-20.04___x64.txt
0:19:17.399820 3 13___pypy-3.8___windows-2022___x64.txt
0:19:45.002122 3 12___pypy-3.7___windows-2022___x64.txt
0:20:03.201926 4 16___3.9___macos-latest___x64.txt
0:20:15.415178 4 17___3.10___macos-latest___x64.txt
0:20:20.263216 4 20___pypy-3.8___macos-latest___x64.txt
0:20:31.998226 3 1___3___windows-latest___mingw64.txt
0:20:40.812286 4 18___3.11___macos-latest___x64.txt
0:22:47.714749 4 19___pypy-3.7___macos-latest___x64.txt
0:23:04.435859 3 2___3___windows-latest___mingw32.txt
0:25:48.719597 3 14___pypy-3.9___windows-2022___x64.txt
0:26:01.211688 4 21___pypy-3.9___macos-latest___x64.txt
0:28:19.971015 1 1___3___CentOS7__PGI_22.9___x64.txt
```
* Update skipif for Python 3.12a7 (the WIP needs to be handled in a separate PR).
394 lines
13 KiB
Python
394 lines
13 KiB
Python
import sys
|
|
|
|
import pytest
|
|
|
|
import env
|
|
import pybind11_cross_module_tests as cm
|
|
import pybind11_tests # noqa: F401
|
|
from pybind11_tests import exceptions as m
|
|
|
|
|
|
def test_std_exception(msg):
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throw_std_exception()
|
|
assert msg(excinfo.value) == "This exception was intentionally thrown."
|
|
|
|
|
|
def test_error_already_set(msg):
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throw_already_set(False)
|
|
assert (
|
|
msg(excinfo.value)
|
|
== "Internal error: pybind11::error_already_set called while Python error indicator not set."
|
|
)
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
m.throw_already_set(True)
|
|
assert msg(excinfo.value) == "foo"
|
|
|
|
|
|
def test_raise_from(msg):
|
|
with pytest.raises(ValueError) as excinfo:
|
|
m.raise_from()
|
|
assert msg(excinfo.value) == "outer"
|
|
assert msg(excinfo.value.__cause__) == "inner"
|
|
|
|
|
|
def test_raise_from_already_set(msg):
|
|
with pytest.raises(ValueError) as excinfo:
|
|
m.raise_from_already_set()
|
|
assert msg(excinfo.value) == "outer"
|
|
assert msg(excinfo.value.__cause__) == "inner"
|
|
|
|
|
|
def test_cross_module_exceptions(msg):
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
cm.raise_runtime_error()
|
|
assert str(excinfo.value) == "My runtime error"
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
cm.raise_value_error()
|
|
assert str(excinfo.value) == "My value error"
|
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
cm.throw_pybind_value_error()
|
|
assert str(excinfo.value) == "pybind11 value error"
|
|
|
|
with pytest.raises(TypeError) as excinfo:
|
|
cm.throw_pybind_type_error()
|
|
assert str(excinfo.value) == "pybind11 type error"
|
|
|
|
with pytest.raises(StopIteration) as excinfo:
|
|
cm.throw_stop_iteration()
|
|
|
|
with pytest.raises(cm.LocalSimpleException) as excinfo:
|
|
cm.throw_local_simple_error()
|
|
assert msg(excinfo.value) == "external mod"
|
|
|
|
with pytest.raises(KeyError) as excinfo:
|
|
cm.throw_local_error()
|
|
# KeyError is a repr of the key, so it has an extra set of quotes
|
|
assert str(excinfo.value) == "'just local'"
|
|
|
|
|
|
# TODO: FIXME
|
|
@pytest.mark.xfail(
|
|
"env.MACOS and (env.PYPY or pybind11_tests.compiler_info.startswith('Homebrew Clang'))",
|
|
raises=RuntimeError,
|
|
reason="See Issue #2847, PR #2999, PR #4324",
|
|
)
|
|
def test_cross_module_exception_translator():
|
|
with pytest.raises(KeyError):
|
|
# translator registered in cross_module_tests
|
|
m.throw_should_be_translated_to_key_error()
|
|
|
|
|
|
def test_python_call_in_catch():
|
|
d = {}
|
|
assert m.python_call_in_destructor(d) is True
|
|
assert d["good"] is True
|
|
|
|
|
|
def ignore_pytest_unraisable_warning(f):
|
|
unraisable = "PytestUnraisableExceptionWarning"
|
|
if hasattr(pytest, unraisable): # Python >= 3.8 and pytest >= 6
|
|
dec = pytest.mark.filterwarnings(f"ignore::pytest.{unraisable}")
|
|
return dec(f)
|
|
return f
|
|
|
|
|
|
# TODO: find out why this fails on PyPy, https://foss.heptapod.net/pypy/pypy/-/issues/3583
|
|
@pytest.mark.xfail(env.PYPY, reason="Failure on PyPy 3.8 (7.3.7)", strict=False)
|
|
@ignore_pytest_unraisable_warning
|
|
def test_python_alreadyset_in_destructor(monkeypatch, capsys):
|
|
hooked = False
|
|
triggered = False
|
|
|
|
if hasattr(sys, "unraisablehook"): # Python 3.8+
|
|
hooked = True
|
|
# Don't take `sys.unraisablehook`, as that's overwritten by pytest
|
|
default_hook = sys.__unraisablehook__
|
|
|
|
def hook(unraisable_hook_args):
|
|
exc_type, exc_value, exc_tb, err_msg, obj = unraisable_hook_args
|
|
if obj == "already_set demo":
|
|
nonlocal triggered
|
|
triggered = True
|
|
default_hook(unraisable_hook_args)
|
|
return
|
|
|
|
# Use monkeypatch so pytest can apply and remove the patch as appropriate
|
|
monkeypatch.setattr(sys, "unraisablehook", hook)
|
|
|
|
assert m.python_alreadyset_in_destructor("already_set demo") is True
|
|
if hooked:
|
|
assert triggered is True
|
|
|
|
_, captured_stderr = capsys.readouterr()
|
|
assert captured_stderr.startswith("Exception ignored in: 'already_set demo'")
|
|
assert captured_stderr.rstrip().endswith("KeyError: 'bar'")
|
|
|
|
|
|
def test_exception_matches():
|
|
assert m.exception_matches()
|
|
assert m.exception_matches_base()
|
|
assert m.modulenotfound_exception_matches_base()
|
|
|
|
|
|
def test_custom(msg):
|
|
# Can we catch a MyException?
|
|
with pytest.raises(m.MyException) as excinfo:
|
|
m.throws1()
|
|
assert msg(excinfo.value) == "this error should go to a custom type"
|
|
|
|
# Can we translate to standard Python exceptions?
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throws2()
|
|
assert msg(excinfo.value) == "this error should go to a standard Python exception"
|
|
|
|
# Can we handle unknown exceptions?
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throws3()
|
|
assert msg(excinfo.value) == "Caught an unknown exception!"
|
|
|
|
# Can we delegate to another handler by rethrowing?
|
|
with pytest.raises(m.MyException) as excinfo:
|
|
m.throws4()
|
|
assert msg(excinfo.value) == "this error is rethrown"
|
|
|
|
# Can we fall-through to the default handler?
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throws_logic_error()
|
|
assert (
|
|
msg(excinfo.value) == "this error should fall through to the standard handler"
|
|
)
|
|
|
|
# OverFlow error translation.
|
|
with pytest.raises(OverflowError) as excinfo:
|
|
m.throws_overflow_error()
|
|
|
|
# Can we handle a helper-declared exception?
|
|
with pytest.raises(m.MyException5) as excinfo:
|
|
m.throws5()
|
|
assert msg(excinfo.value) == "this is a helper-defined translated exception"
|
|
|
|
# Exception subclassing:
|
|
with pytest.raises(m.MyException5) as excinfo:
|
|
m.throws5_1()
|
|
assert msg(excinfo.value) == "MyException5 subclass"
|
|
assert isinstance(excinfo.value, m.MyException5_1)
|
|
|
|
with pytest.raises(m.MyException5_1) as excinfo:
|
|
m.throws5_1()
|
|
assert msg(excinfo.value) == "MyException5 subclass"
|
|
|
|
with pytest.raises(m.MyException5) as excinfo: # noqa: PT012
|
|
try:
|
|
m.throws5()
|
|
except m.MyException5_1 as err:
|
|
raise RuntimeError("Exception error: caught child from parent") from err
|
|
assert msg(excinfo.value) == "this is a helper-defined translated exception"
|
|
|
|
|
|
def test_nested_throws(capture):
|
|
"""Tests nested (e.g. C++ -> Python -> C++) exception handling"""
|
|
|
|
def throw_myex():
|
|
raise m.MyException("nested error")
|
|
|
|
def throw_myex5():
|
|
raise m.MyException5("nested error 5")
|
|
|
|
# In the comments below, the exception is caught in the first step, thrown in the last step
|
|
|
|
# C++ -> Python
|
|
with capture:
|
|
m.try_catch(m.MyException5, throw_myex5)
|
|
assert str(capture).startswith("MyException5: nested error 5")
|
|
|
|
# Python -> C++ -> Python
|
|
with pytest.raises(m.MyException) as excinfo:
|
|
m.try_catch(m.MyException5, throw_myex)
|
|
assert str(excinfo.value) == "nested error"
|
|
|
|
def pycatch(exctype, f, *args): # noqa: ARG001
|
|
try:
|
|
f(*args)
|
|
except m.MyException as e:
|
|
print(e)
|
|
|
|
# C++ -> Python -> C++ -> Python
|
|
with capture:
|
|
m.try_catch(
|
|
m.MyException5,
|
|
pycatch,
|
|
m.MyException,
|
|
m.try_catch,
|
|
m.MyException,
|
|
throw_myex5,
|
|
)
|
|
assert str(capture).startswith("MyException5: nested error 5")
|
|
|
|
# C++ -> Python -> C++
|
|
with capture:
|
|
m.try_catch(m.MyException, pycatch, m.MyException5, m.throws4)
|
|
assert capture == "this error is rethrown"
|
|
|
|
# Python -> C++ -> Python -> C++
|
|
with pytest.raises(m.MyException5) as excinfo:
|
|
m.try_catch(m.MyException, pycatch, m.MyException, m.throws5)
|
|
assert str(excinfo.value) == "this is a helper-defined translated exception"
|
|
|
|
|
|
def test_throw_nested_exception():
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throw_nested_exception()
|
|
assert str(excinfo.value) == "Outer Exception"
|
|
assert str(excinfo.value.__cause__) == "Inner Exception"
|
|
|
|
|
|
# This can often happen if you wrap a pybind11 class in a Python wrapper
|
|
def test_invalid_repr():
|
|
class MyRepr:
|
|
def __repr__(self):
|
|
raise AttributeError("Example error")
|
|
|
|
with pytest.raises(TypeError):
|
|
m.simple_bool_passthrough(MyRepr())
|
|
|
|
|
|
def test_local_translator(msg):
|
|
"""Tests that a local translator works and that the local translator from
|
|
the cross module is not applied"""
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throws6()
|
|
assert msg(excinfo.value) == "MyException6 only handled in this module"
|
|
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.throws_local_error()
|
|
assert not isinstance(excinfo.value, KeyError)
|
|
assert msg(excinfo.value) == "never caught"
|
|
|
|
with pytest.raises(Exception) as excinfo:
|
|
m.throws_local_simple_error()
|
|
assert not isinstance(excinfo.value, cm.LocalSimpleException)
|
|
assert msg(excinfo.value) == "this mod"
|
|
|
|
|
|
def test_error_already_set_message_with_unicode_surrogate(): # Issue #4288
|
|
assert m.error_already_set_what(RuntimeError, "\ud927") == (
|
|
"RuntimeError: \\ud927",
|
|
False,
|
|
)
|
|
|
|
|
|
def test_error_already_set_message_with_malformed_utf8():
|
|
assert m.error_already_set_what(RuntimeError, b"\x80") == (
|
|
"RuntimeError: b'\\x80'",
|
|
False,
|
|
)
|
|
|
|
|
|
class FlakyException(Exception):
|
|
def __init__(self, failure_point):
|
|
if failure_point == "failure_point_init":
|
|
raise ValueError("triggered_failure_point_init")
|
|
self.failure_point = failure_point
|
|
|
|
def __str__(self):
|
|
if self.failure_point == "failure_point_str":
|
|
raise ValueError("triggered_failure_point_str")
|
|
return "FlakyException.__str__"
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("exc_type", "exc_value", "expected_what"),
|
|
[
|
|
(ValueError, "plain_str", "ValueError: plain_str"),
|
|
(ValueError, ("tuple_elem",), "ValueError: tuple_elem"),
|
|
(FlakyException, ("happy",), "FlakyException: FlakyException.__str__"),
|
|
],
|
|
)
|
|
def test_error_already_set_what_with_happy_exceptions(
|
|
exc_type, exc_value, expected_what
|
|
):
|
|
what, py_err_set_after_what = m.error_already_set_what(exc_type, exc_value)
|
|
assert not py_err_set_after_what
|
|
assert what == expected_what
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
# Intentionally very specific:
|
|
"sys.version_info == (3, 12, 0, 'alpha', 7)",
|
|
reason="WIP: https://github.com/python/cpython/issues/102594",
|
|
)
|
|
@pytest.mark.skipif("env.PYPY", reason="PyErr_NormalizeException Segmentation fault")
|
|
def test_flaky_exception_failure_point_init():
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.error_already_set_what(FlakyException, ("failure_point_init",))
|
|
lines = str(excinfo.value).splitlines()
|
|
# PyErr_NormalizeException replaces the original FlakyException with ValueError:
|
|
assert lines[:3] == [
|
|
"pybind11::error_already_set: MISMATCH of original and normalized active exception types:"
|
|
" ORIGINAL FlakyException REPLACED BY ValueError: triggered_failure_point_init",
|
|
"",
|
|
"At:",
|
|
]
|
|
# Checking the first two lines of the traceback as formatted in error_string():
|
|
assert "test_exceptions.py(" in lines[3]
|
|
assert lines[3].endswith("): __init__")
|
|
assert lines[4].endswith("): test_flaky_exception_failure_point_init")
|
|
|
|
|
|
def test_flaky_exception_failure_point_str():
|
|
what, py_err_set_after_what = m.error_already_set_what(
|
|
FlakyException, ("failure_point_str",)
|
|
)
|
|
assert not py_err_set_after_what
|
|
lines = what.splitlines()
|
|
n = 3 if env.PYPY and len(lines) == 3 else 5
|
|
assert (
|
|
lines[:n]
|
|
== [
|
|
"FlakyException: <MESSAGE UNAVAILABLE DUE TO ANOTHER EXCEPTION>",
|
|
"",
|
|
"MESSAGE UNAVAILABLE DUE TO EXCEPTION: ValueError: triggered_failure_point_str",
|
|
"",
|
|
"At:",
|
|
][:n]
|
|
)
|
|
|
|
|
|
def test_cross_module_interleaved_error_already_set():
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.test_cross_module_interleaved_error_already_set()
|
|
assert str(excinfo.value) in (
|
|
"2nd error.", # Almost all platforms.
|
|
"RuntimeError: 2nd error.", # Some PyPy builds (seen under macOS).
|
|
)
|
|
|
|
|
|
def test_error_already_set_double_restore():
|
|
m.test_error_already_set_double_restore(True) # dry_run
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.test_error_already_set_double_restore(False)
|
|
assert str(excinfo.value) == (
|
|
"Internal error: pybind11::detail::error_fetch_and_normalize::restore()"
|
|
" called a second time. ORIGINAL ERROR: ValueError: Random error."
|
|
)
|
|
|
|
|
|
def test_pypy_oserror_normalization():
|
|
# https://github.com/pybind/pybind11/issues/4075
|
|
what = m.test_pypy_oserror_normalization()
|
|
assert "this_filename_must_not_exist" in what
|
|
|
|
|
|
def test_fn_cast_int_exception():
|
|
with pytest.raises(RuntimeError) as excinfo:
|
|
m.test_fn_cast_int(lambda: None)
|
|
|
|
assert str(excinfo.value).startswith(
|
|
"Unable to cast Python instance of type <class 'NoneType'> to C++ type"
|
|
)
|