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):
|
||||
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()
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue