Skip to content

Commit 903f35f

Browse files
committed
bpo-24132: Add proof of concept for AbstractPath to pathlib
Add stub / proof of concept of Barney Gale's AbstractPath interface to show how it can work in combination with the direct subclassing features of PurePath and Path.
1 parent a0c4998 commit 903f35f

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Lib/pathlib.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import sys
99
import warnings
1010
from _collections_abc import Sequence
11+
from abc import ABCMeta
1112
from errno import EINVAL, ENOENT, ENOTDIR, EBADF, ELOOP
1213
from operator import attrgetter
1314
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
@@ -16,6 +17,7 @@
1617

1718
__all__ = [
1819
"PurePath", "PurePosixPath", "PureWindowsPath",
20+
"AbstractPath",
1921
"Path", "PosixPath", "WindowsPath",
2022
]
2123

@@ -260,8 +262,13 @@ def make_uri(self, path):
260262
return 'file://' + urlquote_from_bytes(bpath)
261263

262264

265+
class _GenericFlavour(_PosixFlavour):
266+
pass
267+
268+
263269
_windows_flavour = _WindowsFlavour()
264270
_posix_flavour = _PosixFlavour()
271+
_generic_flavour = _GenericFlavour()
265272

266273

267274
class _Accessor:
@@ -943,10 +950,15 @@ class PureWindowsPath(PurePath):
943950
__slots__ = ()
944951

945952

953+
class AbstractPath(PurePath, metaclass=ABCMeta):
954+
_flavour = _generic_flavour
955+
__slots__ = ()
956+
957+
946958
# Filesystem-accessing classes
947959

948960

949-
class Path(PurePath):
961+
class Path(AbstractPath):
950962
"""PurePath subclass that can make system calls.
951963
952964
Path represents a filesystem path but unlike PurePath, also offers

Lib/test/test_pathlib.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,20 @@ def test_group(self):
14381438
P('c:/').group()
14391439

14401440

1441+
class AbstractPathTest(unittest.TestCase):
1442+
cls = pathlib.AbstractPath
1443+
1444+
def test_abstract_path(self):
1445+
P = self.cls
1446+
p = P()
1447+
self.assertEqual(p._flavour, pathlib._generic_flavour)
1448+
1449+
class Derived(P):
1450+
pass
1451+
d = Derived()
1452+
self.assertEqual(d._flavour, pathlib._generic_flavour)
1453+
1454+
14411455
class _BasePathTest(object):
14421456
"""Tests for the FS-accessing functionalities of the Path classes."""
14431457

0 commit comments

Comments
 (0)