1. Antoine Pitrou
  2. pathlib

Commits

Antoine Pitrou  committed 8e0516d

`parts` is now simply a tuple

  • Participants
  • Parent commits 5146727
  • Branches pep428

Comments (0)

Files changed (3)

File docs/index.rst

View file
 
 .. data:: PurePath.parts
 
-   An immutable sequence-like object giving access to the path's various
-   components.  Indexing this object returns individual strings, while
-   slicing this object returns other path objects of the same flavour::
+   A tuple giving access to the path's various components::
 
       >>> p = PurePath('/usr/bin/python3')
       >>> p.parts
-      <PurePosixPath.parts: ['/', 'usr', 'bin', 'python3']>
-      >>> p.parts[0]
-      '/'
-      >>> p.parts[-1]
-      'python3'
-      >>> p.parts[1:]
-      PurePosixPath('usr/bin/python3')
-      >>> p.parts[:-1]
-      PurePosixPath('/usr/bin')
+      ('/', 'usr', 'bin', 'python3')
 
       >>> p = PureWindowsPath('c:/Program Files/PSF')
-      >>> p.parts[0]
-      'c:\\'
-      >>> p.parts[1:]
-      PureWindowsPath('Program Files/PSF')
+      >>> p.parts
+      ('c:\\', 'Program Files', 'PSF')
 
    (note how the drive and local root are regrouped in a single part)
 

File pathlib.py

View file
 # Public API
 #
 
-class _PathParts(Sequence):
-    """This object provides sequence-like access to the parts of a path.
-    Don't try to construct it yourself."""
-    __slots__ = ('_pathcls', '_parts')
+#class _PathParts(Sequence):
+    #"""This object provides sequence-like access to the parts of a path.
+    #Don't try to construct it yourself."""
+    #__slots__ = ('_pathcls', '_parts')
 
-    def __init__(self, path):
-        # We don't store the instance to avoid reference cycles
-        self._pathcls = type(path)
-        self._parts = path._parts
+    #def __init__(self, path):
+        ## We don't store the instance to avoid reference cycles
+        #self._pathcls = type(path)
+        #self._parts = path._parts
 
-    def __len__(self):
-        return len(self._parts)
+    #def __len__(self):
+        #return len(self._parts)
 
-    def __getitem__(self, idx):
-        if isinstance(idx, slice):
-            return self._pathcls(*self._parts[idx])
-        return self._parts[idx]
+    #def __getitem__(self, idx):
+        #if isinstance(idx, slice):
+            #return self._pathcls(*self._parts[idx])
+        #return self._parts[idx]
 
-    def __repr__(self):
-        return "<{}.parts: {!r}>".format(self._pathcls.__name__, self._parts)
+    #def __repr__(self):
+        #return "<{}.parts: {!r}>".format(self._pathcls.__name__, self._parts)
 
 
 class PurePath(object):
     def parts(self):
         """An object providing sequence-like access to the
         components in the filesystem path."""
+        # We cache the tuple to avoid building a new one each time .parts
+        # is accessed.  XXX is this necessary?
         try:
             return self._pparts
         except AttributeError:
-            self._pparts = _PathParts(self)
+            self._pparts = tuple(self._parts)
             return self._pparts
 
     def joinpath(self, *args):

File test_pathlib.py

View file
             P() < {}
 
     def test_parts_common(self):
+        # `parts` returns a tuple
         sep = self.sep
         P = self.cls
         p = P('a/b')
         parts = p.parts
+        self.assertEqual(parts, ('a', 'b'))
         # The object gets reused
         self.assertIs(parts, p.parts)
-        # Sequence protocol
-        self.assertIsInstance(parts, collections.Sequence)
-        self.assertEqual(len(parts), 2)
-        self.assertEqual(parts[0], 'a')
-        self.assertEqual(list(parts), ['a', 'b'])
-        self.assertEqual(parts[:], P('a/b'))
-        self.assertEqual(parts[0:1], P('a'))
+        # When the path is absolute, the anchor is a separate part
         p = P('/a/b')
         parts = p.parts
-        self.assertEqual(len(parts), 3)
-        self.assertEqual(parts[0], sep)
-        self.assertEqual(list(parts), [sep, 'a', 'b'])
-        self.assertEqual(parts[:], P('/a/b'))
-        self.assertEqual(parts[:2], P('/a'))
-        self.assertEqual(parts[1:], P('a/b'))
+        self.assertEqual(parts, (sep, 'a', 'b'))
 
     def test_equivalences(self):
         for k, tuples in self.equivalences.items():
         P = self.cls
         p = P('c:a/b')
         parts = p.parts
-        self.assertEqual(len(parts), 3)
-        self.assertEqual(list(parts), ['c:', 'a', 'b'])
-        self.assertEqual(parts[:2], P('c:a'))
-        self.assertEqual(parts[1:], P('a/b'))
+        self.assertEqual(parts, ('c:', 'a', 'b'))
         p = P('c:/a/b')
         parts = p.parts
-        self.assertEqual(len(parts), 3)
-        self.assertEqual(list(parts), ['c:\\', 'a', 'b'])
-        self.assertEqual(parts[:2], P('c:/a'))
-        self.assertEqual(parts[1:], P('a/b'))
+        self.assertEqual(parts, ('c:\\', 'a', 'b'))
         p = P('//a/b/c/d')
         parts = p.parts
-        self.assertEqual(len(parts), 3)
-        self.assertEqual(list(parts), ['\\\\a\\b\\', 'c', 'd'])
-        self.assertEqual(parts[:2], P('//a/b/c'))
-        self.assertEqual(parts[1:], P('c/d'))
+        self.assertEqual(parts, ('\\\\a\\b\\', 'c', 'd'))
 
     def test_parent(self):
         # Anchored