Merge pull request #628 from fxdgear/nick/fix_encode_error
better handling of bytestrings in _escape
This commit is contained in:
@@ -26,13 +26,13 @@ def _escape(value):
|
||||
elif isinstance(value, bool):
|
||||
value = str(value).lower()
|
||||
|
||||
# don't decode bytestrings
|
||||
elif isinstance(value, bytes):
|
||||
return value
|
||||
|
||||
# encode strings to utf-8
|
||||
if isinstance(value, string_types):
|
||||
try:
|
||||
return value.encode('utf-8')
|
||||
except UnicodeDecodeError:
|
||||
# Python 2 and str, no need to re-encode
|
||||
pass
|
||||
return value.encode('utf-8')
|
||||
|
||||
return str(value)
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from elasticsearch.client.utils import _make_path
|
||||
from elasticsearch.client.utils import _make_path, _escape
|
||||
from elasticsearch.compat import PY2
|
||||
|
||||
from ..test_cases import TestCase, SkipTest
|
||||
@@ -17,3 +17,24 @@ class TestMakePath(TestCase):
|
||||
id = "中文".encode('utf-8')
|
||||
self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id))
|
||||
|
||||
|
||||
class TestEscape(TestCase):
|
||||
def test_handles_ascii(self):
|
||||
string = "abc123"
|
||||
self.assertEquals(
|
||||
b'abc123',
|
||||
_escape(string)
|
||||
)
|
||||
def test_handles_unicode(self):
|
||||
string = "中文"
|
||||
self.assertEquals(
|
||||
b'\xe4\xb8\xad\xe6\x96\x87',
|
||||
_escape(string)
|
||||
)
|
||||
|
||||
def test_handles_bytestring(self):
|
||||
string = b'celery-task-meta-c4f1201f-eb7b-41d5-9318-a75a8cfbdaa0'
|
||||
self.assertEquals(
|
||||
string,
|
||||
_escape(string)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user