[Python-checkins] bpo-46316: optimize `pathlib.Path.iterdir()` (GH-30501)

zware webhook-mailer at python.org
Thu Jan 20 14:20:08 EST 2022


https://github.com/python/cpython/commit/a1c88414926610a3527398a478c3e63c531dc742
commit: a1c88414926610a3527398a478c3e63c531dc742
branch: main
author: Barney Gale <barney.gale at gmail.com>
committer: zware <zachary.ware at gmail.com>
date: 2022-01-20T13:20:00-06:00
summary:

bpo-46316: optimize `pathlib.Path.iterdir()` (GH-30501)

`os.listdir()` doesn't return entries for `.` or `..`, so we don't need to
check for them here.

files:
A Misc/NEWS.d/next/Library/2022-01-09-15-04-56.bpo-46316.AMTyd0.rst
M Lib/pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index f1a33178e2958..04b321b9ccf16 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 0000000000000..09acb77855f15
--- /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.



More information about the Python-checkins mailing list