-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
subprocess makes environment blocks with duplicate keys on Windows #91018
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
Comments
On Windows, if one writes env = os.environ.copy()
env['http_proxy'] = 'whatever' or either of the documented equivalents ({**os.environ, ...} or (os.environ | {...})), and passes the resulting environment to subprocess.run or subprocess.Popen, the spawned process may get an environment containing both Because os.environ forces all keys to upper case, it's possible to work around this by using only upper case keys in the update, but that behavior of os.environ is nonstandard (bpo-46861), and subprocess shouldn't depend on it always being true, nor should end users have to. Since dicts preserve order, the user's (presumable) intent is preserved in the env argument. I think subprocess should do something like env = {k.upper(): (k, v) for k, v in env.items()}
env = dict(env.values()) to discard duplicate keys, keeping only the rightmost one. |
This should be handled in _winapi.CreateProcess(). An environment block is technically required to be sorted. (Ages ago this was a MUST requirement for getting and setting variables to work correctly, since the implementation depended on the sort order, but I think nowadays it's a SHOULD requirement.) For example, see the documentation of CreateProcessW() [1]:
"Changing Environment Variables" is more specific [2]:
CompareStringOrdinal() [3] implements a case-insensitive ordinal comparison. When a key compares as equal, either keep the current one in the sorted list, or replace it with the new key. --- [1] https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw |
I suggest closing this issue as a duplicate of bpo-43702. |
Duplicate of #87868 |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: