Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Python3 compatibility part 1: Cure errors in local build (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
westover authored and abhinav committed Aug 6, 2019
1 parent a2a86ac commit c37c63a
Show file tree
Hide file tree
Showing 47 changed files with 7,344 additions and 7,293 deletions.
2 changes: 1 addition & 1 deletion crossdock/server/api.py
Expand Up @@ -62,7 +62,7 @@ def downstream_from_dict(json):

def namedtuple_to_dict(tpl):
json = {}
for k, v in tpl._asdict().iteritems():
for k, v in tpl._asdict().items():
if hasattr(v, '_asdict'):
v = namedtuple_to_dict(v)
json[k] = v
Expand Down
2 changes: 1 addition & 1 deletion requirements-test.txt
Expand Up @@ -33,7 +33,7 @@ futures
flake8==2.2.5

# Optional dependency, but must be tested er'ry time
thrift==0.9.2
thrift==0.11.0

# Smarter decorators
wrapt>=1.10,<1.11
Expand Down
2 changes: 1 addition & 1 deletion tchannel/enum.py
Expand Up @@ -24,6 +24,6 @@


def enum(class_name, **values):
class_type = collections.namedtuple(class_name, values.keys())
class_type = collections.namedtuple(class_name, list(values.keys()))
instance = class_type(**values)
return instance
1 change: 0 additions & 1 deletion tchannel/errors.py
Expand Up @@ -61,7 +61,6 @@ class TChannelError(Exception):
"""

__slots__ = (
'code',
'description',
'id',
'tracing',
Expand Down
1 change: 1 addition & 0 deletions tchannel/messages/call_continue.py
Expand Up @@ -23,6 +23,7 @@
from . import common
from .base import BaseMessage
from .common import FlagsType
from six.moves import range


class CallContinueMessage(BaseMessage):
Expand Down
2 changes: 1 addition & 1 deletion tchannel/net.py
Expand Up @@ -54,7 +54,7 @@ def local_ip():
if ip.startswith('127.'):
# Check eth0, eth1, eth2, en0, ...
interfaces = [
i + str(n) for i in ("eth", "en", "wlan") for n in xrange(3)
i + str(n) for i in ("eth", "en", "wlan") for n in range(3)
] # :(
for interface in interfaces:
try:
Expand Down
2 changes: 1 addition & 1 deletion tchannel/peer_strategy.py
Expand Up @@ -36,7 +36,7 @@ class PreferIncomingCalculator(RankCalculator):
# 0: ephemeral peers or unconnected peers
# 1: peers with only outgoing connections
# 2: peers with incoming connections
TIERS = [sys.maxint, sys.maxint / 2, 0]
TIERS = [sys.maxsize, sys.maxsize / 2, 0]

def get_rank(self, peer):
"""Calculate the peer rank based on connections.
Expand Down
4 changes: 2 additions & 2 deletions tchannel/rw.py
Expand Up @@ -597,7 +597,7 @@ def read(self, stream):
def write(self, headers, stream):
# In case someone does write({..}, stream)
if isinstance(headers, dict):
headers = headers.items()
headers = list(headers.items())

self._length.write(len(headers), stream)
for pair in headers:
Expand All @@ -610,7 +610,7 @@ def width(self):
def length(self, headers):
size = 0
if isinstance(headers, dict):
headers = headers.items()
headers = list(headers.items())

size += self._length.length(len(headers))
for pair in headers:
Expand Down
6 changes: 4 additions & 2 deletions tchannel/serializer/json.py
Expand Up @@ -23,6 +23,7 @@
import json

from tchannel.schemes import JSON
import six


class JsonSerializer(object):
Expand All @@ -31,8 +32,9 @@ class JsonSerializer(object):
def serialize_header(self, headers):
headers = headers or {}

for k, v in headers.iteritems():
if not (isinstance(k, basestring) and isinstance(v, basestring)):
for k, v in six.iteritems(headers):
if not (isinstance(k, six.string_types) and
isinstance(v, six.string_types)):
raise ValueError(
'headers must be a map[string]string (a shallow dict '
'where keys and values are strings)'
Expand Down
10 changes: 5 additions & 5 deletions tchannel/tcurl.py
Expand Up @@ -40,7 +40,7 @@
--body '{"nyuck": "nyuck"}'
"""

from __future__ import absolute_import
from __future__ import absolute_import, print_function

import argparse
import logging
Expand Down Expand Up @@ -262,9 +262,9 @@ def main(argv=None):
)

if not args.raw:
print json.dumps(result.body, default=_dictify)
print(json.dumps(result.body, default=_dictify))
else:
print result.body
print(result.body)

raise tornado.gen.Return(result)

Expand All @@ -273,11 +273,11 @@ def main(argv=None):
def _catch_errors(future, verbose, exit=sys.exit):
try:
result = yield future
except Exception, e:
except Exception as e:
if verbose:
traceback.print_exc(file=sys.stderr)
else:
print >> sys.stderr, str(e)
print(str(e), file=sys.stderr)
exit(1)

raise tornado.gen.Return(result)
Expand Down
5 changes: 3 additions & 2 deletions tchannel/testing/vcr/cassette.py
Expand Up @@ -37,6 +37,7 @@
from . import proxy
from tchannel import tracing
from tchannel.serializer.thrift import ThriftSerializer
import six

__all__ = ['Cassette', 'DEFAULT_MATCHERS']

Expand Down Expand Up @@ -73,7 +74,7 @@ def matcher(left, right):

def filter_headers(hs):
return {
k: v for k, v in hs.iteritems()
k: v for k, v in six.iteritems(hs)
if not k.startswith(tracing.TRACING_KEY_PREFIX)
}

Expand Down Expand Up @@ -170,7 +171,7 @@ def __init__(self, path, record_mode=None, matchers=None, serializer=None):
"""
# TODO move documentation around
record_mode = record_mode or RecordMode.ONCE
if isinstance(record_mode, basestring):
if isinstance(record_mode, six.string_types):
record_mode = RecordMode.from_name(record_mode)

self.path = path
Expand Down
3 changes: 2 additions & 1 deletion tchannel/testing/vcr/patch.py
Expand Up @@ -34,6 +34,7 @@
from tchannel.tornado.stream import read_full

from . import proxy
from six.moves import map


_TChannel_request = TChannel.request
Expand Down Expand Up @@ -70,7 +71,7 @@ def send(self, arg1, arg2, arg3,
headers=None,
ttl=None,
**kwargs):
arg1, arg2, arg3 = map(maybe_stream, [arg1, arg2, arg3])
arg1, arg2, arg3 = list(map(maybe_stream, [arg1, arg2, arg3]))

endpoint = yield read_full(arg1)

Expand Down
1 change: 1 addition & 0 deletions tchannel/testing/vcr/proxy.py
Expand Up @@ -18,6 +18,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import absolute_import
import os
import sys

Expand Down
3 changes: 2 additions & 1 deletion tchannel/thrift/module.py
Expand Up @@ -31,6 +31,7 @@
from tchannel.serializer.thrift import ThriftSerializer

from .reflection import get_service_methods, get_module_name
import six


@deprecated(
Expand Down Expand Up @@ -97,7 +98,7 @@ def thrift_request_builder(service, thrift_module, hostport=None,
methods = _create_methods(thrift_module)

# then attach to instane
for name, method in methods.iteritems():
for name, method in six.iteritems(methods):
method = types.MethodType(method, maker, ThriftRequestMaker)
setattr(maker, name, method)

Expand Down
3 changes: 2 additions & 1 deletion tchannel/thrift/server.py
Expand Up @@ -22,6 +22,7 @@

import sys
from collections import namedtuple
import six

from tornado import gen

Expand Down Expand Up @@ -245,4 +246,4 @@ def write_exc_info(self, exc_info=None):
return

# Re-raise the exception (with the same traceback) if it didn't match.
raise exc_info[0], exc_info[1], exc_info[2]
six.reraise(exc_info[0], exc_info[1], exc_info[2])
5 changes: 3 additions & 2 deletions tchannel/tornado/connection.py
Expand Up @@ -54,6 +54,7 @@
from .message_factory import build_raw_error_message
from .message_factory import MessageFactory
from .tombstone import Cemetery
import six

log = logging.getLogger('tchannel')

Expand Down Expand Up @@ -171,7 +172,7 @@ def _on_close(self):
self.closed = True
self._request_tombstones.clear()

for message_id, future in self._outbound_pending_call.iteritems():
for message_id, future in six.iteritems(self._outbound_pending_call):
future.set_exception(
NetworkError(
"canceling outstanding request %d" % message_id
Expand Down Expand Up @@ -335,7 +336,7 @@ def _write_fragment(future):
return answer.set_exc_info(future.exc_info())

try:
fragment = fragments.next()
fragment = next(fragments)
except StopIteration:
return answer.set_result(None)

Expand Down
1 change: 1 addition & 0 deletions tchannel/tornado/message_factory.py
Expand Up @@ -42,6 +42,7 @@
from .request import Request
from .response import Response
from .stream import InMemStream
from six.moves import range

log = logging.getLogger('tchannel')

Expand Down
12 changes: 6 additions & 6 deletions tchannel/tornado/peer.py
Expand Up @@ -115,7 +115,7 @@ def __init__(self, tchannel, hostport, rank=None, on_conn_change=None):
if rank is not None:
self.rank = rank
else:
self.rank = sys.maxint
self.rank = sys.maxsize
# index records the position of the peer in the peer heap
self.index = -1
# order maintains the push order of the peer in the heap.
Expand Down Expand Up @@ -397,7 +397,7 @@ def send(

# set default transport headers
headers = headers or {}
for k, v in self.headers.iteritems():
for k, v in self.headers.items():
headers.setdefault(k, v)

if self.tracing_span is None:
Expand Down Expand Up @@ -497,7 +497,7 @@ def send_with_retry(self, request, peer, retry_limit, connection):
)

if not connection:
raise typ, error, tb
six.reraise(typ, error, tb)
finally:
del tb # for GC

Expand Down Expand Up @@ -636,7 +636,7 @@ def get(self, hostport):
existing Peer is returned.
"""
assert hostport, "hostport is required"
assert isinstance(hostport, basestring), "hostport must be a string"
assert isinstance(hostport, six.string_types), "hostport must be a string" # noqa

if hostport not in self._peers:
self._add(hostport)
Expand Down Expand Up @@ -687,12 +687,12 @@ def _get_isolated(self, hostport):
@property
def hosts(self):
"""Get all host-ports managed by this PeerGroup."""
return self._peers.keys()
return list(self._peers.keys())

@property
def peers(self):
"""Get all Peers managed by this PeerGroup."""
return self._peers.values()
return list(self._peers.values())

def request(self, service, hostport=None, **kwargs):
"""Initiate a new request through this PeerGroup.
Expand Down
4 changes: 3 additions & 1 deletion tchannel/tornado/stream.py
Expand Up @@ -18,6 +18,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

from __future__ import absolute_import
from collections import deque

import tornado
Expand All @@ -31,6 +32,7 @@
from ..errors import UnexpectedError
from ..messages import common
from ..messages.common import StreamState
import six


@tornado.gen.coroutine
Expand Down Expand Up @@ -266,7 +268,7 @@ def maybe_stream(s):
stream.close() # we don't intend to write anything
return stream

if isinstance(s, unicode):
if isinstance(s, six.text_type):
s = s.encode('utf-8')
if isinstance(s, bytearray):
s = bytes(s)
Expand Down
6 changes: 3 additions & 3 deletions tchannel/tracing.py
Expand Up @@ -151,7 +151,7 @@ def start_span(self, request, headers, peer_host, peer_port):
if headers and hasattr(headers, 'iteritems'):
tracing_headers = {
k[len(TRACING_KEY_PREFIX):]: v
for k, v in headers.iteritems()
for k, v in headers.items()
if k.startswith(TRACING_KEY_PREFIX)
}
parent_context = self.tracer.extract(
Expand All @@ -161,7 +161,7 @@ def start_span(self, request, headers, peer_host, peer_port):
if self.span and parent_context:
# we already started a span from Tracing fields,
# so only copy baggage from the headers.
for k, v in parent_context.baggage.iteritems():
for k, v in parent_context.baggage.items():
self.span.set_baggage_item(k, v)
except:
log.exception('Cannot extract tracing span from headers')
Expand Down Expand Up @@ -212,7 +212,7 @@ def start_span(self, service, endpoint, headers=None,
tracing_headers = {}
self.channel.tracer.inject(
span.context, opentracing.Format.TEXT_MAP, tracing_headers)
for k, v in tracing_headers.iteritems():
for k, v in six.iteritems(tracing_headers):
headers[TRACING_KEY_PREFIX + k] = v
except:
log.exception('Failed to inject tracing span into headers')
Expand Down
4 changes: 2 additions & 2 deletions tests/container/test_heap.py
Expand Up @@ -168,7 +168,7 @@ def test_heap_fuzz(int_heap):
for _ in six.moves.range(random.randint(1, 100000)):
ops = random.randint(0, 1)
if ops == 0: # push
heap.push(int_heap, random.randint(0, sys.maxint))
heap.push(int_heap, random.randint(0, sys.maxsize))
elif ops == 1: # pop
if int_heap.size():
heap.pop(int_heap)
Expand All @@ -180,7 +180,7 @@ def test_heap_fuzz(int_heap):


def smallest(vs):
m = sys.maxint
m = sys.maxsize
for value in vs:
if value < m:
m = value
Expand Down

0 comments on commit c37c63a

Please sign in to comment.