Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
43e3d940
Commit
43e3d940
authored
May 13, 2014
by
Antoine Pitrou
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #19775: Add a samefile() method to pathlib Path objects.
Initial patch by Vajrasky Kok.
parent
38acd4c0
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
53 additions
and
0 deletions
+53
-0
Doc/library/pathlib.rst
Doc/library/pathlib.rst
+19
-0
Lib/pathlib.py
Lib/pathlib.py
+11
-0
Lib/test/test_pathlib.py
Lib/test/test_pathlib.py
+20
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Doc/library/pathlib.rst
View file @
43e3d940
...
...
@@ -884,6 +884,25 @@ call fails (for example because the path doesn't exist):
Remove this directory. The directory must be empty.
.. method:: Path.samefile(other_path)
Return whether this path points to the same file as *other_path*, which
can be either a Path object, or a string. The semantics are similar
to :func:`os.path.samefile` and :func:`os.path.samestat`.
An :exc:`OSError` can be raised if either file cannot be accessed for some
reason.
>>> p = Path('spam')
>>> q = Path('eggs')
>>> p.samefile(q)
False
>>> p.samefile('spam')
True
.. versionadded:: 3.5
.. method:: Path.symlink_to(target, target_is_directory=False)
Make this path a symbolic link to *target*. Under Windows,
...
...
Lib/pathlib.py
View file @
43e3d940
...
...
@@ -961,6 +961,17 @@ class Path(PurePath):
"""
return
cls
(
os
.
getcwd
())
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)).
"""
st
=
self
.
stat
()
try
:
other_st
=
other_path
.
stat
()
except
AttributeError
:
other_st
=
os
.
stat
(
other_path
)
return
os
.
path
.
samestat
(
st
,
other_st
)
def
iterdir
(
self
):
"""Iterate over the files in this directory. Does not yield any
result for the special paths '.' and '..'.
...
...
Lib/test/test_pathlib.py
View file @
43e3d940
...
...
@@ -1251,6 +1251,26 @@ class _BasePathTest(object):
p
=
self
.
cls
.
cwd
()
self
.
_test_cwd
(
p
)
def
test_samefile
(
self
):
fileA_path
=
os
.
path
.
join
(
BASE
,
'fileA'
)
fileB_path
=
os
.
path
.
join
(
BASE
,
'dirB'
,
'fileB'
)
p
=
self
.
cls
(
fileA_path
)
pp
=
self
.
cls
(
fileA_path
)
q
=
self
.
cls
(
fileB_path
)
self
.
assertTrue
(
p
.
samefile
(
fileA_path
))
self
.
assertTrue
(
p
.
samefile
(
pp
))
self
.
assertFalse
(
p
.
samefile
(
fileB_path
))
self
.
assertFalse
(
p
.
samefile
(
q
))
# Test the non-existent file case
non_existent
=
os
.
path
.
join
(
BASE
,
'foo'
)
r
=
self
.
cls
(
non_existent
)
self
.
assertRaises
(
FileNotFoundError
,
p
.
samefile
,
r
)
self
.
assertRaises
(
FileNotFoundError
,
p
.
samefile
,
non_existent
)
self
.
assertRaises
(
FileNotFoundError
,
r
.
samefile
,
p
)
self
.
assertRaises
(
FileNotFoundError
,
r
.
samefile
,
non_existent
)
self
.
assertRaises
(
FileNotFoundError
,
r
.
samefile
,
r
)
self
.
assertRaises
(
FileNotFoundError
,
r
.
samefile
,
non_existent
)
def
test_empty_path
(
self
):
# The empty path points to '.'
p
=
self
.
cls
(
''
)
...
...
Misc/NEWS
View file @
43e3d940
...
...
@@ -84,6 +84,9 @@ Core and Builtins
Library
-------
- Issue #19775: Add a samefile() method to pathlib Path objects. Initial
patch by Vajrasky Kok.
- Issue #21398: Fix an unicode error in the pydoc pager when the documentation
contains characters not encodable to the stdout encoding.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment