Revert "Remove unused _pytest.code.Source.isparseable function"
This reverts commit c627ac4e59
.
This commit is contained in:
parent
0fa35960ba
commit
12f74a28fa
|
@ -1 +0,0 @@
|
||||||
Removed unused ``_pytest.code.Source.isparseable`` function.
|
|
|
@ -136,6 +136,26 @@ class Source:
|
||||||
newsource.lines[:] = deindent(self.lines)
|
newsource.lines[:] = deindent(self.lines)
|
||||||
return newsource
|
return newsource
|
||||||
|
|
||||||
|
def isparseable(self, deindent: bool = True) -> bool:
|
||||||
|
""" return True if source is parseable, heuristically
|
||||||
|
deindenting it by default.
|
||||||
|
"""
|
||||||
|
from parser import suite as syntax_checker
|
||||||
|
|
||||||
|
if deindent:
|
||||||
|
source = str(self.deindent())
|
||||||
|
else:
|
||||||
|
source = str(self)
|
||||||
|
try:
|
||||||
|
# compile(source+'\n', "x", "exec")
|
||||||
|
syntax_checker(source + "\n")
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
raise
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return "\n".join(self.lines)
|
return "\n".join(self.lines)
|
||||||
|
|
||||||
|
|
|
@ -121,6 +121,15 @@ def test_syntaxerror_rerepresentation() -> None:
|
||||||
assert ex.value.text == "xyz xyz\n"
|
assert ex.value.text == "xyz xyz\n"
|
||||||
|
|
||||||
|
|
||||||
|
def test_isparseable() -> None:
|
||||||
|
assert Source("hello").isparseable()
|
||||||
|
assert Source("if 1:\n pass").isparseable()
|
||||||
|
assert Source(" \nif 1:\n pass").isparseable()
|
||||||
|
assert not Source("if 1:\n").isparseable()
|
||||||
|
assert not Source(" \nif 1:\npass").isparseable()
|
||||||
|
assert not Source(chr(0)).isparseable()
|
||||||
|
|
||||||
|
|
||||||
class TestAccesses:
|
class TestAccesses:
|
||||||
def setup_class(self) -> None:
|
def setup_class(self) -> None:
|
||||||
self.source = Source(
|
self.source = Source(
|
||||||
|
@ -134,6 +143,7 @@ class TestAccesses:
|
||||||
|
|
||||||
def test_getrange(self) -> None:
|
def test_getrange(self) -> None:
|
||||||
x = self.source[0:2]
|
x = self.source[0:2]
|
||||||
|
assert x.isparseable()
|
||||||
assert len(x.lines) == 2
|
assert len(x.lines) == 2
|
||||||
assert str(x) == "def f(x):\n pass"
|
assert str(x) == "def f(x):\n pass"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue