gh-94808: Cover PyEval_GetFuncName (#98246)

This commit is contained in:
Nikita Sobolev 2022-10-15 18:20:39 +03:00 committed by GitHub
parent 120b4ab2b6
commit f01b56c7bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 0 deletions

View File

@ -880,6 +880,21 @@ class CAPITest(unittest.TestCase):
_testcapi.clear_managed_dict(c)
self.assertEqual(c.__dict__, {})
def test_eval_get_func_name(self):
def function_example(): ...
class A:
def method_example(self): ...
self.assertEqual(_testcapi.eval_get_func_name(function_example),
"function_example")
self.assertEqual(_testcapi.eval_get_func_name(A.method_example),
"method_example")
self.assertEqual(_testcapi.eval_get_func_name(A().method_example),
"method_example")
self.assertEqual(_testcapi.eval_get_func_name(sum), "sum") # c function
self.assertEqual(_testcapi.eval_get_func_name(A), "type")
class TestPendingCalls(unittest.TestCase):

View File

@ -5469,6 +5469,12 @@ frame_getlasti(PyObject *self, PyObject *frame)
return PyLong_FromLong(lasti);
}
static PyObject *
eval_get_func_name(PyObject *self, PyObject *func)
{
return PyUnicode_FromString(PyEval_GetFuncName(func));
}
static PyObject *
get_feature_macros(PyObject *self, PyObject *Py_UNUSED(args))
{
@ -5925,6 +5931,7 @@ static PyMethodDef TestMethods[] = {
{"frame_getgenerator", frame_getgenerator, METH_O, NULL},
{"frame_getbuiltins", frame_getbuiltins, METH_O, NULL},
{"frame_getlasti", frame_getlasti, METH_O, NULL},
{"eval_get_func_name", eval_get_func_name, METH_O, NULL},
{"get_feature_macros", get_feature_macros, METH_NOARGS, NULL},
{"test_code_api", test_code_api, METH_NOARGS, NULL},
{"settrace_to_record", settrace_to_record, METH_O, NULL},