Fix caching list comprehensions #2484
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2305.
The reason this is happening is:
co_cellvarsandco_freevarsin a table, and later looking them up when we resolve references.co_consts, with their ownco_cellvarsandco_freevarsnot listed in the outer function.co_cellvarsandco_freevars, so when we try to resolve references found inside the listcomp, it can't find them in our table, and crashes.What we need to do is save all references as we recursively traverse our "tree" of nested code objects. In pseudocode, it looks something like this:
For example, this code
would produce (roughly) the following object hierarchy:
So if we're hashing
create_raw_data, our cell table state would look like this over time.create_raw_data:{}{'outer': ...}{'outer': ..., 'inner': ...}{'outer': ...}{}create_raw_data:{}I basically replaced
Context.cells, which was previously a simpledict, with a custom class that achieves this. Also, I added a test.(Note: I recommend reading the diff in split view, it's a lot more readable than unified.)