mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-99029: Fix handling of PureWindowsPath('C:\<blah>').relative_to('C:')
(GH-99031)
`relative_to()` now treats naked drive paths as relative. This brings its behaviour in line with other parts of pathlib, and with `ntpath.relpath()`, and so allows us to factor out the pathlib-specific implementation.
This commit is contained in:
parent
7d2dcc53d0
commit
ae234fbc5c
3 changed files with 20 additions and 52 deletions
|
@ -1183,10 +1183,6 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
|
|||
self.assertRaises(ValueError, p.relative_to, P('/Foo'), walk_up=True)
|
||||
self.assertRaises(ValueError, p.relative_to, P('C:/Foo'), walk_up=True)
|
||||
p = P('C:/Foo/Bar')
|
||||
self.assertEqual(p.relative_to(P('c:')), P('/Foo/Bar'))
|
||||
self.assertEqual(p.relative_to('c:'), P('/Foo/Bar'))
|
||||
self.assertEqual(str(p.relative_to(P('c:'))), '\\Foo\\Bar')
|
||||
self.assertEqual(str(p.relative_to('c:')), '\\Foo\\Bar')
|
||||
self.assertEqual(p.relative_to(P('c:/')), P('Foo/Bar'))
|
||||
self.assertEqual(p.relative_to('c:/'), P('Foo/Bar'))
|
||||
self.assertEqual(p.relative_to(P('c:/foO')), P('Bar'))
|
||||
|
@ -1194,10 +1190,6 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
|
|||
self.assertEqual(p.relative_to('c:/foO/'), P('Bar'))
|
||||
self.assertEqual(p.relative_to(P('c:/foO/baR')), P())
|
||||
self.assertEqual(p.relative_to('c:/foO/baR'), P())
|
||||
self.assertEqual(p.relative_to(P('c:'), walk_up=True), P('/Foo/Bar'))
|
||||
self.assertEqual(p.relative_to('c:', walk_up=True), P('/Foo/Bar'))
|
||||
self.assertEqual(str(p.relative_to(P('c:'), walk_up=True)), '\\Foo\\Bar')
|
||||
self.assertEqual(str(p.relative_to('c:', walk_up=True)), '\\Foo\\Bar')
|
||||
self.assertEqual(p.relative_to(P('c:/'), walk_up=True), P('Foo/Bar'))
|
||||
self.assertEqual(p.relative_to('c:/', walk_up=True), P('Foo/Bar'))
|
||||
self.assertEqual(p.relative_to(P('c:/foO'), walk_up=True), P('Bar'))
|
||||
|
@ -1209,6 +1201,8 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
|
|||
self.assertEqual(p.relative_to('C:/Foo/Bar/Baz', walk_up=True), P('..'))
|
||||
self.assertEqual(p.relative_to('C:/Foo/Baz', walk_up=True), P('../Bar'))
|
||||
# Unrelated paths.
|
||||
self.assertRaises(ValueError, p.relative_to, 'c:')
|
||||
self.assertRaises(ValueError, p.relative_to, P('c:'))
|
||||
self.assertRaises(ValueError, p.relative_to, P('C:/Baz'))
|
||||
self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Bar/Baz'))
|
||||
self.assertRaises(ValueError, p.relative_to, P('C:/Foo/Baz'))
|
||||
|
@ -1218,6 +1212,8 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
|
|||
self.assertRaises(ValueError, p.relative_to, P('/'))
|
||||
self.assertRaises(ValueError, p.relative_to, P('/Foo'))
|
||||
self.assertRaises(ValueError, p.relative_to, P('//C/Foo'))
|
||||
self.assertRaises(ValueError, p.relative_to, 'c:', walk_up=True)
|
||||
self.assertRaises(ValueError, p.relative_to, P('c:'), walk_up=True)
|
||||
self.assertRaises(ValueError, p.relative_to, P('C:Foo'), walk_up=True)
|
||||
self.assertRaises(ValueError, p.relative_to, P('d:'), walk_up=True)
|
||||
self.assertRaises(ValueError, p.relative_to, P('d:/'), walk_up=True)
|
||||
|
@ -1275,13 +1271,13 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
|
|||
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('c:'))
|
||||
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')))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue