classification
Title: [PATCH] Debuggers need a way to change the locals of a frame
Type: enhancement Stage: patch review
Components: Interpreter Core Versions: Python 3.3
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, arigo, fabioz, loewis, rhettinger
Priority: normal Keywords: patch

Created on 2007-02-07 17:12 by fabioz, last changed 2010-08-19 23:40 by fabioz.

Files
File name Uploaded Description Edit
test_frames.py fabioz, 2007-02-07 17:12
Messages (6)
msg55003 - (view) Author: Fabio Zadrozny (fabioz) Date: 2007-02-07 17:12
Debuggers need a way to change the local variables in a given frame... currently, this works only if this change is done in the topmost frame (and under certain circumstances), but it should work for any frame.

Initial discussion at:
http://mail.python.org/pipermail/python-dev/2007-February/070884.html

Apparently, the problem is the order in which PyFrame_LocalsToFast / PyFrame_FastToLocals is called.

The proposed solution to this is having a savelocals() method in the frame object and have it reflect the changes in its returned dict into its locals. It will simply enable users to call PyFrame_LocalsToFast externally after a change, to be sure that it will not be changed (and it must be done before another call to PyFrame_LocalsToFast -- which I don't see as a large problem, because it is of restrict use -- mainly for debuggers).


--------- frameobject.c Patch part 1: -----------------

static PyObject *
PyFrame_SaveLocals(PyFrameObject *f)
{
    PyFrame_LocalsToFast(f, 0);
	Py_INCREF(Py_None);
	return Py_None;
}

static PyMethodDef frame_methodlist[] = {
    {"savelocals", (PyCFunction)PyFrame_SaveLocals, METH_NOARGS,
     "After a change in f_locals, this method should be called to save the changes internally."
    },
    {NULL}  /* Sentinel */
};


---------- frameobject.c Patch part 2: ---------------
Add to PyTypeObject PyFrame_Type:

frame_methodlist,/* tp_methods */

------------------ end patch -------------------------

I'm sorry that this is not in an actual patch format... but as I didn't get it from the cvs, it is easier for me to explain it (as it is a rather small patch).

Attached is a test-case for this patch.

Thanks, 

Fabio
msg55004 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-02-08 08:38
Why don't you set the clear parameter to 1?

Please do submit a patch, you can use 'diff -ur' to create a recursive unified diff between source trees. Please also try to come up with a patch to the documentation.
msg55005 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2007-02-10 20:16
A point of detail probably, but I suppose that instead of introducing a new method on frame objects, we could allow f_locals to be a writeable attribute.  Setting it to a dictionary would update the value of the local variables.  It's a bit of a hack, but so is the need for an explicit savelocals() method.

A cleaner solution is to have f_locals return a dict-like object instead of a real dict when appropriate, but it takes more efforts to implement.
msg110520 - (view) Author: Mark Lawrence (BreamoreBoy) Date: 2010-07-16 22:37
Fabio, could you please supply a patch as requested by Martin in msg55004?  Also note Armin's comments in msg55005.
msg113625 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2010-08-11 20:07
I think this falls under the language moratorium in that it requires core changes that make it more difficult for other implementations to catch-up.
msg114415 - (view) Author: Fabio Zadrozny (fabioz) Date: 2010-08-19 23:40
I agree that it'd be cleaner making the frame locals a dict-like object with write access, but I wouldn't be able to do that because of time constraints (and I'd have to research more how to do it and it'd be much more intrusive I guess).

So, if it's guaranteed that it'll be accepted I can provide a patch (changing the clear to 1) of the savelocals() version.

I guess I don't agree this falls in the language moratorium, but if that's the case I can wait 1 or 2 more years before submitting the patch :)
History
Date User Action Args
2010-08-19 23:40:52fabiozsetmessages: + msg114415
2010-08-11 20:07:34rhettingersetnosy: + rhettinger

messages: + msg113625
versions: + Python 3.3, - Python 3.2
2010-08-09 04:20:57terry.reedysetversions: + Python 3.2, - Python 3.1, Python 2.7
2010-07-16 22:37:50BreamoreBoysetnosy: + BreamoreBoy
messages: + msg110520
2009-03-30 21:24:06ajaksu2setkeywords: + patch
stage: patch review
versions: + Python 3.1, Python 2.7, - Python 2.6
2007-02-07 17:12:28fabiozcreate