import six


def _tobytes(val):
    """
    Py2 and Py3 compatible function that coerces
    any non-binary string types into the official "binary type" (str in Py2, bytes in Py3).
    Values that are not a text or binary type are converted to text (unicode)
    first, then encoded via UTF-8 into binary type.
    """
    if isinstance(val, six.binary_type):
        return val
    elif isinstance(val, six.text_type):
        return val.encode('utf-8')
    else:
        return six.text_type(val).encode('utf-8')