[issue46166] Get "self" args or non-null co_varnames from frame object with C-API

Aaron Gokaslan report at bugs.python.org
Mon Feb 7 11:02:06 EST 2022


Aaron Gokaslan <aaronGokaslan at gmail.com> added the comment:

The frame object I am referring to was:

PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());

This frame can not be used with PyObject_GetAttrString. Is there anyway to get the PyObject* associated with a PyFrameObject*? It seems weird that some functionality is just not accessible using the Stable ABI of PyThreadState_GetFrame . 

To elabroate: I was referring to the migration guide in the changelog btw:

    f_code: removed, use PyFrame_GetCode() instead. Warning: the function returns a strong reference, need to call Py_DECREF().

    f_back: changed (see below), use PyFrame_GetBack().

    f_builtins: removed, use PyObject_GetAttrString(frame, "f_builtins").
  
    // this frame object actually has to be a PyObject*, the old one was a  PyFrameObject* . Dropping this in does not work. 
    f_globals: removed, use PyObject_GetAttrString(frame, "f_globals").

    f_locals: removed, use PyObject_GetAttrString(frame, "f_locals").

    f_lasti: removed, use PyObject_GetAttrString(frame, "f_lasti").


I tried importing sys._getframe(), but that gave an attribute error interestingly enough. Run a full code snippit here works: https://github.com/pybind/pybind11/blob/96b943be1d39958661047eadac506745ba92b2bc/include/pybind11/pybind11.h#L2429, but is really slow and we would like avoid having to rely on it. Not to mention relying on a function that is an starts with an underscore seems like it really should be avoided.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46166>
_______________________________________


More information about the Python-bugs-list mailing list