<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#330033" bgcolor="#ffffff">
On 11/19/2010 7:48 PM, Glenn Linderman wrote:
<blockquote cite="mid:4CE7452A.7050109@g.nevcal.com" type="cite">
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
One of the cgitb outputs from my attempt to serve the binary file
claims that my CGI script's output file (which comes from a
subprocess PIPE) is a TextIOWrapper with encoding cp1252. Maybe
that is the default that comes when a new Python is launched, even
though it gets a subprocess PIPE as stdout?<br>
</blockquote>
<br>
So the rather gross code below solves the cp1252 stdout problem, and
also permits both strings and bytes to be written to the same file,
although those two features are separable. But now that I've worked
around it, it seems that subprocesss should somehow ensure that
launched Python programs know they are working on a binary stream?
Of course, not all programs launched are Python programs... so maybe
it should be a documentation issue, but it seems to be missing from
the documentation.<br>
<br>
#####################################<br>
if sys.version_info[ 0 ] == 2: <br>
class IOMix():<br>
def __init__( self, fh, encoding="UTF-8"):<br>
self.fh = fh<br>
def write( self, param ):<br>
if isinstance( param, unicode ):<br>
self.fh.write( param.encode( encoding ))<br>
else:<br>
self.fh.write( param )<br>
#####################################<br>
if sys.version_info[ 0 ] == 3:<br>
class IOMix():<br>
def __init__( self, fh, encoding="UTF-8"):<br>
if hasattr( fh, 'buffer'):<br>
self.bio = fh.buffer<br>
fh.flush()<br>
self.last = 'b'<br>
import io<br>
self.txt = io.TextIOWrapper( self.bio, encoding,
None, '\r\n')<br>
else:<br>
raise ValueError("not a buffered stream")<br>
def write( self, param ):<br>
if isinstance( param, str ):<br>
self.last = 't'<br>
self.txt.write( param )<br>
else:<br>
if self.last == 't':<br>
self.txt.flush()<br>
self.last = 'b'<br>
self.bio.write( param )<br>
#####################################<br>
<br>
<br>
</body>
</html>