This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: pathlib PermissionError problem
Type: Stage: resolved
Components: FreeBSD, Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Jeffrey.Kintscher, andrei.avk, kj, koobs, leonyxz
Priority: normal Keywords:

Created on 2020-08-19 22:28 by leonyxz, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg375677 - (view) Author: leonyxz (leonyxz) Date: 2020-08-19 22:28
python 3.x
FreeBSD 11.3-RELEASE-p12 with custom settings mentioned later.
On vanilla configuration of FreeBSD there is no issue.
Installation of FreeBSD in question has mac_bsdextended turned on (https://www.freebsd.org/cgi/man.cgi?query=mac_bsdextended&sektion=4&apropos=0&manpath=FreeBSD+12.1-RELEASE+and+Ports)
Using ugidfw (https://www.freebsd.org/cgi/man.cgi?query=ugidfw&sektion=8&manpath=freebsd-release-ports), the following rule has been set:
---------------------------------------------------------------
0 subject not uid root gid nobody object gid wheel type d mode sx
---------------------------------------------------------------
The following code shows the minimal reproducible example:
---------------------------------------------------------------
from pathlib import Path
BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
print(BASE_DIR)
---------------------------------------------------------------
Running the code directly works:
---------------------------------------------------------------
$ python test.py
/usr/home/example
---------------------------------------------------------------
But importing it as a module does not:
---------------------------------------------------------------
$ python -m test
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/home/example/django_3136test/test.py", line 2, in <module>
    BASE_DIR = Path(__file__).resolve(strict=True).parent.parent
  File "/usr/local/lib/python3.6/pathlib.py", line 1141, in resolve
    s = self._flavour.resolve(self, strict=strict)
  File "/usr/local/lib/python3.6/pathlib.py", line 346, in resolve
    return _resolve(base, str(path)) or sep
  File "/usr/local/lib/python3.6/pathlib.py", line 330, in _resolve
    target = accessor.readlink(newpath)
  File "/usr/local/lib/python3.6/pathlib.py", line 440, in readlink
    return os.readlink(path)
PermissionError: [Errno 13] Permission denied: '/usr'
---------------------------------------------------------------
The issue seems to be comnected with following piece code
https://github.com/python/cpython/blob/master/Lib/pathlib.py#L342-L346
---------------------------------------------------------------
    try:
                    target = accessor.readlink(newpath)
                except OSError as e:
                    if e.errno != EINVAL and strict:
                        raise
                    # Not a symlink, or non-strict mode. We just leave the path
                    # untouched.
                    path = newpath
---------------------------------------------------------------
With ugidfw enabled, `os.readlink` raises a `PermissionError`, which is unhandled by pathlib.
msg409318 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-12-29 18:36
Cannot reproduce on 3.9 and 3.11; the quoted fragment of code is no longer in pathlib.

Closing as fixed as it seems that it was fixed in 3.9 or earlier.

Please comment if you think it should be reopened.
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85759
2021-12-29 18:36:41andrei.avksetstatus: open -> closed

nosy: + kj, andrei.avk
messages: + msg409318

resolution: fixed
stage: resolved
2020-08-20 02:40:18Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2020-08-19 22:28:29leonyxzcreate