Skip to content

Commit

Permalink
remove more Python2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
a-detiste committed Dec 26, 2024
1 parent 0825ca3 commit ce8e22b
Show file tree
Hide file tree
Showing 20 changed files with 26 additions and 111 deletions.
1 change: 0 additions & 1 deletion compiler/cpp/test/compiler/staleness_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# specific language governing permissions and limitations
# under the License.
#
from __future__ import print_function
import os
import shutil
import subprocess
Expand Down
1 change: 0 additions & 1 deletion contrib/fb303/py/fb303_scripts/fb303_simple_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
# under the License.
#

from __future__ import print_function
import sys
import os
from optparse import OptionParser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
/*
This program was generated by the following Python script:
#!/usr/bin/python2.5
# Remove this when Python 2.6 hits the streets.
from __future__ import with_statement
#!/usr/bin/python3
import sys
import os.path
Expand Down
2 changes: 0 additions & 2 deletions lib/py/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ def run_setup(with_binary):
extensions = dict()

ssl_deps = []
if sys.version_info[0] == 2:
ssl_deps.append('ipaddress')
if sys.hexversion < 0x03050000:
ssl_deps.append('backports.ssl_match_hostname>=3.5')
tornado_deps = ['tornado>=4.0']
Expand Down
5 changes: 0 additions & 5 deletions lib/py/src/TRecursive.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from thrift.Thrift import TType

TYPE_IDX = 1
Expand Down
3 changes: 1 addition & 2 deletions lib/py/src/TTornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
#

from __future__ import absolute_import
import logging
import socket
import struct
Expand All @@ -34,7 +33,7 @@
logger = logging.getLogger(__name__)


class _Lock(object):
class _Lock:
def __init__(self):
self._waiters = deque()

Expand Down
31 changes: 7 additions & 24 deletions lib/py/src/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,13 @@
# under the License.
#

import sys
from io import BytesIO as BufferIO # noqa

if sys.version_info[0] == 2:
def binary_to_str(bin_val):
return bin_val.decode('utf8')

from cStringIO import StringIO as BufferIO
def str_to_binary(str_val):
return bytes(str_val, 'utf8')

def binary_to_str(bin_val):
return bin_val

def str_to_binary(str_val):
return str_val

def byte_index(bytes_val, i):
return ord(bytes_val[i])

else:

from io import BytesIO as BufferIO # noqa

def binary_to_str(bin_val):
return bin_val.decode('utf8')

def str_to_binary(str_val):
return bytes(str_val, 'utf8')

def byte_index(bytes_val, i):
return bytes_val[i]
def byte_index(bytes_val, i):
return bytes_val[i]
14 changes: 3 additions & 11 deletions lib/py/src/protocol/TJSONProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,19 +263,11 @@ def _isLowSurrogate(self, codeunit):

def _toChar(self, high, low=None):
if not low:
if sys.version_info[0] == 2:
return ("\\u%04x" % high).decode('unicode-escape') \
.encode('utf-8')
else:
return chr(high)
return chr(high)
else:
codepoint = (1 << 16) + ((high & 0x3ff) << 10)
codepoint += low & 0x3ff
if sys.version_info[0] == 2:
s = "\\U%08x" % codepoint
return s.decode('unicode-escape').encode('utf-8')
else:
return chr(codepoint)
return chr(codepoint)

def readJSONString(self, skipContext):
highSurrogate = None
Expand Down Expand Up @@ -317,7 +309,7 @@ def readJSONString(self, skipContext):
elif character in ESCAPE_CHAR_VALS:
raise TProtocolException(TProtocolException.INVALID_DATA,
"Unescaped control char")
elif sys.version_info[0] > 2:
else:
utf8_bytes = bytearray([ord(character)])
while ord(self.reader.peek()) >= 0x80:
utf8_bytes.append(ord(self.reader.read()))
Expand Down
5 changes: 0 additions & 5 deletions lib/py/src/protocol/TProtocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,6 @@ def _ttype_handlers(self, ttype, spec):
raise TProtocolException(type=TProtocolException.INVALID_DATA,
message='Invalid binary field type %d' % ttype)
return ('readBinary', 'writeBinary', False)
if sys.version_info[0] == 2 and spec == 'UTF8':
if ttype != TType.STRING:
raise TProtocolException(type=TProtocolException.INVALID_DATA,
message='Invalid string field type %d' % ttype)
return ('readUtf8', 'writeUtf8', False)
return self._TTYPE_HANDLERS[ttype] if ttype < len(self._TTYPE_HANDLERS) else (None, None, False)

def _read_by_ttype(self, ttype, spec, espec):
Expand Down
3 changes: 2 additions & 1 deletion lib/py/src/transport/TTransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
# under the License.
#

from io import BytesIO as BufferIO

from struct import pack, unpack
from thrift.Thrift import TException
from ..compat import BufferIO


class TTransportException(TException):
Expand Down
3 changes: 1 addition & 2 deletions lib/py/src/transport/TZlibTransport.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@
data compression.
"""

from __future__ import division
import zlib
from .TTransport import TTransportBase, CReadableTransport
from ..compat import BufferIO


class TZlibTransportFactory(object):
class TZlibTransportFactory:
"""Factory transport that builds zlib compressed transports.
This factory caches the last single client/transport that it was passed
Expand Down
2 changes: 0 additions & 2 deletions lib/py/test/thrift_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ def test_escaped_unicode_string(self):
transport = TTransport.TBufferedTransportFactory().getTransport(buf)
protocol = TJSONProtocol(transport)

if sys.version_info[0] == 2:
unicode_text = unicode_text.encode('utf8')
self.assertEqual(protocol.readString(), unicode_text)

def test_TJSONProtocol_write(self):
Expand Down
25 changes: 4 additions & 21 deletions test/crossrunner/compat.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import os
import sys

if sys.version_info[0] == 2:
_ENCODE = sys.getfilesystemencoding()
path_join = os.path.join
str_join = str.join

def path_join(*args):
bin_args = map(lambda a: a.decode(_ENCODE), args)
return os.path.join(*bin_args).encode(_ENCODE)

def str_join(left, right):
bin_args = map(lambda a: a.decode(_ENCODE), right)
b = left.decode(_ENCODE)
return b.join(bin_args).encode(_ENCODE)

logfile_open = open

else:

path_join = os.path.join
str_join = str.join

def logfile_open(*args):
return open(*args, errors='replace')
def logfile_open(*args):
return open(*args, errors='replace')
6 changes: 1 addition & 5 deletions test/crossrunner/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
# under the License.
#

from __future__ import print_function
import datetime
import json
import multiprocessing
Expand Down Expand Up @@ -317,10 +316,7 @@ def _print_unexpected_success(self):
self._print_bar()

def _http_server_command(self, port):
if sys.version_info[0] < 3:
return 'python -m SimpleHTTPServer %d' % port
else:
return 'python -m http.server %d' % port
return 'python -m http.server %d' % port

def _print_footer(self):
fail_count = len(self._expected_failure) + len(self._unexpected_failure)
Expand Down
21 changes: 6 additions & 15 deletions test/py/FastbinaryTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

# TODO(dreiss): Test error cases. Check for memory leaks.

from __future__ import print_function

import math
import os
import sys
Expand Down Expand Up @@ -68,15 +66,11 @@ def isOpen(self):
ooe2.integer64 = 64
ooe2.double_precision = (math.sqrt(5) + 1) / 2
ooe2.some_characters = ":R (me going \"rrrr\")"
ooe2.zomg_unicode = u"\xd3\x80\xe2\x85\xae\xce\x9d\x20"\
u"\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe"\
u"\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"\
u"\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba"\
u"\xc7\x83\xe2\x80\xbc"

if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'):
ooe1.zomg_unicode = ooe1.zomg_unicode.encode('utf8')
ooe2.zomg_unicode = ooe2.zomg_unicode.encode('utf8')
ooe2.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"\
"\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe"\
"\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"\
"\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba"\
"\xc7\x83\xe2\x80\xbc"

hm = HolyMoley(**{"big": [], "contain": set(), "bonks": {}})
hm.big.append(ooe1)
Expand All @@ -86,10 +80,7 @@ def isOpen(self):

hm.contain.add(("and a one", "and a two"))
hm.contain.add(("then a one, two", "three!", "FOUR!"))
if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'):
hm.contain.add((u"\xd7\n\a\t".encode('utf8'),))
else:
hm.contain.add((u"\xd7\n\a\t",))
hm.contain.add(("\xd7\n\a\t",))
hm.contain.add(())

hm.bonks["nothing"] = []
Expand Down
2 changes: 0 additions & 2 deletions test/py/RunClientServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
# under the License.
#

from __future__ import division
from __future__ import print_function
import platform
import copy
import os
Expand Down
3 changes: 0 additions & 3 deletions test/py/SerializationTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,6 @@ def testCompactStruct(self):
self.assertTrue(len(rep) > 0)

def testIntegerLimits(self):
if (sys.version_info[0] == 2 and sys.version_info[1] <= 6):
print('Skipping testIntegerLimits for Python 2.6')
return
bad_values = [CompactProtoTestStruct(a_byte=128), CompactProtoTestStruct(a_byte=-129),
CompactProtoTestStruct(a_i16=32768), CompactProtoTestStruct(a_i16=-32769),
CompactProtoTestStruct(a_i32=2147483648), CompactProtoTestStruct(a_i32=-2147483649),
Expand Down
3 changes: 0 additions & 3 deletions test/py/TestClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,6 @@ def testString(self):
Türkçe, Татарча/Tatarça, Українська, اردو, Tiếng Việt, Volapük,
Walon, Winaray, 吴语, isiXhosa, ייִדיש, Yorùbá, Zeêuws, 中文,
Bân-lâm-gú, 粵語"""
if sys.version_info[0] == 2 and os.environ.get('THRIFT_TEST_PY_NO_UTF8STRINGS'):
s1 = s1.encode('utf8')
s2 = s2.encode('utf8')
self.assertEqual(self.client.testString(s1), s1)
self.assertEqual(self.client.testString(s2), s2)

Expand Down
1 change: 0 additions & 1 deletion test/py/TestServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# specific language governing permissions and limitations
# under the License.
#
from __future__ import division
import logging
import os
import signal
Expand Down
1 change: 0 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# subprocess management that are needed for reliability.
#

from __future__ import print_function
from itertools import chain
import json
import logging
Expand Down

0 comments on commit ce8e22b

Please sign in to comment.