Skip to content

Commit c1b4607

Browse files
committed
python3 support, now properly handling unicode.
1 parent cba8442 commit c1b4607

File tree

9 files changed

+17
-8
lines changed

9 files changed

+17
-8
lines changed

examples/debug.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

examples/encrypted.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

examples/kitchen-sink.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

examples/message-signing.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

examples/remote.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

examples/simple.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

examples/udp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
45
import sys, os
56

67
curdir = os.path.abspath(os.path.dirname(__file__))

loghog.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
from __future__ import print_function
2+
from __future__ import print_function, unicode_literals
33
import socket, hmac, hashlib, struct, zlib, ssl, random, select
44
import logging, logging.handlers
55
from collections import deque
@@ -130,7 +130,7 @@ def _encode(self, record):
130130

131131
if self.secret:
132132
hashable_fields = ['app_id', 'module', 'stamp', 'nsecs', 'body']
133-
hashable = u''.join(str(data[field]).decode('utf-8') for field in hashable_fields).encode('utf-8')
133+
hashable = ''.join('{0}'.format(data[field]) for field in hashable_fields).encode('utf-8')
134134
data['signature'] = hmac.new(self.secret.encode('utf-8'), hashable, self.HMAC_DIGEST_ALGO).hexdigest()
135135

136136
payload = json.dumps(data).encode('utf-8')
@@ -139,7 +139,7 @@ def _encode(self, record):
139139
payload = zlib.compress(payload)
140140

141141
size = len(payload)
142-
return struct.pack(self.FORMAT_PROTO.format(size), size, self.flags, payload)
142+
return struct.pack(self.FORMAT_PROTO.format(size).encode('ascii'), size, self.flags, payload)
143143

144144
def emit(self, record):
145145
'''Encodes and sends the messge over the network.'''

tests.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
from __future__ import unicode_literals
5+
46
'''
57
This module contains unit test for the Python LogHog client.
68
'''
@@ -10,9 +12,9 @@
1012

1113
class LoghogClientTest(unittest.TestCase):
1214

13-
HEADER_FORMAT = '!LL'
15+
HEADER_FORMAT = b'!LL'
1416
HEADER_SIZE = struct.calcsize(HEADER_FORMAT)
15-
MSG_FORMAT_PROTO = '%ds'
17+
MSG_FORMAT_PROTO = '{0}s'
1618

1719
REQUIRED_FIELDS = ['version', 'stamp', 'nsecs', 'app_id', 'module', 'body', ]
1820
HASHABLE_FIELDS = ['app_id', 'module', 'stamp', 'nsecs', 'body']
@@ -34,7 +36,7 @@ def verify_signature(self, secret, msg):
3436
if secret:
3537
self.assertTrue('signature' in msg, 'Security alert: message signature is required but not present')
3638

37-
hashable = u''.join(unicode(msg[field]) for field in self.HASHABLE_FIELDS).encode('utf-8')
39+
hashable = ''.join('{0}'.format(msg[field]) for field in self.HASHABLE_FIELDS).encode('utf-8')
3840
signature = hmac.new(secret.encode('utf-8'), hashable, self.HMAC_DIGEST_ALGO).hexdigest()
3941

4042
self.assertEqual(signature, msg['signature'], "Message signature is invalid.")
@@ -51,11 +53,11 @@ def parse_message(self, data):
5153
def check_message_content(self, msg):
5254
self.assertEqual(msg['app_id'], 'test-app')
5355
self.assertEqual(msg['module'], 'web.requests')
54-
self.assertEqual(msg['body'], u"That is one hot jalapño!")
56+
self.assertEqual(msg['body'], "That is one hot jalapño!")
5557

5658
def unpack_payload(self, data):
5759
size, flags = struct.unpack(self.HEADER_FORMAT, data[:self.HEADER_SIZE])
58-
return struct.unpack(self.MSG_FORMAT_PROTO % size, data[self.HEADER_SIZE:size+self.HEADER_SIZE])[0]
60+
return struct.unpack(self.MSG_FORMAT_PROTO.format(size).encode('ascii'), data[self.HEADER_SIZE:size+self.HEADER_SIZE])[0]
5961

6062
def test_encode(self):
6163
handler = LoghogHandler('test-app')

0 commit comments

Comments
 (0)