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

Skip to content

Commit 6edc05a

Browse files
authored
Merge pull request #13436 from Carreau/fix-async-with
Fix and test for "async with does not allow new lines".
2 parents 2605402 + fcb41df commit 6edc05a

2 files changed

Lines changed: 38 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: 32 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
@@ -291,6 +292,7 @@ def test_check_complete_param(code, expected, number):
291292
assert cc(code) == (expected, number)
292293

293294

295+
@pytest.mark.xfail(platform.python_implementation() == "PyPy", reason="fail on pypy")
294296
@pytest.mark.xfail(
295297
reason="Bug in python 3.9.8 – bpo 45738",
296298
condition=sys.version_info in [(3, 9, 8, "final", 0), (3, 11, 0, "alpha", 2)],
@@ -319,33 +321,48 @@ def test_check_complete():
319321
assert cc("def f():\n x=0\n \\\n ") == ("incomplete", 2)
320322

321323

322-
def test_check_complete_II():
324+
@pytest.mark.xfail(platform.python_implementation() == "PyPy", reason="fail on pypy")
325+
@pytest.mark.parametrize(
326+
"value, expected",
327+
[
328+
('''def foo():\n """''', ("incomplete", 4)),
329+
("""async with example:\n pass""", ("incomplete", 4)),
330+
("""async with example:\n pass\n """, ("complete", None)),
331+
],
332+
)
333+
def test_check_complete_II(value, expected):
323334
"""
324335
Test that multiple line strings are properly handled.
325336
326337
Separate test function for convenience
327338
328339
"""
329340
cc = ipt2.TransformerManager().check_complete
330-
assert cc('''def foo():\n """''') == ("incomplete", 4)
331-
332-
333-
def test_check_complete_invalidates_sunken_brackets():
341+
assert cc(value) == expected
342+
343+
344+
@pytest.mark.parametrize(
345+
"value, expected",
346+
[
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+
("()](", ("invalid", None)),
357+
],
358+
)
359+
def test_check_complete_invalidates_sunken_brackets(value, expected):
334360
"""
335361
Test that a single line with more closing brackets than the opening ones is
336362
interpreted as invalid
337363
"""
338364
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)
365+
assert cc(value) == expected
349366

350367

351368
def test_null_cleanup_transformer():

0 commit comments

Comments
 (0)