changeset: 90433:aad6d6b819ed parent: 90430:a15bc8c12370 parent: 90432:c24cbd9bd63b user: Antoine Pitrou date: Wed Apr 23 00:34:51 2014 +0200 files: Misc/ACKS Misc/NEWS description: 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. diff -r a15bc8c12370 -r aad6d6b819ed Lib/pathlib.py --- a/Lib/pathlib.py Tue Apr 22 01:27:06 2014 -0400 +++ b/Lib/pathlib.py Wed Apr 23 00:34:51 2014 +0200 @@ -574,8 +574,8 @@ if isinstance(a, PurePath): parts += a._parts elif isinstance(a, str): - # Assuming a str - parts.append(a) + # Force-cast str subclasses to str (issue #21127) + parts.append(str(a)) else: raise TypeError( "argument should be a path or str object, not %r" diff -r a15bc8c12370 -r aad6d6b819ed Lib/test/test_pathlib.py --- a/Lib/test/test_pathlib.py Tue Apr 22 01:27:06 2014 -0400 +++ b/Lib/test/test_pathlib.py Wed Apr 23 00:34:51 2014 +0200 @@ -197,6 +197,25 @@ self.assertEqual(P(P('a'), '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): P = self.cls p = P('a/b') @@ -690,6 +709,17 @@ p = self.cls('//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): P = self.cls self.assertEqual(P('c:a/b'), P('c:a/b')) diff -r a15bc8c12370 -r aad6d6b819ed Misc/ACKS --- a/Misc/ACKS Tue Apr 22 01:27:06 2014 -0400 +++ b/Misc/ACKS Wed Apr 23 00:34:51 2014 +0200 @@ -744,6 +744,7 @@ Chris Lawrence Brian Leair Mathieu Leduc-Hamel +Antony Lee Christopher Lee Inyeol Lee James Lee diff -r a15bc8c12370 -r aad6d6b819ed Misc/NEWS --- a/Misc/NEWS Tue Apr 22 01:27:06 2014 -0400 +++ b/Misc/NEWS Wed Apr 23 00:34:51 2014 +0200 @@ -54,6 +54,9 @@ 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 _TemporaryFileCloser) facility. Provides a better way to handle file descriptor close. Patch contributed by Christian Theune.