]> granicus.if.org Git - python/commitdiff
Change the example code to prefer PyModule_Add*() instead of using the
authorFred Drake <fdrake@acm.org>
Tue, 12 Mar 2002 21:49:44 +0000 (21:49 +0000)
committerFred Drake <fdrake@acm.org>
Tue, 12 Mar 2002 21:49:44 +0000 (21:49 +0000)
module dictionary directly.  Also, be more careful about not re-initializing
globals in the event of re-initialization of a C extension.

Modules/xxmodule.c
Modules/xxsubtype.c

index 0ee9f7a8baad72035dc5cfceee5d2105f4f82b8c..3587c91eebea91a1b76cbbf9c77dfbb5ee793f78 100644 (file)
@@ -231,7 +231,11 @@ initxx(void)
        m = Py_InitModule("xx", xx_methods);
 
        /* Add some symbolic constants to the module */
-       d = PyModule_GetDict(m);
-       ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
-       PyDict_SetItemString(d, "error", ErrorObject);
+       if (ErrorObject == NULL) {
+               ErrorObject = PyErr_NewException("xx.error", NULL, NULL);
+               if (ErrorObject == NULL)
+                       return;
+       }
+       Py_INCREF(ErrorObject);
+       PyModule_AddObject(d, "error", ErrorObject);
 }
index f3d8e89ed74e10cf4bc965a786fe75b7826e8c62..3b91a0c598fcad60b2c933de3c3bbdf2558ae09b 100644 (file)
@@ -238,7 +238,7 @@ static PyMethodDef xxsubtype_functions[] = {
 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 @@ initxxsubtype(void)
        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;
 }