[issue38316] docs: Code object's "co_stacksize" field is described with mistake

Ammar Askar report at bugs.python.org
Sun Sep 29 17:47:58 EDT 2019


Ammar Askar <ammar at ammaraskar.com> added the comment:

Yeah, that parenthesized bit seems a bit weird: co_stacksize really has nothing to do with the number of variables, it's just that certain opcodes (https://docs.python.org/3/library/dis.html#python-bytecode-instructions) push and pop off the stack, co_stacksize is just the largest the stack will ever grow to from these operations.

For example:

  >>> def f():
  ...   a = 1
  ...   b = 2
  ...   c = 3
  ...   g(a, b, c)
  ...
  >>> f.__code__.co_stacksize
  4

and

  >>> def g():
  ...   g(1, 2, 3)
  ...
  >>> g.__code__.co_stacksize
  4

have the exact same stack size despite differences in variables because the call to `g` has to push all 3 operands (and g itself) onto the stack.

----------
nosy: +ammar2

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue38316>
_______________________________________


More information about the Python-bugs-list mailing list