@@ -217,12 +217,13 @@ \section{Intermezzo: Errors and Exceptions
217217void
218218initspam(void)
219219{
220- PyObject *m, *d ;
220+ PyObject *m;
221221
222222 m = Py_InitModule("spam", SpamMethods);
223- d = PyModule_GetDict(m);
223+
224224 SpamError = PyErr_NewException("spam.error", NULL, NULL);
225- PyDict_SetItemString(d, "error", SpamError);
225+ Py_INCREF(SpamError);
226+ PyModule_AddObject(m, "error", SpamError);
226227}
227228\end {verbatim }
228229
@@ -1277,13 +1278,8 @@ \section{Providing a C API for an Extension Module
12771278 /* Create a CObject containing the API pointer array's address */
12781279 c_api_object = PyCObject_FromVoidPtr((void *)PySpam_API, NULL);
12791280
1280- if (c_api_object != NULL) {
1281- /* Create a name for this object in the module's namespace */
1282- PyObject *d = PyModule_GetDict(m);
1283-
1284- PyDict_SetItemString(d, "_C_API", c_api_object);
1285- Py_DECREF(c_api_object);
1286- }
1281+ if (c_api_object != NULL)
1282+ PyModule_AddObject(m, "_C_API", c_api_object);
12871283}
12881284\end {verbatim }
12891285
@@ -1324,16 +1320,21 @@ \section{Providing a C API for an Extension Module
13241320#define PySpam_System \
13251321 (*(PySpam_System_RETURN (*)PySpam_System_PROTO) PySpam_API[PySpam_System_NUM])
13261322
1327- #define import_spam() \
1328- { \
1329- PyObject *module = PyImport_ImportModule("spam"); \
1330- if (module != NULL) { \
1331- PyObject *module_dict = PyModule_GetDict(module); \
1332- PyObject *c_api_object = PyDict_GetItemString(module_dict, "_C_API"); \
1333- if (PyCObject_Check(c_api_object)) { \
1334- PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object); \
1335- } \
1336- } \
1323+ /* Return -1 and set exception on error, 0 on success. */
1324+ static int
1325+ import_spam(void)
1326+ {
1327+ PyObject *module = PyImport_ImportModule("spam");
1328+
1329+ if (module != NULL) {
1330+ PyObject *c_api_object = PyObject_GetAttrString(module, "_C_API");
1331+ if (c_api_object == NULL)
1332+ return -1;
1333+ if (PyCObject_Check(c_api_object))
1334+ PySpam_API = (void **)PyCObject_AsVoidPtr(c_api_object);
1335+ Py_DECREF(c_api_object);
1336+ }
1337+ return 0;
13371338}
13381339
13391340#endif
@@ -1357,7 +1358,9 @@ \section{Providing a C API for an Extension Module
13571358 PyObject *m;
13581359
13591360 Py_InitModule("client", ClientMethods);
1360- import_spam();
1361+ if (import_spam() < 0)
1362+ return;
1363+ /* additional initialization can happen here */
13611364}
13621365\end {verbatim }
13631366
@@ -1370,6 +1373,7 @@ \section{Providing a C API for an Extension Module
13701373functionality, which is especially useful for memory allocation and
13711374deallocation of the pointer stored in a CObject. The details
13721375are described in the \citetitle [../api/api.html]{Python /C API
1373- Reference Manual } in the section `` CObjects'' and in the
1374- implementation of CObjects (files \file {Include/cobject.h} and
1376+ Reference Manual } in the section
1377+ `` \ulink {CObjects}{../api/cObjects.html}'' and in the implementation
1378+ of CObjects (files \file {Include/cobject.h} and
13751379\file {Objects/cobject.c} in the Python source code distribution).
0 commit comments