Index: Doc/library/tarfile.rst =================================================================== --- Doc/library/tarfile.rst (révision 80008) +++ Doc/library/tarfile.rst (copie de travail) @@ -218,7 +218,7 @@ .. versionadded:: 3.2 Added support for the context manager protocol. -.. class:: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors=None, pax_headers=None, debug=0, errorlevel=0) +.. class:: TarFile(name=None, mode='r', fileobj=None, format=DEFAULT_FORMAT, tarinfo=TarInfo, dereference=False, ignore_zeros=False, encoding=ENCODING, errors='surrogateescape', pax_headers=None, debug=0, errorlevel=0) All following arguments are optional and can be accessed as instance attributes as well. @@ -264,8 +264,7 @@ The *encoding* and *errors* arguments define the character encoding to be used for reading or writing the archive and how conversion errors are going - to be handled. The default settings will work for most users. - See section :ref:`tar-unicode` for in-depth information. + to be handled. See section :ref:`tar-unicode` for in-depth information. The *pax_headers* argument is an optional dictionary of strings which will be added as a pax global header if *format* is :const:`PAX_FORMAT`. @@ -449,7 +448,7 @@ a :class:`TarInfo` object. -.. method:: TarInfo.tobuf(format=DEFAULT_FORMAT, encoding=ENCODING, errors='strict') +.. method:: TarInfo.tobuf(format=DEFAULT_FORMAT, encoding=ENCODING, errors='surrogateescape') Create a string buffer from a :class:`TarInfo` object. For information on the arguments see the constructor of the :class:`TarFile` class. @@ -701,11 +700,9 @@ appropriately, this conversion may fail. The *errors* argument defines how characters are treated that cannot be -converted. Possible values are listed in section :ref:`codec-base-classes`. In -read mode the default scheme is ``'replace'``. This avoids unexpected -:exc:`UnicodeError` exceptions and guarantees that an archive can always be -read. In write mode the default value for *errors* is ``'strict'``. This -ensures that name information is not altered unnoticed. +converted. Possible values are listed in section :ref:`codec-base-classes`. The +default scheme is ``'surrogateescape'``: undecodable bytes are stored as +unicode surrogates, use the same scheme to encode the value to bytes. In case of writing :const:`PAX_FORMAT` archives, *encoding* is ignored because non-ASCII metadata is stored using *UTF-8*. Index: Lib/tarfile.py =================================================================== --- Lib/tarfile.py (révision 80008) +++ Lib/tarfile.py (copie de travail) @@ -985,7 +985,7 @@ return info - def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="strict"): + def tobuf(self, format=DEFAULT_FORMAT, encoding=ENCODING, errors="surrogateescape"): """Return a tar header as a string of 512 byte blocks. """ info = self.get_info() @@ -1497,7 +1497,7 @@ def __init__(self, name=None, mode="r", fileobj=None, format=None, tarinfo=None, dereference=None, ignore_zeros=None, encoding=None, - errors=None, pax_headers=None, debug=None, errorlevel=None): + errors="surrogateescape", pax_headers=None, debug=None, errorlevel=None): """Open an (uncompressed) tar archive `name'. `mode' is either 'r' to read from an existing archive, 'a' to append data to an existing file or 'w' to create a new file overwriting an existing one. `mode' @@ -1538,14 +1538,8 @@ self.ignore_zeros = ignore_zeros if encoding is not None: self.encoding = encoding + self.errors = errors - if errors is not None: - self.errors = errors - elif mode == "r": - self.errors = "replace" - else: - self.errors = "strict" - if pax_headers is not None and self.format == PAX_FORMAT: self.pax_headers = pax_headers else: @@ -1997,7 +1991,7 @@ tarinfo = copy.copy(tarinfo) - buf = tarinfo.tobuf(self.format, self.encoding, self.errors) + buf = tarinfo.tobuf(self.format, self.encoding, "surrogateescape") self.fileobj.write(buf) self.offset += len(buf) Index: Lib/test/test_tarfile.py =================================================================== --- Lib/test/test_tarfile.py (révision 80008) +++ Lib/test/test_tarfile.py (copie de travail) @@ -1100,8 +1100,8 @@ if self.format != tarfile.PAX_FORMAT: tar = tarfile.open(tmpname, encoding="ascii") t = tar.getmember("foo") - self.assertEqual(t.uname, "\ufffd\ufffd\ufffd") - self.assertEqual(t.gname, "\ufffd\ufffd\ufffd") + self.assertEqual(t.uname, "\udce4\udcf6\udcfc") + self.assertEqual(t.gname, "\udce4\udcf6\udcfc") class GNUUnicodeTest(UstarUnicodeTest):