# Buggy code
def handle_exec_method(self, msg):
    """
    Handle data returned by silent executions of kernel methods

    This is based on the _handle_exec_callback of RichJupyterWidget.
    Therefore this is licensed BSD.
    """
    user_exp = msg["content"].get("user_expressions")
    if not user_exp:
        return
    for expression in user_exp:
        if expression in self._kernel_methods:
            # Process kernel reply
            method = self._kernel_methods[expression]
            reply = user_exp[expression]
            data = reply.get("data")
            if "get_namespace_view" in method:
                if data is not None and "text/plain" in data:
                    view = ast.literal_eval(data["text/plain"])
                else:
                    view = None
                self.sig_namespace_view.emit(view)
            elif "get_var_properties" in method:
                if data is not None and "text/plain" in data:
                    properties = ast.literal_eval(data["text/plain"])
                else:
                    properties = None
                self.sig_var_properties.emit(properties)
            elif "get_cwd" in method:
                if data is not None and "text/plain" in data:
                    self._cwd = ast.literal_eval(data["text/plain"])
                    if PY2:
                        self._cwd = encoding.to_unicode_from_fs(self._cwd)
                else:
                    self._cwd = ""
                self.sig_change_cwd.emit(self._cwd)
            elif "get_syspath" in method:
                if data is not None and "text/plain" in data:
                    syspath = ast.literal_eval(data["text/plain"])
                else:
                    syspath = None
                self.sig_show_syspath.emit(syspath)
            elif "get_env" in method:
                if data is not None and "text/plain" in data:
                    env = ast.literal_eval(data["text/plain"])
                else:
                    env = None
                self.sig_show_env.emit(env)
            elif "getattr" in method:
                if data is not None and "text/plain" in data:
                    is_spyder_kernel = data["text/plain"]
                    if "SpyderKernel" in is_spyder_kernel:
                        self.sig_is_spykernel.emit(self)
            else:
                if data is not None and "text/plain" in data:
                    self._kernel_reply = ast.literal_eval(data["text/plain"])
                else:
                    self._kernel_reply = None
                self.sig_got_reply.emit()

            # Remove method after being processed
            self._kernel_methods.pop(expression)


# Traceback (SyntaxError)
File "/home/biomike/anaconda2/lib/python2.7/site-packages/qtconsole/base_frontend_mixin.py", line 163, in _dispatch
Traceback (most recent call last):
File "/home/biomike/anaconda2/lib/python2.7/site-packages/qtconsole/base_frontend_mixin.py", line 163, in _dispatch
handler(msg)
File "/home/biomike/anaconda2/lib/python2.7/site-packages/spyder/widgets/ipythonconsole/namespacebrowser.py", line 214, in _handle_execute_reply
self.handle_exec_method(msg)
File "/home/biomike/anaconda2/lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.py", line 317, in handle_exec_method
properties = ast.literal_eval(data['text/plain'])
File "/home/biomike/anaconda2/lib/python2.7/ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/home/biomike/anaconda2/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 9001
...}
^
SyntaxError: invalid syntax
handler(msg)
File "/home/biomike/anaconda2/lib/python2.7/site-packages/spyder/widgets/ipythonconsole/namespacebrowser.py", line 214, in _handle_execute_reply
self.handle_exec_method(msg)
File "/home/biomike/anaconda2/lib/python2.7/site-packages/spyder/widgets/ipythonconsole/shell.py", line 311, in handle_exec_method
view = ast.literal_eval(data['text/plain'])
File "/home/biomike/anaconda2/lib/python2.7/ast.py", line 49, in literal_eval
node_or_string = parse(node_or_string, mode='eval')
File "/home/biomike/anaconda2/lib/python2.7/ast.py", line 37, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 4001
...}
^
SyntaxError: invalid syntax

# Fixed code
def handle_exec_method(self, msg):
    """
    Handle data returned by silent executions of kernel methods

    This is based on the _handle_exec_callback of RichJupyterWidget.
    Therefore this is licensed BSD.
    """
    user_exp = msg["content"].get("user_expressions")
    if not user_exp:
        return
    for expression in user_exp:
        if expression in self._kernel_methods:
            # Process kernel reply
            method = self._kernel_methods[expression]
            reply = user_exp[expression]
            data = reply.get("data")
            if "get_namespace_view" in method:
                if data is not None and "text/plain" in data:
                    literal = ast.literal_eval(data["text/plain"])
                    view = ast.literal_eval(literal)
                else:
                    view = None
                self.sig_namespace_view.emit(view)
            elif "get_var_properties" in method:
                if data is not None and "text/plain" in data:
                    literal = ast.literal_eval(data["text/plain"])
                    properties = ast.literal_eval(literal)
                else:
                    properties = None
                self.sig_var_properties.emit(properties)
            elif "get_cwd" in method:
                if data is not None and "text/plain" in data:
                    self._cwd = ast.literal_eval(data["text/plain"])
                    if PY2:
                        self._cwd = encoding.to_unicode_from_fs(self._cwd)
                else:
                    self._cwd = ""
                self.sig_change_cwd.emit(self._cwd)
            elif "get_syspath" in method:
                if data is not None and "text/plain" in data:
                    syspath = ast.literal_eval(data["text/plain"])
                else:
                    syspath = None
                self.sig_show_syspath.emit(syspath)
            elif "get_env" in method:
                if data is not None and "text/plain" in data:
                    env = ast.literal_eval(data["text/plain"])
                else:
                    env = None
                self.sig_show_env.emit(env)
            elif "getattr" in method:
                if data is not None and "text/plain" in data:
                    is_spyder_kernel = data["text/plain"]
                    if "SpyderKernel" in is_spyder_kernel:
                        self.sig_is_spykernel.emit(self)
            else:
                if data is not None and "text/plain" in data:
                    self._kernel_reply = ast.literal_eval(data["text/plain"])
                else:
                    self._kernel_reply = None
                self.sig_got_reply.emit()

            # Remove method after being processed
            self._kernel_methods.pop(expression)