-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Make codecs.StreamReader.read() more compatible with read() of other files #76291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Usually the read() method of a file-like object takes one optional argument which limits the amount of data (the number of bytes or characters) returned if specified. codecs.StreamReader.read() also has such parameter. But this is the second parameter. The first parameter limits the number of bytes read for decoding. read(1) can return 70 characters, that will confuse most callers which expect either a single character or an empty string (at the end of stream). Some times ago codecs.open() was recommended as a replacement for the builtin open() in programs that should work in 2.x and 3.x (this was before adding io.open()), and it is still used in many programs. But this peculiarity makes it bad replacement of builtin open(). I wanted to fix this issue long time ago, but forgot, and the question on Stack Overflow has reminded me about this. https://stackoverflow.com/questions/46437761/codecs-openutf-8-fails-to-read-plain-ascii-file |
On 22.11.2017 08:40, Serhiy Storchaka wrote:
That's not true. .read(1) will at most read 1 byte from the stream The reasoning here is the same as for .read() on regular byte The optional second parameter chars was added as convenience, That said, I see in your patch that you want to bind chars |
See the added tests. They are failed without changing the read() method. .read(1) currently returns all characters from the characters buffer. And this buffer can be not empty after .readline(). I understand the reason of having two limitation parameters in StreamReader.read(). But currently its behavior does not completely match the expected behavior of the read() method with one argument. Actually size already has been used instead of chars if chars < 0 for reading in a loop. The code can be simplified. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: