gevent 1.0.1: 500 error writing out Location header

175 views
Skip to first unread message

AM

unread,
Dec 3, 2014, 7:52:15 PM12/3/14
to gev...@googlegroups.com
I have a small reduction case of the problem I am facing:

from gevent import monkey
monkey.patch_all()

from gevent.pywsgi import WSGIServer

def app(env, start_response):
start_response('302 Found', [('Location',
u'https://groups.google.com/forum/#!forum/gevent')])
return []

if __name__ == '__main__':
WSGIServer(('', 5000), app).serve_forever()


To run:

virtualenv _install
source _install/bin/activate
python <filename>.py

Hitting 127.0.0.1:5000 in a browser returns a 500.

The error traceback is:

Traceback (most recent call last):
File
"/work/bt/gevent-bug/_install/lib/python2.7/site-packages/gevent/pywsgi.py",
line 508, in handle_one_response
self.run_application()
File
"/work/bt/gevent-bug/_install/lib/python2.7/site-packages/gevent/pywsgi.py",
line 495, in run_application
self.process_result()
File
"/work/bt/gevent-bug/_install/lib/python2.7/site-packages/gevent/pywsgi.py",
line 488, in process_result
self.write('')
File
"/work/bt/gevent-bug/_install/lib/python2.7/site-packages/gevent/pywsgi.py",
line 380, in write
self._write_with_headers(data)
File
"/work/bt/gevent-bug/_install/lib/python2.7/site-packages/gevent/pywsgi.py",
line 391, in _write_with_headers
towrite.extend('%s: %s\r\n' % header)
TypeError: an integer or string of size 1 is required

Tracking it down in the code, it breaks here:
https://github.com/gevent/gevent/blob/1.0.1/gevent/pywsgi.py#L391

towrite.extend('HTTP/1.1 %s\r\n' % self.status)
for header in self.response_headers:
>> towrite.extend('%s: %s\r\n' % header)

Even if I were to encode the value of the location headers as utf-8 it
still fails.

Using pdb and walking through it, if I were to convert the input of
towrite.extend to a bytearray it will work, however iiuc it should not
be needed as such.

Any workarounds or help would be greatly appreciated.

TIA.
AM

ams.fwd

unread,
Dec 4, 2014, 1:21:02 PM12/4/14
to gev...@googlegroups.com
It appears bytearrays and unicode are not happy campers. Encoding the unicode header values makes it all go away.

Sigh! 2014 and we still cannot deal with unicode cleanly.

AM

Nathan McCorkle

unread,
Apr 2, 2015, 3:21:31 AM4/2/15
to gev...@googlegroups.com
I was running into the same issue, thinking it was the GET parameter query-string, but nope, it was the Response object creation code... I was following this example to send the user a HTML 'file' that I created on the fly (http://flask.pocoo.org/snippets/32/)... pasted here just-in-case:
strIO = StringIO.StringIO()
strIO.write('Hello from Dan Jacob and Stephane Wirtel !')
strIO.seek(0)
but in my case the string being passed to strIO.write() was a unicode string, which was causing the trouble.
The fix looked like this:
strIO = StringIO.StringIO()
now_not_unicode_string = unicode.encode(some_unicode_string)
strIO.write(now_not_unicode_string)
strIO.seek(0)
Reply all
Reply to author
Forward
0 new messages