10#include "nuitka/prelude.h"
13#if PYTHON_VERSION >= 0x300
14static PyObject *module_inspect;
15#if PYTHON_VERSION >= 0x350
16static PyObject *module_types;
19static char *kw_list_object[] = {(
char *)
"object", NULL};
23static PyObject *old_getgeneratorstate = NULL;
25static PyObject *_inspect_getgeneratorstate_replacement(PyObject *self, PyObject *args, PyObject *kwds) {
28 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O:getgeneratorstate", kw_list_object, &
object, NULL)) {
34 if (Nuitka_Generator_Check(
object)) {
37 if (generator->m_running) {
38 return PyObject_GetAttrString(module_inspect,
"GEN_RUNNING");
39 }
else if (generator->m_status == status_Finished) {
40 return PyObject_GetAttrString(module_inspect,
"GEN_CLOSED");
41 }
else if (generator->m_status == status_Unused) {
42 return PyObject_GetAttrString(module_inspect,
"GEN_CREATED");
44 return PyObject_GetAttrString(module_inspect,
"GEN_SUSPENDED");
47 return old_getgeneratorstate->ob_type->tp_call(old_getgeneratorstate, args, kwds);
51#if PYTHON_VERSION >= 0x350
52static PyObject *old_getcoroutinestate = NULL;
54static PyObject *_inspect_getcoroutinestate_replacement(PyObject *self, PyObject *args, PyObject *kwds) {
57 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O:getcoroutinestate", kw_list_object, &
object, NULL)) {
61 if (Nuitka_Coroutine_Check(
object)) {
62 struct Nuitka_CoroutineObject *coroutine = (
struct Nuitka_CoroutineObject *)
object;
64 if (coroutine->m_running) {
65 return PyObject_GetAttrString(module_inspect,
"CORO_RUNNING");
66 }
else if (coroutine->m_status == status_Finished) {
67 return PyObject_GetAttrString(module_inspect,
"CORO_CLOSED");
68 }
else if (coroutine->m_status == status_Unused) {
69 return PyObject_GetAttrString(module_inspect,
"CORO_CREATED");
71 return PyObject_GetAttrString(module_inspect,
"CORO_SUSPENDED");
74 return old_getcoroutinestate->ob_type->tp_call(old_getcoroutinestate, args, kwds);
78static PyObject *old_types_coroutine = NULL;
80static char *kw_list_coroutine[] = {(
char *)
"func", NULL};
82static PyObject *_types_coroutine_replacement(PyObject *self, PyObject *args, PyObject *kwds) {
85 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O:coroutine", kw_list_coroutine, &func, NULL)) {
89 if (Nuitka_Function_Check(func)) {
92 if (function->m_code_object->co_flags & CO_GENERATOR) {
93 function->m_code_object->co_flags |= 0x100;
97 return old_types_coroutine->ob_type->tp_call(old_types_coroutine, args, kwds);
104#if PYTHON_VERSION >= 0x300
105static PyMethodDef _method_def_inspect_getgeneratorstate_replacement = {
106 "getgeneratorstate", (PyCFunction)_inspect_getgeneratorstate_replacement, METH_VARARGS | METH_KEYWORDS, NULL};
108#if PYTHON_VERSION >= 0x350
109static PyMethodDef _method_def_inspect_getcoroutinestate_replacement = {
110 "getcoroutinestate", (PyCFunction)_inspect_getcoroutinestate_replacement, METH_VARARGS | METH_KEYWORDS, NULL};
112static PyMethodDef _method_def_types_coroutine_replacement = {
"coroutine", (PyCFunction)_types_coroutine_replacement,
113 METH_VARARGS | METH_KEYWORDS, NULL};
117#if PYTHON_VERSION >= 0x3c0
119static char *kw_list_depth[] = {(
char *)
"depth", NULL};
121static bool Nuitka_FrameIsCompiled(_PyInterpreterFrame *frame) {
122 return ((frame->frame_obj != NULL) && Nuitka_Frame_Check((PyObject *)frame->frame_obj));
125static bool Nuitka_FrameIsIncomplete(_PyInterpreterFrame *frame) {
126 bool r = _PyFrame_IsIncomplete(frame);
131static PyObject *orig_sys_getframemodulename = NULL;
133static PyObject *_sys_getframemodulename_replacement(PyObject *self, PyObject *args, PyObject *kwds) {
134 PyObject *depth_arg = NULL;
136 if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O:_getframemodulename", kw_list_depth, &depth_arg)) {
140 PyObject *index_value = Nuitka_Number_IndexAsLong(depth_arg ? depth_arg : const_int_0);
142 if (unlikely(index_value == NULL)) {
146 Py_ssize_t depth_ssize = PyLong_AsSsize_t(index_value);
148 Py_DECREF(index_value);
150 PyThreadState *tstate = _PyThreadState_GET();
152 _PyInterpreterFrame *frame = CURRENT_TSTATE_INTERPRETER_FRAME(tstate);
153 while ((frame != NULL) && ((Nuitka_FrameIsIncomplete(frame)) || depth_ssize-- > 0)) {
154 frame = frame->previous;
157 if ((frame != NULL) && (Nuitka_FrameIsCompiled(frame))) {
158 PyObject *frame_globals = PyObject_GetAttrString((PyObject *)frame->frame_obj,
"f_globals");
160 PyObject *result = LOOKUP_ATTRIBUTE(tstate, frame_globals, const_str_plain___name__);
161 Py_DECREF(frame_globals);
166 return CALL_FUNCTION_WITH_SINGLE_ARG(tstate, orig_sys_getframemodulename, depth_arg);
170static PyMethodDef _method_def_sys_getframemodulename_replacement = {
171 "getcoroutinestate", (PyCFunction)_sys_getframemodulename_replacement, METH_VARARGS | METH_KEYWORDS, NULL};
176void patchInspectModule(PyThreadState *tstate) {
177 static bool is_done =
false;
182 CHECK_OBJECT(dict_builtin);
184#if PYTHON_VERSION >= 0x300
185#if _NUITKA_EXE_MODE && !_NUITKA_STANDALONE_MODE
188 if (Py_NoSiteFlag == 0) {
189 PyObject *site_module =
190 IMPORT_MODULE5(tstate, const_str_plain_site, Py_None, Py_None, const_tuple_empty, const_int_0);
192 if (site_module == NULL) {
194 CLEAR_ERROR_OCCURRED(tstate);
200 module_inspect = IMPORT_MODULE5(tstate, const_str_plain_inspect, Py_None, Py_None, const_tuple_empty, const_int_0);
202 if (module_inspect == NULL) {
206 CHECK_OBJECT(module_inspect);
209 old_getgeneratorstate = PyObject_GetAttrString(module_inspect,
"getgeneratorstate");
210 CHECK_OBJECT(old_getgeneratorstate);
212 PyObject *inspect_getgeneratorstate_replacement =
213 PyCFunction_New(&_method_def_inspect_getgeneratorstate_replacement, NULL);
214 CHECK_OBJECT(inspect_getgeneratorstate_replacement);
216 PyObject_SetAttrString(module_inspect,
"getgeneratorstate", inspect_getgeneratorstate_replacement);
218#if PYTHON_VERSION >= 0x350
220 old_getcoroutinestate = PyObject_GetAttrString(module_inspect,
"getcoroutinestate");
221 CHECK_OBJECT(old_getcoroutinestate);
223 if (PyFunction_Check(old_getcoroutinestate)) {
224 PyObject *inspect_getcoroutinestate_replacement =
225 PyCFunction_New(&_method_def_inspect_getcoroutinestate_replacement, NULL);
226 CHECK_OBJECT(inspect_getcoroutinestate_replacement);
228 PyObject_SetAttrString(module_inspect,
"getcoroutinestate", inspect_getcoroutinestate_replacement);
231 module_types = IMPORT_MODULE5(tstate, const_str_plain_types, Py_None, Py_None, const_tuple_empty, const_int_0);
233 if (module_types == NULL) {
237 CHECK_OBJECT(module_types);
240 old_types_coroutine = PyObject_GetAttrString(module_types,
"coroutine");
241 CHECK_OBJECT(old_types_coroutine);
243 if (PyFunction_Check(old_types_coroutine)) {
244 PyObject *types_coroutine_replacement = PyCFunction_New(&_method_def_types_coroutine_replacement, NULL);
245 CHECK_OBJECT(types_coroutine_replacement);
247 PyObject_SetAttrString(module_types,
"coroutine", types_coroutine_replacement);
250 static char const *wrapper_enhancement_code =
"\n\
252_old_GeneratorWrapper = types._GeneratorWrapper\n\
253class GeneratorWrapperEnhanced(_old_GeneratorWrapper):\n\
254 def __init__(self, gen):\n\
255 _old_GeneratorWrapper.__init__(self, gen)\n\
257 if hasattr(gen, 'gi_code'):\n\
258 if gen.gi_code.co_flags & 0x0020:\n\
259 self._GeneratorWrapper__isgen = True\n\
261types._GeneratorWrapper = GeneratorWrapperEnhanced\n"
262#if PYTHON_VERSION >= 0x3b0
265_old_get_code_position = inspect._get_code_position\n\
266def _get_code_position(code, instruction_index):\n\
268 return _old_get_code_position(code, instruction_index)\n\
269 except StopIteration:\n\
270 return None, None, None, None\n\
271inspect._get_code_position=_get_code_position\n\
276 PyObject *wrapper_enhancement_code_object = Py_CompileString(wrapper_enhancement_code,
"<exec>", Py_file_input);
277 CHECK_OBJECT(wrapper_enhancement_code_object);
280 NUITKA_MAY_BE_UNUSED PyObject *
module =
281 PyImport_ExecCodeModule("nuitka_types_patch", wrapper_enhancement_code_object);
282 CHECK_OBJECT(module);
284 NUITKA_MAY_BE_UNUSED
bool bool_res = Nuitka_DelModuleString(tstate,
"nuitka_types_patch");
285 assert(bool_res !=
false);
292#if PYTHON_VERSION >= 0x3c0
293 orig_sys_getframemodulename = Nuitka_SysGetObject(
"_getframemodulename");
295 PyObject *sys_getframemodulename_replacement =
296 PyCFunction_New(&_method_def_sys_getframemodulename_replacement, NULL);
297 CHECK_OBJECT(sys_getframemodulename_replacement);
299 Nuitka_SysSetObject(
"_getframemodulename", sys_getframemodulename_replacement);
306static richcmpfunc original_PyType_tp_richcompare = NULL;
308static PyObject *Nuitka_type_tp_richcompare(PyObject *a, PyObject *b,
int op) {
309 if (likely(op == Py_EQ || op == Py_NE)) {
310 if (a == (PyObject *)&Nuitka_Function_Type) {
311 a = (PyObject *)&PyFunction_Type;
312 }
else if (a == (PyObject *)&Nuitka_Method_Type) {
313 a = (PyObject *)&PyMethod_Type;
314 }
else if (a == (PyObject *)&Nuitka_Generator_Type) {
315 a = (PyObject *)&PyGen_Type;
316#if PYTHON_VERSION >= 0x350
317 }
else if (a == (PyObject *)&Nuitka_Coroutine_Type) {
318 a = (PyObject *)&PyCoro_Type;
320#if PYTHON_VERSION >= 0x360
321 }
else if (a == (PyObject *)&Nuitka_Asyncgen_Type) {
322 a = (PyObject *)&PyAsyncGen_Type;
326 if (b == (PyObject *)&Nuitka_Function_Type) {
327 b = (PyObject *)&PyFunction_Type;
328 }
else if (b == (PyObject *)&Nuitka_Method_Type) {
329 b = (PyObject *)&PyMethod_Type;
330 }
else if (b == (PyObject *)&Nuitka_Generator_Type) {
331 b = (PyObject *)&PyGen_Type;
332#if PYTHON_VERSION >= 0x350
333 }
else if (b == (PyObject *)&Nuitka_Coroutine_Type) {
334 b = (PyObject *)&PyCoro_Type;
336#if PYTHON_VERSION >= 0x360
337 }
else if (b == (PyObject *)&Nuitka_Asyncgen_Type) {
338 b = (PyObject *)&PyAsyncGen_Type;
346 assert(original_PyType_tp_richcompare);
348 return original_PyType_tp_richcompare(a, b, op);
351void patchTypeComparison(
void) {
352 if (original_PyType_tp_richcompare == NULL) {
353 original_PyType_tp_richcompare = PyType_Type.tp_richcompare;
354 PyType_Type.tp_richcompare = Nuitka_type_tp_richcompare;
358#include "nuitka/freelists.h"
361#define MAX_TRACEBACK_FREE_LIST_COUNT 1000
362static PyTracebackObject *free_list_tracebacks = NULL;
363static int free_list_tracebacks_count = 0;
369 PRINT_STRING(
"MAKE_TRACEBACK: Enter");
370 PRINT_ITEM((PyObject *)frame);
378 lineno = frame->m_frame.f_lineno;
382 PyTracebackObject *result;
384 allocateFromFreeListFixed(free_list_tracebacks, PyTracebackObject, PyTraceBack_Type);
386 result->tb_next = NULL;
387 result->tb_frame = (PyFrameObject *)frame;
390 result->tb_lasti = -1;
391 result->tb_lineno = lineno;
393 Nuitka_GC_Track(result);
398static void Nuitka_tb_dealloc(PyTracebackObject *tb) {
400 Nuitka_GC_UnTrack(tb);
403#if PYTHON_VERSION >= 0x380
404 Py_TRASHCAN_BEGIN(tb, Nuitka_tb_dealloc);
406 Py_TRASHCAN_SAFE_BEGIN(tb);
410 Py_XDECREF(tb->tb_next);
411 Py_XDECREF(tb->tb_frame);
413 releaseToFreeList(free_list_tracebacks, tb, MAX_TRACEBACK_FREE_LIST_COUNT);
416#if PYTHON_VERSION >= 0x380
419 Py_TRASHCAN_SAFE_END(tb);
424void patchTracebackDealloc(
void) { PyTraceBack_Type.tp_dealloc = (destructor)Nuitka_tb_dealloc; }
Definition compiled_frame.h:117
Definition compiled_function.h:22
Definition compiled_generator.h:41