@@ -221,6 +221,61 @@ def mouse_input_wrapper(prompt=''):
221221def get_user_stdout (frame ):
222222 return frame .f_globals ['__user_stdout__' ].getvalue ()
223223
224+ '''
225+ Dec 2013
226+ Okay, what's with this f_valuestack business?
227+
228+ If you compile your own CPython and patch Objects/frameobject.c to add a
229+ Python accessor for f_valuestack, then you can actually access the value
230+ stack, which is useful for, say, grabbbing the objects within
231+ list/set/dict comprehensions as they're being built. e.g., try:
232+
233+ z = [x*y for x in range(5) for y in range(5)]
234+
235+
236+ Patch:
237+
238+ static PyObject *
239+ frame_getlineno(PyFrameObject *f, void *closure)
240+ {
241+ return PyLong_FromLong(PyFrame_GetLineNumber(f));
242+ }
243+
244+ +// copied from Py2crazy, which was for Python 2, but let's hope this still works!
245+ +static PyObject *
246+ +frame_getvaluestack(PyFrameObject* f) {
247+ + // pgbovine - TODO: will this memory leak? hopefully not,
248+ + // since all other accessors seem to follow the same idiom
249+ + PyObject* lst = PyList_New(0);
250+ + if (f->f_stacktop != NULL) {
251+ + PyObject** p = NULL;
252+ + for (p = f->f_valuestack; p < f->f_stacktop; p++) {
253+ + PyList_Append(lst, *p);
254+ + }
255+ + }
256+ +
257+ + return lst;
258+ +}
259+ +
260+ /* Setter for f_lineno - you can set f_lineno from within a trace function in
261+ * order to jump to a given line of code, subject to some restrictions. Most
262+ * lines are OK to jump to because they don't make any assumptions about the
263+ @@ -368,6 +384,11 @@
264+
265+ static PyGetSetDef frame_getsetlist[] = {
266+ {"f_locals", (getter)frame_getlocals, NULL, NULL},
267+ {"f_lineno", (getter)frame_getlineno,
268+ (setter)frame_setlineno, NULL},
269+ {"f_trace", (getter)frame_gettrace, (setter)frame_settrace, NULL},
270+ +
271+ + // pgbovine
272+ + {"f_valuestack",(getter)frame_getvaluestack,
273+ + (setter)NULL /* don't let it be set */, NULL},
274+ +
275+ {0}
276+ };
277+ '''
278+
224279# at_global_scope should be true only if 'frame' represents the global scope
225280def get_user_globals (frame , at_global_scope = False ):
226281 d = filter_var_dict (frame .f_globals )
0 commit comments