コード例 #1
0
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])
コード例 #2
0
ファイル: test_utils.py プロジェクト: ccszwg/cli_helpers
def test_bytes_to_string_hexlify():
    """Test that bytes_to_string() hexlifies binary data."""
    assert utils.bytes_to_string(b'\xff') == '0xff'
コード例 #3
0
ファイル: test_utils.py プロジェクト: ccszwg/cli_helpers
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
コード例 #4
0
ファイル: test_utils.py プロジェクト: ccszwg/cli_helpers
def test_bytes_to_string_decode_bytes():
    """Test that bytes_to_string() decodes bytes."""
    assert utils.bytes_to_string(b'foobar') == 'foobar'
コード例 #5
0
ファイル: test_utils.py プロジェクト: dbcli/cli_helpers
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"