Skip to content

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

Closed

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

Linked PRs

Activity

  1. hroncok commented on Nov 14, 2025

    @hroncok
    ContributorAuthor

    FWIW the docuemntation of fileno says:

    An OSError is raised if the IO object does not use a file descriptor.

    That makes me think that expecting io.UnsupportedOperation is outright wrong.

  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 14, 2025
  3. hugovk commented on Nov 14, 2025

    @hugovk
    Member

    Yes, OSError does make more sense here, would you like to open a PR?

  4. hroncok commented on Nov 14, 2025

    @hroncok
    ContributorAuthor

    Will do.

  5. hroncok commented on Nov 14, 2025

    @hroncok
    ContributorAuthor

    FWIW I won't do it right away, because I am thinking how to properly test this. Perhaps by calling _colorize.can_colorize(file=...) on a custom file-like object.

  6. added
    3.14bugs and security fixes
    3.15bugs and security fixes
    on Nov 14, 2025
  7. cmaloney commented on Nov 18, 2025

    @cmaloney
    Contributor

    re: OSError, io.UnsupportedOperation actually inherits from it so both should work. 👍 on changing to the more generic OSError check.

  8. added a commit that references this issue on Nov 19, 2025
  9. added a commit that references this issue on Nov 19, 2025
  10. added 3 commits that reference this issue on Nov 19, 2025
  11. added a commit that references this issue on Dec 6, 2025
  12. added a commit that references this issue on Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15bugs 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