Skip to content

Commit 632800a

Browse files
authored
internal: clean up getfslineno (#6656)
2 parents bc49466 + 9c7f1d9 commit 632800a

File tree

8 files changed

+27
-21
lines changed

8 files changed

+27
-21
lines changed

src/_pytest/_code/code.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def path(self) -> Union[py.path.local, str]:
7272
""" return a path object pointing to source code (or a str in case
7373
of OSError / non-existing file).
7474
"""
75+
if not self.raw.co_filename:
76+
return ""
7577
try:
7678
p = py.path.local(self.raw.co_filename)
7779
# maybe don't try this checking

src/_pytest/_code/source.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from bisect import bisect_right
99
from types import CodeType
1010
from types import FrameType
11+
from typing import Any
1112
from typing import Iterator
1213
from typing import List
1314
from typing import Optional
@@ -17,6 +18,7 @@
1718

1819
import py
1920

21+
from _pytest.compat import get_real_func
2022
from _pytest.compat import overload
2123
from _pytest.compat import TYPE_CHECKING
2224

@@ -282,14 +284,21 @@ def compile_( # noqa: F811
282284
return s.compile(filename, mode, flags, _genframe=_genframe)
283285

284286

285-
def getfslineno(obj) -> Tuple[Optional[Union["Literal['']", py.path.local]], int]:
287+
def getfslineno(obj: Any) -> Tuple[Union[str, py.path.local], int]:
286288
""" Return source location (path, lineno) for the given object.
287289
If the source cannot be determined return ("", -1).
288290
289291
The line number is 0-based.
290292
"""
291293
from .code import Code
292294

295+
# xxx let decorators etc specify a sane ordering
296+
# NOTE: this used to be done in _pytest.compat.getfslineno, initially added
297+
# in 6ec13a2b9. It ("place_as") appears to be something very custom.
298+
obj = get_real_func(obj)
299+
if hasattr(obj, "place_as"):
300+
obj = obj.place_as
301+
293302
try:
294303
code = Code(obj)
295304
except TypeError:
@@ -298,18 +307,16 @@ def getfslineno(obj) -> Tuple[Optional[Union["Literal['']", py.path.local]], int
298307
except TypeError:
299308
return "", -1
300309

301-
fspath = fn and py.path.local(fn) or None
310+
fspath = fn and py.path.local(fn) or ""
302311
lineno = -1
303312
if fspath:
304313
try:
305314
_, lineno = findsource(obj)
306315
except IOError:
307316
pass
317+
return fspath, lineno
308318
else:
309-
fspath = code.path
310-
lineno = code.firstlineno
311-
assert isinstance(lineno, int)
312-
return fspath, lineno
319+
return code.path, code.firstlineno
313320

314321

315322
#

src/_pytest/compat.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import attr
2323
import py
2424

25-
import _pytest
2625
from _pytest._io.saferepr import saferepr
2726
from _pytest.outcomes import fail
2827
from _pytest.outcomes import TEST_OUTCOME
@@ -307,16 +306,6 @@ def get_real_method(obj, holder):
307306
return obj
308307

309308

310-
def getfslineno(obj) -> Tuple[Union[str, py.path.local], int]:
311-
# xxx let decorators etc specify a sane ordering
312-
obj = get_real_func(obj)
313-
if hasattr(obj, "place_as"):
314-
obj = obj.place_as
315-
fslineno = _pytest._code.getfslineno(obj)
316-
assert isinstance(fslineno[1], int), obj
317-
return fslineno
318-
319-
320309
def getimfunc(func):
321310
try:
322311
return func.__func__

src/_pytest/fixtures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import _pytest
1717
from _pytest._code.code import FormattedExcinfo
1818
from _pytest._code.code import TerminalRepr
19+
from _pytest._code.source import getfslineno
1920
from _pytest._io import TerminalWriter
2021
from _pytest.compat import _format_args
2122
from _pytest.compat import _PytestWrapper
2223
from _pytest.compat import get_real_func
2324
from _pytest.compat import get_real_method
24-
from _pytest.compat import getfslineno
2525
from _pytest.compat import getfuncargnames
2626
from _pytest.compat import getimfunc
2727
from _pytest.compat import getlocation

src/_pytest/mark/structures.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
import attr
88

9+
from .._code.source import getfslineno
910
from ..compat import ascii_escaped
1011
from ..compat import ATTRS_EQ_FIELD
11-
from ..compat import getfslineno
1212
from ..compat import NOTSET
1313
from _pytest.outcomes import fail
1414
from _pytest.warning_types import PytestUnknownMarkWarning

src/_pytest/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from _pytest._code.code import ExceptionChainRepr
1616
from _pytest._code.code import ExceptionInfo
1717
from _pytest._code.code import ReprExceptionInfo
18+
from _pytest._code.source import getfslineno
1819
from _pytest.compat import cached_property
19-
from _pytest.compat import getfslineno
2020
from _pytest.compat import TYPE_CHECKING
2121
from _pytest.config import Config
2222
from _pytest.config import PytestPluginManager

src/_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
from _pytest import fixtures
2121
from _pytest import nodes
2222
from _pytest._code import filter_traceback
23+
from _pytest._code.source import getfslineno
2324
from _pytest.compat import ascii_escaped
2425
from _pytest.compat import get_default_arg_names
2526
from _pytest.compat import get_real_func
26-
from _pytest.compat import getfslineno
2727
from _pytest.compat import getimfunc
2828
from _pytest.compat import getlocation
2929
from _pytest.compat import is_generator

testing/code/test_source.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,14 @@ class B:
524524
B.__name__ = "B2"
525525
assert getfslineno(B)[1] == -1
526526

527+
co = compile("...", "", "eval")
528+
assert co.co_filename == ""
529+
530+
if hasattr(sys, "pypy_version_info"):
531+
assert getfslineno(co) == ("", -1)
532+
else:
533+
assert getfslineno(co) == ("", 0)
534+
527535

528536
def test_code_of_object_instance_with_call() -> None:
529537
class A:

0 commit comments

Comments
 (0)