Skip to content
This repository was archived by the owner on Jul 11, 2022. It is now read-only.

Commit 0392cce

Browse files
MrSaintsyurishkuro
authored andcommitted
Futurize for Py3 (#57)
* [py3] futurize(stage1): modernized py2 code w/o dependencies Tests are passing. * [py3] futurize(stage2): builtins, and utilities from `future` This is to introduce more safe changes to support Python 3. It is likely to result in tests failures. * [py3] futurize(stage2): fix broken tests * [py3] fix `TypeError: a bytes-like object is required, not 'str'` Use `six.PY3` to force non-unicode strings in Python 2. * [py3] Add `future` as a dependency * [py3] Replace unnecessary `old_div` usage with `/` * [py3] Replace `iteritems()` / `iterkeys()` -> `six.iteritems()` / `six.iterkeys()` This is in response to CR feedback as using `items()`, and `keys()` in Py2 will result in the creation of new arrays. Thus, this commit will use proxy methods from `six` instead. * [py3] fix linting errors due to import order * [py3] remove unused `urllib` dependencies from `codecs.py`, and fix tests * [py3] revert uses of `old_div` in favour of standard `/` divisor * [py3] Revert change from `long` -> `int` Use `long` for py2, and `int` for py3. Otherwise we will get an integer overflow for py2 32bit.
1 parent 19e7742 commit 0392cce

23 files changed

+101
-54
lines changed

jaeger_client/codecs.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,26 @@
2020

2121
from __future__ import absolute_import
2222

23-
import urllib
23+
from future import standard_library
24+
standard_library.install_aliases() # noqa
25+
26+
from builtins import object
27+
from past.builtins import basestring
2428

2529
from opentracing import (
2630
InvalidCarrierException,
2731
SpanContextCorruptedException,
2832
)
2933
from .constants import (
30-
TRACE_ID_HEADER,
3134
BAGGAGE_HEADER_PREFIX,
3235
DEBUG_ID_HEADER_KEY,
36+
TRACE_ID_HEADER,
3337
)
3438
from .span_context import SpanContext
3539

40+
import six
41+
import urllib.parse
42+
3643

3744
class Codec(object):
3845
def inject(self, span_context, carrier):
@@ -64,9 +71,9 @@ def inject(self, span_context, carrier):
6471
parent_id=span_context.parent_id, flags=span_context.flags)
6572
baggage = span_context.baggage
6673
if baggage:
67-
for key, value in baggage.iteritems():
74+
for key, value in six.iteritems(baggage):
6875
if self.url_encoding:
69-
encoded_value = urllib.quote(value)
76+
encoded_value = urllib.parse.quote(value)
7077
else:
7178
encoded_value = value
7279
carrier['%s%s' % (self.baggage_prefix, key)] = encoded_value
@@ -77,24 +84,24 @@ def extract(self, carrier):
7784
trace_id, span_id, parent_id, flags = None, None, None, None
7885
baggage = None
7986
debug_id = None
80-
for key, value in carrier.iteritems():
87+
for key, value in six.iteritems(carrier):
8188
uc_key = key.lower()
8289
if uc_key == self.trace_id_header:
8390
if self.url_encoding:
84-
value = urllib.unquote(value)
91+
value = urllib.parse.unquote(value)
8592
trace_id, span_id, parent_id, flags = \
8693
span_context_from_string(value)
8794
elif uc_key.startswith(self.baggage_prefix):
8895
if self.url_encoding:
89-
value = urllib.unquote(value)
96+
value = urllib.parse.unquote(value)
9097
attr_key = key[self.prefix_length:]
9198
if baggage is None:
9299
baggage = {attr_key.lower(): value}
93100
else:
94101
baggage[attr_key.lower()] = value
95102
elif uc_key == self.debug_id_header:
96103
if self.url_encoding:
97-
value = urllib.unquote(value)
104+
value = urllib.parse.unquote(value)
98105
debug_id = value
99106
if not trace_id and baggage:
100107
raise SpanContextCorruptedException('baggage without trace ctx')
@@ -137,7 +144,7 @@ def span_context_to_string(trace_id, span_id, parent_id, flags):
137144
:param parent_id:
138145
:param flags:
139146
"""
140-
parent_id = parent_id or 0L
147+
parent_id = parent_id or 0
141148
return '{:x}:{:x}:{:x}:{:x}'.format(trace_id, span_id, parent_id, flags)
142149

143150

@@ -162,9 +169,9 @@ def span_context_from_string(value):
162169
raise SpanContextCorruptedException(
163170
'malformed trace context "%s"' % value)
164171
try:
165-
trace_id = long(parts[0], 16)
166-
span_id = long(parts[1], 16)
167-
parent_id = long(parts[2], 16)
172+
trace_id = int(parts[0], 16)
173+
span_id = int(parts[1], 16)
174+
parent_id = int(parts[2], 16)
168175
flags = int(parts[3], 16)
169176
if trace_id < 1 or span_id < 1 or parent_id < 0 or flags < 0:
170177
raise SpanContextCorruptedException(

jaeger_client/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from __future__ import absolute_import
2222

23+
from builtins import object
2324
import logging
2425
import threading
2526

jaeger_client/constants.py

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

2323
from . import __version__
2424

25+
import six
2526

2627
# Max number of bits to use when generating random ID
2728
MAX_ID_BITS = 64
@@ -33,10 +34,10 @@
3334
DEFAULT_FLUSH_INTERVAL = 1
3435

3536
# Name of the HTTP header used to encode trace ID
36-
TRACE_ID_HEADER = b'uber-trace-id'
37+
TRACE_ID_HEADER = 'uber-trace-id' if six.PY3 else b'uber-trace-id'
3738

3839
# Prefix for HTTP headers used to record baggage items
39-
BAGGAGE_HEADER_PREFIX = b'uberctx-'
40+
BAGGAGE_HEADER_PREFIX = 'uberctx-' if six.PY3 else b'uberctx-'
4041

4142
# The name of HTTP header or a TextMap carrier key which, if found in the
4243
# carrier, forces the trace to be sampled as "debug" trace. The value of the

jaeger_client/local_agent_net.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# THE SOFTWARE.
2020

2121
from __future__ import absolute_import
22+
from builtins import object
2223
from threadloop import ThreadLoop
2324
import tornado
2425
import tornado.httpclient

jaeger_client/metrics.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
# THE SOFTWARE.
2020

2121
from __future__ import absolute_import
22+
from __future__ import division
23+
24+
from builtins import str
25+
from builtins import object
26+
27+
import six
2228

2329

2430
class MetricsFactory(object):
@@ -93,7 +99,7 @@ def _get_key(self, name, tags=None):
9399
if not tags:
94100
return name
95101
key = name
96-
for k in sorted(tags.iterkeys()):
102+
for k in sorted(six.iterkeys(tags)):
97103
key = key + '.' + str(k) + '_' + str(tags[k])
98104
return key
99105

jaeger_client/rate_limiter.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 builtins import object
2122
import time
2223

2324

jaeger_client/reporter.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# THE SOFTWARE.
2020

2121
from __future__ import absolute_import
22+
from builtins import object
2223
import logging
2324
import threading
2425

@@ -225,7 +226,7 @@ def _flush(self):
225226
yield self.queue.join()
226227

227228

228-
class ReporterMetrics:
229+
class ReporterMetrics(object):
229230
def __init__(self, metrics_factory):
230231
self.reporter_success = \
231232
metrics_factory.create_counter(name='jaeger.spans', tags={'reported': 'true'})

jaeger_client/sampler.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@
1919
# THE SOFTWARE.
2020

2121
from __future__ import absolute_import
22+
from __future__ import division
23+
from builtins import object
24+
import json
2225
import logging
2326
import random
24-
import json
27+
import six
2528

2629
from threading import Lock
2730
from tornado.ioloop import PeriodicCallback
@@ -306,7 +309,7 @@ def update(self, strategies):
306309
ProbabilisticSampler(self.default_sampling_probability)
307310

308311
def close(self):
309-
for _, sampler in self.samplers.iteritems():
312+
for _, sampler in six.iteritems(self.samplers):
310313
sampler.close()
311314

312315
def __str__(self):

jaeger_client/span.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020

2121
from __future__ import absolute_import
2222

23+
from builtins import str
2324
import json
25+
import six
2426
import threading
2527
import time
2628

@@ -54,7 +56,7 @@ def __init__(self, context, tracer, operation_name,
5456
self.tags = []
5557
self.logs = []
5658
if tags:
57-
for k, v in tags.iteritems():
59+
for k, v in six.iteritems(tags):
5860
self.set_tag(k, v)
5961

6062
def set_operation_name(self, operation_name):

jaeger_client/thrift.py

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

21+
from past.builtins import basestring
22+
import six
2123
import socket
2224
import struct
2325

@@ -30,8 +32,11 @@
3032

3133
_max_signed_port = (1 << 15) - 1
3234
_max_unsigned_port = (1 << 16)
33-
_max_signed_id = (1L << 63) - 1
34-
_max_unsigned_id = (1L << 64)
35+
_max_signed_id = (1 << 63) - 1
36+
_max_unsigned_id = (1 << 64)
37+
38+
if six.PY3:
39+
long = int
3540

3641

3742
def ipv4_to_int(ipv4):

jaeger_client/tracer.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020

2121
from __future__ import absolute_import
2222

23+
from builtins import object
2324
import socket
2425

25-
import os
26-
import time
2726
import logging
27+
import os
2828
import random
29+
import time
30+
import six
2931
import opentracing
3032
from opentracing import Format, UnsupportedFormatException
3133
from opentracing.ext import tags as ext_tags
@@ -137,7 +139,7 @@ def start_span(self,
137139
if sampled:
138140
flags = SAMPLED_FLAG
139141
tags = tags or {}
140-
for k, v in sampler_tags.iteritems():
142+
for k, v in six.iteritems(sampler_tags):
141143
tags[k] = v
142144
else: # have debug id
143145
flags = SAMPLED_FLAG | DEBUG_FLAG
@@ -164,7 +166,7 @@ def start_span(self,
164166

165167
if (rpc_server or not parent_id) and (flags & SAMPLED_FLAG):
166168
# this is a first-in-process span, and is sampled
167-
for k, v in self.tags.iteritems():
169+
for k, v in six.iteritems(self.tags):
168170
span.set_tag(k, v)
169171

170172
self._emit_span_metrics(span=span, join=rpc_server)
@@ -225,7 +227,7 @@ def random_id(self):
225227
return self.random.getrandbits(constants.MAX_ID_BITS)
226228

227229

228-
class TracerMetrics:
230+
class TracerMetrics(object):
229231
"""Tracer specific metrics."""
230232

231233
def __init__(self, metrics_factory):

jaeger_client/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
# THE SOFTWARE.
20+
from builtins import bytes
21+
from builtins import range
22+
from builtins import object
2023
import fcntl
2124
import socket
2225
import struct
@@ -70,7 +73,7 @@ def local_ip():
7073
if ip.startswith('127.'):
7174
# Check eth0, eth1, eth2, en0, ...
7275
interfaces = [
73-
i + str(n) for i in ('eth', 'en', 'wlan') for n in xrange(3)
76+
i + bytes(n) for i in (b'eth', b'en', b'wlan') for n in range(3)
7477
] # :(
7578
for interface in interfaces:
7679
try:

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
'thrift',
4242
'tornado>=4.3,<5',
4343
'opentracing>=1.2.2,<1.3',
44+
"future",
4445
],
4546
# Uncomment below if need to test with unreleased version of opentracing
4647
# dependency_links=[

tests/test_TUDPTransport.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_constructor_nonblocking(self):
3939
assert t.transport_sock.gettimeout() == 0
4040

4141
def test_write(self):
42-
self.t.write('hello')
42+
self.t.write(b'hello')
4343

4444
def test_isopen_when_open(self):
4545
assert self.t.isOpen() == True
@@ -52,4 +52,4 @@ def test_close(self):
5252
self.t.close()
5353
with self.assertRaises(Exception):
5454
# Something bad should happen if we send on a closed socket..
55-
self.t.write('hello')
55+
self.t.write(b'hello')

tests/test_codecs.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def test_trace_context_from_to_string(self):
8484
from_string = span_context_from_string
8585

8686
tests = [
87-
[(256L, 127L, None, 1), '100:7f:0:1'],
88-
[(256L, 127L, 256L, 0), '100:7f:100:0'],
87+
[(256, 127, None, 1), '100:7f:0:1'],
88+
[(256, 127, 256, 0), '100:7f:100:0'],
8989
]
9090
for test in tests:
9191
ctx = test[0]
@@ -95,13 +95,13 @@ def test_trace_context_from_to_string(self):
9595
self.assertEqual(ctx_rev, ctx)
9696

9797
ctx_rev = from_string(['100:7f:100:0'])
98-
assert ctx_rev == (256L, 127L, 256L, 0), 'Array is acceptable'
98+
assert ctx_rev == (256, 127, 256, 0), 'Array is acceptable'
9999

100100
with self.assertRaises(SpanContextCorruptedException):
101101
from_string(['100:7f:100:0', 'garbage'])
102102

103103
ctx_rev = from_string(u'100:7f:100:0')
104-
assert ctx_rev == (256L, 127L, 256L, 0), 'Unicode is acceptable'
104+
assert ctx_rev == (256, 127, 256, 0), 'Unicode is acceptable'
105105

106106
def test_context_to_readable_headers(self):
107107
for url_encoding in [False, True]:
@@ -215,14 +215,14 @@ def test_context_from_large_ids(self):
215215
'Trace-ID': 'FFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFFF:FFFFFFFFFFFFFFFF:1',
216216
}
217217
context = codec.extract(headers)
218-
assert context.trace_id == 0xFFFFFFFFFFFFFFFFL
219-
assert context.trace_id == (1L << 64) - 1
218+
assert context.trace_id == 0xFFFFFFFFFFFFFFFF
219+
assert context.trace_id == (1 << 64) - 1
220220
assert context.trace_id > 0
221-
assert context.span_id == 0xFFFFFFFFFFFFFFFFL
222-
assert context.span_id == (1L << 64) - 1
221+
assert context.span_id == 0xFFFFFFFFFFFFFFFF
222+
assert context.span_id == (1 << 64) - 1
223223
assert context.span_id > 0
224-
assert context.parent_id == 0xFFFFFFFFFFFFFFFFL
225-
assert context.parent_id == (1L << 64) - 1
224+
assert context.parent_id == 0xFFFFFFFFFFFFFFFF
225+
assert context.parent_id == (1 << 64) - 1
226226
assert context.parent_id > 0
227227

228228
def test_zipkin_codec_extract(self):
@@ -322,6 +322,6 @@ def test_debug_id():
322322
span = tracer.start_span('test', child_of=context)
323323
assert span.is_debug()
324324
assert span.is_sampled()
325-
tags = filter(lambda t: t.key == debug_header, span.tags)
325+
tags = [t for t in span.tags if t.key == debug_header]
326326
assert len(tags) == 1
327327
assert tags[0].value == 'Coraline'

tests/test_crossdock.py

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from __future__ import absolute_import
2222

23+
from builtins import str
2324
import mock
2425
import json
2526
import pytest

0 commit comments

Comments
 (0)