[docs] [issue10344] codecs.readline doesn't care buffering=0
Terry J. Reedy
report at bugs.python.org
Fri Jan 13 10:08:09 CET 2012
Terry J. Reedy <tjreedy at udel.edu> added the comment:
Something seems wrong somewhere. First,
codecs.open(filename, mode[, encoding[, errors[, buffering]]])
in the doc, should be, to match the code, in the current sytle
codecs.open(filename, mode='rb', encoding=None, errors='strict', buffering=1)
The other entries below follow this style.
The Note says "Files are always opened in binary mode, even if no binary mode was specified.". However, the code is
if encoding is not None and \
'b' not in mode:
# Force opening of the file in binary mode
mode = mode + 'b'
so the forcing only happens when an encoding is given. Since the intent is that codecs.open == open when no encoding is given, I believe the Note should be revised rather than the code.
(buffering=1) means line buffered. However, the doc for builtin open() says about buffering "1 to select line buffering (only usable in text mode)" So the default buffering is one that is not usable in the normal forced binary mode. Marc-Andre, can you explain this? (The doc for open() does not specify what happens when the buffering conflicts with the mode.)
The doc for StreamReader.readline() says ""size, if given, is passed as size argument to the stream’s readline() method.". If that were true, size would the max bytes to read. However, the docstring for the same in codecs.py says "size, if given, is passed as size argument to the read() method.", and that is what the code does. If not given, 72 is used as the default. (Why not 80?)
So, while the doc needs a minor tweak, I do not see what the OP's posted original result has to do with buffering. .readline does not have a fixed internal buffer of 72 chars that I can see. Rather, that is the default number of chars to read. So that is what it read, given that the file is longer than that.
I believe this is what Marc-Andre said, in different words, in his first post, in between the distraction of whether to remove open.
Santiago, yes, there is a difference between open.readline and codecs.readline. It will be more obvious when the codecs.readline size doc is corrected to specify that it is passed to read(), not readline(), and that it defaults to 72.
versions: +Python 3.3 -Python 3.1
Python tracker <report at bugs.python.org>
More information about the docs