Show More
@@ -12,31 +12,15 b' The code to actually do these transformations is in :mod:`IPython.core.inputtran' | |||
|
12 | 12 | and stores the results. |
|
13 | 13 | |
|
14 | 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 | #----------------------------------------------------------------------------- | |
|
31 | # Imports | |
|
32 | #----------------------------------------------------------------------------- | |
|
33 | # stdlib | |
|
17 | # Copyright (c) IPython Development Team. | |
|
18 | # Distributed under the terms of the Modified BSD License. | |
|
34 | 19 | import ast |
|
35 | 20 | import codeop |
|
36 | 21 | import re |
|
37 | 22 | import sys |
|
38 | 23 | |
|
39 | # IPython modules | |
|
40 | 24 | from IPython.utils.py3compat import cast_unicode |
|
41 | 25 | from IPython.core.inputtransformer import (leading_indent, |
|
42 | 26 | classic_prompt, |
@@ -512,18 +496,42 b' class IPythonInputSplitter(InputSplitter):' | |||
|
512 | 496 | |
|
513 | 497 | def flush_transformers(self): |
|
514 | 498 | def _flush(transform, out): |
|
515 | if out is not None: | |
|
516 | tmp = transform.push(out) | |
|
517 | return tmp or transform.reset() or None | |
|
518 | else: | |
|
519 | return transform.reset() or None | |
|
499 | """yield transformed lines | |
|
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 | 529 | for t in self.transforms_in_use: |
|
523 | 530 | out = _flush(t, out) |
|
524 | 531 | |
|
525 |
|
|
|
526 | self._store(out) | |
|
532 | out = list(out) | |
|
533 | if out: | |
|
534 | self._store('\n'.join(out)) | |
|
527 | 535 | |
|
528 | 536 | def raw_reset(self): |
|
529 | 537 | """Return raw input only and perform a full reset. |
@@ -1,31 +1,18 b'' | |||
|
1 | 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 | 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 | #----------------------------------------------------------------------------- | |
|
18 | # Imports | |
|
19 | #----------------------------------------------------------------------------- | |
|
20 | # stdlib | |
|
6 | # Copyright (c) IPython Development Team. | |
|
7 | # Distributed under the terms of the Modified BSD License. | |
|
8 | ||
|
21 | 9 | import unittest |
|
22 | 10 | import sys |
|
23 | 11 | |
|
24 | # Third party | |
|
25 | 12 | import nose.tools as nt |
|
26 | 13 | |
|
27 | # Our own | |
|
28 | 14 | from IPython.core import inputsplitter as isp |
|
15 | from IPython.core.inputtransformer import InputTransformer | |
|
29 | 16 | from IPython.core.tests.test_inputtransformer import syntax, syntax_ml |
|
30 | 17 | from IPython.testing import tools as tt |
|
31 | 18 | from IPython.utils import py3compat |
@@ -467,7 +454,32 b' class IPythonInputTestCase(InputSplitterTestCase):' | |||
|
467 | 454 | out = isp.transform_cell(raw) |
|
468 | 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 | 485 | # Main - use as a script, mostly for developer experiments |
General Comments 0
You need to be logged in to leave comments.
Login now