Message412762
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. |
|
Date |
User |
Action |
Args |
2022-02-07 16:15:32 | vstinner | set | recipients:
+ vstinner, terry.reedy, gregory.p.smith, Mark.Shannon, eric.snow, Henry Schreiner, pablogsal, Skylion007 |
2022-02-07 16:15:32 | vstinner | set | messageid: <1644250532.57.0.985687695893.issue46166@roundup.psfhosted.org> |
2022-02-07 16:15:32 | vstinner | link | issue46166 messages |
2022-02-07 16:15:32 | vstinner | create | |
|