From 43356413809907cde09455ad6004158d8b855466 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Sat, 27 Apr 2019 21:43:26 -0400 Subject: [PATCH 1/6] Implement pathlib.Path.link --- Doc/library/pathlib.rst | 6 ++++++ Doc/whatsnew/3.8.rst | 4 ++-- Lib/pathlib.py | 11 +++++++++++ Lib/test/test_pathlib.py | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index 450e8ff378a3a5b..c16d2a99742d1c9 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1054,6 +1054,12 @@ call fails (for example because the path doesn't exist). use :func:`Path.rmdir` instead. +.. method:: Path.link(target) + + Create a hard link pointing to a path named *target*. If the path is a directory, + use :func:`Path.mkdir` instead. + + .. method:: Path.write_bytes(data) Open the file pointed to in bytes mode, write *data* to it, and close the diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 8d94a9ff5441868..f43293e73159327 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -333,8 +333,8 @@ pathlib :exc:`ValueError` or its subclass :exc:`UnicodeEncodeError` for paths that contain characters unrepresentable at the OS level. (Contributed by Serhiy Storchaka in :issue:`33721`.) - - +Added :meth:`pathlib.Path.link()`. +(Contributed by Joannah Nanjekye in :issue:`26978`) socket ------ diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 911b774b5649846..966a39f79eff5ea 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -411,6 +411,8 @@ def lchmod(self, pathobj, mode): unlink = os.unlink + link = os.link + rmdir = os.rmdir rename = os.rename @@ -1303,6 +1305,15 @@ def lstat(self): self._raise_closed() return self._accessor.lstat(self) + def link(self, target): + """ + Create a hard link pointing to a path named target. + If the path is a directory, use mkdir() instead. + """ + if self._closed: + self._raise_closed() + self._accessor.link(self, target) + def rename(self, target): """ Rename this path to the given path. diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index f8325eb93275a60..8f77555cc75463c 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1643,6 +1643,24 @@ def test_rmdir(self): self.assertFileNotFound(p.stat) self.assertFileNotFound(p.unlink) + def test_link(self): + P = self.cls(BASE) + p = P / 'fileA' + size = p.stat().st_size + # linking to another path. + q = P / 'dirA' / 'fileAA' + try: + p.link(q) + except PermissionError as e: + self.skipTest('os.link(): %s' % e) + self.assertEqual(q.stat().st_size, size) + self.assertTrue(p.stat) + # Linking to a str of a relative path. + r = rel_join('fileAAA') + q.link(r) + self.assertEqual(os.stat(r).st_size, size) + self.assertTrue(q.stat) + def test_rename(self): P = self.cls(BASE) p = P / 'fileA' From 95aadda21e24b87a978d6f889ca799ca073cd2a2 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Sat, 27 Apr 2019 21:47:19 -0400 Subject: [PATCH 2/6] Fix documentation --- Doc/whatsnew/3.8.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index f43293e73159327..116f6b1cd3478b3 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -333,8 +333,11 @@ pathlib :exc:`ValueError` or its subclass :exc:`UnicodeEncodeError` for paths that contain characters unrepresentable at the OS level. (Contributed by Serhiy Storchaka in :issue:`33721`.) + Added :meth:`pathlib.Path.link()`. (Contributed by Joannah Nanjekye in :issue:`26978`) + + socket ------ From 51f965458496bcfb75a9d32350f82f139a432954 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" Date: Sun, 28 Apr 2019 01:52:41 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst 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 index 000000000000000..b8b108fec56e088 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst @@ -0,0 +1 @@ +`pathlib.path.link()` is now implemented. \ No newline at end of file From a418a80dfbfa721866c2d725eb41bb46e5ee66e5 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Sat, 4 May 2019 08:42:52 -0400 Subject: [PATCH 4/6] Implement pathlib.Path.link --- Doc/library/pathlib.rst | 7 ++++--- Lib/pathlib.py | 7 +++---- Lib/test/test_pathlib.py | 7 ++++--- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Doc/library/pathlib.rst b/Doc/library/pathlib.rst index c16d2a99742d1c9..7a4a20dc61182fc 100644 --- a/Doc/library/pathlib.rst +++ b/Doc/library/pathlib.rst @@ -1054,10 +1054,11 @@ call fails (for example because the path doesn't exist). use :func:`Path.rmdir` instead. -.. method:: Path.link(target) +.. method:: Path.link_to(target) - Create a hard link pointing to a path named *target*. If the path is a directory, - use :func:`Path.mkdir` instead. + Create a hard link pointing to a path named *target*. + + .. versionchanged:: 3.8 .. method:: Path.write_bytes(data) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 966a39f79eff5ea..1ba98b19e833444 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -411,7 +411,7 @@ def lchmod(self, pathobj, mode): unlink = os.unlink - link = os.link + link_to = os.link rmdir = os.rmdir @@ -1305,14 +1305,13 @@ def lstat(self): self._raise_closed() return self._accessor.lstat(self) - def link(self, target): + def link_to(self, target): """ Create a hard link pointing to a path named target. - If the path is a directory, use mkdir() instead. """ if self._closed: self._raise_closed() - self._accessor.link(self, target) + self._accessor.link_to(self, target) def rename(self, target): """ diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index 8f77555cc75463c..990207b9c4e4884 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1643,21 +1643,22 @@ def test_rmdir(self): self.assertFileNotFound(p.stat) self.assertFileNotFound(p.unlink) - def test_link(self): + 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(q) + 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(r) + q.link_to(r) self.assertEqual(os.stat(r).st_size, size) self.assertTrue(q.stat) From 0852e47426dc0542615534088295d4b182c300c4 Mon Sep 17 00:00:00 2001 From: nanjekyejoannah Date: Sat, 4 May 2019 10:54:03 -0400 Subject: [PATCH 5/6] fix Doc --- Doc/whatsnew/3.8.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.8.rst b/Doc/whatsnew/3.8.rst index 116f6b1cd3478b3..c014097867081ea 100644 --- a/Doc/whatsnew/3.8.rst +++ b/Doc/whatsnew/3.8.rst @@ -334,7 +334,8 @@ pathlib contain characters unrepresentable at the OS level. (Contributed by Serhiy Storchaka in :issue:`33721`.) -Added :meth:`pathlib.Path.link()`. +Added :meth:`pathlib.Path.link_to()` which creates a hard link pointing +to a path. (Contributed by Joannah Nanjekye in :issue:`26978`) From 3291e1c5a584a14eeb7b7c7236e5c0fe8efc6ab6 Mon Sep 17 00:00:00 2001 From: Joannah Nanjekye <33177550+nanjekyejoannah@users.noreply.github.com> Date: Sat, 4 May 2019 11:00:06 -0400 Subject: [PATCH 6/6] update news entry --- .../next/Library/2019-04-28-01-52-39.bpo-26978.Lpm-SI.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 index b8b108fec56e088..0b14920ad45a81c 100644 --- 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 @@ -1 +1,2 @@ -`pathlib.path.link()` is now implemented. \ No newline at end of file +`pathlib.path.link_to()` is now implemented. It creates a hard link pointing +to a path.