From 5b9e9e3c9654d8e3e05f1502fc53644ad07a83ef Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Sat, 29 Oct 2011 00:09:38 +0100 Subject: [PATCH] Adds encoding.ensure_bytes --- kombu/utils/encoding.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kombu/utils/encoding.py b/kombu/utils/encoding.py index c953ede8..72a545fa 100644 --- a/kombu/utils/encoding.py +++ b/kombu/utils/encoding.py @@ -14,7 +14,7 @@ import traceback __all__ = ["str_to_bytes", "bytes_to_str", "from_utf8", "default_encoding", "safe_str", "safe_repr", - "bytes_t", "str_t"] + "bytes_t", "str_t", "ensure_bytes"] is_py3k = sys.version_info >= (3, 0) @@ -33,6 +33,11 @@ if is_py3k: def from_utf8(s, *args, **kwargs): return s + def ensure_bytes(s): + if not isinstance(s, bytes): + return str_to_bytes(s) + return s + bytes_t = bytes str_t = str @@ -51,6 +56,7 @@ else: bytes_t = str str_t = unicode + ensure_bytes = str_to_bytes if sys.platform.startswith("java"):