]> granicus.if.org Git - python/commitdiff
Issue #21127: Path objects can now be instantiated from str subclass instances (such...
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 22 Apr 2014 22:34:15 +0000 (00:34 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 22 Apr 2014 22:34:15 +0000 (00:34 +0200)
Thanks to Antony Lee for the report and preliminary patch.

Lib/pathlib.py
Lib/test/test_pathlib.py
Misc/ACKS
Misc/NEWS

index 8d08e8b41396f400a582bbb252f466f479f191fb..d3d1af8decee8e8be6ad66797ecaad387521d797 100644 (file)
@@ -574,8 +574,8 @@ class PurePath(object):
             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"
index 2268f94a725cfb7d4cc98c49c166e18395bdc07e..6378d8c7f9f24c8c79af584b012aacd893ba2132 100644 (file)
@@ -197,6 +197,25 @@ class _BasePurePathTest(object):
         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 @@ class PureWindowsPathTest(_BasePurePathTest, unittest.TestCase):
         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'))
index 4173917e7314d79419d128e656a74a630f9c20b7..6064bfd5624645cc54aca3f566c75d0c842cedca 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -743,6 +743,7 @@ Julia Lawall
 Chris Lawrence
 Brian Leair
 Mathieu Leduc-Hamel
+Antony Lee
 Christopher Lee
 Inyeol Lee
 James Lee
index fcdaabfe92fd790688ded05a63d4b725fb6cf43e..332c7efb6c9c06fb72fc69be1f8c20305c1dfdf4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -39,6 +39,9 @@ Core and Builtins
 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.