Esempio n. 1
0
def test_findsource():
    from _pytest._code.source import findsource
    co = _pytest._code.compile("""if 1:
    def x():
        pass
""")

    src, lineno = findsource(co)
    assert 'if 1:' in str(src)

    d = {}
    eval(co, d)
    src, lineno = findsource(d['x'])
    assert 'if 1:' in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 2
0
def test_findsource():
    from _pytest._code.source import findsource
    co = _pytest._code.compile("""if 1:
    def x():
        pass
""")

    src, lineno = findsource(co)
    assert 'if 1:' in str(src)

    d = {}
    eval(co, d)
    src, lineno = findsource(d['x'])
    assert 'if 1:' in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 3
0
    def fullsource(self):
        """ return a _pytest._code.Source object for the full source file of the code
        """
        from _pytest._code import source

        full, _ = source.findsource(self.raw)
        return full
Esempio n. 4
0
def getfslineno(obj: object) -> Tuple[Union[str, Path], int]:
    """Return source location (path, lineno) for the given object.

    If the source cannot be determined return ("", -1).

    The line number is 0-based.
    """
    # xxx let decorators etc specify a sane ordering
    # NOTE: this used to be done in _pytest.compat.getfslineno, initially added
    #       in 6ec13a2b9.  It ("place_as") appears to be something very custom.
    obj = get_real_func(obj)
    if hasattr(obj, "place_as"):
        obj = obj.place_as  # type: ignore[attr-defined]

    try:
        code = Code.from_function(obj)
    except TypeError:
        try:
            fn = inspect.getsourcefile(obj) or inspect.getfile(
                obj)  # type: ignore[arg-type]
        except TypeError:
            return "", -1

        fspath = fn and absolutepath(fn) or ""
        lineno = -1
        if fspath:
            try:
                _, lineno = findsource(obj)
            except OSError:
                pass
        return fspath, lineno

    return code.path, code.firstlineno
Esempio n. 5
0
def test_findsource_fallback() -> None:
    from _pytest._code.source import findsource

    src, lineno = findsource(x)
    assert src is not None
    assert "test_findsource_simple" in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 6
0
    def fullsource(self):
        """ return a _pytest._code.Source object for the full source file of the code
        """
        from _pytest._code import source

        full, _ = source.findsource(self.raw)
        return full
Esempio n. 7
0
def test_findsource() -> None:
    from _pytest._code.source import findsource

    co = _pytest._code.compile("""if 1:
    def x():
        pass
""")

    src, lineno = findsource(co)
    assert src is not None
    assert "if 1:" in str(src)

    d = {}  # type: Dict[str, Any]
    eval(co, d)
    src, lineno = findsource(d["x"])
    assert src is not None
    assert "if 1:" in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 8
0
def test_findsource(monkeypatch) -> None:
    from _pytest._code.source import findsource

    filename = "<pytest-test_findsource>"
    lines = ["if 1:\n", "    def x():\n", "          pass\n"]
    co = compile("".join(lines), filename, "exec")

    monkeypatch.setitem(linecache.cache, filename, (1, None, lines, filename))

    src, lineno = findsource(co)
    assert src is not None
    assert "if 1:" in str(src)

    d: Dict[str, Any] = {}
    eval(co, d)
    src, lineno = findsource(d["x"])
    assert src is not None
    assert "if 1:" in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 9
0
def test_findsource(monkeypatch) -> None:
    from _pytest._code.source import findsource

    filename = "<pytest-test_findsource>"
    lines = ["if 1:\n", "    def x():\n", "          pass\n"]
    co = compile("".join(lines), filename, "exec")

    # Type ignored because linecache.cache is private.
    monkeypatch.setitem(linecache.cache, filename, (1, None, lines, filename))  # type: ignore[attr-defined]

    src, lineno = findsource(co)
    assert src is not None
    assert "if 1:" in str(src)

    d = {}  # type: Dict[str, Any]
    eval(co, d)
    src, lineno = findsource(d["x"])
    assert src is not None
    assert "if 1:" in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 10
0
 def fullsource(self) -> Optional["Source"]:
     """Return a _pytest._code.Source object for the full source file of the code."""
     full, _ = findsource(self.raw)
     return full
Esempio n. 11
0
def test_findsource_fallback():
    from _pytest._code.source import findsource

    src, lineno = findsource(x)
    assert "test_findsource_simple" in str(src)
    assert src[lineno] == "    def x():"
Esempio n. 12
0
def test_findsource_fallback():
    from _pytest._code.source import findsource
    src, lineno = findsource(x)
    assert 'test_findsource_simple' in str(src)
    assert src[lineno] == '    def x():'