Skip to content

pytest_keyboard_interrupt hook is also called with --maxfail #1865

Closed

Description

@blueyed

When using -x / --maxfail the Interrupted exception is thrown, and gets handled in the same place as KeyboardInterrupt (

pytest/_pytest/main.py

Lines 99 to 106 in 86ec3f3

except KeyboardInterrupt:
excinfo = _pytest._code.ExceptionInfo()
if initstate < 2 and isinstance(
excinfo.value, pytest.exit.Exception):
sys.stderr.write('{0}: {1}\n'.format(
excinfo.typename, excinfo.value.msg))
config.hook.pytest_keyboard_interrupt(excinfo=excinfo)
session.exitstatus = EXIT_INTERRUPTED
), because it is a subclass.

Therefore I think the hook name is confusing / misleading.
If this behavior is intentional, it should get added to the documentation at least.

I have noticed this, because pytest-testmon uses this hook to not save its data: https://github.com/tarpas/pytest-testmon/blob/55c89bb3df65c94e226ead8dd465f3dd5bb8927b/testmon/pytest_testmon.py#L173-L174
Here a check using excinfo.typename could be added to handle 'Interrupted' different from 'KeyboardInterrupt'.

btw: is there a way to import the Interrupted class from pytest?

Activity

  1. The-Compiler commented on Aug 24, 2016

    @The-Compiler
    Member

    FWIW, Interrupted being a subclass of KeyboardInterrupt sounds weird to me...

  2. added a commit that references this issue on Aug 24, 2016
  3. nicoddemus commented on Aug 26, 2016

    @nicoddemus
    Member

    I agree. I think that we should:

    1. Subclass Interrupted from something else (RuntimeError?)
    2. Make Interrupted part of the public API so plugins can raise or catch it; perhaps by making it available in the pytest namespace? Also, how about renaming it to SessionInterrupt?

    This has to be done carefully, because there are a number of places which handle KeyboardInterrupt on pytest's code base that will have to be changed to also handle SessionInterrupt.

    I also noticed that Exit also subclasses KeyboardInterrupt.

  4. The-Compiler commented on Aug 26, 2016

    @The-Compiler
    Member
    1. Subclass Interrupted from something else (RuntimeError?)

    If you do except RuntimeError:, would you expect Interrupted to be caught (I don't)? If not, it should simply subclass Exception.

  5. nicoddemus commented on Aug 26, 2016

    @nicoddemus
    Member

    Good point!

  6. added
    type: enhancement
    on Aug 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions