Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f6fae6

Browse files
authored
bpo-35692: pathlib no longer raises when checking file and directory existence on drives that are not ready (GH-11746)
1 parent f75d59e commit 2f6fae6

2 files changed

Lines changed: 20 additions & 9 deletions

File tree

Lib/pathlib.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@
3737
# EBADF - guard agains macOS `stat` throwing EBADF
3838
_IGNORED_ERROS = (ENOENT, ENOTDIR, EBADF)
3939

40+
_IGNORED_WINERRORS = (
41+
21, # ERROR_NOT_READY - drive exists but is not accessible
42+
)
43+
44+
def _ignore_error(exception):
45+
return (getattr(exception, 'errno', None) in _IGNORED_ERROS or
46+
getattr(exception, 'winerror', None) in _IGNORED_WINERRORS)
47+
48+
4049
def _is_wildcard_pattern(pat):
4150
# Whether this pattern needs actual matching using fnmatch, or can
4251
# be looked up directly as a file.
@@ -535,7 +544,7 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
535544
try:
536545
entry_is_dir = entry.is_dir()
537546
except OSError as e:
538-
if e.errno not in _IGNORED_ERROS:
547+
if not _ignore_error(e):
539548
raise
540549
if entry_is_dir and not entry.is_symlink():
541550
path = parent_path._make_child_relpath(entry.name)
@@ -1328,7 +1337,7 @@ def exists(self):
13281337
try:
13291338
self.stat()
13301339
except OSError as e:
1331-
if e.errno not in _IGNORED_ERROS:
1340+
if not _ignore_error(e):
13321341
raise
13331342
return False
13341343
except ValueError:
@@ -1343,7 +1352,7 @@ def is_dir(self):
13431352
try:
13441353
return S_ISDIR(self.stat().st_mode)
13451354
except OSError as e:
1346-
if e.errno not in _IGNORED_ERROS:
1355+
if not _ignore_error(e):
13471356
raise
13481357
# Path doesn't exist or is a broken symlink
13491358
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1360,7 +1369,7 @@ def is_file(self):
13601369
try:
13611370
return S_ISREG(self.stat().st_mode)
13621371
except OSError as e:
1363-
if e.errno not in _IGNORED_ERROS:
1372+
if not _ignore_error(e):
13641373
raise
13651374
# Path doesn't exist or is a broken symlink
13661375
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1397,7 +1406,7 @@ def is_symlink(self):
13971406
try:
13981407
return S_ISLNK(self.lstat().st_mode)
13991408
except OSError as e:
1400-
if e.errno not in _IGNORED_ERROS:
1409+
if not _ignore_error(e):
14011410
raise
14021411
# Path doesn't exist
14031412
return False
@@ -1412,7 +1421,7 @@ def is_block_device(self):
14121421
try:
14131422
return S_ISBLK(self.stat().st_mode)
14141423
except OSError as e:
1415-
if e.errno not in _IGNORED_ERROS:
1424+
if not _ignore_error(e):
14161425
raise
14171426
# Path doesn't exist or is a broken symlink
14181427
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1428,7 +1437,7 @@ def is_char_device(self):
14281437
try:
14291438
return S_ISCHR(self.stat().st_mode)
14301439
except OSError as e:
1431-
if e.errno not in _IGNORED_ERROS:
1440+
if not _ignore_error(e):
14321441
raise
14331442
# Path doesn't exist or is a broken symlink
14341443
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1444,7 +1453,7 @@ def is_fifo(self):
14441453
try:
14451454
return S_ISFIFO(self.stat().st_mode)
14461455
except OSError as e:
1447-
if e.errno not in _IGNORED_ERROS:
1456+
if not _ignore_error(e):
14481457
raise
14491458
# Path doesn't exist or is a broken symlink
14501459
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1460,7 +1469,7 @@ def is_socket(self):
14601469
try:
14611470
return S_ISSOCK(self.stat().st_mode)
14621471
except OSError as e:
1463-
if e.errno not in _IGNORED_ERROS:
1472+
if not _ignore_error(e):
14641473
raise
14651474
# Path doesn't exist or is a broken symlink
14661475
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``pathlib`` no longer raises when checking file and directory existence on
2+
drives that are not ready

0 commit comments

Comments
 (0)