Issue #21714: Disallow the construction of invalid paths using Path.with_name(). Original patch by Antony Lee.

This commit is contained in:
Antoine Pitrou 2014-07-06 21:31:12 -04:00
parent 7bc5fb6916
commit 7084e736db
3 changed files with 15 additions and 0 deletions

View file

@ -749,6 +749,10 @@ class PurePath(object):
"""Return a new path with the file name changed."""
if not self.name:
raise ValueError("%r has an empty name" % (self,))
drv, root, parts = self._flavour.parse_parts((name,))
if (not name or name[-1] in [self._flavour.sep, self._flavour.altsep]
or drv or root or len(parts) != 1):
raise ValueError("Invalid name %r" % (name))
return self._from_parsed_parts(self._drv, self._root,
self._parts[:-1] + [name])