Skip to content

Error in click.utils.echo() when console is unavailable #2415

@rokm

Description

@rokm

The click.utils.echo() function does not seem to account for the case when the console is not available on Windows, i.e., when running under pythonw.exe interpreter instead of the python.exe one.

Minimal example:

# program.py
import sys
import os

try:
    import click
    click.utils.echo("Hello world")
except Exception:
    import traceback
    error_file = os.path.join(os.path.dirname(__file__), "error_log.txt")
    with open(error_file, "w") as fp:
        traceback.print_exc(file=fp)

Running this program with pythonw.exe program.py produces the error_log.txt:

Traceback (most recent call last):
  File "C:\Users\Rok\Development\pyi-click\program.py", line 6, in <module>
    click.utils.echo("Hello world")
  File "C:\Users\Rok\Development\pyi-click\venv\lib\site-packages\click\utils.py", line 299, in echo
    file.write(out)  # type: ignore
AttributeError: 'NoneType' object has no attribute 'write'

In contrast, standard print() function gracefully handles situations when console is unavailable and sys.stdout and sys.stderr are None.

This attempt at retrieving stdout/stderr:

click/src/click/utils.py

Lines 250 to 254 in c65c6ad

if file is None:
if err:
file = _default_text_stderr()
else:
file = _default_text_stdout()

should be followed by another None check, and if the stream is unavailable, the function should become a no-op (exit immediately).

Environment:

  • OS: Windows
  • Python version: any
  • Click version: 8.1.3 (and earlier)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions