Note
This documentation is for a development version of IPython. There may be significant differences from the latest stable release.
The execution of use code consists of the following phases:
Warning
The API for running code before/after the main code block is likely to change soon. Both the pre_runcode_hook and the register_post_execute() are susceptible to modification, as we find a consistent model for both.
To understand how the code field is executed, one must know that Python code can be compiled in one of three modes (controlled by the mode argument to the compile() builtin):
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 sys.displayhook() for any expression in the block that returns a value. This means that a single statement can actually produce multiple calls to 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
The code field is split into individual blocks each of which is valid for execution in ‘single’ mode, and then:
Errors in any registered post_execute functions are reported, and the failing function is removed from the post_execution set so that it does not continue triggering failures.