From 756a6aee2561159004a0aab07dfecffeca330651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Thu, 29 May 2014 01:16:17 +0200 Subject: [PATCH] unicode_literals ftw --- elasticsearch/client/utils.py | 6 ++++-- elasticsearch/compat.py | 2 -- test_elasticsearch/test_client/__init__.py | 5 +++-- test_elasticsearch/test_client/test_utils.py | 8 +++++--- test_elasticsearch/test_server/test_memcached.py | 8 ++++---- test_elasticsearch/test_transport.py | 4 ++-- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/elasticsearch/client/utils.py b/elasticsearch/client/utils.py index c2755fb9..a49d72d0 100644 --- a/elasticsearch/client/utils.py +++ b/elasticsearch/client/utils.py @@ -1,6 +1,8 @@ +from __future__ import unicode_literals + from datetime import date, datetime from functools import wraps -from ..compat import string_types, quote_plus, u +from ..compat import string_types, quote_plus # parts of URL to be omitted SKIP_IN_PATH = (None, '', [], ()) @@ -13,7 +15,7 @@ def _escape(value): # make sequences into comma-separated stings if isinstance(value, (list, tuple)): - value = u(',').join(value) + value = ','.join(value) # dates and datetimes into isoformat elif isinstance(value, (date, datetime)): diff --git a/elasticsearch/compat.py b/elasticsearch/compat.py index 0c3e628d..5004096e 100644 --- a/elasticsearch/compat.py +++ b/elasticsearch/compat.py @@ -3,12 +3,10 @@ import sys PY2 = sys.version_info[0] == 2 if PY2: - u = lambda s: s.decode('utf-8') string_types = basestring, from urllib import quote_plus, urlencode from itertools import imap as map else: - u = str string_types = str, bytes from urllib.parse import quote_plus, urlencode map = map diff --git a/test_elasticsearch/test_client/__init__.py b/test_elasticsearch/test_client/__init__.py index 356c3e45..9901dc0c 100644 --- a/test_elasticsearch/test_client/__init__.py +++ b/test_elasticsearch/test_client/__init__.py @@ -1,7 +1,8 @@ +from __future__ import unicode_literals + from mock import patch from elasticsearch.client import _normalize_hosts, Elasticsearch -from elasticsearch.compat import u from ..test_cases import TestCase, ElasticsearchTestCase @@ -15,7 +16,7 @@ class TestNormalizeHosts(TestCase): def test_strings_are_parsed_for_port(self): self.assertEquals( [{"host": "elasticsearch.org", "port": 42}, {"host": "user:secret@elasticsearch.com"}], - _normalize_hosts(["elasticsearch.org:42", u("user:secret@elasticsearch.com")]) + _normalize_hosts(["elasticsearch.org:42", "user:secret@elasticsearch.com"]) ) def test_dicts_are_left_unchanged(self): diff --git a/test_elasticsearch/test_client/test_utils.py b/test_elasticsearch/test_client/test_utils.py index a3040be9..9fd6df4f 100644 --- a/test_elasticsearch/test_client/test_utils.py +++ b/test_elasticsearch/test_client/test_utils.py @@ -1,17 +1,19 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals + from elasticsearch.client.utils import _make_path -from elasticsearch.compat import PY2, u +from elasticsearch.compat import PY2 from ..test_cases import TestCase, SkipTest class TestMakePath(TestCase): def test_handles_unicode(self): - id = u("中文") + id = "中文" self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id)) def test_handles_utf_encoded_string(self): if not PY2: raise SkipTest('Only relevant for py2') - id = u("中文").encode('utf-8') + id = "中文".encode('utf-8') self.assertEquals('/some-index/type/%E4%B8%AD%E6%96%87', _make_path('some-index', 'type', id)) diff --git a/test_elasticsearch/test_server/test_memcached.py b/test_elasticsearch/test_server/test_memcached.py index e9cf144d..d08f1183 100644 --- a/test_elasticsearch/test_server/test_memcached.py +++ b/test_elasticsearch/test_server/test_memcached.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- -from elasticsearch import Elasticsearch, MemcachedConnection, NotFoundError +from __future__ import unicode_literals +from elasticsearch import Elasticsearch, MemcachedConnection, NotFoundError from elasticsearch.transport import ADDRESS_RE -from elasticsearch.compat import u from . import ElasticsearchTestCase from ..test_cases import SkipTest @@ -35,8 +35,8 @@ class TestMemcachedConnection(ElasticsearchTestCase): self.assertEquals({"answer": 42}, self.mc_client.get("test_index", doc_type="test_type", id=1)["_source"]) def test_unicode(self): - self.mc_client.index("test_index", "test_type", {"answer": u("你好")}, id=u("你好")) - self.assertEquals({"answer": u("你好")}, self.mc_client.get("test_index", doc_type="test_type", id=u("你好"))["_source"]) + self.mc_client.index("test_index", "test_type", {"answer": "你好"}, id="你好") + self.assertEquals({"answer": "你好"}, self.mc_client.get("test_index", doc_type="test_type", id="你好")["_source"]) def test_missing(self): self.assertRaises(NotFoundError, self.mc_client.get, "test_index", doc_type="test_type", id=42) diff --git a/test_elasticsearch/test_transport.py b/test_elasticsearch/test_transport.py index 25ccf442..3f8580c0 100644 --- a/test_elasticsearch/test_transport.py +++ b/test_elasticsearch/test_transport.py @@ -1,10 +1,10 @@ # -*- coding: utf-8 -*- +from __future__ import unicode_literals import time from elasticsearch.transport import Transport from elasticsearch.connection import Connection from elasticsearch.exceptions import ConnectionError -from elasticsearch.compat import u from .test_cases import TestCase @@ -55,7 +55,7 @@ class TestTransport(TestCase): def test_body_gets_encoded_into_bytes(self): t = Transport([{}], connection_class=DummyConnection) - t.perform_request('GET', '/', body=u('你好')) + t.perform_request('GET', '/', body='你好') 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])