Skip to content

Conversation

jbower-fb
Copy link
Contributor

@jbower-fb jbower-fb commented Nov 19, 2024

I just spent several hours fixing a bug in the Cinder JIT as a result of a misunderstanding of the value of co_nlocalsplus. The existing comment implied co_nlocalsplus is just the literal sum of co_nlocals + co_ncellvars + co_nfreevars. However, this is not the case. It may be smaller than this as locals can also be cells.

Summary:

Test Plan:

Reviewers:

Subscribers:

Tasks:

Tags:
facebook-github-bot pushed a commit to facebookincubator/cinderx that referenced this pull request Nov 19, 2024
Summary:
In CPython's code.h it says of `PyCodeObject::co_nlocalsplus`:
```
    int co_nlocalsplus;           /* number of local + cell + free variables */
```
This turns out to be very misleading - it IS the number of those variables but that number is not equal to the sum of the count of each type. The reason for this is some variables can be both a local and a cell.

For more context see: https://github.com/python/cpython/blob/d6b3e78504b3168c432b20002dbcf8ec9a435e61/Objects/codeobject.c#L334

PR up to fix the comment: python/cpython#126993

Reviewed By: martindemello

Differential Revision: D66140761

fbshipit-source-id: 635e6d01616ac27c56af8159590283ab5ffbf0e1
@DinoV DinoV merged commit 3fafc1b into python:main Nov 21, 2024
41 checks passed
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants