Skip to content

Commit

Permalink
GH-110488: Fix two small issues in pathlib.PurePath.with_name() (#1…
Browse files Browse the repository at this point in the history
…10651)

Ensure that `PurePath('foo/a').with_name('.')` raises `ValueError`

Ensure that `PureWindowsPath('foo/a').with_name('a:b')` does not raise
`ValueError`.
  • Loading branch information
barneygale authored Oct 11, 2023
1 parent 790ecf6 commit b5f7777
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
3 changes: 1 addition & 2 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,7 @@ def with_name(self, name):
if not self.name:
raise ValueError("%r has an empty name" % (self,))
m = self.pathmod
drv, root, tail = m.splitroot(name)
if drv or root or not tail or m.sep in tail or (m.altsep and m.altsep in tail):
if not name or m.sep in name or (m.altsep and m.altsep in name) or name == '.':
raise ValueError("Invalid name %r" % (name))
return self._from_parsed_parts(self.drive, self.root,
self._tail[:-1] + [name])
Expand Down
14 changes: 10 additions & 4 deletions Lib/test/test_pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,7 @@ def test_with_name_common(self):
self.assertRaises(ValueError, P('.').with_name, 'd.xml')
self.assertRaises(ValueError, P('/').with_name, 'd.xml')
self.assertRaises(ValueError, P('a/b').with_name, '')
self.assertRaises(ValueError, P('a/b').with_name, '.')
self.assertRaises(ValueError, P('a/b').with_name, '/c')
self.assertRaises(ValueError, P('a/b').with_name, 'c/')
self.assertRaises(ValueError, P('a/b').with_name, 'c/d')
Expand All @@ -631,6 +632,7 @@ def test_with_stem_common(self):
self.assertRaises(ValueError, P('.').with_stem, 'd')
self.assertRaises(ValueError, P('/').with_stem, 'd')
self.assertRaises(ValueError, P('a/b').with_stem, '')
self.assertRaises(ValueError, P('a/b').with_stem, '.')
self.assertRaises(ValueError, P('a/b').with_stem, '/c')
self.assertRaises(ValueError, P('a/b').with_stem, 'c/')
self.assertRaises(ValueError, P('a/b').with_stem, 'c/d')
Expand Down Expand Up @@ -1235,8 +1237,10 @@ def test_with_name(self):
self.assertRaises(ValueError, P('c:').with_name, 'd.xml')
self.assertRaises(ValueError, P('c:/').with_name, 'd.xml')
self.assertRaises(ValueError, P('//My/Share').with_name, 'd.xml')
self.assertRaises(ValueError, P('c:a/b').with_name, 'd:')
self.assertRaises(ValueError, P('c:a/b').with_name, 'd:e')
self.assertEqual(str(P('a').with_name('d:')), '.\\d:')
self.assertEqual(str(P('a').with_name('d:e')), '.\\d:e')
self.assertEqual(P('c:a/b').with_name('d:'), P('c:a/d:'))
self.assertEqual(P('c:a/b').with_name('d:e'), P('c:a/d:e'))
self.assertRaises(ValueError, P('c:a/b').with_name, 'd:/e')
self.assertRaises(ValueError, P('c:a/b').with_name, '//My/Share')

Expand All @@ -1249,8 +1253,10 @@ def test_with_stem(self):
self.assertRaises(ValueError, P('c:').with_stem, 'd')
self.assertRaises(ValueError, P('c:/').with_stem, 'd')
self.assertRaises(ValueError, P('//My/Share').with_stem, 'd')
self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:')
self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:e')
self.assertEqual(str(P('a').with_stem('d:')), '.\\d:')
self.assertEqual(str(P('a').with_stem('d:e')), '.\\d:e')
self.assertEqual(P('c:a/b').with_stem('d:'), P('c:a/d:'))
self.assertEqual(P('c:a/b').with_stem('d:e'), P('c:a/d:e'))
self.assertRaises(ValueError, P('c:a/b').with_stem, 'd:/e')
self.assertRaises(ValueError, P('c:a/b').with_stem, '//My/Share')

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Fix a couple of issues in :meth:`pathlib.PurePath.with_name`: a single dot
was incorrectly considered a valid name, and in :class:`PureWindowsPath`, a
name with an NTFS alternate data stream, like ``a:b``, was incorrectly
considered invalid.

0 comments on commit b5f7777

Please sign in to comment.