| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-18 14:38:43 +01:00
										 |  |  | #define _PY_INTERPRETER
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "frameobject.h"
 | 
					
						
							| 
									
										
										
										
											2022-06-19 12:02:33 +02:00
										 |  |  | #include "pycore_code.h"          // stats
 | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | #include "pycore_frame.h"
 | 
					
						
							| 
									
										
										
										
											2025-03-17 14:41:05 +00:00
										 |  |  | #include "pycore_genobject.h"
 | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | #include "pycore_object.h"        // _PyObject_GC_UNTRACK()
 | 
					
						
							| 
									
										
										
										
											2022-01-20 11:46:39 +00:00
										 |  |  | #include "opcode.h"
 | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  | _PyFrame_Traverse(_PyInterpreterFrame *frame, visitproc visit, void *arg) | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     Py_VISIT(frame->frame_obj); | 
					
						
							|  |  |  |     Py_VISIT(frame->f_locals); | 
					
						
							| 
									
										
										
										
											2024-09-24 13:08:18 -07:00
										 |  |  |     _Py_VISIT_STACKREF(frame->f_funcobj); | 
					
						
							|  |  |  |     _Py_VISIT_STACKREF(frame->f_executable); | 
					
						
							| 
									
										
										
										
											2024-08-15 12:09:11 -04:00
										 |  |  |     return _PyGC_VisitFrameStack(frame, visit, arg); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyFrameObject * | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  | _PyFrame_MakeAndSetFrameObject(_PyInterpreterFrame *frame) | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     assert(frame->frame_obj == NULL); | 
					
						
							| 
									
										
										
										
											2023-02-28 11:50:52 +00:00
										 |  |  |     PyObject *exc = PyErr_GetRaisedException(); | 
					
						
							| 
									
										
										
										
											2021-11-29 12:34:59 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-14 13:46:37 +01:00
										 |  |  |     PyFrameObject *f = _PyFrame_New_NoTrack(_PyFrame_GetCode(frame)); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     if (f == NULL) { | 
					
						
							| 
									
										
										
										
											2023-02-28 11:50:52 +00:00
										 |  |  |         Py_XDECREF(exc); | 
					
						
							| 
									
										
										
										
											2022-10-06 16:20:01 -07:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2023-02-28 11:50:52 +00:00
										 |  |  |     PyErr_SetRaisedException(exc); | 
					
						
							| 
									
										
										
										
											2024-03-12 16:35:28 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // GH-97002: There was a time when a frame object could be created when we
 | 
					
						
							|  |  |  |     // are allocating the new frame object f above, so frame->frame_obj would
 | 
					
						
							|  |  |  |     // be assigned already. That path does not exist anymore. We won't call any
 | 
					
						
							|  |  |  |     // Python code in this function and garbage collection will not run.
 | 
					
						
							|  |  |  |     // Notice that _PyFrame_New_NoTrack() can potentially raise a MemoryError,
 | 
					
						
							|  |  |  |     // but it won't allocate a traceback until the frame unwinds, so we are safe
 | 
					
						
							|  |  |  |     // here.
 | 
					
						
							|  |  |  |     assert(frame->frame_obj == NULL); | 
					
						
							| 
									
										
										
										
											2022-10-06 16:20:01 -07:00
										 |  |  |     assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); | 
					
						
							|  |  |  |     f->f_frame = frame; | 
					
						
							|  |  |  |     frame->frame_obj = f; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     return f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  | take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-01-21 10:15:02 +00:00
										 |  |  |     assert(frame->owner < FRAME_OWNED_BY_INTERPRETER); | 
					
						
							| 
									
										
										
										
											2022-03-22 12:57:19 +00:00
										 |  |  |     assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); | 
					
						
							| 
									
										
										
										
											2025-03-10 14:06:56 +00:00
										 |  |  |     _PyInterpreterFrame *new_frame = (_PyInterpreterFrame *)f->_f_frame_data; | 
					
						
							|  |  |  |     _PyFrame_Copy(frame, new_frame); | 
					
						
							|  |  |  |     // _PyFrame_Copy takes the reference to the executable,
 | 
					
						
							|  |  |  |     // so we need to restore it.
 | 
					
						
							|  |  |  |     frame->f_executable = PyStackRef_DUP(new_frame->f_executable); | 
					
						
							|  |  |  |     f->f_frame = new_frame; | 
					
						
							|  |  |  |     new_frame->owner = FRAME_OWNED_BY_FRAME_OBJECT; | 
					
						
							|  |  |  |     if (_PyFrame_IsIncomplete(new_frame)) { | 
					
						
							| 
									
										
										
										
											2022-10-04 17:30:03 -07:00
										 |  |  |         // This may be a newly-created generator or coroutine frame. Since it's
 | 
					
						
							|  |  |  |         // dead anyways, just pretend that the first RESUME ran:
 | 
					
						
							| 
									
										
										
										
											2025-03-10 14:06:56 +00:00
										 |  |  |         PyCodeObject *code = _PyFrame_GetCode(new_frame); | 
					
						
							|  |  |  |         new_frame->instr_ptr = | 
					
						
							|  |  |  |             _PyFrame_GetBytecode(new_frame) + code->_co_firsttraceable + 1; | 
					
						
							| 
									
										
										
										
											2022-10-04 17:30:03 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-03-10 14:06:56 +00:00
										 |  |  |     assert(!_PyFrame_IsIncomplete(new_frame)); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     assert(f->f_back == NULL); | 
					
						
							| 
									
										
										
										
											2023-01-09 12:20:04 -08:00
										 |  |  |     _PyInterpreterFrame *prev = _PyFrame_GetFirstComplete(frame->previous); | 
					
						
							| 
									
										
										
										
											2022-07-01 11:08:20 +01:00
										 |  |  |     if (prev) { | 
					
						
							| 
									
										
										
										
											2025-01-21 10:15:02 +00:00
										 |  |  |         assert(prev->owner < FRAME_OWNED_BY_INTERPRETER); | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  |         /* Link PyFrameObjects.f_back and remove link through _PyInterpreterFrame.previous */ | 
					
						
							| 
									
										
										
										
											2022-07-01 11:08:20 +01:00
										 |  |  |         PyFrameObject *back = _PyFrame_GetFrameObject(prev); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |         if (back == NULL) { | 
					
						
							|  |  |  |             /* Memory error here. */ | 
					
						
							|  |  |  |             assert(PyErr_ExceptionMatches(PyExc_MemoryError)); | 
					
						
							|  |  |  |             /* Nothing we can do about it */ | 
					
						
							|  |  |  |             PyErr_Clear(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							|  |  |  |             f->f_back = (PyFrameObject *)Py_NewRef(back); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (!_PyObject_GC_IS_TRACKED((PyObject *)f)) { | 
					
						
							|  |  |  |         _PyObject_GC_TRACK((PyObject *)f); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-30 19:32:25 +01:00
										 |  |  | void | 
					
						
							|  |  |  | _PyFrame_ClearLocals(_PyInterpreterFrame *frame) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-07-18 12:47:21 +01:00
										 |  |  |     assert(frame->stackpointer != NULL); | 
					
						
							|  |  |  |     _PyStackRef *sp = frame->stackpointer; | 
					
						
							|  |  |  |     _PyStackRef *locals = frame->localsplus; | 
					
						
							|  |  |  |     frame->stackpointer = locals; | 
					
						
							|  |  |  |     while (sp > locals) { | 
					
						
							|  |  |  |         sp--; | 
					
						
							|  |  |  |         PyStackRef_XCLOSE(*sp); | 
					
						
							| 
									
										
										
										
											2024-04-30 19:32:25 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     Py_CLEAR(frame->f_locals); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-29 12:34:59 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2023-02-23 10:19:01 +00:00
										 |  |  | _PyFrame_ClearExceptCode(_PyInterpreterFrame *frame) | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-11-22 14:01:23 +00:00
										 |  |  |     /* It is the responsibility of the owning generator/coroutine
 | 
					
						
							| 
									
										
										
										
											2022-01-20 11:46:39 +00:00
										 |  |  |      * to have cleared the enclosing generator, if any. */ | 
					
						
							| 
									
										
										
										
											2022-03-22 12:57:19 +00:00
										 |  |  |     assert(frame->owner != FRAME_OWNED_BY_GENERATOR || | 
					
						
							| 
									
										
										
										
											2024-06-24 10:23:38 +01:00
										 |  |  |         _PyGen_GetGeneratorFromFrame(frame)->gi_frame_state == FRAME_CLEARED); | 
					
						
							| 
									
										
										
										
											2022-12-06 06:01:38 -08:00
										 |  |  |     // GH-99729: Clearing this frame can expose the stack (via finalizers). It's
 | 
					
						
							|  |  |  |     // crucial that this frame has been unlinked, and is no longer visible:
 | 
					
						
							| 
									
										
										
										
											2023-08-17 11:16:03 +01:00
										 |  |  |     assert(_PyThreadState_GET()->current_frame != frame); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     if (frame->frame_obj) { | 
					
						
							|  |  |  |         PyFrameObject *f = frame->frame_obj; | 
					
						
							|  |  |  |         frame->frame_obj = NULL; | 
					
						
							|  |  |  |         if (Py_REFCNT(f) > 1) { | 
					
						
							|  |  |  |             take_ownership(f, frame); | 
					
						
							|  |  |  |             Py_DECREF(f); | 
					
						
							| 
									
										
										
										
											2021-11-29 12:34:59 +00:00
										 |  |  |             return; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |         Py_DECREF(f); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2024-04-30 19:32:25 +01:00
										 |  |  |     _PyFrame_ClearLocals(frame); | 
					
						
							| 
									
										
										
										
											2024-09-24 13:08:18 -07:00
										 |  |  |     PyStackRef_CLEAR(frame->f_funcobj); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2022-01-28 12:42:30 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-05 17:53:07 +01:00
										 |  |  | /* Unstable API functions */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 10:10:15 +01:00
										 |  |  | PyObject * | 
					
						
							| 
									
										
										
										
											2023-05-05 17:53:07 +01:00
										 |  |  | PyUnstable_InterpreterFrame_GetCode(struct _PyInterpreterFrame *frame) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-09-12 12:37:06 -04:00
										 |  |  |     return PyStackRef_AsPyObjectNew(frame->f_executable); | 
					
						
							| 
									
										
										
										
											2023-05-05 17:53:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | PyUnstable_InterpreterFrame_GetLasti(struct _PyInterpreterFrame *frame) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-07 12:31:01 -07:00
										 |  |  | int | 
					
						
							| 
									
										
										
										
											2023-05-05 17:53:07 +01:00
										 |  |  | PyUnstable_InterpreterFrame_GetLine(_PyInterpreterFrame *frame) | 
					
						
							| 
									
										
										
										
											2022-04-07 12:31:01 -07:00
										 |  |  | { | 
					
						
							|  |  |  |     int addr = _PyInterpreterFrame_LASTI(frame) * sizeof(_Py_CODEUNIT); | 
					
						
							| 
									
										
										
										
											2023-06-14 13:46:37 +01:00
										 |  |  |     return PyCode_Addr2Line(_PyFrame_GetCode(frame), addr); | 
					
						
							| 
									
										
										
										
											2022-04-07 12:31:01 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-14 13:46:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-31 09:56:06 +02:00
										 |  |  | const PyTypeObject *const PyUnstable_ExecutableKinds[PyUnstable_EXECUTABLE_KINDS+1] = { | 
					
						
							|  |  |  |     [PyUnstable_EXECUTABLE_KIND_SKIP] = &_PyNone_Type, | 
					
						
							|  |  |  |     [PyUnstable_EXECUTABLE_KIND_PY_FUNCTION] = &PyCode_Type, | 
					
						
							|  |  |  |     [PyUnstable_EXECUTABLE_KIND_BUILTIN_FUNCTION] = &PyMethod_Type, | 
					
						
							|  |  |  |     [PyUnstable_EXECUTABLE_KIND_METHOD_DESCRIPTOR] = &PyMethodDescr_Type, | 
					
						
							|  |  |  |     [PyUnstable_EXECUTABLE_KINDS] = NULL, | 
					
						
							| 
									
										
										
										
											2023-06-14 13:46:37 +01:00
										 |  |  | }; |