
karl3@writeme.com wrote:
karl3@writeme.com wrote:
karl3@writeme.com wrote: karl3@writeme.com wrote: karl3@writeme.com wrote: karl3@writeme.com wrote: GPT-4 User Come up with an analog to "goto" in python that engages the raw interpreter and/or bytecode systems of python to function. i want to try! it sounds like one preference is to see if it works to create a function that accesses the bytes of its caller's code and uses ctypes to modify them taking a pause cause it feels funny, might do a different thing now i bet however pdb and trace work would be a higher level way without ctypes! they use sys.settrace() which lets one install an event handler for major interpreter actions like line, call, return meanwhile, the frame object does have access to the local variables of callers, so one could have a function that turns callers into coroutines by collecting little chunks of code and evaluating them with injected locals. whenever a goto()-like function is called, execution would return to this alternative-vm that would break functions apart and move between frames by injecting locals into bytecode. it would only return if its original frame were goto'd back into. a little wonky but interesting!
the frame.f_locals[] object is writeable, so one could theoretically update the locals of callers properly if they are changed in a side-vm
ok given claude also suggest a preprocessing approach, i realized one can run global constructor code in python by putting at file scope of a module! so basically you'd have your goto() module instrument your codebase when it is imported. you could for example replace all calls to its goto function with jump instructions by seating new code objects using .__code__ = .__code__.replace(co_code=bytes(...)) . it's a little edgy. you'd have to instrument further imports of code somehow. you could raise exceptions for conditions like using the goto object for anything other than a function call. you could simplify it a little by using something other than a global object, like maybe instead a literal expression like `'__goto' 'label'` or anything, then you don't have to worry about it being otherwise captured or unexpectedly used then the big challenges would be (a) finding all imported code (likely there's a module function to enumerate this); (b) instrumenting compilation of code constructed after you and (c) figuring out what to do about the vm stack after the jump complets {-S