Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 958b3e4

Browse files
committed
issue27186: add PathLike ABC
1 parent 8bc9378 commit 958b3e4

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/os.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""
2323

2424
#'
25-
25+
import abc
2626
import sys, errno
2727
import stat as st
2828

@@ -1125,3 +1125,18 @@ def fspath(path):
11251125

11261126
raise TypeError("expected str, bytes or os.PathLike object, not "
11271127
+ path_type.__name__)
1128+
1129+
class PathLike(abc.ABC):
1130+
"""
1131+
Abstract base class for implementing the file system path protocol.
1132+
"""
1133+
@abc.abstractmethod
1134+
def __fspath__(self):
1135+
"""
1136+
Return the file system path representation of the object.
1137+
"""
1138+
raise NotImplementedError
1139+
1140+
@classmethod
1141+
def __subclasshook__(cls, subclass):
1142+
return hasattr(subclass, '__fspath__')

Lib/test/test_os.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3127,6 +3127,8 @@ def __fspath__(self):
31273127
return '#feelthegil'
31283128

31293129
self.assertEqual('#feelthegil', os.fspath(PathLike()))
3130+
self.assertTrue(issubclass(PathLike, os.PathLike))
3131+
self.assertTrue(isinstance(PathLike(), os.PathLike))
31303132

31313133
def test_garbage_in_exception_out(self):
31323134
vapor = type('blah', (), {})

0 commit comments

Comments
 (0)