Change the example code to prefer PyModule_Add*() instead of using the
module dictionary directly. Also, be more careful about not re-initializing
globals in the event of re-initialization of a C extension.
diff --git a/Modules/xxsubtype.c b/Modules/xxsubtype.c
index f3d8e89..3b91a0c 100644
--- a/Modules/xxsubtype.c
+++ b/Modules/xxsubtype.c
@@ -238,7 +238,7 @@
DL_EXPORT(void)
initxxsubtype(void)
{
- PyObject *m, *d;
+ PyObject *m;
/* Fill in deferred data addresses. This must be done before
PyType_Ready() is called. Note that PyType_Ready() automatically
@@ -263,17 +263,13 @@
if (PyType_Ready(&spamdict_type) < 0)
return;
- d = PyModule_GetDict(m);
- if (d == NULL)
- return;
-
Py_INCREF(&spamlist_type);
- if (PyDict_SetItemString(d, "spamlist",
- (PyObject *) &spamlist_type) < 0)
+ if (PyModule_AddObject(m, "spamlist",
+ (PyObject *) &spamlist_type) < 0)
return;
Py_INCREF(&spamdict_type);
- if (PyDict_SetItemString(d, "spamdict",
- (PyObject *) &spamdict_type) < 0)
+ if (PyModule_AddObject(m, "spamdict",
+ (PyObject *) &spamdict_type) < 0)
return;
}