MCPcopy
hub / github.com/pytest-dev/pytest / getfslineno

Function getfslineno

src/_pytest/_code/code.py:1542–1573  ·  view source on GitHub ↗

Return source location (path, lineno) for the given object. If the source cannot be determined return ("", -1). The line number is 0-based.

(obj: object)

Source from the content-addressed store, hash-verified

1540
1541
1542def getfslineno(obj: object) -> tuple[str | Path, int]:
1543 """Return source location (path, lineno) for the given object.
1544
1545 If the source cannot be determined return ("", -1).
1546
1547 The line number is 0-based.
1548 """
1549 # xxx let decorators etc specify a sane ordering
1550 # NOTE: this used to be done in _pytest.compat.getfslineno, initially added
1551 # in 6ec13a2b9. It ("place_as") appears to be something very custom.
1552 obj = get_real_func(obj)
1553 if hasattr(obj, "place_as"):
1554 obj = obj.place_as
1555
1556 try:
1557 code = Code.from_function(obj)
1558 except TypeError:
1559 try:
1560 fn = inspect.getsourcefile(obj) or inspect.getfile(obj) # type: ignore[arg-type]
1561 except TypeError:
1562 return "", -1
1563
1564 fspath = (fn and absolutepath(fn)) or ""
1565 lineno = -1
1566 if fspath:
1567 try:
1568 _, lineno = findsource(obj)
1569 except OSError:
1570 pass
1571 return fspath, lineno
1572
1573 return code.path, code.firstlineno
1574
1575
1576def _byte_offset_to_character_offset(str, offset):

Callers 9

reportinfoMethod · 0.90
formatreprMethod · 0.90
_teardown_yield_fixtureFunction · 0.90
get_fslocation_from_itemFunction · 0.90
test_wrapped_getfslinenoFunction · 0.90
test_getfslinenoFunction · 0.90

Calls 4

get_real_funcFunction · 0.90
absolutepathFunction · 0.90
findsourceFunction · 0.90
from_functionMethod · 0.80

Tested by 2

test_wrapped_getfslinenoFunction · 0.72
test_getfslinenoFunction · 0.72