diff --git a/tensorflow/python/util/compat.py b/tensorflow/python/util/compat.py index 79c7b89db4bb7d..4f31498815fb79 100644 --- a/tensorflow/python/util/compat.py +++ b/tensorflow/python/util/compat.py @@ -40,12 +40,12 @@ def as_bytes(bytes_or_text, encoding='utf-8'): - """Converts either `bytes` or unicode python input types to `bytes`. + """Converts `bytearray`, `bytes`, or unicode python input types to `bytes`. Uses utf-8 encoding for text by default. Args: - bytes_or_text: A `bytes`, `str`, or `unicode` object. + bytes_or_text: A `bytearray`, `bytes`, `str`, or `unicode` object. encoding: A string indicating the charset for encoding unicode. Returns: @@ -54,7 +54,9 @@ def as_bytes(bytes_or_text, encoding='utf-8'): Raises: TypeError: If `bytes_or_text` is not a binary or unicode string. """ - if isinstance(bytes_or_text, _six.text_type): + if isinstance(bytes_or_text, bytearray): + return bytes(bytes_or_text) + elif isinstance(bytes_or_text, _six.text_type): return bytes_or_text.encode(encoding) elif isinstance(bytes_or_text, bytes): return bytes_or_text