summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-06-22 13:35:51 (GMT)
committerGitHub <noreply@github.com>2023-06-22 13:35:51 (GMT)
commita8006706f7d6e8825b90f1970beed7845d1d72ed (patch)
tree3926d14507d730ee20c402e78b6f5e567f0caf4e /Lib/test/test_pathlib.py
parent04492cbc9aa45ac2c12d22083c406a0364c39f5b (diff)
downloadcpython-a8006706f7d6e8825b90f1970beed7845d1d72ed.zip
cpython-a8006706f7d6e8825b90f1970beed7845d1d72ed.tar.gz
cpython-a8006706f7d6e8825b90f1970beed7845d1d72ed.tar.bz2
GH-89812: Add `pathlib.UnsupportedOperation` (GH-105926)
This new exception type is raised instead of `NotImplementedError` when a path operation is not supported. It can be raised from `Path.readlink()`, `symlink_to()`, `hardlink_to()`, `owner()` and `group()`. In a future version of pathlib, it will be raised by `AbstractPath` for these methods and others, such as `AbstractPath.mkdir()` and `unlink()`.
Diffstat (limited to 'Lib/test/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib.py32
1 files changed, 27 insertions, 5 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index 02a0f25..f935690 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -24,6 +24,12 @@ except ImportError:
grp = pwd = None
+class UnsupportedOperationTest(unittest.TestCase):
+ def test_is_notimplemented(self):
+ self.assertTrue(issubclass(pathlib.UnsupportedOperation, NotImplementedError))
+ self.assertTrue(isinstance(pathlib.UnsupportedOperation(), NotImplementedError))
+
+
# Make sure any symbolic links in the base test path are resolved.
BASE = os.path.realpath(TESTFN)
join = lambda *x: os.path.join(BASE, *x)
@@ -1550,12 +1556,12 @@ class WindowsPathAsPureTest(PureWindowsPathTest):
def test_owner(self):
P = self.cls
- with self.assertRaises(NotImplementedError):
+ with self.assertRaises(pathlib.UnsupportedOperation):
P('c:/').owner()
def test_group(self):
P = self.cls
- with self.assertRaises(NotImplementedError):
+ with self.assertRaises(pathlib.UnsupportedOperation):
P('c:/').group()
@@ -2055,6 +2061,13 @@ class PathTest(unittest.TestCase):
with self.assertRaises(OSError):
(P / 'fileA').readlink()
+ @unittest.skipIf(hasattr(os, "readlink"), "os.readlink() is present")
+ def test_readlink_unsupported(self):
+ P = self.cls(BASE)
+ p = P / 'fileA'
+ with self.assertRaises(pathlib.UnsupportedOperation):
+ q.readlink(p)
+
def _check_resolve(self, p, expected, strict=True):
q = p.resolve(strict)
self.assertEqual(q, expected)
@@ -2343,7 +2356,7 @@ class PathTest(unittest.TestCase):
if self.cls._flavour is os.path:
self.skipTest("path flavour is supported")
else:
- self.assertRaises(NotImplementedError, self.cls)
+ self.assertRaises(pathlib.UnsupportedOperation, self.cls)
def _test_cwd(self, p):
q = self.cls(os.getcwd())
@@ -2543,12 +2556,12 @@ class PathTest(unittest.TestCase):
self.assertTrue(link2.exists())
@unittest.skipIf(hasattr(os, "link"), "os.link() is present")
- def test_link_to_not_implemented(self):
+ def test_hardlink_to_unsupported(self):
P = self.cls(BASE)
p = P / 'fileA'
# linking to another path.
q = P / 'dirA' / 'fileAA'
- with self.assertRaises(NotImplementedError):
+ with self.assertRaises(pathlib.UnsupportedOperation):
q.hardlink_to(p)
def test_rename(self):
@@ -2776,6 +2789,15 @@ class PathTest(unittest.TestCase):
self.assertTrue(link.is_dir())
self.assertTrue(list(link.iterdir()))
+ @unittest.skipIf(hasattr(os, "symlink"), "os.symlink() is present")
+ def test_symlink_to_unsupported(self):
+ P = self.cls(BASE)
+ p = P / 'fileA'
+ # linking to another path.
+ q = P / 'dirA' / 'fileAA'
+ with self.assertRaises(pathlib.UnsupportedOperation):
+ q.symlink_to(p)
+
def test_is_junction(self):
P = self.cls(BASE)