Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #27994: compat.as_bytes should support bytearray #27995

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions tensorflow/python/util/compat.py
Expand Up @@ -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:
Expand All @@ -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
Expand Down