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!

> > here's a cool thing you can do. this function disassembles itself. it shows 
> > a call to the function to get the stack frame and pass it to the 
> > disassembler:

> >         122 RETURN_VALUE
> > oops:
> > exec(disassemble_self.__code__.replace(co_code=a.__code__.co_code))
> > Segmentation fault
> >

>>> import sys, dis
>>> def a1():
...     b = 1
...     return b
... 
>>> def a2():
...     b = 1
...     b += 1
...     return b
... 
>>> def a3():
...     b = 1
...     magicfunc()
...     return b
... 
>>> list(a1.__code__.co_code)
[151, 0, 100, 1, 125, 0, 124, 0, 83, 0]
>>> list(a2.__code__.co_code)
[151, 0, 100, 1, 125, 0, 124, 0, 100, 1, 122, 13, 0, 0, 125, 0, 124, 0, 83, 0]
>>> list(a3.__code__.co_code)
[151, 0, 100, 1, 125, 0, 116, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 166, 0, 0, 0, 
171, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 124, 0, 83, 0]

ok ummmm so what am i trying to do
what happens if i mutate the code object on the stack frame? is that possible? 
likely not. how do i figure out if i can?
i'm assuming the segfault was from indexing a nonexistent local variable name 
rather than changing something in-place

>>> sys._getframe().f_code = sys._getframe().f_code
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: attribute 'f_code' of 'frame' objects is not writable

the frame doesn't seem to be writeable.
further options:
- force writeability of these attributes by engaging python internals
- nonreturning

what about exceptions? could i add an exception handler in the caller's stack 
and catch it ...? (unlikely but i could maybe place a global exception handler)

if i interrupt their execution how do i know what their variables were at the 
time?

what would happen if i forked() with memory changes? i could do these by trial 
:s

uhhhh

so for example i could fork and suspend, and then inspect the frozen child. of 
course i might as well just inspect my own stack!

Reply via email to