[New-bugs-announce] [issue43702] [Windows] correctly sort and remove duplicates in _winapi getenvironment()
report at bugs.python.org
Fri Apr 2 00:16:14 EDT 2021
New submission from Eryk Sun <eryksun at gmail.com>:
getenvironment() in Modules/_winapi.c needs to sort variables in the environment block and remove duplicates case insensitively .
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 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() .
_winapi.LCMapStringEx(src, flags=LCMAP_UPPERCASE, locale=LOCALE_NAME_INVARIANT) could also be used in ntpath.normcase(), which would resolve bpo-42658.
components: Extension Modules, Windows
nosy: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
stage: needs patch
title: [Windows] correctly sort and remove duplicates in _winapi getenvironment()
versions: Python 3.10, Python 3.8, Python 3.9
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce