Skip to content

Commit

Permalink
Full remove PY2
Browse files Browse the repository at this point in the history
  • Loading branch information
pquentin committed Oct 30, 2023
1 parent 3e1d2ab commit 33d5cf2
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 36 deletions.
6 changes: 2 additions & 4 deletions elasticsearch/client/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from functools import wraps

from .._version import __versionstr__
from ..compat import PY2, quote, string_types, to_bytes, to_str, unquote, urlparse
from ..compat import quote, string_types, to_bytes, to_str, unquote, urlparse

# parts of URL to be omitted
SKIP_IN_PATH = (None, "", b"", [], ())
Expand Down Expand Up @@ -105,9 +105,7 @@ def _escape(value):

# encode strings to utf-8
if isinstance(value, string_types):
if PY2 and isinstance(value, unicode): # noqa: F821
return value.encode("utf-8")
if not PY2 and isinstance(value, str):
if isinstance(value, str):
return value.encode("utf-8")

return str(value)
Expand Down
1 change: 0 additions & 1 deletion elasticsearch/compat.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import sys
from typing import Callable, Tuple, Type, Union

PY2: bool
string_types: Tuple[type, ...]

to_str: Callable[[Union[str, bytes]], str]
Expand Down
22 changes: 0 additions & 22 deletions elasticsearch/connection/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import json

from .. import __versionstr__
from ..compat import PY2
from ..exceptions import (
HTTP_EXCEPTIONS,
ElasticsearchWarning,
Expand Down Expand Up @@ -259,9 +258,6 @@ def log_request_success(
except AttributeError:
pass

if response is not None:
response = loggable_response_body(response)

logger.info(
"%s %s [status:%s request:%.3fs]", method, full_url, status_code, duration
)
Expand Down Expand Up @@ -302,9 +298,6 @@ def log_request_fail(
except AttributeError:
pass

if response is not None:
response = loggable_response_body(response)

logger.debug("> %s", body)

self._log_trace(method, path, body, status_code, response, duration)
Expand Down Expand Up @@ -342,18 +335,3 @@ def _get_api_key_header_val(self, api_key):
s = "{0}:{1}".format(api_key[0], api_key[1]).encode("utf-8")
return "ApiKey " + binascii.b2a_base64(s).rstrip(b"\r\n").decode("utf-8")
return "ApiKey " + api_key


def loggable_response_body(response):
# If 'response' isn't unicode we need to try converting it to
# unicode otherwise it's likely binary so should be encoded
# properly. On Python 3.x this works out fine.
if PY2 and not isinstance(response, unicode): # noqa
try:
response = response.decode("utf-8")
except (AttributeError, UnicodeError):
# Encodes unprintable characters to '\xXX' hex
# like how is done in Python 3.x in bytes.__repr__
response = u"b" + repr(response).decode("utf-8")

return response
9 changes: 0 additions & 9 deletions test_elasticsearch/test_client/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import pytest

from elasticsearch.client.utils import _bulk_body, _escape, _make_path, query_params
from elasticsearch.compat import PY2

from ..test_cases import SkipTest, TestCase

Expand Down Expand Up @@ -446,14 +445,6 @@ def test_handles_unicode(self):
"/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 = "中文".encode("utf-8")
self.assertEqual(
"/some-index/type/%E4%B8%AD%E6%96%87", _make_path("some-index", "type", id)
)


class TestEscape(TestCase):
def test_handles_ascii(self):
Expand Down

0 comments on commit 33d5cf2

Please sign in to comment.