We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Error when connecting to external kernels
qt 4.8.7 does not have this bug, but qt5.6 with mac will have

```
Spyder Internal Console

This console is used to report application
internal errors and to inspect Spyder
internals with the following commands:
  spy.app, spy.window, dir(spy)

Please don't use it to run your code

>>> WARNING:traitlets:kernel died: 3.0032799243927
Traceback (most recent call last):
  File "/Users/HWJ/anaconda3/lib/python3.5/site-packages/qtconsole/base_frontend_mixin.py", line 163, in _dispatch
    handler(msg)
  File "/Users/HWJ/anaconda3/lib/python3.5/site-packages/spyder/widgets/ipythonconsole/namespacebrowser.py", line 182, in _handle_execute_reply
    self.set_namespace_view_settings()
  File "/Users/HWJ/anaconda3/lib/python3.5/site-packages/spyder/widgets/ipythonconsole/namespacebrowser.py", line 68, in set_namespace_view_settings
    settings = to_text_string(self.namespacebrowser.get_view_settings())
AttributeError: 'NoneType' object has no attribute 'get_view_settings'
```


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `spyder/widgets/ipythonconsole/namespacebrowser.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright © Spyder Project Contributors
4 # Licensed under the terms of the MIT License
5 # (see spyder/__init__.py for details)
6 
7 """
8 Widget that handle communications between the IPython Console and
9 the Variable Explorer
10 """
11 
12 from qtpy.QtCore import QEventLoop
13 
14 from ipykernel.pickleutil import CannedObject
15 from ipykernel.serialize import deserialize_object
16 from qtconsole.rich_jupyter_widget import RichJupyterWidget
17 
18 from spyder.config.base import _
19 from spyder.py3compat import to_text_string
20 
21 
22 class NamepaceBrowserWidget(RichJupyterWidget):
23     """
24     Widget with the necessary attributes and methods to handle communications
25     between the IPython Console and the Variable Explorer
26     """
27 
28     # Reference to the nsb widget connected to this client
29     namespacebrowser = None
30 
31     # To save the replies of kernel method executions (except
32     # getting values of variables)
33     _kernel_methods = {}
34 
35     # To save values and messages returned by the kernel
36     _kernel_value = None
37     _kernel_is_starting = True
38 
39     # --- Public API --------------------------------------------------
40     def set_namespacebrowser(self, namespacebrowser):
41         """Set namespace browser widget"""
42         self.namespacebrowser = namespacebrowser
43         self.configure_namespacebrowser()
44 
45     def configure_namespacebrowser(self):
46         """Configure associated namespace browser widget"""
47         # Tell it that we are connected to client
48         self.namespacebrowser.is_ipyclient = True
49 
50         # Update namespace view
51         self.sig_namespace_view.connect(lambda data:
52             self.namespacebrowser.process_remote_view(data))
53 
54         # Update properties of variables
55         self.sig_var_properties.connect(lambda data:
56             self.namespacebrowser.set_var_properties(data))
57 
58     def refresh_namespacebrowser(self):
59         """Refresh namespace browser"""
60         if self.namespacebrowser:
61             self.silent_exec_method(
62                 'get_ipython().kernel.get_namespace_view()')
63             self.silent_exec_method(
64                 'get_ipython().kernel.get_var_properties()')
65 
66     def set_namespace_view_settings(self):
67         """Set the namespace view settings"""
68         settings = to_text_string(self.namespacebrowser.get_view_settings())
69         code = u"get_ipython().kernel.namespace_view_settings = %s" % settings
70         self.silent_execute(code)
71 
72     def get_value(self, name):
73         """Ask kernel for a value"""
74         # Don't ask for values while reading (ipdb) is active
75         if self._reading:
76             raise ValueError(_("Inspecting and setting values while debugging "
77                                "in IPython consoles is not supported yet by "
78                                "Spyder."))
79 
80         # Wait until the kernel returns the value
81         wait_loop = QEventLoop()
82         self.sig_got_reply.connect(wait_loop.quit)
83         self.silent_execute("get_ipython().kernel.get_value('%s')" % name)
84         wait_loop.exec_()
85 
86         # Remove loop connection and loop
87         self.sig_got_reply.disconnect(wait_loop.quit)
88         wait_loop = None
89 
90         # Handle exceptions
91         if self._kernel_value is None:
92             if self._kernel_reply:
93                 msg = self._kernel_reply[:]
94                 self._kernel_reply = None
95                 raise ValueError(msg)
96 
97         return self._kernel_value
98 
99     def set_value(self, name, value):
100         """Set value for a variable"""
101         value = to_text_string(value)
102         self.silent_execute("get_ipython().kernel.set_value('%s', %s)" %
103                             (name, value))
104 
105     def remove_value(self, name):
106         """Remove a variable"""
107         self.silent_execute("get_ipython().kernel.remove_value('%s')" % name)
108 
109     def copy_value(self, orig_name, new_name):
110         """Copy a variable"""
111         self.silent_execute("get_ipython().kernel.copy_value('%s', '%s')" %
112                             (orig_name, new_name))
113 
114     def load_data(self, filename, ext):
115         # Wait until the kernel tries to load the file
116         wait_loop = QEventLoop()
117         self.sig_got_reply.connect(wait_loop.quit)
118         self.silent_exec_method(
119                 "get_ipython().kernel.load_data('%s', '%s')" % (filename, ext))
120         wait_loop.exec_()
121 
122         # Remove loop connection and loop
123         self.sig_got_reply.disconnect(wait_loop.quit)
124         wait_loop = None
125 
126         return self._kernel_reply
127 
128     def save_namespace(self, filename):
129         # Wait until the kernel tries to save the file
130         wait_loop = QEventLoop()
131         self.sig_got_reply.connect(wait_loop.quit)
132         self.silent_exec_method("get_ipython().kernel.save_namespace('%s')" %
133                                 filename)
134         wait_loop.exec_()
135 
136         # Remove loop connection and loop
137         self.sig_got_reply.disconnect(wait_loop.quit)
138         wait_loop = None
139 
140         return self._kernel_reply
141 
142     # ---- Private API (defined by us) ------------------------------
143     def _handle_data_message(self, msg):
144         """
145         Handle raw (serialized) data sent by the kernel
146 
147         We only handle data asked by Spyder, in case people use
148         publish_data for other purposes.
149         """
150         # Deserialize data
151         try:
152             data = deserialize_object(msg['buffers'])[0]
153         except Exception as msg:
154             self._kernel_value = None
155             self._kernel_reply = repr(msg)
156             self.sig_got_reply.emit()
157             return
158 
159         # We only handle data asked by Spyder
160         value = data.get('__spy_data__', None)
161         if value is not None:
162             if isinstance(value, CannedObject):
163                 value = value.get_object()
164             self._kernel_value = value
165             self.sig_got_reply.emit()
166 
167     # ---- Private API (overrode by us) ----------------------------
168     def _handle_execute_reply(self, msg):
169         """
170         Reimplemented to handle communications between Spyder
171         and the kernel
172         """
173         msg_id = msg['parent_header']['msg_id']
174         info = self._request_info['execute'].get(msg_id)
175         # unset reading flag, because if execute finished, raw_input can't
176         # still be pending.
177         self._reading = False
178 
179         # Refresh namespacebrowser after the kernel starts running
180         exec_count = msg['content']['execution_count']
181         if exec_count == 0 and self._kernel_is_starting:
182             self.set_namespace_view_settings()
183             self.refresh_namespacebrowser()
184             self._kernel_is_starting = False
185 
186         # Handle silent execution of kernel methods
187         if info and info.kind == 'silent_exec_method' and not self._hidden:
188             self.handle_exec_method(msg)
189             self._request_info['execute'].pop(msg_id)
190         else:
191             super(NamepaceBrowserWidget, self)._handle_execute_reply(msg)
192 
193     def _handle_status(self, msg):
194         """
195         Reimplemented to refresh the namespacebrowser after kernel
196         restarts
197         """
198         state = msg['content'].get('execution_state', '')
199         msg_type = msg['parent_header'].get('msg_type', '')
200         if state == 'starting' and not self._kernel_is_starting:
201             # This handles restarts when the kernel dies
202             # unexpectedly
203             self._kernel_is_starting = True
204         elif state == 'idle' and msg_type == 'shutdown_request':
205             # This handles restarts asked by the user
206             if self.namespacebrowser is not None:
207                 self.set_namespace_view_settings()
208                 self.refresh_namespacebrowser()
209         else:
210             super(NamepaceBrowserWidget, self)._handle_status(msg)
211
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/spyder/widgets/ipythonconsole/namespacebrowser.py b/spyder/widgets/ipythonconsole/namespacebrowser.py
--- a/spyder/widgets/ipythonconsole/namespacebrowser.py
+++ b/spyder/widgets/ipythonconsole/namespacebrowser.py
@@ -179,8 +179,9 @@
         # Refresh namespacebrowser after the kernel starts running
         exec_count = msg['content']['execution_count']
         if exec_count == 0 and self._kernel_is_starting:
-            self.set_namespace_view_settings()
-            self.refresh_namespacebrowser()
+            if self.namespacebrowser is not None:
+                self.set_namespace_view_settings()
+                self.refresh_namespacebrowser()
             self._kernel_is_starting = False
 
         # Handle silent execution of kernel methods

{"golden_diff": "diff --git a/spyder/widgets/ipythonconsole/namespacebrowser.py b/spyder/widgets/ipythonconsole/namespacebrowser.py\n--- a/spyder/widgets/ipythonconsole/namespacebrowser.py\n+++ b/spyder/widgets/ipythonconsole/namespacebrowser.py\n@@ -179,8 +179,9 @@\n         # Refresh namespacebrowser after the kernel starts running\n         exec_count = msg['content']['execution_count']\n         if exec_count == 0 and self._kernel_is_starting:\n-            self.set_namespace_view_settings()\n-            self.refresh_namespacebrowser()\n+            if self.namespacebrowser is not None:\n+                self.set_namespace_view_settings()\n+                self.refresh_namespacebrowser()\n             self._kernel_is_starting = False\n \n         # Handle silent execution of kernel methods\n", "issue": "Error when connecting to external kernels\nqt 4.8.7 does not have this bug, but qt5.6 with mac will have\n\n```\nSpyder Internal Console\n\nThis console is used to report application\ninternal errors and to inspect Spyder\ninternals with the following commands:\n  spy.app, spy.window, dir(spy)\n\nPlease don't use it to run your code\n\n>>> WARNING:traitlets:kernel died: 3.0032799243927\nTraceback (most recent call last):\n  File \"/Users/HWJ/anaconda3/lib/python3.5/site-packages/qtconsole/base_frontend_mixin.py\", line 163, in _dispatch\n    handler(msg)\n  File \"/Users/HWJ/anaconda3/lib/python3.5/site-packages/spyder/widgets/ipythonconsole/namespacebrowser.py\", line 182, in _handle_execute_reply\n    self.set_namespace_view_settings()\n  File \"/Users/HWJ/anaconda3/lib/python3.5/site-packages/spyder/widgets/ipythonconsole/namespacebrowser.py\", line 68, in set_namespace_view_settings\n    settings = to_text_string(self.namespacebrowser.get_view_settings())\nAttributeError: 'NoneType' object has no attribute 'get_view_settings'\n```\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright \u00a9 Spyder Project Contributors\n# Licensed under the terms of the MIT License\n# (see spyder/__init__.py for details)\n\n\"\"\"\nWidget that handle communications between the IPython Console and\nthe Variable Explorer\n\"\"\"\n\nfrom qtpy.QtCore import QEventLoop\n\nfrom ipykernel.pickleutil import CannedObject\nfrom ipykernel.serialize import deserialize_object\nfrom qtconsole.rich_jupyter_widget import RichJupyterWidget\n\nfrom spyder.config.base import _\nfrom spyder.py3compat import to_text_string\n\n\nclass NamepaceBrowserWidget(RichJupyterWidget):\n    \"\"\"\n    Widget with the necessary attributes and methods to handle communications\n    between the IPython Console and the Variable Explorer\n    \"\"\"\n\n    # Reference to the nsb widget connected to this client\n    namespacebrowser = None\n\n    # To save the replies of kernel method executions (except\n    # getting values of variables)\n    _kernel_methods = {}\n\n    # To save values and messages returned by the kernel\n    _kernel_value = None\n    _kernel_is_starting = True\n\n    # --- Public API --------------------------------------------------\n    def set_namespacebrowser(self, namespacebrowser):\n        \"\"\"Set namespace browser widget\"\"\"\n        self.namespacebrowser = namespacebrowser\n        self.configure_namespacebrowser()\n\n    def configure_namespacebrowser(self):\n        \"\"\"Configure associated namespace browser widget\"\"\"\n        # Tell it that we are connected to client\n        self.namespacebrowser.is_ipyclient = True\n\n        # Update namespace view\n        self.sig_namespace_view.connect(lambda data:\n            self.namespacebrowser.process_remote_view(data))\n\n        # Update properties of variables\n        self.sig_var_properties.connect(lambda data:\n            self.namespacebrowser.set_var_properties(data))\n\n    def refresh_namespacebrowser(self):\n        \"\"\"Refresh namespace browser\"\"\"\n        if self.namespacebrowser:\n            self.silent_exec_method(\n                'get_ipython().kernel.get_namespace_view()')\n            self.silent_exec_method(\n                'get_ipython().kernel.get_var_properties()')\n\n    def set_namespace_view_settings(self):\n        \"\"\"Set the namespace view settings\"\"\"\n        settings = to_text_string(self.namespacebrowser.get_view_settings())\n        code = u\"get_ipython().kernel.namespace_view_settings = %s\" % settings\n        self.silent_execute(code)\n\n    def get_value(self, name):\n        \"\"\"Ask kernel for a value\"\"\"\n        # Don't ask for values while reading (ipdb) is active\n        if self._reading:\n            raise ValueError(_(\"Inspecting and setting values while debugging \"\n                               \"in IPython consoles is not supported yet by \"\n                               \"Spyder.\"))\n\n        # Wait until the kernel returns the value\n        wait_loop = QEventLoop()\n        self.sig_got_reply.connect(wait_loop.quit)\n        self.silent_execute(\"get_ipython().kernel.get_value('%s')\" % name)\n        wait_loop.exec_()\n\n        # Remove loop connection and loop\n        self.sig_got_reply.disconnect(wait_loop.quit)\n        wait_loop = None\n\n        # Handle exceptions\n        if self._kernel_value is None:\n            if self._kernel_reply:\n                msg = self._kernel_reply[:]\n                self._kernel_reply = None\n                raise ValueError(msg)\n\n        return self._kernel_value\n\n    def set_value(self, name, value):\n        \"\"\"Set value for a variable\"\"\"\n        value = to_text_string(value)\n        self.silent_execute(\"get_ipython().kernel.set_value('%s', %s)\" %\n                            (name, value))\n\n    def remove_value(self, name):\n        \"\"\"Remove a variable\"\"\"\n        self.silent_execute(\"get_ipython().kernel.remove_value('%s')\" % name)\n\n    def copy_value(self, orig_name, new_name):\n        \"\"\"Copy a variable\"\"\"\n        self.silent_execute(\"get_ipython().kernel.copy_value('%s', '%s')\" %\n                            (orig_name, new_name))\n\n    def load_data(self, filename, ext):\n        # Wait until the kernel tries to load the file\n        wait_loop = QEventLoop()\n        self.sig_got_reply.connect(wait_loop.quit)\n        self.silent_exec_method(\n                \"get_ipython().kernel.load_data('%s', '%s')\" % (filename, ext))\n        wait_loop.exec_()\n\n        # Remove loop connection and loop\n        self.sig_got_reply.disconnect(wait_loop.quit)\n        wait_loop = None\n\n        return self._kernel_reply\n\n    def save_namespace(self, filename):\n        # Wait until the kernel tries to save the file\n        wait_loop = QEventLoop()\n        self.sig_got_reply.connect(wait_loop.quit)\n        self.silent_exec_method(\"get_ipython().kernel.save_namespace('%s')\" %\n                                filename)\n        wait_loop.exec_()\n\n        # Remove loop connection and loop\n        self.sig_got_reply.disconnect(wait_loop.quit)\n        wait_loop = None\n\n        return self._kernel_reply\n\n    # ---- Private API (defined by us) ------------------------------\n    def _handle_data_message(self, msg):\n        \"\"\"\n        Handle raw (serialized) data sent by the kernel\n\n        We only handle data asked by Spyder, in case people use\n        publish_data for other purposes.\n        \"\"\"\n        # Deserialize data\n        try:\n            data = deserialize_object(msg['buffers'])[0]\n        except Exception as msg:\n            self._kernel_value = None\n            self._kernel_reply = repr(msg)\n            self.sig_got_reply.emit()\n            return\n\n        # We only handle data asked by Spyder\n        value = data.get('__spy_data__', None)\n        if value is not None:\n            if isinstance(value, CannedObject):\n                value = value.get_object()\n            self._kernel_value = value\n            self.sig_got_reply.emit()\n\n    # ---- Private API (overrode by us) ----------------------------\n    def _handle_execute_reply(self, msg):\n        \"\"\"\n        Reimplemented to handle communications between Spyder\n        and the kernel\n        \"\"\"\n        msg_id = msg['parent_header']['msg_id']\n        info = self._request_info['execute'].get(msg_id)\n        # unset reading flag, because if execute finished, raw_input can't\n        # still be pending.\n        self._reading = False\n\n        # Refresh namespacebrowser after the kernel starts running\n        exec_count = msg['content']['execution_count']\n        if exec_count == 0 and self._kernel_is_starting:\n            self.set_namespace_view_settings()\n            self.refresh_namespacebrowser()\n            self._kernel_is_starting = False\n\n        # Handle silent execution of kernel methods\n        if info and info.kind == 'silent_exec_method' and not self._hidden:\n            self.handle_exec_method(msg)\n            self._request_info['execute'].pop(msg_id)\n        else:\n            super(NamepaceBrowserWidget, self)._handle_execute_reply(msg)\n\n    def _handle_status(self, msg):\n        \"\"\"\n        Reimplemented to refresh the namespacebrowser after kernel\n        restarts\n        \"\"\"\n        state = msg['content'].get('execution_state', '')\n        msg_type = msg['parent_header'].get('msg_type', '')\n        if state == 'starting' and not self._kernel_is_starting:\n            # This handles restarts when the kernel dies\n            # unexpectedly\n            self._kernel_is_starting = True\n        elif state == 'idle' and msg_type == 'shutdown_request':\n            # This handles restarts asked by the user\n            if self.namespacebrowser is not None:\n                self.set_namespace_view_settings()\n                self.refresh_namespacebrowser()\n        else:\n            super(NamepaceBrowserWidget, self)._handle_status(msg)\n", "path": "spyder/widgets/ipythonconsole/namespacebrowser.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright \u00a9 Spyder Project Contributors\n# Licensed under the terms of the MIT License\n# (see spyder/__init__.py for details)\n\n\"\"\"\nWidget that handle communications between the IPython Console and\nthe Variable Explorer\n\"\"\"\n\nfrom qtpy.QtCore import QEventLoop\n\nfrom ipykernel.pickleutil import CannedObject\nfrom ipykernel.serialize import deserialize_object\nfrom qtconsole.rich_jupyter_widget import RichJupyterWidget\n\nfrom spyder.config.base import _\nfrom spyder.py3compat import to_text_string\n\n\nclass NamepaceBrowserWidget(RichJupyterWidget):\n    \"\"\"\n    Widget with the necessary attributes and methods to handle communications\n    between the IPython Console and the Variable Explorer\n    \"\"\"\n\n    # Reference to the nsb widget connected to this client\n    namespacebrowser = None\n\n    # To save the replies of kernel method executions (except\n    # getting values of variables)\n    _kernel_methods = {}\n\n    # To save values and messages returned by the kernel\n    _kernel_value = None\n    _kernel_is_starting = True\n\n    # --- Public API --------------------------------------------------\n    def set_namespacebrowser(self, namespacebrowser):\n        \"\"\"Set namespace browser widget\"\"\"\n        self.namespacebrowser = namespacebrowser\n        self.configure_namespacebrowser()\n\n    def configure_namespacebrowser(self):\n        \"\"\"Configure associated namespace browser widget\"\"\"\n        # Tell it that we are connected to client\n        self.namespacebrowser.is_ipyclient = True\n\n        # Update namespace view\n        self.sig_namespace_view.connect(lambda data:\n            self.namespacebrowser.process_remote_view(data))\n\n        # Update properties of variables\n        self.sig_var_properties.connect(lambda data:\n            self.namespacebrowser.set_var_properties(data))\n\n    def refresh_namespacebrowser(self):\n        \"\"\"Refresh namespace browser\"\"\"\n        if self.namespacebrowser:\n            self.silent_exec_method(\n                'get_ipython().kernel.get_namespace_view()')\n            self.silent_exec_method(\n                'get_ipython().kernel.get_var_properties()')\n\n    def set_namespace_view_settings(self):\n        \"\"\"Set the namespace view settings\"\"\"\n        settings = to_text_string(self.namespacebrowser.get_view_settings())\n        code = u\"get_ipython().kernel.namespace_view_settings = %s\" % settings\n        self.silent_execute(code)\n\n    def get_value(self, name):\n        \"\"\"Ask kernel for a value\"\"\"\n        # Don't ask for values while reading (ipdb) is active\n        if self._reading:\n            raise ValueError(_(\"Inspecting and setting values while debugging \"\n                               \"in IPython consoles is not supported yet by \"\n                               \"Spyder.\"))\n\n        # Wait until the kernel returns the value\n        wait_loop = QEventLoop()\n        self.sig_got_reply.connect(wait_loop.quit)\n        self.silent_execute(\"get_ipython().kernel.get_value('%s')\" % name)\n        wait_loop.exec_()\n\n        # Remove loop connection and loop\n        self.sig_got_reply.disconnect(wait_loop.quit)\n        wait_loop = None\n\n        # Handle exceptions\n        if self._kernel_value is None:\n            if self._kernel_reply:\n                msg = self._kernel_reply[:]\n                self._kernel_reply = None\n                raise ValueError(msg)\n\n        return self._kernel_value\n\n    def set_value(self, name, value):\n        \"\"\"Set value for a variable\"\"\"\n        value = to_text_string(value)\n        self.silent_execute(\"get_ipython().kernel.set_value('%s', %s)\" %\n                            (name, value))\n\n    def remove_value(self, name):\n        \"\"\"Remove a variable\"\"\"\n        self.silent_execute(\"get_ipython().kernel.remove_value('%s')\" % name)\n\n    def copy_value(self, orig_name, new_name):\n        \"\"\"Copy a variable\"\"\"\n        self.silent_execute(\"get_ipython().kernel.copy_value('%s', '%s')\" %\n                            (orig_name, new_name))\n\n    def load_data(self, filename, ext):\n        # Wait until the kernel tries to load the file\n        wait_loop = QEventLoop()\n        self.sig_got_reply.connect(wait_loop.quit)\n        self.silent_exec_method(\n                \"get_ipython().kernel.load_data('%s', '%s')\" % (filename, ext))\n        wait_loop.exec_()\n\n        # Remove loop connection and loop\n        self.sig_got_reply.disconnect(wait_loop.quit)\n        wait_loop = None\n\n        return self._kernel_reply\n\n    def save_namespace(self, filename):\n        # Wait until the kernel tries to save the file\n        wait_loop = QEventLoop()\n        self.sig_got_reply.connect(wait_loop.quit)\n        self.silent_exec_method(\"get_ipython().kernel.save_namespace('%s')\" %\n                                filename)\n        wait_loop.exec_()\n\n        # Remove loop connection and loop\n        self.sig_got_reply.disconnect(wait_loop.quit)\n        wait_loop = None\n\n        return self._kernel_reply\n\n    # ---- Private API (defined by us) ------------------------------\n    def _handle_data_message(self, msg):\n        \"\"\"\n        Handle raw (serialized) data sent by the kernel\n\n        We only handle data asked by Spyder, in case people use\n        publish_data for other purposes.\n        \"\"\"\n        # Deserialize data\n        try:\n            data = deserialize_object(msg['buffers'])[0]\n        except Exception as msg:\n            self._kernel_value = None\n            self._kernel_reply = repr(msg)\n            self.sig_got_reply.emit()\n            return\n\n        # We only handle data asked by Spyder\n        value = data.get('__spy_data__', None)\n        if value is not None:\n            if isinstance(value, CannedObject):\n                value = value.get_object()\n            self._kernel_value = value\n            self.sig_got_reply.emit()\n\n    # ---- Private API (overrode by us) ----------------------------\n    def _handle_execute_reply(self, msg):\n        \"\"\"\n        Reimplemented to handle communications between Spyder\n        and the kernel\n        \"\"\"\n        msg_id = msg['parent_header']['msg_id']\n        info = self._request_info['execute'].get(msg_id)\n        # unset reading flag, because if execute finished, raw_input can't\n        # still be pending.\n        self._reading = False\n\n        # Refresh namespacebrowser after the kernel starts running\n        exec_count = msg['content']['execution_count']\n        if exec_count == 0 and self._kernel_is_starting:\n            if self.namespacebrowser is not None:\n                self.set_namespace_view_settings()\n                self.refresh_namespacebrowser()\n            self._kernel_is_starting = False\n\n        # Handle silent execution of kernel methods\n        if info and info.kind == 'silent_exec_method' and not self._hidden:\n            self.handle_exec_method(msg)\n            self._request_info['execute'].pop(msg_id)\n        else:\n            super(NamepaceBrowserWidget, self)._handle_execute_reply(msg)\n\n    def _handle_status(self, msg):\n        \"\"\"\n        Reimplemented to refresh the namespacebrowser after kernel\n        restarts\n        \"\"\"\n        state = msg['content'].get('execution_state', '')\n        msg_type = msg['parent_header'].get('msg_type', '')\n        if state == 'starting' and not self._kernel_is_starting:\n            # This handles restarts when the kernel dies\n            # unexpectedly\n            self._kernel_is_starting = True\n        elif state == 'idle' and msg_type == 'shutdown_request':\n            # This handles restarts asked by the user\n            if self.namespacebrowser is not None:\n                self.set_namespace_view_settings()\n                self.refresh_namespacebrowser()\n        else:\n            super(NamepaceBrowserWidget, self)._handle_status(msg)\n", "path": "spyder/widgets/ipythonconsole/namespacebrowser.py"}]}