Skip to content

fix: resolve PUBLIC before looking up the user home - #546

Merged
gaborbernat merged 5 commits into
tox-dev:mainfrom
emme1t:fix/public-directory-without-home
Sep 16, 2026
Merged

gaborbernat merged 5 commits into
tox-dev:mainfrom
emme1t:fix/public-directory-without-home

Conversation

@emme1t

@emme1t emme1t commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

Windows().user_publicshare_dir raised RuntimeError: Could not determine home directory when PUBLIC was set but USERPROFILE and HOMEPATH were missing. Python evaluated the os.environ.get() default, Path("~").expanduser(), before the lookup.

import os
from unittest.mock import patch
from platformdirs.windows import Windows

with patch.dict(os.environ, {"PUBLIC": r"C:\Users\Shared"}, clear=True):
    print(Windows().user_publicshare_dir)

On main this raises RuntimeError; with the fix it prints C:\Users\Shared.

The home-based fallback now runs only when PUBLIC is unset. An empty PUBLIC still normalizes to .. Two tests cover an explicit PUBLIC with no home directory, and the fallback without PUBLIC; the first fails on main.

tox r passes on Python 3.12 and 3.13 (1,243 passed, 94 skipped) with 100% diff coverage.

Prepared with OpenAI Codex assistance.

@emme1t
emme1t marked this pull request as ready for review September 12, 2026 06:50
@gaborbernat
gaborbernat merged commit de87396 into tox-dev:main Sep 16, 2026
36 checks passed
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
user_publicshare_dir and the environment variable fallback resolver
checked only for None. PUBLIC="" gave "." and LOCALAPPDATA="" gave
".\Acme\MyApp", both relative to the working directory. The tox-dev#546 PR
noted the empty PUBLIC case and left it open.

A falsy check sends an empty PUBLIC to the home-based default and makes
the fallback resolver raise the same ValueError it raises for an unset
variable. The property still reads PUBLIC before touching the home
directory, so the tox-dev#546 fix holds when Python cannot find the home.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
On Windows, PUBLIC="" made user_publicshare_dir return "." and
LOCALAPPDATA="" made the environment variable fallback give
".\Acme\MyApp". Both point at the working directory. The two lookups
checked for None only, and the tox-dev#546 PR noted the empty PUBLIC case
without fixing it.

With a falsy check, user_publicshare_dir falls back to the home-based
Public folder, and the fallback resolver raises the ValueError it
raises for an unset variable. The property still reads
PUBLIC before it touches the home directory, so the tox-dev#546 fix holds
when Python cannot find the home.
gaborbernat added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
On Windows, PUBLIC="" made user_publicshare_dir return "." and
LOCALAPPDATA="" made the environment variable fallback give
".\Acme\MyApp". Both point at the working directory. The two lookups
checked for None only. The tox-dev#546 PR noted the empty PUBLIC case without
fixing it.

With a falsy check, user_publicshare_dir falls back to the home-based
Public folder, and the fallback resolver raises the ValueError it raises
for an unset variable. 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 added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
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 now 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 added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
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 added a commit to gaborbernat/platformdirs that referenced this pull request Sep 25, 2026
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 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants