2 """pytest configuration
4 Extends output capture as needed by pybind11: ignore constructors, optional unordered lines.
5 Adds docstring and exceptions message sanitizers: ignore Python 2 vs 3 differences.
21 _unicode_marker = re.compile(
r"u(\'[^\']*\')")
22 _long_marker = re.compile(
r"([0-9])L")
23 _hexadecimal = re.compile(
r"0x[0-9a-fA-F]+")
28 collect_ignore.append(
"test_async.py")
31 def _strip_and_dedent(s):
32 """For triple-quote strings"""
33 return textwrap.dedent(s.lstrip(
"\n").rstrip())
36 def _split_and_sort(s):
37 """For output which does not require specific line order"""
38 return sorted(_strip_and_dedent(s).splitlines())
41 def _make_explanation(a, b):
42 """Explanation for a failed assert -- the a and b arguments are List[str]"""
43 return [
"--- actual / +++ expected"] + [
44 line.strip(
"\n")
for line
in difflib.ndiff(a, b)
49 """Basic output post-processing and comparison"""
62 for line
in self.
string.strip().splitlines()
63 if not line.startswith(
"###")
65 b = _strip_and_dedent(other).splitlines()
74 """Custom comparison for output without strict line ordering"""
77 a = _split_and_sort(self.
string)
78 b = _split_and_sort(other)
93 self.
capfd.readouterr()
112 return item
in self.
out
125 """Extended `capsys` with context manager and custom equality operators"""
141 b = _strip_and_dedent(other)
145 self.
explanation = _make_explanation(a.splitlines(), b.splitlines())
149 def _sanitize_general(s):
151 s = s.replace(
"pybind11_tests.",
"m.")
152 s = s.replace(
"unicode",
"str")
153 s = _long_marker.sub(
r"\1", s)
154 s = _unicode_marker.sub(
r"\1", s)
158 def _sanitize_docstring(thing):
160 s = _sanitize_general(s)
166 """Sanitize docstrings and add custom failure explanation"""
170 def _sanitize_message(thing):
172 s = _sanitize_general(s)
173 s = _hexadecimal.sub(
"0", s)
179 """Sanitize messages and add custom failure explanation"""
185 """Hook to insert custom failure explanation"""
186 if hasattr(left,
"explanation"):
187 return left.explanation
190 @contextlib.contextmanager
192 """Suppress the desired exception"""
200 """Run the garbage collector twice (needed when running
201 reference counting tests with PyPy)"""
207 pytest.suppress = suppress
208 pytest.gc_collect = gc_collect