You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[doc] say in open() doc that line buffering only applies to write #59254
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee='https://github.com/bitdancer'closed_at=Nonecreated_at=<Date2012-06-12.01:15:24.022>labels= ['type-bug', '3.8', '3.9', '3.10', 'docs']
title='[doc] say in open() doc that line buffering only applies to write'updated_at=<Date2020-11-06.17:20:11.298>user='https://github.com/bitdancer'
This line is just ascii
A second line for good measure.
This comment contains undecodable stuff: "�" or "\\xe9" in "pass�"" cannot be decoded.
The last line above is in latin-1, with an é inside those quotes.
rdmurray@hey:~/python/p32>cat bug.py
import sys
withopen('./bad.py', buffering=int(sys.argv[1])) as f:
for line in f:
print(line, end='')
rdmurray@hey:~/python/p32>python3 bug.py -1
Traceback (most recent call last):
File "bug.py", line 3, in<module>for line in f:
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) =self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 99: invalid continuation byte
rdmurray@hey:~/python/p32>python3 bug.py 1
Traceback (most recent call last):
File "bug.py", line 3, in<module>for line in f:
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) =self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 99:
invalid continuation byte
rdmurray@hey:~/python/p32>python3 bug.py 2
This line is just ascii
A second line for good measure.
Traceback (most recent call last):
File "bug.py", line 3, in<module>for line in f:
File "/usr/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) =self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe9 in position 0: invalid
continuation byte
So, line buffering does not appear to buffer line by line.
I ran into this problem because I had a much larger file that I thought
was in utf-8. When I got the encoding error, I was annoyed that the
error message didn't really tell me which line the error was on, but I
figured, OK, I'll just set line buffering and then I'll be able to tell.
But that didn't work. Fortunately using '2' did work....but at a minimum
the docs need to be updated to indicate when line buffering really is
line buffering and when it isn't.
Indeed, line buffering on the read size would very slow (since you would have to read and decode one byte at a time from the raw stream to make sure you don't overshoot the line boundaries).
Don't know if the issue can be replicated, or if it's in a different form:
$ printf '
first line
second line
some buffer text \xFF some trailing text' >> text
$ cat bug.py
import sys
with open('text', encoding='ascii', buffering=int(sys.argv[1])) as f:
for line in f:
print(line, end='')
but when given -1,1,2, they all return UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 41: ordinal not in range(128) with no printing of the ascii lines.
Activity
bitdancer commentedon Jun 12, 2012
rdmurray@hey:~/python/p32>cat bad.py
The last line above is in latin-1, with an é inside those quotes.
So, line buffering does not appear to buffer line by line.
I ran into this problem because I had a much larger file that I thought
was in utf-8. When I got the encoding error, I was annoyed that the
error message didn't really tell me which line the error was on, but I
figured, OK, I'll just set line buffering and then I'll be able to tell.
But that didn't work. Fortunately using '2' did work....but at a minimum
the docs need to be updated to indicate when line buffering really is
line buffering and when it isn't.
loewis commentedon Jun 12, 2012
Without looking at the code, it seems that
http://docs.python.org/release/3.1.5/library/io.html?highlight=io#io.TextIOWrapper
gives the answer
"If line_buffering is True, flush() is implied when a call to write contains a newline character."
So, "line buffering" may have a meaning only for writing. I don't think there is a reasonable way to implement it for reading.
bitdancer commentedon Jun 12, 2012
That makes sense. I'll add a mention of this to the 'open' docs that discuss the buffering parameter.
pitrou commentedon Jun 13, 2012
Indeed, line buffering on the read size would very slow (since you would have to read and decode one byte at a time from the raw stream to make sure you don't overshoot the line boundaries).
[-]line buffering isn't always[/-][+][doc] say in open() doc that line buffering only applies to write[/+]slateny commentedon Jun 1, 2022
Don't know if the issue can be replicated, or if it's in a different form:
but when given
-1,1,2, they all returnUnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 41: ordinal not in range(128)with no printing of the ascii lines.pythongh-59254: mention in open() doc that line buffering is for writing
gh-59254: mention in open() doc that line buffering is for writing (#…
pythongh-59254: mention in open() doc that line buffering is for writ…
pythongh-59254: mention in open() doc that line buffering is for writ…
[3.11] gh-59254: mention in open() doc that line buffering is for wri…
[3.12] gh-59254: mention in open() doc that line buffering is for wri…
pythongh-59254: mention in open() doc that line buffering is for writ…
pythongh-59254: mention in open() doc that line buffering is for writ…