##// END OF EJS Templates
Merge pull request #1 from ipython/master...
Merge pull request #1 from ipython/master Merged new changes

File last commit:

r25266:7382f0c5
r25329:875db0e2 merge
Show More
execution.rst
72 lines | 2.7 KiB | text/x-rst | RstLexer
MinRK
msg spec 5.0
r16594 .. _execution_semantics:
anatoly techtonik
Expand docs on how cells are executed
r25265 Execution of cells in the IPython kernel
========================================
MinRK
msg spec 5.0
r16594
anatoly techtonik
Expand docs on how cells are executed
r25265 When IPython kernel receives `execute_request <https://jupyter-client.readthedocs.io/en/latest/messaging.html#execute>`_
with user code, it processes the message in the following phases:
MinRK
msg spec 5.0
r16594
1. Fire the ``pre_execute`` event.
Fabio Niephaus
Add support for "finally" event callbacks....
r23909 2. Fire the ``pre_run_cell`` event unless silent is ``True``.
anatoly techtonik
Expand docs on how cells are executed
r25265 3. Execute ``run_cell`` method to preprocess ``code``, compile and run it, see below for details.
MinRK
msg spec 5.0
r16594 4. If execution succeeds, expressions in ``user_expressions`` are computed.
This ensures that any error in the expressions don't affect the main code execution.
Fabio Niephaus
Ensure post event callbacks are always called....
r23982 5. Fire the ``post_execute`` event.
6. Fire the ``post_run_cell`` event unless silent is ``True``.
MinRK
msg spec 5.0
r16594
MinRK
updates per review...
r16665 .. seealso::
Thomas Kluyver
Some doc fixes
r16815 :doc:`/config/callbacks`
MinRK
msg spec 5.0
r16594
anatoly techtonik
Expand docs on how cells are executed
r25265 Running user ``code``
=====================
First, the ``code`` cell is transformed to expand ``%magic`` and ``!system``
anatoly techtonik
Fill in some missing words
r25266 commands by ``IPython.core.inputtransformer2``. Then expanded cell is compiled
using standard Python :func:`compile` function and executed.
anatoly techtonik
Expand docs on how cells are executed
r25265
anatoly techtonik
Fill in some missing words
r25266 Python :func:`compile` function provides ``mode`` argument to select one
of three ways of compiling code:
MinRK
msg spec 5.0
r16594
*single*
Valid for a single interactive statement (though the source can contain
multiple lines, such as a for loop). When compiled in this mode, the
generated bytecode contains special instructions that trigger the calling of
:func:`sys.displayhook` for any expression in the block that returns a value.
This means that a single statement can actually produce multiple calls to
:func:`sys.displayhook`, if for example it contains a loop where each
iteration computes an unassigned expression would generate 10 calls::
for i in range(10):
i**2
*exec*
An arbitrary amount of source code, this is how modules are compiled.
:func:`sys.displayhook` is *never* implicitly called.
*eval*
A single expression that returns a value. :func:`sys.displayhook` is *never*
implicitly called.
The ``code`` field is split into individual blocks each of which is valid for
execution in 'single' mode, and then:
- If there is only a single block: it is executed in 'single' mode.
- If there is more than one block:
anatoly techtonik
Expand docs on how cells are executed
r25265 * if the last block is a single line long, run all but the last in 'exec' mode
MinRK
msg spec 5.0
r16594 and the very last one in 'single' mode. This makes it easy to type simple
expressions at the end to see computed values.
anatoly techtonik
Expand docs on how cells are executed
r25265 * if the last block is no more than two lines long, run all but the last in
MinRK
msg spec 5.0
r16594 'exec' mode and the very last one in 'single' mode. This makes it easy to
type simple expressions at the end to see computed values. - otherwise
(last one is also multiline), run all in 'exec' mode
anatoly techtonik
Expand docs on how cells are executed
r25265 * otherwise (last block is also multiline), run all in 'exec' mode as a single
MinRK
msg spec 5.0
r16594 unit.