Skip to content

Commit 530771d

Browse files
committed
Suppress OSErrors
1 parent dc6edc8 commit 530771d

File tree

2 files changed

+21
-22
lines changed

2 files changed

+21
-22
lines changed

Lib/pathlib/_local.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def _get_mode(self, *, follow_symlinks=True):
8585
if mode is None:
8686
try:
8787
st = os.stat(self._path, follow_symlinks=follow_symlinks)
88-
except (FileNotFoundError, ValueError):
88+
except (OSError, ValueError):
8989
mode = 0
9090
else:
9191
mode = st.st_mode
@@ -141,27 +141,36 @@ def exists(self, *, follow_symlinks=True):
141141
if follow_symlinks:
142142
try:
143143
self._entry.stat()
144-
except FileNotFoundError:
144+
except OSError:
145145
return False
146146
return True
147147

148148
def is_dir(self, *, follow_symlinks=True):
149149
"""
150150
Whether this path is a directory.
151151
"""
152-
return self._entry.is_dir(follow_symlinks=follow_symlinks)
152+
try:
153+
return self._entry.is_dir(follow_symlinks=follow_symlinks)
154+
except OSError:
155+
return False
153156

154157
def is_file(self, *, follow_symlinks=True):
155158
"""
156159
Whether this path is a regular file.
157160
"""
158-
return self._entry.is_file(follow_symlinks=follow_symlinks)
161+
try:
162+
return self._entry.is_file(follow_symlinks=follow_symlinks)
163+
except OSError:
164+
return False
159165

160166
def is_symlink(self):
161167
"""
162168
Whether this path is a symbolic link.
163169
"""
164-
return self._entry.is_symlink()
170+
try:
171+
return self._entry.is_symlink()
172+
except OSError:
173+
return False
165174

166175

167176
class PurePath(PurePathBase):

Lib/test/test_pathlib/test_pathlib_abc.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,25 +1858,15 @@ def test_iterdir_status(self):
18581858
for child in p.iterdir():
18591859
entry = child.status
18601860
self.assertIsInstance(entry, Status)
1861+
self.assertEqual(entry.exists(), child.exists())
1862+
self.assertEqual(entry.is_dir(), child.is_dir())
1863+
self.assertEqual(entry.is_file(), child.is_file())
1864+
self.assertEqual(entry.is_symlink(), child.is_symlink())
18611865
self.assertTrue(entry.exists(follow_symlinks=False))
18621866
self.assertEqual(entry.is_dir(follow_symlinks=False),
18631867
child.is_dir(follow_symlinks=False))
18641868
self.assertEqual(entry.is_file(follow_symlinks=False),
18651869
child.is_file(follow_symlinks=False))
1866-
self.assertEqual(entry.is_symlink(),
1867-
child.is_symlink())
1868-
if child.name == 'brokenLink':
1869-
self.assertFalse(entry.exists())
1870-
self.assertFalse(entry.is_dir())
1871-
self.assertFalse(entry.is_file())
1872-
elif child.name == 'brokenLinkLoop':
1873-
self.assertRaises(OSError, entry.exists)
1874-
self.assertRaises(OSError, entry.is_dir)
1875-
self.assertRaises(OSError, entry.is_file)
1876-
else:
1877-
self.assertTrue(entry.exists())
1878-
self.assertEqual(entry.is_dir(), child.is_dir())
1879-
self.assertEqual(entry.is_file(), child.is_file())
18801870

18811871
def test_glob_common(self):
18821872
def _check(glob, expected):
@@ -2018,7 +2008,7 @@ def test_status_exists(self):
20182008
self.assertTrue((p / 'linkB').status.exists(follow_symlinks=True))
20192009
self.assertFalse((p / 'brokenLink').status.exists())
20202010
self.assertTrue((p / 'brokenLink').status.exists(follow_symlinks=False))
2021-
self.assertRaises(OSError, (p / 'brokenLinkLoop').status.exists)
2011+
self.assertFalse((p / 'brokenLinkLoop').status.exists())
20222012
self.assertTrue((p / 'brokenLinkLoop').status.exists(follow_symlinks=False))
20232013
self.assertFalse((p / 'fileA\udfff').status.exists())
20242014
self.assertFalse((p / 'fileA\udfff').status.exists(follow_symlinks=False))
@@ -2040,7 +2030,7 @@ def test_status_is_dir(self):
20402030
self.assertFalse((p / 'linkB').status.is_dir(follow_symlinks=False))
20412031
self.assertFalse((p / 'brokenLink').status.is_dir())
20422032
self.assertFalse((p / 'brokenLink').status.is_dir(follow_symlinks=False))
2043-
self.assertRaises(OSError, (p / 'brokenLinkLoop').status.is_dir)
2033+
self.assertFalse((p / 'brokenLinkLoop').status.is_dir())
20442034
self.assertFalse((p / 'brokenLinkLoop').status.is_dir(follow_symlinks=False))
20452035
self.assertFalse((p / 'dirA\udfff').status.is_dir())
20462036
self.assertFalse((p / 'dirA\udfff').status.is_dir(follow_symlinks=False))
@@ -2062,7 +2052,7 @@ def test_status_is_file(self):
20622052
self.assertFalse((p / 'linkB').status.is_file(follow_symlinks=False))
20632053
self.assertFalse((p / 'brokenLink').status.is_file())
20642054
self.assertFalse((p / 'brokenLink').status.is_file(follow_symlinks=False))
2065-
self.assertRaises(OSError, (p / 'brokenLinkLoop').status.is_file)
2055+
self.assertFalse((p / 'brokenLinkLoop').status.is_file())
20662056
self.assertFalse((p / 'brokenLinkLoop').status.is_file(follow_symlinks=False))
20672057
self.assertFalse((p / 'fileA\udfff').status.is_file())
20682058
self.assertFalse((p / 'fileA\udfff').status.is_file(follow_symlinks=False))

0 commit comments

Comments
 (0)