From 08c29452f88ebc58891418b05fb50ddf24dff663 Mon Sep 17 00:00:00 2001 From: Matthew L Daniel Date: Fri, 22 Aug 2014 15:42:37 -0700 Subject: [PATCH 1/5] Correctly declare unittest2 dependency --- setup.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index d719bf4..3bf9b49 100644 --- a/setup.py +++ b/setup.py @@ -5,6 +5,7 @@ from setuptools import setup, Command with open('VERSION', 'r') as v: __version__ = v.read().rstrip() + class Tox(Command): user_options = [] @@ -15,7 +16,8 @@ class Tox(Command): def finalize_options(self): pass - def run(self): + @classmethod + def run(cls): import tox sys.exit(tox.cmdline([])) @@ -24,7 +26,7 @@ setup( name="kafka-python", version=__version__, - tests_require=["tox", "mock"], + tests_require=["tox", "mock", "unittest2"], cmdclass={"test": Tox}, packages=["kafka"], From b4bdd8eabdfd5a5df894eb3aad29743f984e20e0 Mon Sep 17 00:00:00 2001 From: Matthew L Daniel Date: Fri, 22 Aug 2014 15:44:22 -0700 Subject: [PATCH 2/5] PEP8 fixes --- test/test_util.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/test/test_util.py b/test/test_util.py index 8179b01..3f6e2f1 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -1,10 +1,10 @@ -import os -import random +# -*- coding: utf-8 -*- import struct import unittest2 import kafka.util import kafka.common + class UtilTest(unittest2.TestCase): @unittest2.skip("Unwritten") def test_relative_unpack(self): @@ -64,21 +64,20 @@ class UtilTest(unittest2.TestCase): self.assertEqual(kafka.util.read_short_string('\x00\x00', 0), ('', 2)) self.assertEqual(kafka.util.read_short_string('\x00\x0bsome string', 0), ('some string', 13)) - def test_read_int_string__insufficient_data(self): + def test_read_int_string__insufficient_data2(self): with self.assertRaises(kafka.common.BufferUnderflowError): kafka.util.read_int_string('\x00\x021', 0) - def test_relative_unpack(self): + def test_relative_unpack2(self): self.assertEqual( kafka.util.relative_unpack('>hh', '\x00\x01\x00\x00\x02', 0), ((1, 0), 4) ) - def test_relative_unpack(self): + def test_relative_unpack3(self): with self.assertRaises(kafka.common.BufferUnderflowError): kafka.util.relative_unpack('>hh', '\x00', 0) - def test_group_by_topic_and_partition(self): t = kafka.common.TopicAndPartition @@ -91,12 +90,12 @@ class UtilTest(unittest2.TestCase): ] self.assertEqual(kafka.util.group_by_topic_and_partition(l), { - "a" : { - 1 : t("a", 1), - 2 : t("a", 2), - 3 : t("a", 3), + "a": { + 1: t("a", 1), + 2: t("a", 2), + 3: t("a", 3), }, - "b" : { - 3 : t("b", 3), + "b": { + 3: t("b", 3), } }) From a27e521fd7078a551eb09160e8880df6a509a987 Mon Sep 17 00:00:00 2001 From: Matthew L Daniel Date: Fri, 22 Aug 2014 15:51:40 -0700 Subject: [PATCH 3/5] Failing test for write_int and write_short --- test/test_util.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test_util.py b/test/test_util.py index 3f6e2f1..7b5f294 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -16,6 +16,14 @@ class UtilTest(unittest2.TestCase): '\x00\x00\x00\x0bsome string' ) + def test_write_int_string__unicode(self): + with self.assertRaises(TypeError) as cm: + kafka.util.write_int_string(u'unicode') + #: :type: TypeError + te = cm.exception + self.assertIn('unicode', te.message) + self.assertIn('to be str', te.message) + def test_write_int_string__empty(self): self.assertEqual( kafka.util.write_int_string(''), @@ -43,6 +51,14 @@ class UtilTest(unittest2.TestCase): '\x00\x0bsome string' ) + def test_write_short_string__unicode(self): + with self.assertRaises(TypeError) as cm: + kafka.util.write_short_string(u'hello') + #: :type: TypeError + te = cm.exception + self.assertIn('unicode', te.message) + self.assertIn('to be str', te.message) + def test_write_short_string__empty(self): self.assertEqual( kafka.util.write_short_string(''), From bb3827eb2e13820e52c138ce81f4076199e2ae4f Mon Sep 17 00:00:00 2001 From: Matthew L Daniel Date: Fri, 22 Aug 2014 15:53:27 -0700 Subject: [PATCH 4/5] PEP8 fixes --- kafka/util.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/kafka/util.py b/kafka/util.py index a918234..09a5bbb 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -16,7 +16,7 @@ def write_int_string(s): def write_short_string(s): if s is None: return struct.pack('>h', -1) - elif len(s) > 32767 and sys.version < (2,7): + elif len(s) > 32767 and sys.version < (2, 7): # Python 2.6 issues a deprecation warning instead of a struct error raise struct.error(len(s)) else: @@ -117,4 +117,5 @@ class ReentrantTimer(object): self.active.set() self.thread.join(self.t + 1) + # noinspection PyAttributeOutsideInit self.timer = None From 6d6dc174cd1ecec6a74789562ce3d8a8f8e17261 Mon Sep 17 00:00:00 2001 From: Matthew L Daniel Date: Fri, 22 Aug 2014 16:00:06 -0700 Subject: [PATCH 5/5] Fix write_int and write_short type validation It will still die, just as before, but it now includes a *helpful* error message --- kafka/util.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kafka/util.py b/kafka/util.py index 09a5bbb..9121374 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -7,6 +7,9 @@ from kafka.common import BufferUnderflowError def write_int_string(s): + if s is not None and not isinstance(s, str): + raise TypeError('Expected "%s" to be str\n' + 'data=%s' % (type(s), repr(s))) if s is None: return struct.pack('>i', -1) else: @@ -14,6 +17,9 @@ def write_int_string(s): def write_short_string(s): + if s is not None and not isinstance(s, str): + raise TypeError('Expected "%s" to be str\n' + 'data=%s' % (type(s), repr(s))) if s is None: return struct.pack('>h', -1) elif len(s) > 32767 and sys.version < (2, 7):