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/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;
 }