-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-120057: Add os.get_user_default_environ() function #120494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
499af13
576e2b9
e76c379
337b22b
f79fd79
81ea5b0
6d1c3b1
d6edf64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,6 +96,10 @@ def _get_exports_list(module): | |
| from nt import _create_environ | ||
| except ImportError: | ||
| pass | ||
| try: | ||
| from nt import _get_user_default_environ | ||
| except ImportError: | ||
| pass | ||
|
|
||
| else: | ||
| raise ImportError('no os specific module found') | ||
|
|
@@ -826,6 +830,29 @@ def decode(value): | |
| del _create_environ_mapping | ||
|
|
||
|
|
||
| if _exists("_get_user_default_environ"): | ||
| def get_user_default_environ(): | ||
| """ | ||
| Get the default environment of the current process user as a dictionary. | ||
| """ | ||
| env_str = _get_user_default_environ() | ||
| env = {} | ||
| for entry in env_str.split('\0'): | ||
| parts = entry.split('=', 1) | ||
| if len(parts) != 2: | ||
| # Silently skip variable with no name | ||
| continue | ||
|
|
||
| name, value = parts | ||
| if not name: | ||
| # Silently skip variable with empty name | ||
| continue | ||
|
|
||
| # If a variable is set twice, use the first value | ||
| env.setdefault(name, value) | ||
|
Comment on lines
+851
to
+852
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose this doesn't hurt. But it shouldn't be possible to get duplicates unless there's a serious bug. The new environment is built by assigning values to the environment block, which is handled as a parameter by the same code that implements WinAPI |
||
| return env | ||
|
|
||
|
|
||
| def getenv(key, default=None): | ||
| """Get an environment variable, return None if it doesn't exist. | ||
| The optional second argument can specify an alternate default. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Added the :func:`os.get_user_default_environ` function to get the user | ||
| default environment. It can be used to update :data:`os.environ` to the | ||
| latest user and system environment variables, such as the ``Path`` variable. | ||
| Patch by Victor Stinner. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Normally, a new environment block gets passed to a spawned child process, such as via the
envparameter ofsubprocess.Popen. I'd emphasize that over updatingos.environ, which seems to be a point of controversy.