From 9c2d09e7c6d76af710f45d81caa35ef813c35d6d Mon Sep 17 00:00:00 2001 From: barneygale Date: Sun, 9 Jan 2022 02:21:03 +0000 Subject: [PATCH] bpo-46316: optimize `pathlib.Path.iterdir()` `os.listdir()` doesn't return entries for `.` or `..`, so we don't need to check for them here. See https://docs.python.org/3/library/os.html#os.listdir --- Lib/pathlib.py | 3 --- .../next/Library/2022-01-09-15-04-56.bpo-46316.AMTyd0.rst | 1 + 2 files changed, 1 insertion(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-01-09-15-04-56.bpo-46316.AMTyd0.rst diff --git a/Lib/pathlib.py b/Lib/pathlib.py index f1a33178e2958ba..04b321b9ccf1668 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1013,9 +1013,6 @@ def iterdir(self): result for the special paths '.' and '..'. """ for name in self._accessor.listdir(self): - if name in {'.', '..'}: - # Yielding a path object for these makes little sense - continue yield self._make_child_relpath(name) def glob(self, pattern): diff --git a/Misc/NEWS.d/next/Library/2022-01-09-15-04-56.bpo-46316.AMTyd0.rst b/Misc/NEWS.d/next/Library/2022-01-09-15-04-56.bpo-46316.AMTyd0.rst new file mode 100644 index 000000000000000..09acb77855f15cb --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-09-15-04-56.bpo-46316.AMTyd0.rst @@ -0,0 +1 @@ +Optimize :meth:`pathlib.Path.iterdir` by removing an unnecessary check for special entries.