classification
Title: inspect.py: access block stack
Type: enhancement Stage:
Components: Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Dennis Sweeney, gchalony
Priority: normal Keywords:

Created on 2020-09-24 08:23 by gchalony, last changed 2020-09-25 02:48 by Dennis Sweeney.

Messages (2)
msg377442 - (view) Author: Gregoire Chalony (gchalony) Date: 2020-09-24 08:23
The inspect module doesn't currently expose any way to access the block stack at runtime.
Would that be possible?

Aked on StackOverflow here: https://stackoverflow.com/questions/63187845/cpython-access-loop-depth-level-block-stack.
msg377475 - (view) Author: Dennis Sweeney (Dennis Sweeney) * Date: 2020-09-25 02:48
I believe the block stack is only for catching exceptions. Loops do not interact with the block stack. Only SETUP_FINALLY adds to the block stack:

https://docs.python.org/3/library/dis.html#opcode-SETUP_FINALLY

Meanwhile, loops and conditionals and the like are compiled into goto-like jumps.

There may be some existing solution, but I'm not sure what. Maybe look at the traceback or ast modules.
History
Date User Action Args
2020-09-25 02:48:48Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg377475
2020-09-24 08:23:10gchalonycreate