bpo-46417: _thread uses PyStructSequence_NewType() (GH-30733) · python/cpython@f389b37
@@ -28,6 +28,7 @@ static struct PyModuleDef thread_module;
282829293030typedef struct {
31+PyTypeObject *excepthook_type;
3132PyTypeObject *lock_type;
3233PyTypeObject *local_type;
3334PyTypeObject *local_dummy_type;
@@ -1473,8 +1474,6 @@ PyDoc_STRVAR(ExceptHookArgs__doc__,
14731474\n\
14741475Type used to pass arguments to threading.excepthook.");
147514761476-static PyTypeObject ExceptHookArgsType;
1477-14781477static PyStructSequence_Field ExceptHookArgs_fields[] = {
14791478 {"exc_type", "Exception type"},
14801479 {"exc_value", "Exception value"},
@@ -1492,9 +1491,11 @@ static PyStructSequence_Desc ExceptHookArgs_desc = {
149214911493149214941493static PyObject *
1495-thread_excepthook(PyObject *self, PyObject *args)
1494+thread_excepthook(PyObject *module, PyObject *args)
14961495{
1497-if (!Py_IS_TYPE(args, &ExceptHookArgsType)) {
1496+thread_module_state *state = get_thread_state(module);
1497+1498+if (!Py_IS_TYPE(args, state->excepthook_type)) {
14981499PyErr_SetString(PyExc_TypeError,
14991500"_thread.excepthook argument type "
15001501"must be ExceptHookArgs");
@@ -1629,18 +1630,17 @@ thread_module_exec(PyObject *module)
16291630return -1;
16301631 }
163116321632-if (ExceptHookArgsType.tp_name == NULL) {
1633-if (PyStructSequence_InitType2(&ExceptHookArgsType,
1634-&ExceptHookArgs_desc) < 0) {
1635-return -1;
1636- }
1637- }
1638-16391633// Add module attributes
16401634if (PyDict_SetItemString(d, "error", ThreadError) < 0) {
16411635return -1;
16421636 }
1643-if (PyModule_AddType(module, &ExceptHookArgsType) < 0) {
1637+1638+// _ExceptHookArgs type
1639+state->excepthook_type = PyStructSequence_NewType(&ExceptHookArgs_desc);
1640+if (state->excepthook_type == NULL) {
1641+return -1;
1642+ }
1643+if (PyModule_AddType(module, state->excepthook_type) < 0) {
16441644return -1;
16451645 }
16461646@@ -1664,6 +1664,7 @@ static int
16641664thread_module_traverse(PyObject *module, visitproc visit, void *arg)
16651665{
16661666thread_module_state *state = get_thread_state(module);
1667+Py_VISIT(state->excepthook_type);
16671668Py_VISIT(state->lock_type);
16681669Py_VISIT(state->local_type);
16691670Py_VISIT(state->local_dummy_type);
@@ -1674,6 +1675,7 @@ static int
16741675thread_module_clear(PyObject *module)
16751676{
16761677thread_module_state *state = get_thread_state(module);
1678+Py_CLEAR(state->excepthook_type);
16771679Py_CLEAR(state->lock_type);
16781680Py_CLEAR(state->local_type);
16791681Py_CLEAR(state->local_dummy_type);