1. Antoine Pitrou
  2. pathlib

Commits

Antoine Pitrou  committed dc80696

Issue #12: is_dir and friends return False when path doesn't exist or is a broken symlink, instead of raising OSError.

  • Participants
  • Parent commits bd812f0
  • Branches default

Comments (0)

Files changed (3)

File docs/index.rst

View file
    Return True if the path points to a directory (or a symbolic link
    pointing to a directory), False if it points to another kind of file.
 
+   False is also returned if the path doesn't exist or is a broken symlink;
+   other errors (such as permission errors) are propagated.
+
 
 .. method:: Path.is_file()
 
    Return True if the path points to a regular file (or a symbolic link
    pointing to a regular file), False if it points to another kind of file.
 
+   False is also returned if the path doesn't exist or is a broken symlink;
+   other errors (such as permission errors) are propagated.
+
 
 .. method:: Path.is_symlink()
 
    Return True if the path points to a symbolic link, False otherwise.
 
+   False is also returned if the path doesn't exist; other errors (such
+   as permission errors) are propagated.
+
 
 .. method:: Path.lchmod(mode)
 

File pathlib.py

View file
         """
         Whether this path is a directory.
         """
-        return S_ISDIR(self._stat.st_mode)
+        try:
+            return S_ISDIR(self._stat.st_mode)
+        except OSError as e:
+            if e.errno != ENOENT:
+                raise
+            # Path doesn't exist or is a broken symlink
+            # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
+            return False
 
     def is_file(self):
         """
         Whether this path is a regular file (also True for symlinks pointing
         to regular files).
         """
-        return S_ISREG(self._stat.st_mode)
+        try:
+            return S_ISREG(self._stat.st_mode)
+        except OSError as e:
+            if e.errno != ENOENT:
+                raise
+            # Path doesn't exist or is a broken symlink
+            # (see https://bitbucket.org/pitrou/pathlib/issue/12/)
+            return False
 
     def is_symlink(self):
         """
         Whether this path is a symbolic link.
         """
-        st = self.lstat()
+        try:
+            st = self.lstat()
+        except OSError as e:
+            if e.errno != ENOENT:
+                raise
+            # Path doesn't exist
+            return False
         return S_ISLNK(st.st_mode)
 
 

File test_pathlib.py

View file
         P = self.cls(BASE)
         self.assertTrue(P['dirA'].is_dir())
         self.assertFalse(P['fileA'].is_dir())
+        self.assertFalse(P['non-existing'].is_dir())
         if not symlink_skip_reason:
             self.assertFalse(P['linkA'].is_dir())
             self.assertTrue(P['linkB'].is_dir())
         P = self.cls(BASE)
         self.assertTrue(P['fileA'].is_file())
         self.assertFalse(P['dirA'].is_file())
+        self.assertFalse(P['non-existing'].is_file())
         if not symlink_skip_reason:
             self.assertTrue(P['linkA'].is_file())
             self.assertFalse(P['linkB'].is_file())
         P = self.cls(BASE)
         self.assertFalse(P['fileA'].is_symlink())
         self.assertFalse(P['dirA'].is_symlink())
+        self.assertFalse(P['non-existing'].is_symlink())
         if not symlink_skip_reason:
             self.assertTrue(P['linkA'].is_symlink())
             self.assertTrue(P['linkB'].is_symlink())