Repository navigation
🐛 fix(windows): treat empty PUBLIC and fallback env vars as unset - #571
Merged
Merged
Conversation
gaborbernat
force-pushed
the
fix/win-empty-env
branch
5 times, most recently
from
September 25, 2026 15:57
21d009c to
dfe31b8
Compare
On Windows, PUBLIC="" made user_publicshare_dir return "." and LOCALAPPDATA="" made the environment variable fallback give ".\Acme\MyApp". The same fallback turned an empty USERPROFILE or APPDATA into relative media and Start Menu folders such as Documents, and ignored the ALLUSERSPROFILE default when PROGRAMDATA was empty. These lookups checked for a missing variable only. The tox-dev#546 PR noted the empty PUBLIC case without fixing it. Each lookup treats an empty value as unset. user_publicshare_dir falls back to the home-based Public folder, LOCALAPPDATA raises the ValueError of a missing variable, and USERPROFILE and APPDATA raise the KeyError that os.environ raises when they are unset. An empty PROGRAMDATA falls through to ALLUSERSPROFILE and C:\ProgramData. The property keeps reading PUBLIC before it touches the home directory, so the tox-dev#546 fix holds when Python cannot find the home.
gaborbernat
force-pushed
the
fix/win-empty-env
branch
from
September 25, 2026 16:50
dfe31b8 to
e9cff4e
Compare
gaborbernat
added a commit
to gaborbernat/platformdirs
that referenced
this pull request
Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable. Windows lets a user redirect per-user known folders such as FOLDERID_Templates and FOLDERID_UserProgramFiles; after such a move platformdirs kept returning the default location. Qt asks for FOLDERID_Templates and FOLDERID_Public. The three now share the resolver of the other folders, so a matching WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers for Templates. The environment fallback keeps the old joins and reads PUBLIC before the home directory, treating an empty value as unset as tox-dev#546 and tox-dev#571 require.
gaborbernat
added a commit
to gaborbernat/platformdirs
that referenced
this pull request
Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable. Windows lets a user redirect per-user known folders such as FOLDERID_Templates and FOLDERID_UserProgramFiles; after such a move platformdirs kept returning the default location. Qt asks for FOLDERID_Templates and FOLDERID_Public. The three now share the resolver of the other folders, so a matching WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers for Templates. The environment fallback keeps the old joins and reads PUBLIC before the home directory, treating an empty value as unset as tox-dev#546 and tox-dev#571 require. Four tests restored os.environ in the wrong order and left HOME, USERPROFILE, PUBLIC or APPDATA changed for later tests. On Windows, SHGetKnownFolderPath expanded the leaked USERPROFILE, so the check against the default layout needs a clean environment.
gaborbernat
added a commit
to gaborbernat/platformdirs
that referenced
this pull request
Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable. Windows lets a user redirect per-user known folders such as FOLDERID_Templates and FOLDERID_UserProgramFiles; after such a move platformdirs kept returning the default location. Qt asks for FOLDERID_Templates and FOLDERID_Public. The three now share the resolver of the other folders, so a matching WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers for Templates. The environment fallback keeps the old joins and reads PUBLIC before the home directory, treating an empty value as unset as tox-dev#546 and tox-dev#571 require. Four tests restored os.environ in the wrong order and left HOME, USERPROFILE, PUBLIC or APPDATA changed for later tests. On Windows, SHGetKnownFolderPath expanded the leaked USERPROFILE, so the check against the default layout needs a clean environment.
gaborbernat
added a commit
to gaborbernat/platformdirs
that referenced
this pull request
Sep 26, 2026
user_templates_dir and user_bin_dir joined fixed names onto APPDATA and LOCALAPPDATA, and user_publicshare_dir read the PUBLIC variable, so platformdirs did not ask Windows for these folders. A user can redirect FOLDERID_Templates and FOLDERID_UserProgramFiles, and after such a move platformdirs kept returning the default location. Qt asks for FOLDERID_Templates and FOLDERID_Public. The three share the resolver of the other folders, so a matching WIN_PD_OVERRIDE_* variable wins. Without ctypes, the registry answers for Templates. The environment fallback keeps the old joins and reads PUBLIC before the home directory, treating an empty value as unset as tox-dev#546 and tox-dev#571 require. Four tests restored os.environ in the wrong order. They left HOME, USERPROFILE, PUBLIC or APPDATA changed for the tests after them, and on Windows SHGetKnownFolderPath expanded the leaked USERPROFILE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows, an empty
PUBLICmadeuser_publicshare_dir()return.. On interpreters that resolve folders from environment variables, an emptyLOCALAPPDATAmadeuser_data_dir("MyApp", "Acme")return.\Acme\MyApp, and an emptyUSERPROFILEgave a relative media folder such asDocuments. 🐛 Each of these points at the working directory. #546 moved thePUBLIClookup ahead of the home directory and noted the empty case without fixing it.platformdirs treats an empty value as unset in each lookup.
user_publicshare_dir()falls back to thePublicfolder next to the home directory. In the environment variable fallback an empty variable fails with the error an unset one raises,ValueErrorforLOCALAPPDATAandKeyErrorforUSERPROFILEandAPPDATA, and an emptyPROGRAMDATAfalls through toALLUSERSPROFILEandC:\ProgramData.PUBLICkeeps its place ahead of the home directory lookup, so the #546 case ofPUBLICset with no resolvable home keeps returningPUBLIC.