summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_pathlib/test_pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-12-22 15:11:16 (GMT)
committerGitHub <noreply@github.com>2023-12-22 15:11:16 (GMT)
commit237e2cff00cca49db47bcb7ea13683a4d9ad1ea5 (patch)
treef1d2f5db8d04d263b2c2b3808f31a800df4a45ac /Lib/test/test_pathlib/test_pathlib.py
parent45e09f921be55e23bed19b5db4c95ce7bd7aad6b (diff)
downloadcpython-237e2cff00cca49db47bcb7ea13683a4d9ad1ea5.zip
cpython-237e2cff00cca49db47bcb7ea13683a4d9ad1ea5.tar.gz
cpython-237e2cff00cca49db47bcb7ea13683a4d9ad1ea5.tar.bz2
GH-110109: Fix misleading `pathlib._abc.PurePathBase` repr (#113376)
`PurePathBase.__repr__()` produces a string like `MyPath('/foo')`. This repr is incorrect/misleading when a subclass's `__init__()` method is customized, which I expect to be the very common. This commit moves the `__repr__()` method to `PurePath`, leaving `PurePathBase` with the default `object` repr. No user-facing changes because the `pathlib._abc` module remains private.
Diffstat (limited to 'Lib/test/test_pathlib/test_pathlib.py')
-rw-r--r--Lib/test/test_pathlib/test_pathlib.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py
index 00cfdd3..d55ccd9 100644
--- a/Lib/test/test_pathlib/test_pathlib.py
+++ b/Lib/test/test_pathlib/test_pathlib.py
@@ -69,6 +69,18 @@ class PurePathTest(test_pathlib_abc.DummyPurePathTest):
self.assertEqual(hash(pp), hash(p))
self.assertEqual(str(pp), str(p))
+ def test_repr_common(self):
+ for pathstr in ('a', 'a/b', 'a/b/c', '/', '/a/b', '/a/b/c'):
+ with self.subTest(pathstr=pathstr):
+ p = self.cls(pathstr)
+ clsname = p.__class__.__name__
+ r = repr(p)
+ # The repr() is in the form ClassName("forward-slashes path").
+ self.assertTrue(r.startswith(clsname + '('), r)
+ self.assertTrue(r.endswith(')'), r)
+ inner = r[len(clsname) + 1 : -1]
+ self.assertEqual(eval(inner), p.as_posix())
+
def test_fspath_common(self):
P = self.cls
p = P('a/b')