hroncok · GitHub

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:

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

Linked PRs

Read the original on github.com ↗