aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_pathlib
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2024-01-14 21:49:53 +0000
committerGitHub <noreply@github.com>2024-01-14 21:49:53 +0000
commitca6cf56330ae7751819b62748f33f23d98596703 (patch)
tree461b2a8b3651a1570736ff9cbed4832671d813bd /Lib/test/test_pathlib
parentc2808431b32fa7bc0d222d4549389f781f1a7333 (diff)
downloadcpython-ca6cf56330ae7751819b62748f33f23d98596703.tar.gz
cpython-ca6cf56330ae7751819b62748f33f23d98596703.zip
Add `pathlib._abc.PathModuleBase` (#113893)
Path modules provide a subset of the `os.path` API, specifically those functions needed to provide `PurePathBase` functionality. Each `PurePathBase` subclass references its path module via a `pathmod` class attribute. This commit adds a new `PathModuleBase` class, which provides abstract methods that unconditionally raise `UnsupportedOperation`. An instance of this class is assigned to `PurePathBase.pathmod`, replacing `posixpath`. As a result, `PurePathBase` is no longer POSIX-y by default, and all its methods raise `UnsupportedOperation` courtesy of `pathmod`. Users who subclass `PurePathBase` or `PathBase` should choose the path syntax by setting `pathmod` to `posixpath`, `ntpath`, `os.path`, or their own subclass of `PathModuleBase`, as circumstances demand.
Diffstat (limited to 'Lib/test/test_pathlib')
-rw-r--r--Lib/test/test_pathlib/test_pathlib.py1
-rw-r--r--Lib/test/test_pathlib/test_pathlib_abc.py56
2 files changed, 55 insertions, 2 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py
index 1b560adfc3b..61d7939ad14 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -1151,6 +1151,7 @@ class PathTest(test_pathlib_abc.DummyPathTest, PurePathTest):
def test_matches_pathbase_api(self):
our_names = {name for name in dir(self.cls) if name[0] != '_'}
+ our_names.remove('is_reserved') # only present in PurePath
path_names = {name for name in dir(pathlib._abc.PathBase) if name[0] != '_'}
self.assertEqual(our_names, path_names)
for attr_name in our_names:
diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py
index 14df1e69db1..c3c568c296e 100644
--- a/Lib/test/test_pathlib/test_pathlib_abc.py
+++ b/Lib/test/test_pathlib/test_pathlib_abc.py
@@ -5,7 +5,7 @@ import errno
import stat
import unittest
-from pathlib._abc import UnsupportedOperation, PurePathBase, PathBase
+from pathlib._abc import UnsupportedOperation, PathModuleBase, PurePathBase, PathBase
import posixpath
from test.support.os_helper import TESTFN
@@ -17,6 +17,20 @@ class UnsupportedOperationTest(unittest.TestCase):
self.assertTrue(isinstance(UnsupportedOperation(), NotImplementedError))
+class PathModuleBaseTest(unittest.TestCase):
+ cls = PathModuleBase
+
+ def test_unsupported_operation(self):
+ m = self.cls()
+ e = UnsupportedOperation
+ with self.assertRaises(e):
+ m.sep
+ self.assertRaises(e, m.join, 'foo')
+ self.assertRaises(e, m.split, 'foo')
+ self.assertRaises(e, m.splitroot, 'foo')
+ self.assertRaises(e, m.normcase, 'foo')
+ self.assertRaises(e, m.isabs, 'foo')
+
#
# Tests for the pure classes.
#
@@ -25,6 +39,42 @@ class UnsupportedOperationTest(unittest.TestCase):
class PurePathBaseTest(unittest.TestCase):
cls = PurePathBase
+ def test_unsupported_operation_pure(self):
+ p = self.cls('foo')
+ e = UnsupportedOperation
+ with self.assertRaises(e):
+ p.drive
+ with self.assertRaises(e):
+ p.root
+ with self.assertRaises(e):
+ p.anchor
+ with self.assertRaises(e):
+ p.parts
+ with self.assertRaises(e):
+ p.parent
+ with self.assertRaises(e):
+ p.parents
+ with self.assertRaises(e):
+ p.name
+ with self.assertRaises(e):
+ p.stem
+ with self.assertRaises(e):
+ p.suffix
+ with self.assertRaises(e):
+ p.suffixes
+ with self.assertRaises(e):
+ p / 'bar'
+ with self.assertRaises(e):
+ 'bar' / p
+ self.assertRaises(e, p.joinpath, 'bar')
+ self.assertRaises(e, p.with_name, 'bar')
+ self.assertRaises(e, p.with_stem, 'bar')
+ self.assertRaises(e, p.with_suffix, '.txt')
+ self.assertRaises(e, p.relative_to, '')
+ self.assertRaises(e, p.is_relative_to, '')
+ self.assertRaises(e, p.is_absolute)
+ self.assertRaises(e, p.match, '*')
+
def test_magic_methods(self):
P = self.cls
self.assertFalse(hasattr(P, '__fspath__'))
@@ -39,11 +89,12 @@ class PurePathBaseTest(unittest.TestCase):
self.assertIs(P.__ge__, object.__ge__)
def test_pathmod(self):
- self.assertIs(self.cls.pathmod, posixpath)
+ self.assertIsInstance(self.cls.pathmod, PathModuleBase)
class DummyPurePath(PurePathBase):
__slots__ = ()
+ pathmod = posixpath
def __eq__(self, other):
if not isinstance(other, DummyPurePath):
@@ -669,6 +720,7 @@ class DummyPath(PathBase):
memory.
"""
__slots__ = ()
+ pathmod = posixpath
_files = {}
_directories = {}