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

Commit c37c63a

Browse files
westoverabhinav
authored andcommitted
Python3 compatibility part 1: Cure errors in local build (#488)
1 parent a2a86ac commit c37c63a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+7344
-7293
lines changed

crossdock/server/api.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def downstream_from_dict(json):
6262

6363
def namedtuple_to_dict(tpl):
6464
json = {}
65-
for k, v in tpl._asdict().iteritems():
65+
for k, v in tpl._asdict().items():
6666
if hasattr(v, '_asdict'):
6767
v = namedtuple_to_dict(v)
6868
json[k] = v

requirements-test.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ futures
3333
flake8==2.2.5
3434

3535
# Optional dependency, but must be tested er'ry time
36-
thrift==0.9.2
36+
thrift==0.11.0
3737

3838
# Smarter decorators
3939
wrapt>=1.10,<1.11

tchannel/enum.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@
2424

2525

2626
def enum(class_name, **values):
27-
class_type = collections.namedtuple(class_name, values.keys())
27+
class_type = collections.namedtuple(class_name, list(values.keys()))
2828
instance = class_type(**values)
2929
return instance

tchannel/errors.py

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class TChannelError(Exception):
6161
"""
6262

6363
__slots__ = (
64-
'code',
6564
'description',
6665
'id',
6766
'tracing',

tchannel/messages/call_continue.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from . import common
2424
from .base import BaseMessage
2525
from .common import FlagsType
26+
from six.moves import range
2627

2728

2829
class CallContinueMessage(BaseMessage):

tchannel/net.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def local_ip():
5454
if ip.startswith('127.'):
5555
# Check eth0, eth1, eth2, en0, ...
5656
interfaces = [
57-
i + str(n) for i in ("eth", "en", "wlan") for n in xrange(3)
57+
i + str(n) for i in ("eth", "en", "wlan") for n in range(3)
5858
] # :(
5959
for interface in interfaces:
6060
try:

tchannel/peer_strategy.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class PreferIncomingCalculator(RankCalculator):
3636
# 0: ephemeral peers or unconnected peers
3737
# 1: peers with only outgoing connections
3838
# 2: peers with incoming connections
39-
TIERS = [sys.maxint, sys.maxint / 2, 0]
39+
TIERS = [sys.maxsize, sys.maxsize / 2, 0]
4040

4141
def get_rank(self, peer):
4242
"""Calculate the peer rank based on connections.

tchannel/rw.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ def read(self, stream):
597597
def write(self, headers, stream):
598598
# In case someone does write({..}, stream)
599599
if isinstance(headers, dict):
600-
headers = headers.items()
600+
headers = list(headers.items())
601601

602602
self._length.write(len(headers), stream)
603603
for pair in headers:
@@ -610,7 +610,7 @@ def width(self):
610610
def length(self, headers):
611611
size = 0
612612
if isinstance(headers, dict):
613-
headers = headers.items()
613+
headers = list(headers.items())
614614

615615
size += self._length.length(len(headers))
616616
for pair in headers:

tchannel/serializer/json.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import json
2424

2525
from tchannel.schemes import JSON
26+
import six
2627

2728

2829
class JsonSerializer(object):
@@ -31,8 +32,9 @@ class JsonSerializer(object):
3132
def serialize_header(self, headers):
3233
headers = headers or {}
3334

34-
for k, v in headers.iteritems():
35-
if not (isinstance(k, basestring) and isinstance(v, basestring)):
35+
for k, v in six.iteritems(headers):
36+
if not (isinstance(k, six.string_types) and
37+
isinstance(v, six.string_types)):
3638
raise ValueError(
3739
'headers must be a map[string]string (a shallow dict '
3840
'where keys and values are strings)'

tchannel/tcurl.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
--body '{"nyuck": "nyuck"}'
4141
"""
4242

43-
from __future__ import absolute_import
43+
from __future__ import absolute_import, print_function
4444

4545
import argparse
4646
import logging
@@ -262,9 +262,9 @@ def main(argv=None):
262262
)
263263

264264
if not args.raw:
265-
print json.dumps(result.body, default=_dictify)
265+
print(json.dumps(result.body, default=_dictify))
266266
else:
267-
print result.body
267+
print(result.body)
268268

269269
raise tornado.gen.Return(result)
270270

@@ -273,11 +273,11 @@ def main(argv=None):
273273
def _catch_errors(future, verbose, exit=sys.exit):
274274
try:
275275
result = yield future
276-
except Exception, e:
276+
except Exception as e:
277277
if verbose:
278278
traceback.print_exc(file=sys.stderr)
279279
else:
280-
print >> sys.stderr, str(e)
280+
print(str(e), file=sys.stderr)
281281
exit(1)
282282

283283
raise tornado.gen.Return(result)

tchannel/testing/vcr/cassette.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from . import proxy
3838
from tchannel import tracing
3939
from tchannel.serializer.thrift import ThriftSerializer
40+
import six
4041

4142
__all__ = ['Cassette', 'DEFAULT_MATCHERS']
4243

@@ -73,7 +74,7 @@ def matcher(left, right):
7374

7475
def filter_headers(hs):
7576
return {
76-
k: v for k, v in hs.iteritems()
77+
k: v for k, v in six.iteritems(hs)
7778
if not k.startswith(tracing.TRACING_KEY_PREFIX)
7879
}
7980

@@ -170,7 +171,7 @@ def __init__(self, path, record_mode=None, matchers=None, serializer=None):
170171
"""
171172
# TODO move documentation around
172173
record_mode = record_mode or RecordMode.ONCE
173-
if isinstance(record_mode, basestring):
174+
if isinstance(record_mode, six.string_types):
174175
record_mode = RecordMode.from_name(record_mode)
175176

176177
self.path = path

tchannel/testing/vcr/patch.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from tchannel.tornado.stream import read_full
3535

3636
from . import proxy
37+
from six.moves import map
3738

3839

3940
_TChannel_request = TChannel.request
@@ -70,7 +71,7 @@ def send(self, arg1, arg2, arg3,
7071
headers=None,
7172
ttl=None,
7273
**kwargs):
73-
arg1, arg2, arg3 = map(maybe_stream, [arg1, arg2, arg3])
74+
arg1, arg2, arg3 = list(map(maybe_stream, [arg1, arg2, arg3]))
7475

7576
endpoint = yield read_full(arg1)
7677

tchannel/testing/vcr/proxy.py

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
# THE SOFTWARE.
2020

21+
from __future__ import absolute_import
2122
import os
2223
import sys
2324

tchannel/thrift/module.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from tchannel.serializer.thrift import ThriftSerializer
3232

3333
from .reflection import get_service_methods, get_module_name
34+
import six
3435

3536

3637
@deprecated(
@@ -97,7 +98,7 @@ def thrift_request_builder(service, thrift_module, hostport=None,
9798
methods = _create_methods(thrift_module)
9899

99100
# then attach to instane
100-
for name, method in methods.iteritems():
101+
for name, method in six.iteritems(methods):
101102
method = types.MethodType(method, maker, ThriftRequestMaker)
102103
setattr(maker, name, method)
103104

tchannel/thrift/server.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import sys
2424
from collections import namedtuple
25+
import six
2526

2627
from tornado import gen
2728

@@ -245,4 +246,4 @@ def write_exc_info(self, exc_info=None):
245246
return
246247

247248
# Re-raise the exception (with the same traceback) if it didn't match.
248-
raise exc_info[0], exc_info[1], exc_info[2]
249+
six.reraise(exc_info[0], exc_info[1], exc_info[2])

tchannel/tornado/connection.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
from .message_factory import build_raw_error_message
5555
from .message_factory import MessageFactory
5656
from .tombstone import Cemetery
57+
import six
5758

5859
log = logging.getLogger('tchannel')
5960

@@ -171,7 +172,7 @@ def _on_close(self):
171172
self.closed = True
172173
self._request_tombstones.clear()
173174

174-
for message_id, future in self._outbound_pending_call.iteritems():
175+
for message_id, future in six.iteritems(self._outbound_pending_call):
175176
future.set_exception(
176177
NetworkError(
177178
"canceling outstanding request %d" % message_id
@@ -335,7 +336,7 @@ def _write_fragment(future):
335336
return answer.set_exc_info(future.exc_info())
336337

337338
try:
338-
fragment = fragments.next()
339+
fragment = next(fragments)
339340
except StopIteration:
340341
return answer.set_result(None)
341342

tchannel/tornado/message_factory.py

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
from .request import Request
4343
from .response import Response
4444
from .stream import InMemStream
45+
from six.moves import range
4546

4647
log = logging.getLogger('tchannel')
4748

tchannel/tornado/peer.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __init__(self, tchannel, hostport, rank=None, on_conn_change=None):
115115
if rank is not None:
116116
self.rank = rank
117117
else:
118-
self.rank = sys.maxint
118+
self.rank = sys.maxsize
119119
# index records the position of the peer in the peer heap
120120
self.index = -1
121121
# order maintains the push order of the peer in the heap.
@@ -397,7 +397,7 @@ def send(
397397

398398
# set default transport headers
399399
headers = headers or {}
400-
for k, v in self.headers.iteritems():
400+
for k, v in self.headers.items():
401401
headers.setdefault(k, v)
402402

403403
if self.tracing_span is None:
@@ -497,7 +497,7 @@ def send_with_retry(self, request, peer, retry_limit, connection):
497497
)
498498

499499
if not connection:
500-
raise typ, error, tb
500+
six.reraise(typ, error, tb)
501501
finally:
502502
del tb # for GC
503503

@@ -636,7 +636,7 @@ def get(self, hostport):
636636
existing Peer is returned.
637637
"""
638638
assert hostport, "hostport is required"
639-
assert isinstance(hostport, basestring), "hostport must be a string"
639+
assert isinstance(hostport, six.string_types), "hostport must be a string" # noqa
640640

641641
if hostport not in self._peers:
642642
self._add(hostport)
@@ -687,12 +687,12 @@ def _get_isolated(self, hostport):
687687
@property
688688
def hosts(self):
689689
"""Get all host-ports managed by this PeerGroup."""
690-
return self._peers.keys()
690+
return list(self._peers.keys())
691691

692692
@property
693693
def peers(self):
694694
"""Get all Peers managed by this PeerGroup."""
695-
return self._peers.values()
695+
return list(self._peers.values())
696696

697697
def request(self, service, hostport=None, **kwargs):
698698
"""Initiate a new request through this PeerGroup.

tchannel/tornado/stream.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
# THE SOFTWARE.
2020

21+
from __future__ import absolute_import
2122
from collections import deque
2223

2324
import tornado
@@ -31,6 +32,7 @@
3132
from ..errors import UnexpectedError
3233
from ..messages import common
3334
from ..messages.common import StreamState
35+
import six
3436

3537

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

269-
if isinstance(s, unicode):
271+
if isinstance(s, six.text_type):
270272
s = s.encode('utf-8')
271273
if isinstance(s, bytearray):
272274
s = bytes(s)

tchannel/tracing.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def start_span(self, request, headers, peer_host, peer_port):
151151
if headers and hasattr(headers, 'iteritems'):
152152
tracing_headers = {
153153
k[len(TRACING_KEY_PREFIX):]: v
154-
for k, v in headers.iteritems()
154+
for k, v in headers.items()
155155
if k.startswith(TRACING_KEY_PREFIX)
156156
}
157157
parent_context = self.tracer.extract(
@@ -161,7 +161,7 @@ def start_span(self, request, headers, peer_host, peer_port):
161161
if self.span and parent_context:
162162
# we already started a span from Tracing fields,
163163
# so only copy baggage from the headers.
164-
for k, v in parent_context.baggage.iteritems():
164+
for k, v in parent_context.baggage.items():
165165
self.span.set_baggage_item(k, v)
166166
except:
167167
log.exception('Cannot extract tracing span from headers')
@@ -212,7 +212,7 @@ def start_span(self, service, endpoint, headers=None,
212212
tracing_headers = {}
213213
self.channel.tracer.inject(
214214
span.context, opentracing.Format.TEXT_MAP, tracing_headers)
215-
for k, v in tracing_headers.iteritems():
215+
for k, v in six.iteritems(tracing_headers):
216216
headers[TRACING_KEY_PREFIX + k] = v
217217
except:
218218
log.exception('Failed to inject tracing span into headers')

tests/container/test_heap.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def test_heap_fuzz(int_heap):
168168
for _ in six.moves.range(random.randint(1, 100000)):
169169
ops = random.randint(0, 1)
170170
if ops == 0: # push
171-
heap.push(int_heap, random.randint(0, sys.maxint))
171+
heap.push(int_heap, random.randint(0, sys.maxsize))
172172
elif ops == 1: # pop
173173
if int_heap.size():
174174
heap.pop(int_heap)
@@ -180,7 +180,7 @@ def test_heap_fuzz(int_heap):
180180

181181

182182
def smallest(vs):
183-
m = sys.maxint
183+
m = sys.maxsize
184184
for value in vs:
185185
if value < m:
186186
m = value

0 commit comments

Comments
 (0)