summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2016-01-06 19:01:42 (GMT)
committerGuido van Rossum <guido@python.org>2016-01-06 19:01:42 (GMT)
commite42823153990ab5a4bb5f9148458cf48e189f4f9 (patch)
treed9f073721f49104b44e505f308441f1e005a38c0
parent69bfb15bd8cb87dd07f1cf6c53f78b399c7ef937 (diff)
downloadcpython-e42823153990ab5a4bb5f9148458cf48e189f4f9.zip
cpython-e42823153990ab5a4bb5f9148458cf48e189f4f9.tar.gz
cpython-e42823153990ab5a4bb5f9148458cf48e189f4f9.tar.bz2
Issue #22570: Add 'path' attribute to pathlib.Path objects.
-rw-r--r--Lib/pathlib.py7
-rw-r--r--Lib/test/test_pathlib.py27
-rw-r--r--Misc/NEWS6
3 files changed, 40 insertions, 0 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index cab4151..1bd457a 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -645,6 +645,13 @@ class PurePath(object):
self._parts) or '.'
return self._str
+ @property
+ def path(self):
+ try:
+ return self._str
+ except AttributeError:
+ return str(self)
+
def as_posix(self):
"""Return the string representation of the path with forward (/)
slashes."""
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index f91cda0..061c764 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -480,6 +480,22 @@ class _BasePurePathTest(object):
self.assertEqual(P('a/b.py').name, 'b.py')
self.assertEqual(P('/a/b.py').name, 'b.py')
+ def test_path_common(self):
+ P = self.cls
+ def check(arg, expected=None):
+ if expected is None:
+ expected = arg
+ self.assertEqual(P(arg).path, expected.replace('/', self.sep))
+ check('', '.')
+ check('.')
+ check('/')
+ check('a/b')
+ check('/a/b')
+ check('/a/b/', '/a/b')
+ check('/a/b/.', '/a/b')
+ check('a/b.py')
+ check('/a/b.py')
+
def test_suffix_common(self):
P = self.cls
self.assertEqual(P('').suffix, '')
@@ -903,6 +919,17 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self.assertEqual(P('//My.py/Share.php').name, '')
self.assertEqual(P('//My.py/Share.php/a/b').name, 'b')
+ def test_path(self):
+ P = self.cls
+ self.assertEqual(P('c:').path, 'c:')
+ self.assertEqual(P('c:/').path, 'c:\\')
+ self.assertEqual(P('c:a/b').path, 'c:a\\b')
+ self.assertEqual(P('c:/a/b').path, 'c:\\a\\b')
+ self.assertEqual(P('c:a/b.py').path, 'c:a\\b.py')
+ self.assertEqual(P('c:/a/b.py').path, 'c:\\a\\b.py')
+ self.assertEqual(P('//My.py/Share.php').path, '\\\\My.py\\Share.php\\')
+ self.assertEqual(P('//My.py/Share.php/a/b').path, '\\\\My.py\\Share.php\\a\\b')
+
def test_suffix(self):
P = self.cls
self.assertEqual(P('c:').suffix, '')
diff --git a/Misc/NEWS b/Misc/NEWS
index 4e00bcd..ae9301a 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,12 @@ Core and Builtins
Library
-------
+- Issue #22570: Add 'path' attribute to pathlib.Path objects,
+ returning the same as str(), to make it more similar to DirEntry.
+ Library code can now write getattr(p, 'path', p) to get the path as
+ a string from a Path, a DirEntry, or a plain string. This is
+ essentially a small one-off protocol.
+
- Issue #26012: Don't traverse into symlinks for ** pattern in
pathlib.Path.[r]glob().