Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove invalid historical compatibility code #253

Merged
merged 1 commit into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions tests/test_json_protocol.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 6 additions & 8 deletions tests/test_protocol_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -82,21 +80,21 @@ 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())


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)


Expand Down
16 changes: 7 additions & 9 deletions tests/test_protocol_compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -102,19 +101,19 @@ 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():
b, proto = gen_proto(b'\x0c\xe4\xbd\xa0\xe5\xa5'
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():
Expand Down Expand Up @@ -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")
Expand Down
15 changes: 7 additions & 8 deletions tests/test_protocol_cybinary.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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())
Expand All @@ -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)


Expand Down
9 changes: 0 additions & 9 deletions thriftpy2/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions thriftpy2/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions thriftpy2/protocol/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)),
Expand All @@ -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)),
Expand Down
Loading