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: document whether io.TextIOBase.readline(size>0) will always read the full newline
Type: Stage:
Components: Documentation Versions:
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: andrei.avk, benjamin.peterson, calestyo, docs@python
Priority: normal Keywords:

Created on 2021-02-27 02:15 by calestyo, last changed 2022-04-11 14:59 by admin.

Messages (4)
msg387758 - (view) Author: Christoph Anton Mitterer (calestyo) Date: 2021-02-27 02:15
Hey.

It would be nice if the following behaviour could be definitely clarified:

When reading from a text stream with readline(size) with a n > 0size it says: "If size is specified, at most size characters will be read."

Also, depending on the settings of newlines of the stream, \r\n would be converted to \n.


It's not definitely clear whether a string like
"abc\r\n"
read with a size of = 4 will return "abc\n" (which it seems to do, in other words, it reads actually 5 characters, but sill returns only 4), or whether it returns "abc\r".


Cheers,
Chris.
msg387759 - (view) Author: Christoph Anton Mitterer (calestyo) Date: 2021-02-27 02:16
"a n > 0size" should have read "a size > 0"
msg388935 - (view) Author: Christoph Anton Mitterer (calestyo) Date: 2021-03-17 16:20
I guess that the translation from CRLF to LF simply happens before the size restriction is enforced (which is good so, btw), so effectively it:

will not *read* at most size characters from the stream, but *return* at most size characters from it (with any newlines translated before, and thus more characters read from the stream than returned)
msg396887 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-07-03 03:11
I don't think it's incorrect right now, from the POV of internal data of the stream, there's 6 chars, from the POV of caller, 5 chars are read. So this can be interpreted as 2 chars are combined into 1 char read.

On the other hand if TextIOBase was reading '\r' and letting caller process it and then reading '\n....' that would be of course a recipe to shoot the user in the foot, and would require a warning in big red letters in the docs. (Or even better fixing it to do what it does now.)
History
Date User Action Args
2022-04-11 14:59:42adminsetgithub: 87502
2021-07-03 03:11:32andrei.avksetnosy: + andrei.avk
messages: + msg396887
2021-03-17 16:20:32calestyosetmessages: + msg388935
2021-03-05 21:02:21terry.reedysetnosy: + benjamin.peterson
2021-02-27 02:16:17calestyosetmessages: + msg387759
2021-02-27 02:15:37calestyocreate