From d71e137cffa2c91652eeebb1c38c39ed845544ac Mon Sep 17 00:00:00 2001 From: Eric Johnson Date: Tue, 17 Sep 2024 10:56:49 -0700 Subject: [PATCH] No public description PiperOrigin-RevId: 675630891 --- google/colab/_kernel.py | 22 ++++++++-------------- google/colab/_shell.py | 24 ++++++++++++++++++++++++ setup.py | 2 +- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/google/colab/_kernel.py b/google/colab/_kernel.py index b7c715d3..73eaab8b 100644 --- a/google/colab/_kernel.py +++ b/google/colab/_kernel.py @@ -26,7 +26,7 @@ class Kernel(ipkernel.IPythonKernel): def _shell_class_default(self): return _shell.Shell - def do_inspect(self, code, cursor_pos, detail_level=0): + def do_inspect(self, code, cursor_pos, detail_level=0, *args, **kwargs): name = tokenutil.token_at_cursor(code, cursor_pos) info = self.shell.object_inspect(name) @@ -102,23 +102,17 @@ def complete_request(self, stream, ident, parent): self.session.send(stream, 'complete_reply', matches, parent, ident) - def inspect_request(self, stream, ident, parent): - # TODO: Consider reverting to a `super()` call here once we - # support async. + async def inspect_request(self, stream, ident, parent): try: - content = parent['content'] - reply_content = self.do_inspect( - content['code'], content['cursor_pos'], content.get('detail_level', 0) - ) - reply_content = jsonutil.json_clean(reply_content) + await super().inspect_request(stream, ident, parent) except BaseException as e: # pylint: disable=broad-except # TODO: Consider returning an error here. - self.log.info('Error caught during object inspection: %s', e) + self.log.warning('Error caught during object inspection: %s', e) reply_content = '{"status":"ok","found":false}' - msg = self.session.send( - stream, 'inspect_reply', reply_content, parent, ident - ) - self.log.debug('%s', msg) + msg = self.session.send( + stream, 'inspect_reply', reply_content, parent, ident + ) + self.log.debug('%s', msg) def _to_primitive(o): diff --git a/google/colab/_shell.py b/google/colab/_shell.py index 02a677b4..ddb2caca 100644 --- a/google/colab/_shell.py +++ b/google/colab/_shell.py @@ -26,6 +26,7 @@ from ipykernel import jsonutil from ipykernel import zmqshell from IPython.core import alias +from IPython.core import compilerop from IPython.core import inputsplitter from IPython.core import interactiveshell from IPython.core import oinspect @@ -70,6 +71,29 @@ def init_history(self): self.history_manager = _history.ColabHistoryManager(shell=self, parent=self) self.configurables.append(self.history_manager) + def init_instance_attrs(self): + """Initialize instance attributes. + + For enhanced debugging, ipykernel compiler defines a new XCachingCompiler: + https://github.com/ipython/ipykernel/blob/v6.17.1/ipykernel/compiler.py#L91 + + It's used as the default IPythonKernel shell_class' compiler_class: + https://github.com/ipython/ipykernel/blob/v6.17.1/ipykernel/ipkernel.py#L96 + + This means, functionally, the `code_name` has changed to + something like /tmp/ipykernel_{pid}/{murmurhash}.py, rather than + . + https://github.com/ipython/ipykernel/blob/v6.17.1/ipykernel/compiler.py#L75 + + Since we implement our own Shell which inherits from + `zmqshell.ZMQInteractiveShell`, which inherits from + `IPython.core.interactiveshell.InteractiveShell`, we pick up this change. + The old code_name (e.g. `ipython-N-XXXXX.py`) is widely used and parsed. + We therefore update our shell to pull in the old behavior. + """ + self.compiler_class = compilerop.CachingCompiler + super().init_instance_attrs() + def _should_use_native_system_methods(self): # TODO: Update to match intended values, as appropriate. return bool(os.getenv('USE_NATIVE_IPYTHON_SYSTEM_COMMANDS')) diff --git a/setup.py b/setup.py index 2fc36c0e..13f42cb0 100644 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ # Note: these dependency versions should be kept in-sync with the versions # specified in the docker container requirements files. 'google-auth==2.27.0', - 'ipykernel==5.5.6', + 'ipykernel==6.17.1', 'ipyparallel==8.8.0', 'ipython==7.34.0', 'notebook==6.5.5',