##// END OF EJS Templates
Backport PR #12456: Allow to mark transformers as having side effects
Matthias Bussonnier -
Show More
@@ -0,0 +1,2 b''
1 input_transformers can now have an attribute ``has_side_effects`` set to `True`, which will prevent the
2 transformers from being ran when IPython is trying to guess whether the user input is complete.
@@ -634,7 +634,8 b' class TransformerManager:'
634
634
635 try:
635 try:
636 for transform in self.cleanup_transforms:
636 for transform in self.cleanup_transforms:
637 lines = transform(lines)
637 if not getattr(transform, 'has_side_effects', False):
638 lines = transform(lines)
638 except SyntaxError:
639 except SyntaxError:
639 return 'invalid', None
640 return 'invalid', None
640
641
@@ -647,7 +648,8 b' class TransformerManager:'
647
648
648 try:
649 try:
649 for transform in self.line_transforms:
650 for transform in self.line_transforms:
650 lines = transform(lines)
651 if not getattr(transform, 'has_side_effects', False):
652 lines = transform(lines)
651 lines = self.do_token_transforms(lines)
653 lines = self.do_token_transforms(lines)
652 except SyntaxError:
654 except SyntaxError:
653 return 'invalid', None
655 return 'invalid', None
@@ -289,4 +289,38 b' def test_check_complete_II():'
289 def test_null_cleanup_transformer():
289 def test_null_cleanup_transformer():
290 manager = ipt2.TransformerManager()
290 manager = ipt2.TransformerManager()
291 manager.cleanup_transforms.insert(0, null_cleanup_transformer)
291 manager.cleanup_transforms.insert(0, null_cleanup_transformer)
292 nt.assert_is(manager.transform_cell(""), "")
292 assert manager.transform_cell("") == ""
293
294
295
296
297 def test_side_effects_I():
298 count = 0
299 def counter(lines):
300 nonlocal count
301 count += 1
302 return lines
303
304 counter.has_side_effects = True
305
306 manager = ipt2.TransformerManager()
307 manager.cleanup_transforms.insert(0, counter)
308 assert manager.check_complete("a=1\n") == ('complete', None)
309 assert count == 0
310
311
312
313
314 def test_side_effects_II():
315 count = 0
316 def counter(lines):
317 nonlocal count
318 count += 1
319 return lines
320
321 counter.has_side_effects = True
322
323 manager = ipt2.TransformerManager()
324 manager.line_transforms.insert(0, counter)
325 assert manager.check_complete("b=1\n") == ('complete', None)
326 assert count == 0
@@ -689,6 +689,30 b' class TestAstTransform(unittest.TestCase):'
689 with tt.AssertPrints("8"):
689 with tt.AssertPrints("8"):
690 ip.run_cell("amacro")
690 ip.run_cell("amacro")
691
691
692 class TestMiscTransform(unittest.TestCase):
693
694
695 def test_transform_only_once(self):
696 cleanup = 0
697 line_t = 0
698 def count_cleanup(lines):
699 nonlocal cleanup
700 cleanup += 1
701 return lines
702
703 def count_line_t(lines):
704 nonlocal line_t
705 line_t += 1
706 return lines
707
708 ip.input_transformer_manager.cleanup_transforms.append(count_cleanup)
709 ip.input_transformer_manager.line_transforms.append(count_line_t)
710
711 ip.run_cell('1')
712
713 assert cleanup == 1
714 assert line_t == 1
715
692 class IntegerWrapper(ast.NodeTransformer):
716 class IntegerWrapper(ast.NodeTransformer):
693 """Wraps all integers in a call to Integer()"""
717 """Wraps all integers in a call to Integer()"""
694
718
@@ -62,6 +62,14 b' To start using this::'
62 ip = get_ipython()
62 ip = get_ipython()
63 ip.input_transformers_cleanup.append(reverse_line_chars)
63 ip.input_transformers_cleanup.append(reverse_line_chars)
64
64
65 .. versionadded:: 7.17
66
67 input_transformers can now have an attribute ``has_side_effects`` set to
68 `True`, which will prevent the transformers from being ran when IPython is
69 trying to guess whether the user input is complete.
70
71
72
65 AST transformations
73 AST transformations
66 ===================
74 ===================
67
75
General Comments 0
You need to be logged in to leave comments. Login now