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: Number of object on stack required by opcode
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, mdartiailh
Priority: normal Keywords:

Created on 2021-02-04 21:25 by mdartiailh, last changed 2022-04-11 14:59 by admin.

Messages (2)
msg386494 - (view) Author: Matthieu Dartiailh (mdartiailh) * Date: 2021-02-04 21:25
When constructing bytecode object manually as can be done using the bytecode library (https://github.com/MatthieuDartiailh/bytecode which was first developed by V Stinner), one can use dis.stack_effect to compute the required stack size, thus avoiding stack overflows.

However it can be interesting for those manually built bytecode object to also check that no underflow can occur. This computation is straightforward once one knows the number of element on the stack a specific opcode expects. 

This works has been done manually in the bytecode project, but it may interesting to provide a way in the dis module to access this information with an interface similar to dis.stack_effect.

If there is an interest in such a feature I would be happy to contribute it. I would however like some opinion on how to do that in an optimal manner. I assume it would require to add the implementation in https://github.com/python/cpython/blob/master/Python/compile.c and expose it in a similar manner to stack_effect.
msg386530 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-02-05 16:33
stack_effect is implemented in compile.c because it is used there.

If you need something for an application like bytecode, it doesn't need to be in the cpython c code. It can be in the standard library in opcode.py, or even in bytecode library itself. The question is of course how to keep it up to date, and this is pretty much the same problem whether it's part of compile.c, the standard library or an external application.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87295
2021-02-05 16:33:12iritkatrielsetnosy: + iritkatriel
messages: + msg386530
2021-02-04 21:25:49mdartiailhcreate