# Buggy code
def handle_msg(self, message):
    """Handle one message"""
    msg_type, _id, file, msg = [message[k] for k in ("type", "id", "file", "msg")]
    logger.debug("Got request id {0}: {1} for file {2}".format(_id, msg_type, file))
    if msg_type == LSPRequestTypes.DOCUMENT_DID_OPEN:
        self.file_tokens[file] = {
            "text": msg["text"],
            "offset": msg["offset"],
            "language": msg["language"],
        }
    elif msg_type == LSPRequestTypes.DOCUMENT_DID_CHANGE:
        if file not in self.file_tokens:
            self.file_tokens[file] = {
                "text": "",
                "offset": msg["offset"],
                "language": msg["language"],
            }
        diff = msg["diff"]
        text = self.file_tokens[file]
        text["offset"] = msg["offset"]
        text, _ = self.diff_patch.patch_apply(diff, text["text"])
        self.file_tokens[file]["text"] = text
    elif msg_type == LSPRequestTypes.DOCUMENT_DID_CLOSE:
        self.file_tokens.pop(file, {})
    elif msg_type == LSPRequestTypes.DOCUMENT_COMPLETION:
        tokens = []
        if file in self.file_tokens:
            text_info = self.file_tokens[file]
            tokens = self.tokenize(
                text_info["text"], text_info["offset"], text_info["language"]
            )
        tokens = {"params": tokens}
        self.sig_set_tokens.emit(_id, tokens)


# Traceback (UnicodeEncodeError)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 59: ordinal not in range(128)
Traceback (most recent call last):
File "/Users/gpena-castellanos/Google Drive/develop/quansight/spyder/spyder/plugins/completion/kite/client.py", line 131, in perform_request
response = handler(params)
File "/Users/gpena-castellanos/Google Drive/develop/quansight/spyder/spyder/plugins/completion/kite/decorators.py", line 20, in wrapper
params = req(self, *args, **kwargs)
File "/Users/gpena-castellanos/Google Drive/develop/quansight/spyder/spyder/plugins/completion/kite/providers/document.py", line 79, in document_did_open
self.get_status(params['file'])
File "/Users/gpena-castellanos/Google Drive/develop/quansight/spyder/spyder/plugins/completion/kite/client.py", line 89, in get_status
kite_status = self._get_status(filename)
File "/Users/gpena-castellanos/Google Drive/develop/quansight/spyder/spyder/plugins/completion/kite/client.py", line 81, in _get_status
url = url.format(filename=filename)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 59: ordinal not in range(128)

# Fixed code
def handle_msg(self, message):
    """Handle one message"""
    msg_type, _id, file, msg = [message[k] for k in ("type", "id", "file", "msg")]
    logger.debug("Got request id {0}: {1} for file {2}".format(_id, msg_type, file))
    if msg_type == LSPRequestTypes.DOCUMENT_DID_OPEN:
        self.file_tokens[file] = {
            "text": msg["text"],
            "offset": msg["offset"],
            "language": msg["language"],
        }
    elif msg_type == LSPRequestTypes.DOCUMENT_DID_CHANGE:
        if file not in self.file_tokens:
            self.file_tokens[file] = {
                "text": "",
                "offset": msg["offset"],
                "language": msg["language"],
            }
        diff = msg["diff"]
        text = self.file_tokens[file]
        text["offset"] = msg["offset"]
        text, _ = self.diff_patch.patch_apply(diff, text["text"])
        self.file_tokens[file]["text"] = text
    elif msg_type == LSPRequestTypes.DOCUMENT_DID_CLOSE:
        self.file_tokens.pop(file, {})
    elif msg_type == LSPRequestTypes.DOCUMENT_COMPLETION:
        tokens = []
        if file in self.file_tokens:
            text_info = self.file_tokens[file]
            tokens = self.tokenize(
                text_info["text"], text_info["offset"], text_info["language"]
            )
        tokens = {"params": tokens}
        self.sig_set_tokens.emit(_id, tokens)