---
title: inputtransformer2
description: Notebook syntax from IPython/core/inputtransformer2.py and IPython/utils/tokenutil.py.
---
## generate\_tokens\_catch\_errors

```python
def generate_tokens_catch_errors(
    readline, extra_errors_to_catch:list[str] | None=None
):
```

## leading\_empty\_lines

```python
def leading_empty_lines(
    lines
):
```

*Remove leading empty lines*

If the leading lines are empty or contain only whitespace, they will be
removed.

## leading\_indent

```python
def leading_indent(
    lines
):
```

*Remove leading indentation.*

Removes the minimum common leading indentation from all lines.

## cell\_magic

```python
def cell_magic(
    lines
):
```

## find\_end\_of\_continued\_line

```python
def find_end_of_continued_line(
    lines, start_line:int
):
```

*Find the last line of a line explicitly extended using backslashes.*

Uses 0-indexed line numbers.

## assemble\_continued\_line

```python
def assemble_continued_line(
    lines, start:tuple[int, int], end_line:int
):
```

*Assemble a single line from multiple continued line pieces*

Continued lines are lines ending in `\`, and the line following the last
`\` in the block.

For example, this code continues over multiple lines::

```
if (assign_ix is not None) \
     and (len(line) > assign_ix + 2) \
     and (line[assign_ix+1].string == '%') \
     and (line[assign_ix+2].type == tokenize.NAME):
```

This statement contains four continued line pieces.
Assembling these pieces into a single line would give::

```
if (assign_ix is not None) and (len(line) > assign_ix + 2) and (line[...
```

This uses 0-indexed line numbers. *start* is (lineno, colno).

Used to allow `%magic` and `!system` commands to be continued over
multiple lines.

## TokenTransformBase

```python
def TokenTransformBase(
    start
):
```

*Base class for transformations which examine tokens.*

Special syntax should not be transformed when it occurs inside strings or
comments. This is hard to reliably avoid with regexes. The solution is to
tokenise the code as Python, and recognise the special syntax in the tokens.

IPython's special syntax is not valid Python syntax, so tokenising may go
wrong after the special syntax starts. These classes therefore find and
transform *one* instance of special syntax at a time into regular Python
syntax. After each transformation, tokens are regenerated to find the next
piece of special syntax.

Subclasses need to implement one class method (find)
and one regular method (transform).

The priority attribute can select which transformation to apply if multiple
transformers match in the same place. Lower numbers have higher priority.
This allows "%magic?" to be turned into a help call rather than a magic call.

### TokenTransformBase.sortby

```python
def sortby():
```

### TokenTransformBase.find

```python
def find(
    tokens_by_line
):
```

*Find one instance of special syntax in the provided tokens.*

Tokens are grouped into logical lines for convenience,
so it is easy to e.g. look at the first token of each line.
*tokens\_by\_line* is a list of lists of tokenize.TokenInfo objects.

This should return an instance of its class, pointing to the start
position it has found, or None if it found no match.

### TokenTransformBase.transform

```python
def transform(
    lines:list[str]
):
```

*Transform one instance of special syntax found by `find()`*

Takes a list of strings representing physical lines,
returns a similar list of transformed lines.

## MagicAssign

```python
def MagicAssign(
    start
):
```

*Transformer for assignments from magics (a \= %foo)*

### MagicAssign.find

```python
def find(
    tokens_by_line
):
```

*Find the first magic assignment (a \= %foo) in the cell.*

### MagicAssign.transform

```python
def transform(
    lines
):
```

*Transform one instance of special syntax found by `find()`*

Takes a list of strings representing physical lines,
returns a similar list of transformed lines.

## SystemAssign

```python
def SystemAssign(
    start
):
```

*Transformer for assignments from system commands (a \= !foo)*

### SystemAssign.find\_pre\_312

```python
def find_pre_312(
    tokens_by_line
):
```

### SystemAssign.find\_post\_312

```python
def find_post_312(
    tokens_by_line
):
```

### SystemAssign.find

```python
def find(
    tokens_by_line
):
```

*Find the first system assignment (a \= !foo) in the cell.*

### SystemAssign.transform

```python
def transform(
    lines
):
```

*Transform one instance of special syntax found by `find()`*

Takes a list of strings representing physical lines,
returns a similar list of transformed lines.

## EscapedCommand

```python
def EscapedCommand(
    start
):
```

*Transformer for escaped commands like %foo and !foo.*

### EscapedCommand.find

```python
def find(
    tokens_by_line
):
```

*Find the first escaped command (%foo, !foo, etc.) in the cell.*

### EscapedCommand.transform

```python
def transform(
    lines
):
```

*Transform one instance of special syntax found by `find()`*

Takes a list of strings representing physical lines,
returns a similar list of transformed lines.

## HelpEnd

```python
def HelpEnd(
    start, q_locn
):
```

*Transformer for help syntax: obj? and obj??*

### HelpEnd.find

```python
def find(
    tokens_by_line
):
```

*Find the first help command (foo?) in the cell.*

### HelpEnd.transform

```python
def transform(
    lines
):
```

*Transform a help command found by the `find()` classmethod.*

## make\_tokens\_by\_line

```python
def make_tokens_by_line(
    lines:list[str]
):
```

*Group tokens by logical line, keeping multiline expressions and strings together. Pass `source.splitlines(keepends=True)`.*

## TransformerManager

```python
def TransformerManager():
```

*Apply line and token transformations to a cell.*

### TransformerManager.do\_one\_token\_transform

```python
def do_one_token_transform(
    lines
):
```

*Find and run the transform earliest in the code.*

Returns (changed, lines).

This method is called repeatedly until changed is False, indicating
that all available transformations are complete.

The tokens following IPython special syntax might not be valid, so
the transformed code is retokenised every time to identify the next
piece of special syntax. Hopefully long code cells are mostly valid
Python, not using lots of IPython special syntax, so this shouldn't be
a performance issue.

### TransformerManager.do\_token\_transforms

```python
def do_token_transforms(
    lines
):
```

### TransformerManager.transform\_cell

```python
def transform_cell(
    cell:str
)->str:
```

*Transforms a cell of input code*
