diff -r 5f5e1d408c0c Lib/tarfile.py --- a/Lib/tarfile.py Wed Dec 18 15:37:03 2013 -0600 +++ b/Lib/tarfile.py Thu Dec 19 11:37:33 2013 +0800 @@ -1843,7 +1843,12 @@ print("%d-%02d-%02d %02d:%02d:%02d" \ % time.localtime(tarinfo.mtime)[:6], end=' ') - print(tarinfo.name + ("/" if tarinfo.isdir() else ""), end=' ') + tarinfo_dir = "/" if tarinfo.isdir() else "" + try: + print(tarinfo.name + tarinfo_dir, end=' ') + except UnicodeEncodeError: + print(tarinfo.name.encode(errors='replace').decode() + + tarinfo_dir, end=' ') if verbose: if tarinfo.issym(): diff -r 5f5e1d408c0c Lib/test/test_tarfile.py --- a/Lib/test/test_tarfile.py Wed Dec 18 15:37:03 2013 -0600 +++ b/Lib/test/test_tarfile.py Thu Dec 19 11:37:33 2013 +0800 @@ -1786,6 +1786,17 @@ out = self.tarfilecmd(opt, tmpname) self.assertEqual(out, expected) + def test_list_print_unencodable_characters(self): + arguments = [support.findfile('tarfile.py'), + '-l', + support.findfile('testtar.tar')] + rc, out, err = script_helper.assert_python_ok(*arguments) + self.assertIn(b'ustar/umlauts-???????', out) + self.assertIn(b'misc/regtype-hpux-signed-chksum-???????', out) + self.assertIn(b'misc/regtype-old-v7-signed-chksum-???????', out) + self.assertIn(b'pax/bad-pax-???', out) + self.assertIn(b'pax/hdrcharset-???', out) + def test_list_command_verbose(self): self.make_simple_tarfile(tmpname) with support.captured_stdout() as t: