Skip to content

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

Closed

Description

@pfalcon
mannequin
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']

    Activity

    1. pfalcon commented on Sep 29, 2019

      pfalconmannequin
      MannequinAuthor

      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.

    2. ammaraskar commented on Sep 29, 2019

      @ammaraskar
      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.

    3. vstinner commented on Dec 15, 2019

      @vstinner
      Member

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

    4. miss-islington commented on Dec 19, 2019

      @miss-islington
      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

    5. miss-islington commented on Dec 19, 2019

      @miss-islington
      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

    6. vstinner commented on Dec 19, 2019

      @vstinner
      Member

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

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions