gallery: Use byte-compatible sys.stdout for CGI output
authorW. Trevor King <wking@tremily.us>
Mon, 9 Dec 2013 05:27:29 +0000 (21:27 -0800)
committerW. Trevor King <wking@tremily.us>
Mon, 9 Dec 2013 05:27:29 +0000 (21:27 -0800)
We're going to be writing bytes.  In Python 2 they should go to
sys.stdout, but in Python 3 they should go to sys.stdout.buffer.

posts/gallery/gallery.py

index 110f383c8908708cc73925fa3f2f473c6e9e2a0c..40e09e16907fec4d4c5e59fd2ca685c26fece2a4 100755 (executable)
@@ -752,7 +752,10 @@ def serve_cgi(server):
         if isinstance(p, list):
             p = p[0]
         url = p.value
-    server.serve(url=url, page=page, stream=sys.stdout)
+    stream = sys.stdout  # Python 2
+    if hasattr(stream, 'buffer'):  # Python 3
+        stream = sys.stdout.buffer
+    server.serve(url=url, page=page, stream=stream)
 
 def serve_scgi(server, host='localhost', port=4000):
     import scgi