Debian Codegraph @

Modules/_testcapi/code.c

raw
1 #include "parts.h"
2 #include "util.h"
3
4 static Py_ssize_t
5 get_code_extra_index(PyInterpreterState* interp) {
6 Py_ssize_t result = -1;
7
8 static const char *key = "_testcapi.frame_evaluation.code_index";
9
10 PyObject *interp_dict = PyInterpreterState_GetDict(interp); // borrowed
11 assert(interp_dict); // real users would handle missing dict... somehow
12
13 PyObject *index_obj;
14 if (PyDict_GetItemStringRef(interp_dict, key, &index_obj) < 0) {
15 goto finally;
16 }
17 Py_ssize_t index = 0;
18 if (!index_obj) {
19 index = PyUnstable_Eval_RequestCodeExtraIndex(NULL);
20 if (index < 0 || PyErr_Occurred()) {
21 goto finally;
22 }
23 index_obj = PyLong_FromSsize_t(index); // strong ref
24 if (!index_obj) {
25 goto finally;
26 }
27 int res = PyDict_SetItemString(interp_dict, key, index_obj);
28 Py_DECREF(index_obj);
29 if (res < 0) {
30 goto finally;
31 }
32 }
33 else {
34 index = PyLong_AsSsize_t(index_obj);
35 Py_DECREF(index_obj);
36 if (index == -1 && PyErr_Occurred()) {
37 goto finally;
38 }
39 }
40
41 result = index;
42 finally:
43 return result;
44 }
45
46 static PyObject *
47 test_code_extra(PyObject* self, PyObject *Py_UNUSED(callable))
48 {
49 PyObject *result = NULL;
50 PyObject *test_func = NULL;
51
52 // Get or initialize interpreter-specific code object storage index
53 PyInterpreterState *interp = PyInterpreterState_Get();
54 if (!interp) {
55 return NULL;
56 }
57 Py_ssize_t code_extra_index = get_code_extra_index(interp);
58 if (PyErr_Occurred()) {
59 goto finally;
60 }
61
62 // Get a function to test with
63 // This can be any Python function. Use `test.test_misc.testfunction`.
64 test_func = PyImport_ImportModuleAttrString("test.test_capi.test_misc",
65 "testfunction");
66 if (!test_func) {
67 goto finally;
68 }
69 PyObject *test_func_code = PyFunction_GetCode(test_func); // borrowed
70 if (!test_func_code) {
71 goto finally;
72 }
73
74 // Check the value is initially NULL
75 void *extra = UNINITIALIZED_PTR;
76 int res = PyUnstable_Code_GetExtra(test_func_code, code_extra_index, &extra);
77 if (res < 0) {
78 goto finally;
79 }
80 assert (extra == NULL);
81
82 // Set another code extra value
83 res = PyUnstable_Code_SetExtra(test_func_code, code_extra_index, (void*)(uintptr_t)77);
84 if (res < 0) {
85 goto finally;
86 }
87 // Assert it was set correctly
88 extra = UNINITIALIZED_PTR;
89 res = PyUnstable_Code_GetExtra(test_func_code, code_extra_index, &extra);
90 if (res < 0) {
91 goto finally;
92 }
93 assert ((uintptr_t)extra == 77);
94 // Revert to initial code extra value.
95 res = PyUnstable_Code_SetExtra(test_func_code, code_extra_index, NULL);
96 if (res < 0) {
97 goto finally;
98 }
99 result = Py_NewRef(Py_None);
100 finally:
101 Py_XDECREF(test_func);
102 return result;
103 }
104
105 static PyMethodDef TestMethods[] = {
106 {"test_code_extra", test_code_extra, METH_NOARGS},
107 {NULL},
108 };
109
110 int
111 _PyTestCapi_Init_Code(PyObject *m) {
112 if (PyModule_AddFunctions(m, TestMethods) < 0) {
113 return -1;
114 }
115
116 return 0;
117 }