This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients Henry Schreiner, Mark.Shannon, Skylion007, eric.snow, gregory.p.smith, pablogsal, terry.reedy, vstinner
Date 2022-02-07.16:15:32
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1644250532.57.0.985687695893.issue46166@roundup.psfhosted.org>
In-reply-to
Content
Example of C code that I added to _testcapi:
---
static PyObject *
get_caller_locals(PyObject *self, PyObject *Py_UNUSED(args))
{
    PyFrameObject *frame = PyThreadState_GetFrame(PyThreadState_Get());
    if (frame == NULL) {
        Py_RETURN_NONE;
    }
    return PyObject_GetAttrString(frame, "f_locals");
}
---

Python example:
---
import _testcapi

def f():
    x = 1
    y = 2
    print(_testcapi.get_caller_locals())

f()
---

Output on Python 3.11:
---
{'x': 1, 'y': 2}
---

=> it just works.

A PyFrameObject is a regular Python object, you can use functions like PyObject_GetAttrString().

Maybe I missed something, correct me if I'm wrong.
History
Date User Action Args
2022-02-07 16:15:32vstinnersetrecipients: + vstinner, terry.reedy, gregory.p.smith, Mark.Shannon, eric.snow, Henry Schreiner, pablogsal, Skylion007
2022-02-07 16:15:32vstinnersetmessageid: <1644250532.57.0.985687695893.issue46166@roundup.psfhosted.org>
2022-02-07 16:15:32vstinnerlinkissue46166 messages
2022-02-07 16:15:32vstinnercreate