Yeah, I answered my own question. In short, it’s impossible, though I really hope Python could expose this information, which can unblock a lot of things.
I would recommend creating a new thread in capi-sig@python.org to further discuss this. I don’t have a specific answer for you, but I suspect that you’d be able to find an answer there. Be sure to search through the archives to see if it was previously addressed.
Also, if you do manage to find a more detailed answer, I’m sure that others who encounter this topic would greatly appreciate if it were posted here (and perhaps in the stack overflow question).
Thanks Kyle. I can send an email to discuss if making changes to expose this information makes sense. As for the current codebase, I’m pretty sure it’s impossible, as explained in this answer: https://stackoverflow.com/a/44443331/2142577
I don’t think it makes much sense to expose details like this. It would mean we couldn’t do things like switch to a register-based VM instead of a stack-based one.
Why do you want this information? What’s the actual problem you’re trying to solve?
Even if it was non-public API (which debuggers could use), keeping the info in a more accessible place than a local variable would probably slow things down. Keeping another place updated (like we do with yield, but continuously) sounds even slower.
Update it in PUSH/POP/STACK_GROW/STACK_SHRINK macros (When these called, f, the frame object, and stack_pointer are available. These should always be used when the stack is adjusted. If you find a place where that’s not the case, it’s a bug; please let me know.)
If (and it’s a big if) you can find a way to get this information out without sacrificing performance, and the patch is not too unmaintainable, perhaps we can add it in.