# Path: pyinstrument/stack_sampler.py
# class StackSamplerSubscriber:
# class StackSampler:
#     class SubscriberNotFound(Exception):
# class AsyncState(NamedTuple):
#     def __init__(
#         self,
#         *,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         bound_to_async_context: bool,
#         async_state: AsyncState | None,
#     ) -> None:
#     def __init__(self) -> None:
#     def subscribe(
#         self,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         use_async_context: bool,
#     ):
#     def unsubscribe(self, target: StackSamplerSubscriberTarget):
#     def _update(self):
#     def _start_sampling(self, interval: float):
#     def _stop_sampling(self):
#     def _sample(self, frame: types.FrameType, event: str, arg: Any):
#     def _timer(self):
# def get_stack_sampler() -> StackSampler:
# def build_call_stack(frame: types.FrameType | None, event: str, arg: Any) -> list[str]:


import sys
import pytest
from pyinstrument import stack_sampler

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

# Path: pyinstrument/stack_sampler.py
# class StackSamplerSubscriber:
# class StackSampler:
#     class SubscriberNotFound(Exception):
# class AsyncState(NamedTuple):
#     def __init__(
#         self,
#         *,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         bound_to_async_context: bool,
#         async_state: AsyncState | None,
#     ) -> None:
#     def __init__(self) -> None:
#     def subscribe(
#         self,
#         target: StackSamplerSubscriberTarget,
#         desired_interval: float,
#         use_async_context: bool,
#     ):
#     def unsubscribe(self, target: StackSamplerSubscriberTarget):
#     def _update(self):
#     def _start_sampling(self, interval: float):
#     def _stop_sampling(self):
#     def _sample(self, frame: types.FrameType, event: str, arg: Any):
#     def _timer(self):
# def get_stack_sampler() -> StackSampler:
# def build_call_stack(frame: types.FrameType | None, event: str, arg: Any) -> list[str]:


# Path: test/conftest.py
import sys
import pytest
from pyinstrument import stack_sampler


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