|  | 
|  | 1 | +#include "parts.h" | 
|  | 2 | +#include "util.h" | 
|  | 3 | + | 
|  | 4 | +#include "frameobject.h"          // PyFrame_New() | 
|  | 5 | + | 
|  | 6 | + | 
|  | 7 | +static PyObject * | 
|  | 8 | +frame_getlocals(PyObject *self, PyObject *frame) | 
|  | 9 | +{ | 
|  | 10 | +    if (!PyFrame_Check(frame)) { | 
|  | 11 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 12 | +        return NULL; | 
|  | 13 | +    } | 
|  | 14 | +    return PyFrame_GetLocals((PyFrameObject *)frame); | 
|  | 15 | +} | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +static PyObject * | 
|  | 19 | +frame_getglobals(PyObject *self, PyObject *frame) | 
|  | 20 | +{ | 
|  | 21 | +    if (!PyFrame_Check(frame)) { | 
|  | 22 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 23 | +        return NULL; | 
|  | 24 | +    } | 
|  | 25 | +    return PyFrame_GetGlobals((PyFrameObject *)frame); | 
|  | 26 | +} | 
|  | 27 | + | 
|  | 28 | + | 
|  | 29 | +static PyObject * | 
|  | 30 | +frame_getgenerator(PyObject *self, PyObject *frame) | 
|  | 31 | +{ | 
|  | 32 | +    if (!PyFrame_Check(frame)) { | 
|  | 33 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 34 | +        return NULL; | 
|  | 35 | +    } | 
|  | 36 | +    return PyFrame_GetGenerator((PyFrameObject *)frame); | 
|  | 37 | +} | 
|  | 38 | + | 
|  | 39 | + | 
|  | 40 | +static PyObject * | 
|  | 41 | +frame_getbuiltins(PyObject *self, PyObject *frame) | 
|  | 42 | +{ | 
|  | 43 | +    if (!PyFrame_Check(frame)) { | 
|  | 44 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 45 | +        return NULL; | 
|  | 46 | +    } | 
|  | 47 | +    return PyFrame_GetBuiltins((PyFrameObject *)frame); | 
|  | 48 | +} | 
|  | 49 | + | 
|  | 50 | + | 
|  | 51 | +static PyObject * | 
|  | 52 | +frame_getlasti(PyObject *self, PyObject *frame) | 
|  | 53 | +{ | 
|  | 54 | +    if (!PyFrame_Check(frame)) { | 
|  | 55 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 56 | +        return NULL; | 
|  | 57 | +    } | 
|  | 58 | +    int lasti = PyFrame_GetLasti((PyFrameObject *)frame); | 
|  | 59 | +    if (lasti < 0) { | 
|  | 60 | +        assert(lasti == -1); | 
|  | 61 | +        Py_RETURN_NONE; | 
|  | 62 | +    } | 
|  | 63 | +    return PyLong_FromLong(lasti); | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | + | 
|  | 67 | +static PyObject * | 
|  | 68 | +frame_new(PyObject *self, PyObject *args) | 
|  | 69 | +{ | 
|  | 70 | +    PyObject *code, *globals, *locals; | 
|  | 71 | +    if (!PyArg_ParseTuple(args, "OOO", &code, &globals, &locals)) { | 
|  | 72 | +        return NULL; | 
|  | 73 | +    } | 
|  | 74 | +    if (!PyCode_Check(code)) { | 
|  | 75 | +        PyErr_SetString(PyExc_TypeError, "argument must be a code object"); | 
|  | 76 | +        return NULL; | 
|  | 77 | +    } | 
|  | 78 | +    PyThreadState *tstate = PyThreadState_Get(); | 
|  | 79 | + | 
|  | 80 | +    return (PyObject *)PyFrame_New(tstate, (PyCodeObject *)code, globals, locals); | 
|  | 81 | +} | 
|  | 82 | + | 
|  | 83 | + | 
|  | 84 | +static PyObject * | 
|  | 85 | +frame_getvar(PyObject *self, PyObject *args) | 
|  | 86 | +{ | 
|  | 87 | +    PyObject *frame, *name; | 
|  | 88 | +    if (!PyArg_ParseTuple(args, "OO", &frame, &name)) { | 
|  | 89 | +        return NULL; | 
|  | 90 | +    } | 
|  | 91 | +    if (!PyFrame_Check(frame)) { | 
|  | 92 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 93 | +        return NULL; | 
|  | 94 | +    } | 
|  | 95 | + | 
|  | 96 | +    return PyFrame_GetVar((PyFrameObject *)frame, name); | 
|  | 97 | +} | 
|  | 98 | + | 
|  | 99 | + | 
|  | 100 | +static PyObject * | 
|  | 101 | +frame_getvarstring(PyObject *self, PyObject *args) | 
|  | 102 | +{ | 
|  | 103 | +    PyObject *frame; | 
|  | 104 | +    const char *name; | 
|  | 105 | +    if (!PyArg_ParseTuple(args, "Oy", &frame, &name)) { | 
|  | 106 | +        return NULL; | 
|  | 107 | +    } | 
|  | 108 | +    if (!PyFrame_Check(frame)) { | 
|  | 109 | +        PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | 
|  | 110 | +        return NULL; | 
|  | 111 | +    } | 
|  | 112 | + | 
|  | 113 | +    return PyFrame_GetVarString((PyFrameObject *)frame, name); | 
|  | 114 | +} | 
|  | 115 | + | 
|  | 116 | + | 
|  | 117 | +static PyMethodDef test_methods[] = { | 
|  | 118 | +    {"frame_getlocals", frame_getlocals, METH_O, NULL}, | 
|  | 119 | +    {"frame_getglobals", frame_getglobals, METH_O, NULL}, | 
|  | 120 | +    {"frame_getgenerator", frame_getgenerator, METH_O, NULL}, | 
|  | 121 | +    {"frame_getbuiltins", frame_getbuiltins, METH_O, NULL}, | 
|  | 122 | +    {"frame_getlasti", frame_getlasti, METH_O, NULL}, | 
|  | 123 | +    {"frame_new", frame_new, METH_VARARGS, NULL}, | 
|  | 124 | +    {"frame_getvar", frame_getvar, METH_VARARGS, NULL}, | 
|  | 125 | +    {"frame_getvarstring", frame_getvarstring, METH_VARARGS, NULL}, | 
|  | 126 | +    {NULL}, | 
|  | 127 | +}; | 
|  | 128 | + | 
|  | 129 | +int | 
|  | 130 | +_PyTestCapi_Init_Frame(PyObject *m) | 
|  | 131 | +{ | 
|  | 132 | +    return PyModule_AddFunctions(m, test_methods); | 
|  | 133 | +} | 
|  | 134 | + | 
0 commit comments