def _ensureBytes(val):
            """
            Ensure that C{val} is bytes, encoding using UTF-8 if
            needed.

            @param val: L{bytes} or L{unicode}

            @return: L{bytes}
            """
            if val is None:
                # It's None, so we don't want to touch it
                return val

            if isinstance(val, bytes):
                return val
            else:
                return val.encode('utf8')

            Ensure that C{val} is bytes, encoding using UTF-8 if
            needed.

            @param val: L{bytes} or L{unicode}

            @return: L{bytes}
            
stackoverflow/venv/lib/python3.6/site-packages/twisted/web/http.py