'PyFrame / PyThreadState / C-API
I stumbled across code used in a c# program, which uses the python c api to replace the current frame with a "dummy" frame.
// Create dummy code (needed for the new frame)
_dummyCode = new PyObject(this, Py.PyCode_NewEmpty("", "", 1), true);
// Create a new frame
_frame = new PyObject(this,
Py.PyFrame_New(Py.GetThreadState(), _dummyCode, Import("__main__").Attribute("__dict__"), Import("__main__").Attribute("__dict__")), true);
// Exchange frames
_oldFrame = new PyObject(this, Py.ExchangePyFrame(_frame), false);
internal static IntPtr ExchangePyFrame(IntPtr frame)
{
var tstate = Marshal.ReadIntPtr(GetThreadState());
var prevFrame = Marshal.ReadIntPtr(tstate.Add(16));
Marshal.WriteIntPtr(tstate.Add(16), frame);
return prevFrame;
}
While I understand how the code is working, I do not see the reason why it is being used and the implications the exchange of a frame brings with it. Maybe someone could give me some insight or a hint into the right direction! Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
