Module: utils.tokenutil

Token-related utilities

1 Class

class IPython.utils.tokenutil.Token(token, text, start, end, line)

Bases: NamedTuple

end: int

Alias for field number 3

line: str

Alias for field number 4

start: int

Alias for field number 2

text: str

Alias for field number 1

token: int

Alias for field number 0

4 Functions

IPython.utils.tokenutil.generate_tokens(readline: Callable) Generator[TokenInfo, None, None]

wrap generate_tkens to catch EOF errors

IPython.utils.tokenutil.generate_tokens_catch_errors(readline, extra_errors_to_catch: list[str] | None = None)
IPython.utils.tokenutil.line_at_cursor(cell: str, cursor_pos: int = 0) tuple[str, int]

Return the line in a cell at a given cursor position

Used for calling line-based APIs that don’t support multi-line input, yet.

Parameters:
  • cell (str) – multiline block of text

  • cursor_pos (integer) – the cursor position

Returns:

(line, offset) – The line with the current cursor, and the character offset of the start of the line.

Return type:

(string, integer)

IPython.utils.tokenutil.token_at_cursor(cell: str, cursor_pos: int = 0) str

Get the token at a given cursor

Used for introspection.

Function calls are prioritized, so the token for the callable will be returned if the cursor is anywhere inside the call.

Parameters:
  • cell (str) – A block of Python code

  • cursor_pos (int) – The location of the cursor in the block where the token should be found