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: misleading error about fd / follow_symlinks from os.stat()
Type: behavior Stage:
Components: C API, Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk
Priority: normal Keywords:

Created on 2021-08-08 20:18 by andrei.avk, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg399237 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-08-08 20:18
(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).
msg399239 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-08-08 20:20
> 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.

I should clarify that fwalk() cannot accept an fd as the `top` arg, see https://bugs.python.org/issue42053
History
Date User Action Args
2022-04-11 14:59:48adminsetgithub: 89031
2021-08-08 20:20:23andrei.avksetmessages: + msg399239
2021-08-08 20:18:13andrei.avkcreate