Repository: pypa/pip

Issue:
Installing packages with non-privileged users on Windows, failed with PermissionError
**Environment**

* pip version: 20.0.2
* Python version: 3.8.2
* OS: Windows 10

<!-- Feel free to add more information about your environment here -->

**Description**
<!-- A clear and concise description of what the bug is. -->
Installing packages with non-privileged users on Windows, failed with PermissionError.

**Expected behavior**
<!-- A clear and concise description of what you expected to happen. -->
Should detect site-packages folder is not writable, try install to user appdata folder instead.

**How to Reproduce**
<!-- Describe the steps to reproduce this bug. -->
Run `pip install pandas` with non-privileged user on windows.

**Output**

```
ERROR: Exception:
Traceback (most recent call last):
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\cli\base_command.py", line 186, in _main
    status = self.run(options, args)
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\commands\install.py", line 253, in run
    options.use_user_site = decide_user_install(
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\commands\install.py", line 604, in decide_user_install
    if site_packages_writable(root=root_path, isolated=isolated_mode):
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\commands\install.py", line 548, in site_packages_writable
    return all(
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\commands\install.py", line 549, in <genexpr>
    test_writable_dir(d) for d in set(get_lib_location_guesses(**kwargs))
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\utils\filesystem.py", line 140, in test_writable_dir
    return _test_writable_dir_win(path)
  File "c:\program files (x86)\python38-32\lib\site-packages\pip\_internal\utils\filesystem.py", line 153, in _test_writable_dir_win
    fd = os.open(file, os.O_RDWR | os.O_CREAT | os.O_EXCL)
PermissionError: [Errno 13] Permission denied: 'c:\\program files (x86)\\python38-32\\Lib\\site-packages\\accesstest_deleteme_fishfingers_custard_m59lch'
```
With pdb I notice `os.open` raise a `OSError` which `errno` is `errno.EACCES`.


Discussion:
Related to https://github.com/pypa/pip/issues/1668

Resolving patch:
diff --git a/src/pip/_internal/utils/filesystem.py b/src/pip/_internal/utils/filesystem.py
--- a/src/pip/_internal/utils/filesystem.py
+++ b/src/pip/_internal/utils/filesystem.py
@@ -158,10 +158,13 @@ def _test_writable_dir_win(path):
         file = os.path.join(path, name)
         try:
             fd = os.open(file, os.O_RDWR | os.O_CREAT | os.O_EXCL)
+        # Python 2 doesn't support FileExistsError and PermissionError.
         except OSError as e:
+            # exception FileExistsError
             if e.errno == errno.EEXIST:
                 continue
-            if e.errno == errno.EPERM:
+            # exception PermissionError
+            if e.errno == errno.EPERM or e.errno == errno.EACCES:
                 # This could be because there's a directory with the same name.
                 # But it's highly unlikely there's a directory called that,
                 # so we'll assume it's because the parent dir is not writable.


Test patch: