Skip to content

Commit e4dbdb8

Browse files
Fix no-ctypes fallback on windows (#403)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 4cc78aa commit e4dbdb8

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/platformdirs/windows.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ def get_win_folder_from_registry(csidl_name: str) -> str:
188188
for all CSIDL_* names.
189189
190190
"""
191+
machine_names = {
192+
"CSIDL_COMMON_APPDATA",
193+
}
191194
shell_folder_name = {
192195
"CSIDL_APPDATA": "AppData",
193196
"CSIDL_COMMON_APPDATA": "Common AppData",
@@ -205,7 +208,10 @@ def get_win_folder_from_registry(csidl_name: str) -> str:
205208
raise NotImplementedError
206209
import winreg # noqa: PLC0415
207210

208-
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
211+
# Use HKEY_LOCAL_MACHINE for system-wide folders, HKEY_CURRENT_USER for user-specific folders
212+
hkey = winreg.HKEY_LOCAL_MACHINE if csidl_name in machine_names else winreg.HKEY_CURRENT_USER
213+
214+
key = winreg.OpenKey(hkey, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
209215
directory, _ = winreg.QueryValueEx(key, shell_folder_name)
210216
return str(directory)
211217

tests/test_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ def _fake_import(name: str, *args: Any, **kwargs: Any) -> ModuleType: # noqa: A
9999
return builtin_import(name, *args, **kwargs)
100100

101101

102-
def mock_import(func: Callable[[], None]) -> Callable[[], None]:
102+
def mock_import(func: Callable[..., None]) -> Callable[..., None]:
103103
@functools.wraps(func)
104-
def wrap() -> None:
104+
def wrap(*args: Any, **kwargs: Any) -> None: # noqa: ANN401
105105
platformdirs_module_items = [item for item in sys.modules.items() if item[0].startswith("platformdirs")]
106106
try:
107107
builtins.__import__ = _fake_import
108108
for name, _ in platformdirs_module_items:
109109
del sys.modules[name]
110-
return func()
110+
return func(*args, **kwargs)
111111
finally:
112112
# restore original modules
113113
builtins.__import__ = builtin_import
@@ -118,11 +118,15 @@ def wrap() -> None:
118118

119119

120120
@mock_import
121-
def test_no_ctypes() -> None:
121+
def test_no_ctypes(func: str) -> None:
122122
import platformdirs # noqa: PLC0415
123123

124124
assert platformdirs
125125

126+
dirs = platformdirs.PlatformDirs("MyApp", "MyCompany", version="1.0")
127+
result = getattr(dirs, func)
128+
assert isinstance(result, str)
129+
126130

127131
def test_mypy_subclassing() -> None:
128132
# Ensure that PlatformDirs / AppDirs is seen as a valid superclass by mypy

0 commit comments

Comments
 (0)