Show More
@@ -1,65 +1,72 b'' | |||
|
1 | 1 | .. _execution_semantics: |
|
2 | 2 | |
|
3 |
Execution |
|
|
4 |
======================================== |
|
|
3 | Execution of cells in the IPython kernel | |
|
4 | ======================================== | |
|
5 | 5 | |
|
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: | |
|
7 | 8 | |
|
8 | 9 | 1. Fire the ``pre_execute`` event. |
|
9 | 10 | 2. Fire the ``pre_run_cell`` event unless silent is ``True``. |
|
10 |
3. Execute the ``code`` |
|
|
11 | 3. Execute ``run_cell`` method to preprocess ``code``, compile and run it, see below for details. | |
|
11 | 12 | 4. If execution succeeds, expressions in ``user_expressions`` are computed. |
|
12 | 13 | This ensures that any error in the expressions don't affect the main code execution. |
|
13 | 14 | 5. Fire the ``post_execute`` event. |
|
14 | 15 | 6. Fire the ``post_run_cell`` event unless silent is ``True``. |
|
15 | 16 | |
|
16 | 17 | .. seealso:: |
|
17 | 18 | |
|
18 | 19 | :doc:`/config/callbacks` |
|
19 | 20 | |
|
20 | 21 | |
|
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): | |
|
24 | 31 | |
|
25 | 32 | *single* |
|
26 | 33 | Valid for a single interactive statement (though the source can contain |
|
27 | 34 | multiple lines, such as a for loop). When compiled in this mode, the |
|
28 | 35 | generated bytecode contains special instructions that trigger the calling of |
|
29 | 36 | :func:`sys.displayhook` for any expression in the block that returns a value. |
|
30 | 37 | This means that a single statement can actually produce multiple calls to |
|
31 | 38 | :func:`sys.displayhook`, if for example it contains a loop where each |
|
32 | 39 | iteration computes an unassigned expression would generate 10 calls:: |
|
33 | 40 | |
|
34 | 41 | for i in range(10): |
|
35 | 42 | i**2 |
|
36 | 43 | |
|
37 | 44 | *exec* |
|
38 | 45 | An arbitrary amount of source code, this is how modules are compiled. |
|
39 | 46 | :func:`sys.displayhook` is *never* implicitly called. |
|
40 | 47 | |
|
41 | 48 | *eval* |
|
42 | 49 | A single expression that returns a value. :func:`sys.displayhook` is *never* |
|
43 | 50 | implicitly called. |
|
44 | 51 | |
|
45 | 52 | |
|
46 | 53 | The ``code`` field is split into individual blocks each of which is valid for |
|
47 | 54 | execution in 'single' mode, and then: |
|
48 | 55 | |
|
49 | 56 | - If there is only a single block: it is executed in 'single' mode. |
|
50 | 57 | |
|
51 | 58 | - If there is more than one block: |
|
52 | 59 | |
|
53 |
* if the last |
|
|
60 | * if the last block is a single line long, run all but the last in 'exec' mode | |
|
54 | 61 | and the very last one in 'single' mode. This makes it easy to type simple |
|
55 | 62 | expressions at the end to see computed values. |
|
56 | 63 | |
|
57 |
* if the last |
|
|
64 | * if the last block is no more than two lines long, run all but the last in | |
|
58 | 65 | 'exec' mode and the very last one in 'single' mode. This makes it easy to |
|
59 | 66 | type simple expressions at the end to see computed values. - otherwise |
|
60 | 67 | (last one is also multiline), run all in 'exec' mode |
|
61 | 68 | |
|
62 |
* otherwise (last |
|
|
69 | * otherwise (last block is also multiline), run all in 'exec' mode as a single | |
|
63 | 70 | unit. |
|
64 | 71 | |
|
65 | 72 |
General Comments 0
You need to be logged in to leave comments.
Login now