diff options
author | Victor Stinner <vstinner@python.org> | 2022-01-21 01:51:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-21 01:51:04 (GMT) |
commit | f389b37fb1cebe7ed66331cdd373a014695261f6 (patch) | |
tree | f328c5fd2878572d61d17f030fa1e82ef23a840c | |
parent | 6415e2ee4955b1a995c1e75544e2506b03780c3d (diff) | |
download | cpython-f389b37fb1cebe7ed66331cdd373a014695261f6.zip cpython-f389b37fb1cebe7ed66331cdd373a014695261f6.tar.gz cpython-f389b37fb1cebe7ed66331cdd373a014695261f6.tar.bz2 |
bpo-46417: _thread uses PyStructSequence_NewType() (GH-30733)
The _thread module now creates its _ExceptHookArgs type as a heap
type using PyStructSequence_NewType(), rather than using a static
type.
-rw-r--r-- | Modules/_threadmodule.c | 26 |
1 files changed, 14 insertions, 12 deletions
diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index cde2e0b..9e6e462 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -28,6 +28,7 @@ static struct PyModuleDef thread_module; typedef struct { + PyTypeObject *excepthook_type; PyTypeObject *lock_type; PyTypeObject *local_type; PyTypeObject *local_dummy_type; @@ -1473,8 +1474,6 @@ PyDoc_STRVAR(ExceptHookArgs__doc__, \n\ Type used to pass arguments to threading.excepthook."); -static PyTypeObject ExceptHookArgsType; - static PyStructSequence_Field ExceptHookArgs_fields[] = { {"exc_type", "Exception type"}, {"exc_value", "Exception value"}, @@ -1492,9 +1491,11 @@ static PyStructSequence_Desc ExceptHookArgs_desc = { static PyObject * -thread_excepthook(PyObject *self, PyObject *args) +thread_excepthook(PyObject *module, PyObject *args) { - if (!Py_IS_TYPE(args, &ExceptHookArgsType)) { + thread_module_state *state = get_thread_state(module); + + if (!Py_IS_TYPE(args, state->excepthook_type)) { PyErr_SetString(PyExc_TypeError, "_thread.excepthook argument type " "must be ExceptHookArgs"); @@ -1629,18 +1630,17 @@ thread_module_exec(PyObject *module) return -1; } - if (ExceptHookArgsType.tp_name == NULL) { - if (PyStructSequence_InitType2(&ExceptHookArgsType, - &ExceptHookArgs_desc) < 0) { - return -1; - } - } - // Add module attributes if (PyDict_SetItemString(d, "error", ThreadError) < 0) { return -1; } - if (PyModule_AddType(module, &ExceptHookArgsType) < 0) { + + // _ExceptHookArgs type + state->excepthook_type = PyStructSequence_NewType(&ExceptHookArgs_desc); + if (state->excepthook_type == NULL) { + return -1; + } + if (PyModule_AddType(module, state->excepthook_type) < 0) { return -1; } @@ -1664,6 +1664,7 @@ static int thread_module_traverse(PyObject *module, visitproc visit, void *arg) { thread_module_state *state = get_thread_state(module); + Py_VISIT(state->excepthook_type); Py_VISIT(state->lock_type); Py_VISIT(state->local_type); Py_VISIT(state->local_dummy_type); @@ -1674,6 +1675,7 @@ static int thread_module_clear(PyObject *module) { thread_module_state *state = get_thread_state(module); + Py_CLEAR(state->excepthook_type); Py_CLEAR(state->lock_type); Py_CLEAR(state->local_type); Py_CLEAR(state->local_dummy_type); |