4#include "nuitka/freelists.h"
5#include "nuitka/prelude.h"
6#include <structmember.h>
11int count_active_Nuitka_Frame_Type = 0;
12int count_allocated_Nuitka_Frame_Type = 0;
13int count_released_Nuitka_Frame_Type = 0;
18int count_active_frame_cache_instances = 0;
19int count_allocated_frame_cache_instances = 0;
20int count_released_frame_cache_instances = 0;
21int count_hit_frame_cache_instances = 0;
24#if PYTHON_VERSION < 0x3b0
25static PyMemberDef Nuitka_Frame_members[] = {
26 {(
char *)
"f_back", T_OBJECT, offsetof(PyFrameObject, f_back), READONLY | RESTRICTED},
27 {(
char *)
"f_code", T_OBJECT, offsetof(PyFrameObject, f_code), READONLY | RESTRICTED},
28 {(
char *)
"f_builtins", T_OBJECT, offsetof(PyFrameObject, f_builtins), READONLY | RESTRICTED},
29 {(
char *)
"f_globals", T_OBJECT, offsetof(PyFrameObject, f_globals), READONLY | RESTRICTED},
30 {(
char *)
"f_lasti", T_INT, offsetof(PyFrameObject, f_lasti), READONLY | RESTRICTED},
34#define Nuitka_Frame_members 0
37#if PYTHON_VERSION < 0x300
39static PyObject *_Nuitka_Frame_get_exc_traceback(PyObject *self,
void *data) {
40 assert(Nuitka_Frame_CheckExact(self));
42 assert(_PyObject_GC_IS_TRACKED(self));
45 PyObject *result = frame->m_frame.f_exc_traceback;
55static int _Nuitka_Frame_set_exc_traceback(PyObject *self, PyObject *traceback,
void *data) {
56 assert(Nuitka_Frame_CheckExact(self));
58 assert(_PyObject_GC_IS_TRACKED(self));
61 Py_XDECREF(frame->m_frame.f_exc_traceback);
63 if (traceback == Py_None) {
67 frame->m_frame.f_exc_traceback = traceback;
68 Py_XINCREF(traceback);
73static PyObject *_Nuitka_Frame_get_exc_type(PyObject *self,
void *data) {
74 assert(Nuitka_Frame_CheckExact(self));
76 assert(_PyObject_GC_IS_TRACKED(self));
81 if (frame->m_frame.f_exc_type != NULL) {
82 result = frame->m_frame.f_exc_type;
91static int _Nuitka_Frame_set_exc_type(PyObject *self, PyObject *exception_type,
void *data) {
92 assert(Nuitka_Frame_CheckExact(self));
94 assert(_PyObject_GC_IS_TRACKED(self));
97 PyObject *old = frame->m_frame.f_exc_type;
99 if (exception_type == Py_None) {
100 exception_type = NULL;
103 frame->m_frame.f_exc_type = exception_type;
104 Py_XINCREF(frame->m_frame.f_exc_type);
111static PyObject *_Nuitka_Frame_get_exc_value(PyObject *self,
void *data) {
112 assert(Nuitka_Frame_CheckExact(self));
114 assert(_PyObject_GC_IS_TRACKED(self));
119 if (frame->m_frame.f_exc_value != NULL) {
120 result = frame->m_frame.f_exc_value;
129static int _Nuitka_Frame_set_exc_value(PyObject *self, PyObject *exception_value,
void *data) {
130 assert(Nuitka_Frame_CheckExact(self));
132 assert(_PyObject_GC_IS_TRACKED(self));
135 PyObject *old = frame->m_frame.f_exc_value;
137 if (exception_value == Py_None) {
138 exception_value = NULL;
141 frame->m_frame.f_exc_value = exception_value;
142 Py_XINCREF(exception_value);
148static PyObject *_Nuitka_Frame_get_restricted(PyObject *self,
void *data) {
149 assert(Nuitka_Frame_CheckExact(self));
151 assert(_PyObject_GC_IS_TRACKED(self));
153 Py_INCREF_IMMORTAL(Py_False);
159static PyObject *_Nuitka_Frame_get_locals(PyObject *self,
void *data) {
160 assert(Nuitka_Frame_CheckExact(self));
162 assert(_PyObject_GC_IS_TRACKED(self));
164 NUITKA_MAY_BE_UNUSED PyThreadState *tstate = PyThreadState_GET();
167 if (nuitka_frame->m_type_description == NULL) {
168#if PYTHON_VERSION < 0x3b0
169 PyFrameObject *locals_owner = &nuitka_frame->m_frame;
171 _PyInterpreterFrame *locals_owner = &nuitka_frame->m_interpreter_frame;
174 if (locals_owner->f_locals == NULL) {
175 locals_owner->f_locals = MAKE_DICT_EMPTY(tstate);
178 Py_INCREF(locals_owner->f_locals);
179 return locals_owner->f_locals;
181 PyObject *result = MAKE_DICT_EMPTY(tstate);
182 PyObject **var_names = Nuitka_GetCodeVarNames(Nuitka_GetFrameCodeObject(nuitka_frame));
184 char const *w = nuitka_frame->m_type_description;
185 char const *t = nuitka_frame->m_locals_storage;
189 case NUITKA_TYPE_DESCRIPTION_OBJECT:
190 case NUITKA_TYPE_DESCRIPTION_OBJECT_PTR: {
191 PyObject *value = *(PyObject **)t;
192 CHECK_OBJECT_X(value);
195 DICT_SET_ITEM(result, *var_names, value);
198 t +=
sizeof(PyObject *);
202 case NUITKA_TYPE_DESCRIPTION_CELL: {
204 assert(Nuitka_CellOrPyCell_Check((PyObject *)value));
207 PyObject *cell_value = Nuitka_CellOrPyCell_GET((PyObject *)value);
209 if (cell_value != NULL) {
210 DICT_SET_ITEM(result, *var_names, cell_value);
217 case NUITKA_TYPE_DESCRIPTION_NULL: {
220 case NUITKA_TYPE_DESCRIPTION_BOOL: {
221 int value = *(
int *)t;
223 switch ((nuitka_bool)value) {
224 case NUITKA_BOOL_TRUE: {
225 DICT_SET_ITEM(result, *var_names, Py_True);
228 case NUITKA_BOOL_FALSE: {
229 DICT_SET_ITEM(result, *var_names, Py_False);
250void Nuitka_Frame_AssignLocals(
struct Nuitka_FrameObject *frame_object, PyObject *locals_value) {
251 assert(Nuitka_Frame_CheckExact((PyObject *)frame_object));
252 CHECK_OBJECT((PyObject *)frame_object);
253 CHECK_OBJECT(locals_value);
255#if PYTHON_VERSION < 0x300
256 NUITKA_MAY_BE_UNUSED PyThreadState *tstate = PyThreadState_GET();
259#if PYTHON_VERSION < 0x3b0
260 PyFrameObject *locals_owner = &frame_object->m_frame;
262 _PyInterpreterFrame *locals_owner = &frame_object->m_interpreter_frame;
265 assert(locals_owner->f_locals == NULL);
267#if PYTHON_VERSION < 0x300
268 if (PyModule_Check(locals_value)) {
270 PyObject *kw_pairs[2] = {const_str_plain___module__, MODULE_NAME0(tstate, locals_value)};
272 locals_owner->f_locals = MAKE_DICT(kw_pairs, 1);
277 locals_owner->f_locals = locals_value;
278 Py_INCREF(locals_owner->f_locals);
283 assert(Nuitka_Frame_CheckExact((PyObject *)frame_object));
284 CHECK_OBJECT((PyObject *)frame_object);
286#if PYTHON_VERSION < 0x3b0
287 PyFrameObject *locals_owner = &frame_object->m_frame;
289 _PyInterpreterFrame *locals_owner = &frame_object->m_interpreter_frame;
292 Py_CLEAR(locals_owner->f_locals);
295static PyObject *_Nuitka_Frame_get_lineno(PyObject *self,
void *data) {
296 assert(Nuitka_Frame_CheckExact(self));
298 assert(_PyObject_GC_IS_TRACKED(self));
301 return Nuitka_PyInt_FromLong(frame->m_frame.f_lineno);
304static PyObject *_Nuitka_Frame_get_trace(PyObject *self,
void *data) {
305 assert(Nuitka_Frame_CheckExact(self));
307 assert(_PyObject_GC_IS_TRACKED(self));
310 PyObject *result = frame->m_frame.f_trace;
315static int _Nuitka_Frame_set_trace(PyObject *self, PyObject *value,
void *data) {
316 assert(Nuitka_Frame_CheckExact(self));
318 assert(_PyObject_GC_IS_TRACKED(self));
319#if !defined(_NUITKA_DEPLOYMENT_MODE) && !defined(_NUITKA_NO_DEPLOYMENT_FRAME_USELESS_SET_TRACE)
320 if (value == Py_None) {
323 PyThreadState *tstate = PyThreadState_GET();
325 SET_CURRENT_EXCEPTION_TYPE0_STR(
326 tstate, PyExc_RuntimeError,
327 "f_trace is not writable in Nuitka, ignore with '--no-deployment-flag=frame-useless-set-trace'");
335#if PYTHON_VERSION >= 0x370
336static PyObject *_Nuitka_Frame_get_trace_lines(PyObject *self,
void *data) {
337 assert(Nuitka_Frame_CheckExact(self));
339 assert(_PyObject_GC_IS_TRACKED(self));
341 PyObject *result = Py_False;
342 Py_INCREF_IMMORTAL(result);
346static int _Nuitka_Frame_set_trace_lines(PyObject *self, PyObject *value,
void *data) {
347 assert(Nuitka_Frame_CheckExact(self));
349 assert(_PyObject_GC_IS_TRACKED(self));
351 PyThreadState *tstate = PyThreadState_GET();
353 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError,
"f_trace_lines is not writable in Nuitka");
357static PyObject *_Nuitka_Frame_get_trace_opcodes(PyObject *self,
void *data) {
358 assert(Nuitka_Frame_CheckExact(self));
360 assert(_PyObject_GC_IS_TRACKED(self));
362 PyObject *result = Py_False;
363 Py_INCREF_IMMORTAL(result);
367static int _Nuitka_Frame_set_trace_opcodes(PyObject *self, PyObject *value,
void *data) {
368 assert(Nuitka_Frame_CheckExact(self));
370 assert(_PyObject_GC_IS_TRACKED(self));
372 PyThreadState *tstate = PyThreadState_GET();
374 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError,
"f_trace_opcodes is not writable in Nuitka");
379static PyGetSetDef Nuitka_Frame_tp_getset[] = {
380 {(
char *)
"f_locals", _Nuitka_Frame_get_locals, NULL, NULL},
381 {(
char *)
"f_lineno", _Nuitka_Frame_get_lineno, NULL, NULL},
382 {(
char *)
"f_trace", _Nuitka_Frame_get_trace, _Nuitka_Frame_set_trace, NULL},
383#if PYTHON_VERSION < 0x300
384 {(
char *)
"f_restricted", _Nuitka_Frame_get_restricted, NULL, NULL},
385 {(
char *)
"f_exc_traceback", _Nuitka_Frame_get_exc_traceback, _Nuitka_Frame_set_exc_traceback, NULL},
386 {(
char *)
"f_exc_type", _Nuitka_Frame_get_exc_type, _Nuitka_Frame_set_exc_type, NULL},
387 {(
char *)
"f_exc_value", _Nuitka_Frame_get_exc_value, _Nuitka_Frame_set_exc_value, NULL},
389#if PYTHON_VERSION >= 0x370
390 {(
char *)
"f_trace_lines", _Nuitka_Frame_get_trace_lines, _Nuitka_Frame_set_trace_lines, NULL},
391 {(
char *)
"f_trace_opcodes", _Nuitka_Frame_get_trace_opcodes, _Nuitka_Frame_set_trace_opcodes, NULL},
397 assert(Nuitka_Frame_CheckExact((PyObject *)nuitka_frame));
398 CHECK_OBJECT((PyObject *)nuitka_frame);
399 assert(_PyObject_GC_IS_TRACKED(nuitka_frame));
401#if PYTHON_VERSION >= 0x370
402 PyCodeObject *code_object = Nuitka_GetFrameCodeObject(nuitka_frame);
403 return Nuitka_String_FromFormat(
"<compiled_frame at %p, file %R, line %d, code %S>", nuitka_frame,
404 code_object->co_filename, Nuitka_GetFrameLineNumber(nuitka_frame),
405 code_object->co_name);
406#elif _DEBUG_FRAME || _DEBUG_REFRAME || _DEBUG_EXCEPTIONS
407 PyCodeObject *code_object = Nuitka_GetFrameCodeObject(nuitka_frame);
408 return Nuitka_String_FromFormat(
"<compiled_frame object for %s at %p>",
409 Nuitka_String_AsString(code_object->co_name), nuitka_frame);
411 return Nuitka_String_FromFormat(
"<compiled_frame object at %p>", nuitka_frame);
416 if (frame->m_type_description) {
417 char const *w = frame->m_type_description;
418 char const *t = frame->m_locals_storage;
422 case NUITKA_TYPE_DESCRIPTION_OBJECT:
423 case NUITKA_TYPE_DESCRIPTION_OBJECT_PTR:
424 case NUITKA_TYPE_DESCRIPTION_NILONG: {
425 PyObject *value = *(PyObject **)t;
426 CHECK_OBJECT_X(value);
430 t +=
sizeof(PyObject *);
434 case NUITKA_TYPE_DESCRIPTION_CELL: {
436 assert(Nuitka_CellOrPyCell_Check((PyObject *)value));
445 case NUITKA_TYPE_DESCRIPTION_NULL: {
448 case NUITKA_TYPE_DESCRIPTION_BOOL: {
460 frame->m_type_description = NULL;
464static int Nuitka_Frame_tp_clear(PyObject *frame_obj) {
470#define MAX_FRAME_FREE_LIST_COUNT 100
472static int free_list_frames_count = 0;
476 count_active_Nuitka_Frame_Type -= 1;
477 count_released_Nuitka_Frame_Type += 1;
480#ifndef __NUITKA_NO_ASSERT__
482 PyThreadState *tstate = PyThreadState_GET();
485 FETCH_ERROR_OCCURRED_STATE(tstate, &saved_exception_state1);
486 RESTORE_ERROR_OCCURRED_STATE(tstate, &saved_exception_state1);
489 Nuitka_GC_UnTrack(nuitka_frame);
491 PyFrameObject *frame = &nuitka_frame->m_frame;
492#if PYTHON_VERSION < 0x3b0
493 PyFrameObject *locals_owner = frame;
495 _PyInterpreterFrame *locals_owner = &nuitka_frame->m_interpreter_frame;
498 assert(Nuitka_GC_IS_TRACKED_X((PyObject *)frame->f_back));
499 Py_XDECREF(frame->f_back);
500 Py_DECREF(locals_owner->f_builtins);
501 Py_DECREF(locals_owner->f_globals);
502 Py_XDECREF(locals_owner->f_locals);
504#if PYTHON_VERSION >= 0x3e0
505 PyStackRef_CLEAR(locals_owner->f_executable);
506 Py_CLEAR(frame->f_extra_locals);
507 Py_CLEAR(frame->f_locals_cache);
508 Py_CLEAR(frame->f_overwritten_fast_locals);
511#if PYTHON_VERSION < 0x370
512 Py_XDECREF(frame->f_exc_type);
513 Py_XDECREF(frame->f_exc_value);
514 Py_XDECREF(frame->f_exc_traceback);
517 _Nuitka_Frame_tp_clear(nuitka_frame);
519 if (Py_REFCNT(nuitka_frame) > 0) {
520 Py_SET_REFCNT(nuitka_frame, Py_REFCNT(nuitka_frame) - 1);
521 if (Py_REFCNT(nuitka_frame) >= 1) {
528#if PYTHON_VERSION >= 0x3b0
531 Py_SET_SIZE(nuitka_frame, nuitka_frame->m_ob_size);
534 releaseToFreeList(free_list_frames, nuitka_frame, MAX_FRAME_FREE_LIST_COUNT);
536#ifndef __NUITKA_NO_ASSERT__
538 FETCH_ERROR_OCCURRED_STATE(tstate, &saved_exception_state2);
539 RESTORE_ERROR_OCCURRED_STATE(tstate, &saved_exception_state2);
541 ASSERT_SAME_EXCEPTION_STATE(&saved_exception_state1, &saved_exception_state2);
545static int Nuitka_Frame_tp_traverse(
struct Nuitka_FrameObject *frame, visitproc visit,
void *arg) {
546 assert(Nuitka_Frame_CheckExact((PyObject *)frame));
547 CHECK_OBJECT((PyObject *)frame);
548 assert(_PyObject_GC_IS_TRACKED(frame));
550 Py_VISIT(frame->m_frame.f_back);
552#if PYTHON_VERSION < 0x3b0
553 PyFrameObject *locals_owner = &frame->m_frame;
555 _PyInterpreterFrame *locals_owner = &frame->m_interpreter_frame;
558 Py_VISIT(locals_owner->f_builtins);
559 Py_VISIT(locals_owner->f_globals);
562#if PYTHON_VERSION < 0x370
563 Py_VISIT(frame->m_frame.f_exc_type);
564 Py_VISIT(frame->m_frame.f_exc_value);
565 Py_VISIT(frame->m_frame.f_exc_traceback);
569 char const *w = frame->m_type_description;
570 char const *t = frame->m_locals_storage;
572 while (w != NULL && *w != 0) {
574 case NUITKA_TYPE_DESCRIPTION_OBJECT:
575 case NUITKA_TYPE_DESCRIPTION_OBJECT_PTR: {
576 PyObject *value = *(PyObject **)t;
577 CHECK_OBJECT_X(value);
580 t +=
sizeof(PyObject *);
584 case NUITKA_TYPE_DESCRIPTION_CELL: {
586 assert(Nuitka_CellOrPyCell_Check((PyObject *)value));
595 case NUITKA_TYPE_DESCRIPTION_NULL: {
598 case NUITKA_TYPE_DESCRIPTION_BOOL: {
604 NUITKA_CANNOT_GET_HERE(
"invalid type description");
614#if PYTHON_VERSION >= 0x300
617#if PYTHON_VERSION < 0x3b0
618 return nuitka_frame->m_frame.f_gen;
620 return nuitka_frame->m_generator;
624static PyObject *Nuitka_Frame_clear(
struct Nuitka_FrameObject *frame, PyObject *unused) {
625 assert(Nuitka_Frame_CheckExact((PyObject *)frame));
626 CHECK_OBJECT((PyObject *)frame);
627 assert(_PyObject_GC_IS_TRACKED(frame));
629 PyThreadState *tstate = PyThreadState_GET();
631 if (Nuitka_Frame_IsExecuting(frame)) {
632 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError,
"cannot clear an executing frame");
637#if PYTHON_VERSION >= 0x3d0
638 if (Nuitka_Frame_IsSuspended(frame)) {
639 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError,
"cannot clear a suspended frame");
645#if PYTHON_VERSION >= 0x3b0
646 if (frame->m_frame_state == FRAME_COMPLETED) {
647 _Nuitka_Frame_tp_clear(frame);
652 if (frame->m_frame_state == FRAME_EXECUTING) {
653 SET_CURRENT_EXCEPTION_TYPE0_STR(tstate, PyExc_RuntimeError,
"cannot clear an executing frame");
658#if PYTHON_VERSION >= 0x300
660 PyObject *f_gen = Nuitka_GetFrameGenerator(frame);
666 bool close_exception;
668 if (Nuitka_Generator_Check(f_gen)) {
670 Nuitka_SetFrameGenerator(frame, NULL);
672 close_exception = !_Nuitka_Generator_close(tstate, generator);
674#if PYTHON_VERSION >= 0x350
675 else if (Nuitka_Coroutine_Check(f_gen)) {
676 struct Nuitka_CoroutineObject *coroutine = (
struct Nuitka_CoroutineObject *)f_gen;
677 Nuitka_SetFrameGenerator(frame, NULL);
679 close_exception = !_Nuitka_Coroutine_close(tstate, coroutine);
682#if PYTHON_VERSION >= 0x360
683 else if (Nuitka_Asyncgen_Check(f_gen)) {
684 struct Nuitka_AsyncgenObject *asyncgen = (
struct Nuitka_AsyncgenObject *)f_gen;
685 Nuitka_SetFrameGenerator(frame, NULL);
687 close_exception = !_Nuitka_Asyncgen_close(tstate, asyncgen);
694 Nuitka_SetFrameGenerator(frame, NULL);
696 close_exception =
false;
699 if (unlikely(close_exception)) {
700 PyErr_WriteUnraisable(f_gen);
707 _Nuitka_Frame_tp_clear(frame);
715 assert(Nuitka_Frame_CheckExact((PyObject *)frame));
716 CHECK_OBJECT((PyObject *)frame);
717 assert(_PyObject_GC_IS_TRACKED(frame));
719#if PYTHON_VERSION < 0x3b0
720 return Py_SIZE(frame);
722 return frame->m_ob_size;
726static PyObject *Nuitka_Frame_sizeof(
struct Nuitka_FrameObject *frame, PyObject *unused) {
727 assert(Nuitka_Frame_CheckExact((PyObject *)frame));
728 CHECK_OBJECT((PyObject *)frame);
729 assert(_PyObject_GC_IS_TRACKED(frame));
734static PyMethodDef Nuitka_Frame_methods[] = {
735#if PYTHON_VERSION >= 0x300
736 {
"clear", (PyCFunction)Nuitka_Frame_clear, METH_NOARGS,
"F.clear(): clear most references held by the frame"},
738 {
"__sizeof__", (PyCFunction)Nuitka_Frame_sizeof, METH_NOARGS,
"F.__sizeof__() -> size of F in memory, in bytes"},
741PyTypeObject Nuitka_Frame_Type = {
742 PyVarObject_HEAD_INIT(NULL, 0)
"compiled_frame",
745 (destructor)Nuitka_Frame_tp_dealloc,
750 (reprfunc)Nuitka_Frame_tp_repr,
760 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
762 (traverseproc)Nuitka_Frame_tp_traverse,
763 (inquiry)Nuitka_Frame_tp_clear,
768 Nuitka_Frame_methods,
769 Nuitka_Frame_members,
770 Nuitka_Frame_tp_getset,
775void _initCompiledFrameType(
void) {
776 assert(Nuitka_Frame_Type.tp_doc != PyFrame_Type.tp_doc || PyFrame_Type.tp_doc == NULL);
777 assert(Nuitka_Frame_Type.tp_traverse != PyFrame_Type.tp_traverse);
778 assert(Nuitka_Frame_Type.tp_clear != PyFrame_Type.tp_clear || PyFrame_Type.tp_clear == NULL);
779 assert(Nuitka_Frame_Type.tp_richcompare != PyFrame_Type.tp_richcompare || PyFrame_Type.tp_richcompare == NULL);
780 assert(Nuitka_Frame_Type.tp_weaklistoffset != PyFrame_Type.tp_weaklistoffset ||
781 PyFrame_Type.tp_weaklistoffset == 0);
782 assert(Nuitka_Frame_Type.tp_iter != PyFrame_Type.tp_iter || PyFrame_Type.tp_iter == NULL);
783 assert(Nuitka_Frame_Type.tp_iternext != PyFrame_Type.tp_iternext || PyFrame_Type.tp_iternext == NULL);
784 assert(Nuitka_Frame_Type.tp_methods != PyFrame_Type.tp_methods);
785 assert(Nuitka_Frame_Type.tp_members != PyFrame_Type.tp_members);
786 assert(Nuitka_Frame_Type.tp_getset != PyFrame_Type.tp_getset);
788 assert(Nuitka_Frame_Type.tp_descr_get != PyFrame_Type.tp_descr_get || PyFrame_Type.tp_descr_get == NULL);
790 assert(Nuitka_Frame_Type.tp_descr_set != PyFrame_Type.tp_descr_set || PyFrame_Type.tp_descr_set == NULL);
791 assert(Nuitka_Frame_Type.tp_dictoffset != PyFrame_Type.tp_dictoffset || PyFrame_Type.tp_dictoffset == 0);
799 assert(Nuitka_Frame_Type.tp_bases != PyFrame_Type.tp_bases);
800 assert(Nuitka_Frame_Type.tp_mro != PyFrame_Type.tp_mro);
801 assert(Nuitka_Frame_Type.tp_cache != PyFrame_Type.tp_cache || PyFrame_Type.tp_cache == NULL);
802 assert(Nuitka_Frame_Type.tp_subclasses != PyFrame_Type.tp_subclasses || PyFrame_Type.tp_cache == NULL);
803 assert(Nuitka_Frame_Type.tp_del != PyFrame_Type.tp_del || PyFrame_Type.tp_del == NULL);
804#if PYTHON_VERSION >= 0x300
805 assert(Nuitka_Frame_Type.tp_finalize != PyFrame_Type.tp_finalize || PyFrame_Type.tp_finalize == NULL);
807 Nuitka_PyType_Ready(&Nuitka_Frame_Type, &PyFrame_Type,
true,
true,
false,
false,
false);
813static struct Nuitka_FrameObject *_MAKE_COMPILED_FRAME(PyCodeObject *code, PyObject *module, PyObject *f_locals,
814 Py_ssize_t locals_size) {
815 CHECK_CODE_OBJECT(code);
816 CHECK_OBJECT(module);
819 count_active_Nuitka_Frame_Type += 1;
820 count_allocated_Nuitka_Frame_Type += 1;
824 CHECK_OBJECT(globals);
826 assert(PyDict_Check(globals));
831 allocateFromFreeList(free_list_frames,
struct Nuitka_FrameObject, Nuitka_Frame_Type, locals_size);
833 result->m_type_description = NULL;
835 PyFrameObject *frame = &result->m_frame;
837#if PYTHON_VERSION < 0x3b0
838 PyFrameObject *locals_owner = frame;
840 _PyInterpreterFrame *locals_owner = &result->m_interpreter_frame;
843#if PYTHON_VERSION < 0x3d0
844 locals_owner->f_code = code;
845#elif PYTHON_VERSION < 0x3e0
846 locals_owner->f_executable = (PyObject *)code;
849 locals_owner->f_executable = _PyStackRef_FromPyObjectNew((PyObject *)code);
852 frame->f_trace = Py_None;
854#if PYTHON_VERSION < 0x370
855 frame->f_exc_type = NULL;
856 frame->f_exc_value = NULL;
857 frame->f_exc_traceback = NULL;
859 frame->f_trace_lines = 0;
860 frame->f_trace_opcodes = 0;
863#if PYTHON_VERSION >= 0x3e0
864 frame->f_extra_locals = NULL;
865 frame->f_locals_cache = NULL;
866 frame->f_overwritten_fast_locals = NULL;
869#if PYTHON_VERSION >= 0x3b0
870 result->m_ob_size = Py_SIZE(result);
872 frame->f_back = NULL;
874 Py_INCREF(dict_builtin);
875 locals_owner->f_builtins = (PyObject *)dict_builtin;
878 locals_owner->f_globals = globals;
881 CHECK_OBJECT_X(f_locals);
882 locals_owner->f_locals = f_locals;
884#if PYTHON_VERSION < 0x300
885 frame->f_tstate = PyThreadState_GET();
888#if PYTHON_VERSION < 0x3b0
893 frame->f_lineno = code->co_firstlineno;
895#if PYTHON_VERSION >= 0x300
896 Nuitka_SetFrameGenerator(result, NULL);
898 Nuitka_Frame_MarkAsNotExecuting(result);
901#if PYTHON_VERSION >= 0x3b0
902 result->m_interpreter_frame.frame_obj = &result->m_frame;
903 result->m_interpreter_frame.owner = FRAME_OWNED_BY_GENERATOR;
904#if PYTHON_VERSION >= 0x3e0
905 result->m_interpreter_frame.f_funcobj = PyStackRef_NULL;
906 locals_owner->stackpointer = &locals_owner->localsplus[0];
907#elif PYTHON_VERSION >= 0x3c0
908 result->m_interpreter_frame.f_funcobj = NULL;
910 result->m_interpreter_frame.f_func = NULL;
912#if PYTHON_VERSION < 0x3d0
913 result->m_interpreter_frame.prev_instr = _PyCode_CODE(code);
915 result->m_interpreter_frame.instr_ptr = _PyCode_CODE(code);
917 result->m_frame.f_frame = &result->m_interpreter_frame;
919 assert(!_PyFrame_IsIncomplete(&result->m_interpreter_frame));
922 Nuitka_GC_Track(result);
930 return _MAKE_COMPILED_FRAME(code, module, f_locals, 0);
933struct Nuitka_FrameObject *MAKE_FUNCTION_FRAME(PyThreadState *tstate, PyCodeObject *code, PyObject *module,
934 Py_ssize_t locals_size) {
937 if (likely((code->co_flags & CO_OPTIMIZED) == CO_OPTIMIZED)) {
940 PyObject *kw_pairs[2] = {const_str_plain___module__, MODULE_NAME0(tstate, module)};
941 f_locals = MAKE_DICT(kw_pairs, 1);
944 return _MAKE_COMPILED_FRAME(code, module, f_locals, locals_size);
947struct Nuitka_FrameObject *MAKE_CLASS_FRAME(PyThreadState *tstate, PyCodeObject *code, PyObject *module,
948 PyObject *f_locals, Py_ssize_t locals_size) {
950 if (f_locals != NULL) {
954 return _MAKE_COMPILED_FRAME(code, module, f_locals, locals_size);
958PyCodeObject *makeCodeObject(PyObject *filename,
int line,
int flags, PyObject *function_name,
959#
if PYTHON_VERSION >= 0x3b0
960 PyObject *function_qualname,
962 PyObject *arg_names, PyObject *free_vars,
int arg_count
963#
if PYTHON_VERSION >= 0x300
967#
if PYTHON_VERSION >= 0x380
973 if (filename == Py_None) {
974 filename = const_str_empty;
980 CHECK_OBJECT(filename);
981 assert(Nuitka_StringOrUnicode_CheckExact(filename));
983 CHECK_OBJECT(function_name);
984 assert(Nuitka_String_CheckExact(function_name));
986#if PYTHON_VERSION >= 0x3b0
987 if (function_qualname) {
988 CHECK_OBJECT(function_qualname);
989 assert(PyUnicode_CheckExact(function_qualname));
993#if PYTHON_VERSION >= 0x300
994 assert(kw_only_count >= 0);
996#if PYTHON_VERSION >= 0x380
997 assert(pos_only_count >= 0);
1000 assert(arg_count >= 0);
1005 if (pos_only_count < 0) abort();
1006 if (kw_only_count < 0) abort();
1007 if (flags < 0) abort();
1008 if (arg_count < 0) abort();
1011#if PYTHON_VERSION < 0x300
1012 PyObject *filename_str = NULL;
1016 if (PyUnicode_CheckExact(filename)) {
1017 filename_str = PyUnicode_AsUTF8String(filename);
1018 filename = filename_str;
1020 filename_str = filename;
1021 Py_INCREF(filename);
1025 if (arg_names == NULL || arg_names == Py_None) {
1026 arg_names = const_tuple_empty;
1028 CHECK_OBJECT(arg_names);
1029 assert(PyTuple_Check(arg_names));
1031 if (free_vars == NULL || free_vars == Py_None) {
1032 free_vars = const_tuple_empty;
1034 CHECK_OBJECT(free_vars);
1035 assert(PyTuple_Check(free_vars));
1040#ifndef __NUITKA_NO_ASSERT__
1048#if PYTHON_VERSION < 0x300
1049 PyObject *code = const_str_empty;
1050 PyObject *lnotab = const_str_empty;
1051 PyObject *consts = const_tuple_empty;
1052 PyObject *names = const_tuple_empty;
1054#elif PYTHON_VERSION < 0x3b0
1055 PyObject *code = const_bytes_empty;
1056 PyObject *lnotab = const_bytes_empty;
1057 PyObject *consts = const_tuple_empty;
1058 PyObject *names = const_tuple_empty;
1063 static PyObject *empty_code = NULL;
1064 static PyObject *lnotab = NULL;
1065 static PyObject *consts = NULL;
1066 static PyObject *names = NULL;
1068 static PyObject *exception_table = NULL;
1069 static int stacksize = 0;
1071 if (empty_code == NULL) {
1073 PyThreadState *tstate = PyThreadState_GET();
1075 PyObject *empty_code_module_object = Py_CompileString(
1076 "def empty(): raise RuntimeError('Compiled function bytecode used')",
"<exec>", Py_file_input);
1077 PyObject *
module = PyImport_ExecCodeModule("nuitka_empty_function", empty_code_module_object);
1078 CHECK_OBJECT(module);
1080 PyObject *empty_function = PyObject_GetAttrString(module,
"empty");
1081 CHECK_OBJECT(empty_function);
1082 PyObject *empty_code_object = PyObject_GetAttrString(empty_function,
"__code__");
1083 CHECK_OBJECT(empty_code_object);
1085 NUITKA_MAY_BE_UNUSED
bool bool_res = Nuitka_DelModuleString(tstate,
"nuitka_empty_function");
1086 assert(bool_res !=
false);
1088 empty_code = PyObject_GetAttrString(empty_code_object,
"co_code");
1089 CHECK_OBJECT(empty_code);
1090#if PYTHON_VERSION >= 0x3c0
1091 lnotab = ((PyCodeObject *)empty_code_object)->co_linetable;
1093 lnotab = PyObject_GetAttrString(empty_code_object,
"co_lnotab");
1094 CHECK_OBJECT(lnotab);
1096 consts = PyObject_GetAttrString(empty_code_object,
"co_consts");
1097 CHECK_OBJECT(consts);
1098 names = PyObject_GetAttrString(empty_code_object,
"co_names");
1099 CHECK_OBJECT(names);
1100 exception_table = PyObject_GetAttrString(empty_code_object,
"co_exceptiontable");
1101 CHECK_OBJECT(exception_table);
1103 stacksize = (int)PyLong_AsLong(PyObject_GetAttrString(empty_code_object,
"co_stacksize"));
1106 PyObject *code = empty_code;
1107 CHECK_OBJECT(empty_code);
1108 assert(PyBytes_Check(code));
1109 CHECK_OBJECT(lnotab);
1110 assert(PyBytes_Check(lnotab));
1111 CHECK_OBJECT(consts);
1112 assert(PyTuple_Check(consts));
1113 CHECK_OBJECT(names);
1114 assert(PyTuple_Check(names));
1115 CHECK_OBJECT(exception_table);
1116 assert(PyBytes_Check(exception_table));
1117 assert(stacksize >= 0);
1121#if PYTHON_VERSION >= 0x3b0
1122 int nlocals = (int)PyTuple_GET_SIZE(arg_names);
1130#if PYTHON_VERSION >= 0x380
1131 PyCodeObject *result = PyCode_NewWithPosOnlyArgs(arg_count,
1133 PyCodeObject *result = PyCode_New(arg_count,
1136#
if PYTHON_VERSION >= 0x300
1137#
if PYTHON_VERSION >= 0x380
1153#
if PYTHON_VERSION >= 0x3b0
1158#
if PYTHON_VERSION >= 0x3b0
1166#if PYTHON_VERSION < 0x300
1167 Py_DECREF(filename_str);
1170 if (result == NULL) {
1172 NUITKA_CANNOT_GET_HERE(
"Failed to create code object");
1178PyCodeObject *USE_CODE_OBJECT(PyThreadState *tstate, PyObject *code_object, PyObject *module_filename_obj) {
1179 assert(PyCode_Check(code_object));
1180 CHECK_OBJECT(module_filename_obj);
1182 PyCodeObject *co = (PyCodeObject *)code_object;
1183 PyObject *old = co->co_filename;
1185 if (old == const_str_empty) {
1188 co->co_filename = Py_NewRef(module_filename_obj);
1190#if PYTHON_VERSION >= 0x3b0
1193 if (co->co_qualname != co->co_name) {
1194 PyObject *w = UNICODE_CONCAT(tstate, co->co_qualname, const_str_dot);
1195 co->co_qualname = UNICODE_CONCAT(tstate, w, co->co_name);
1204void Nuitka_Frame_AttachLocals(
struct Nuitka_FrameObject *frame_object,
char const *type_description, ...) {
1205 assert(Nuitka_Frame_CheckExact((PyObject *)frame_object));
1206 CHECK_OBJECT((PyObject *)frame_object);
1207 assert(_PyObject_GC_IS_TRACKED(frame_object));
1208 assertFrameObject(frame_object);
1211 PRINT_FORMAT(
"Attaching to frame 0x%lx %s\n", frame_object,
1212 Nuitka_String_AsString(PyObject_Repr((PyObject *)Nuitka_Frame_GetCodeObject(&frame_object->m_frame))));
1215 assert(frame_object->m_type_description == NULL);
1220 if (type_description == NULL) {
1221 type_description =
"";
1224 frame_object->m_type_description = type_description;
1226 char const *w = type_description;
1227 char *t = frame_object->m_locals_storage;
1230 va_start(ap, type_description);
1234 case NUITKA_TYPE_DESCRIPTION_OBJECT: {
1235 PyObject *value = va_arg(ap, PyObject *);
1236 memcpy(t, &value,
sizeof(PyObject *));
1238 t +=
sizeof(PyObject *);
1242 case NUITKA_TYPE_DESCRIPTION_OBJECT_PTR: {
1245 PyObject **value = va_arg(ap, PyObject **);
1246 CHECK_OBJECT_X(*value);
1248 memcpy(t, value,
sizeof(PyObject *));
1251 t +=
sizeof(PyObject *);
1255 case NUITKA_TYPE_DESCRIPTION_CELL: {
1257 assert(Nuitka_CellOrPyCell_Check((PyObject *)value));
1258 CHECK_OBJECT(value);
1259 CHECK_OBJECT_X(Nuitka_CellOrPyCell_GET((PyObject *)value));
1273 case NUITKA_TYPE_DESCRIPTION_NULL: {
1278 case NUITKA_TYPE_DESCRIPTION_BOOL: {
1279 int value = va_arg(ap,
int);
1280 memcpy(t, &value,
sizeof(
int));
1286 case NUITKA_TYPE_DESCRIPTION_NILONG: {
1288 ENFORCE_NILONG_OBJECT_VALUE(&value);
1290 CHECK_OBJECT(value.python_value);
1291 memcpy(t, &value.python_value,
sizeof(PyObject *));
1292 Py_XINCREF(value.python_value);
1293 t +=
sizeof(PyObject *);
1298 NUITKA_CANNOT_GET_HERE(
"invalid type description");
1307 assert(t - frame_object->m_locals_storage <= Nuitka_Frame_GetSize(frame_object));
1312void dumpFrameStack(
void) {
1313 PyThreadState *tstate = PyThreadState_GET();
1316 FETCH_ERROR_OCCURRED_STATE(tstate, &saved_exception_state);
1320#if PYTHON_VERSION < 0x3b0
1321 PyFrameObject *current = PyThreadState_GET()->frame;
1322 while (current != NULL) {
1324 current = current->f_back;
1327 current = tstate->frame;
1329 _PyCFrame *current = tstate->cframe;
1330 while (current != NULL) {
1332 current = current->previous;
1335 current = tstate->cframe;
1338 PRINT_STRING(
">--------->\n");
1341#if PYTHON_VERSION < 0x3b0
1342 PyObject *current_repr = PyObject_Str((PyObject *)current);
1343 PyObject *code_repr = PyObject_Str((PyObject *)current->f_code);
1345 PyObject *current_repr = NULL;
1346 if (current->current_frame->frame_obj != NULL) {
1347 current_repr = PyObject_Str((PyObject *)current->current_frame->frame_obj);
1349 current_repr = const_str_empty;
1350 Py_INCREF(const_str_empty);
1352 PyObject *code_repr = PyObject_Str((PyObject *)Nuitka_InterpreterFrame_GetCodeObject(current->current_frame));
1355 PRINT_FORMAT(
"Frame stack %d: %s %d %s\n", total--, Nuitka_String_AsString(current_repr), Py_REFCNT(current),
1356 Nuitka_String_AsString(code_repr));
1358 Py_DECREF(current_repr);
1359 Py_DECREF(code_repr);
1361#if PYTHON_VERSION < 0x3b0
1362 current = current->f_back;
1364 current = current->previous;
1368 PRINT_STRING(
">---------<\n");
1370 RESTORE_ERROR_OCCURRED_STATE(tstate, &saved_exception_state);
1373static void PRINT_UNCOMPILED_FRAME(
char const *prefix, PyFrameObject *frame) {
1374 PRINT_STRING(prefix);
1378 PyObject *frame_str = PyObject_Str((PyObject *)frame);
1379 PRINT_ITEM(frame_str);
1380 Py_DECREF(frame_str);
1382 PyObject *code_object_str = PyObject_Repr((PyObject *)Nuitka_Frame_GetCodeObject(frame));
1383 PRINT_ITEM(code_object_str);
1384 Py_DECREF(code_object_str);
1386 PRINT_REFCOUNT((PyObject *)frame);
1388 PRINT_STRING(
"<NULL> no frame");
1395 return PRINT_UNCOMPILED_FRAME(prefix, &frame->m_frame);
1398void PRINT_INTERPRETER_FRAME(
char const *prefix, Nuitka_ThreadStateFrameType *frame) {
1399#if PYTHON_VERSION < 0x3b0
1400 PRINT_UNCOMPILED_FRAME(prefix, frame);
1402 PRINT_STRING(prefix);
1406 PRINT_FORMAT(
"0x%lx ", frame);
1408 PyObject *code_object_str = PyObject_Repr((PyObject *)Nuitka_InterpreterFrame_GetCodeObject(frame));
1409 PRINT_ITEM(code_object_str);
1410 Py_DECREF(code_object_str);
1412 PRINT_STRING(
"<NULL> no frame");
1419void PRINT_TOP_FRAME(
char const *prefix) {
1420 PyThreadState *tstate = PyThreadState_GET();
1422#if PYTHON_VERSION < 0x3b0
1423 PRINT_UNCOMPILED_FRAME(prefix, tstate->frame);
1425 PRINT_INTERPRETER_FRAME(prefix, CURRENT_TSTATE_INTERPRETER_FRAME(tstate));
Definition compiled_cell.h:17
Definition exceptions.h:712
Definition compiled_frame.h:119
Definition compiled_generator.h:41