Skip to content

PyCode_Addr2Line and PyFrame_GetLineNumber return -1 #111519

Closed as not planned
Closed as not planned
@notEvil

Description

@notEvil

Bug report

Bug description:

Hi,

while testing line_profiler on 3.12 I saw PyCode_Addr2Line and PyFrame_GetLineNumber return -1. The following minimal example reproduces the issue for me:

line.py
import pyximport

pyximport.install()

import _line
import asyncio


async def f():
    pass


print(_line.lines(f))


async def function_1(bool):
    if bool:
        async for _ in function_2():
            pass


async def function_2():
    if False:
        yield


_line.start()
asyncio.run(function_1(True))
_line.stop()
_line.pyx
cdef extern from "Python.h":
    ctypedef struct PyObject
    ctypedef struct PyCodeObject
    ctypedef struct PyFrameObject
    ctypedef int (*Py_tracefunc)(PyObject* obj, PyFrameObject* frame, int what, PyObject* arg)
    cdef int PyTrace_CALL
    cdef int PyTrace_LINE
    cdef int PyTrace_RETURN
    cdef int PyCode_Addr2Line(PyCodeObject *co, int byte_offset)
    cdef void PyEval_SetTrace(Py_tracefunc func, PyObject* obj)
    cdef int PyFrame_GetLineNumber(PyFrameObject* frame)


def lines(function):
    code = function.__code__
    return [PyCode_Addr2Line(<PyCodeObject*>code, byte_offset) for byte_offset in range(0, len(code.co_code), 2)]


def start():
    PyEval_SetTrace(trace, NULL)


def stop():
    PyEval_SetTrace(NULL, NULL)


cdef int trace(PyObject* obj, PyFrameObject* frame, int what, PyObject* arg) noexcept:
    print(f"{PyFrame_GetLineNumber(frame):3}", {PyTrace_CALL: "call", PyTrace_LINE: "line", PyTrace_RETURN: "return"}.get(what, "other"))

On 3.11 it prints

[9, 9, 9, 10, 10]
 28 line
160 call
184 line
189 line
...
691 return
 63 return
189 return
 29 line

and on 3.12

[9, -1, 9, 10, -1, -1]
 28 line
160 call
188 line
193 line
...
 -1 return
...
702 return
 62 return
193 return
 29 line

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions