diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 851aabd479725f..293462000c2a6a 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -527,7 +527,8 @@ def __init__(self, pat, child_parts, flavour): 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 @@ def __init__(self, pat, child_parts, flavour): 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 index 00000000000000..5f490627b21e39 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-03-09-18-56-27.bpo-39916.BHHyp3.rst @@ -0,0 +1,2 @@ +More reliable use of ``os.scandir()`` in ``Path.glob()``. It no longer emits +a ResourceWarning when interrupted.