Skip to content

Commit f2ddceb

Browse files
committed
gh-94808: Cover PyFunction_GetCode, PyFunction_GetGlobals, PyFunction_GetModule
1 parent 553d3c1 commit f2ddceb

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

Lib/test/test_capi.py

+35
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,41 @@ def __init__(self):
880880
_testcapi.clear_managed_dict(c)
881881
self.assertEqual(c.__dict__, {})
882882

883+
def test_function_get_code(self):
884+
import types
885+
886+
def some():
887+
pass
888+
889+
code = _testcapi.function_get_code(some)
890+
self.assertIsInstance(code, types.CodeType)
891+
self.assertEqual(code, some.__code__)
892+
893+
with self.assertRaises(SystemError):
894+
_testcapi.function_get_code(None) # not a function
895+
896+
def test_function_get_globals(self):
897+
def some():
898+
pass
899+
900+
globals_ = _testcapi.function_get_globals(some)
901+
self.assertIsInstance(globals_, dict)
902+
self.assertEqual(globals_, some.__globals__)
903+
904+
with self.assertRaises(SystemError):
905+
_testcapi.function_get_globals(None) # not a function
906+
907+
def test_function_get_module(self):
908+
def some():
909+
pass
910+
911+
module = _testcapi.function_get_module(some)
912+
self.assertIsInstance(module, str)
913+
self.assertEqual(module, some.__module__)
914+
915+
with self.assertRaises(SystemError):
916+
_testcapi.function_get_module(None) # not a function
917+
883918

884919
class TestPendingCalls(unittest.TestCase):
885920

Modules/_testcapimodule.c

+39
Original file line numberDiff line numberDiff line change
@@ -5652,6 +5652,42 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args))
56525652
Py_RETURN_NONE;
56535653
}
56545654

5655+
static PyObject *
5656+
function_get_code(PyObject *self, PyObject *func)
5657+
{
5658+
PyObject *code = PyFunction_GetCode(func);
5659+
if (code != NULL) {
5660+
Py_INCREF(code);
5661+
return code;
5662+
} else {
5663+
return NULL;
5664+
}
5665+
}
5666+
5667+
static PyObject *
5668+
function_get_globals(PyObject *self, PyObject *func)
5669+
{
5670+
PyObject *globals = PyFunction_GetGlobals(func);
5671+
if (globals != NULL) {
5672+
Py_INCREF(globals);
5673+
return globals;
5674+
} else {
5675+
return NULL;
5676+
}
5677+
}
5678+
5679+
static PyObject *
5680+
function_get_module(PyObject *self, PyObject *func)
5681+
{
5682+
PyObject *module = PyFunction_GetModule(func);
5683+
if (module != NULL) {
5684+
Py_INCREF(module);
5685+
return module;
5686+
} else {
5687+
return NULL;
5688+
}
5689+
}
5690+
56555691

56565692
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
56575693
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
@@ -5935,6 +5971,9 @@ static PyMethodDef TestMethods[] = {
59355971
{"watch_dict", watch_dict, METH_VARARGS, NULL},
59365972
{"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
59375973
{"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL},
5974+
{"function_get_code", function_get_code, METH_O, NULL},
5975+
{"function_get_globals", function_get_globals, METH_O, NULL},
5976+
{"function_get_module", function_get_module, METH_O, NULL},
59385977
{NULL, NULL} /* sentinel */
59395978
};
59405979

0 commit comments

Comments
 (0)