|
10 | 10 | import os |
11 | 11 | from pathlib import Path |
12 | 12 | import py_compile |
| 13 | +import re |
13 | 14 | import stat |
14 | 15 | import sys |
15 | 16 | import textwrap |
|
24 | 25 | from _pytest.assertion import util |
25 | 26 | from _pytest.assertion.rewrite import _get_assertion_exprs |
26 | 27 | from _pytest.assertion.rewrite import _get_maxsize_for_saferepr |
| 28 | +from _pytest.assertion.rewrite import _saferepr |
27 | 29 | from _pytest.assertion.rewrite import AssertionRewritingHook |
28 | 30 | from _pytest.assertion.rewrite import get_cache_dir |
29 | 31 | from _pytest.assertion.rewrite import PYC_TAIL |
@@ -2036,7 +2038,9 @@ def test_foo(): |
2036 | 2038 | assert test_foo_pyc.is_file() |
2037 | 2039 |
|
2038 | 2040 | # normal file: not touched by pytest, normal cache tag |
2039 | | - bar_init_pyc = get_cache_dir(bar_init) / f"__init__.{sys.implementation.cache_tag}.pyc" |
| 2041 | + bar_init_pyc = ( |
| 2042 | + get_cache_dir(bar_init) / f"__init__.{sys.implementation.cache_tag}.pyc" |
| 2043 | + ) |
2040 | 2044 | assert bar_init_pyc.is_file() |
2041 | 2045 |
|
2042 | 2046 |
|
@@ -2103,3 +2107,26 @@ def test_foo(): |
2103 | 2107 | ) |
2104 | 2108 | result = pytester.runpytest() |
2105 | 2109 | assert result.ret == 0 |
| 2110 | + |
| 2111 | + |
| 2112 | +class TestSafereprUnbounded: |
| 2113 | + class Help: |
| 2114 | + def bound_method(self): # pragma: no cover |
| 2115 | + pass |
| 2116 | + |
| 2117 | + def test_saferepr_bound_method(self): |
| 2118 | + """saferepr() of a bound method should show only the method name""" |
| 2119 | + assert _saferepr(self.Help().bound_method) == "bound_method" |
| 2120 | + |
| 2121 | + def test_saferepr_unbounded(self): |
| 2122 | + """saferepr() of an unbound method should still show the full information""" |
| 2123 | + obj = self.Help() |
| 2124 | + # using id() to fetch memory address fails on different platforms |
| 2125 | + pattern = re.compile( |
| 2126 | + rf"<{Path(__file__).stem}.{self.__class__.__name__}.Help object at 0x[0-9a-fA-F]*>", |
| 2127 | + ) |
| 2128 | + assert pattern.match(_saferepr(obj)) |
| 2129 | + assert ( |
| 2130 | + _saferepr(self.Help) |
| 2131 | + == f"<class '{Path(__file__).stem}.{self.__class__.__name__}.Help'>" |
| 2132 | + ) |
0 commit comments