[New-bugs-announce] [issue44868] misleading error about fd / follow_symlinks from os.stat()
Andrei Kulakov
report at bugs.python.org
Sun Aug 8 16:18:13 EDT 2021
New submission from Andrei Kulakov <andrei.avk at gmail.com>:
(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 at 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).
----------
components: C API, Library (Lib)
messages: 399237
nosy: andrei.avk
priority: normal
severity: normal
status: open
title: misleading error about fd / follow_symlinks from os.stat()
type: behavior
versions: Python 3.11
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue44868>
_______________________________________
More information about the New-bugs-announce
mailing list