]> granicus.if.org Git - python/commitdiff
Issue #21549: Added the "members" parameter to TarFile.list().
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 21 Aug 2014 07:01:16 +0000 (10:01 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 21 Aug 2014 07:01:16 +0000 (10:01 +0300)
Doc/library/tarfile.rst
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

index 15b88f87b76e929c3c2965ff1add07ee57212b1c..3adb6331038daa8b4c822b01b35b4c3a1e6e5aa9 100644 (file)
@@ -325,11 +325,15 @@ be finalized; only the internally used file object will be closed. See the
    returned by :meth:`getmembers`.
 
 
-.. method:: TarFile.list(verbose=True)
+.. method:: TarFile.list(verbose=True, *, members=None)
 
    Print a table of contents to ``sys.stdout``. If *verbose* is :const:`False`,
    only the names of the members are printed. If it is :const:`True`, output
-   similar to that of :program:`ls -l` is produced.
+   similar to that of :program:`ls -l` is produced. If optional *members* is
+   given, it must be a subset of the list returned by :meth:`getmembers`.
+
+   .. versionchanged:: 3.5
+      Added the *members* parameter.
 
 
 .. method:: TarFile.next()
index 9e291c2a584609fec742862075da078ad8f5f665..4b4e0d35e14b3559db058d5561b41f79c43fdd50 100755 (executable)
@@ -1842,14 +1842,17 @@ class TarFile(object):
                 tarinfo.devminor = os.minor(statres.st_rdev)
         return tarinfo
 
-    def list(self, verbose=True):
+    def list(self, verbose=True, *, members=None):
         """Print a table of contents to sys.stdout. If `verbose' is False, only
            the names of the members are printed. If it is True, an `ls -l'-like
-           output is produced.
+           output is produced. `members' is optional and must be a subset of the
+           list returned by getmembers().
         """
         self._check()
 
-        for tarinfo in self:
+        if members is None:
+            members = self
+        for tarinfo in members:
             if verbose:
                 _safe_print(stat.filemode(tarinfo.mode))
                 _safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid,
index e527e403fa68365831b24102601c56b143053026..810b76be53242394c9a3de05dcbc6507fb797fb4 100644 (file)
@@ -285,6 +285,18 @@ class ListTest(ReadTest, unittest.TestCase):
         self.assertIn(b'pax' + (b'/123' * 125) + b'/longlink link to pax' +
                       (b'/123' * 125) + b'/longname', out)
 
+    def test_list_members(self):
+        tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
+        def members(tar):
+            for tarinfo in tar.getmembers():
+                if 'reg' in tarinfo.name:
+                    yield tarinfo
+        with support.swap_attr(sys, 'stdout', tio):
+            self.tar.list(verbose=False, members=members(self.tar))
+        out = tio.detach().getvalue()
+        self.assertIn(b'ustar/regtype', out)
+        self.assertNotIn(b'ustar/conttype', out)
+
 
 class GzipListTest(GzipTest, ListTest):
     pass
index e6e09edea6d9d4e47d521b6758ac9f8b72009d9f..5ac80b822ac4b30177762d0f922bf87d0542d7ea 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -124,6 +124,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #21549: Added the "members" parameter to TarFile.list().
+
 - Issue #19628: Allow compileall recursion depth to be specified with a -r
   option.