From f5c69f3eb2ea43d363e30c73b83209e13e953139 Mon Sep 17 00:00:00 2001 From: Ran Benita Date: Wed, 1 Jul 2020 20:20:13 +0300 Subject: [PATCH] code/source: inline getsource() The recursive way in which Source and getsource interact is a bit confusing, just inline it. --- src/_pytest/_code/source.py | 10 +++------- testing/code/test_source.py | 6 ++---- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index 6cb602a93..65560be2a 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -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() diff --git a/testing/code/test_source.py b/testing/code/test_source.py index 11f2f53cf..ea5b7a4a5 100644 --- a/testing/code/test_source.py +++ b/testing/code/test_source.py @@ -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