[Python-checkins] r87818 - peps/trunk/pep-3333.txt

phillip.eby python-checkins at python.org
Fri Jan 7 16:53:02 CET 2011


Author: phillip.eby
Date: Fri Jan  7 16:53:02 2011
New Revision: 87818

Log:
Need to actually encode headers being sent...


Modified:
   peps/trunk/pep-3333.txt

Modified: peps/trunk/pep-3333.txt
==============================================================================
--- peps/trunk/pep-3333.txt	(original)
+++ peps/trunk/pep-3333.txt	Fri Jan  7 16:53:02 2011
@@ -283,12 +283,15 @@
 
     enc, esc = sys.getfilesystemencoding(), 'surrogateescape'
 
-    def wsgi_string(u):
+    def unicode_to_wsgi(u):
         # Convert an environment variable to a WSGI "bytes-as-unicode" string
         return u.encode(enc, esc).decode('iso-8859-1')
 
+    def wsgi_to_bytes(s):
+        return s.encode('iso-8859-1')
+
     def run_with_cgi(application):
-        environ = {k: wsgi_string(v) for k,v in os.environ.items()}
+        environ = {k: unicode_to_wsgi(v) for k,v in os.environ.items()}
         environ['wsgi.input']        = sys.stdin
         environ['wsgi.errors']       = sys.stderr
         environ['wsgi.version']      = (1, 0)
@@ -305,19 +308,21 @@
         headers_sent = []
 
         def write(data):
+            out = sys.stdout.buffer
+
             if not headers_set:
                  raise AssertionError("write() before start_response()")
 
             elif not headers_sent:
                  # Before the first output, send the stored headers
                  status, response_headers = headers_sent[:] = headers_set
-                 sys.stdout.buffer.write('Status: %s\r\n' % status)
+                 out.write(wsgi_to_bytes('Status: %s\r\n' % status))
                  for header in response_headers:
-                     sys.stdout.buffer.write('%s: %s\r\n' % header)
-                 sys.stdout.write('\r\n')
+                     out.write(wsgi_to_bytes('%s: %s\r\n' % header))
+                 out.write(wsgi_to_bytes('\r\n'))
 
-            sys.stdout.buffer.write(data)
-            sys.stdout.buffer.flush()
+            out.write(data)
+            out.flush()
 
         def start_response(status, response_headers, exc_info=None):
             if exc_info:


More information about the Python-checkins mailing list