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
82642a05
Commit
82642a05
authored
Aug 13, 2019
by
Hai Shi
Committed by
Antoine Pitrou
Aug 13, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37689: add Path.is_relative_to() method (GH-14982)
parent
8a784af7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
111 additions
and
1 deletion
+111
-1
Doc/library/pathlib.rst
Doc/library/pathlib.rst
+14
-1
Lib/pathlib.py
Lib/pathlib.py
+9
-0
Lib/test/test_pathlib.py
Lib/test/test_pathlib.py
+87
-0
Misc/NEWS.d/next/Library/2019-07-27-18-00-43.bpo-37689.glEmZi.rst
...S.d/next/Library/2019-07-27-18-00-43.bpo-37689.glEmZi.rst
+1
-0
No files found.
Doc/library/pathlib.rst
View file @
82642a05
...
...
@@ -273,7 +273,7 @@ Methods and properties
.. testsetup::
from pathlib import PurePosixPath, PureWindowsPath
from pathlib import PureP
ath, PureP
osixPath, PureWindowsPath
Pure paths provide the following methods and properties:
...
...
@@ -462,6 +462,19 @@ Pure paths provide the following methods and properties:
True
.. method:: PurePath.is_relative_to(*other)
Return whether or not this path is relative to the *other* path.
>>> p = PurePath('/etc/passwd')
>>> p.is_relative_to('/etc')
True
>>> p.is_relative_to('/usr')
False
.. versionadded:: 3.9
.. method:: PurePath.is_reserved()
With :class:`PureWindowsPath`, return ``True`` if the path is considered
...
...
Lib/pathlib.py
View file @
82642a05
...
...
@@ -886,6 +886,15 @@ class PurePath(object):
return
self
.
_from_parsed_parts
(
''
,
root
if
n
==
1
else
''
,
abs_parts
[
n
:])
def
is_relative_to
(
self
,
*
other
):
"""Return True if the path is relative to another path or False.
"""
try
:
self
.
relative_to
(
*
other
)
return
True
except
ValueError
:
return
False
@
property
def
parts
(
self
):
"""An object providing sequence-like access to the
...
...
Lib/test/test_pathlib.py
View file @
82642a05
...
...
@@ -619,6 +619,40 @@ class _BasePurePathTest(object):
self
.
assertRaises
(
ValueError
,
p
.
relative_to
,
''
)
self
.
assertRaises
(
ValueError
,
p
.
relative_to
,
P
(
'a'
))
def
test_is_relative_to_common
(
self
):
P
=
self
.
cls
p
=
P
(
'a/b'
)
self
.
assertRaises
(
TypeError
,
p
.
is_relative_to
)
self
.
assertRaises
(
TypeError
,
p
.
is_relative_to
,
b'a'
)
self
.
assertTrue
(
p
.
is_relative_to
(
P
()))
self
.
assertTrue
(
p
.
is_relative_to
(
''
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'a'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'a/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'a/b'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'a/b'
))
# With several args.
self
.
assertTrue
(
p
.
is_relative_to
(
'a'
,
'b'
))
# Unrelated paths.
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'c'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'a/b/c'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'a/c'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/a'
)))
p
=
P
(
'/a/b'
)
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'/'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'/a'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'/a'
))
self
.
assertTrue
(
p
.
is_relative_to
(
'/a/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'/a/b'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'/a/b'
))
# Unrelated paths.
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/c'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/a/b/c'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/a/c'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
()))
self
.
assertFalse
(
p
.
is_relative_to
(
''
))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'a'
)))
def
test_pickling_common
(
self
):
P
=
self
.
cls
p
=
P
(
'/a/b'
)
...
...
@@ -1062,6 +1096,59 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
self
.
assertRaises
(
ValueError
,
p
.
relative_to
,
P
(
'//z/Share/Foo'
))
self
.
assertRaises
(
ValueError
,
p
.
relative_to
,
P
(
'//Server/z/Foo'
))
def
test_is_relative_to
(
self
):
P
=
self
.
cls
p
=
P
(
'C:Foo/Bar'
)
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'c:'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'c:'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'c:foO'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'c:foO'
))
self
.
assertTrue
(
p
.
is_relative_to
(
'c:foO/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'c:foO/baR'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'c:foO/baR'
))
# Unrelated paths.
self
.
assertFalse
(
p
.
is_relative_to
(
P
()))
self
.
assertFalse
(
p
.
is_relative_to
(
''
))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'd:'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:/Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:Foo/Bar/Baz'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:Foo/Baz'
)))
p
=
P
(
'C:/Foo/Bar'
)
self
.
assertTrue
(
p
.
is_relative_to
(
'c:'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'c:/'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'c:/foO'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'c:/foO/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'c:/foO/baR'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'c:/foO/baR'
))
# Unrelated paths.
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:/Baz'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:/Foo/Bar/Baz'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:/Foo/Baz'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'C:Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'd:'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'd:/'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'//C/Foo'
)))
# UNC paths.
p
=
P
(
'//Server/Share/Foo/Bar'
)
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'//sErver/sHare'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'//sErver/sHare'
))
self
.
assertTrue
(
p
.
is_relative_to
(
'//sErver/sHare/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'//sErver/sHare/Foo'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'//sErver/sHare/Foo'
))
self
.
assertTrue
(
p
.
is_relative_to
(
'//sErver/sHare/Foo/'
))
self
.
assertTrue
(
p
.
is_relative_to
(
P
(
'//sErver/sHare/Foo/Bar'
)))
self
.
assertTrue
(
p
.
is_relative_to
(
'//sErver/sHare/Foo/Bar'
))
# Unrelated paths.
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'/Server/Share/Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'c:/Server/Share/Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'//z/Share/Foo'
)))
self
.
assertFalse
(
p
.
is_relative_to
(
P
(
'//Server/z/Foo'
)))
def
test_is_absolute
(
self
):
P
=
self
.
cls
# Under NT, only paths with both a drive and a root are absolute.
...
...
Misc/NEWS.d/next/Library/2019-07-27-18-00-43.bpo-37689.glEmZi.rst
0 → 100644
View file @
82642a05
Add :meth:`is_relative_to` in :class:`PurePath` to determine whether or not one path is relative to another.
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