这是indexloc提供的服务,不要输入任何密码
Skip to content

_colorize.can_colorize() raises when sys.stdout is mod_wsgi logger #141570

@hroncok

Description

@hroncok

Bug report

Bug description:

In Fedora, we've been given a slightly incomplete reproducer for a problematic Python 3.14 color-related change in argparse that leads to an exception when Python is used from mod_wsgi. cc @GrahamDumpleton

Apparently the following code is enough to trigger it:

import argparse

parser = argparse.ArgumentParser(add_help=False, color=False)
arg = parser.add_argument('-v', '--verbose', help='verbose', action='store_true', default=False)

Looking at GrahamDumpleton/mod_wsgi#890 it seems that mod_wsgi replaces sys.stdout with a custom object that raises OSError on .fileno():

https://github.com/GrahamDumpleton/mod_wsgi/blob/8460dbfcd5c7108892b3cde9fab7cbc1caa27886/src/server/wsgi_logger.c#L434-L440

The relevant code in _colorize.can_colorize() does not expect OSError -- it only expects a more specific io.UnsupportedOperation:

cpython/Lib/_colorize.py

Lines 313 to 316 in a2442fd

try:
return os.isatty(file.fileno())
except io.UnsupportedOperation:
return hasattr(file, "isatty") and file.isatty()

I don't know if raising OSError from .fileno() in mod_wsgi is OK or not, yet perhaps _colorize.can_colorize() should be more robust and expect either OSError or outright Exception. WDYT?

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions