]> granicus.if.org Git - python/commitdiff
Issue #19920: TarFile.list() no longer fails when outputs a listing
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 5 Feb 2014 18:54:43 +0000 (20:54 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 5 Feb 2014 18:54:43 +0000 (20:54 +0200)
containing non-encodable characters.  Added tests for TarFile.list().
Based on patch by Vajrasky Kok.

1  2 
Lib/tarfile.py
Lib/test/test_tarfile.py
Misc/NEWS

diff --cc Lib/tarfile.py
Simple merge
index b53f3ac39963def1cb42f718ba6535451da768ea,ad6f320894f1b7a080fea01ebb44bb10c40d78d1..ab88be41b725d449c9a7d8684cc06e68db70979b
@@@ -219,6 -217,84 +219,84 @@@ class LzmaUstarReadTest(LzmaTest, Ustar
      pass
  
  
 -        self.assertRegex(out, (br'-rw-r--r-- tarfile/tarfile\s+7011 '
+ class ListTest(ReadTest, unittest.TestCase):
+     # Override setUp to use default encoding (UTF-8)
+     def setUp(self):
+         self.tar = tarfile.open(self.tarname, mode=self.mode)
+     def test_list(self):
+         tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
+         with support.swap_attr(sys, 'stdout', tio):
+             self.tar.list(verbose=False)
+         out = tio.detach().getvalue()
+         self.assertIn(b'ustar/conttype', out)
+         self.assertIn(b'ustar/regtype', out)
+         self.assertIn(b'ustar/lnktype', out)
+         self.assertIn(b'ustar' + (b'/12345' * 40) + b'67/longname', out)
+         self.assertIn(b'./ustar/linktest2/symtype', out)
+         self.assertIn(b'./ustar/linktest2/lnktype', out)
+         # Make sure it puts trailing slash for directory
+         self.assertIn(b'ustar/dirtype/', out)
+         self.assertIn(b'ustar/dirtype-with-size/', out)
+         # Make sure it is able to print unencodable characters
+         self.assertIn(br'ustar/umlauts-'
+                       br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out)
+         self.assertIn(br'misc/regtype-hpux-signed-chksum-'
+                       br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out)
+         self.assertIn(br'misc/regtype-old-v7-signed-chksum-'
+                       br'\udcc4\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out)
+         self.assertIn(br'pax/bad-pax-\udce4\udcf6\udcfc', out)
+         self.assertIn(br'pax/hdrcharset-\udce4\udcf6\udcfc', out)
+         # Make sure it prints files separated by one newline without any
+         # 'ls -l'-like accessories if verbose flag is not being used
+         # ...
+         # ustar/conttype
+         # ustar/regtype
+         # ...
+         self.assertRegex(out, br'ustar/conttype ?\r?\n'
+                               br'ustar/regtype ?\r?\n')
+         # Make sure it does not print the source of link without verbose flag
+         self.assertNotIn(b'link to', out)
+         self.assertNotIn(b'->', out)
+     def test_list_verbose(self):
+         tio = io.TextIOWrapper(io.BytesIO(), 'ascii', newline='\n')
+         with support.swap_attr(sys, 'stdout', tio):
+             self.tar.list(verbose=True)
+         out = tio.detach().getvalue()
+         # Make sure it prints files separated by one newline with 'ls -l'-like
+         # accessories if verbose flag is being used
+         # ...
+         # ?rw-r--r-- tarfile/tarfile     7011 2003-01-06 07:19:43 ustar/conttype
+         # ?rw-r--r-- tarfile/tarfile     7011 2003-01-06 07:19:43 ustar/regtype
+         # ...
++        self.assertRegex(out, (br'\?rw-r--r-- tarfile/tarfile\s+7011 '
+                                br'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d '
+                                br'ustar/\w+type ?\r?\n') * 2)
+         # Make sure it prints the source of link with verbose flag
+         self.assertIn(b'ustar/symtype -> regtype', out)
+         self.assertIn(b'./ustar/linktest2/symtype -> ../linktest1/regtype', out)
+         self.assertIn(b'./ustar/linktest2/lnktype link to '
+                       b'./ustar/linktest1/regtype', out)
+         self.assertIn(b'gnu' + (b'/123' * 125) + b'/longlink link to gnu' +
+                       (b'/123' * 125) + b'/longname', out)
+         self.assertIn(b'pax' + (b'/123' * 125) + b'/longlink link to pax' +
+                       (b'/123' * 125) + b'/longname', out)
+ class GzipListTest(GzipTest, ListTest):
+     pass
+ class Bz2ListTest(Bz2Test, ListTest):
+     pass
+ class LzmaListTest(LzmaTest, ListTest):
+     pass
  class CommonReadTest(ReadTest):
  
      def test_empty_tarfile(self):
@@@ -1764,168 -1845,6 +1842,171 @@@ class MiscTest(unittest.TestCase)
              tarfile.itn(0x10000000000, 6, tarfile.GNU_FORMAT)
  
  
-     def tarfilecmd(self, *args):
-         rc, out, err = script_helper.assert_python_ok('-m', 'tarfile', *args)
 +class CommandLineTest(unittest.TestCase):
 +
-         self.make_simple_tarfile(tmpname)
-         with support.captured_stdout() as t:
-             with tarfile.open(tmpname, 'r') as tf:
-                 tf.list(verbose=False)
-         expected = t.getvalue().encode(sys.getfilesystemencoding())
-         for opt in '-l', '--list':
-             out = self.tarfilecmd(opt, tmpname)
-             self.assertEqual(out, expected)
++    def tarfilecmd(self, *args, **kwargs):
++        rc, out, err = script_helper.assert_python_ok('-m', 'tarfile', *args,
++                                                      **kwargs)
 +        return out.replace(os.linesep.encode(), b'\n')
 +
 +    def tarfilecmd_failure(self, *args):
 +        return script_helper.assert_python_failure('-m', 'tarfile', *args)
 +
 +    def make_simple_tarfile(self, tar_name):
 +        files = [support.findfile('tokenize_tests.txt'),
 +                 support.findfile('tokenize_tests-no-coding-cookie-'
 +                                  'and-utf8-bom-sig-only.txt')]
 +        self.addCleanup(support.unlink, tar_name)
 +        with tarfile.open(tar_name, 'w') as tf:
 +            for tardata in files:
 +                tf.add(tardata, arcname=os.path.basename(tardata))
 +
 +    def test_test_command(self):
 +        for tar_name in testtarnames:
 +            for opt in '-t', '--test':
 +                out = self.tarfilecmd(opt, tar_name)
 +                self.assertEqual(out, b'')
 +
 +    def test_test_command_verbose(self):
 +        for tar_name in testtarnames:
 +            for opt in '-v', '--verbose':
 +                out = self.tarfilecmd(opt, '-t', tar_name)
 +                self.assertIn(b'is a tar archive.\n', out)
 +
 +    def test_test_command_invalid_file(self):
 +        zipname = support.findfile('zipdir.zip')
 +        rc, out, err = self.tarfilecmd_failure('-t', zipname)
 +        self.assertIn(b' is not a tar archive.', err)
 +        self.assertEqual(out, b'')
 +        self.assertEqual(rc, 1)
 +
 +        for tar_name in testtarnames:
 +            with self.subTest(tar_name=tar_name):
 +                with open(tar_name, 'rb') as f:
 +                    data = f.read()
 +                try:
 +                    with open(tmpname, 'wb') as f:
 +                        f.write(data[:511])
 +                    rc, out, err = self.tarfilecmd_failure('-t', tmpname)
 +                    self.assertEqual(out, b'')
 +                    self.assertEqual(rc, 1)
 +                finally:
 +                    support.unlink(tmpname)
 +
 +    def test_list_command(self):
-         self.make_simple_tarfile(tmpname)
-         with support.captured_stdout() as t:
-             with tarfile.open(tmpname, 'r') as tf:
-                 tf.list(verbose=True)
-         expected = t.getvalue().encode(sys.getfilesystemencoding())
-         for opt in '-v', '--verbose':
-             out = self.tarfilecmd(opt, '-l', tmpname)
-             self.assertEqual(out, expected)
++        for tar_name in testtarnames:
++            with support.captured_stdout() as t:
++                with tarfile.open(tar_name, 'r') as tf:
++                    tf.list(verbose=False)
++            expected = t.getvalue().encode('ascii', 'backslashreplace')
++            for opt in '-l', '--list':
++                out = self.tarfilecmd(opt, tar_name,
++                                      PYTHONIOENCODING='ascii')
++                self.assertEqual(out, expected)
 +
 +    def test_list_command_verbose(self):
++        for tar_name in testtarnames:
++            with support.captured_stdout() as t:
++                with tarfile.open(tar_name, 'r') as tf:
++                    tf.list(verbose=True)
++            expected = t.getvalue().encode('ascii', 'backslashreplace')
++            for opt in '-v', '--verbose':
++                out = self.tarfilecmd(opt, '-l', tar_name,
++                                      PYTHONIOENCODING='ascii')
++                self.assertEqual(out, expected)
 +
 +    def test_list_command_invalid_file(self):
 +        zipname = support.findfile('zipdir.zip')
 +        rc, out, err = self.tarfilecmd_failure('-l', zipname)
 +        self.assertIn(b' is not a tar archive.', err)
 +        self.assertEqual(out, b'')
 +        self.assertEqual(rc, 1)
 +
 +    def test_create_command(self):
 +        files = [support.findfile('tokenize_tests.txt'),
 +                 support.findfile('tokenize_tests-no-coding-cookie-'
 +                                  'and-utf8-bom-sig-only.txt')]
 +        for opt in '-c', '--create':
 +            try:
 +                out = self.tarfilecmd(opt, tmpname, *files)
 +                self.assertEqual(out, b'')
 +                with tarfile.open(tmpname) as tar:
 +                    tar.getmembers()
 +            finally:
 +                support.unlink(tmpname)
 +
 +    def test_create_command_verbose(self):
 +        files = [support.findfile('tokenize_tests.txt'),
 +                 support.findfile('tokenize_tests-no-coding-cookie-'
 +                                  'and-utf8-bom-sig-only.txt')]
 +        for opt in '-v', '--verbose':
 +            try:
 +                out = self.tarfilecmd(opt, '-c', tmpname, *files)
 +                self.assertIn(b' file created.', out)
 +                with tarfile.open(tmpname) as tar:
 +                    tar.getmembers()
 +            finally:
 +                support.unlink(tmpname)
 +
 +    def test_create_command_dotless_filename(self):
 +        files = [support.findfile('tokenize_tests.txt')]
 +        try:
 +            out = self.tarfilecmd('-c', dotlessname, *files)
 +            self.assertEqual(out, b'')
 +            with tarfile.open(dotlessname) as tar:
 +                tar.getmembers()
 +        finally:
 +            support.unlink(dotlessname)
 +
 +    def test_create_command_dot_started_filename(self):
 +        tar_name = os.path.join(TEMPDIR, ".testtar")
 +        files = [support.findfile('tokenize_tests.txt')]
 +        try:
 +            out = self.tarfilecmd('-c', tar_name, *files)
 +            self.assertEqual(out, b'')
 +            with tarfile.open(tar_name) as tar:
 +                tar.getmembers()
 +        finally:
 +            support.unlink(tar_name)
 +
 +    def test_extract_command(self):
 +        self.make_simple_tarfile(tmpname)
 +        for opt in '-e', '--extract':
 +            try:
 +                with support.temp_cwd(tarextdir):
 +                    out = self.tarfilecmd(opt, tmpname)
 +                self.assertEqual(out, b'')
 +            finally:
 +                support.rmtree(tarextdir)
 +
 +    def test_extract_command_verbose(self):
 +        self.make_simple_tarfile(tmpname)
 +        for opt in '-v', '--verbose':
 +            try:
 +                with support.temp_cwd(tarextdir):
 +                    out = self.tarfilecmd(opt, '-e', tmpname)
 +                self.assertIn(b' file is extracted.', out)
 +            finally:
 +                support.rmtree(tarextdir)
 +
 +    def test_extract_command_different_directory(self):
 +        self.make_simple_tarfile(tmpname)
 +        try:
 +            with support.temp_cwd(tarextdir):
 +                out = self.tarfilecmd('-e', tmpname, 'spamdir')
 +            self.assertEqual(out, b'')
 +        finally:
 +            support.rmtree(tarextdir)
 +
 +    def test_extract_command_invalid_file(self):
 +        zipname = support.findfile('zipdir.zip')
 +        with support.temp_cwd(tarextdir):
 +            rc, out, err = self.tarfilecmd_failure('-e', zipname)
 +        self.assertIn(b' is not a tar archive.', err)
 +        self.assertEqual(out, b'')
 +        self.assertEqual(rc, 1)
 +
 +
  class ContextManagerTest(unittest.TestCase):
  
      def test_basic(self):
diff --cc Misc/NEWS
index 90da3a73f4e3f947340d91c22f8d778edcd6eb5a,59bc95d2695d74f6b84254da7f000e80ac629091..99214b74eaecd0e516a72d170ec6726b267da6e4
+++ b/Misc/NEWS
@@@ -98,110 -80,6 +101,112 @@@ Librar
    codecs.StreamReader returned incomplete data when were called after
    readline() or read(size).  Based on patch by Amaury Forgeot d'Arc.
  
 +- Issue #20105: the codec exception chaining now correctly sets the
 +  traceback of the original exception as its __traceback__ attribute.
 +
 +- Issue #17481: inspect.getfullargspec() now uses inspect.signature() API.
 +
 +- Issue #15304: concurrent.futures.wait() can block forever even if
 +  Futures have completed. Patch by Glenn Langford.
 +
 +IDLE
 +----
 +
 +- Update the python.gif icon for the Idle classbrowser and pathbowser
 +  from the old green snake to the new new blue and yellow snakes.
 +
 +- Issue #17721: Remove non-functional configuration dialog help button until we
 +  make it actually gives some help when clicked. Patch by Guilherme Simões.
 +
 +Tests
 +-----
 +
++- Issue #19920: Added tests for TarFile.list().  Based on patch by Vajrasky Kok.
++
 +- Issue #19990: Added tests for the imghdr module.  Based on patch by
 +  Claudiu Popa.
 +
 +- Issue #20474: Fix test_socket "unexpected success" failures on OS X 10.7+.
 +
 +Tools/Demos
 +-----------
 +
 +- #Issue 20456: Argument Clinic now observes the C preprocessor conditional
 +  compilation statements of the C files it parses.  When a Clinic block is
 +  inside a conditional code, it adjusts its output to match, including
 +  automatically generating an empty methoddef macro.
 +
 +- #Issue 20456: Cloned functions in Argument Clinic now use the correct
 +  name, not the name of the function they were cloned from, for text
 +  strings inside generated code.
 +
 +- #Issue 20456: Fixed Argument Clinic's test suite and "--converters" feature.
 +
 +- #Issue 20456: Argument Clinic now allows specifying different names
 +  for a parameter in Python and C, using "as" on the parameter line.
 +
 +- Issue #20326: Argument Clinic now uses a simple, unique signature to
 +  annotate text signatures in docstrings, resulting in fewer false
 +  positives.  "self" parameters are also explicitly marked, allowing
 +  inspect.Signature() to authoritatively detect (and skip) said parameters.
 +
 +- Issue #20326: Argument Clinic now generates separate checksums for the
 +  input and output sections of the block, allowing external tools to verify
 +  that the input has not changed (and thus the output is not out-of-date).
 +
 +Build
 +-----
 +
 +- Issue #20465: Update SQLite shipped with OS X installer to 3.8.3.
 +
 +
 +What's New in Python 3.4.0 Beta 3?
 +==================================
 +
 +Release date: 2014-01-26
 +
 +Core and Builtins
 +-----------------
 +
 +- Issue #20189: Four additional builtin types (PyTypeObject,
 +  PyMethodDescr_Type, _PyMethodWrapper_Type, and PyWrapperDescr_Type)
 +  have been modified to provide introspection information for builtins.
 +
 +- Issue #17825: Cursor "^" is correctly positioned for SyntaxError and
 +  IndentationError.
 +
 +- Issue #2382: SyntaxError cursor "^" is now written at correct position in most
 +  cases when multibyte characters are in line (before "^").  This still not
 +  works correctly with wide East Asian characters.
 +
 +- Issue #18960: The first line of Python script could be executed twice when
 +  the source encoding was specified on the second line.  Now the source encoding
 +  declaration on the second line isn't effective if the first line contains
 +  anything except a comment.  'python -x' works now again with files with the
 +  source encoding declarations, and can be used to make Python batch files
 +  on Windows.
 +
 +Library
 +-------
 +
 +- asyncio: Various improvements and small changes not all covered by
 +  issues listed below.  E.g. wait_for() now cancels the inner task if
 +  the timeout occcurs; tweaked the set of exported symbols; renamed
 +  Empty/Full to QueueEmpty/QueueFull; "with (yield from lock)" now
 +  uses a separate context manager; readexactly() raises if not enough
 +  data was read; PTY support tweaks.
 +
 +- Issue #20311: asyncio: Add a granularity attribute to BaseEventLoop: maximum
 +  between the resolution of the BaseEventLoop.time() method and the resolution
 +  of the selector. The granuarility is used in the scheduler to round time and
 +  deadline.
 +
 +- Issue #20311: selectors: Add a resolution attribute to BaseSelector.
 +
 +- Issue #20189: unittest.mock now no longer assumes that any object for
 +  which it could get an inspect.Signature is a callable written in Python.
 +  Fix courtesy of Michael Foord.
 +
  - Issue #20317: ExitStack.__exit__ could create a self-referential loop if an
    exception raised by a cleanup operation already had its context set
    correctly (for example, by the @contextmanager decorator). The infinite