[New-bugs-announce] [issue8057] Impreciseness in bz2 module documentation?
Andreas Poisel
report at bugs.python.org
Thu Mar 4 17:46:33 CET 2010
New submission from Andreas Poisel <ap at acat.cc>:
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?
----------
messages: 100397
nosy: Haudegen
severity: normal
status: open
title: Impreciseness in bz2 module documentation?
versions: Python 3.1
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue8057>
_______________________________________
More information about the New-bugs-announce
mailing list