Fix unicode body handling.

Only body should be encoded as bytes, not anything going through the
serializer (mostly because of bulk)
This commit is contained in:
Honza Král
2013-12-04 15:07:56 +01:00
parent 4420296636
commit 95fba05e02
4 changed files with 20 additions and 7 deletions
+1 -5
View File
@@ -21,11 +21,7 @@ class JSONSerializer(object):
def dumps(self, data):
# don't serialize strings
if isinstance(data, (type(''), type(u''))):
try:
return data.encode('utf-8')
except UnicodeDecodeError:
# Python 2 and str, no need to re-encode
return data
return data
try:
return json.dumps(data, default=self.default)
+7
View File
@@ -234,6 +234,13 @@ class Transport(object):
params['source'] = body
body = None
if body is not None:
try:
body = body.encode('utf-8')
except UnicodeDecodeError:
# Python 2 and str, no need to re-encode
pass
ignore = ()
if params and 'ignore' in params:
ignore = params.pop('ignore')
+2 -1
View File
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import sys
from datetime import datetime
@@ -26,4 +27,4 @@ class TestJSONSerializer(TestCase):
self.assertRaises(SerializationError, JSONSerializer().loads, '{{')
def test_strings_are_left_untouched(self):
self.assertEquals('Hello World!', JSONSerializer().dumps('Hello World!'))
self.assertEquals(u"你好", JSONSerializer().dumps(u"你好"))
+10 -1
View File
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
import time
from elasticsearch.transport import Transport
@@ -47,7 +48,15 @@ class TestTransport(TestCase):
t.perform_request('GET', '/', body={})
self.assertEquals(1, len(t.get_connection().calls))
self.assertEquals(('POST', '/', None, '{}'), t.get_connection().calls[0][0])
self.assertEquals(('POST', '/', None, b'{}'), t.get_connection().calls[0][0])
def test_body_gets_encoded_into_bytes(self):
t = Transport([{}], connection_class=DummyConnection)
t.perform_request('GET', '/', body=u'你好')
self.assertEquals(1, len(t.get_connection().calls))
self.assertEquals(('GET', '/', None, b'\xe4\xbd\xa0\xe5\xa5\xbd'), t.get_connection().calls[0][0])
def test_kwargs_passed_on_to_connections(self):
t = Transport([{'host': 'google.com'}], port=123)