[Python-checkins] cpython: Issue #21549: Added the "members" parameter to TarFile.list().

serhiy.storchaka python-checkins at python.org
Thu Aug 21 09:02:47 CEST 2014


http://hg.python.org/cpython/rev/5875c50e93fe
changeset:   92174:5875c50e93fe
parent:      92170:92dcee426014
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Thu Aug 21 10:01:16 2014 +0300
summary:
  Issue #21549: Added the "members" parameter to TarFile.list().

files:
  Doc/library/tarfile.rst  |   8 ++++++--
  Lib/tarfile.py           |   9 ++++++---
  Lib/test/test_tarfile.py |  12 ++++++++++++
  Misc/NEWS                |   2 ++
  4 files changed, 26 insertions(+), 5 deletions(-)


diff --git a/Doc/library/tarfile.rst b/Doc/library/tarfile.rst
--- a/Doc/library/tarfile.rst
+++ b/Doc/library/tarfile.rst
@@ -325,11 +325,15 @@
    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()
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -1842,14 +1842,17 @@
                 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,
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -285,6 +285,18 @@
         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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -124,6 +124,8 @@
 Library
 -------
 
+- Issue #21549: Added the "members" parameter to TarFile.list().
+
 - Issue #19628: Allow compileall recursion depth to be specified with a -r
   option.
 

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list