From 5924a52e3207bb0f36b2e348514d37efb5e6cf0a Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Wed, 29 Jan 2020 00:53:20 +0100 Subject: [PATCH] getfslineno: skip trying Code It goes through `inspect` for e.g. classes already anyway, and simplifies the code. --- src/_pytest/_code/source.py | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/src/_pytest/_code/source.py b/src/_pytest/_code/source.py index c7b10311427..74fa656b36a 100644 --- a/src/_pytest/_code/source.py +++ b/src/_pytest/_code/source.py @@ -283,27 +283,20 @@ def getfslineno(obj) -> Tuple[Optional[Union["Literal['']", py.path.local]], int The line number is 0-based. """ - from .code import Code - try: - code = Code(obj) + fn = inspect.getsourcefile(obj) or inspect.getfile(obj) # type: ignore[arg-type] # noqa: F821 except TypeError: + return "", -1 + + assert fn, repr(fn) + + fspath = fn and py.path.local(fn) or None + lineno = -1 + if fspath: try: - fn = inspect.getsourcefile(obj) or inspect.getfile(obj) - except TypeError: - return "", -1 - - fspath = fn and py.path.local(fn) or None - lineno = -1 - if fspath: - try: - _, lineno = findsource(obj) - except IOError: - pass - else: - fspath = code.path - lineno = code.firstlineno - assert isinstance(lineno, int) + _, lineno = findsource(obj) + except IOError: + pass return fspath, lineno