gh-111495: Add more tests on PyEval C APIs (#122789)

* Add Lib/test/test_capi/test_eval.py
* Add Modules/_testlimitedcapi/eval.c
This commit is contained in:
Victor Stinner 2024-08-08 14:16:20 +02:00 committed by GitHub
parent 81c739e2dc
commit bf8b374639
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 205 additions and 65 deletions

View file

@ -163,7 +163,7 @@
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/test_lock.c _testinternalcapi/pytime.c _testinternalcapi/set.c _testinternalcapi/test_critical_sections.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/gc.c _testcapi/hash.c _testcapi/time.c _testcapi/bytes.c _testcapi/object.c _testcapi/monitoring.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c
@MODULE__TESTLIMITEDCAPI_TRUE@_testlimitedcapi _testlimitedcapi.c _testlimitedcapi/abstract.c _testlimitedcapi/bytearray.c _testlimitedcapi/bytes.c _testlimitedcapi/complex.c _testlimitedcapi/dict.c _testlimitedcapi/eval.c _testlimitedcapi/float.c _testlimitedcapi/heaptype_relative.c _testlimitedcapi/list.c _testlimitedcapi/long.c _testlimitedcapi/object.c _testlimitedcapi/pyos.c _testlimitedcapi/set.c _testlimitedcapi/sys.c _testlimitedcapi/unicode.c _testlimitedcapi/vectorcall_limited.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c

View file

@ -2655,18 +2655,6 @@ test_frame_getvarstring(PyObject *self, PyObject *args)
}
static PyObject *
eval_get_func_name(PyObject *self, PyObject *func)
{
return PyUnicode_FromString(PyEval_GetFuncName(func));
}
static PyObject *
eval_get_func_desc(PyObject *self, PyObject *func)
{
return PyUnicode_FromString(PyEval_GetFuncDesc(func));
}
static PyObject *
gen_get_code(PyObject *self, PyObject *gen)
{
@ -3341,12 +3329,6 @@ test_critical_sections(PyObject *module, PyObject *Py_UNUSED(args))
Py_RETURN_NONE;
}
static PyObject *
pyeval_getlocals(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetLocals());
}
static PyMethodDef TestMethods[] = {
{"set_errno", set_errno, METH_VARARGS},
{"test_config", test_config, METH_NOARGS},
@ -3467,8 +3449,6 @@ static PyMethodDef TestMethods[] = {
{"frame_new", frame_new, METH_VARARGS, NULL},
{"frame_getvar", test_frame_getvar, METH_VARARGS, NULL},
{"frame_getvarstring", test_frame_getvarstring, METH_VARARGS, NULL},
{"eval_get_func_name", eval_get_func_name, METH_O, NULL},
{"eval_get_func_desc", eval_get_func_desc, METH_O, NULL},
{"gen_get_code", gen_get_code, METH_O, NULL},
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
{"test_code_api", test_code_api, METH_NOARGS, NULL},
@ -3489,7 +3469,6 @@ static PyMethodDef TestMethods[] = {
{"test_weakref_capi", test_weakref_capi, METH_NOARGS},
{"function_set_warning", function_set_warning, METH_NOARGS},
{"test_critical_sections", test_critical_sections, METH_NOARGS},
{"pyeval_getlocals", pyeval_getlocals, METH_NOARGS},
{NULL, NULL} /* sentinel */
};

View file

@ -44,6 +44,9 @@ PyInit__testlimitedcapi(void)
if (_PyTestLimitedCAPI_Init_Dict(mod) < 0) {
return NULL;
}
if (_PyTestLimitedCAPI_Init_Eval(mod) < 0) {
return NULL;
}
if (_PyTestLimitedCAPI_Init_Float(mod) < 0) {
return NULL;
}

View file

@ -0,0 +1,95 @@
#include "parts.h"
#include "util.h"
static PyObject *
eval_get_func_name(PyObject *self, PyObject *func)
{
return PyUnicode_FromString(PyEval_GetFuncName(func));
}
static PyObject *
eval_get_func_desc(PyObject *self, PyObject *func)
{
return PyUnicode_FromString(PyEval_GetFuncDesc(func));
}
static PyObject *
eval_getlocals(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetLocals());
}
static PyObject *
eval_getglobals(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetGlobals());
}
static PyObject *
eval_getbuiltins(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetBuiltins());
}
static PyObject *
eval_getframe(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetFrame());
}
static PyObject *
eval_getframe_builtins(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetFrameBuiltins());
}
static PyObject *
eval_getframe_globals(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetFrameGlobals());
}
static PyObject *
eval_getframe_locals(PyObject *module, PyObject *Py_UNUSED(args))
{
return Py_XNewRef(PyEval_GetFrameLocals());
}
static PyObject *
eval_get_recursion_limit(PyObject *module, PyObject *Py_UNUSED(args))
{
int limit = Py_GetRecursionLimit();
return PyLong_FromLong(limit);
}
static PyObject *
eval_set_recursion_limit(PyObject *module, PyObject *args)
{
int limit;
if (!PyArg_ParseTuple(args, "i", &limit)) {
return NULL;
}
Py_SetRecursionLimit(limit);
Py_RETURN_NONE;
}
static PyMethodDef test_methods[] = {
{"eval_get_func_name", eval_get_func_name, METH_O, NULL},
{"eval_get_func_desc", eval_get_func_desc, METH_O, NULL},
{"eval_getlocals", eval_getlocals, METH_NOARGS},
{"eval_getglobals", eval_getglobals, METH_NOARGS},
{"eval_getbuiltins", eval_getbuiltins, METH_NOARGS},
{"eval_getframe", eval_getframe, METH_NOARGS},
{"eval_getframe_builtins", eval_getframe_builtins, METH_NOARGS},
{"eval_getframe_globals", eval_getframe_globals, METH_NOARGS},
{"eval_getframe_locals", eval_getframe_locals, METH_NOARGS},
{"eval_get_recursion_limit", eval_get_recursion_limit, METH_NOARGS},
{"eval_set_recursion_limit", eval_set_recursion_limit, METH_VARARGS},
{NULL},
};
int
_PyTestLimitedCAPI_Init_Eval(PyObject *m)
{
return PyModule_AddFunctions(m, test_methods);
}

View file

@ -27,6 +27,7 @@ int _PyTestLimitedCAPI_Init_ByteArray(PyObject *module);
int _PyTestLimitedCAPI_Init_Bytes(PyObject *module);
int _PyTestLimitedCAPI_Init_Complex(PyObject *module);
int _PyTestLimitedCAPI_Init_Dict(PyObject *module);
int _PyTestLimitedCAPI_Init_Eval(PyObject *module);
int _PyTestLimitedCAPI_Init_Float(PyObject *module);
int _PyTestLimitedCAPI_Init_HeaptypeRelative(PyObject *module);
int _PyTestLimitedCAPI_Init_Object(PyObject *module);