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 the second Python2 vs Py3 compat.py #3105

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
29 changes: 0 additions & 29 deletions lib/py/src/compat.py

This file was deleted.

6 changes: 3 additions & 3 deletions lib/py/src/protocol/TBinaryProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
# under the License.
#

from .TProtocol import TType, TProtocolBase, TProtocolException, TProtocolFactory
from ..compat import binary_to_str
from struct import pack, unpack

from .TProtocol import TType, TProtocolBase, TProtocolException, TProtocolFactory


class TBinaryProtocol(TProtocolBase):
"""Binary implementation of the Thrift protocol driver."""
Expand Down Expand Up @@ -146,7 +146,7 @@ def readMessageBegin(self):
if self.strictRead:
raise TProtocolException(type=TProtocolException.BAD_VERSION,
message='No protocol version header')
name = binary_to_str(self.trans.readAll(sz))
name = self.trans.readAll(sz).decode('utf-8')
type = self.readByte()
seqid = self.readI32()
return (name, type, seqid)
Expand Down
6 changes: 2 additions & 4 deletions lib/py/src/protocol/TCompactProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
from .TProtocol import TType, TProtocolBase, TProtocolException, TProtocolFactory, checkIntegerLimits
from struct import pack, unpack

from ..compat import binary_to_str, str_to_binary

__all__ = ['TCompactProtocol', 'TCompactProtocolFactory']

CLEAR = 0
Expand Down Expand Up @@ -165,7 +163,7 @@ def writeMessageBegin(self, name, type, seqid):
if tseqid < 0:
tseqid = 2147483648 + (2147483648 + tseqid)
self.__writeVarint(tseqid)
self.__writeBinary(str_to_binary(name))
self.__writeBinary(bytes(name, 'utf-8'))
self.state = VALUE_WRITE

def writeMessageEnd(self):
Expand Down Expand Up @@ -346,7 +344,7 @@ def readMessageBegin(self):
# however the sequence is actually signed...
if seqid > 2147483647:
seqid = -2147483648 - (2147483648 - seqid)
name = binary_to_str(self.__readBinary())
name = self.__readBinary().decode('utf-8')
return (name, type, seqid)

def readMessageEnd(self):
Expand Down
6 changes: 2 additions & 4 deletions lib/py/src/protocol/TJSONProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import math
import sys

from ..compat import str_to_binary


__all__ = ['TJSONProtocol',
'TJSONProtocolFactory',
Expand Down Expand Up @@ -213,7 +211,7 @@ def writeJSONString(self, string):
escaped = ESCAPE_CHAR_VALS.get(s, s)
json_str.append(escaped)
json_str.append('"')
self.trans.write(str_to_binary(''.join(json_str)))
self.trans.write(bytes(''.join(json_str), 'utf-8'))

def writeJSONNumber(self, number, formatter='{0}'):
self.context.write()
Expand Down Expand Up @@ -313,7 +311,7 @@ def readJSONString(self, skipContext):
utf8_bytes = bytearray([ord(character)])
while ord(self.reader.peek()) >= 0x80:
utf8_bytes.append(ord(self.reader.read()))
character = utf8_bytes.decode('utf8')
character = utf8_bytes.decode('utf-8')
string.append(character)

if highSurrogate:
Expand Down
9 changes: 4 additions & 5 deletions lib/py/src/protocol/TProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

from thrift.Thrift import TException, TType, TFrozenDict
from thrift.transport.TTransport import TTransportException
from ..compat import binary_to_str, str_to_binary

import sys
from itertools import islice
Expand Down Expand Up @@ -117,13 +116,13 @@ def writeDouble(self, dub):
pass

def writeString(self, str_val):
self.writeBinary(str_to_binary(str_val))
self.writeBinary(bytes(str_val, 'utf-8'))

def writeBinary(self, str_val):
pass

def writeUtf8(self, str_val):
self.writeString(str_val.encode('utf8'))
self.writeString(str_val.encode('utf-8'))

def readMessageBegin(self):
pass
Expand Down Expand Up @@ -180,13 +179,13 @@ def readDouble(self):
pass

def readString(self):
return binary_to_str(self.readBinary())
return self.readBinary().decode('utf-8')

def readBinary(self):
pass

def readUtf8(self):
return self.readString().decode('utf8')
return self.readString().decode('utf-8')

def skip(self, ttype):
if ttype == TType.BOOL:
Expand Down
31 changes: 15 additions & 16 deletions lib/py/src/transport/THeaderTransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

import struct
import zlib
from io import BytesIO

from thrift.compat import BufferIO, byte_index
from thrift.protocol.TBinaryProtocol import TBinaryProtocol
from thrift.protocol.TCompactProtocol import TCompactProtocol, readVarint, writeVarint
from thrift.Thrift import TApplicationException
Expand All @@ -31,7 +31,6 @@
TTransportException,
)


