Skip to content
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

Closed
pfalcon mannequin opened this issue Sep 29, 2019 · 6 comments
Closed

docs: Code object's "co_stacksize" field is described with mistake #82497

pfalcon mannequin opened this issue Sep 29, 2019 · 6 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir

Comments

@pfalcon
Copy link
Mannequin

pfalcon mannequin commented Sep 29, 2019

BPO 38316
Nosy @pfalcon, @vstinner, @ammaraskar, @miss-islington
PRs
  • bpo-38316: describe co_stacksize a little bit better #16983
  • [3.7] bpo-38316: Fix co_stacksize documentation (GH-16983). #17660
  • [3.8] bpo-38316: Fix co_stacksize documentation (GH-16983) #17661
  • 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:

    assignee = None
    closed_at = <Date 2019-12-19.14:51:51.667>
    created_at = <Date 2019-09-29.20:34:54.438>
    labels = ['3.7', '3.8', '3.9', 'docs']
    title = 'docs: Code object\'s "co_stacksize" field is described with mistake'
    updated_at = <Date 2019-12-19.14:51:51.666>
    user = 'https://github.com/pfalcon'

    bugs.python.org fields:

    activity = <Date 2019-12-19.14:51:51.666>
    actor = 'vstinner'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2019-12-19.14:51:51.667>
    closer = 'vstinner'
    components = ['Documentation']
    creation = <Date 2019-09-29.20:34:54.438>
    creator = 'pfalcon'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38316
    keywords = ['patch']
    message_count = 6.0
    messages = ['353508', '353511', '358454', '358681', '358682', '358683']
    nosy_count = 5.0
    nosy_names = ['pfalcon', 'vstinner', 'docs@python', 'ammar2', 'miss-islington']
    pr_nums = ['16983', '17660', '17661']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue38316'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @pfalcon
    Copy link
    Mannequin Author

    pfalcon mannequin commented Sep 29, 2019

    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:

    • "co_nlocals is the number of local variables used by the function (including arguments);"
    • "co_stacksize is the required stack size (including local variables);"

    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.

    @pfalcon pfalcon mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes labels Sep 29, 2019
    @pfalcon pfalcon mannequin assigned docspython Sep 29, 2019
    @pfalcon pfalcon mannequin added the docs Documentation in the Doc dir label Sep 29, 2019
    @ammaraskar
    Copy link
    Member

    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.

    @vstinner
    Copy link
    Member

    New changeset d587272 by Victor Stinner (Batuhan Taşkaya) in branch 'master':
    bpo-38316: Fix co_stacksize documentation (GH-16983)
    d587272

    @miss-islington
    Copy link
    Contributor

    New changeset 917419f by Miss Islington (bot) (Batuhan Taşkaya) in branch '3.7':
    [3.7] bpo-38316: Fix co_stacksize documentation (GH-16983). (GH-17660)
    917419f

    @miss-islington
    Copy link
    Contributor

    New changeset eba61f3 by Miss Islington (bot) (Batuhan Taşkaya) in branch '3.8':
    [3.8] bpo-38316: Fix co_stacksize documentation (GH-16983) (GH-17661)
    eba61f3

    @vstinner
    Copy link
    Member

    Thanks Paul Sokolovsky for the bug report and thanks Batuhan Taşkaya for the fix ;-)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes docs Documentation in the Doc dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants