Skip to content

Commit e289336

Browse files
committed
Merge pull request #204 from mdaniel/better-type-errors
Better type errors
2 parents c3df8a0 + 6d6dc17 commit e289336

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

kafka/util.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,22 @@
77

88

99
def write_int_string(s):
10+
if s is not None and not isinstance(s, str):
11+
raise TypeError('Expected "%s" to be str\n'
12+
'data=%s' % (type(s), repr(s)))
1013
if s is None:
1114
return struct.pack('>i', -1)
1215
else:
1316
return struct.pack('>i%ds' % len(s), len(s), s)
1417

1518

1619
def write_short_string(s):
20+
if s is not None and not isinstance(s, str):
21+
raise TypeError('Expected "%s" to be str\n'
22+
'data=%s' % (type(s), repr(s)))
1723
if s is None:
1824
return struct.pack('>h', -1)
19-
elif len(s) > 32767 and sys.version < (2,7):
25+
elif len(s) > 32767 and sys.version < (2, 7):
2026
# Python 2.6 issues a deprecation warning instead of a struct error
2127
raise struct.error(len(s))
2228
else:
@@ -117,4 +123,5 @@ def stop(self):
117123

118124
self.active.set()
119125
self.thread.join(self.t + 1)
126+
# noinspection PyAttributeOutsideInit
120127
self.timer = None

setup.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
with open('VERSION', 'r') as v:
66
__version__ = v.read().rstrip()
77

8+
89
class Tox(Command):
910

1011
user_options = []
@@ -15,7 +16,8 @@ def initialize_options(self):
1516
def finalize_options(self):
1617
pass
1718

18-
def run(self):
19+
@classmethod
20+
def run(cls):
1921
import tox
2022
sys.exit(tox.cmdline([]))
2123

@@ -24,7 +26,7 @@ def run(self):
2426
name="kafka-python",
2527
version=__version__,
2628

27-
tests_require=["tox", "mock"],
29+
tests_require=["tox", "mock", "unittest2"],
2830
cmdclass={"test": Tox},
2931

3032
packages=["kafka"],

test/test_util.py

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import os
2-
import random
1+
# -*- coding: utf-8 -*-
32
import struct
43
import unittest2
54
import kafka.util
65
import kafka.common
76

7+
88
class UtilTest(unittest2.TestCase):
99
@unittest2.skip("Unwritten")
1010
def test_relative_unpack(self):
@@ -16,6 +16,14 @@ def test_write_int_string(self):
1616
'\x00\x00\x00\x0bsome string'
1717
)
1818

19+
def test_write_int_string__unicode(self):
20+
with self.assertRaises(TypeError) as cm:
21+
kafka.util.write_int_string(u'unicode')
22+
#: :type: TypeError
23+
te = cm.exception
24+
self.assertIn('unicode', te.message)
25+
self.assertIn('to be str', te.message)
26+
1927
def test_write_int_string__empty(self):
2028
self.assertEqual(
2129
kafka.util.write_int_string(''),
@@ -43,6 +51,14 @@ def test_write_short_string(self):
4351
'\x00\x0bsome string'
4452
)
4553

54+
def test_write_short_string__unicode(self):
55+
with self.assertRaises(TypeError) as cm:
56+
kafka.util.write_short_string(u'hello')
57+
#: :type: TypeError
58+
te = cm.exception
59+
self.assertIn('unicode', te.message)
60+
self.assertIn('to be str', te.message)
61+
4662
def test_write_short_string__empty(self):
4763
self.assertEqual(
4864
kafka.util.write_short_string(''),
@@ -64,21 +80,20 @@ def test_read_short_string(self):
6480
self.assertEqual(kafka.util.read_short_string('\x00\x00', 0), ('', 2))
6581
self.assertEqual(kafka.util.read_short_string('\x00\x0bsome string', 0), ('some string', 13))
6682

67-
def test_read_int_string__insufficient_data(self):
83+
def test_read_int_string__insufficient_data2(self):
6884
with self.assertRaises(kafka.common.BufferUnderflowError):
6985
kafka.util.read_int_string('\x00\x021', 0)
7086

71-
def test_relative_unpack(self):
87+
def test_relative_unpack2(self):
7288
self.assertEqual(
7389
kafka.util.relative_unpack('>hh', '\x00\x01\x00\x00\x02', 0),
7490
((1, 0), 4)
7591
)
7692

77-
def test_relative_unpack(self):
93+
def test_relative_unpack3(self):
7894
with self.assertRaises(kafka.common.BufferUnderflowError):
7995
kafka.util.relative_unpack('>hh', '\x00', 0)
8096

81-
8297
def test_group_by_topic_and_partition(self):
8398
t = kafka.common.TopicAndPartition
8499

@@ -91,12 +106,12 @@ def test_group_by_topic_and_partition(self):
91106
]
92107

93108
self.assertEqual(kafka.util.group_by_topic_and_partition(l), {
94-
"a" : {
95-
1 : t("a", 1),
96-
2 : t("a", 2),
97-
3 : t("a", 3),
109+
"a": {
110+
1: t("a", 1),
111+
2: t("a", 2),
112+
3: t("a", 3),
98113
},
99-
"b" : {
100-
3 : t("b", 3),
114+
"b": {
115+
3: t("b", 3),
101116
}
102117
})

0 commit comments

Comments
 (0)