[Python-checkins] cpython (merge 3.5 -> default): Issue #22570: Add 'path' attribute to pathlib.Path objects. (Merge 3.5->3.6)

guido.van.rossum python-checkins at python.org
Wed Jan 6 14:04:28 EST 2016


https://hg.python.org/cpython/rev/d5f96a5da219
changeset:   99783:d5f96a5da219
parent:      99780:36864abbfe02
parent:      99782:9c49c417a68a
user:        Guido van Rossum <guido at dropbox.com>
date:        Wed Jan 06 11:03:47 2016 -0800
summary:
  Issue #22570: Add 'path' attribute to pathlib.Path objects. (Merge 3.5->3.6)

files:
  Lib/pathlib.py           |   7 +++++++
  Lib/test/test_pathlib.py |  27 +++++++++++++++++++++++++++
  Misc/NEWS                |   6 ++++++
  3 files changed, 40 insertions(+), 0 deletions(-)


diff --git a/Lib/pathlib.py b/Lib/pathlib.py
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -690,6 +690,13 @@
                                                   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
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -477,6 +477,22 @@
         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, '')
@@ -899,6 +915,17 @@
         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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -128,6 +128,12 @@
 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().
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list