Show More
@@ -66,13 +66,6 b' def cell_magic(lines):' | |||
|
66 | 66 | return ['get_ipython().run_cell_magic(%r, %r, %r)\n' |
|
67 | 67 | % (magic_name, first_line, body)] |
|
68 | 68 | |
|
69 | line_transforms = [ | |
|
70 | leading_indent, | |
|
71 | classic_prompt, | |
|
72 | ipython_prompt, | |
|
73 | cell_magic, | |
|
74 | ] | |
|
75 | ||
|
76 | 69 | # ----- |
|
77 | 70 | |
|
78 | 71 | def _find_assign_op(token_line): |
@@ -378,16 +371,22 b' def show_linewise_tokens(s: str):' | |||
|
378 | 371 | for tokinfo in line: |
|
379 | 372 | print(" ", tokinfo) |
|
380 | 373 | |
|
381 |
class |
|
|
374 | class TransformerManager: | |
|
382 | 375 | def __init__(self): |
|
383 |
self.transform |
|
|
376 | self.line_transforms = [ | |
|
377 | leading_indent, | |
|
378 | classic_prompt, | |
|
379 | ipython_prompt, | |
|
380 | cell_magic, | |
|
381 | ] | |
|
382 | self.token_transformers = [ | |
|
384 | 383 | MagicAssign, |
|
385 | 384 | SystemAssign, |
|
386 | 385 | EscapedCommand, |
|
387 | 386 | HelpEnd, |
|
388 | 387 | ] |
|
389 | 388 | |
|
390 | def do_one_transform(self, lines): | |
|
389 | def do_one_token_transform(self, lines): | |
|
391 | 390 | """Find and run the transform earliest in the code. |
|
392 | 391 | |
|
393 | 392 | Returns (changed, lines). |
@@ -403,7 +402,7 b' class TokenTransformers:' | |||
|
403 | 402 | """ |
|
404 | 403 | tokens_by_line = make_tokens_by_line(lines) |
|
405 | 404 | candidates = [] |
|
406 | for transformer_cls in self.transformers: | |
|
405 | for transformer_cls in self.token_transformers: | |
|
407 | 406 | transformer = transformer_cls.find(tokens_by_line) |
|
408 | 407 | if transformer: |
|
409 | 408 | candidates.append(transformer) |
@@ -415,20 +414,19 b' class TokenTransformers:' | |||
|
415 | 414 | transformer = min(candidates, key=TokenTransformBase.sortby) |
|
416 | 415 | return True, transformer.transform(lines) |
|
417 | 416 | |
|
418 |
def |
|
|
417 | def do_token_transforms(self, lines): | |
|
419 | 418 | while True: |
|
420 | changed, lines = self.do_one_transform(lines) | |
|
419 | changed, lines = self.do_one_token_transform(lines) | |
|
421 | 420 | if not changed: |
|
422 | 421 | return lines |
|
423 | 422 | |
|
424 | ||
|
425 | def transform_cell(cell): | |
|
423 | def transform_cell(self, cell: str): | |
|
426 | 424 | if not cell.endswith('\n'): |
|
427 | 425 | cell += '\n' # Ensure every line has a newline |
|
428 | 426 | lines = cell.splitlines(keepends=True) |
|
429 | for transform in line_transforms: | |
|
427 | for transform in self.line_transforms: | |
|
430 | 428 | #print(transform, lines) |
|
431 | 429 | lines = transform(lines) |
|
432 | 430 | |
|
433 |
lines = |
|
|
431 | lines = self.do_token_transforms(lines) | |
|
434 | 432 | return ''.join(lines) |
@@ -339,10 +339,9 b' class InteractiveShell(SingletonConfigurable):' | |||
|
339 | 339 | input_splitter = Instance('IPython.core.inputsplitter.IPythonInputSplitter', |
|
340 | 340 | (), {'line_input_checker': True}) |
|
341 | 341 | |
|
342 | # This InputSplitter instance is used to transform completed cells before | |
|
343 | # running them. It allows cell magics to contain blank lines. | |
|
344 | input_transformer_manager = Instance('IPython.core.inputsplitter.IPythonInputSplitter', | |
|
345 | (), {'line_input_checker': False}) | |
|
342 | # Used to transform cells before running them. | |
|
343 | input_transformer_manager = Instance('IPython.core.inputtransformer2.TransformerManager', | |
|
344 | ()) | |
|
346 | 345 | |
|
347 | 346 | logstart = Bool(False, help= |
|
348 | 347 | """ |
@@ -832,28 +832,22 b' def test_user_expression():' | |||
|
832 | 832 | class TestSyntaxErrorTransformer(unittest.TestCase): |
|
833 | 833 | """Check that SyntaxError raised by an input transformer is handled by run_cell()""" |
|
834 | 834 | |
|
835 | class SyntaxErrorTransformer(InputTransformer): | |
|
836 | ||
|
837 | def push(self, line): | |
|
835 | @staticmethod | |
|
836 | def transformer(lines): | |
|
837 | for line in lines: | |
|
838 | 838 | pos = line.find('syntaxerror') |
|
839 | 839 | if pos >= 0: |
|
840 | 840 | e = SyntaxError('input contains "syntaxerror"') |
|
841 | 841 | e.text = line |
|
842 | 842 | e.offset = pos + 1 |
|
843 | 843 | raise e |
|
844 |
|
|
|
845 | ||
|
846 | def reset(self): | |
|
847 | pass | |
|
844 | return lines | |
|
848 | 845 | |
|
849 | 846 | def setUp(self): |
|
850 | self.transformer = TestSyntaxErrorTransformer.SyntaxErrorTransformer() | |
|
851 | ip.input_splitter.python_line_transforms.append(self.transformer) | |
|
852 | ip.input_transformer_manager.python_line_transforms.append(self.transformer) | |
|
847 | ip.input_transformer_manager.line_transforms.append(self.transformer) | |
|
853 | 848 | |
|
854 | 849 | def tearDown(self): |
|
855 |
ip.input_ |
|
|
856 | ip.input_transformer_manager.python_line_transforms.remove(self.transformer) | |
|
850 | ip.input_transformer_manager.line_transforms.remove(self.transformer) | |
|
857 | 851 | |
|
858 | 852 | def test_syntaxerror_input_transformer(self): |
|
859 | 853 | with tt.AssertPrints('1234'): |
General Comments 0
You need to be logged in to leave comments.
Login now