FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[feature request] Method to refresh os.environ · Issue #119737 · python/cpython · GitHub

Repository navigation

[feature request] Method to refresh os.environ #119737

Closed as not planned
Closed as not planned

Description

Bug report

Bug description:

os.environ doesn't reflect environment changes that are made outside of Python.

For example, this code adds the environment XXX but os.environ doesn't see this change.

This program:

import os

import ctypes
import ctypes.util

clib = ctypes.util.find_library("c")
dll = ctypes.CDLL(clib)

setenv = dll.setenv
setenv.argtypes = [ctypes.POINTER(ctypes.c_char), ctypes.POINTER(ctypes.c_char), ctypes.c_int]

getenv = dll.getenv
getenv.argtypes = [ctypes.POINTER(ctypes.c_char)]
getenv.restype=ctypes.c_char_p

# set XXX outside of Python's os.environ
setenv(b"XXX", b"Hello", 2)

print(f'real XXX value: {str(getenv(b"XXX"))}')
print(f'XXX is in os.getenv: {os.getenv("XXX")}')
print(f'XXX is in os.environ: {"YES" if "XXX" in os.environ else "NO"}')

prints:

real XXX value: b'Hello'
XXX is in os.getenv: None
XXX is in os.environ: NO

There should be a method, for example os.environ_refresh, that would re-read environment into os.environ.

I've hit this on the real-world case when Python didn't see environment set by TCL script executed in the same process.

CPython versions tested on:

3.9

Operating systems tested on:

Other

Activity

  1. added
    type-featureA feature request or enhancement
    stdlibStandard Library Python modules in the Lib/ directory
    3.14bugs and security fixes
    and removed
    type-bugAn unexpected behavior, bug, or error
    on May 29, 2024
  2. itamaro commented on Jun 1, 2024

    Contributor

    the behavior matches the documented behavior:

    This mapping is captured the first time the os module is imported, typically during Python startup as part of processing site.py. Changes to the environment made after this time are not reflected in os.environ, except for changes made by modifying os.environ directly.

    so this is not a bug, but a feature request.
    please propose this on the Python discuss ideas category first!

  3. added
    pendingThe issue will be closed if no feedback is provided
    and removed
    3.14bugs and security fixes
    on Jun 1, 2024
  4. removed
    pendingThe issue will be closed if no feedback is provided
    on Jun 1, 2024
  5. eryksun commented on Jun 1, 2024

    Contributor

    This new feature would have to be implemented in C in "Modules/posixmodule.c". That's why I added the "extension-modules" label.

  6. yurivict commented on Jun 2, 2024

    Author
  7. vstinner commented on Jun 5, 2024

    Member

    I close this issue as a duplicate of #120057

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

    extension-modulesC modules in the Modules dirstdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL