Given a C Python frame pointer, how do I look at arbitrary evaluation stack entries? (Some specific stack entries can be found via
Here are two potential solution partial solutions, since this problem has no simple obvious answer, short of modifying the CPython interpreter or by instrumenting the bytecode beforehand the bytecode such as with byteplay to save such information.
Thanks to user2357112 for enlightenment on the difficulty of the problem, descriptions of the various Python stacks used at runtime,
The first solution is to write a C extension to access
f_valuestack which is the bottom (not top) of a frame. From that you can access values, and that too would have to go in the C extension. The C extension would wrap the PyFrameObject so it can get access to the unexposed field
f_valuestack. Although the PyFrameObject can change from Python version to Python version (so the extension might have to check which python version is running), it still is doable.
From that use an Abstract Virtual Machine to figure out which entry position you'd be at.
Something similar for my purposes would for my purposes would be to use a real but alternative VM, like Ned Batchhelder's byterun. It runs a Python bytecode interpreter in Python. The advantage here would be that since this acts as a second VM so stores don't change the running of the current and real CPython VM. However you'd still need to deal with the fact of interacting with external persistent state (e.g. calls across sockets or changes to files). And byterun would need to be extended to cover all opcodes and Python versions that potentially might be needed.
By the way for multi-version access to bytecode in a uniform way (since not only does bytecode change a bit, but also the collections of routines to access it), see xdis.
So although this isn't a general solution it could probably work for the special case of trying to figure out the value of say an
EXEC up which appear briefly on the evaluation stack.