GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `can_colorize()` ignores `FORCE_COLOR`/`NO_COLOR`/`TERM` when `-E` is set · Issue #127873 · python/cpython

can_colorize() ignores FORCE_COLOR/NO_COLOR/TERM when -E is set #127873

New issue
Copy link
New issue
Copy link
Closed
Closed
can_colorize() ignores FORCE_COLOR/NO_COLOR/TERM when -E is set#127873
Copy link
Labels
3.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@hugovk
hugovk
opened on Dec 12, 2024
Issue body actions

Bug report

Bug description:

Using one of the FORCE_COLOR, NO_COLOR or TERM=dumb environment variables is ignored when you use Python with -E.

  • https://docs.python.org/3/using/cmdline.html#using-on-controlling-color

-E means:

Ignore all PYTHON* environment variables, e.g. PYTHONPATH and PYTHONHOME, that might be set.

  • https://docs.python.org/3/using/cmdline.html#cmdoption-E

The -E is stored in sys.flags.ignore_environment.

  • https://docs.python.org/3/library/sys.html#sys.flags.ignore_environment

sys.flags.ignore_environment is used to ignore PYTHON_COLORS (correct) but it's also ignoring these other env vars (incorrect).

For example, this is not colourised, as expected:

❯ NO_COLOR=1 python3.13 -c 1/0
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    1/0
    ~^~
ZeroDivisionError: division by zero

image

However, NO_COLOR=1 is ignored when passing -E and the output has colour when it should not:

❯ NO_COLOR=1 python3.13 -E -c 1/0
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    1/0
    ~^~
ZeroDivisionError: division by zero

image

This bit needs updating:

cpython/Lib/_colorize.py

Lines 43 to 56 in 487fdbe

if not sys.flags.ignore_environment:
if os.environ.get("PYTHON_COLORS") == "0":
return False
if os.environ.get("PYTHON_COLORS") == "1":
return True
if "NO_COLOR" in os.environ:
return False
if not COLORIZE:
return False
if not sys.flags.ignore_environment:
if "FORCE_COLOR" in os.environ:
return True
if os.environ.get("TERM") == "dumb":
return False

CPython versions tested on:

3.13, 3.14

Operating systems tested on:

Linux, macOS, Windows

Linked PRs

  • gh-127873: Only check sys.flags.ignore_environment for PYTHON* env vars #127877
  • [3.13] gh-127873: Only check sys.flags.ignore_environment for PYTHON* env vars (GH-127877) #129138

Activity

  1. hugovk
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    3.13only security fixesonly security fixes
    3.14bugs and security fixesbugs and security fixes
    on Dec 12, 2024
  2. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Dec 13, 2024
  3. hugovk
    added 3 commits that reference this issue on Jan 21, 2025

    gh-127873: Only check `sys.flags.ignore_environment` for `PYTHON*` en…

    05d12ee

    [3.13] pythongh-127873: Only check `sys.flags.ignore_environment` for…

    5c86cc4

    [3.13] gh-127873: Only check `sys.flags.ignore_environment` for `PYTH…

    cc3dc8a
  4. hugovk
    closed this as completedon Jan 22, 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.13only security fixesonly security fixes3.14bugs and security fixesbugs and security fixesstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.