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.

Author andrei.avk
Recipients andrei.avk
Date 2021-08-08.20:18:12
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1628453893.12.0.662297515485.issue44868@roundup.psfhosted.org>
In-reply-to
Content
(note the actual relevant code is in posixmodule.c) 

os.stat() error can be confusing and misleading when given an fd and with follow_symlinks=False:

ValueError: stat: cannot use fd and follow_symlinks together

It's less bad when os.stat() is used directly because the user would look at the signature and would have provided the follow_symlinks=False directly, but it's confusing when used indirectly by other function.

I've ran into this when reviewing https://github.com/python/cpython/pull/27524

list(os.fwalk(1))
/usr/local/Cellar/python@3.9/3.9.1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/os.py in fwalk(top, topdown, onerror, follow_symlinks, dir_fd)
    467         # lstat()/open()/fstat() trick.
    468         if not follow_symlinks:
--> 469             orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
    470         topfd = open(top, O_RDONLY, dir_fd=dir_fd)
    471         try:

ValueError: stat: cannot use fd and follow_symlinks together

----
A few things are confusing here: I did not use follow_symlinks argument; I can see from traceback that the arg is used but set to False, which is the usual meaning of "do not use follow_symlinks".

In addition, fwalk() can probably check that `top` arg is a string and raise an error stating that it should be a string if it's not.

If that's done, this issue will no longer happen for current code anywhere in os module, but stat(follow_symlinks=False) is also used in shutil and pathlib (I didn't check if fd may be passed in those cases), but also in 3rd party libraries.

So I think it would be clearer to rephrase the error to say

stat: cannot use fd and follow_symlinks set to False or NULL together

(adding NULL as it may be used from C code).
History
Date User Action Args
2021-08-08 20:18:13andrei.avksetrecipients: + andrei.avk
2021-08-08 20:18:13andrei.avksetmessageid: <1628453893.12.0.662297515485.issue44868@roundup.psfhosted.org>
2021-08-08 20:18:13andrei.avklinkissue44868 messages
2021-08-08 20:18:12andrei.avkcreate