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