Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
resolve conflicts with GH-19611
  • Loading branch information
domragusa committed May 28, 2020
commit 6d721a0ee1fbc841fd555d3acf173f56f36ba410
19 changes: 9 additions & 10 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -549,25 +549,24 @@ Pure paths provide the following methods and properties:
>>> p.relative_to('/usr')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pathlib.py", line 940, in relative_to
File "pathlib.py", line 941, in relative_to
raise ValueError(error_message.format(str(self), str(formatted)))
ValueError: '/etc/passwd' does not start with '/usr'
ValueError: '/etc/passwd' is not in the subpath of '/usr' OR one path is relative and the other is absolute.
>>> p.relative_to('/usr', strict=False)
PurePosixPath('../etc/passwd')
>>> p.relative_to('foo', strict=False)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "pathlib.py", line 940, in relative_to
File "pathlib.py", line 941, in relative_to
raise ValueError(error_message.format(str(self), str(formatted)))
ValueError: '/etc/passwd' is not related to 'foo'
ValueError: '/etc/passwd' is not on the same drive as 'foo' OR one path is relative and the other is absolute.

If the path doesn't start with *other* and *strict* is ``True``,
:exc:`ValueError` is raised. If *strict* is ``False`` and the paths are
not both relative or both absolute :exc:`ValueError` is raised (on Windows
both paths must reference the same drive as well).
If the path doesn't start with *other* and *strict* is ``True``, :exc:`ValueError` is raised. If *strict* is ``False`` and one path is relative and the other is absolute or if they reference different drives :exc:`ValueError` is raised.
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.

I think the key difference should be spelled out in terms of whether adding .. entries is allowed. How about something like this?

In strict mode (the default), the path must start with *other*. In non-strict 
mode, ``..`` entries may be added to form the relative path. In all other 
cases, such as the paths referencing different drives, :exe:`ValueError` is 
raised.

.. warning::
    Non-strict mode assumes that no symlinks are present in the path; you 
    should call :meth:`~Path.resolve` first to ensure this.


.. versionadded:: 3.9
The *strict* parameter was added.
NOTE: This function is part of :class:`PurePath` and works with strings. It does not check or access the underlying file structure.

.. versionadded:: 3.10
The *strict* argument (pre-3.10 behavior is strict).
Comment thread
domragusa marked this conversation as resolved.
Outdated


.. method:: PurePath.with_name(name)
Expand Down
5 changes: 3 additions & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,14 +927,15 @@ def relative_to(self, *other, strict=True):
common += 1
if strict:
failure = (root or drv) if n == 0 else common != n
error_message = "{!r} does not start with {!r}"
error_message = "{!r} is not in the subpath of {!r}"
up_parts = []
else:
failure = root != to_root
if drv or to_drv:
failure = cf([drv]) != cf([to_drv]) or (failure and n > 1)
error_message = "{!r} is not related to {!r}"
error_message = "{!r} is not on the same drive as {!r}"
up_parts = (n-common)*['..']
error_message += " OR one path is relative and the other is absolute."
if failure:
formatted = self._format_parsed_parts(to_drv, to_root, to_parts)
raise ValueError(error_message.format(str(self), str(formatted)))
Comment thread
brettcannon marked this conversation as resolved.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Add strict parameter in :meth:`pathlib.PurePath.relative_to`. Patch by
Domenico Ragusa.
Add strict argument in :meth:`pathlib.PurePath.relative_to`.
You are viewing a condensed version of this merge commit. You can view the full changes here.