]> granicus.if.org Git - python/commitdiff
issue27186: add PathLike ABC
authorEthan Furman <ethan@stoneleaf.us>
Sat, 4 Jun 2016 19:49:35 +0000 (12:49 -0700)
committerEthan Furman <ethan@stoneleaf.us>
Sat, 4 Jun 2016 19:49:35 +0000 (12:49 -0700)
Lib/os.py
Lib/test/test_os.py

index 0131ed8195edcc7bdf1701a49db13046233356f5..e7d089ed35b32325a0ced57e90242408e78b4a4b 100644 (file)
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -22,7 +22,7 @@ and opendir), and leave all pathname manipulation to os.path
 """
 
 #'
-
+import abc
 import sys, errno
 import stat as st
 
@@ -1125,3 +1125,18 @@ if not _exists('fspath'):
 
             raise TypeError("expected str, bytes or os.PathLike object, not "
                             + path_type.__name__)
+
+class PathLike(abc.ABC):
+    """
+    Abstract base class for implementing the file system path protocol.
+    """
+    @abc.abstractmethod
+    def __fspath__(self):
+        """
+        Return the file system path representation of the object.
+        """
+        raise NotImplementedError
+
+    @classmethod
+    def __subclasshook__(cls, subclass):
+        return hasattr(subclass, '__fspath__')
index bf06438db224f4da5fb06aa60a73a0dec44eb337..e740edf644e796373002c8d31a06bd65f1086983 100644 (file)
@@ -3127,6 +3127,8 @@ class TestPEP519(unittest.TestCase):
                 return '#feelthegil'
 
         self.assertEqual('#feelthegil', os.fspath(PathLike()))
+        self.assertTrue(issubclass(PathLike, os.PathLike))
+        self.assertTrue(isinstance(PathLike(), os.PathLike))
 
     def test_garbage_in_exception_out(self):
         vapor = type('blah', (), {})