2016-08-12 11:50:00 +00:00
|
|
|
"""pytest configuration
|
|
|
|
|
|
|
|
Extends output capture as needed by pybind11: ignore constructors, optional unordered lines.
|
2022-02-11 02:28:08 +00:00
|
|
|
Adds docstring and exceptions message sanitizers.
|
2016-08-12 11:50:00 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import contextlib
|
2020-08-16 20:02:12 +00:00
|
|
|
import difflib
|
2016-12-16 14:00:46 +00:00
|
|
|
import gc
|
2022-11-23 01:17:02 +00:00
|
|
|
import multiprocessing
|
|
|
|
import os
|
2020-08-16 20:02:12 +00:00
|
|
|
import re
|
|
|
|
import textwrap
|
2023-01-14 21:47:56 +00:00
|
|
|
import traceback
|
2020-08-16 20:02:12 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
# Early diagnostic for failed imports
|
2023-01-14 21:47:56 +00:00
|
|
|
try:
|
|
|
|
import pybind11_tests
|
|
|
|
except Exception:
|
|
|
|
# pytest does not show the traceback without this.
|
|
|
|
traceback.print_exc()
|
|
|
|
raise
|
2016-08-12 11:50:00 +00:00
|
|
|
|
2022-12-01 20:15:47 +00:00
|
|
|
|
|
|
|
@pytest.fixture(scope="session", autouse=True)
|
|
|
|
def always_forkserver_on_unix():
|
|
|
|
if os.name == "nt":
|
|
|
|
return
|
|
|
|
|
2022-11-23 01:17:02 +00:00
|
|
|
# Full background: https://github.com/pybind/pybind11/issues/4105#issuecomment-1301004592
|
|
|
|
# In a nutshell: fork() after starting threads == flakiness in the form of deadlocks.
|
|
|
|
# It is actually a well-known pitfall, unfortunately without guard rails.
|
|
|
|
# "forkserver" is more performant than "spawn" (~9s vs ~13s for tests/test_gil_scoped.py,
|
|
|
|
# visit the issuecomment link above for details).
|
|
|
|
# Windows does not have fork() and the associated pitfall, therefore it is best left
|
|
|
|
# running with defaults.
|
|
|
|
multiprocessing.set_start_method("forkserver")
|
|
|
|
|
2022-12-01 20:15:47 +00:00
|
|
|
|
2020-10-16 20:38:13 +00:00
|
|
|
_long_marker = re.compile(r"([0-9])L")
|
|
|
|
_hexadecimal = re.compile(r"0x[0-9a-fA-F]+")
|
2016-08-12 11:50:00 +00:00
|
|
|
|
2020-08-19 17:11:57 +00:00
|
|
|
# Avoid collecting Python3 only files
|
|
|
|
collect_ignore = []
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
def _strip_and_dedent(s):
|
|
|
|
"""For triple-quote strings"""
|
2020-10-16 20:38:13 +00:00
|
|
|
return textwrap.dedent(s.lstrip("\n").rstrip())
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _split_and_sort(s):
|
|
|
|
"""For output which does not require specific line order"""
|
|
|
|
return sorted(_strip_and_dedent(s).splitlines())
|
|
|
|
|
|
|
|
|
|
|
|
def _make_explanation(a, b):
|
|
|
|
"""Explanation for a failed assert -- the a and b arguments are List[str]"""
|
2020-10-16 20:38:13 +00:00
|
|
|
return ["--- actual / +++ expected"] + [
|
|
|
|
line.strip("\n") for line in difflib.ndiff(a, b)
|
|
|
|
]
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
class Output:
|
2016-08-12 11:50:00 +00:00
|
|
|
"""Basic output post-processing and comparison"""
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
def __init__(self, string):
|
|
|
|
self.string = string
|
|
|
|
self.explanation = []
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.string
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
# Ignore constructor/destructor output which is prefixed with "###"
|
2020-10-16 20:38:13 +00:00
|
|
|
a = [
|
|
|
|
line
|
|
|
|
for line in self.string.strip().splitlines()
|
|
|
|
if not line.startswith("###")
|
|
|
|
]
|
2016-08-12 11:50:00 +00:00
|
|
|
b = _strip_and_dedent(other).splitlines()
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = _make_explanation(a, b)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class Unordered(Output):
|
|
|
|
"""Custom comparison for output without strict line ordering"""
|
2020-10-16 20:38:13 +00:00
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
def __eq__(self, other):
|
|
|
|
a = _split_and_sort(self.string)
|
|
|
|
b = _split_and_sort(other)
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = _make_explanation(a, b)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
class Capture:
|
2016-08-12 11:50:00 +00:00
|
|
|
def __init__(self, capfd):
|
|
|
|
self.capfd = capfd
|
|
|
|
self.out = ""
|
2016-08-29 16:03:34 +00:00
|
|
|
self.err = ""
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
2016-09-06 22:50:10 +00:00
|
|
|
self.capfd.readouterr()
|
2016-08-12 11:50:00 +00:00
|
|
|
return self
|
|
|
|
|
2019-02-04 16:09:47 +00:00
|
|
|
def __exit__(self, *args):
|
2016-09-06 22:50:10 +00:00
|
|
|
self.out, self.err = self.capfd.readouterr()
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
a = Output(self.out)
|
|
|
|
b = other
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = a.explanation
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.out
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.out
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unordered(self):
|
|
|
|
return Unordered(self.out)
|
|
|
|
|
2016-08-29 16:03:34 +00:00
|
|
|
@property
|
|
|
|
def stderr(self):
|
|
|
|
return Output(self.err)
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
@pytest.fixture
|
2017-03-10 14:42:42 +00:00
|
|
|
def capture(capsys):
|
|
|
|
"""Extended `capsys` with context manager and custom equality operators"""
|
|
|
|
return Capture(capsys)
|
2016-08-12 11:50:00 +00:00
|
|
|
|
|
|
|
|
2022-02-11 02:28:08 +00:00
|
|
|
class SanitizedString:
|
2016-08-12 11:50:00 +00:00
|
|
|
def __init__(self, sanitizer):
|
|
|
|
self.sanitizer = sanitizer
|
|
|
|
self.string = ""
|
|
|
|
self.explanation = []
|
|
|
|
|
|
|
|
def __call__(self, thing):
|
|
|
|
self.string = self.sanitizer(thing)
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
a = self.string
|
|
|
|
b = _strip_and_dedent(other)
|
|
|
|
if a == b:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
self.explanation = _make_explanation(a.splitlines(), b.splitlines())
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_general(s):
|
|
|
|
s = s.strip()
|
|
|
|
s = s.replace("pybind11_tests.", "m.")
|
|
|
|
s = _long_marker.sub(r"\1", s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_docstring(thing):
|
|
|
|
s = thing.__doc__
|
|
|
|
s = _sanitize_general(s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def doc():
|
|
|
|
"""Sanitize docstrings and add custom failure explanation"""
|
|
|
|
return SanitizedString(_sanitize_docstring)
|
|
|
|
|
|
|
|
|
|
|
|
def _sanitize_message(thing):
|
|
|
|
s = str(thing)
|
|
|
|
s = _sanitize_general(s)
|
|
|
|
s = _hexadecimal.sub("0", s)
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def msg():
|
|
|
|
"""Sanitize messages and add custom failure explanation"""
|
|
|
|
return SanitizedString(_sanitize_message)
|
|
|
|
|
|
|
|
|
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def pytest_assertrepr_compare(op, left, right):
|
|
|
|
"""Hook to insert custom failure explanation"""
|
2020-10-16 20:38:13 +00:00
|
|
|
if hasattr(left, "explanation"):
|
2016-08-12 11:50:00 +00:00
|
|
|
return left.explanation
|
|
|
|
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
|
|
|
def suppress(exception):
|
|
|
|
"""Suppress the desired exception"""
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
except exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2016-12-16 14:00:46 +00:00
|
|
|
def gc_collect():
|
2020-10-16 20:38:13 +00:00
|
|
|
"""Run the garbage collector twice (needed when running
|
|
|
|
reference counting tests with PyPy)"""
|
2016-12-16 14:00:46 +00:00
|
|
|
gc.collect()
|
|
|
|
gc.collect()
|
|
|
|
|
|
|
|
|
2019-01-23 13:22:39 +00:00
|
|
|
def pytest_configure():
|
|
|
|
pytest.suppress = suppress
|
|
|
|
pytest.gc_collect = gc_collect
|
2022-07-08 00:51:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def pytest_report_header(config):
|
|
|
|
del config # Unused.
|
|
|
|
assert (
|
|
|
|
pybind11_tests.compiler_info is not None
|
|
|
|
), "Please update pybind11_tests.cpp if this assert fails."
|
|
|
|
return (
|
|
|
|
"C++ Info:"
|
|
|
|
f" {pybind11_tests.compiler_info}"
|
|
|
|
f" {pybind11_tests.cpp_std}"
|
|
|
|
f" {pybind11_tests.PYBIND11_INTERNALS_ID}"
|
Add `PYBIND11_SIMPLE_GIL_MANAGEMENT` option (cmake, C++ define) (#4216)
* Add option to force the use of the PYPY GIL scoped acquire/release logic to support nested gil access, see https://github.com/pybind/pybind11/issues/1276 and https://github.com/pytorch/pytorch/issues/83101
* Apply suggestions from code review
* Update CMakeLists.txt
* docs: update upgrade guide
* Update docs/upgrade.rst
* All bells & whistles.
* Add Reminder to common.h, so that we will not forget to purge `!WITH_THREAD` branches when dropping Python 3.6
* New sentence instead of semicolon.
* Temporarily pull in snapshot of PR #4246
* Add `test_release_acquire`
* Add more unit tests for nested gil locking
* Add test_report_builtins_internals_keys
* Very minor enhancement: sort list only after filtering.
* Revert change in docs/upgrade.rst
* Add test_multi_acquire_release_cross_module, while also forcing unique PYBIND11_INTERNALS_VERSION for cross_module_gil_utils.cpp
* Hopefully fix apparently new ICC error.
```
2022-10-28T07:57:54.5187728Z -- The CXX compiler identification is Intel 2021.7.0.20220726
...
2022-10-28T07:58:53.6758994Z icpc: remark #10441: The Intel(R) C++ Compiler Classic (ICC) is deprecated and will be removed from product release in the second half of 2023. The Intel(R) oneAPI DPC++/C++ Compiler (ICX) is the recommended compiler moving forward. Please transition to use this compiler. Use '-diag-disable=10441' to disable this message.
2022-10-28T07:58:54.5801597Z In file included from /home/runner/work/pybind11/pybind11/include/pybind11/detail/../detail/type_caster_base.h(15),
2022-10-28T07:58:54.5803794Z from /home/runner/work/pybind11/pybind11/include/pybind11/detail/../cast.h(15),
2022-10-28T07:58:54.5805740Z from /home/runner/work/pybind11/pybind11/include/pybind11/detail/../attr.h(14),
2022-10-28T07:58:54.5809556Z from /home/runner/work/pybind11/pybind11/include/pybind11/detail/class.h(12),
2022-10-28T07:58:54.5812154Z from /home/runner/work/pybind11/pybind11/include/pybind11/pybind11.h(13),
2022-10-28T07:58:54.5948523Z from /home/runner/work/pybind11/pybind11/tests/cross_module_gil_utils.cpp(13):
2022-10-28T07:58:54.5949009Z /home/runner/work/pybind11/pybind11/include/pybind11/detail/../detail/internals.h(177): error #2282: unrecognized GCC pragma
2022-10-28T07:58:54.5949374Z PYBIND11_TLS_KEY_INIT(tstate)
2022-10-28T07:58:54.5949579Z ^
2022-10-28T07:58:54.5949695Z
```
* clang-tidy fixes
* Workaround for PYPY WIN exitcode None
* Revert "Temporarily pull in snapshot of PR #4246"
This reverts commit 23ac16e859150f27fda25ca865cabcb4444e0770.
* Another workaround for PYPY WIN exitcode None
* Clean up how the tests are run "run in process" Part 1: uniformity
* Clean up how the tests are run "run in process" Part 2: use `@pytest.mark.parametrize` and clean up the naming.
* Skip some tests `#if defined(THREAD_SANITIZER)` (tested with TSAN using the Google-internal toolchain).
* Run all tests again but ignore ThreadSanitizer exitcode 66 (this is less likely to mask unrelated ThreadSanitizer issues in the future).
* bug fix: missing common.h include before using `PYBIND11_SIMPLE_GIL_MANAGEMENT`
For the tests in the github CI this does not matter, because
`PYBIND11_SIMPLE_GIL_MANAGEMENT` is always defined from the command line,
but when monkey-patching common.h locally, it matters.
* if process.exitcode is None: assert t_delta > 9.9
* More sophisiticated `_run_in_process()` implementation, clearly reporting `DEADLOCK`, additionally exercised via added `intentional_deadlock()`
* Wrap m.intentional_deadlock in a Python function, for `ForkingPickler` compatibility.
```
> ForkingPickler(file, protocol).dump(obj)
E TypeError: cannot pickle 'PyCapsule' object
```
Observed with all Windows builds including mingw but not PyPy, and macos-latest with Python 3.9, 3.10, 3.11 but not 3.6.
* Add link to potential solution for WOULD-BE-NICE-TO-HAVE feature.
* Add `SKIP_IF_DEADLOCK = True` option, to not pollute the CI results with expected `DEADLOCK` failures while we figure out what to do about them.
* Add COPY-PASTE-THIS: gdb ... command (to be used for debugging the detected deadlock)
* style: pre-commit fixes
* Do better than automatic pre-commit fixes.
* Add `PYBIND11_SIMPLE_GIL_MANAGEMENT` to `pytest_report_header()` (so that we can easily know when harvesting deadlock information from the CI logs).
Co-authored-by: Arnim Balzer <arnim@seechange.ai>
Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: Ralf W. Grosse-Kunstleve <rwgk@google.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2022-10-30 15:57:23 +00:00
|
|
|
f" PYBIND11_SIMPLE_GIL_MANAGEMENT={pybind11_tests.PYBIND11_SIMPLE_GIL_MANAGEMENT}"
|
2022-07-08 00:51:44 +00:00
|
|
|
)
|