| 
									
										
										
										
											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"
 | 
					
						
							|  |  |  | #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); | 
					
						
							| 
									
										
										
										
											2022-08-25 10:16:55 +01:00
										 |  |  |     Py_VISIT(frame->f_funcobj); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     Py_VISIT(frame->f_code); | 
					
						
							|  |  |  |    /* locals */ | 
					
						
							|  |  |  |     PyObject **locals = _PyFrame_GetLocalsArray(frame); | 
					
						
							| 
									
										
										
										
											2021-08-25 13:44:20 +01:00
										 |  |  |     int i = 0; | 
					
						
							|  |  |  |     /* locals and stack */ | 
					
						
							|  |  |  |     for (; i <frame->stacktop; i++) { | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |         Py_VISIT(locals[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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
										 |  |  | 
 | 
					
						
							|  |  |  |     PyFrameObject *f = _PyFrame_New_NoTrack(frame->f_code); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2022-10-06 16:20:01 -07:00
										 |  |  |     if (frame->frame_obj) { | 
					
						
							|  |  |  |         // GH-97002: How did we get into this horrible situation? Most likely,
 | 
					
						
							|  |  |  |         // allocating f triggered a GC collection, which ran some code that
 | 
					
						
							|  |  |  |         // *also* created the same frame... while we were in the middle of
 | 
					
						
							|  |  |  |         // creating it! See test_sneaky_frame_object in test_frame.py for a
 | 
					
						
							|  |  |  |         // concrete example.
 | 
					
						
							|  |  |  |         //
 | 
					
						
							|  |  |  |         // Regardless, just throw f away and use that frame instead, since it's
 | 
					
						
							|  |  |  |         // already been exposed to user code. It's actually a bit tricky to do
 | 
					
						
							|  |  |  |         // this, since we aren't backed by a real _PyInterpreterFrame anymore.
 | 
					
						
							|  |  |  |         // Just pretend that we have an owned, cleared frame so frame_dealloc
 | 
					
						
							|  |  |  |         // doesn't make the situation worse:
 | 
					
						
							|  |  |  |         f->f_frame = (_PyInterpreterFrame *)f->_f_frame_data; | 
					
						
							|  |  |  |         f->f_frame->owner = FRAME_CLEARED; | 
					
						
							|  |  |  |         f->f_frame->frame_obj = f; | 
					
						
							|  |  |  |         Py_DECREF(f); | 
					
						
							|  |  |  |         return frame->frame_obj; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-06 16:20:01 -07:00
										 |  |  |     assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); | 
					
						
							|  |  |  |     assert(frame->owner != FRAME_CLEARED); | 
					
						
							|  |  |  |     f->f_frame = frame; | 
					
						
							|  |  |  |     frame->frame_obj = f; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     return f; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 10:13:49 +00:00
										 |  |  | void | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  | _PyFrame_Copy(_PyInterpreterFrame *src, _PyInterpreterFrame *dest) | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-06 10:13:49 +00:00
										 |  |  |     assert(src->stacktop >= src->f_code->co_nlocalsplus); | 
					
						
							|  |  |  |     Py_ssize_t size = ((char*)&src->localsplus[src->stacktop]) - (char *)src; | 
					
						
							|  |  |  |     memcpy(dest, src, size); | 
					
						
							| 
									
										
										
										
											2022-10-03 16:36:52 -07:00
										 |  |  |     // Don't leave a dangling pointer to the old frame when creating generators
 | 
					
						
							|  |  |  |     // and coroutines:
 | 
					
						
							|  |  |  |     dest->previous = NULL; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  | take_ownership(PyFrameObject *f, _PyInterpreterFrame *frame) | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-11-10 04:34:57 -08:00
										 |  |  |     assert(frame->owner != FRAME_OWNED_BY_CSTACK); | 
					
						
							| 
									
										
										
										
											2022-03-22 12:57:19 +00:00
										 |  |  |     assert(frame->owner != FRAME_OWNED_BY_FRAME_OBJECT); | 
					
						
							|  |  |  |     assert(frame->owner != FRAME_CLEARED); | 
					
						
							| 
									
										
										
										
											2021-11-29 12:34:59 +00:00
										 |  |  |     Py_ssize_t size = ((char*)&frame->localsplus[frame->stacktop]) - (char *)frame; | 
					
						
							| 
									
										
										
										
											2023-02-23 10:19:01 +00:00
										 |  |  |     Py_INCREF(frame->f_code); | 
					
						
							| 
									
										
										
										
											2022-02-25 16:22:00 +01:00
										 |  |  |     memcpy((_PyInterpreterFrame *)f->_f_frame_data, frame, size); | 
					
						
							|  |  |  |     frame = (_PyInterpreterFrame *)f->_f_frame_data; | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     f->f_frame = frame; | 
					
						
							| 
									
										
										
										
											2022-03-22 12:57:19 +00:00
										 |  |  |     frame->owner = FRAME_OWNED_BY_FRAME_OBJECT; | 
					
						
							| 
									
										
										
										
											2022-10-04 17:30:03 -07:00
										 |  |  |     if (_PyFrame_IsIncomplete(frame)) { | 
					
						
							|  |  |  |         // This may be a newly-created generator or coroutine frame. Since it's
 | 
					
						
							|  |  |  |         // dead anyways, just pretend that the first RESUME ran:
 | 
					
						
							|  |  |  |         PyCodeObject *code = frame->f_code; | 
					
						
							|  |  |  |         frame->prev_instr = _PyCode_CODE(code) + code->_co_firsttraceable; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     assert(!_PyFrame_IsIncomplete(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-11-10 04:34:57 -08:00
										 |  |  |     frame->previous = NULL; | 
					
						
							| 
									
										
										
										
											2022-07-01 11:08:20 +01:00
										 |  |  |     if (prev) { | 
					
						
							| 
									
										
										
										
											2022-11-10 04:34:57 -08:00
										 |  |  |         assert(prev->owner != FRAME_OWNED_BY_CSTACK); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 || | 
					
						
							|  |  |  |         _PyFrame_GetGenerator(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:
 | 
					
						
							|  |  |  |     assert(_PyThreadState_GET()->cframe->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); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-06 10:13:49 +00:00
										 |  |  |     assert(frame->stacktop >= 0); | 
					
						
							| 
									
										
										
										
											2021-08-25 13:44:20 +01:00
										 |  |  |     for (int i = 0; i < frame->stacktop; i++) { | 
					
						
							|  |  |  |         Py_XDECREF(frame->localsplus[i]); | 
					
						
							| 
									
										
										
										
											2021-07-26 11:22:16 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-01-20 11:46:39 +00:00
										 |  |  |     Py_XDECREF(frame->frame_obj); | 
					
						
							|  |  |  |     Py_XDECREF(frame->f_locals); | 
					
						
							| 
									
										
										
										
											2022-08-25 10:16:55 +01:00
										 |  |  |     Py_DECREF(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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-18 10:10:15 +01:00
										 |  |  |     PyObject *code = (PyObject *)frame->f_code; | 
					
						
							| 
									
										
										
										
											2023-05-05 17:53:07 +01:00
										 |  |  |     Py_INCREF(code); | 
					
						
							|  |  |  |     return code; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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); | 
					
						
							|  |  |  |     return PyCode_Addr2Line(frame->f_code, addr); | 
					
						
							|  |  |  | } |