def cstrencode(pystr):
    """
    encode a string into bytes.  If already bytes, do nothing.
    """
    try:
        return pystr.encode('utf-8')
    except UnicodeDecodeError:
        return pystr.decode('utf-8').encode('utf-8')
    except AttributeError:
        return pystr
