Skip to content

Commit

Permalink
Added the Unicode support that ought to have been there from the begi…
Browse files Browse the repository at this point in the history
…nning. Thanks to jlilly for point this out.
  • Loading branch information
toastdriven committed Jan 12, 2010
1 parent 55177fd commit 2a5ae6d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
5 changes: 5 additions & 0 deletions examples/http_header_support.py
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from itty import *

@get('/ct')
Expand All @@ -22,4 +23,8 @@ def index(request):
def test_redirect(request):
raise Redirect('/redirected')

@get('/unicode')
def unicode(request):
return u'Works with Unîcødé too!'

run_itty()
27 changes: 23 additions & 4 deletions itty.py
Expand Up @@ -32,7 +32,7 @@ def index(request):
from cgi import parse_qs

__author__ = 'Daniel Lindsley'
__version__ = ('0', '6', '4')
__version__ = ('0', '6', '5')
__license__ = 'BSD'


Expand Down Expand Up @@ -211,9 +211,28 @@ def add_header(self, key, value):

def send(self, start_response):
status = "%d %s" % (self.status, HTTP_MAPPINGS.get(self.status))
headers = [('Content-Type', self.content_type)] + self.headers
start_response(status, headers)
return self.output
headers = [('Content-Type', "%s; charset=utf-8" % self.content_type)] + self.headers
final_headers = []

# Because Unicode is unsupported...
for header in headers:
final_headers.append((self.convert_to_ascii(header[0]), self.convert_to_ascii(header[1])))

start_response(status, final_headers)

if isinstance(self.output, unicode):
return self.output.encode('utf-8')
else:
return self.output

def convert_to_ascii(self, data):
if isinstance(data, unicode):
try:
return data.encode('us-ascii')
except UnicodeError, e:
raise
else:
return str(data)


def handle_request(environ, start_response):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -12,7 +12,7 @@

setup(
name='itty',
version='0.6.4',
version='0.6.5',
description='The itty-bitty Python web framework.',
long_description=long_desc,
author='Daniel Lindsley',
Expand Down

0 comments on commit 2a5ae6d

Please sign in to comment.