File: conftest.py

package info (click to toggle)
python-pyinstrument 5.1.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,624 kB
  • sloc: python: 6,713; ansic: 897; makefile: 46; sh: 26; javascript: 18
file content (52 lines) | stat: -rw-r--r-- 1,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import sys

import pytest

from pyinstrument import stack_sampler


def pytest_addoption(parser) -> None:
    # IPython tests seem to pollute the test environment, so they're run in a
    # separate process.

    parser.addoption(
        "--only-ipython-magic",
        action="store_true",
        default=False,
        help="run only ipython magic tests",
    )


def pytest_configure(config):
    config.addinivalue_line(
        "markers", "ipythonmagic: test requires --only-ipython-magic flag to run"
    )


def pytest_collection_modifyitems(config, items) -> None:
    flag_was_passed = config.getoption("--only-ipython-magic")

    skip_not_ipython = pytest.mark.skip(reason="not an ipython test")
    skip_ipython = pytest.mark.skip(reason="requires --only-ipython-magic option to run")

    for item in items:
        if "ipythonmagic" in item.keywords:
            if not flag_was_passed:
                item.add_marker(skip_ipython)
        else:
            if flag_was_passed:
                item.add_marker(skip_not_ipython)


@pytest.fixture(autouse=True)
def check_sampler_state():
    assert sys.getprofile() is None
    assert len(stack_sampler.get_stack_sampler().subscribers) == 0

    try:
        yield
        assert sys.getprofile() is None
        assert len(stack_sampler.get_stack_sampler().subscribers) == 0
    finally:
        sys.setprofile(None)
        stack_sampler.thread_locals.__dict__.clear()