Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af009fb

Browse files
bpo-32035: Fix words about strings and bytes in zipfile documentation. (GH-10592)
(cherry picked from commit 4bb186d) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 8c70c08 commit af009fb

2 files changed

Lines changed: 12 additions & 16 deletions

File tree

Doc/library/zipfile.rst

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,6 @@ ZipFile Objects
360360
the new entry.
361361
The archive must be open with mode ``'w'``, ``'x'`` or ``'a'``.
362362

363-
.. note::
364-
365-
There is no official file name encoding for ZIP files. If you have unicode file
366-
names, you must convert them to byte strings in your desired encoding before
367-
passing them to :meth:`write`. WinZip interprets all file names as encoded in
368-
CP437, also known as DOS Latin.
369-
370363
.. note::
371364

372365
Archive names should be relative to the archive root, that is, they should not
@@ -385,7 +378,9 @@ ZipFile Objects
385378

386379
.. method:: ZipFile.writestr(zinfo_or_arcname, data[, compress_type])
387380

388-
Write the string *data* to the archive; *zinfo_or_arcname* is either the file
381+
Write a file into the archive. The contents is *data*, which may be either
382+
a :class:`str` or a :class:`bytes` instance; if it is a :class:`str`,
383+
it is encoded as UTF-8 first. *zinfo_or_arcname* is either the file
389384
name it will be given in the archive, or a :class:`ZipInfo` instance. If it's
390385
an instance, at least the filename, date, and time must be given. If it's a
391386
name, the date and time is set to the current date and time.
@@ -425,11 +420,11 @@ The following data attributes are also available:
425420

426421
.. attribute:: ZipFile.comment
427422

428-
The comment text associated with the ZIP file. If assigning a comment to a
423+
The comment associated with the ZIP file as a :class:`bytes` object.
424+
If assigning a comment to a
429425
:class:`ZipFile` instance created with mode ``'w'``, ``'x'`` or ``'a'``,
430-
this should be a
431-
string no longer than 65535 bytes. Comments longer than this will be
432-
truncated in the written archive when :meth:`close` is called.
426+
it should be no longer than 65535 bytes. Comments longer than this will be
427+
truncated.
433428

434429

435430
.. _pyzipfile-objects:
@@ -583,13 +578,14 @@ Instances have the following methods and attributes:
583578

584579
.. attribute:: ZipInfo.comment
585580

586-
Comment for the individual archive member.
581+
Comment for the individual archive member as a :class:`bytes` object.
587582

588583

589584
.. attribute:: ZipInfo.extra
590585

591586
Expansion field data. The `PKZIP Application Note`_ contains
592-
some comments on the internal structure of the data contained in this string.
587+
some comments on the internal structure of the data contained in this
588+
:class:`bytes` object.
593589

594590

595591
.. attribute:: ZipInfo.create_system

Lib/zipfile.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ def __repr__(self):
405405
return ''.join(result)
406406

407407
def FileHeader(self, zip64=None):
408-
"""Return the per-file header as a string."""
408+
"""Return the per-file header as a bytes object."""
409409
dt = self.date_time
410410
dosdate = (dt[0] - 1980) << 9 | dt[1] << 5 | dt[2]
411411
dostime = dt[3] << 11 | dt[4] << 5 | (dt[5] // 2)
@@ -1333,7 +1333,7 @@ def comment(self, comment):
13331333
self._didModify = True
13341334

13351335
def read(self, name, pwd=None):
1336-
"""Return file bytes (as a string) for name."""
1336+
"""Return file bytes for name."""
13371337
with self.open(name, "r", pwd) as fp:
13381338
return fp.read()
13391339

0 commit comments

Comments
 (0)