Issue #18520: Add a new PyStructSequence_InitType2() function, same than
PyStructSequence_InitType() except that it has a return value (0 on success,
-1 on error).

 * PyStructSequence_InitType2() now raises MemoryError on memory allocation failure
 * Fix also some calls to PyDict_SetItemString(): handle error
diff --git a/Modules/_lsprof.c b/Modules/_lsprof.c
index fef255f..8947889 100644
--- a/Modules/_lsprof.c
+++ b/Modules/_lsprof.c
@@ -884,10 +884,12 @@
     PyDict_SetItemString(d, "Profiler", (PyObject *)&PyProfiler_Type);
 
     if (!initialized) {
-        PyStructSequence_InitType(&StatsEntryType,
-                                  &profiler_entry_desc);
-        PyStructSequence_InitType(&StatsSubEntryType,
-                                  &profiler_subentry_desc);
+        if (PyStructSequence_InitType2(&StatsEntryType,
+                                       &profiler_entry_desc) < 0)
+            return NULL;
+        if (PyStructSequence_InitType2(&StatsSubEntryType,
+                                       &profiler_subentry_desc) < 0)
+            return NULL;
     }
     Py_INCREF((PyObject*) &StatsEntryType);
     Py_INCREF((PyObject*) &StatsSubEntryType);
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index f85cdd4..73596d3 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -210,9 +210,14 @@
     if (m == NULL)
         return NULL;
     d = PyModule_GetDict(m);
-    if (!initialized)
-            PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
-    PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
+    if (!initialized) {
+        if (PyStructSequence_InitType2(&StructGrpType,
+                                       &struct_group_type_desc) < 0)
+            return NULL;
+    }
+    if (PyDict_SetItemString(d, "struct_group",
+                             (PyObject *)&StructGrpType) < 0)
+        return NULL;
     initialized = 1;
     return m;
 }
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index cc54790..1ae04c4 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -11518,19 +11518,23 @@
     if (!initialized) {
 #if defined(HAVE_WAITID) && !defined(__APPLE__)
         waitid_result_desc.name = MODNAME ".waitid_result";
-        PyStructSequence_InitType(&WaitidResultType, &waitid_result_desc);
+        if (PyStructSequence_InitType2(&WaitidResultType, &waitid_result_desc) < 0)
+            return NULL;
 #endif
 
         stat_result_desc.name = MODNAME ".stat_result";
         stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
         stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
         stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
-        PyStructSequence_InitType(&StatResultType, &stat_result_desc);
+        if (PyStructSequence_InitType2(&StatResultType, &stat_result_desc) < 0)
+            return NULL;
         structseq_new = StatResultType.tp_new;
         StatResultType.tp_new = statresult_new;
 
         statvfs_result_desc.name = MODNAME ".statvfs_result";
-        PyStructSequence_InitType(&StatVFSResultType, &statvfs_result_desc);
+        if (PyStructSequence_InitType2(&StatVFSResultType,
+                                       &statvfs_result_desc) < 0)
+            return NULL;
 #ifdef NEED_TICKS_PER_SECOND
 #  if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
         ticks_per_second = sysconf(_SC_CLK_TCK);
@@ -11543,12 +11547,15 @@
 
 #if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER)
         sched_param_desc.name = MODNAME ".sched_param";
-        PyStructSequence_InitType(&SchedParamType, &sched_param_desc);
+        if (PyStructSequence_InitType2(&SchedParamType, &sched_param_desc) < 0)
+            return NULL;
         SchedParamType.tp_new = sched_param_new;
 #endif
 
         /* initialize TerminalSize_info */
-        PyStructSequence_InitType(&TerminalSizeType, &TerminalSize_desc);
+        if (PyStructSequence_InitType2(&TerminalSizeType,
+                                       &TerminalSize_desc) < 0)
+            return NULL;
     }
 #if defined(HAVE_WAITID) && !defined(__APPLE__)
     Py_INCREF((PyObject*) &WaitidResultType);
