cffi_utils/py2to3.py
def toBytes(x):
    '''
    x-->unicode string | bytearray | bytes
    Returns-->bytes
    If x is unicode, MUST have encoding=latin1
    '''
    if isinstance(x, bytes):
        return x
    elif isinstance(x, bytearray):
        return bytes(x)
    elif isinstance(x, unicode):
        pass
    else:
        return x  # unchanged (int etc)
    # ASSUMES latin1 encoding - Could raise an exception
    return encode(x, DEF_ENCODING)
def toBytes(x):
    '''
    x-->unicode string | bytearray | bytes
    Returns-->bytes
    If x is unicode, MUST have encoding=latin1
    '''
    if isinstance(x, bytes):
        return x
    elif isinstance(x, bytearray):
        return bytes(x)
    elif isinstance(x, unicode):
        pass
    else:
        return x  # unchanged (int etc)
    # ASSUMES latin1 encoding - Could raise an exception
    return encode(x, DEF_ENCODING)
x-->unicode string | bytearray | bytes
    Returns-->bytes
    If x is unicode, MUST have encoding=latin1