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.
This commit is contained in:
Fred Drake 2002-03-12 21:49:44 +00:00
parent 193a3f6d37
commit 1de5a722d6
2 changed files with 12 additions and 12 deletions

View File

@ -231,7 +231,11 @@ initxx(void)
m = Py_InitModule("xx", xx_methods); m = Py_InitModule("xx", xx_methods);
/* Add some symbolic constants to the module */ /* Add some symbolic constants to the module */
d = PyModule_GetDict(m); if (ErrorObject == NULL) {
ErrorObject = PyErr_NewException("xx.error", NULL, NULL); ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
PyDict_SetItemString(d, "error", ErrorObject); if (ErrorObject == NULL)
return;
}
Py_INCREF(ErrorObject);
PyModule_AddObject(d, "error", ErrorObject);
} }

View File

@ -238,7 +238,7 @@ static PyMethodDef xxsubtype_functions[] = {
DL_EXPORT(void) DL_EXPORT(void)
initxxsubtype(void) initxxsubtype(void)
{ {
PyObject *m, *d; PyObject *m;
/* Fill in deferred data addresses. This must be done before /* Fill in deferred data addresses. This must be done before
PyType_Ready() is called. Note that PyType_Ready() automatically PyType_Ready() is called. Note that PyType_Ready() automatically
@ -263,17 +263,13 @@ initxxsubtype(void)
if (PyType_Ready(&spamdict_type) < 0) if (PyType_Ready(&spamdict_type) < 0)
return; return;
d = PyModule_GetDict(m);
if (d == NULL)
return;
Py_INCREF(&spamlist_type); Py_INCREF(&spamlist_type);
if (PyDict_SetItemString(d, "spamlist", if (PyModule_AddObject(m, "spamlist",
(PyObject *) &spamlist_type) < 0) (PyObject *) &spamlist_type) < 0)
return; return;
Py_INCREF(&spamdict_type); Py_INCREF(&spamdict_type);
if (PyDict_SetItemString(d, "spamdict", if (PyModule_AddObject(m, "spamdict",
(PyObject *) &spamdict_type) < 0) (PyObject *) &spamdict_type) < 0)
return; return;
} }