[Python-checkins] cpython: issue27186: add PathLike ABC

ethan.furman python-checkins at python.org
Sat Jun 4 15:50:02 EDT 2016


https://hg.python.org/cpython/rev/e672cf63d08a
changeset:   101710:e672cf63d08a
user:        Ethan Furman <ethan at stoneleaf.us>
date:        Sat Jun 04 12:49:35 2016 -0700
summary:
  issue27186: add PathLike ABC

files:
  Lib/os.py           |  17 ++++++++++++++++-
  Lib/test/test_os.py |   2 ++
  2 files changed, 18 insertions(+), 1 deletions(-)


diff --git a/Lib/os.py b/Lib/os.py
--- a/Lib/os.py
+++ b/Lib/os.py
@@ -22,7 +22,7 @@
 """
 
 #'
-
+import abc
 import sys, errno
 import stat as st
 
@@ -1125,3 +1125,18 @@
 
             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__')
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -3127,6 +3127,8 @@
                 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', (), {})

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list