MCPcopy Create free account
hub / github.com/ipython/ipython / transform_cell

Method transform_cell

IPython/core/interactiveshell.py:3164–3192  ·  view source on GitHub ↗

Transform an input cell before parsing it. Static transformations, implemented in IPython.core.inputtransformer2, deal with things like ``%magic`` and ``!system`` commands. These run on all input. Dynamic transformations, for things like unescaped magics and the exit

(self, raw_cell)

Source from the content-addressed store, hash-verified

3162 return result
3163
3164 def transform_cell(self, raw_cell):
3165 """Transform an input cell before parsing it.
3166
3167 Static transformations, implemented in IPython.core.inputtransformer2,
3168 deal with things like ``%magic`` and ``!system`` commands.
3169 These run on all input.
3170 Dynamic transformations, for things like unescaped magics and the exit
3171 autocall, depend on the state of the interpreter.
3172 These only apply to single line inputs.
3173
3174 These string-based transformations are followed by AST transformations;
3175 see :meth:`transform_ast`.
3176 """
3177 # Static input transformations
3178 cell = self.input_transformer_manager.transform_cell(raw_cell)
3179
3180 if len(cell.splitlines()) == 1:
3181 # Dynamic transformations - only applied for single line commands
3182 with self.builtin_trap:
3183 # use prefilter_lines to handle trailing newlines
3184 # restore trailing newline for ast.parse
3185 cell = self.prefilter_manager.prefilter_lines(cell) + '\n'
3186
3187 lines = cell.splitlines(keepends=True)
3188 for transform in self.input_transformers_post:
3189 lines = transform(lines)
3190 cell = ''.join(lines)
3191
3192 return cell
3193
3194 def transform_ast(self, node):
3195 """Apply the AST transformations from self.ast_transformers

Callers 11

_run_cellMethod · 0.95
should_run_asyncMethod · 0.95
run_cell_asyncMethod · 0.95
test_whole_cellMethod · 0.45
test_no_strip_codingMethod · 0.45
prunMethod · 0.45
timeMethod · 0.45
ip2pyMethod · 0.45

Calls 2

transformFunction · 0.85
prefilter_linesMethod · 0.80

Tested by 6

test_whole_cellMethod · 0.36
test_no_strip_codingMethod · 0.36
ip2pyMethod · 0.36