Skip to content

Commit 704e206

Browse files
bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880)
1 parent e553f20 commit 704e206

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Lib/pathlib.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,8 @@ def __init__(self, pat, child_parts, flavour):
527527

528528
def _select_from(self, parent_path, is_dir, exists, scandir):
529529
try:
530-
entries = list(scandir(parent_path))
530+
with scandir(parent_path) as scandir_it:
531+
entries = list(scandir_it)
531532
for entry in entries:
532533
if self.dironly:
533534
try:
@@ -557,7 +558,8 @@ def __init__(self, pat, child_parts, flavour):
557558
def _iterate_directories(self, parent_path, is_dir, scandir):
558559
yield parent_path
559560
try:
560-
entries = list(scandir(parent_path))
561+
with scandir(parent_path) as scandir_it:
562+
entries = list(scandir_it)
561563
for entry in entries:
562564
entry_is_dir = False
563565
try:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
More reliable use of ``os.scandir()`` in ``Path.glob()``. It no longer emits
2+
a ResourceWarning when interrupted.

0 commit comments

Comments
 (0)