Repository: psf/black

Issue:
Exception raised if installed under root.
**Describe the bug**
If black is installed under root and run as restricted user following exception is raised:
```
python3 -m black --diff doc gcovr
Running chmod with sudoTraceback (most recent call last):
  File "/usr/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.6/dist-packages/black/__main__.py", line 3, in <module>
    patched_main()
  File "/usr/local/lib/python3.6/dist-packages/black/__init__.py", line 7002, in patched_main
    main()
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 781, in main
    with self.make_context(prog_name, args, **extra) as ctx:
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 700, in make_context
    self.parse_args(ctx, args)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1048, in parse_args
    value, args = param.handle_parse_result(ctx, opts, args)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1630, in handle_parse_result
    value = invoke_param_callback(self.callback, ctx, self, value)
  File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 123, in invoke_param_callback
    return callback(ctx, param, value)
  File "/usr/local/lib/python3.6/dist-packages/black/__init__.py", line 337, in read_pyproject_toml
    value = find_pyproject_toml(ctx.params.get("src", ()))
  File "/usr/local/lib/python3.6/dist-packages/black/__init__.py", line 315, in find_pyproject_toml
    return str(path_user_pyproject_toml) if path_user_pyproject_toml.is_file() else None
  File "/usr/lib/python3.6/pathlib.py", line 1362, in is_file
    return S_ISREG(self.stat().st_mode)
  File "/usr/lib/python3.6/pathlib.py", line 1158, in stat
    return self._accessor.stat(self)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
PermissionError: [Errno 13] Permission denied: '/root/.config/black'
```

**To Reproduce**

1. Install black as root
2. Run `python3 -m black --diff .`
3. See error (e.g. https://github.com/Spacetown/gcovr/runs/2450843804?check_suite_focus=true)

**Expected behavior**

The code shall formatted the same way as in `20.8b1, see https://github.com/Spacetown/gcovr/runs/2433190163?check_suite_focus=true.

**Environment (please complete the following information):**

- Version: 21.4b1
- OS and Python version: Docker image based on ubuntu:18.04 with Python 3.6.7.

**Does this bug also happen on master?**

This is the current master.

**Additional context**
-


Discussion:

Resolving patch:
diff --git a/src/black/__init__.py b/src/black/__init__.py
--- a/src/black/__init__.py
+++ b/src/black/__init__.py
@@ -311,8 +311,17 @@ def find_pyproject_toml(path_search_start: Tuple[str, ...]) -> Optional[str]:
     if path_pyproject_toml.is_file():
         return str(path_pyproject_toml)
 
-    path_user_pyproject_toml = find_user_pyproject_toml()
-    return str(path_user_pyproject_toml) if path_user_pyproject_toml.is_file() else None
+    try:
+        path_user_pyproject_toml = find_user_pyproject_toml()
+        return (
+            str(path_user_pyproject_toml)
+            if path_user_pyproject_toml.is_file()
+            else None
+        )
+    except PermissionError as e:
+        # We do not have access to the user-level config directory, so ignore it.
+        err(f"Ignoring user configuration directory due to {e!r}")
+        return None
 
 
 def parse_pyproject_toml(path_config: str) -> Dict[str, Any]:


Test patch: