]> granicus.if.org Git - python/commitdiff
bpo-26978: Implement pathlib.Path.link_to (Using os.link) (GH-12990)
authorJoannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com>
Sat, 4 May 2019 15:27:10 +0000 (11:27 -0400)
committerAntoine Pitrou <antoine@python.org>
Sat, 4 May 2019 15:27:10 +0000 (17:27 +0200)
Doc/library/pathlib.rst
Doc/whatsnew/3.8.rst
Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/NEWS.d/next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst [new file with mode: 0644]

index 450e8ff378a3a5b28176831c45e61890d4088b0e..7a4a20dc61182fc373d089ace5e5f19031f05c48 100644 (file)
@@ -1054,6 +1054,13 @@ call fails (for example because the path doesn't exist).
    use :func:`Path.rmdir` instead.
 
 
+.. method:: Path.link_to(target)
+
+   Create a hard link pointing to a path named *target*.
+
+   .. versionchanged:: 3.8
+
+
 .. method:: Path.write_bytes(data)
 
    Open the file pointed to in bytes mode, write *data* to it, and close the
index 764bd00544d9f5af3e3738b87d92efe8c1e283d7..64ef6e1840605b44e8cbdd639e59d1bb3f04e010 100644 (file)
@@ -369,6 +369,10 @@ pathlib
 contain characters unrepresentable at the OS level.
 (Contributed by Serhiy Storchaka in :issue:`33721`.)
 
+Added :meth:`pathlib.Path.link_to()` which creates a hard link pointing
+to a path.
+(Contributed by Joannah Nanjekye in :issue:`26978`)
+
 
 socket
 ------
index 911b774b5649846c27c2995ce4a55a0727d5c0c9..1ba98b19e833444bdd806b5eb97de608a5e50ef3 100644 (file)
@@ -411,6 +411,8 @@ class _NormalAccessor(_Accessor):
 
     unlink = os.unlink
 
+    link_to = os.link
+
     rmdir = os.rmdir
 
     rename = os.rename
@@ -1303,6 +1305,14 @@ class Path(PurePath):
             self._raise_closed()
         return self._accessor.lstat(self)
 
+    def link_to(self, target):
+        """
+        Create a hard link pointing to a path named target.
+        """
+        if self._closed:
+            self._raise_closed()
+        self._accessor.link_to(self, target)
+
     def rename(self, target):
         """
         Rename this path to the given path.
index f8325eb93275a608dd838c01d97705096eec521e..990207b9c4e4884c6e87fd2d4fc56b97a8940650 100644 (file)
@@ -1643,6 +1643,25 @@ class _BasePathTest(object):
         self.assertFileNotFound(p.stat)
         self.assertFileNotFound(p.unlink)
 
+    def test_link_to(self):
+        P = self.cls(BASE)
+        p = P / 'fileA'
+        size = p.stat().st_size
+        # linking to another path.
+        q = P / 'dirA' / 'fileAA'
+        try:
+            p.link_to(q)
+        except PermissionError as e:
+            self.skipTest('os.link(): %s' % e)
+        self.assertEqual(q.stat().st_size, size)
+        self.assertEqual(os.path.samefile(p, q), True)
+        self.assertTrue(p.stat)
+        # Linking to a str of a relative path.
+        r = rel_join('fileAAA')
+        q.link_to(r)
+        self.assertEqual(os.stat(r).st_size, size)
+        self.assertTrue(q.stat)
+
     def test_rename(self):
         P = self.cls(BASE)
         p = P / 'fileA'
diff --git a/Misc/NEWS.d/next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst b/Misc/NEWS.d/next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst
new file mode 100644 (file)
index 0000000..0b14920
--- /dev/null
@@ -0,0 +1,2 @@
+`pathlib.path.link_to()` is now implemented. It creates a hard link pointing
+to a path.