Skip to content

Commit b1b1d5f

Browse files
bpo-39916: Use os.scandir() as context manager in Path.glob(). (GH-18880)
(cherry picked from commit 704e206) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 3136f6f commit b1b1d5f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

Lib/pathlib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,8 @@ def __init__(self, pat, child_parts, flavour):
522522

523523
def _select_from(self, parent_path, is_dir, exists, scandir):
524524
try:
525-
entries = list(scandir(parent_path))
525+
with scandir(parent_path) as scandir_it:
526+
entries = list(scandir_it)
526527
for entry in entries:
527528
if self.dironly:
528529
try:
@@ -552,7 +553,8 @@ def __init__(self, pat, child_parts, flavour):
552553
def _iterate_directories(self, parent_path, is_dir, scandir):
553554
yield parent_path
554555
try:
555-
entries = list(scandir(parent_path))
556+
with scandir(parent_path) as scandir_it:
557+
entries = list(scandir_it)
556558
for entry in entries:
557559
entry_is_dir = False
558560
try:
Lines changed: 2 additions & 0 deletions
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)