7
7
import re
8
8
import sys
9
9
from _collections_abc import Sequence
10
- from errno import EINVAL , ENOENT , ENOTDIR
10
+ from errno import EINVAL , ENOENT , ENOTDIR , EBADF
11
11
from operator import attrgetter
12
12
from stat import S_ISDIR , S_ISLNK , S_ISREG , S_ISSOCK , S_ISBLK , S_ISCHR , S_ISFIFO
13
13
from urllib .parse import quote_from_bytes as urlquote_from_bytes
34
34
# Internals
35
35
#
36
36
37
+ # EBADF - guard agains macOS `stat` throwing EBADF
38
+ _IGNORED_ERROS = (ENOENT , ENOTDIR , EBADF )
39
+
37
40
def _is_wildcard_pattern (pat ):
38
41
# Whether this pattern needs actual matching using fnmatch, or can
39
42
# be looked up directly as a file.
@@ -528,7 +531,13 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
528
531
try :
529
532
entries = list (scandir (parent_path ))
530
533
for entry in entries :
531
- if entry .is_dir () and not entry .is_symlink ():
534
+ entry_is_dir = False
535
+ try :
536
+ entry_is_dir = entry .is_dir ()
537
+ except OSError as e :
538
+ if e .errno not in _IGNORED_ERROS :
539
+ raise
540
+ if entry_is_dir and not entry .is_symlink ():
532
541
path = parent_path ._make_child_relpath (entry .name )
533
542
for p in self ._iterate_directories (path , is_dir , scandir ):
534
543
yield p
@@ -1319,7 +1328,7 @@ def exists(self):
1319
1328
try :
1320
1329
self .stat ()
1321
1330
except OSError as e :
1322
- if e .errno not in ( ENOENT , ENOTDIR ) :
1331
+ if e .errno not in _IGNORED_ERROS :
1323
1332
raise
1324
1333
return False
1325
1334
return True
@@ -1331,7 +1340,7 @@ def is_dir(self):
1331
1340
try :
1332
1341
return S_ISDIR (self .stat ().st_mode )
1333
1342
except OSError as e :
1334
- if e .errno not in ( ENOENT , ENOTDIR ) :
1343
+ if e .errno not in _IGNORED_ERROS :
1335
1344
raise
1336
1345
# Path doesn't exist or is a broken symlink
1337
1346
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1345,7 +1354,7 @@ def is_file(self):
1345
1354
try :
1346
1355
return S_ISREG (self .stat ().st_mode )
1347
1356
except OSError as e :
1348
- if e .errno not in ( ENOENT , ENOTDIR ) :
1357
+ if e .errno not in _IGNORED_ERROS :
1349
1358
raise
1350
1359
# Path doesn't exist or is a broken symlink
1351
1360
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1379,7 +1388,7 @@ def is_symlink(self):
1379
1388
try :
1380
1389
return S_ISLNK (self .lstat ().st_mode )
1381
1390
except OSError as e :
1382
- if e .errno not in ( ENOENT , ENOTDIR ) :
1391
+ if e .errno not in _IGNORED_ERROS :
1383
1392
raise
1384
1393
# Path doesn't exist
1385
1394
return False
@@ -1391,7 +1400,7 @@ def is_block_device(self):
1391
1400
try :
1392
1401
return S_ISBLK (self .stat ().st_mode )
1393
1402
except OSError as e :
1394
- if e .errno not in ( ENOENT , ENOTDIR ) :
1403
+ if e .errno not in _IGNORED_ERROS :
1395
1404
raise
1396
1405
# Path doesn't exist or is a broken symlink
1397
1406
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1404,7 +1413,7 @@ def is_char_device(self):
1404
1413
try :
1405
1414
return S_ISCHR (self .stat ().st_mode )
1406
1415
except OSError as e :
1407
- if e .errno not in ( ENOENT , ENOTDIR ) :
1416
+ if e .errno not in _IGNORED_ERROS :
1408
1417
raise
1409
1418
# Path doesn't exist or is a broken symlink
1410
1419
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1417,7 +1426,7 @@ def is_fifo(self):
1417
1426
try :
1418
1427
return S_ISFIFO (self .stat ().st_mode )
1419
1428
except OSError as e :
1420
- if e .errno not in ( ENOENT , ENOTDIR ) :
1429
+ if e .errno not in _IGNORED_ERROS :
1421
1430
raise
1422
1431
# Path doesn't exist or is a broken symlink
1423
1432
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
@@ -1430,7 +1439,7 @@ def is_socket(self):
1430
1439
try :
1431
1440
return S_ISSOCK (self .stat ().st_mode )
1432
1441
except OSError as e :
1433
- if e .errno not in ( ENOENT , ENOTDIR ) :
1442
+ if e .errno not in _IGNORED_ERROS :
1434
1443
raise
1435
1444
# Path doesn't exist or is a broken symlink
1436
1445
# (see https://bitbucket.org/pitrou/pathlib/issue/12/)
0 commit comments