Skip to content

click.testing.CliRunner.invoke prevents use of pdb #138

Closed

Description

@wallrj

Here's another one (related to #136). I wanted to explore the contents of the context object passed to a command decorated with @click.pass_context.

But when I try dropping a pdb.set_trace into the invoked command, it immediately quits...but only when invoked via the CliRunner.

if I actually invoke the command from the command line, the debugger works as expected.

Maybe something to do with the handling of stdin and stdout by CliRunner.invoke?

Here's an example:

"""
(click)(master ? =)[~/projects/click]$ python click_bdb_swallow.py 
F
======================================================================
FAIL: test_noarguments (__main__.FooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "click_bdb_swallow.py", line 21, in test_noarguments
    result = runner.invoke(foo)
AssertionError: <object object at 0x7fb7d0f9f1b0> != <Result BdbQuit()>

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (failures=1)
"""

import unittest

import click
from click.testing import CliRunner

@click.command()
@click.pass_context
def foo(context):
    """
    I want to drop to pdb in here so that I can explore the context object, but
    pdb just exits, maybe because of the way CliRunner.invoke changes stdin,
    stdout.
    """
    import pdb; pdb.set_trace()



class FooTests(unittest.TestCase):
    def test_noarguments(self):
        runner = CliRunner()
        result = runner.invoke(foo)
        # And again it's confusing that BdbQuit has been swallowed by invoke
        # (see https://github.com/mitsuhiko/click/issues/136)
        self.assertEqual(object(), result)



if __name__ == '__main__':
    unittest.main()

Activity

  1. mitsuhiko commented on Jun 14, 2014

    @mitsuhiko
    Contributor

    There is a way to prevent this with Click 3.0

  2. torbjorntorbjorn commented on Feb 17, 2015

    @torbjorntorbjorn

    @mitsuhiko, could you explain how to prevent pdb from quiting when started under CliRunner.invoke ?
    I have tried to provide the input argument to invoke(), and that seems to let me pass input to a running pdb, however, the output seems to get eaten by invoke().
    Looking at the code, it's not clear to me how to avoid this effect when using invoke().
    I'm on click 3.3.

  3. nikhilkalige commented on Feb 23, 2015

    @nikhilkalige

    call invoke with catch_exceptions=False

    runner.invoke(loader.loader, ["-t", "rdr", fname], catch_exceptions=False)
    
  4. torbjorntorbjorn commented on Feb 24, 2015

    @torbjorntorbjorn

    It seems like calling with catch_exceptions=False makes invoke() raise a bdb.BdbQuit exception.
    Here is a gist which trigger the behaviors I'm seeing. I'm on Python 2.7.3 on Debian stable.
    https://gist.github.com/anonymous/1f1ea59bac42e8ea6b2b

    Could this be happening due to CliRunner.isolation() replacing sys.stdin with a StringIO(), which is empty by default. pdb then reads the empty buffer and gets a EOF ?

  5. DXist commented on Jun 3, 2016

    @DXist

    +1

    Click can provide lightweight method that just calls cli.main and catches SystemExit. I'm not sure why catch_exceptions is True by default. From my perspective it's better to get failed test and see default traceback.

  6. endzyme commented on Mar 23, 2017

    @endzyme

    getting this in Click 6.6 - anyone ever find a workaround? When setting catch_exceptions=False i still get dumped in

    > /usr/local/Cellar/python3/3.5.2_1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/bdb.py(67)dispatch_line()
    -> if self.quitting: raise BdbQuit```
    
  7. bresmith-wayfair commented on May 19, 2017

    @bresmith-wayfair

    also getting this on click==6.6

    would love to get a solution to this, making it really hard to debug this issue i'm having

  8. juledwar commented on May 24, 2017

    @juledwar

    I am using rpdb until this is fixed. Not ideal but serves a need.

  9. Elfoniok commented on Aug 7, 2017

    @Elfoniok

    Hey
    Name: click
    Version: 6.7

    Still having this issue. This occurs on python 3.5.3 i didn't observe this on python 2.7.

  10. chaoflow commented on Oct 25, 2017

    @chaoflow
    Contributor

    as well with b471d34 and python 3.5.2

  11. jtcho commented on May 16, 2018

    @jtcho

    I have provided a workaround that worked for me in #843.

  12. deleted a comment from salarkhan on Jul 13, 2018
  13. locked as resolved and limited conversation to collaborators on Nov 13, 2020
  14. added
    test runner
    on Aug 8, 2026
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

    test runner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions