Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/7495.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make the bundled appdirs compatible with Python 3 on Windows when ``ctypes`` is not available.
11 changes: 7 additions & 4 deletions src/pip/_internal/utils/appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,22 @@ def _get_win_folder_from_registry(csidl_name):
registry for this guarantees us the correct answer for all CSIDL_*
names.
"""
import _winreg
if PY2:
import _winreg as winreg
else:
import winreg

shell_folder_name = {
"CSIDL_APPDATA": "AppData",
"CSIDL_COMMON_APPDATA": "Common AppData",
"CSIDL_LOCAL_APPDATA": "Local AppData",
}[csidl_name]

key = _winreg.OpenKey(
_winreg.HKEY_CURRENT_USER,
key = winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
)
directory, _type = _winreg.QueryValueEx(key, shell_folder_name)
directory, _type = winreg.QueryValueEx(key, shell_folder_name)
return directory


Expand Down
76 changes: 76 additions & 0 deletions tests/unit/test_appdirs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import posixpath
import sys
import types

import pretend

Expand Down Expand Up @@ -300,3 +301,78 @@ def test_user_config_dir_linux_home_slash(self, monkeypatch):
monkeypatch.setattr(sys, "platform", "linux2")

assert appdirs.user_config_dir("pip") == "/.config/pip"


class TestGetWinFolder:

def test_win_folder_ctypes(self, monkeypatch):
if sys.platform != 'win32':
return

@pretend.call_recorder
def _get_win_folder_from_registry(csidl_name):
return "return-value-from-registry"

@pretend.call_recorder
def _get_win_folder_with_ctypes(csidl_name):
return "return-value-from-ctypes"

monkeypatch.setitem(sys.modules, 'ctypes', types.ModuleType('ctypes'))
monkeypatch.setattr(
appdirs,
"_get_win_folder_from_registry",
_get_win_folder_from_registry,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was actually hoping that winreg.OpenKey and winreg.QueryValueEx could be called for real on Windows. Is there a blocker ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocker but I don’t think it’s meaningful. Their return values might not be predictable (being essentially system configuration).

raising=False,
)
monkeypatch.setattr(
appdirs,
"_get_win_folder_with_ctypes",
_get_win_folder_with_ctypes,
raising=False,
)

assert (
appdirs._get_win_folder("place-holder") ==
"return-value-from-ctypes"
)
assert (
_get_win_folder_with_ctypes.calls ==
[pretend.call("place-holder")]
)
assert _get_win_folder_from_registry.calls == []

def test_win_folder_registry(self, monkeypatch):
if sys.platform != 'win32':
return

@pretend.call_recorder
def _get_win_folder_from_registry(csidl_name):
return "return-value-from-registry"

@pretend.call_recorder
def _get_win_folder_with_ctypes(csidl_name):
return "return-value-from-ctypes"

monkeypatch.setitem(sys.modules, 'ctypes', None)
monkeypatch.setattr(
appdirs,
"_get_win_folder_from_registry",
_get_win_folder_from_registry,
raising=False,
)
monkeypatch.setattr(
appdirs,
"_get_win_folder_with_ctypes",
_get_win_folder_with_ctypes,
raising=False,
)

assert (
appdirs._get_win_folder("place-holder") ==
"return-value-from-registry"
)
assert _get_win_folder_with_ctypes.calls == []
assert (
_get_win_folder_from_registry.calls ==
[pretend.call("place-holder")]
)