Skip to content

Commit 4ac69f4

Browse files
committed
Decode bytestring to utf8; Add py3 basestring equivalent
1 parent cc0290a commit 4ac69f4

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

instana/util.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
import struct
55
import binascii
66

7+
import sys
8+
if sys.version_info.major is 2:
9+
string_types = basestring
10+
else:
11+
string_types = str
12+
713
_rnd = random.Random()
814
_current_pid = 0
915

@@ -26,13 +32,13 @@ def id_to_header(id):
2632
return ""
2733

2834
byteString = struct.pack('>q', id)
29-
return binascii.hexlify(byteString).lstrip('0')
35+
return binascii.hexlify(byteString).decode('UTF-8').lstrip('0')
3036

3137

3238
def header_to_id(header):
3339
""" Convert an unsigned base 16 hex string into a 64bit signed integer """
3440

35-
if not isinstance(header, basestring):
41+
if not isinstance(header, string_types):
3642
return 0
3743

3844
# Pad the header to 16 chars

tests/test_id_management.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import instana.util
22
import string
33
from nose.tools import assert_equals
4+
import sys
5+
6+
if sys.version_info.major is 2:
7+
string_types = basestring
8+
else:
9+
string_types = str
410

511

612
def test_id_generation():
@@ -72,15 +78,15 @@ def test_id_to_header_conversion():
7278
converted_id = instana.util.id_to_header(original_id)
7379

7480
# Assert that it is a string and there are no non-hex characters
75-
assert isinstance(converted_id, basestring)
81+
assert isinstance(converted_id, string_types)
7682
assert all(c in string.hexdigits for c in converted_id)
7783

7884
# Test passing a standard Integer ID as a String
7985
original_id = instana.util.generate_id()
8086
converted_id = instana.util.id_to_header(original_id)
8187

8288
# Assert that it is a string and there are no non-hex characters
83-
assert isinstance(converted_id, basestring)
89+
assert isinstance(converted_id, string_types)
8490
assert all(c in string.hexdigits for c in converted_id)
8591

8692

@@ -89,21 +95,21 @@ def test_id_to_header_conversion_with_bogus_id():
8995
converted_id = instana.util.id_to_header('')
9096

9197
# Assert that it is a string and there are no non-hex characters
92-
assert isinstance(converted_id, basestring)
98+
assert isinstance(converted_id, string_types)
9399
assert converted_id == ''
94100

95101
# Test passing a nil
96102
converted_id = instana.util.id_to_header(None)
97103

98104
# Assert that it is a string and there are no non-hex characters
99-
assert isinstance(converted_id, basestring)
105+
assert isinstance(converted_id, string_types)
100106
assert converted_id == ''
101107

102108
# Test passing an Array
103109
converted_id = instana.util.id_to_header([])
104110

105111
# Assert that it is a string and there are no non-hex characters
106-
assert isinstance(converted_id, basestring)
112+
assert isinstance(converted_id, string_types)
107113
assert converted_id == ''
108114

109115

0 commit comments

Comments
 (0)