Search code examples
pythonpython-3.xpyodide

Modify variable values in bdb without ctypes


I have a class extending bdb.Bbd that I use for analyzing Python 3 code. Now, I need to also modify some of the variable values as it steps through the program.

Outside of functions, I can do this by just modifying the frame.f_locals dictionary:

frame.f_locals['x'] = 2

I can't figure out how to do this inside functions without using the ctypes trick mentioned in this question. I'm running Python 3 inside Pyodide which unfortunately does not support ctypes yet.


Solution

  • It should be possible to do this as follows. Define a Javascript function:

    function frameLocalsToFast(frame){
       pyodide._module._PyFrame_LocalsToFast(frame.$$.ptr, 0);
       // Hopefully avoid memory leak
       frame.destroy();
    }
    

    Then import frameLocalsToFast into Python and call it when you want to update the frame.