diff -r 483096ef1cf6 Lib/tarfile.py --- a/Lib/tarfile.py Wed Jan 29 11:45:31 2014 +0200 +++ b/Lib/tarfile.py Wed Jan 29 13:52:52 2014 +0200 @@ -257,6 +257,12 @@ DeprecationWarning, 2) return stat.filemode(mode) +def _safe_print(s): + encoding = getattr(sys.stdout, 'encoding', None) + if encoding is not None: + s = s.encode(encoding, 'backslashreplace').decode(encoding) + print(s, end=' ') + class TarError(Exception): """Base exception.""" @@ -1846,24 +1852,24 @@ for tarinfo in self: if verbose: - print(stat.filemode(tarinfo.mode), end=' ') - print("%s/%s" % (tarinfo.uname or tarinfo.uid, - tarinfo.gname or tarinfo.gid), end=' ') + _safe_print(stat.filemode(tarinfo.mode)) + _safe_print("%s/%s" % (tarinfo.uname or tarinfo.uid, + tarinfo.gname or tarinfo.gid)) if tarinfo.ischr() or tarinfo.isblk(): - print("%10s" % ("%d,%d" \ - % (tarinfo.devmajor, tarinfo.devminor)), end=' ') + _safe_print("%10s" % + ("%d,%d" % (tarinfo.devmajor, tarinfo.devminor))) else: - print("%10d" % tarinfo.size, end=' ') - print("%d-%02d-%02d %02d:%02d:%02d" \ - % time.localtime(tarinfo.mtime)[:6], end=' ') + _safe_print("%10d" % tarinfo.size) + _safe_print("%d-%02d-%02d %02d:%02d:%02d" \ + % time.localtime(tarinfo.mtime)[:6]) - print(tarinfo.name + ("/" if tarinfo.isdir() else ""), end=' ') + _safe_print(tarinfo.name + ("/" if tarinfo.isdir() else "")) if verbose: if tarinfo.issym(): - print("->", tarinfo.linkname, end=' ') + _safe_print("-> " + tarinfo.linkname) if tarinfo.islnk(): - print("link to", tarinfo.linkname, end=' ') + _safe_print("link to " + tarinfo.linkname) print() def add(self, name, arcname=None, recursive=True, exclude=None, *, filter=None): diff -r 483096ef1cf6 Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py Wed Jan 29 11:45:31 2014 +0200 +++ b/Lib/test/test_tarfile.py Wed Jan 29 13:52:52 2014 +0200 @@ -219,6 +219,84 @@ pass +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(rb'ustar/umlauts-\udcc4\udcd6\udcdc\udce4' + rb'\udcf6\udcfc\udcdf', out) + self.assertIn(rb'misc/regtype-hpux-signed-chksum-\udcc4' + rb'\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out) + self.assertIn(rb'misc/regtype-old-v7-signed-chksum-\udcc4' + rb'\udcd6\udcdc\udce4\udcf6\udcfc\udcdf', out) + self.assertIn(rb'pax/bad-pax-\udce4\udcf6\udcfc', out) + self.assertIn(rb'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 + # ... + # gnu/sparse + # gnu/sparse-0.0 + # gnu/sparse-0.1 + # ... + self.assertRegex(out, b'gnu/sparse\s+gnu/sparse-0.0\s+gnu/sparse-0.1') + # 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, (rb'\?rw-r--r-- tarfile/tarfile\s+7011 ' + rb'\d{4}-\d\d-\d\d\s+\d\d:\d\d:\d\d ' + rb'ustar/\w+type ?\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): @@ -1766,8 +1844,9 @@ class CommandLineTest(unittest.TestCase): - def tarfilecmd(self, *args): - rc, out, err = script_helper.assert_python_ok('-m', 'tarfile', *args) + 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): @@ -1815,24 +1894,26 @@ 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=False) - expected = t.getvalue().encode(sys.getfilesystemencoding()) - for opt in '-l', '--list': - out = self.tarfilecmd(opt, 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): - 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=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')