Skip to content

Commit 7ed4fb5

Browse files
committed
Fix OSFS.scandir calling os.stat up to 3 times when several namespaces are requested
1 parent 67a8ce9 commit 7ed4fb5

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

fs/osfs.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ def _scandir(self, path, namespaces=None):
475475
# type: (Text, Optional[Collection[Text]]) -> Iterator[Info]
476476
self.check()
477477
namespaces = namespaces or ()
478+
requires_stat = not {"details", "stat", "access"}.isdisjoint(namespaces)
478479
_path = self.validatepath(path)
479480
if _WINDOWS_PLATFORM:
480481
sys_path = os.path.join(
@@ -492,16 +493,18 @@ def _scandir(self, path, namespaces=None):
492493
"is_dir": dir_entry.is_dir(),
493494
}
494495
}
495-
if "details" in namespaces:
496+
if requires_stat:
496497
stat_result = dir_entry.stat()
497-
info["details"] = self._make_details_from_stat(stat_result)
498-
if "stat" in namespaces:
499-
stat_result = dir_entry.stat()
500-
info["stat"] = {
501-
k: getattr(stat_result, k)
502-
for k in dir(stat_result)
503-
if k.startswith("st_")
504-
}
498+
if "details" in namespaces:
499+
info["details"] = self._make_details_from_stat(stat_result)
500+
if "stat" in namespaces:
501+
info["stat"] = {
502+
k: getattr(stat_result, k)
503+
for k in dir(stat_result)
504+
if k.startswith("st_")
505+
}
506+
if "access" in namespaces:
507+
info["access"] = self._make_access_from_stat(stat_result)
505508
if "lstat" in namespaces:
506509
lstat_result = dir_entry.stat(follow_symlinks=False)
507510
info["lstat"] = {
@@ -513,10 +516,7 @@ def _scandir(self, path, namespaces=None):
513516
info["link"] = self._make_link_info(
514517
os.path.join(sys_path, dir_entry.name)
515518
)
516-
if "access" in namespaces:
517-
stat_result = dir_entry.stat()
518-
info["access"] = self._make_access_from_stat(stat_result)
519-
519+
520520
yield Info(info)
521521
finally:
522522
if sys.version_info >= (3, 6):

0 commit comments

Comments
 (0)