You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
click.testing.CliRunner.invoke prevents use of pdb #843
Will someone please re-open #138. It's still a problem with Click 6.7. In particular, my stack trace ends with:
File "/home/rsyring/projects/agari-adm-src/agariadm/cli.py", line 64, in batch
import pdb; pdb.set_trace()
File "/usr/lib/python3.5/bdb.py", line 52, in trace_dispatch
return self.dispatch_return(frame, arg)
File "/usr/lib/python3.5/bdb.py", line 96, in dispatch_return
if self.quitting: raise BdbQuit
bdb.BdbQuit
And there is explanation about why this is happening in the related issue.
Reacted by Florian Friesdorf, Davis Kirkendall, Kai Richard Koenig, mattmoskwa, Jim King, Jose Eduardo, Antonis Christofides, Michael Goerz, Matt Black, Haoran Tang and 3 more
@rsyring Perhaps you've figured this out in the interim, but the workaround that I've come up for this is to mock out click.testing.make_input_stream and replace the returned value with the original sys.stdin.
Since CliRunner overrides sys.{stdin,stdout} and pdb depends on them, it's quite expected that this doesn't just work. pytest, which also does IO redirection, monkey patches the global pdb.set_trace so that it disables IO redirection when called allowing it work even if IO redirection is enabled. But I think click generally tries to avoid affecting global state...
I tried out a few samples and workarounds from here. The CliRunner replaces the stdin and stdout with _NamedTextIOWrapper on top of EchoingStdin and io.BytesIO objects respectively. The return value of make_input_stream and io.BytesIO is simply not compatible with pdb which uses sys.stdin and sys.stdout.
I went through pdb source code as well and from what I can tell, BdbQuit is raised when the debugger is quit and the program is aborted though there could be some other places as well that raise it. I don't think it has anything to do with the exception, just that which stream is being used to output it.
In my opinion, it will be better to leave this as it is until there is a better way to mock sys.stdin and sys.stdout inside CliRunner. One way could be to somehow detect the usage of pdb inside CliRunner and accordingly mock the streams. Perhaps an extra flag could serve this purpose. Or mock pdb itself like pytest does as mentioned by @segevfiner
I had the same error, calling PDB from pytest using pytest --pdb, with import pytest; pytest.set_trace() in my code instead of import pdb; pdb.set_trace() fixed it for me.
changed the title [-]click.testing.CliRunner.invoke prevents use of pdb[/-][+]`click.testing.CliRunner.invoke` prevents use of `pdb`[/+]on Apr 11, 2026
Activity
jtcho commentedon May 16, 2018
@rsyring Perhaps you've figured this out in the interim, but the workaround that I've come up for this is to mock out
click.testing.make_input_streamand replace the returned value with the originalsys.stdin.For example:
Please let me know if you have any more questions.
segevfiner commentedon Oct 7, 2018
Since
CliRunneroverridessys.{stdin,stdout}and pdb depends on them, it's quite expected that this doesn't just work.pytest, which also does IO redirection, monkey patches the globalpdb.set_traceso that it disables IO redirection when called allowing it work even if IO redirection is enabled. But I thinkclickgenerally tries to avoid affecting global state...You can try this workaround:
How click should be handling this issue is left open for discussion.
rcoup commentedon Jun 27, 2019
This might be useful for some:
Click CliRunner with PDB working better under pytest
https://gist.github.com/rcoup/2566c92a1c47d66cfb429a6e3cb0cca2
delijati commentedon May 26, 2020
I use this snipped and it works basically always (argsparse, click, sys.argv):
Saif807380 commentedon May 11, 2021
I tried out a few samples and workarounds from here. The
CliRunnerreplaces thestdinandstdoutwith_NamedTextIOWrapperon top ofEchoingStdinandio.BytesIOobjects respectively. The return value ofmake_input_streamandio.BytesIOis simply not compatible withpdbwhich usessys.stdinandsys.stdout.I went through
pdbsource code as well and from what I can tell,BdbQuitis raised when the debugger is quit and the program is aborted though there could be some other places as well that raise it. I don't think it has anything to do with the exception, just that which stream is being used to output it.In my opinion, it will be better to leave this as it is until there is a better way to mock
sys.stdinandsys.stdoutinsideCliRunner. One way could be to somehow detect the usage ofpdbinsideCliRunnerand accordingly mock the streams. Perhaps an extra flag could serve this purpose. Or mockpdbitself likepytestdoes as mentioned by @segevfinerjmlrt commentedon Jun 22, 2021
I had the same error, calling PDB from pytest using
pytest --pdb, withimport pytest; pytest.set_trace()in my code instead ofimport pdb; pdb.set_trace()fixed it for me.Fabien-GELUS commentedon May 31, 2022
kdeldycke commentedon Mar 2, 2026
A fix is available in PR #3235 . It will propably be part of Click 8.3.2 release. Can you test it?
Patch `pdb` in `CliRunner` to allow debugger interactions in tests
[-]click.testing.CliRunner.invoke prevents use of pdb[/-][+]`click.testing.CliRunner.invoke` prevents use of `pdb`[/+]kdeldycke commentedon Apr 11, 2026
Fixed in #3235