Important

This documentation covers IPython versions 6.0 and higher. Beginning with version 6.0, IPython stopped supporting compatibility with Python versions lower than 3.3 including all versions of Python 2.7.

If you are looking for an IPython version compatible with Python 2.7, please use the IPython 5.x LTS release and refer to its documentation (LTS is the long term support release).

Module: core.inputsplitter

Input handling and transformation machinery.

The first class in this module, InputSplitter, is designed to tell when input from a line-oriented frontend is complete and should be executed, and when the user should be prompted for another line of code instead. The name ‘input splitter’ is largely for historical reasons.

A companion, IPythonInputSplitter, provides the same functionality but with full support for the extended IPython syntax (magics, system calls, etc). The code to actually do these transformations is in IPython.core.inputtransformer. IPythonInputSplitter feeds the raw code to the transformers in order and stores the results.

For more details, see the class docstrings below.

4 Classes

class IPython.core.inputsplitter.IncompleteString(s, start, end, line)

Bases: object

__init__(s, start, end, line)
class IPython.core.inputsplitter.InMultilineStatement(pos, line)

Bases: object

__init__(pos, line)
class IPython.core.inputsplitter.InputSplitter

Bases: object

An object that can accumulate lines of Python source before execution.

This object is designed to be fed python source line-by-line, using push(). It will return on each push whether the currently pushed code could be executed already. In addition, it provides a method called push_accepts_more() that can be used to query whether more input can be pushed into a single interactive block.

This is a simple example of how an interactive terminal-based client can use this tool:

isp = InputSplitter()
while isp.push_accepts_more():
    indent = ' '*isp.indent_spaces
    prompt = '>>> ' + indent
    line = indent + raw_input(prompt)
    isp.push(line)
print 'Input source was:\n', isp.source_reset(),
__init__()

Create a new InputSplitter instance.

check_complete(source)

Return whether a block of code is ready to execute, or should be continued

This is a non-stateful API, and will reset the state of this InputSplitter.

Parameters:source (string) – Python input code, which can be multiline.
Returns:
  • status (str) – One of ‘complete’, ‘incomplete’, or ‘invalid’ if source is not a prefix of valid code.
  • indent_spaces (int or None) – The number of spaces by which to indent the next line of code. If status is not ‘incomplete’, this is None.
push(lines)

Push one or more lines of input.

This stores the given lines and returns a status code indicating whether the code forms a complete Python block or not.

Any exceptions generated in compilation are swallowed, but if an exception was produced, the method returns True.

Parameters:lines (string) – One or more lines of Python input.
Returns:is_complete – True if the current input source (the result of the current input plus prior inputs) forms a complete Python execution block. Note that this value is also stored as a private attribute (_is_complete), so it can be queried at any time.
Return type:boolean
push_accepts_more()

Return whether a block of interactive input can accept more input.

This method is meant to be used by line-oriented frontends, who need to guess whether a block is complete or not based solely on prior and current input lines. The InputSplitter considers it has a complete interactive block and will not accept more input when either:

  • A SyntaxError is raised
  • The code is complete and consists of a single line or a single non-compound statement
  • The code is complete and has a blank line at the end

If the current input produces a syntax error, this method immediately returns False but does not raise the syntax error exception, as typically clients will want to send invalid syntax to an execution backend which might convert the invalid syntax into valid Python via one of the dynamic IPython mechanisms.

reset()

Reset the input buffer and associated state.

source_reset()

Return the input source and perform a full reset.

class IPython.core.inputsplitter.IPythonInputSplitter(line_input_checker=True, physical_line_transforms=None, logical_line_transforms=None, python_line_transforms=None)

Bases: IPython.core.inputsplitter.InputSplitter

An input splitter that recognizes all of IPython’s special syntax.

__init__(line_input_checker=True, physical_line_transforms=None, logical_line_transforms=None, python_line_transforms=None)
push(lines)

Push one or more lines of IPython input.

This stores the given lines and returns a status code indicating whether the code forms a complete Python block or not, after processing all input lines for special IPython syntax.

Any exceptions generated in compilation are swallowed, but if an exception was produced, the method returns True.

Parameters:lines (string) – One or more lines of Python input.
Returns:is_complete – True if the current input source (the result of the current input plus prior inputs) forms a complete Python execution block. Note that this value is also stored as a private attribute (_is_complete), so it can be queried at any time.
Return type:boolean
raw_reset()

Return raw input only and perform a full reset.

reset()

Reset the input buffer and associated state.

transform_cell(cell)

Process and translate a cell of input.

transforms

Quick access to all transformers.

transforms_in_use

Transformers, excluding logical line transformers if we’re in a Python line.

7 Functions

IPython.core.inputsplitter.num_ini_spaces(s)

Return the number of initial spaces in a string.

Note that tabs are counted as a single space. For now, we do not support mixing of tabs and spaces in the user’s input.

Parameters:s (string) –
Returns:n
Return type:int
IPython.core.inputsplitter.partial_tokens(s)

Iterate over tokens from a possibly-incomplete string of code.

This adds two special token types: INCOMPLETE_STRING and IN_MULTILINE_STATEMENT. These can only occur as the last token yielded, and represent the two main ways for code to be incomplete.

IPython.core.inputsplitter.find_next_indent(code)

Find the number of spaces for the next line of indentation

IPython.core.inputsplitter.last_blank(src)

Determine if the input source ends in a blank.

A blank is either a newline or a line consisting of whitespace.

Parameters:src (string) – A single or multiline string.
IPython.core.inputsplitter.last_two_blanks(src)

Determine if the input source ends in two blanks.

A blank is either a newline or a line consisting of whitespace.

Parameters:src (string) – A single or multiline string.
IPython.core.inputsplitter.remove_comments(src)

Remove all comments from input source.

Note: comments are NOT recognized inside of strings!

Parameters:src (string) – A single or multiline input string.
Returns:
Return type:String with all Python comments removed.
IPython.core.inputsplitter.get_input_encoding()

Return the default standard input encoding.

If sys.stdin has no encoding, ‘ascii’ is returned.