New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
os.path.realpath('notadir/', strict=True)
doesn't raise
#118289
Comments
In strict mode, raise `NotADirectoryError` if a file path is given with a trailing slash, or subsequent dot segments. We use a `part_count` variable rather than `len(rest)` because the `rest` stack also contains markers for unresolved symlinks.
Here's a list of all invalid paths for
Should I make an issue for the symlinks? |
It's deliberate behaviour - Does it differ from |
You mean the built-in function in macOS? wannes@Stefans-iMac dirs % realpath s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s
realpath: s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s: Too many levels of symbolic links Maybe a |
I have just tested the If one is still on If one is on |
>>> import os
>>> os.path.realpath('s/' * 64, strict=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen ntpath>", line 722, in realpath
OSError: [WinError 1921] The name of the file cannot be resolved by the system: 'C:\\Users\\wanne\\path-picker\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s\\s' |
Hmm, only with '--logical': wannes@Stefans-iMac dirs % grealpath -e s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s
/Users/wannes/path-picker/link-test/dirs
wannes@Stefans-iMac dirs % grealpath -L s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s
grealpath: s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s/s: Too many levels of symbolic links |
On POSIX systems, wouldn't it be reasonable for Linux: import os, ctypes
pg = ctypes.CDLL(None, use_errno=True)
buf = (ctypes.c_char * 4096)()
pg.realpath.restype = ctypes.c_char_p
pg.realpath.argtypes = (ctypes.c_char_p, ctypes.c_char_p)
for i in range(41):
os.symlink(f's{i+1}', f's{i}')
open('s41', 'w').close() >>> os.path.realpath('s0', strict=True)
'/home/user/Temp/test/symlinks/s41'
>>> pg.realpath(b's0', buf)
>>> ctypes.get_errno() == errno.ELOOP
True
>>> os.strerror(errno.ELOOP)
'Too many levels of symbolic links' |
Would that be instead of, or in addition to, the existing symlink loop detection? The current code uses a |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Let's track this further in #118441. |
How about secret symlinks? Should these be allowed in non-strict mode? wannes@Stefans-iMac dirs % sudo ls -l secret-symlink
l--------- 1 wannes staff 44 Jun 30 2023 secret-symlink -> /Users/wannes/path-picker/link-test/dirs/dir
wannes@Stefans-iMac dirs % grealpath -m secret-symlink
/Users/wannes/path-picker/link-test/dirs/secret-symlink >>> import posixpath
>>> posixpath.realpath("secret-symlink")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen posixpath>", line 435, in realpath
File "<frozen posixpath>", line 495, in _joinrealpath
PermissionError: [Errno 13] Permission denied: 'secret-symlink' |
Looks like a bug to me! |
OK, see #118447. |
Bug report
Bug description:
GNU coreutils
realpath -e
errors out when given a path to a file (not a directory) with a trailing slash:... but Python's
os.path.realpath(..., strict=True)
doesn't raise any error:CPython versions tested on:
3.12, 3.13, CPython main branch
Operating systems tested on:
Linux
Linked PRs
os.path.realpath()
#118290The text was updated successfully, but these errors were encountered: