Converts either bytes or unicode to `bytes`, using utf-8 encoding for text. Args: bytes_or_text: A `bytes`, `str`, or `unicode` object. encoding: A string indicating the charset for encoding unicode. Returns: A `bytes` object. Raises: TypeError: If `bytes_or_text` is not a bina
(bytes_or_text, encoding='utf-8')
| 48 | |
| 49 | |
| 50 | def as_bytes(bytes_or_text, encoding='utf-8'): |
| 51 | """Converts either bytes or unicode to `bytes`, using utf-8 encoding for text. |
| 52 | Args: |
| 53 | bytes_or_text: A `bytes`, `str`, or `unicode` object. |
| 54 | encoding: A string indicating the charset for encoding unicode. |
| 55 | Returns: |
| 56 | A `bytes` object. |
| 57 | Raises: |
| 58 | TypeError: If `bytes_or_text` is not a binary or unicode string. |
| 59 | """ |
| 60 | if isinstance(bytes_or_text, _six.text_type): |
| 61 | return bytes_or_text.encode(encoding) |
| 62 | elif isinstance(bytes_or_text, bytes): |
| 63 | return bytes_or_text |
| 64 | else: |
| 65 | raise TypeError('Expected binary or unicode string, got %r' % (bytes_or_text, )) |
| 66 | |
| 67 | |
| 68 | def as_text(bytes_or_text, encoding='utf-8'): |
no outgoing calls
no test coverage detected
searching dependent graphs…