@@ -11566,11 +11573,13 @@
 #endif
 
     times_result_desc.name = MODNAME ".times_result";
-    PyStructSequence_InitType(&TimesResultType, &times_result_desc);
+    if (PyStructSequence_InitType2(&TimesResultType, &times_result_desc) < 0)
+        return NULL;
     PyModule_AddObject(m, "times_result", (PyObject *)&TimesResultType);
 
     uname_result_desc.name = MODNAME ".uname_result";
-    PyStructSequence_InitType(&UnameResultType, &uname_result_desc);
+    if (PyStructSequence_InitType2(&UnameResultType, &uname_result_desc) < 0)
+        return NULL;
     PyModule_AddObject(m, "uname_result", (PyObject *)&UnameResultType);
 
 #ifdef __APPLE__
@@ -11648,7 +11657,6 @@
     initialized = 1;
 
     return m;
-
 }
 
 #ifdef __cplusplus
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index d4ef733..9909400 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -216,8 +216,9 @@
         return NULL;
 
     if (!initialized) {
-        PyStructSequence_InitType(&StructPwdType,
-                                  &struct_pwd_type_desc);
+        if (PyStructSequence_InitType2(&StructPwdType,
+                                       &struct_pwd_type_desc) < 0)
+            return NULL;
         initialized = 1;
     }
     Py_INCREF((PyObject *) &StructPwdType);
diff --git a/Modules/resource.c b/Modules/resource.c
index 4411225..8768315 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -263,9 +263,12 @@
     /* Add some symbolic constants to the module */
     Py_INCREF(PyExc_OSError);
     PyModule_AddObject(m, "error", PyExc_OSError);
-    if (!initialized)
-        PyStructSequence_InitType(&StructRUsageType,
-                                  &struct_rusage_desc);
+    if (!initialized) {
+        if (PyStructSequence_InitType2(&StructRUsageType,
+                                       &struct_rusage_desc) < 0)
+            return NULL;
+    }
+
     Py_INCREF(&StructRUsageType);
     PyModule_AddObject(m, "struct_rusage",
                        (PyObject*) &StructRUsageType);
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index 122530b..0810633 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -978,9 +978,10 @@
         return NULL;
 
 #if defined(HAVE_SIGWAITINFO) || defined(HAVE_SIGTIMEDWAIT)
-    if (!initialized)
-        PyStructSequence_InitType(&SiginfoType, &struct_siginfo_desc);
-
+    if (!initialized) {
+        if (PyStructSequence_InitType2(&SiginfoType, &struct_siginfo_desc) < 0)
+            return NULL;
+    }
     Py_INCREF((PyObject*) &SiginfoType);
     PyModule_AddObject(m, "struct_siginfo", (PyObject*) &SiginfoType);
     initialized = 1;
diff --git a/Modules/spwdmodule.c b/Modules/spwdmodule.c
index 194ae19..d06f8ce 100644
--- a/Modules/spwdmodule.c
+++ b/Modules/spwdmodule.c
@@ -196,9 +196,11 @@
     m=PyModule_Create(&spwdmodule);
     if (m == NULL)
         return NULL;
-    if (!initialized)
-        PyStructSequence_InitType(&StructSpwdType,
-                                  &struct_spwd_type_desc);
+    if (!initialized) {
+        if (PyStructSequence_InitType2(&StructSpwdType,
+                                       &struct_spwd_type_desc) < 0)
+            return NULL;
+    }
     Py_INCREF((PyObject *) &StructSpwdType);
     PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
     initialized = 1;
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 2c3341c..8d161d4 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -1476,8 +1476,9 @@
     PyInit_timezone(m);
 
     if (!initialized) {
-        PyStructSequence_InitType(&StructTimeType,
-                                  &struct_time_type_desc);
+        if (PyStructSequence_InitType2(&StructTimeType,
+                                       &struct_time_type_desc) < 0)
+            return NULL;
 
 #ifdef MS_WINDOWS
         winver.dwOSVersionInfoSize = sizeof(winver);