##// END OF EJS Templates
Allow to mark transformers as having side effects...
Matthias Bussonnier -
Show More
@@ -637,7 +637,8 b' class TransformerManager:'
637
637
638 try:
638 try:
639 for transform in self.cleanup_transforms:
639 for transform in self.cleanup_transforms:
640 lines = transform(lines)
640 if not getattr(transform, 'has_side_effects', False):
641 lines = transform(lines)
641 except SyntaxError:
642 except SyntaxError:
642 return 'invalid', None
643 return 'invalid', None
643
644
@@ -650,7 +651,8 b' class TransformerManager:'
650
651
651 try:
652 try:
652 for transform in self.line_transforms:
653 for transform in self.line_transforms:
653 lines = transform(lines)
654 if not getattr(transform, 'has_side_effects', False):
655 lines = transform(lines)
654 lines = self.do_token_transforms(lines)
656 lines = self.do_token_transforms(lines)
655 except SyntaxError:
657 except SyntaxError:
656 return 'invalid', None
658 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
General Comments 0
You need to be logged in to leave comments. Login now