summaryrefslogtreecommitdiff
path: root/Lib/tarfile.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-02-05 20:54:43 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2014-02-05 20:54:43 +0200
commit255493c81321f56afd32fb2e18d5eb3a13bed42f (patch)
treea928f22b921ffcf250451e3b271407ff1cca6adb /Lib/tarfile.py
parent4d5d69d452d37bfc29d8f182cb4b99a344f0dadb (diff)
parent3b4f1594ffb360e9a93841a4c8dfe00d6f519bf6 (diff)
downloadcpython-git-255493c81321f56afd32fb2e18d5eb3a13bed42f.tar.gz
Issue #19920: TarFile.list() no longer fails when outputs a listing
containing non-encodable characters. Added tests for TarFile.list(). Based on patch by Vajrasky Kok.
Diffstat (limited to 'Lib/tarfile.py')
-rwxr-xr-xLib/tarfile.py28
1 files changed, 17 insertions, 11 deletions
diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 79566e7e97..3409efea03 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -257,6 +257,12 @@ def filemode(mode):
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 @@ class TarFile(object):
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):