Antoine Pitrou avatar 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.

Comments (0)

Files changed (3)

    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)
 
         """
         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)
 
 
         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())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.