Issue #21127: Path objects can now be instantiated from str subclass instances (such as numpy.str_).
Thanks to Antony Lee for the report and preliminary patch.
This commit is contained in:
parent
9573638c2d
commit
cb5ec77d33
@ -574,8 +574,8 @@ class PurePath(object):
|
|||||||
if isinstance(a, PurePath):
|
if isinstance(a, PurePath):
|
||||||
parts += a._parts
|
parts += a._parts
|
||||||
elif isinstance(a, str):
|
elif isinstance(a, str):
|
||||||
# Assuming a str
|
# Force-cast str subclasses to str (issue #21127)
|
||||||
parts.append(a)
|
parts.append(str(a))
|
||||||
else:
|
else:
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"argument should be a path or str object, not %r"
|
"argument should be a path or str object, not %r"
|
||||||
|
@ -197,6 +197,25 @@ class _BasePurePathTest(object):
|
|||||||
self.assertEqual(P(P('a'), 'b'), P('a/b'))
|
self.assertEqual(P(P('a'), 'b'), P('a/b'))
|
||||||
self.assertEqual(P(P('a'), P('b')), P('a/b'))
|
self.assertEqual(P(P('a'), P('b')), P('a/b'))
|
||||||
|
|
||||||
|
def _check_str_subclass(self, *args):
|
||||||
|
# Issue #21127: it should be possible to construct a PurePath object
|
||||||
|
# from an str subclass instance, and it then gets converted to
|
||||||
|
# a pure str object.
|
||||||
|
class StrSubclass(str):
|
||||||
|
pass
|
||||||
|
P = self.cls
|
||||||
|
p = P(*(StrSubclass(x) for x in args))
|
||||||
|
self.assertEqual(p, P(*args))
|
||||||
|
for part in p.parts:
|
||||||
|
self.assertIs(type(part), str)
|
||||||
|
|
||||||
|
def test_str_subclass_common(self):
|
||||||
|
self._check_str_subclass('')
|
||||||
|
self._check_str_subclass('.')
|
||||||
|
self._check_str_subclass('a')
|
||||||
|
self._check_str_subclass('a/b.txt')
|
||||||
|
self._check_str_subclass('/a/b.txt')
|
||||||
|
|
||||||
def test_join_common(self):
|
def test_join_common(self):
|
||||||
P = self.cls
|
P = self.cls
|
||||||
p = P('a/b')
|
p = P('a/b')
|
||||||
@ -690,6 +709,17 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
|
|||||||
p = self.cls('//a/b/c/d')
|
p = self.cls('//a/b/c/d')
|
||||||
self.assertEqual(str(p), '\\\\a\\b\\c\\d')
|
self.assertEqual(str(p), '\\\\a\\b\\c\\d')
|
||||||
|
|
||||||
|
def test_str_subclass(self):
|
||||||
|
self._check_str_subclass('c:')
|
||||||
|
self._check_str_subclass('c:a')
|
||||||
|
self._check_str_subclass('c:a\\b.txt')
|
||||||
|
self._check_str_subclass('c:\\')
|
||||||
|
self._check_str_subclass('c:\\a')
|
||||||
|
self._check_str_subclass('c:\\a\\b.txt')
|
||||||
|
self._check_str_subclass('\\\\some\\share')
|
||||||
|
self._check_str_subclass('\\\\some\\share\\a')
|
||||||
|
self._check_str_subclass('\\\\some\\share\\a\\b.txt')
|
||||||
|
|
||||||
def test_eq(self):
|
def test_eq(self):
|
||||||
P = self.cls
|
P = self.cls
|
||||||
self.assertEqual(P('c:a/b'), P('c:a/b'))
|
self.assertEqual(P('c:a/b'), P('c:a/b'))
|
||||||
|
@ -743,6 +743,7 @@ Julia Lawall
|
|||||||
Chris Lawrence
|
Chris Lawrence
|
||||||
Brian Leair
|
Brian Leair
|
||||||
Mathieu Leduc-Hamel
|
Mathieu Leduc-Hamel
|
||||||
|
Antony Lee
|
||||||
Christopher Lee
|
Christopher Lee
|
||||||
Inyeol Lee
|
Inyeol Lee
|
||||||
James Lee
|
James Lee
|
||||||
|
@ -39,6 +39,9 @@ Core and Builtins
|
|||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #21127: Path objects can now be instantiated from str subclass
|
||||||
|
instances (such as numpy.str_).
|
||||||
|
|
||||||
- Issue #15002: urllib.response object to use _TemporaryFileWrapper (and
|
- Issue #15002: urllib.response object to use _TemporaryFileWrapper (and
|
||||||
_TemporaryFileCloser) facility. Provides a better way to handle file
|
_TemporaryFileCloser) facility. Provides a better way to handle file
|
||||||
descriptor close. Patch contributed by Christian Theune.
|
descriptor close. Patch contributed by Christian Theune.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user