Kaydet (Commit) 958b3e40 authored tarafından Ethan Furman's avatar Ethan Furman

issue27186: add PathLike ABC

üst 8bc9378c
...@@ -22,7 +22,7 @@ and opendir), and leave all pathname manipulation to os.path ...@@ -22,7 +22,7 @@ and opendir), and leave all pathname manipulation to os.path
""" """
#' #'
import abc
import sys, errno import sys, errno
import stat as st import stat as st
...@@ -1125,3 +1125,18 @@ if not _exists('fspath'): ...@@ -1125,3 +1125,18 @@ if not _exists('fspath'):
raise TypeError("expected str, bytes or os.PathLike object, not " raise TypeError("expected str, bytes or os.PathLike object, not "
+ path_type.__name__) + 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__')
...@@ -3127,6 +3127,8 @@ class TestPEP519(unittest.TestCase): ...@@ -3127,6 +3127,8 @@ class TestPEP519(unittest.TestCase):
return '#feelthegil' return '#feelthegil'
self.assertEqual('#feelthegil', os.fspath(PathLike())) 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): def test_garbage_in_exception_out(self):
vapor = type('blah', (), {}) vapor = type('blah', (), {})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment