From a5b228a26dfed9be8aaec1f93318004dea660160 Mon Sep 17 00:00:00 2001 From: Nick Lang Date: Mon, 7 Aug 2017 15:38:47 -0600 Subject: [PATCH] better handling of bytestrings in _escape Fixes #627 --- elasticsearch/client/utils.py | 10 ++++----- test_elasticsearch/test_client/test_utils.py | 23 +++++++++++++++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/elasticsearch/client/utils.py b/elasticsearch/client/utils.py index 78b005cd..120b4abe 100644 --- a/elasticsearch/client/utils.py +++ b/elasticsearch/client/utils.py @@ -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) diff --git a/test_elasticsearch/test_client/test_utils.py b/test_elasticsearch/test_client/test_utils.py index 9fd6df4f..66a83293 100644 --- a/test_elasticsearch/test_client/test_utils.py +++ b/test_elasticsearch/test_client/test_utils.py @@ -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) + )