Debian Codegraph @

pypy/module/cpyext/test/test_frameobject.py

raw symbols SCIP
1 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
2
3 class AppTestFrameObject(AppTestCpythonExtensionBase):
4
5 def test_forge_frame(self):
6 module = self.import_extension('foo', [
7 ("raise_exception", "METH_NOARGS",
8 """
9 PyObject *py_srcfile = PyUnicode_FromString("filename");
10 PyObject *py_funcname = PyUnicode_FromString("funcname");
11 PyObject *py_globals = PyDict_New();
12 PyObject *py_locals = PyDict_New();
13 PyObject *empty_bytes = PyBytes_FromString("");
14 PyObject *empty_tuple = PyTuple_New(0);
15 PyCodeObject *py_code;
16 PyFrameObject *py_frame = NULL;
17
18 py_code = (PyCodeObject *)PyCode_New(
19 0, /*int argcount,*/
20 #if PY_MAJOR_VERSION >= 3
21 0, /*int kwonlyargcount,*/
22 #endif
23 0, /*int nlocals,*/
24 0, /*int stacksize,*/
25 0, /*int flags,*/
26 empty_bytes, /*PyObject *code,*/
27 empty_tuple, /*PyObject *consts,*/
28 empty_tuple, /*PyObject *names,*/
29 empty_tuple, /*PyObject *varnames,*/
30 empty_tuple, /*PyObject *freevars,*/
31 empty_tuple, /*PyObject *cellvars,*/
32 py_srcfile, /*PyObject *filename,*/
33 py_funcname, /*PyObject *qualname,*/
34 py_funcname, /*PyObject *name,*/
35 42, /*int firstlineno,*/
36 empty_bytes /*PyObject *lnotab*/
37 );
38
39 if (!py_code) goto bad;
40 py_frame = PyFrame_New(
41 PyThreadState_Get(), /*PyThreadState *tstate,*/
42 py_code, /*PyCodeObject *code,*/
43 py_globals, /*PyObject *globals,*/
44 py_locals /*PyObject *locals*/
45 );
46 if (!py_frame) goto bad;
47 py_frame->f_lineno = 48; /* Does not work with CPython */
48 PyErr_SetString(PyExc_ValueError, "error message");
49 PyTraceBack_Here(py_frame);
50 bad:
51 Py_XDECREF(py_srcfile);
52 Py_XDECREF(py_funcname);
53 Py_XDECREF(empty_bytes);
54 Py_XDECREF(empty_tuple);
55 Py_XDECREF(py_globals);
56 Py_XDECREF(py_locals);
57 Py_XDECREF(py_code);
58 Py_XDECREF(py_frame);
59 return NULL;
60 """),
61 ], prologue='#include "frameobject.h"')
62 exc = raises(ValueError, module.raise_exception)
63 exc.value.args[0] == 'error message'
64 frame = exc.traceback.tb_frame
65 if not self.runappdirect:
66 frame = exc.traceback.tb_frame
67 assert frame.f_code.co_filename == "filename"
68 assert frame.f_code.co_name == "funcname"
69
70 # Cython does not work on CPython as well...
71 assert exc.traceback.tb_lineno in (-1, 42, 48) # should be 48
72 assert frame.f_lineno in (-1, 42, 48)
73
74 def test_forge_frame_newer(self):
75 module = self.import_extension('foo', [
76 ("raise_exception", "METH_NOARGS",
77 """
78 PyCodeObject *py_code = NULL;
79 PyFrameObject *py_frame = NULL;
80 PyObject *py_globals = NULL;
81
82 py_code = PyCode_NewEmpty(
83 "filename", "funcname", 42);
84 py_globals = PyDict_New();
85
86 py_frame = PyFrame_New(
87 PyThreadState_Get(), /*PyThreadState *tstate,*/
88 py_code, /*PyCodeObject *code,*/
89 py_globals, /*PyObject *globals,*/
90 NULL /*PyObject *locals*/
91 );
92 PyErr_SetString(PyExc_ValueError, "error message");
93 PyTraceBack_Here(py_frame);
94
95 Py_XDECREF(py_globals);
96 Py_XDECREF(py_code);
97 Py_XDECREF(py_frame);
98 return NULL;
99 """),
100 ], prologue='#include "frameobject.h"')
101 exc = raises(ValueError, module.raise_exception)
102 exc.value.args[0] == 'error message'
103 frame = exc.traceback.tb_frame
104 if not self.runappdirect:
105 assert frame.f_code.co_filename == "filename"
106 assert frame.f_code.co_name == "funcname"
107
108 assert exc.traceback.tb_lineno == 42
109 assert frame.f_lineno == 42
110
111
112 def test_traceback_check(self):
113 module = self.import_extension('foo', [
114 ("traceback_check", "METH_NOARGS",
115 """
116 int check;
117 PyObject *type, *value, *tb;
118 PyObject *ret = PyRun_String("XXX", Py_eval_input,
119 Py_None, Py_None);
120 if (ret) {
121 Py_DECREF(ret);
122 PyErr_SetString(PyExc_AssertionError, "should raise");
123 return NULL;
124 }
125 PyErr_Fetch(&type, &value, &tb);
126 if (tb == NULL) {
127 Py_XDECREF(type);
128 Py_XDECREF(value);
129 PyErr_SetString(PyExc_AssertionError, "No traceback");
130 return NULL;
131 }
132 check = PyTraceBack_Check(tb);
133 Py_XDECREF(type);
134 Py_XDECREF(value);
135 Py_XDECREF(tb);
136 if (check) {
137 Py_RETURN_TRUE;
138 }
139 else {
140 Py_RETURN_FALSE;
141 }
142 """),
143 ])
144 # On CPython the traceback is NULL
145 assert module.traceback_check()
146
147 def test_get_xxx(self):
148 module = self.import_extension('foo', [
149 ("frame_getlocals", "METH_O",
150 """
151 if (!PyFrame_Check(args)) {
152 PyErr_SetString(PyExc_TypeError, "argument must be a frame");
153 return NULL;
154 }
155 return PyFrame_GetLocals((PyFrameObject *)args);
156
157 """),
158 ("frame_getglobals", "METH_O",
159 """
160 if (!PyFrame_Check(args)) {
161 PyErr_SetString(PyExc_TypeError, "argument must be a frame");
162 return NULL;
163 }
164 return PyFrame_GetGlobals((PyFrameObject *)args);
165 """),
166 ("frame_getgenerator", "METH_O",
167 """
168 if (!PyFrame_Check(args)) {
169 PyErr_SetString(PyExc_TypeError, "argument must be a frame");
170 return NULL;
171 }
172 return PyFrame_GetGenerator((PyFrameObject *)args);
173 """),
174 ("frame_getbuiltins", "METH_O",
175 """
176 if (!PyFrame_Check(args)) {
177 PyErr_SetString(PyExc_TypeError, "argument must be a frame");
178 return NULL;
179 }
180 return PyFrame_GetBuiltins((PyFrameObject *)args);
181 """),
182 ("frame_getlasti", "METH_O",
183 """
184 if (!PyFrame_Check(args)) {
185 PyErr_SetString(PyExc_TypeError, "argument must be a frame");
186 return NULL;
187 }
188 int lasti = PyFrame_GetLasti((PyFrameObject *)args);
189 if (lasti < 0) {
190 assert(lasti == -1);
191 Py_RETURN_NONE;
192 }
193 return PyLong_FromLong(lasti);
194 """),
195 ("frame_getlinenumber", "METH_O",
196 """
197 if (!PyFrame_Check(args)) {
198 PyErr_SetString(PyExc_TypeError, "argument must be a frame");
199 return NULL;
200 }
201 int num = PyFrame_GetLineNumber((PyFrameObject *)args);
202 if (num < 0) {
203 Py_RETURN_NONE;
204 }
205 return PyLong_FromLong(num);
206 """),
207 ])
208 import sys
209 frame = sys._getframe()
210 assert frame.f_locals == module.frame_getlocals(frame)
211 assert frame.f_globals == module.frame_getglobals(frame)
212 assert frame.f_builtins == module.frame_getbuiltins(frame)
213 lasti = module.frame_getlasti(frame)
214 assert abs(frame.f_lasti - lasti) < 40
215 assert frame.f_lineno == module.frame_getlinenumber(frame)
216
217 def getgenframe():
218 yield sys._getframe()
219
220 gen = getgenframe()
221 frame = next(gen)
222 assert gen == module.frame_getgenerator(frame)
223
224
225
226
227