kernel.core.interpreter

Module: kernel.core.interpreter

Inheritance diagram for IPython.kernel.core.interpreter:

Central interpreter object for an IPython engine.

The interpreter is the object whose job is to process lines of user input and actually execute them in the user’s namespace.

Classes

Interpreter

class IPython.kernel.core.interpreter.Interpreter(user_ns=None, global_ns=None, translator=None, magic=None, display_formatters=None, traceback_formatters=None, output_trap=None, history=None, message_cache=None, filename='<string>', config=None)

Bases: object

An interpreter object.

fixme: needs to negotiate available formatters with frontends.

Important: the interpeter should be built so that it exposes a method for each attribute/method of its sub-object. This way it can be replaced by a network adapter.

__init__(user_ns=None, global_ns=None, translator=None, magic=None, display_formatters=None, traceback_formatters=None, output_trap=None, history=None, message_cache=None, filename='<string>', config=None)
complete(line, text=None, pos=None)

Complete the given text.

Parameters:
text : str

Text fragment to be completed on. Typically this is

error(text)

Pass an error message back to the shell.

Parameters:text : str

Notes

This should only be called when self.message is set. In other words, when code is being executed.

execute(commands, raiseException=True)

Execute some IPython commands.

  1. Translate them into Python.
  2. Run them.
  3. Trap stdout/stderr.
  4. Trap sys.displayhook().
  5. Trap exceptions.
  6. Return a message object.
Parameters:

commands : str

The raw commands that the user typed into the prompt.

Returns:

message : dict

The dictionary of responses. See the README.txt in this directory for an explanation of the format.

execute_block(code)

Execute a single block of code in the user namespace.

Return value: a flag indicating whether the code to be run completed successfully:

  • 0: successful execution.
  • 1: an error occurred.
execute_macro(macro)

Execute the value of a macro.

Parameters:macro : Macro
execute_python(python)

Actually run the Python code in the namespace.

Parameters:
python : str
Pure, exec’able Python code. Special IPython commands should have already been translated into pure Python.
feed_block(source, filename='<input>', symbol='single')

Compile some source in the interpreter.

One several things can happen:

1) The input is incorrect; compile_command() raised an exception (SyntaxError or OverflowError).

2) The input is incomplete, and more input is required; compile_command() returned None. Nothing happens.

3) The input is complete; compile_command() returned a code object. The code is executed by calling self.runcode() (which also handles run-time exceptions, except for SystemExit).

The return value is:

  • True in case 2
  • False in the other cases, unless an exception is raised, where

None is returned instead. This can be used by external callers to know whether to continue feeding input or not.

The return value can be used to decide whether to use sys.ps1 or sys.ps2 to prompt the next line.

formatTraceback(et, ev, tb, message='')

Put a formatted version of the traceback into value and reraise.

When exceptions have to be sent over the network, the traceback needs to be put into the value of the exception in a nicely formatted way. The method takes the type, value and tb of an exception and puts a string representation of the tb into the value of the exception and reraises it.

Currently this method uses the ultraTb formatter from IPython trunk. Eventually it should simply use the traceback formatters in core that are loaded into self.tracback_trap.formatters.

generate_prompt(is_continuation)

Calculate and return a string with the prompt to display.

Parameters:
is_continuation : bool

Whether the input line is continuing multiline input or not, so

that a proper continuation prompt can be computed.

getCommand(i=None)

Gets the ith message in the message_cache.

This is implemented here for compatibility with the old ipython1 shell I am not sure we need this though. I even seem to remember that we were going to get rid of it.

ipmagic(arg_string)

Call a magic function by name.

ipmagic(‘name -opt foo bar’) is equivalent to typing at the ipython prompt:

In[1]: %name -opt foo bar

To call a magic without arguments, simply use ipmagic(‘name’).

This provides a proper Python function to call IPython’s magics in any valid Python code you can type at the interpreter, including loops and compound statements. It is added by IPython to the Python builtin namespace upon initialization.

Parameters:

arg_string : str

A string containing the name of the magic function to call and any additional arguments to be passed to the magic.

Returns:

something : object

The return value of the actual object.

ipsystem(command)

Execute a command in a system shell while expanding variables in the current namespace.

Parameters:command : str
pack_exception(message, exc)
pull(keys)

Get an item out of the namespace by key.

Parameters:

key : str

Returns:

value : object

Raises:

TypeError if the key is not a string. :

NameError if the object doesn’t exist. :

pull_function(keys)
push(ns)

Put value into the namespace with name key.

Parameters:**kwds :
push_function(ns)
reset()

Reset the interpreter.

Currently this only resets the users variables in the namespace. In the future we might want to also reset the other stateful things like that the Interpreter has, like In, Out, etc.

set_traps()
Set all of the output, display, and traceback traps.
setup_message()

Return a message object.

This method prepares and returns a message dictionary. This dict contains the various fields that are used to transfer information about execution, results, tracebacks, etc, to clients (either in or out of process ones). Because of the need to work with possibly out of process clients, this dict MUST contain strictly pickle-safe values.

setup_namespace()
Add things to the namespace.
split_commands(python)

Split multiple lines of code into discrete commands that can be executed singly.

Parameters:

python : str

Pure, exec’able Python code.

Returns:

commands : list of str

Separate commands that can be exec’ed independently.

unset_traps()
Unset all of the output, display, and traceback traps.
var_expand(template)

Expand $variables in the current namespace using Itpl.

Parameters:template : str

NotDefined

class IPython.kernel.core.interpreter.NotDefined

Bases: object

__init__()
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Functions

IPython.kernel.core.interpreter.default_display_formatters()
Return a list of default display formatters.
IPython.kernel.core.interpreter.default_traceback_formatters()
Return a list of default traceback formatters.