GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [_winapi] correctly sort and remove duplicates in getenvironment() · Issue #87868 · python/cpython

[_winapi] correctly sort and remove duplicates in getenvironment() #87868

New issue
Copy link
New issue
Copy link
Closed
#102731
Closed
[_winapi] correctly sort and remove duplicates in getenvironment()#87868
#102731
Copy link
Labels
3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifeOS-windowsextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@eryksun
eryksun
opened on Apr 2, 2021
Issue body actions
BPO 43702
Nosy @pfmoore, @tjguk, @zware, @eryksun, @zooba, @aisk

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:

assignee = None
closed_at = None
created_at = <Date 2021-04-02.04:16:14.463>
labels = ['type-bug', '3.9', '3.10', '3.11', 'extension-modules', 'OS-windows']
title = '[Windows] correctly sort and remove duplicates in _winapi getenvironment()'
updated_at = <Date 2022-03-19.18:03:11.308>
user = 'https://github.com/eryksun'

bugs.python.org fields:

activity = <Date 2022-03-19.18:03:11.308>
actor = 'eryksun'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Extension Modules', 'Windows']
creation = <Date 2021-04-02.04:16:14.463>
creator = 'eryksun'
dependencies = []
files = []
hgrepos = []
issue_num = 43702
keywords = []
message_count = 3.0
messages = ['390038', '415560', '415563']
nosy_count = 6.0
nosy_names = ['paul.moore', 'tim.golden', 'zach.ware', 'eryksun', 'steve.dower', 'asaka']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue43702'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

  • gh-87868: Sort and remove duplicates in getenvironment() #102731
  • [3.12] gh-87868: Sort and remove duplicates in getenvironment() (GH-102731) #113865
  • [3.11] gh-87868: Sort and remove duplicates in getenvironment() (GH-102731) #113866
  • gh-87868: Skip some test in test_subprocess if python is configured with --enable-shared #113867

Activity

Next
  1. eryksun commented on Apr 2, 2021

    @eryksun
    eryksun
    on Apr 2, 2021
    ContributorAuthor
    More actions

    The getenvironment() function in "Modules/_winapi.c" needs to sort variables in the environment block and remove duplicates case insensitively1.

    The sort order used to matter with SetEnvironmentVairableW(). As soon as it reached a name in the environment block that compared greater than the target name, it would insert a new variable. Nowadays, SetEnvironmentVairableW() searches the entire environment block before inserting a new value. Regardless, at the very least, getenvironment() is not well-behaved and is not setting the environment in the documented sort order that users, and possibly other programs, expect.

    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 LCMapStringEx(), with the flag LCMAP_UPPERCASE. Loop over the sorted list to create the environment block. Remove duplicates by skipping a name that compares equal to the previously stored name according to CompareStringOrdinal().

    Footnotes

    1. Changing Environment Variables ↩

  2. eryksun
    added
    3.8 (EOL)end of lifeend of life
    3.10 (EOL)end of lifeend of life
    3.9 (EOL)end of lifeend of life
    extension-modulesC modules in the Modules dirC modules in the Modules dir
    OS-windows
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Apr 2, 2021
  3. eryksun
    added
    3.11only security fixesonly security fixes
    and removed
    3.8 (EOL)end of lifeend of life
    on Feb 26, 2022
  4. aisk commented on Mar 19, 2022

    @aisk
    aiskmannequin
    on Mar 19, 2022
    Mannequin
    More actions

    I have a question, how to determine which name should be stored if they are duplicated with case insensitive?

  5. eryksun commented on Mar 19, 2022

    @eryksun
    eryksun
    on Mar 19, 2022
    ContributorAuthor
    More actions

    which name should be stored if they are duplicated with case insensitive?

    Ideally os.environ would preserve the original case of the process environment, and os.environ.copy() would return a copy that's also case insensitive. That would prevent most problems with duplicates keys. See msg387676 in bpo-28824, and msg414319 in bpo-15373.

    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.

  6. ezio-melotti
    transferred this issue fromon Apr 10, 2022
  7. GalaxySnail commented on May 14, 2022

    @GalaxySnail
    GalaxySnail
    on May 14, 2022
    Contributor
    More actions

    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 nt.environ, but they don't affect the real environment:

    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 os._convertenviron:

    https://github.com/python/cpython/blob/v3.11.0b1/Lib/os.py#L750

    Related to GH-91018, GH-73010.

  8. eryksun
    changed the title [-][Windows] correctly sort and remove duplicates in _winapi getenvironment()[/-] [+][_winapi] correctly sort and remove duplicates in getenvironment()[/+] on Aug 8, 2022
  9. aisk
    added a commit that references this issue on Mar 15, 2023

    pythongh-87868: correctly sort and remove duplicates in getenvironment()

    eb5e3f2
  10. 5 remaining items

  11. miss-islington
    added a commit that references this issue on Jan 9, 2024

    pythongh-87868: Sort and remove duplicates in getenvironment() (pytho…

    255872a
  12. erlend-aasland
    reopened this on Jan 9, 2024
  13. gpshead
    added a commit that references this issue on Jan 10, 2024

    gh-87868: Skip `test_one_environment_variable` in `test_subprocess` w…

    fafb327
  14. gpshead commented on Jan 10, 2024

    @gpshead
    gpshead
    on Jan 10, 2024
    Member
    More actions

    i'll apply the test skip PR into the existing 3.12 and 3.11 backport branches.

    Reacted by Steve Dower, Erlend E. Aasland and An Long
  15. gpshead
    added 2 commits that reference this issue on Jan 10, 2024

    pythongh-87868: Skip `test_one_environment_variable` in `test_subproc…

    84a5a59

    pythongh-87868: Skip `test_one_environment_variable` in `test_subproc…

    03022e4
  16. zooba
    added 2 commits that reference this issue on Jan 10, 2024

    gh-87868: Sort and remove duplicates in getenvironment() (GH-102731)

    4050a15

    gh-87868: Sort and remove duplicates in getenvironment() (GH-102731)

    d15e1ac
  17. zooba
    closed this as completedon Jan 11, 2024
  18. kulikjak
    added 2 commits that reference this issue on Jan 22, 2024

    pythongh-87868: Sort and remove duplicates in getenvironment() (pytho…

    f9c3b4a

    pythongh-87868: Skip `test_one_environment_variable` in `test_subproc…

    aa2027a
  19. aisk
    added 2 commits that reference this issue on Feb 11, 2024

    pythongh-87868: Sort and remove duplicates in getenvironment() (pytho…

    aa60e17

    pythongh-87868: Skip `test_one_environment_variable` in `test_subproc…

    9b8a723
  20. Glyphack
    added 2 commits that reference this issue on Sep 2, 2024

    pythongh-87868: Sort and remove duplicates in getenvironment() (pytho…

    888b7c5

    pythongh-87868: Skip `test_one_environment_variable` in `test_subproc…

    17dfdb8
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

    3.10 (EOL)end of lifeend of life3.11only security fixesonly security fixes3.9 (EOL)end of lifeend of lifeOS-windowsextension-modulesC modules in the Modules dirC modules in the Modules dirtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.