11.. _execution_semantics :
22
3- Execution semantics in the IPython kernel
4- =========================================
3+ Execution of cells in the IPython kernel
4+ ========================================
55
6- The execution of user code consists of the following phases:
6+ When IPython kernel receives `execute_request <https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute >`_
7+ with user code, it processes the message in the following phases:
78
891. Fire the ``pre_execute `` event.
9102. Fire the ``pre_run_cell `` event unless silent is ``True ``.
10- 3. Execute the ``code `` field , see below for details.
11+ 3. Execute `` run_cell `` method to preprocess ``code ``, compile and run it , see below for details.
11124. If execution succeeds, expressions in ``user_expressions `` are computed.
1213 This ensures that any error in the expressions don't affect the main code execution.
13145. Fire the ``post_execute `` event.
@@ -18,9 +19,15 @@ The execution of user code consists of the following phases:
1819 :doc: `/config/callbacks `
1920
2021
21- To understand how the ``code `` field is executed, one must know that Python
22- code can be compiled in one of three modes (controlled by the ``mode `` argument
23- to the :func: `compile ` builtin):
22+ Running user ``code ``
23+ =====================
24+
25+ First, the ``code `` cell is transformed to expand ``%magic `` and ``!system ``
26+ by ``IPython.core.inputtransformer2 ``. Then is being compiled using standard
27+ Python :func: `compile ` function and executed.
28+
29+ Not specific to IPython, all Python code can be compiled in one of three modes
30+ (see ``mode `` argument to the standard :func: `compile ` function):
2431
2532*single *
2633 Valid for a single interactive statement (though the source can contain
@@ -50,16 +57,16 @@ execution in 'single' mode, and then:
5057
5158- If there is more than one block:
5259
53- * if the last one is a single line long, run all but the last in 'exec' mode
60+ * if the last block is a single line long, run all but the last in 'exec' mode
5461 and the very last one in 'single' mode. This makes it easy to type simple
5562 expressions at the end to see computed values.
5663
57- * if the last one is no more than two lines long, run all but the last in
64+ * if the last block is no more than two lines long, run all but the last in
5865 'exec' mode and the very last one in 'single' mode. This makes it easy to
5966 type simple expressions at the end to see computed values. - otherwise
6067 (last one is also multiline), run all in 'exec' mode
6168
62- * otherwise (last one is also multiline), run all in 'exec' mode as a single
69+ * otherwise (last block is also multiline), run all in 'exec' mode as a single
6370 unit.
6471
6572
0 commit comments