forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pythongh-93649: Move PyFrame C API tests to test_capi
* Move C API tests from test_frame to test_capi.test_frame. * Add Modules/_testcapi/frame.c file. * Add Lib/test/test_capi/test_frame.py file.
- Loading branch information
Showing
8 changed files
with
199 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import sys | ||
import unittest | ||
from test.support import import_helper | ||
|
||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
|
||
class TestCAPI(unittest.TestCase): | ||
def getframe(self): | ||
return sys._getframe() | ||
|
||
def test_frame_getters(self): | ||
frame = self.getframe() | ||
self.assertEqual(frame.f_locals, _testcapi.frame_getlocals(frame)) | ||
self.assertIs(frame.f_globals, _testcapi.frame_getglobals(frame)) | ||
self.assertIs(frame.f_builtins, _testcapi.frame_getbuiltins(frame)) | ||
self.assertEqual(frame.f_lasti, _testcapi.frame_getlasti(frame)) | ||
|
||
def test_getvar(self): | ||
current_frame = sys._getframe() | ||
x = 1 | ||
self.assertEqual(_testcapi.frame_getvar(current_frame, "x"), 1) | ||
self.assertEqual(_testcapi.frame_getvarstring(current_frame, b"x"), 1) | ||
with self.assertRaises(NameError): | ||
_testcapi.frame_getvar(current_frame, "y") | ||
with self.assertRaises(NameError): | ||
_testcapi.frame_getvarstring(current_frame, b"y") | ||
|
||
# wrong name type | ||
with self.assertRaises(TypeError): | ||
_testcapi.frame_getvar(current_frame, b'x') | ||
with self.assertRaises(TypeError): | ||
_testcapi.frame_getvar(current_frame, 123) | ||
|
||
def getgenframe(self): | ||
yield sys._getframe() | ||
|
||
def test_frame_get_generator(self): | ||
gen = self.getgenframe() | ||
frame = next(gen) | ||
self.assertIs(gen, _testcapi.frame_getgenerator(frame)) | ||
|
||
def test_frame_fback_api(self): | ||
"""Test that accessing `f_back` does not cause a segmentation fault on | ||
a frame created with `PyFrame_New` (GH-99110).""" | ||
def dummy(): | ||
pass | ||
|
||
frame = _testcapi.frame_new(dummy.__code__, globals(), locals()) | ||
# The following line should not cause a segmentation fault. | ||
self.assertIsNone(frame.f_back) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "parts.h" | ||
#include "util.h" | ||
|
||
#include "frameobject.h" // PyFrame_New() | ||
|
||
|
||
static PyObject * | ||
frame_getlocals(PyObject *self, PyObject *frame) | ||
{ | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
return PyFrame_GetLocals((PyFrameObject *)frame); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_getglobals(PyObject *self, PyObject *frame) | ||
{ | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
return PyFrame_GetGlobals((PyFrameObject *)frame); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_getgenerator(PyObject *self, PyObject *frame) | ||
{ | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
return PyFrame_GetGenerator((PyFrameObject *)frame); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_getbuiltins(PyObject *self, PyObject *frame) | ||
{ | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
return PyFrame_GetBuiltins((PyFrameObject *)frame); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_getlasti(PyObject *self, PyObject *frame) | ||
{ | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
int lasti = PyFrame_GetLasti((PyFrameObject *)frame); | ||
if (lasti < 0) { | ||
assert(lasti == -1); | ||
Py_RETURN_NONE; | ||
} | ||
return PyLong_FromLong(lasti); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_new(PyObject *self, PyObject *args) | ||
{ | ||
PyObject *code, *globals, *locals; | ||
if (!PyArg_ParseTuple(args, "OOO", &code, &globals, &locals)) { | ||
return NULL; | ||
} | ||
if (!PyCode_Check(code)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a code object"); | ||
return NULL; | ||
} | ||
PyThreadState *tstate = PyThreadState_Get(); | ||
|
||
return (PyObject *)PyFrame_New(tstate, (PyCodeObject *)code, globals, locals); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_getvar(PyObject *self, PyObject *args) | ||
{ | ||
PyObject *frame, *name; | ||
if (!PyArg_ParseTuple(args, "OO", &frame, &name)) { | ||
return NULL; | ||
} | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
|
||
return PyFrame_GetVar((PyFrameObject *)frame, name); | ||
} | ||
|
||
|
||
static PyObject * | ||
frame_getvarstring(PyObject *self, PyObject *args) | ||
{ | ||
PyObject *frame; | ||
const char *name; | ||
if (!PyArg_ParseTuple(args, "Oy", &frame, &name)) { | ||
return NULL; | ||
} | ||
if (!PyFrame_Check(frame)) { | ||
PyErr_SetString(PyExc_TypeError, "argument must be a frame"); | ||
return NULL; | ||
} | ||
|
||
return PyFrame_GetVarString((PyFrameObject *)frame, name); | ||
} | ||
|
||
|
||
static PyMethodDef test_methods[] = { | ||
{"frame_getlocals", frame_getlocals, METH_O, NULL}, | ||
{"frame_getglobals", frame_getglobals, METH_O, NULL}, | ||
{"frame_getgenerator", frame_getgenerator, METH_O, NULL}, | ||
{"frame_getbuiltins", frame_getbuiltins, METH_O, NULL}, | ||
{"frame_getlasti", frame_getlasti, METH_O, NULL}, | ||
{"frame_new", frame_new, METH_VARARGS, NULL}, | ||
{"frame_getvar", frame_getvar, METH_VARARGS, NULL}, | ||
{"frame_getvarstring", frame_getvarstring, METH_VARARGS, NULL}, | ||
{NULL}, | ||
}; | ||
|
||
int | ||
_PyTestCapi_Init_Frame(PyObject *m) | ||
{ | ||
return PyModule_AddFunctions(m, test_methods); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.