Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Document and enforce invariants
  • Loading branch information
gvanrossum committed Mar 20, 2024
commit 0fd96be84aa28ad987f76686139037a6352b06c1
17 changes: 16 additions & 1 deletion Objects/funcobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ function object and the code object.

The cache doesn't contain strong references; cache entries are
invalidated whenever the function or code object is deallocated.

Invariants
----------

These should hold at any time except when one of the cache-mutating
functions is running.

- For any slot s at index i:
- s->func == NULL or s->func->func_version % FUNC_VERSION_CACHE_SIZE == i
- s->code == NULL or s->code->co_version % FUNC_VERSION_CACHE_SIZE == i
if s->func != NULL, then s->func->func_code == s->code

*/

void
Expand Down Expand Up @@ -297,7 +309,10 @@ _PyFunction_ClearCodeByVersion(uint32_t version)
assert(PyCode_Check(slot->code));
PyCodeObject *code = (PyCodeObject *)slot->code;
if (code->co_version == version) {
slot->code = NULL;
slot->code = NULL;
if (slot->func != NULL) {
slot->code = slot->func->func_code;
}
}
}
}
Expand Down