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.

Title: should assume a binary stream for output
Type: behavior Stage:
Components: Unicode Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: facundobatista, fdrake, orsenthil, v+python
Priority: normal Keywords:

Created on 2010-11-21 05:37 by v+python, last changed 2022-04-11 14:57 by admin.

Messages (2)
msg121865 - (view) Author: Glenn Linderman (v+python) * Date: 2010-11-21 05:37
The CGI interface is a binary stream, because it is pumped directly to/from the HTTP protocol, which is a binary stream.

Hence, should produce binary output.  Presently, it produces text output.

When one sets stdout to a binary stream, and then cgitb intercepts an error, cgitb fails.

Demonstration of problem:

import sys
import traceback
sys.stdout = open("sob", "wb")  # WSGI sez data should be binary, so stdout should be binary???
import cgitb
fhb = open("fhb", "wb")
fhb.write("abcdef")  # try writing non-binary to binary file.  Expect an error, of course.
msg127574 - (view) Author: Glenn Linderman (v+python) * Date: 2011-01-31 04:43
So since was fixed to use the .buffer attribute of sys.stdout, that leaves sys.stdout itself as a character stream, and can successfully write to that.

If never writes anything but ASCII, then maybe that should be documented, and this issue closed.

If writes non-ASCII, then it should use an appropriate encoding for the web application, which isn't necessarily the default encoding on the system.  Some user control over the appropriate encoding should be given, or it should be documented that the encoding of sys.stdout should be changed to an appropriate encoding, because that is where will write its character stream.  Guidance on how to do that would be appropriate for the documentation also, as a CGI application may be the first one a programmer might write that can't just use the default encoding configured for the system.
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54688
2020-11-16 21:55:03iritkatrielsetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.1, Python 3.2
2011-01-31 04:43:32v+pythonsetnosy: fdrake, facundobatista, orsenthil, v+python
messages: + msg127574
2010-11-21 16:58:06pitrousetnosy: + fdrake, facundobatista, orsenthil

versions: + Python 3.1
2010-11-21 07:54:24v+pythonsettype: behavior
2010-11-21 05:37:14v+pythoncreate