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.

Title: Add follow_symlinks=True parameter to both os.path.samefile() and Path.samefile()
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Tom Hale, andrei.avk, serhiy.storchaka, trrhodes
Priority: normal Keywords: patch

Created on 2020-12-29 07:51 by Tom Hale, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 23996 closed trrhodes, 2020-12-29 12:36
Messages (6)
msg383965 - (view) Author: Tom Hale (Tom Hale) * Date: 2020-12-29 07:51
The os.path and Path implementations of samefile() do not allow comparisons of symbolic links:

% mkdir empty && chdir empty
% ln -s non-existant broken
% ln broken lnbroken
% ls -i # Show inode numbers
19325632 broken@  19325632 lnbroken@
% Yup, they are the same file... but...
% python -c 'import os; print(os.path.samefile("lnbroken", "broken", follow_symlinks=False))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
TypeError: samefile() got an unexpected keyword argument 'follow_symlinks'
% python -c 'import os; print(os.path.samefile("lnbroken", "broken"))'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.8/", line 100, in samefile
    s1 = os.stat(f1)
FileNotFoundError: [Errno 2] No such file or directory: 'lnbroken'

Both samefile()s use os.stat under the hood, but neither allow setting  os.stat()'s `follow_symlinks` parameter.
msg383966 - (view) Author: Tom Hale (Tom Hale) * Date: 2020-12-29 07:55
In summary:

The underlying os.stat() takes a follow_symlinks=True parameter but it can't be set to False when trying to samefile() two symbolic links.
msg383990 - (view) Author: Ross Rhodes (trrhodes) * Date: 2020-12-29 12:37
Hi Tom,

Thanks for raising this issue. I've opened a PR to permit us to set `follow_symlinks` in both os.path and pathlib. Feel free to leave feedback.
msg385324 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-01-20 10:27
Why is this feature needed? Currently you can use a combination of samestat() with lstat(). And more, you can follow symbolic links only for one of arguments.

samestat(stat(path1), stat(path1))  # same as samefile(path1, path1)
samestat(lstat(path1), stat(path1))
samestat(stat(path1), lstat(path1))
samestat(lstat(path1), lstat(path1))

samefile() covers one (presumably most common) of these cases. The proposed option would cover yet one (is it common enough?). And there are two mixed cases remained.
msg386660 - (view) Author: Ross Rhodes (trrhodes) * Date: 2021-02-08 20:32
Thanks for sharing the alternative approach, Serhiy. Sounds like the proposed changes aren’t necessary if the combined use of samestat and lstat achieve the desired behaviour.

Any objections if I close the open PR?
msg397111 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-07-07 20:12
Looks like this issue can be closed.
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86944
2021-07-08 06:46:34serhiy.storchakasetstatus: open -> closed
resolution: rejected
stage: patch review -> resolved
2021-07-07 20:12:21andrei.avksetnosy: + andrei.avk
messages: + msg397111
2021-02-08 20:32:07trrhodessetmessages: + msg386660
2021-01-20 10:27:09serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg385324
2020-12-29 12:37:46trrhodessetnosy: Tom Hale, trrhodes
messages: + msg383990
2020-12-29 12:36:29trrhodessetkeywords: + patch
nosy: + trrhodes

pull_requests: + pull_request22839
stage: patch review
2020-12-29 07:55:27Tom Halesetmessages: + msg383966
title: Add follow_symlinks=True to {os.path,Path}.samefile -> Add follow_symlinks=True parameter to both os.path.samefile() and Path.samefile()
2020-12-29 07:51:01Tom Halecreate