FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-26978: Implement pathlib.Path.link_to (Using os.link) by nanjekyejoannah · Pull Request #12990 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (3) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
7 changes: 7 additions & 0 deletions Doc/library/pathlib.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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


Comment thread
nanjekyejoannah marked this conversation as resolved.
.. method:: Path.write_bytes(data)

Open the file pointed to in bytes mode, write *data* to it, and close the
Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.8.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,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`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
(Contributed by Joannah Nanjekye in :issue:`26978`)
(Contributed by Joannah Nanjekye in :issue:`26978`.)



socket
------
Expand Down
10 changes: 10 additions & 0 deletions Lib/pathlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ def lchmod(self, pathobj, mode):

unlink = os.unlink

link_to = os.link

rmdir = os.rmdir

rename = os.rename
Expand Down Expand Up @@ -1303,6 +1305,14 @@ def lstat(self):
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.
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_pathlib.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,25 @@ def test_rmdir(self):
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)
Comment thread
nanjekyejoannah marked this conversation as resolved.
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'
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
`pathlib.path.link_to()` is now implemented. It creates a hard link pointing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It should be pathlib.Path.link_to(), not pathlib.path.link_to().

Also, double backqoutes should be used here instead of single backquotes (as per https://devguide.python.org/documenting/#inline-markup). The following could also be used:

:meth:`pathlib.Path.link_to()`

to a path.

Back | FazBrowse Home | New Git URL