[Python-checkins] bpo-35692: pathlib no longer raises when checking file and directory existence on drives that are not ready (GH-11746)

Miss Islington (bot) webhook-mailer at python.org
Mon Feb 4 02:27:40 EST 2019


https://github.com/python/cpython/commit/69af4395a25481e9de4009c816b6d1f032a2d8eb
commit: 69af4395a25481e9de4009c816b6d1f032a2d8eb
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2019-02-03T23:27:37-08:00
summary:

bpo-35692: pathlib no longer raises when checking file and directory existence on drives that are not ready (GH-11746)

(cherry picked from commit 2f6fae6e510dba653391cb510a2aca8322eec03b)

Co-authored-by: Steve Dower <steve.dower at microsoft.com>

files:
A Misc/NEWS.d/next/Windows/2019-02-02-16-23-57.bpo-35692.cIiiE9.rst
M Lib/pathlib.py

diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index c2986bd022d0..067edf1221e9 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -37,6 +37,15 @@
 # EBADF - guard agains macOS `stat` throwing EBADF
 _IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF)
 
+_IGNORED_WINERRORS = (
+    21,  # ERROR_NOT_READY - drive exists but is not accessible
+)
+
+def _ignore_error(exception):
+    return (getattr(exception, 'errno', None) in _IGNORED_ERROS or
+            getattr(exception, 'winerror', None) in _IGNORED_WINERRORS)
+
+
 def _is_wildcard_pattern(pat):
     # Whether this pattern needs actual matching using fnmatch, or can
     # be looked up directly as a file.
@@ -535,7 +544,7 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
                 try:
                     entry_is_dir = entry.is_dir()
                 except OSError as e:
-                    if e.errno not in _IGNORED_ERROS:
+                    if not _ignore_error(e):
                         raise
                 if entry_is_dir and not entry.is_symlink():
                     path = parent_path._make_child_relpath(entry.name)
@@ -1328,7 +1337,7 @@ def exists(self):
         try:
             self.stat()
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             return False
         return True
@@ -1340,7 +1349,7 @@ def is_dir(self):
         try:
             return S_ISDIR(self.stat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist or is a broken symlink
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1354,7 +1363,7 @@ def is_file(self):
         try:
             return S_ISREG(self.stat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist or is a broken symlink
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1388,7 +1397,7 @@ def is_symlink(self):
         try:
             return S_ISLNK(self.lstat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist
             return False
@@ -1400,7 +1409,7 @@ def is_block_device(self):
         try:
             return S_ISBLK(self.stat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist or is a broken symlink
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1413,7 +1422,7 @@ def is_char_device(self):
         try:
             return S_ISCHR(self.stat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist or is a broken symlink
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1426,7 +1435,7 @@ def is_fifo(self):
         try:
             return S_ISFIFO(self.stat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist or is a broken symlink
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1439,7 +1448,7 @@ def is_socket(self):
         try:
             return S_ISSOCK(self.stat().st_mode)
         except OSError as e:
-            if e.errno not in _IGNORED_ERROS:
+            if not _ignore_error(e):
                 raise
             # Path doesn't exist or is a broken symlink
             # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
diff --git a/Misc/NEWS.d/next/Windows/2019-02-02-16-23-57.bpo-35692.cIiiE9.rst b/Misc/NEWS.d/next/Windows/2019-02-02-16-23-57.bpo-35692.cIiiE9.rst
new file mode 100644
index 000000000000..f3715739d4df
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2019-02-02-16-23-57.bpo-35692.cIiiE9.rst
@@ -0,0 +1,2 @@
+``pathlib`` no longer raises when checking file and directory existence on
+drives that are not ready



More information about the Python-checkins mailing list