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:
parent
ef39115001
commit
f5c69f3eb2
|
@ -30,7 +30,9 @@ class Source:
|
||||||
elif isinstance(obj, str):
|
elif isinstance(obj, str):
|
||||||
self.lines = deindent(obj.split("\n"))
|
self.lines = deindent(obj.split("\n"))
|
||||||
else:
|
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:
|
def __eq__(self, other: object) -> bool:
|
||||||
if not isinstance(other, Source):
|
if not isinstance(other, Source):
|
||||||
|
@ -141,12 +143,6 @@ def getrawcode(obj, trycall: bool = True):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def getsource(obj) -> Source:
|
|
||||||
obj = getrawcode(obj)
|
|
||||||
strsrc = inspect.getsource(obj)
|
|
||||||
return Source(strsrc)
|
|
||||||
|
|
||||||
|
|
||||||
def deindent(lines: Iterable[str]) -> List[str]:
|
def deindent(lines: Iterable[str]) -> List[str]:
|
||||||
return textwrap.dedent("\n".join(lines)).splitlines()
|
return textwrap.dedent("\n".join(lines)).splitlines()
|
||||||
|
|
||||||
|
|
|
@ -307,12 +307,10 @@ if True:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def test_getsource_fallback() -> None:
|
def test_source_fallback() -> None:
|
||||||
from _pytest._code.source import getsource
|
src = Source(x)
|
||||||
|
|
||||||
expected = """def x():
|
expected = """def x():
|
||||||
pass"""
|
pass"""
|
||||||
src = getsource(x)
|
|
||||||
assert str(src) == expected
|
assert str(src) == expected
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue