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