New submission from Skip Montanaro <skip.montan...@gmail.com>:

Consider this trivial function:

>>> def f():
...   while True:
...     pass
...

and its disassembly:

>>> dis.dis(f)
  3     >>    0 JUMP_ABSOLUTE               0
              2 LOAD_CONST                  0 (None)
              4 RETURN_VALUE

Despite its infinite-loop-ness, the generated LOAD_CONST/RETURN_VALUE pair 
suggests the code object's stacksize should be 1, but it's 0:

>>> print(f.__code__.co_stacksize)
0

I understand that the compiler might have decided the code was unreachable and 
the LOAD_CONST instruction would never be reached, but if that was the case, 
why would that instruction pair be generated?

This is only of interest because my register virtual machine translator trusts 
that the co_nlocals and co_stacksize attributes of the code object reflect the 
actual space allocation in the frame object. I can use

max(1, f.__code__.co_stacksize + f.__code__.co_nlocals)

as the allocated locals+stack space. (That first slot in the frame object will 
always be available.) That makes this example translate, but doesn't guarantee 
there's not a bug in determination of the stack size which could pop up again 
later.

----------
components: Interpreter Core
messages: 366692
nosy: skip.montanaro
priority: normal
severity: normal
status: open
title: Incorrect stacksize in code object
type: compile error
versions: Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue40315>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to