]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 11 Mar 2020 17:07:04 +0000 (10:07 -0700)
committerGitHub <noreply@github.com>
Wed, 11 Mar 2020 17:07:04 +0000 (10:07 -0700)
(cherry picked from commit 704e2065f8b8021a4a6999470fb6ed3453f7679e)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Lib/pathlib.py
Misc/NEWS.d/next/Library/2020-03-09-18-56-27.bpo-39916.BHHyp3.rst [new file with mode: 0644]

index d188026bcde840b6f035f859b16a2e73f94f0101..ff8bac94bc0bea2edbd078a616812708e77ce796 100644 (file)
@@ -527,7 +527,8 @@ class _WildcardSelector(_Selector):
 
     def _select_from(self, parent_path, is_dir, exists, scandir):
         try:
-            entries = list(scandir(parent_path))
+            with scandir(parent_path) as scandir_it:
+                entries = list(scandir_it)
             for entry in entries:
                 if self.dironly:
                     try:
@@ -557,7 +558,8 @@ class _RecursiveWildcardSelector(_Selector):
     def _iterate_directories(self, parent_path, is_dir, scandir):
         yield parent_path
         try:
-            entries = list(scandir(parent_path))
+            with scandir(parent_path) as scandir_it:
+                entries = list(scandir_it)
             for entry in entries:
                 entry_is_dir = False
                 try:
diff --git a/Misc/NEWS.d/next/Library/2020-03-09-18-56-27.bpo-39916.BHHyp3.rst b/Misc/NEWS.d/next/Library/2020-03-09-18-56-27.bpo-39916.BHHyp3.rst
new file mode 100644 (file)
index 0000000..5f49062
--- /dev/null
@@ -0,0 +1,2 @@
+More reliable use of ``os.scandir()`` in ``Path.glob()``. It no longer emits
+a ResourceWarning when interrupted.