44#include "frameobject.h" // PyFrame_New()
55
66
7+ static int
8+ check_frame (PyObject * op )
9+ {
10+ if (!PyFrame_Check (op )) {
11+ PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
12+ return -1 ;
13+ }
14+ return 0 ;
15+ }
16+
17+
718static PyObject *
819frame_getlocals (PyObject * self , PyObject * frame )
920{
10- if (!PyFrame_Check (frame )) {
11- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
21+ if (check_frame (frame ) < 0 ) {
1222 return NULL ;
1323 }
1424 return PyFrame_GetLocals ((PyFrameObject * )frame );
@@ -18,8 +28,7 @@ frame_getlocals(PyObject *self, PyObject *frame)
1828static PyObject *
1929frame_getglobals (PyObject * self , PyObject * frame )
2030{
21- if (!PyFrame_Check (frame )) {
22- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
31+ if (check_frame (frame ) < 0 ) {
2332 return NULL ;
2433 }
2534 return PyFrame_GetGlobals ((PyFrameObject * )frame );
@@ -29,8 +38,7 @@ frame_getglobals(PyObject *self, PyObject *frame)
2938static PyObject *
3039frame_getgenerator (PyObject * self , PyObject * frame )
3140{
32- if (!PyFrame_Check (frame )) {
33- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
41+ if (check_frame (frame ) < 0 ) {
3442 return NULL ;
3543 }
3644 return PyFrame_GetGenerator ((PyFrameObject * )frame );
@@ -40,8 +48,7 @@ frame_getgenerator(PyObject *self, PyObject *frame)
4048static PyObject *
4149frame_getbuiltins (PyObject * self , PyObject * frame )
4250{
43- if (!PyFrame_Check (frame )) {
44- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
51+ if (check_frame (frame ) < 0 ) {
4552 return NULL ;
4653 }
4754 return PyFrame_GetBuiltins ((PyFrameObject * )frame );
@@ -51,8 +58,7 @@ frame_getbuiltins(PyObject *self, PyObject *frame)
5158static PyObject *
5259frame_getlasti (PyObject * self , PyObject * frame )
5360{
54- if (!PyFrame_Check (frame )) {
55- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
61+ if (check_frame (frame ) < 0 ) {
5662 return NULL ;
5763 }
5864 int lasti = PyFrame_GetLasti ((PyFrameObject * )frame );
@@ -88,8 +94,7 @@ frame_getvar(PyObject *self, PyObject *args)
8894 if (!PyArg_ParseTuple (args , "OO" , & frame , & name )) {
8995 return NULL ;
9096 }
91- if (!PyFrame_Check (frame )) {
92- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
97+ if (check_frame (frame ) < 0 ) {
9398 return NULL ;
9499 }
95100
@@ -105,15 +110,47 @@ frame_getvarstring(PyObject *self, PyObject *args)
105110 if (!PyArg_ParseTuple (args , "Oy" , & frame , & name )) {
106111 return NULL ;
107112 }
108- if (!PyFrame_Check (frame )) {
109- PyErr_SetString (PyExc_TypeError , "argument must be a frame" );
113+ if (check_frame (frame ) < 0 ) {
110114 return NULL ;
111115 }
112116
113117 return PyFrame_GetVarString ((PyFrameObject * )frame , name );
114118}
115119
116120
121+ static PyObject *
122+ frame_getback (PyObject * self , PyObject * frame )
123+ {
124+ if (check_frame (frame ) < 0 ) {
125+ return NULL ;
126+ }
127+ PyFrameObject * back = PyFrame_GetBack ((PyFrameObject * )frame );
128+ if (back == NULL ) {
129+ Py_RETURN_NONE ;
130+ }
131+ return (PyObject * )back ;
132+ }
133+
134+
135+ static PyObject *
136+ frame_setback (PyObject * self , PyObject * args )
137+ {
138+ PyObject * frame , * back ;
139+ if (!PyArg_ParseTuple (args , "OO" , & frame , & back )) {
140+ return NULL ;
141+ }
142+ if (check_frame (frame ) < 0 ) {
143+ return NULL ;
144+ }
145+ if (check_frame (back ) < 0 ) {
146+ return NULL ;
147+ }
148+
149+ PyFrame_SetBack ((PyFrameObject * )frame , (PyFrameObject * )back );
150+ Py_RETURN_NONE ;
151+ }
152+
153+
117154static PyMethodDef test_methods [] = {
118155 {"frame_getlocals" , frame_getlocals , METH_O , NULL },
119156 {"frame_getglobals" , frame_getglobals , METH_O , NULL },
@@ -123,6 +160,8 @@ static PyMethodDef test_methods[] = {
123160 {"frame_new" , frame_new , METH_VARARGS , NULL },
124161 {"frame_getvar" , frame_getvar , METH_VARARGS , NULL },
125162 {"frame_getvarstring" , frame_getvarstring , METH_VARARGS , NULL },
163+ {"frame_getback" , frame_getback , METH_O , NULL },
164+ {"frame_setback" , frame_setback , METH_VARARGS , NULL },
126165 {NULL },
127166};
128167
0 commit comments