镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

os.environ['VAR'] = '' is not portable (windows) #158694

Description

@asottile

Bug report

import os

def test_env(x):
    print(f'setting X={x}')
    os.environ['X'] = x
    assert os.environ.get('X') == x, os.environ.get('X')
    os.reload_environ()
    assert os.environ.get('X') == x, os.environ.get('X')

test_env('1')
test_env('')

it is possible to set an empty environment variable on windows (via SetEnvironmentVariableW) -- but not from python's os.environ . there's more context here: pre-commit/pre-commit#3766 (comment) (due to _wputenv)

on linux (I only attempted with amd64 ubuntu 26.04) the script above succeeds -- on windows it fails with the following:

>python t.py
setting X=1
setting X=
Traceback (most recent call last):
  File "C:\Users\asott\workspace\t.py", line 11, in <module>
    test_env('')
    ~~~~~~~~^^^^
  File "C:\Users\asott\workspace\t.py", line 8, in test_env
    assert os.environ.get('X') == x, os.environ.get('X')
           ^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: None

note also that this bug also results in os.environ diverging from the real environment (line 8 above is an assertion failure only after os.reload_environ())

note that I've only marked "3.14" as the version I tested on -- I suspect this also affects mainline but I do not have an easy way to build on the machine I'm working on at the moment

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions