def bytes_to_string(data, headers, **_): """Convert all *data* and *headers* bytes to strings. Binary data that cannot be decoded is converted to a hexadecimal representation via :func:`binascii.hexlify`. :param iterable data: An :term:`iterable` (e.g. list) of rows. :param iterable headers: The column headers. :return: The processed data and headers. :rtype: tuple """ return (([utils.bytes_to_string(v) for v in row] for row in data), [utils.bytes_to_string(h) for h in headers])
def test_bytes_to_string_hexlify(): """Test that bytes_to_string() hexlifies binary data.""" assert utils.bytes_to_string(b'\xff') == '0xff'
def test_bytes_to_string_non_bytes(): """Test that bytes_to_string() returns non-bytes untouched.""" assert utils.bytes_to_string('abc') == 'abc' assert utils.bytes_to_string(1) == 1
def test_bytes_to_string_decode_bytes(): """Test that bytes_to_string() decodes bytes.""" assert utils.bytes_to_string(b'foobar') == 'foobar'
def test_bytes_to_string_unprintable(): """Test that bytes_to_string() hexlifies data that is valid unicode, but unprintable.""" assert utils.bytes_to_string(b"\0") == "0x00" assert utils.bytes_to_string(b"\1") == "0x01" assert utils.bytes_to_string(b"a\0") == "0x6100"