You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Difference in _use_fd_functions in shutil.py and test_shutil.py #119048
When switching from os.listdir to os.scandir7 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 commentedon 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:
in
test_rmtree_uses_safe_fd_version_if_availablechecks whether both os.listdir and os.scandir are inos.supports_fdbecause that's the only difference between_use_fd_functionsandshutil._use_fd_functionsin this context.Does it make sense to test this?
frenzymadness commentedon May 14, 2024
Ccing @encukou since we were discussing it briefly.
encukou commentedon 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_functionsdirectly.@serhiy-storchaka do you agree?
vasiliyk commentedon Sep 21, 2026
I've opened the PR implementing what @encukou suggested: the test now uses
shutil._use_fd_functionsdirectly instead of recomputing it with the staleos.listdircheck.gh-119048: Use shutil._use_fd_functions directly in test (GH-157912)