title: Is it possible to programmatically construct a Python stack frame and start execution at an arbitrary point in the code?
body: <p>Is it possible to programmatically construct a stack (one or more stack frames) in CPython and start execution at an arbitrary code point?  Imagine the following scenario:</p>
  
  <ol>
  <li><p>You have a workflow engine where workflows can be scripted in Python with some constructs (e.g. branching, waiting/joining) that are calls to the workflow engine.</p></li>
  <li><p>A blocking call, such as a wait or join sets up a listener condition in an event-dispatching engine with a persistent backing store of some sort.</p></li>
  <li><p>You have a workflow script, which calls the Wait condition in the engine, waiting for some condition that will be signalled later.  This sets up the listener in the event dispatching engine.</p></li>
  <li><p>The workflow script's state, relevant stack frames including the program counter (or equivalent state) are persisted - as the wait condition could occur days or months later.</p></li>
  <li><p>In the interim, the workflow engine might be stopped and re-started, meaning that it must be possible to programmatically store and reconstruct the context of the workflow script.</p></li>
  <li><p>The event dispatching engine fires the event that the wait condition picks up.</p></li>
  <li><p>The workflow engine reads the serialised state and stack and reconstructs a thread with the stack.  It then continues execution at the point where the wait service was called.</p></li>
  </ol>
  
  <p><strong>The Question</strong></p>
  
  <p>Can this be done with an unmodified Python interpreter?  Even better, can anyone point me to some documentation that might cover this sort of thing or an example of code that programmatically constructs a stack frame and starts execution somewhere in the middle of a block of code?</p>
  
  <p><strong>Edit:</strong> To clarify 'unmodified python interpreter', I don't mind using the C API (is there enough information in a PyThreadState to do this?) but I don't want to go poking around the internals of the Python interpreter and having to build a modified one.</p>
  
  <p><strong>Update:</strong> From some initial investigation, one can get the execution context with <code>PyThreadState_Get()</code>.  This returns the thread state in a <code>PyThreadState</code> (defined in <code>pystate.h</code>), which has a reference to the stack frame in <code>frame</code>.  A stack frame is held in a struct typedef'd to <code>PyFrameObject</code>, which is defined in <code>frameobject.h</code>.  <code>PyFrameObject</code> has a field <code>f_lasti</code> (props to <a href="https://stackoverflow.com/questions/541329/is-it-possible-to-programatically-construct-a-python-stack-frame-and-start-execut/541529#541529">bobince</a>) which has a program counter expressed as an offset from the beginning of the code block.</p>
  
  <p>This last is sort of good news, because it means that as long as you preserve the actual compiled code block, you should be able to reconstruct locals for as many stack frames as necessary and re-start the code.  I'd say this means that it is theoretically possible without having to make a modified python interpereter, although it means that the code is still probably going to be fiddly and tightly coupled to specific versions of the interpreter.</p>
  
  <p>The three remaining problems are: </p>
  
  <ul>
  <li><p>Transaction state and 'saga' rollback, which can probably be accomplished by the sort of metaclass hacking one would use to build an O/R mapper.  I did build a prototype once, so I have a fair idea of how this might be accomplished.</p></li>
  <li><p>Robustly serialising transaction state and arbitrary locals.  This might be accomplished by reading <code>__locals__</code> (which is available from the stack frame) and programatically constructing a call to pickle.  However, I don't know what, if any, gotchas there might be here.</p></li>
  <li><p>Versioning and upgrade of workflows.  This is somewhat trickier, as the system is not providing any symbolic anchors for workflow nodes.  All we have is the anchor
  In order to do this, one would have to identify the offsets of all of the entry points and map them to the new version.  Probably feasible to do manually, but I suspect it would be hard to automate.  This is probably the biggest obstacle if you want to support this capability.  </p></li>
  </ul>
  
  <p><strong>Update 2:</strong> <code>PyCodeObject</code> (<code>code.h</code>) has a list of addr (<code>f_lasti</code>)-> line number mappings in <code>PyCodeObject.co_lnotab</code> (correct me if wrong here).  This might be used to facilitate a migration process to update workflows to a new version, as frozen instruction pointers could be mapped to the appropriate place in the new script, done in terms of the line numbers.  Still quite messy but a little more promising.</p>
  
  <p><strong>Update 3:</strong> I think the answer to this might be <a href="http://www.stackless.com" rel="nofollow noreferrer">Stackless Python.</a>  You can suspend tasks and serialise them.  I haven't worked out whether this will also work with the stack as well.</p>