[3.6] bpo-30604: Move co_extra_freefuncs to interpreter state to avoid crashes in threads (#2015)
* Move co_extra_freefuncs to interpreter state to avoid crashes in
multi-threaded scenarios involving deletion of code objects
* Don't require that extra be zero initialized
* Build test list instead of defining empty test class
* Ensure extra is always assigned on success
* Keep the old fields in the thread state object, just don't use them
Add new linked list of code extra objects on a per-interpreter basis
so that interpreter state size isn't changed
* Rename __PyCodeExtraState_Get and add comment about it going away in 3.7
Fix sort order of import's in test_code.py
* Remove an extraneous space
* Remove docstrings for comments
* Touch up formatting
* Fix casing of coextra local
* Fix casing of another variable
* Prefix PyCodeExtraState with __ to match C API for getting it
* Update NEWS file for bpo-30604
diff --git a/Python/ceval.c b/Python/ceval.c
index eba892c..ea79f5f 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -5453,14 +5453,14 @@
Py_ssize_t
_PyEval_RequestCodeExtraIndex(freefunc free)
{
- PyThreadState *tstate = PyThreadState_Get();
+ __PyCodeExtraState *state = __PyCodeExtraState_Get();
Py_ssize_t new_index;
- if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
+ if (state->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
return -1;
}
- new_index = tstate->co_extra_user_count++;
- tstate->co_extra_freefuncs[new_index] = free;
+ new_index = state->co_extra_user_count++;
+ state->co_extra_freefuncs[new_index] = free;
return new_index;
}