classification
Title: PureWindowsPath.relative_to() is not case insensitive
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: pitrou Nosy List: chris.jerdonek, pitrou, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-12-07 11:02 by serhiy.storchaka, last changed 2014-12-18 15:00 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
pathlib_relative_to.patch serhiy.storchaka, 2013-12-07 11:47 review
pathlib_relative_to_2.patch serhiy.storchaka, 2013-12-07 13:02 review
Messages (7)
msg205448 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-07 11:02
>>> import pathlib
>>> pathlib.PureWindowsPath('C:/Foo/Bar').relative_to('C:/Foo')
PureWindowsPath('Bar')
>>> pathlib.PureWindowsPath('C:/Foo/Bar').relative_to('C:/foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/pathlib.py", line 797, in relative_to
    .format(str(self), str(formatted)))
ValueError: 'C:\\Foo\\Bar' does not start with 'C:\\foo'
>>> pathlib.PureWindowsPath('C:/Foo/Bar').relative_to('c:/Foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/serhiy/py/cpython/Lib/pathlib.py", line 797, in relative_to
    .format(str(self), str(formatted)))
ValueError: 'C:\\Foo\\Bar' does not start with 'c:\\Foo'

It also returns strange result when an argument is naked drive:

>>> pathlib.PureWindowsPath('C:/Foo/Bar').relative_to('C:')
PureWindowsPath('//Foo/Bar')
msg205450 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-07 11:47
Here is a patch which fixes first bug.
msg205452 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-12-07 13:02
Actually relative_to() returns invalid path object.

>>> import pathlib
>>> p = pathlib.PureWindowsPath('C:/Foo/Bar/Baz').relative_to('C:')
>>> p
PureWindowsPath('//Foo/Bar/Baz')
>>> str(p)
'\\\\Foo\\Bar\\Baz'
>>> p.drive
''
>>> p.root
''
>>> p.parts
('\\', 'Foo', 'Bar', 'Baz')
>>> p.is_absolute()
False
>>> p2 = pathlib.PureWindowsPath(str(p))
>>> p2.drive
'\\\\Foo\\Bar'
>>> p2.root
'\\'
>>> p2.parts
('\\\\Foo\\Bar\\', 'Baz')
>>> p2.is_absolute()
True

Here is a patch which fixes both bugs.

>>> import pathlib
>>> p = pathlib.PureWindowsPath('C:/Foo/Bar/Baz').relative_to('C:')
>>> p
PureWindowsPath('/Foo/Bar/Baz')
>>> str(p)
'\\Foo\\Bar\\Baz'
>>> p.drive
''
>>> p.root
'\\'
>>> p.parts
('\\', 'Foo', 'Bar', 'Baz')
msg207042 - (view) Author: Roundup Robot (python-dev) Date: 2013-12-28 18:49
New changeset 763f4416c4fd by Antoine Pitrou in branch 'default':
Issue #19918: Fix PurePath.relative_to() under Windows.
http://hg.python.org/cpython/rev/763f4416c4fd
msg207043 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-12-28 18:49
Thanks! I've tweaked the patch a bit and committed it.
msg232878 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2014-12-18 13:55
Was this also fixed for Mac OS X?  Mac OS X is also case-insensitive by default, and on Python 3.4.2 I'm getting:

>>> Path("Foo").relative_to("foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/pathlib.py", line 806, in relative_to
    .format(str(self), str(formatted)))
ValueError: 'Foo' does not start with 'foo'
msg232882 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-12-18 15:00
pathlib is case-sensitive under OS X (under any non-Windows platform actually).
History
Date User Action Args
2014-12-18 15:00:05pitrousetmessages: + msg232882
2014-12-18 13:55:37chris.jerdoneksetnosy: + chris.jerdonek
messages: + msg232878
2013-12-28 18:49:45pitrousetstatus: open -> closed
resolution: fixed
messages: + msg207043

stage: patch review -> resolved
2013-12-28 18:49:10python-devsetnosy: + python-dev
messages: + msg207042
2013-12-11 22:31:28serhiy.storchakasetassignee: pitrou
2013-12-07 13:02:46serhiy.storchakasetfiles: + pathlib_relative_to_2.patch

messages: + msg205452
2013-12-07 11:47:19serhiy.storchakasetfiles: + pathlib_relative_to.patch
keywords: + patch
messages: + msg205450

stage: needs patch -> patch review
2013-12-07 11:02:31serhiy.storchakacreate