##// END OF EJS Templates
always pass single lines to transform.push...
MinRK -
Show More
@@ -12,31 +12,15 b' The code to actually do these transformations is in :mod:`IPython.core.inputtran'
12 and stores the results.
12 and stores the results.
13
13
14 For more details, see the class docstrings below.
14 For more details, see the class docstrings below.
15
16 Authors
17 -------
18
19 * Fernando Perez
20 * Brian Granger
21 * Thomas Kluyver
22 """
15 """
23 #-----------------------------------------------------------------------------
24 # Copyright (C) 2010 The IPython Development Team
25 #
26 # Distributed under the terms of the BSD License. The full license is in
27 # the file COPYING, distributed as part of this software.
28 #-----------------------------------------------------------------------------
29
16
30 #-----------------------------------------------------------------------------
17 # Copyright (c) IPython Development Team.
31 # Imports
18 # Distributed under the terms of the Modified BSD License.
32 #-----------------------------------------------------------------------------
33 # stdlib
34 import ast
19 import ast
35 import codeop
20 import codeop
36 import re
21 import re
37 import sys
22 import sys
38
23
39 # IPython modules
40 from IPython.utils.py3compat import cast_unicode
24 from IPython.utils.py3compat import cast_unicode
41 from IPython.core.inputtransformer import (leading_indent,
25 from IPython.core.inputtransformer import (leading_indent,
42 classic_prompt,
26 classic_prompt,
@@ -512,18 +496,42 b' class IPythonInputSplitter(InputSplitter):'
512
496
513 def flush_transformers(self):
497 def flush_transformers(self):
514 def _flush(transform, out):
498 def _flush(transform, out):
515 if out is not None:
499 """yield transformed lines
516 tmp = transform.push(out)
517 return tmp or transform.reset() or None
518 else:
519 return transform.reset() or None
520
500
521 out = None
501 always strings, never None
502
503 transform: the current transform
504 out: an iterable of previously transformed inputs.
505 Each may be multiline, which will be passed
506 one line at a time to transform.
507 """
508 anything = False
509 for out in out:
510 anything = True
511 tmp = None
512 for line in out.splitlines():
513 # push one line at a time
514 tmp = transform.push(line)
515 if tmp is not None:
516 yield tmp
517 if tmp is None:
518 # transformer is still consuming, reset
519 tmp = transform.reset()
520 if tmp is not None:
521 yield tmp
522 if not anything:
523 # nothing was pushed, reset
524 tmp = transform.reset()
525 if tmp is not None:
526 yield tmp
527
528 out = []
522 for t in self.transforms_in_use:
529 for t in self.transforms_in_use:
523 out = _flush(t, out)
530 out = _flush(t, out)
524
531
525 if out is not None:
532 out = list(out)
526 self._store(out)
533 if out:
534 self._store('\n'.join(out))
527
535
528 def raw_reset(self):
536 def raw_reset(self):
529 """Return raw input only and perform a full reset.
537 """Return raw input only and perform a full reset.
@@ -1,31 +1,18 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """Tests for the inputsplitter module.
2 """Tests for the inputsplitter module."""
3
3
4 Authors
5 -------
6 * Fernando Perez
7 * Robert Kern
8 """
9 from __future__ import print_function
4 from __future__ import print_function
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2010-2011 The IPython Development Team
12 #
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
16
5
17 #-----------------------------------------------------------------------------
6 # Copyright (c) IPython Development Team.
18 # Imports
7 # Distributed under the terms of the Modified BSD License.
19 #-----------------------------------------------------------------------------
8
20 # stdlib
21 import unittest
9 import unittest
22 import sys
10 import sys
23
11
24 # Third party
25 import nose.tools as nt
12 import nose.tools as nt
26
13
27 # Our own
28 from IPython.core import inputsplitter as isp
14 from IPython.core import inputsplitter as isp
15 from IPython.core.inputtransformer import InputTransformer
29 from IPython.core.tests.test_inputtransformer import syntax, syntax_ml
16 from IPython.core.tests.test_inputtransformer import syntax, syntax_ml
30 from IPython.testing import tools as tt
17 from IPython.testing import tools as tt
31 from IPython.utils import py3compat
18 from IPython.utils import py3compat
@@ -467,7 +454,32 b' class IPythonInputTestCase(InputSplitterTestCase):'
467 out = isp.transform_cell(raw)
454 out = isp.transform_cell(raw)
468 self.assertEqual(out.rstrip(), expected.rstrip())
455 self.assertEqual(out.rstrip(), expected.rstrip())
469
456
470
457 def test_multiline_passthrough(self):
458 isp = self.isp
459 class CommentTransformer(InputTransformer):
460 def __init__(self):
461 self._lines = []
462
463 def push(self, line):
464 self._lines.append(line + '#')
465
466 def reset(self):
467 text = '\n'.join(self._lines)
468 self._lines = []
469 return text
470
471 isp.physical_line_transforms.insert(0, CommentTransformer())
472
473 for raw, expected in [
474 ("a=5", "a=5#"),
475 ("%ls foo", "get_ipython().magic(%r)" % u'ls foo#'),
476 ("!ls foo\n%ls bar", "get_ipython().system(%r)\nget_ipython().magic(%r)" % (
477 u'ls foo#', u'ls bar#'
478 )),
479 ("1\n2\n3\n%ls foo\n4\n5", "1#\n2#\n3#\nget_ipython().magic(%r)\n4#\n5#" % u'ls foo#'),
480 ]:
481 out = isp.transform_cell(raw)
482 self.assertEqual(out.rstrip(), expected.rstrip())
471
483
472 #-----------------------------------------------------------------------------
484 #-----------------------------------------------------------------------------
473 # Main - use as a script, mostly for developer experiments
485 # Main - use as a script, mostly for developer experiments
General Comments 0
You need to be logged in to leave comments. Login now