From e972089e19091ce5a8596be2b627dea347a0c1f3 2020-08-01 00:24:50 From: Kyle Cutler Date: 2020-08-01 00:24:50 Subject: [PATCH] Support CRLF line endings in magic transforms --- diff --git a/IPython/core/inputtransformer2.py b/IPython/core/inputtransformer2.py index afbce23..67be409 100644 --- a/IPython/core/inputtransformer2.py +++ b/IPython/core/inputtransformer2.py @@ -94,7 +94,7 @@ def cell_magic(lines): if re.match(r'%%\w+\?', lines[0]): # This case will be handled by help_end return lines - magic_name, _, first_line = lines[0][2:-1].partition(' ') + magic_name, _, first_line = lines[0][2:].rstrip().partition(' ') body = ''.join(lines[1:]) return ['get_ipython().run_cell_magic(%r, %r, %r)\n' % (magic_name, first_line, body)] @@ -153,8 +153,8 @@ def assemble_continued_line(lines, start: Tuple[int, int], end_line: int): multiple lines. """ parts = [lines[start[0]][start[1]:]] + lines[start[0]+1:end_line+1] - return ' '.join([p[:-2] for p in parts[:-1]] # Strip backslash+newline - + [parts[-1][:-1]]) # Strip newline from last line + return ' '.join([p.rstrip()[:-1] for p in parts[:-1]] # Strip backslash+newline + + [parts[-1].rstrip()]) # Strip newline from last line class TokenTransformBase: """Base class for transformations which examine tokens. diff --git a/IPython/core/tests/test_inputtransformer2.py b/IPython/core/tests/test_inputtransformer2.py index 1ff0fd8..61f183c 100644 --- a/IPython/core/tests/test_inputtransformer2.py +++ b/IPython/core/tests/test_inputtransformer2.py @@ -31,6 +31,16 @@ for a in range(5): get_ipython().run_line_magic('ls', '') """.splitlines(keepends=True)) +CRLF_MAGIC = ([ + "a = f()\n", + "%ls\r\n", + "g()\n" +], (2, 0), [ + "a = f()\n", + "get_ipython().run_line_magic('ls', '')\n", + "g()\n" +]) + MULTILINE_MAGIC_ASSIGN = ("""\ a = f() b = %foo \\ @@ -190,6 +200,7 @@ def test_find_magic_escape(): def test_transform_magic_escape(): check_transform(ipt2.EscapedCommand, MULTILINE_MAGIC) check_transform(ipt2.EscapedCommand, INDENTED_MAGIC) + check_transform(ipt2.EscapedCommand, CRLF_MAGIC) def test_find_autocalls(): for case in [AUTOCALL_QUOTE, AUTOCALL_QUOTE2, AUTOCALL_PAREN]: diff --git a/IPython/core/tests/test_inputtransformer2_line.py b/IPython/core/tests/test_inputtransformer2_line.py index 41b6ed2..263bbd9 100644 --- a/IPython/core/tests/test_inputtransformer2_line.py +++ b/IPython/core/tests/test_inputtransformer2_line.py @@ -114,3 +114,13 @@ def test_leading_empty_lines(): nt.assert_equal( ipt2.leading_empty_lines(sample.splitlines(keepends=True)), expected.splitlines(keepends=True)) + +CRLF_MAGIC = ([ + "%%ls\r\n" +], [ + "get_ipython().run_cell_magic('ls', '', '')\n" +]) + +def test_crlf_magic(): + for sample, expected in [CRLF_MAGIC]: + nt.assert_equal(ipt2.cell_magic(sample), expected) \ No newline at end of file