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