When switching from os.listdir to os.scandir 7 years ago somebody forgot to update the definition of _use_fd_functions in test_shutil together with shutil module.
in shutil:
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
os.supports_dir_fd and
os.scandir in os.supports_fd and
os.stat in os.supports_follow_symlinks)
in test_shutil:
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
os.supports_dir_fd and
os.listdir in os.supports_fd and
os.stat in os.supports_follow_symlinks)
I believe there is no need to calculate this variable twice so I'm gonna propose a PR where I use the value from shutil.py in its tests. Nobody will ever forget to update both places again.
As I'm thinking about it more there might be a reason why this is calculated again in the tests and then compared with the value from the shutil module.
But given the current status, this check:
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
os.supports_dir_fd and
os.listdir in os.supports_fd and
os.stat in os.supports_follow_symlinks)
if _use_fd_functions:
self.assertTrue(shutil._use_fd_functions)
in test_rmtree_uses_safe_fd_version_if_available checks whether both os.listdir and os.scandir are in os.supports_fd because that's the only difference between _use_fd_functions and shutil._use_fd_functions in this context.
I've opened the PR implementing what @encukou suggested: the test now uses shutil._use_fd_functions directly instead of recomputing it with the stale os.listdir check.
Activity
frenzymadness commented on May 14, 2024
As I'm thinking about it more there might be a reason why this is calculated again in the tests and then compared with the value from the shutil module.
But given the current status, this check:
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <= os.supports_dir_fd and os.listdir in os.supports_fd and os.stat in os.supports_follow_symlinks) if _use_fd_functions: self.assertTrue(shutil._use_fd_functions)in test_rmtree_uses_safe_fd_version_if_available checks whether both os.listdir and os.scandir are in os.supports_fd because that's the only difference between _use_fd_functions and shutil._use_fd_functions in this context.
Does it make sense to test this?
frenzymadness commented on May 14, 2024
Ccing @encukou since we were discussing it briefly.
encukou commented on May 30, 2024
If the difference is intentional, it should have a comment. I don't think it is.
IMO, the repeated code in the test is not useful and we can use shutil._use_fd_functions directly.
@serhiy-storchaka do you agree?
vasiliyk commented on Sep 21, 2026
I've opened the PR implementing what @encukou suggested: the test now uses shutil._use_fd_functions directly instead of recomputing it with the stale os.listdir check.
gh-119048: Use shutil._use_fd_functions directly in test (GH-157912)