This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

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

Created on 2020-09-24 08:23 by gchalony, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
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) * (Python committer) 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.
msg409292 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-12-29 07:32
With the advent of zero-cost exception handling in Python 3.10, there is no block stack, neither for exceptions nor for loops. These were always regarded as an implementation detail of the compiler. If you have any new ideas for a feature like this, I would suggest sending them to the Python-Ideas mailing list, but I'm closing this for now.
History
Date User Action Args
2022-04-11 14:59:36adminsetgithub: 86016
2021-12-29 07:32:26Dennis Sweeneysetstatus: open -> closed
resolution: out of date
messages: + msg409292

stage: resolved
2020-09-25 02:48:48Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg377475
2020-09-24 08:23:10gchalonycreate