Module: core.compilerop

Compiler tools with improved interactive support.

Provides compilation machinery similar to codeop, but with caching support so we can provide interactive tracebacks.

Authors

  • Robert Kern

  • Fernando Perez

  • Thomas Kluyver

1 Class

class IPython.core.compilerop.CachingCompiler

Bases: Compile

A compiler that caches code compiled from interactive statements.

__init__()
ast_parse(source: str, filename: str = '<unknown>', symbol: str = 'exec') AST

Parse code to an AST with the current compiler flags active.

Arguments are exactly the same as ast.parse (in the standard library), and are passed to the built-in compile function.

cache(transformed_code: str, number: int = 0, raw_code: str | None = None) str

Make a name for a block of code, and cache the code.

Parameters:
  • transformed_code (str) – The executable Python source code to cache and compile.

  • number (int) – A number which forms part of the code’s name. Used for the execution counter.

  • raw_code (str) – The raw code before transformation, if None, set to transformed_code.

Returns:

  • The name of the cached code (as a string). Pass this as the filename

  • argument to compilation, so that tracebacks are correctly hooked up.

property compiler_flags: int

Flags currently active in the compilation process.

format_code_name(name: str) tuple[str, str] | None

Return a user-friendly label and name for a code block.

Parameters:

name (str) – The name for the code block returned from get_code_name

Return type:

A (label, name) pair that can be used in tracebacks, or None if the default formatting should be used.

get_code_name(raw_code: str, transformed_code: str, number: int) str

Compute filename given the code, and the cell number.

Parameters:
  • raw_code (str) – The raw cell code.

  • transformed_code (str) – The executable Python source code to cache and compile.

  • number (int) – A number which forms part of the code’s name. Used for the execution counter.

Return type:

The computed filename.

reset_compiler_flags() None

Reset compiler flags to default state.

1 Function

IPython.core.compilerop.code_name(code: str, number: int = 0) str

Compute a (probably) unique name for code for caching.

This now expects code to be unicode.