[Python-checkins] cpython: Issue #19777: Provide a home() classmethod on Path objects.

antoine.pitrou python-checkins at python.org
Mon Jan 12 21:45:53 CET 2015


https://hg.python.org/cpython/rev/4a55b98314cd
changeset:   94119:4a55b98314cd
user:        Antoine Pitrou <solipsis at pitrou.net>
date:        Mon Jan 12 21:03:41 2015 +0100
summary:
  Issue #19777: Provide a home() classmethod on Path objects.
Contributed by Victor Salgado and Mayank Tripathi.

files:
  Doc/library/pathlib.rst  |  11 +++++++++++
  Lib/pathlib.py           |   7 +++++++
  Lib/test/test_pathlib.py |  11 +++++++++++
  Misc/ACKS                |   2 ++
  Misc/NEWS                |   3 +++
  5 files changed, 34 insertions(+), 0 deletions(-)


diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst
--- a/Doc/library/pathlib.rst
+++ b/Doc/library/pathlib.rst
@@ -628,6 +628,17 @@
       PosixPath('/home/antoine/pathlib')
 
 
+.. classmethod:: Path.home()
+
+   Return a new path object representing the user's home directory (as
+   returned by :func:`os.path.expanduser` with ``~`` construct)::
+
+      >>> Path.home()
+      PosixPath('/home/antoine')
+
+   .. versionadded:: 3.5
+
+
 .. method:: Path.stat()
 
    Return information about this path (similarly to :func:`os.stat`).
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -1008,6 +1008,13 @@
         """
         return cls(os.getcwd())
 
+    @classmethod
+    def home(cls):
+        """Return a new path pointing to the user's home directory (as
+        returned by os.path.expanduser('~')).
+        """
+        return cls(cls()._flavour.gethomedir(None))
+
     def samefile(self, other_path):
         """Return whether `other_file` is the same or not as this file.
         (as returned by os.path.samefile(file, other_file)).
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
@@ -1261,6 +1261,17 @@
         p = self.cls.cwd()
         self._test_cwd(p)
 
+    def _test_home(self, p):
+        q = self.cls(os.path.expanduser('~'))
+        self.assertEqual(p, q)
+        self.assertEqual(str(p), str(q))
+        self.assertIs(type(p), type(q))
+        self.assertTrue(p.is_absolute())
+
+    def test_home(self):
+        p = self.cls.home()
+        self._test_home(p)
+
     def test_samefile(self):
         fileA_path = os.path.join(BASE, 'fileA')
         fileB_path = os.path.join(BASE, 'dirB', 'fileB')
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1201,6 +1201,7 @@
 Suman Saha
 Hajime Saitou
 George Sakkis
+Victor Salgado
 Rich Salz
 Kevin Samborn
 Adrian Sampson
@@ -1390,6 +1391,7 @@
 Nathan Trapuzzano
 Laurence Tratt
 Alberto Trevino
+Mayank Tripathi
 Matthias Troffaes
 Tom Tromey
 John Tromp
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -203,6 +203,9 @@
 Library
 -------
 
+- Issue #19777: Provide a home() classmethod on Path objects.  Contributed
+  by Victor Salgado and Mayank Tripathi.
+
 - Issue #23206: Make ``json.dumps(..., ensure_ascii=False)`` as fast as the
   default case of ``ensure_ascii=True``.  Patch by Naoki Inada.
 

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


More information about the Python-checkins mailing list