Adds encoding.ensure_bytes

This commit is contained in:
Ask Solem 2011-10-29 00:09:38 +01:00
parent cdcbb52d76
commit 5b9e9e3c96
1 changed files with 7 additions and 1 deletions

View File

@ -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"):