This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Impreciseness in bz2 module documentation?
Type: Stage: resolved
Components: Documentation Versions: Python 3.1, Python 3.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ezio.melotti Nosy List: Haudegen, ezio.melotti
Priority: normal Keywords:

Created on 2010-03-04 16:46 by Haudegen, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg100397 - (view) Author: Andreas Poisel (Haudegen) Date: 2010-03-04 16:46
A string in Python 3 is a sequence of unicode characters, right?  The
documentation of the bz2 module says:

8<------------------------------------------------------------------
class bz2.BZ2File(filename, mode='r', buffering=0, compresslevel=9)
[...]
write(data)
    Write string data to file. Note that due to buffering, close() may be
    needed before the file on disk reflects the data written.
[...]
8<------------------------------------------------------------------

So the documentation wants me to pass a "string data" to the write() method:

8<------------------------------------------------------------------
>>> import bz2
>>> with bz2.BZ2File('test.bz2', mode='w') as cfh:
...     cfh.write('Test')
...
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: must be bytes or buffer, not str
8<------------------------------------------------------------------

So what write() really wants is a byte array or an encoded string:

8<------------------------------------------------------------------
>>> with bz2.BZ2File('test.bz2', mode='w') as cfh:
...     cfh.write(bytes('Test', encoding='iso-8859-1'))
... 
>>> with bz2.BZ2File('test.bz2', mode='w') as cfh:
...     cfh.write('Test'.encode('iso-8859-1'))
... 
8<------------------------------------------------------------------

Is this an inaccuracy of the documentation or did I get something wrong?
msg100979 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-03-12 22:49
Fixed in r78884 (py3k) and r78885 (release31-maint), thanks for the report!
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52305
2010-03-12 22:49:30ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg100979

stage: needs patch -> resolved
2010-03-04 16:51:38ezio.melottisetnosy: + ezio.melotti
versions: + Python 3.2
priority: normal
assignee: ezio.melotti
components: + Documentation
stage: needs patch
2010-03-04 16:46:32Haudegencreate