| 1 |
import py, pytest |
| 2 |
from rpython.tool import leakfinder |
| 3 |
|
| 4 |
pytest_plugins = 'rpython.tool.pytest.expecttest' |
| 5 |
|
| 6 |
option = None |
| 7 |
|
| 8 |
try: |
| 9 |
from hypothesis import settings |
| 10 |
except ImportError: |
| 11 |
pass |
| 12 |
else: |
| 13 |
try: |
| 14 |
settings.register_profile('default', deadline=None) |
| 15 |
except Exception: |
| 16 |
import warnings |
| 17 |
warnings.warn("Version of hypothesis too old, " |
| 18 |
"cannot set the deadline to None") |
| 19 |
settings.load_profile('default') |
| 20 |
settings.register_profile('longrunning', max_examples=20000, deadline=None) |
| 21 |
|
| 22 |
def braindead_deindent(self): |
| 23 |
"""monkeypatch that wont end up doing stupid in the python tokenizer""" |
| 24 |
text = '\n'.join(self.lines) |
| 25 |
short = py.std.textwrap.dedent(text) |
| 26 |
newsource = py.code.Source() |
| 27 |
newsource.lines[:] = short.splitlines() |
| 28 |
return newsource |
| 29 |
|
| 30 |
py.code.Source.deindent = braindead_deindent |
| 31 |
|
| 32 |
def pytest_report_header(): |
| 33 |
return "pytest-%s from %s" %(pytest.__version__, pytest.__file__) |
| 34 |
|
| 35 |
def pytest_configure(config): |
| 36 |
global option |
| 37 |
option = config.option |
| 38 |
from rpython.config.translationoption import PLATFORMS |
| 39 |
from rpython.translator.platform import set_platform |
| 40 |
platform = config.option.platform |
| 41 |
if platform not in PLATFORMS: |
| 42 |
raise ValueError("%s not in %s" % (platform, PLATFORMS)) |
| 43 |
set_platform(platform, None) |
| 44 |
|
| 45 |
|
| 46 |
def pytest_addoption(parser): |
| 47 |
group = parser.getgroup("rpython options") |
| 48 |
group.addoption('--view', action="store_true", dest="view", default=False, |
| 49 |
help="view translation tests' flow graphs with Pygame") |
| 50 |
group.addoption('-P', '--platform', action="store", dest="platform", |
| 51 |
type="string", default="host", |
| 52 |
help="set up tests to use specified platform as compile/run target") |
| 53 |
group = parser.getgroup("JIT options") |
| 54 |
group.addoption('--viewloops', action="store_true", |
| 55 |
default=False, dest="viewloops", |
| 56 |
help="show only the compiled loops") |
| 57 |
group.addoption('--viewdeps', action="store_true", |
| 58 |
default=False, dest="viewdeps", |
| 59 |
help="show the dependencies that have been constructed from a trace") |
| 60 |
|
| 61 |
|
| 62 |
def pytest_addhooks(pluginmanager): |
| 63 |
pluginmanager.register(LeakFinder()) |
| 64 |
|
| 65 |
class LeakFinder: |
| 66 |
"""Track memory allocations during test execution. |
| 67 |
|
| 68 |
So far, only used by the function lltype.malloc(flavor='raw'). |
| 69 |
""" |
| 70 |
@pytest.hookimpl(trylast=True) |
| 71 |
def pytest_runtest_setup(self, item): |
| 72 |
if not isinstance(item, py.test.collect.Function): |
| 73 |
return |
| 74 |
if not getattr(item.obj, 'dont_track_allocations', False): |
| 75 |
leakfinder.start_tracking_allocations() |
| 76 |
from rpython.rlib import rgil |
| 77 |
rgil._reset_emulated_gil_holder() |
| 78 |
|
| 79 |
@pytest.hookimpl(trylast=True) |
| 80 |
def pytest_runtest_call(self, item): |
| 81 |
if not isinstance(item, py.test.collect.Function): |
| 82 |
return |
| 83 |
item._success = True |
| 84 |
|
| 85 |
@pytest.hookimpl(trylast=True) |
| 86 |
def pytest_runtest_teardown(self, item): |
| 87 |
if not isinstance(item, py.test.collect.Function): |
| 88 |
return |
| 89 |
if (not getattr(item.obj, 'dont_track_allocations', False) |
| 90 |
and leakfinder.TRACK_ALLOCATIONS): |
| 91 |
kwds = {} |
| 92 |
try: |
| 93 |
kwds['do_collection'] = item.track_allocations_collect |
| 94 |
except AttributeError: |
| 95 |
pass |
| 96 |
item._pypytest_leaks = leakfinder.stop_tracking_allocations(False, |
| 97 |
**kwds) |
| 98 |
else: |
| 99 |
item._pypytest_leaks = None |
| 100 |
|
| 101 |
|
| 102 |
if getattr(item, '_success', False) and item._pypytest_leaks: |
| 103 |
raise leakfinder.MallocMismatch(item._pypytest_leaks) |