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
TextIOWrapper Buffering Inconsistent Between _io and _pyio #52203
Comments
The following snippet behaves differently in the C IO implementation than in the Python IO implementation: import sys
sys.stdout.write('unicode ')
sys.stdout.buffer.write(b'bytes ') To test this, I have created two scripts, testpyio.py (using _pyio) and testio.py (using _io). The output is as follows: % python3 testpyio.py In my opinion, the behavior exhibited by _pyio is more correct. It appears that to get the C implementation to print the lines in the correct order, there must be a flush in between the statements. This extra flush would create a lot of overhead. I am attaching the two test scripts. The C implementation prints the output in the correct order if each write ends with a newline. |
This is by design, for performance the C TextIOWrapper stores the encoded strings in a list, and calls buffer.write() less often. |
This seems like a common need (particularly for stdout and stderr), and setting
|
I agree this deserves documentation. I'm not convinced it's a common need, though. Usually you either use stdin/stdout in binary mode or in text mode, but you don't interleave both quite frequently. |
I would imagine that this would come up in most programs that read data from a pipe or from a socket (which are binary data) and then output to stdout or stderr. I ran across the problem in my first non-trivial port to Python 3, and it seems like a common case to me. But having the weird behavior documented is the most important thing. |
This issue was opened and last time updated in February 2010. The But standard streams are opened with |
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: