code/source: inline getsource()

The recursive way in which Source and getsource interact is a bit
confusing, just inline it.
This commit is contained in:
Ran Benita 2020-07-01 20:20:13 +03:00
parent ef39115001
commit f5c69f3eb2
2 changed files with 5 additions and 11 deletions

View File

@ -30,7 +30,9 @@ class Source:
elif isinstance(obj, str):
self.lines = deindent(obj.split("\n"))
else:
self.lines = deindent(getsource(obj).lines)
rawcode = getrawcode(obj)
src = inspect.getsource(rawcode)
self.lines = deindent(src.split("\n"))
def __eq__(self, other: object) -> bool:
if not isinstance(other, Source):
@ -141,12 +143,6 @@ def getrawcode(obj, trycall: bool = True):
return obj
def getsource(obj) -> Source:
obj = getrawcode(obj)
strsrc = inspect.getsource(obj)
return Source(strsrc)
def deindent(lines: Iterable[str]) -> List[str]:
return textwrap.dedent("\n".join(lines)).splitlines()

View File

@ -307,12 +307,10 @@ if True:
pass
def test_getsource_fallback() -> None:
from _pytest._code.source import getsource
def test_source_fallback() -> None:
src = Source(x)
expected = """def x():
pass"""
src = getsource(x)
assert str(src) == expected