Skip to content

Commit 7b48d02

Browse files
authored
pythongh-94808: Cover PyFunction_GetCode, PyFunction_GetGlobals, PyFunction_GetModule (python#98158)
1 parent 660f102 commit 7b48d02

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
@@ -895,6 +895,41 @@ def method_example(self): ...
895895
self.assertEqual(_testcapi.eval_get_func_name(sum), "sum") # c function
896896
self.assertEqual(_testcapi.eval_get_func_name(A), "type")
897897

898+
def test_function_get_code(self):
899+
import types
900+
901+
def some():
902+
pass
903+
904+
code = _testcapi.function_get_code(some)
905+
self.assertIsInstance(code, types.CodeType)
906+
self.assertEqual(code, some.__code__)
907+
908+
with self.assertRaises(SystemError):
909+
_testcapi.function_get_code(None) # not a function
910+
911+
def test_function_get_globals(self):
912+
def some():
913+
pass
914+
915+
globals_ = _testcapi.function_get_globals(some)
916+
self.assertIsInstance(globals_, dict)
917+
self.assertEqual(globals_, some.__globals__)
918+
919+
with self.assertRaises(SystemError):
920+
_testcapi.function_get_globals(None) # not a function
921+
922+
def test_function_get_module(self):
923+
def some():
924+
pass
925+
926+
module = _testcapi.function_get_module(some)
927+
self.assertIsInstance(module, str)
928+
self.assertEqual(module, some.__module__)
929+
930+
with self.assertRaises(SystemError):
931+
_testcapi.function_get_module(None) # not a function
932+
898933

899934
class TestPendingCalls(unittest.TestCase):
900935

Modules/_testcapimodule.c

+39
Original file line numberDiff line numberDiff line change
@@ -5658,6 +5658,42 @@ test_macros(PyObject *self, PyObject *Py_UNUSED(args))
56585658
Py_RETURN_NONE;
56595659
}
56605660

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

56625698
static PyObject *test_buildvalue_issue38913(PyObject *, PyObject *);
56635699
static PyObject *getargs_s_hash_int(PyObject *, PyObject *, PyObject*);
@@ -5942,6 +5978,9 @@ static PyMethodDef TestMethods[] = {
59425978
{"watch_dict", watch_dict, METH_VARARGS, NULL},
59435979
{"unwatch_dict", unwatch_dict, METH_VARARGS, NULL},
59445980
{"get_dict_watcher_events", get_dict_watcher_events, METH_NOARGS, NULL},
5981+
{"function_get_code", function_get_code, METH_O, NULL},
5982+
{"function_get_globals", function_get_globals, METH_O, NULL},
5983+
{"function_get_module", function_get_module, METH_O, NULL},
59455984
{NULL, NULL} /* sentinel */
59465985
};
59475986

0 commit comments

Comments
 (0)