diff --git a/IPython/core/completer.py b/IPython/core/completer.py index e4d3995..9a84f7e 100644 --- a/IPython/core/completer.py +++ b/IPython/core/completer.py @@ -41,7 +41,7 @@ or using unicode completion: Only valid Python identifiers will complete. Combining characters (like arrow or dots) are also available, unlike latex they need to be put after the their -counterpart that is to say, `F\\\\vec` is correct, not `\\\\vecF`. +counterpart that is to say, ``F\\\\vec`` is correct, not ``\\\\vecF``. Some browsers are known to display combining characters incorrectly. diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index e222e99..8fd546b 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -297,19 +297,29 @@ class ExecutionInfo(object): store_history = False silent = False shell_futures = True + cell_id = None - def __init__(self, raw_cell, store_history, silent, shell_futures): + def __init__(self, raw_cell, store_history, silent, shell_futures, cell_id): self.raw_cell = raw_cell self.store_history = store_history self.silent = silent self.shell_futures = shell_futures + self.cell_id = cell_id def __repr__(self): name = self.__class__.__qualname__ - raw_cell = ((self.raw_cell[:50] + '..') - if len(self.raw_cell) > 50 else self.raw_cell) - return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s>' %\ - (name, id(self), raw_cell, self.store_history, self.silent, self.shell_futures) + raw_cell = ( + (self.raw_cell[:50] + "..") if len(self.raw_cell) > 50 else self.raw_cell + ) + return '<%s object at %x, raw_cell="%s" store_history=%s silent=%s shell_futures=%s cell_id=%s>' % ( + name, + id(self), + raw_cell, + self.store_history, + self.silent, + self.shell_futures, + self.cell_id, + ) class ExecutionResult(object): @@ -2928,7 +2938,14 @@ class InteractiveShell(SingletonConfigurable): self.showtraceback() warn('Unknown failure executing module: <%s>' % mod_name) - def run_cell(self, raw_cell, store_history=False, silent=False, shell_futures=True): + def run_cell( + self, + raw_cell, + store_history=False, + silent=False, + shell_futures=True, + cell_id=None, + ): """Run a complete IPython cell. Parameters @@ -2955,14 +2972,22 @@ class InteractiveShell(SingletonConfigurable): result = None try: result = self._run_cell( - raw_cell, store_history, silent, shell_futures) + raw_cell, store_history, silent, shell_futures, cell_id + ) finally: self.events.trigger('post_execute') if not silent: self.events.trigger('post_run_cell', result) return result - def _run_cell(self, raw_cell:str, store_history:bool, silent:bool, shell_futures:bool): + def _run_cell( + self, + raw_cell: str, + store_history: bool, + silent: bool, + shell_futures: bool, + cell_id: str, + ) -> ExecutionResult: """Internal method to run a complete IPython cell.""" # we need to avoid calling self.transform_cell multiple time on the same thing @@ -2982,6 +3007,7 @@ class InteractiveShell(SingletonConfigurable): shell_futures=shell_futures, transformed_cell=transformed_cell, preprocessing_exc_tuple=preprocessing_exc_tuple, + cell_id=cell_id, ) # run_cell_async is async, but may not actually need an eventloop. @@ -3002,7 +3028,9 @@ class InteractiveShell(SingletonConfigurable): try: return runner(coro) except BaseException as e: - info = ExecutionInfo(raw_cell, store_history, silent, shell_futures) + info = ExecutionInfo( + raw_cell, store_history, silent, shell_futures, cell_id + ) result = ExecutionResult(info) result.error_in_exec = e self.showtraceback(running_compiled_code=True) @@ -3060,7 +3088,8 @@ class InteractiveShell(SingletonConfigurable): shell_futures=True, *, transformed_cell: Optional[str] = None, - preprocessing_exc_tuple: Optional[Any] = None + preprocessing_exc_tuple: Optional[Any] = None, + cell_id=None, ) -> ExecutionResult: """Run a complete IPython cell asynchronously. @@ -3091,8 +3120,7 @@ class InteractiveShell(SingletonConfigurable): .. versionadded:: 7.0 """ - info = ExecutionInfo( - raw_cell, store_history, silent, shell_futures) + info = ExecutionInfo(raw_cell, store_history, silent, shell_futures, cell_id) result = ExecutionResult(info) if (not raw_cell) or raw_cell.isspace(): diff --git a/IPython/terminal/ipapp.py b/IPython/terminal/ipapp.py index b2b8d5f..1a3c6c7 100755 --- a/IPython/terminal/ipapp.py +++ b/IPython/terminal/ipapp.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 """ -The :class:`~IPython.core.application.Application` object for the command +The :class:`~traitlets.config.application.Application` object for the command line :command:`ipython` program. """ diff --git a/docs/source/config/callbacks.rst b/docs/source/config/callbacks.rst index 6c75c73..60c7aba 100644 --- a/docs/source/config/callbacks.rst +++ b/docs/source/config/callbacks.rst @@ -17,22 +17,29 @@ For example:: def __init__(self, ip): self.shell = ip self.last_x = None - + def pre_execute(self): self.last_x = self.shell.user_ns.get('x', None) - + def pre_run_cell(self, info): - print('Cell code: "%s"' % info.raw_cell) - + print('info.raw_cell =', info.raw_cell) + print('info.store_history =', info.store_history) + print('info.silent =', info.silent) + print('info.shell_futures =', info.shell_futures) + print('info.cell_id =', info.cell_id) + print(dir(info)) + def post_execute(self): if self.shell.user_ns.get('x', None) != self.last_x: print("x changed!") - + def post_run_cell(self, result): - print('Cell code: "%s"' % result.info.raw_cell) - if result.error_before_exec: - print('Error before execution: %s' % result.error_before_exec) - + print('result.execution_count = ', result.execution_count) + print('result.error_before_exec = ', result.error_before_exec) + print('result.error_in_exec = ', result.error_in_exec) + print('result.info = ', result.info) + print('result.result = ', result.result) + def load_ipython_extension(ip): vw = VarWatcher(ip) ip.events.register('pre_execute', vw.pre_execute) @@ -40,6 +47,13 @@ For example:: ip.events.register('post_execute', vw.post_execute) ip.events.register('post_run_cell', vw.post_run_cell) +.. versionadded:: 8.3 + + Since IPython 8.3 and ipykernel 6.12.1, the ``info`` objects in the callback + now have a the ``cell_id`` that will be set to the value sent by the + frontened, when those send it. + + Events ======