File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -238,14 +238,14 @@ def exists_and_is_accessible(path: Path) -> bool:
238238 """
239239 try :
240240 return path .exists ()
241- except PermissionError :
242- # Treat any permission-denied error (including Windows WinError 5) as
243- # inaccessible rather than crashing.
244- return False
245- except OSError :
246- # Catch other OS-level errors (e.g. Windows ERROR_ACCESS_DENIED variants
247- # that may surface as OSError rather than PermissionError).
248- return False
241+ except PermissionError as error :
242+ if error . errno == errno . EACCES or getattr ( error , "winerror" , None ) == 5 :
243+ return False
244+ raise
245+ except OSError as error :
246+ if error . errno == errno . EACCES or getattr ( error , "winerror" , None ) == 5 :
247+ return False
248+ raise
249249
250250
251251def is_in_path (path : str | Path , parent_path : str | Path ) -> bool :
Original file line number Diff line number Diff line change @@ -241,6 +241,19 @@ def test_exists_and_is_accessible():
241241 ):
242242 exists_and_is_accessible (Path ("/usr/bin/python" ))
243243
244+ class WindowsAccessDenied (OSError ):
245+ def __init__ (self ):
246+ super ().__init__ ("Access is denied" )
247+ self .winerror = 5
248+ self .errno = None
249+
250+ with mock .patch ("pathlib.Path.exists" , side_effect = WindowsAccessDenied ()):
251+ assert not exists_and_is_accessible (Path ("/usr/bin/python" ))
252+
253+ with pytest .raises (OSError ):
254+ with mock .patch ("pathlib.Path.exists" , side_effect = OSError (1 , "Other error" )):
255+ exists_and_is_accessible (Path ("/usr/bin/python" ))
256+
244257
245258def test_is_in_path ():
246259 """Test that is_in_path correctly checks if a path is inside another path."""
You can’t perform that action at this time.
0 commit comments