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
[_winapi] correctly sort and remove duplicates in getenvironment() #87868
Comments
The The sort order used to matter with Case-insensitive sorting in Windows uses upper case. The variable names in the mapping can be added to a list and sorted with a key function that's based on Footnotes |
I have a question, how to determine which name should be stored if they are duplicated with case insensitive? |
Ideally In msg390038 I suggested keeping the first key that's encountered. However, dicts preserve insertion order nowadays, so one could assume that the last one is the one that the caller wants to keep. |
This issue can be reproduced on msys2 (also cygwin, I think) easily: msys2 shell $ ABC=1 abc=2 python
Python 3.11.0b1 (main, May 7 2022, 22:58:47) [MSC v.1931 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.environ["ABC"], os.environ["abc"])
2 2
>>> import subprocess
>>> subprocess.run(["bash", "-c", "echo $ABC $abc"])
1 2
CompletedProcess(args=['bash', '-c', 'echo $ABC $abc'], returncode=0)
>>>
>>> os.environ["abc"] = "3"
>>> print(os.environ["ABC"], os.environ["abc"])
3 3
>>> subprocess.run(["bash", "-c", "echo $ABC $abc"])
3 2
CompletedProcess(args=['bash', '-c', 'echo $ABC $abc'], returncode=0) msys2 shell $ ABC=1 abc=2 cmd
cmd > echo %ABC% %abc%
1 1
cmd > bash -c "echo $ABC $abc"
1 2
cmd > set abc=3
cmd > echo %ABC% %abc%
3 3
cmd > bash -c "echo $ABC $abc"
3 2 msys2 shell $ ABC=1 abc=2 powershell
PS > echo $Env:ABC $Env:abc
1
1
PS > bash -c "echo $ABC $abc"
1 2
PS > $Env:abc = 3
PS > echo $Env:ABC $Env:abc
3
3
PS > bash -c 'echo $ABC $abc'
3 2 Python behaves differently from both cmd.exe and powershell. Case-sensitive environment variables are in msys2 shell $ ABC=1 abc=2 python
Python 3.11.0b1 (main, May 7 2022, 22:58:47) [MSC v.1931 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import nt, os
>>> print(nt.environ["ABC"], nt.environ["abc"])
1 2
>>> print(os.environ["ABC"], os.environ["abc"])
2 2 This is because environment vairiables are not sorted correctly in https://github.com/python/cpython/blob/v3.11.0b1/Lib/os.py#L750 |
Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Pieter Eendebak <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
…nGH-102731) (cherry picked from commit c31be58) Co-authored-by: AN Long <[email protected]> Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Pieter Eendebak <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
…nGH-102731) (cherry picked from commit c31be58) Co-authored-by: AN Long <[email protected]> Co-authored-by: Alex Waygood <[email protected]> Co-authored-by: Pieter Eendebak <[email protected]> Co-authored-by: Erlend E. Aasland <[email protected]>
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:
Linked PRs
The text was updated successfully, but these errors were encountered: