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)
| 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 |