From 60e3be3e3cbfc949d83ee37c89c13f6100c9c92d Mon Sep 17 00:00:00 2001 From: cocolato Date: Thu, 21 Mar 2024 21:48:30 +0800 Subject: [PATCH] remove invalid historical compatibility code --- tests/test_json_protocol.py | 6 ++---- tests/test_protocol_binary.py | 14 ++++++-------- tests/test_protocol_compact.py | 16 +++++++--------- tests/test_protocol_cybinary.py | 15 +++++++-------- thriftpy2/_compat.py | 9 --------- thriftpy2/parser/parser.py | 4 ++-- thriftpy2/protocol/json.py | 9 ++++----- 7 files changed, 28 insertions(+), 45 deletions(-) diff --git a/tests/test_json_protocol.py b/tests/test_json_protocol.py index 6c14846b..34178ddf 100644 --- a/tests/test_json_protocol.py +++ b/tests/test_json_protocol.py @@ -1,11 +1,9 @@ # -*- coding: utf-8 -*- +import thriftpy2.protocol.json as proto from thriftpy2.protocol import TJSONProtocol from thriftpy2.thrift import TPayload, TType from thriftpy2.transport import TMemoryBuffer -from thriftpy2._compat import u - -import thriftpy2.protocol.json as proto class TItem(TPayload): @@ -105,7 +103,7 @@ class Foo(TPayload): trans = TMemoryBuffer() p = TJSONProtocol(trans) - foo = Foo(name=u('pão de açúcar')) + foo = Foo(name='pão de açúcar') foo.write(p) foo2 = Foo() diff --git a/tests/test_protocol_binary.py b/tests/test_protocol_binary.py index 1657ab95..2e9b0bed 100644 --- a/tests/test_protocol_binary.py +++ b/tests/test_protocol_binary.py @@ -4,12 +4,10 @@ import pytest -from thriftpy2._compat import u -from thriftpy2.thrift import TType, TPayload -from thriftpy2.utils import hexlify -from thriftpy2.protocol import binary as proto from thriftpy2 import load -from thriftpy2.utils import serialize +from thriftpy2.protocol import binary as proto +from thriftpy2.thrift import TPayload, TType +from thriftpy2.utils import hexlify, serialize class TItem(TPayload): @@ -82,7 +80,7 @@ def test_pack_string(): hexlify(b.getvalue()) b = BytesIO() - proto.write_val(b, TType.STRING, u("你好世界")) + proto.write_val(b, TType.STRING, "你好世界") assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \ hexlify(b.getvalue()) @@ -90,13 +88,13 @@ def test_pack_string(): def test_unpack_string(): b = BytesIO(b"\x00\x00\x00\x0c" b"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c") - assert u("你好世界") == proto.read_val(b, TType.STRING) + assert "你好世界" == proto.read_val(b, TType.STRING) def test_unpack_binary(): bs = BytesIO(b"\x00\x00\x00\x0c" b"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c") - assert u("你好世界").encode("utf-8") == proto.read_val( + assert "你好世界".encode("utf-8") == proto.read_val( bs, TType.STRING, decode_response=False) diff --git a/tests/test_protocol_compact.py b/tests/test_protocol_compact.py index e08021c6..bee5c642 100644 --- a/tests/test_protocol_compact.py +++ b/tests/test_protocol_compact.py @@ -4,10 +4,9 @@ import pytest -from thriftpy2._compat import u -from thriftpy2.thrift import TType, TPayload -from thriftpy2.utils import hexlify from thriftpy2.protocol import compact +from thriftpy2.thrift import TPayload, TType +from thriftpy2.utils import hexlify class TItem(TPayload): @@ -102,11 +101,11 @@ def test_pack_string(): def test_unpack_string(): b, proto = gen_proto(b"\x0c\x68\x65\x6c\x6c\x6f" b"\x20\x77\x6f\x72\x6c\x64\x21") - assert u('hello world!') == proto._read_val(TType.STRING) + assert 'hello world!' == proto._read_val(TType.STRING) b, proto = gen_proto(b'\x0c\xe4\xbd\xa0\xe5\xa5' b'\xbd\xe4\xb8\x96\xe7\x95\x8c') - assert u('你好世界') == proto._read_val(TType.STRING) + assert '你好世界' == proto._read_val(TType.STRING) def test_unpack_binary(): @@ -114,7 +113,7 @@ def test_unpack_binary(): b'\xbd\xe4\xb8\x96\xe7\x95\x8c') proto.decode_response = False - assert u('你好世界').encode("utf-8") == proto._read_val(TType.STRING) + assert '你好世界'.encode("utf-8") == proto._read_val(TType.STRING) def test_strict_decode(): @@ -157,11 +156,10 @@ def test_unpack_container_bool(): assert [True, False, True] == proto._read_val(TType.LIST, TType.BOOL) b, proto = gen_proto(b"\x01\x81\x01\x61\x01") - assert {u("a"): True} == proto._read_val(TType.MAP, - (TType.STRING, TType.BOOL)) + assert {"a": True} == proto._read_val(TType.MAP, (TType.STRING, TType.BOOL)) b, proto = gen_proto(b"\x01\x89\x01\x61\x21\x01\x02") - assert {u("a"): [True, False]} == proto._read_val( + assert {"a": [True, False]} == proto._read_val( TType.MAP, (TType.STRING, (TType.LIST, TType.BOOL))) b, proto = gen_proto(b"\x03\x81\x01\x61\x01\x01\x63\x01\x01\x62\x02") diff --git a/tests/test_protocol_cybinary.py b/tests/test_protocol_cybinary.py index 482ea703..41c4494f 100644 --- a/tests/test_protocol_cybinary.py +++ b/tests/test_protocol_cybinary.py @@ -7,18 +7,17 @@ import pytest -from thriftpy2._compat import u -from thriftpy2.thrift import TType, TPayload, TDecodeException -from thriftpy2.transport import TSocket, TServerSocket +from thriftpy2._compat import PYPY +from thriftpy2.thrift import TDecodeException, TPayload, TType +from thriftpy2.transport import TServerSocket, TSocket from thriftpy2.utils import hexlify -from thriftpy2._compat import PYPY pytestmark = pytest.mark.skipif(PYPY, reason="cython not enabled in pypy.") if not PYPY: from thriftpy2.protocol import cybin as proto - from thriftpy2.transport.memory import TCyMemoryBuffer from thriftpy2.transport.buffered import TCyBufferedTransport + from thriftpy2.transport.memory import TCyMemoryBuffer class TItem(TPayload): @@ -128,7 +127,7 @@ def test_write_string(): hexlify(b.getvalue()) b = TCyMemoryBuffer() - proto.write_val(b, TType.STRING, u("你好世界")) + proto.write_val(b, TType.STRING, "你好世界") b.flush() assert "00 00 00 0c e4 bd a0 e5 a5 bd e4 b8 96 e7 95 8c" == \ hexlify(b.getvalue()) @@ -137,13 +136,13 @@ def test_write_string(): def test_read_string(): b = TCyMemoryBuffer(b"\x00\x00\x00\x0c" b"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c") - assert u("你好世界") == proto.read_val(b, TType.STRING) + assert "你好世界" == proto.read_val(b, TType.STRING) def test_read_binary(): b = TCyMemoryBuffer(b"\x00\x00\x00\x0c" b"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xb8\x96\xe7\x95\x8c") - assert u("你好世界").encode("utf-8") == proto.read_val( + assert "你好世界".encode("utf-8") == proto.read_val( b, TType.STRING, decode_response=False) diff --git a/thriftpy2/_compat.py b/thriftpy2/_compat.py index c886e352..6ace9ad0 100644 --- a/thriftpy2/_compat.py +++ b/thriftpy2/_compat.py @@ -11,21 +11,12 @@ import platform import sys -from urllib.parse import urlparse # noqa -from urllib.request import urlopen # noqa PYPY = "__pypy__" in sys.modules UNIX = platform.system() in ("Linux", "Darwin") CYTHON = UNIX and not PYPY # Cython always disabled in pypy and windows -text_type = str -string_types = (str,) - - -def u(s): - return s - def with_metaclass(meta, *bases): """Create a base class with a metaclass for py2 & py3 diff --git a/thriftpy2/parser/parser.py b/thriftpy2/parser/parser.py index d20081d3..b4d861b0 100644 --- a/thriftpy2/parser/parser.py +++ b/thriftpy2/parser/parser.py @@ -10,11 +10,11 @@ import collections import os import types +from urllib.parse import urlparse +from urllib.request import urlopen from ply import lex, yacc -from thriftpy2._compat import urlopen, urlparse - from ..thrift import TException, TPayload, TType, gen_init from .exc import ThriftGrammarError, ThriftParserError from .lexer import * # noqa diff --git a/thriftpy2/protocol/json.py b/thriftpy2/protocol/json.py index 6a662415..73ab2e8f 100644 --- a/thriftpy2/protocol/json.py +++ b/thriftpy2/protocol/json.py @@ -2,19 +2,18 @@ from __future__ import absolute_import +import base64 import json import struct -import base64 import sys from warnings import warn import six -from thriftpy2._compat import u from thriftpy2.thrift import TType -from .exc import TProtocolException from .base import TProtocolBase +from .exc import TProtocolException VERSION = 1 @@ -32,7 +31,7 @@ def json_value(ttype, val, spec=None): TType.I32: (int, (val,)), TType.I64: (int, (val,)), TType.DOUBLE: (float, (val,)), - TType.STRING: (u, (val,)), + TType.STRING: (str, (val,)), TType.BOOL: (bool, (val,)), TType.STRUCT: (struct_to_json, (val,)), TType.SET: (list_to_json, (val, spec)), @@ -59,7 +58,7 @@ def obj_value(ttype, val, spec=None): TType.I32: (int, (val,)), TType.I64: (int, (val,)), TType.DOUBLE: (float, (val,)), - TType.STRING: (u, (val,)), + TType.STRING: (str, (val,)), TType.BOOL: (bool, (val,)), TType.SET: (list_to_obj, (val, spec)), TType.LIST: (list_to_obj, (val, spec)),