New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: Code object's "co_stacksize" field is described with mistake #82497
Comments
CPython's Data Model -> Internal types -> Code objects, direct link as of version 3.7 is: https://docs.python.org/3.7/reference/datamodel.html?highlight=co_stacksize#index-55 , states following:
However, practical checking shows that co_stacksize is the required stack size WITHOUT local variables. One could argue about the meaning of "local variables" in the description of co_stacksize above, saying that what is meant is temporary stack variables of something. That's why I quoted above co_nlocals description too, it clearly shows that local variebles are local function variables (include functions args), and nothing else. Code to reproduce: ====== def func():
a = 1
b = 2
c = 3
print("co_stacksize", func.__code__.co_stacksize)
print("co_nlocals", func.__code__.co_nlocals) ====== Result of running: $ python3.7 script_under_test.py
co_stacksize 1
co_nlocals 3 ====== Clearly, co_stacksize doesn't include the size of local vars, or it would have been larger than co_nlocals in printout. |
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 |
Thanks Paul Sokolovsky for the bug report and thanks Batuhan Taşkaya for the fix ;-) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: