You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Setting permissions on a symlink is a BSD (including macOS) feature. Linux doesn't allow it. Testing this should be limited to platforms with os.chmod in os.supports_follow_symlinks.
That's probably a reason which this is not part of POSIX... I expected you needed permission to follow it, not to determine the real location.
Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path.
We can't eliminate this symlink, because we don't know where it points to, which we need to know for determining the parent directory. Also, coreutils doesn't even raise an error in strict mode:
On macOS and NetBSD, fcntl() supports F_GETPATH. In that case, you could try to open "secret-symlink" and query the resolved path, e.g. target = os.fsdecode(fcntl.fcntl(fd, fcntl.F_GETPATH, bytes(1024))). You may have to revisit the design of fcntl.fcntl() to make it use an internal buffer bigger than 1024 bytes, though I think that's currently the maximum path length supported by macOS.
Activity
eryksun commentedon Apr 30, 2024
Setting permissions on a symlink is a BSD (including macOS) feature. Linux doesn't allow it. Testing this should be limited to platforms with
os.chmod in os.supports_follow_symlinks.pythonGH-118447: Handle unreadable symlinks in `os.path.realpath()`
nineteendo commentedon May 2, 2024
Upon closer inspection, this is a bug in coreutils:
barneygale commentedon May 2, 2024
I wouldn't expect
os.path.realpath(..., strict=False)to raiseOSError, no matter what coreutils does!nineteendo commentedon May 2, 2024
That's probably a reason which this is not part of POSIX... I expected you needed permission to follow it, not to determine the real location.
We can't eliminate this symlink, because we don't know where it points to,
which we need to know for determining the parent directory. Also, coreutils doesn't even raise an error in strict mode:Raising an error here will lead to the fewest bugs.eryksun commentedon May 2, 2024
On macOS and NetBSD,
fcntl()supportsF_GETPATH. In that case, you could try to open "secret-symlink" and query the resolved path, e.g.target = os.fsdecode(fcntl.fcntl(fd, fcntl.F_GETPATH, bytes(1024))). You may have to revisit the design offcntl.fcntl()to make it use an internal buffer bigger than 1024 bytes, though I think that's currently the maximum path length supported by macOS.nineteendo commentedon May 2, 2024
That works:
But I think it might be cleaner to fix this in readlink...
nineteendo commentedon May 3, 2024
But it doesn't work for broken symlinks:
GH-118447: Fix handling of unreadable symlinks in `os.path.realpath()` (
pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…
pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…
[3.12] pythonGH-118447: Fix handling of unreadable symlinks in `os.pa…
[3.13] GH-118447: Fix handling of unreadable symlinks in `os.path.rea…
pythonGH-118447: Fix FreeBSD test failures.
GH-118447: Fix FreeBSD test failures. (#119170)
pythonGH-118447: Fix FreeBSD test failures. (pythonGH-119170)
[3.13] GH-118447: Fix FreeBSD test failures. (GH-119170) (#119181)
nineteendo commentedon May 22, 2024
With the FreeBSD test failures fixed, we can close this.
pythonGH-118447: Fix handling of unreadable symlinks in `os.path.real…
pythonGH-118447: Fix FreeBSD test failures. (python#119170)