Skip to content

Crash when using --setup-show and KeyboardInterrupt #5906

Closed
@iwanb

Description

@iwanb

When interrupting a test using Ctrl-C (KeyboardInterrupt/SIGINT), the "setuponly" plugin crashes due to some interaction with the capture manager.

Using pytest 5.2.0 (fresh install on Python 3.7 in a virtual env), given a dummy test:

import time
import pytest


@pytest.fixture()
def setup():
    print("Setup")
    yield
    print("Teardown")


def test_something(setup):
    time.sleep(60)

Run it with pytest --setup-show test_teardown.py and abort it while the test is running.

Stacktrace:

Traceback (most recent call last):
  File "venv/bin/pytest", line 10, in <module>
    sys.exit(main())
  File "venv/lib/python3.7/site-packages/_pytest/config/__init__.py", line 90, in main
    return config.hook.pytest_cmdline_main(config=config)
  File "venv/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "venv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "venv/lib/python3.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "venv/lib/python3.7/site-packages/_pytest/main.py", line 228, in pytest_cmdline_main
    return wrap_session(config, _main)
  File "venv/lib/python3.7/site-packages/_pytest/main.py", line 221, in wrap_session
    session=session, exitstatus=session.exitstatus
  File "venv/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "venv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "venv/lib/python3.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 203, in _multicall
    gen.send(outcome)
  File "venv/lib/python3.7/site-packages/_pytest/terminal.py", line 650, in pytest_sessionfinish
    outcome.get_result()
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "venv/lib/python3.7/site-packages/_pytest/runner.py", line 75, in pytest_sessionfinish
    session._setupstate.teardown_all()
  File "venv/lib/python3.7/site-packages/_pytest/runner.py", line 326, in teardown_all
    self._pop_and_teardown()
  File "venv/lib/python3.7/site-packages/_pytest/runner.py", line 299, in _pop_and_teardown
    self._teardown_with_finalization(colitem)
  File "venv/lib/python3.7/site-packages/_pytest/runner.py", line 319, in _teardown_with_finalization
    self._callfinalizers(colitem)
  File "venv/lib/python3.7/site-packages/_pytest/runner.py", line 316, in _callfinalizers
    raise val.with_traceback(tb)
  File "venv/lib/python3.7/site-packages/_pytest/runner.py", line 307, in _callfinalizers
    fin()
  File "venv/lib/python3.7/site-packages/_pytest/fixtures.py", line 867, in finish
    hook.pytest_fixture_post_finalizer(fixturedef=self, request=request)
  File "venv/lib/python3.7/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "venv/lib/python3.7/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "venv/lib/python3.7/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "venv/lib/python3.7/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "venv/lib/python3.7/site-packages/_pytest/setuponly.py", line 44, in pytest_fixture_post_finalizer
    _show_fixture_action(fixturedef, "TEARDOWN")
  File "venv/lib/python3.7/site-packages/_pytest/setuponly.py", line 54, in _show_fixture_action
    out, err = capman.read_global_capture()
  File "venv/lib/python3.7/site-packages/_pytest/capture.py", line 141, in read_global_capture
    return self._global_capturing.readouterr()
AttributeError: 'NoneType' object has no attribute 'readouterr'

I'm not sure how the capture manager gets into this state where _global_capturing is None, one easy fix would be to make read_global_capture check for None so it can always succeed. Fixing the underlying reason it gets into that state would of course be better.

Metadata

Metadata

Assignees

Labels

type: bugproblem that needs to be addressed

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions