]> granicus.if.org Git - python/commitdiff
Issue #19777: Provide a home() classmethod on Path objects.
authorAntoine Pitrou <solipsis@pitrou.net>
Mon, 12 Jan 2015 20:03:41 +0000 (21:03 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Mon, 12 Jan 2015 20:03:41 +0000 (21:03 +0100)
Contributed by Victor Salgado and Mayank Tripathi.

Doc/library/pathlib.rst
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/ACKS
Misc/NEWS

index c796cf436096dd9e76739afbe24ca69b48f50552..0226ce444be6c726f9f9027cde411df4234b3d13 100644 (file)
@@ -628,6 +628,17 @@ call fails (for example because the path doesn't exist):
       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`).
index 624493248245a20f47d591600c7eaf5917f21206..dd2ccba1a61d72349df68629884a0e9e4f217cc7 100644 (file)
@@ -1008,6 +1008,13 @@ class Path(PurePath):
         """
         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)).
index 2e97a5e2de1a2a783fe2c38553c1664c8f6f0208..f4ee5191153d6b09462d526a40e975587741b4ae 100644 (file)
@@ -1261,6 +1261,17 @@ class _BasePathTest(object):
         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')
index 95f12ef05d621d519465e88862294e5e2e77aef3..ff72992e5e9dc4b96f155aa30fa4f227d698c399 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1201,6 +1201,7 @@ SĂ©bastien SablĂ©
 Suman Saha
 Hajime Saitou
 George Sakkis
+Victor Salgado
 Rich Salz
 Kevin Samborn
 Adrian Sampson
@@ -1390,6 +1391,7 @@ David Townshend
 Nathan Trapuzzano
 Laurence Tratt
 Alberto Trevino
+Mayank Tripathi
 Matthias Troffaes
 Tom Tromey
 John Tromp
index 3c71d9e9fc59520879c98ff340184c38ab17d9b4..ec2e74a792fb82a5a38b7f48475a8e6dc4f04496 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -203,6 +203,9 @@ Core and Builtins
 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.