Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb6563d

Browse files
committed
Fix and test for "async with does not allow new lines".
Use the opportunity to add a test, and parametrise a few other, plus set the correct stacklevel. Closes #12975
1 parent c7def1d commit cb6563d

2 files changed

Lines changed: 37 additions & 21 deletions

File tree

IPython/core/inputtransformer2.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -507,9 +507,12 @@ def make_tokens_by_line(lines:List[str]):
507507

508508
# reexported from token on 3.7+
509509
NEWLINE, NL = tokenize.NEWLINE, tokenize.NL # type: ignore
510-
tokens_by_line:List[List[Any]] = [[]]
511-
if len(lines) > 1 and not lines[0].endswith(('\n', '\r', '\r\n', '\x0b', '\x0c')):
512-
warnings.warn("`make_tokens_by_line` received a list of lines which do not have lineending markers ('\\n', '\\r', '\\r\\n', '\\x0b', '\\x0c'), behavior will be unspecified")
510+
tokens_by_line: List[List[Any]] = [[]]
511+
if len(lines) > 1 and not lines[0].endswith(("\n", "\r", "\r\n", "\x0b", "\x0c")):
512+
warnings.warn(
513+
"`make_tokens_by_line` received a list of lines which do not have lineending markers ('\\n', '\\r', '\\r\\n', '\\x0b', '\\x0c'), behavior will be unspecified",
514+
stacklevel=2,
515+
)
513516
parenlev = 0
514517
try:
515518
for token in tokenize.generate_tokens(iter(lines).__next__):
@@ -782,9 +785,6 @@ def __init__(self, extra_flags=0):
782785
super().__init__()
783786
self.flags |= extra_flags
784787

785-
def __call__(self, *args, **kwds):
786-
return compile(*args, **kwds)
787-
788788

789789
class MaybeAsyncCommandCompiler(CommandCompiler):
790790
def __init__(self, extra_flags=0):

IPython/core/tests/test_inputtransformer2.py

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
more complex. See test_inputtransformer2_line for tests for line-based
55
transformations.
66
"""
7+
import platform
78
import string
89
import sys
910
from textwrap import dedent
@@ -319,33 +320,48 @@ def test_check_complete():
319320
assert cc("def f():\n x=0\n \\\n ") == ("incomplete", 2)
320321

321322

322-
def test_check_complete_II():
323+
@pytest.mark.skipif(platform.python_implementation() == "PyPy", reason="fail on pypy")
324+
@pytest.mark.parametrize(
325+
"value, expected",
326+
[
327+
('''def foo():\n """''', ("incomplete", 4)),
328+
("""async with example:\n pass""", ("incomplete", 4)),
329+
("""async with example:\n pass\n """, ("complete", None)),
330+
],
331+
)
332+
def test_check_complete_II(value, expected):
323333
"""
324334
Test that multiple line strings are properly handled.
325335
326336
Separate test function for convenience
327337
328338
"""
329339
cc = ipt2.TransformerManager().check_complete
330-
assert cc('''def foo():\n """''') == ("incomplete", 4)
331-
332-
333-
def test_check_complete_invalidates_sunken_brackets():
340+
assert cc(value) == expected
341+
342+
343+
@pytest.mark.parametrize(
344+
"value, expected",
345+
[
346+
(")", ("invalid", None)),
347+
("]", ("invalid", None)),
348+
("}", ("invalid", None)),
349+
(")(", ("invalid", None)),
350+
("][", ("invalid", None)),
351+
("}{", ("invalid", None)),
352+
("]()(", ("invalid", None)),
353+
("())(", ("invalid", None)),
354+
(")[](", ("invalid", None)),
355+
("()](", ("invalid", None)),
356+
],
357+
)
358+
def test_check_complete_invalidates_sunken_brackets(value, expected):
334359
"""
335360
Test that a single line with more closing brackets than the opening ones is
336361
interpreted as invalid
337362
"""
338363
cc = ipt2.TransformerManager().check_complete
339-
assert cc(")") == ("invalid", None)
340-
assert cc("]") == ("invalid", None)
341-
assert cc("}") == ("invalid", None)
342-
assert cc(")(") == ("invalid", None)
343-
assert cc("][") == ("invalid", None)
344-
assert cc("}{") == ("invalid", None)
345-
assert cc("]()(") == ("invalid", None)
346-
assert cc("())(") == ("invalid", None)
347-
assert cc(")[](") == ("invalid", None)
348-
assert cc("()](") == ("invalid", None)
364+
assert cc(value) == expected
349365

350366

351367
def test_null_cleanup_transformer():

0 commit comments

Comments
 (0)