Repository navigation
Labels
test runnerCliRunner and Click's test utilities feature
Milestone
Description
It looks like the output redirection for CliRunner doesn't play nicely with Python's faulthandler library, as it terminates prematurely with the following backtrace (slightly modified to hide local filesystem details):
Traceback (most recent call last):
File "$CONDA_PREFIX/lib/python3.11/site-packages/click/testing.py", line 408, in invoke
return_value = cli.main(args=args or (), prog_name=prog_name, **extra)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "$CONDA_PREFIX/lib/python3.11/site-packages/click/core.py", line 1078, in main
rv = self.invoke(ctx)
^^^^^^^^^^^^^^^^
File "$CONDA_PREFIX/lib/python3.11/site-packages/click/core.py", line 1434, in invoke
return ctx.invoke(self.callback, **ctx.params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "$CONDA_PREFIX/lib/python3.11/site-packages/click/core.py", line 783, in invoke
return __callback(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "./test_click.py", line 12, in main
faulthandler.enable()
io.UnsupportedOperation: filenoCode for test_click.py that replicates the problem:
import click
from click.testing import CliRunner
import faulthandler
import traceback
@click.command()
@click.option('--flag', type=bool, default=True)
def main(flag):
print("Executing main function...")
if flag:
print("Registering faulthandler")
faulthandler.enable()
print("Finished executing main function.")
if __name__ == '__main__':
flag_value = True
runner = CliRunner()
result = runner.invoke(main, ["--flag", flag_value])
print("Exit code = %d; contents of stdout:\n%s" % (result.exit_code, result.stdout))
if result.exit_code != 0:
traceback.print_exception(result.exc_info[1])The code was then executed by running python test_click.py
I assume the problem is that the CliRunner redirected stderr doesn't implement some details that are required by the faulthandler signal handler registration. Ideally, it should just work; or at least, it should avoid crashing and just do nothing (and perhaps be documented somewhere).
Environment:
- Python version: 3.11.10
- Click version: 8.1.7
Metadata
Metadata
Assignees
Labels
test runnerCliRunner and Click's test utilities feature
Activity
Expose original file descriptor in `CliRunner`
kdeldycke commentedon Mar 4, 2026
@ThomasLoke I proposed a fix in #3244 , can you test it?
[-]CliRunner terminates with io.UnsupportedOperation when using faulthandler[/-][+]`CliRunner` terminates with `io.UnsupportedOperation` when using `faulthandler`[/+]Expose original file descriptor in `CliRunner`
ThomasLoke commentedon Mar 5, 2026
@kdeldycke Took it for a spin, and it works for me! And from a quick peek of the PR, the changes make sense to me, so all good from my side :)
kdeldycke commentedon Mar 5, 2026
Thanks a lot @ThomasLoke for the review and test! Your feedback is valuable. Thanks for your patience! :)
abhii734 commentedon Mar 9, 2026
kdeldycke commentedon Mar 9, 2026
You did not read the thread: a fix has already been proposed at #3244 and validated by the original reporter.
Expose original file descriptor in `CliRunner`
Expose original file descriptor in `CliRunner`
Expose original file descriptor in `CliRunner`
kdeldycke commentedon Apr 16, 2026
#3244 has been merged upstream and will be part of the next 8.3.3 release.