Skip to content

Commit f5c69f3

Browse files
committed
code/source: inline getsource()
The recursive way in which Source and getsource interact is a bit confusing, just inline it.
1 parent ef39115 commit f5c69f3

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

src/_pytest/_code/source.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ def __init__(self, obj: object = None) -> None:
3030
elif isinstance(obj, str):
3131
self.lines = deindent(obj.split("\n"))
3232
else:
33-
self.lines = deindent(getsource(obj).lines)
33+
rawcode = getrawcode(obj)
34+
src = inspect.getsource(rawcode)
35+
self.lines = deindent(src.split("\n"))
3436

3537
def __eq__(self, other: object) -> bool:
3638
if not isinstance(other, Source):
@@ -141,12 +143,6 @@ def getrawcode(obj, trycall: bool = True):
141143
return obj
142144

143145

144-
def getsource(obj) -> Source:
145-
obj = getrawcode(obj)
146-
strsrc = inspect.getsource(obj)
147-
return Source(strsrc)
148-
149-
150146
def deindent(lines: Iterable[str]) -> List[str]:
151147
return textwrap.dedent("\n".join(lines)).splitlines()
152148

testing/code/test_source.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,10 @@ def x():
307307
pass
308308

309309

310-
def test_getsource_fallback() -> None:
311-
from _pytest._code.source import getsource
312-
310+
def test_source_fallback() -> None:
311+
src = Source(x)
313312
expected = """def x():
314313
pass"""
315-
src = getsource(x)
316314
assert str(src) == expected
317315

318316

0 commit comments

Comments
 (0)