From ed893c3bcfdf54f440b98b958c86fc0c13573b6f Mon Sep 17 00:00:00 2001 From: Dana Powers Date: Thu, 11 Dec 2014 16:35:15 -0800 Subject: Use kafka.util.kafka_bytestring to encode utf-8 when necessary --- kafka/util.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'kafka/util.py') diff --git a/kafka/util.py b/kafka/util.py index 1e03cf1..72ac521 100644 --- a/kafka/util.py +++ b/kafka/util.py @@ -86,6 +86,18 @@ def group_by_topic_and_partition(tuples): return out +def kafka_bytestring(s): + """ + Takes a string or bytes instance + Returns bytes, encoding strings in utf-8 as necessary + """ + if isinstance(s, six.binary_type): + return s + if isinstance(s, six.string_types): + return s.encode('utf-8') + raise TypeError(s) + + class ReentrantTimer(object): """ A timer that can be restarted, unlike threading.Timer -- cgit v1.2.1