U16 = struct.Struct("!H")
I32 = struct.Struct("!i")
HEADER_MAGIC = 0x0FFF
Expand Down Expand Up @@ -92,10 +91,10 @@ def __init__(self, transport, allowed_client_types, default_protocol=THeaderSubp
self._client_type = THeaderClientType.HEADERS
self._allowed_client_types = allowed_client_types

self._read_buffer = BufferIO(b"")
self._read_buffer = BytesIO(b"")
self._read_headers = {}

self._write_buffer = BufferIO()
self._write_buffer = BytesIO()
self._write_headers = {}
self._write_transforms = []

Expand Down Expand Up @@ -184,8 +183,8 @@ def readFrame(self, req_sz):
if frame_size & TBinaryProtocol.VERSION_MASK == TBinaryProtocol.VERSION_1:
self._set_client_type(THeaderClientType.UNFRAMED_BINARY)
is_unframed = True
elif (byte_index(first_word, 0) == TCompactProtocol.PROTOCOL_ID and
byte_index(first_word, 1) & TCompactProtocol.VERSION_MASK == TCompactProtocol.VERSION):
elif (first_word[0] == TCompactProtocol.PROTOCOL_ID and
first_word[1] & TCompactProtocol.VERSION_MASK == TCompactProtocol.VERSION):
self._set_client_type(THeaderClientType.UNFRAMED_COMPACT)
is_unframed = True

Expand All @@ -195,7 +194,7 @@ def readFrame(self, req_sz):
rest = self._transport.read(bytes_left_to_read)
else:
rest = b""
self._read_buffer = BufferIO(first_word + rest)
self._read_buffer = BytesIO(first_word + rest)
return

# ok, we're still here so we're framed.
Expand All @@ -204,7 +203,7 @@ def readFrame(self, req_sz):
TTransportException.SIZE_LIMIT,
"Frame was too large.",
)
read_buffer = BufferIO(self._transport.readAll(frame_size))
read_buffer = BytesIO(self._transport.readAll(frame_size))

# the next word is either going to be the version field of a
# binary/compact protocol message or the magic value + flags of a
Expand All @@ -218,8 +217,8 @@ def readFrame(self, req_sz):
elif version & TBinaryProtocol.VERSION_MASK == TBinaryProtocol.VERSION_1:
self._set_client_type(THeaderClientType.FRAMED_BINARY)
self._read_buffer = read_buffer
elif (byte_index(second_word, 0) == TCompactProtocol.PROTOCOL_ID and
byte_index(second_word, 1) & TCompactProtocol.VERSION_MASK == TCompactProtocol.VERSION):
elif (second_word[0] == TCompactProtocol.PROTOCOL_ID and
second_word[1] & TCompactProtocol.VERSION_MASK == TCompactProtocol.VERSION):
self._set_client_type(THeaderClientType.FRAMED_COMPACT)
self._read_buffer = read_buffer
else:
Expand All @@ -229,7 +228,7 @@ def readFrame(self, req_sz):
)

def _parse_header_format(self, buffer):
# make BufferIO look like TTransport for varint helpers
# make BytesIO look like TTransport for varint helpers
buffer_transport = TMemoryBuffer()
buffer_transport._buffer = buffer

Expand Down Expand Up @@ -279,22 +278,22 @@ def _parse_header_format(self, buffer):
for transform_id in transforms:
transform_fn = READ_TRANSFORMS_BY_ID[transform_id]
payload = transform_fn(payload)
return BufferIO(payload)
return BytesIO(payload)

def write(self, buf):
self._write_buffer.write(buf)

def flush(self):
payload = self._write_buffer.getvalue()
self._write_buffer = BufferIO()
self._write_buffer = BytesIO()

buffer = BufferIO()
buffer = BytesIO()
if self._client_type == THeaderClientType.HEADERS:
for transform_id in self._write_transforms:
transform_fn = WRITE_TRANSFORMS_BY_ID[transform_id]
payload = transform_fn(payload)

headers = BufferIO()
headers = BytesIO()
writeVarint(headers, self._protocol_id)
writeVarint(headers, len(self._write_transforms))
for transform_id in self._write_transforms:
Expand Down Expand Up @@ -348,5 +347,5 @@ def cstringio_refill(self, partialread, reqlen):
result = bytearray(partialread)
while len(result) < reqlen:
result += self.read(reqlen - len(result))
self._read_buffer = BufferIO(result)
self._read_buffer = BytesIO(result)
return self._read_buffer
Loading
Loading