Removed fatal errors from Py_Initmodule4() (and thus from

Py_Initmodule(), which is a macro wrapper around it).

The return value is now a NULL pointer if the initialization failed.
This may make old modules fail with a SEGFAULT, since they don't
expect this kind of failure.  That's OK, since (a) it "never" happens,
and (b) they would fail with a fatal error otherwise, anyway.

Tons of extension modules should now check the return value of
Py_Initmodule*() -- that's on my TODO list.
This commit is contained in:
Guido van Rossum 1997-08-02 03:07:46 +00:00
parent aee094cc60
commit 40b33c648a
1 changed files with 12 additions and 12 deletions

View File

@ -39,13 +39,17 @@ typedef extended va_double;
typedef double va_double; typedef double va_double;
#endif #endif
/* initmodule4() parameters: /* Py_InitModule4() parameters:
- name is the module name - name is the module name
- methods is the list of top-level functions - methods is the list of top-level functions
- doc is the documentation string - doc is the documentation string
- passthrough is passed as self to functions defined in the module - passthrough is passed as self to functions defined in the module
- api_version is the value of PYTHON_API_VERSION at the time the - api_version is the value of PYTHON_API_VERSION at the time the
module was compiled module was compiled
Return value is a borrowed reference to the module object; or NULL
if an error occurred (in Python 1.4 and before, errors were fatal).
Errors may still leak memory.
*/ */
static char api_version_warning[] = static char api_version_warning[] =
@ -65,25 +69,21 @@ Py_InitModule4(name, methods, doc, passthrough, module_api_version)
if (module_api_version != PYTHON_API_VERSION) if (module_api_version != PYTHON_API_VERSION)
fprintf(stderr, api_version_warning, fprintf(stderr, api_version_warning,
name, PYTHON_API_VERSION, name, module_api_version); name, PYTHON_API_VERSION, name, module_api_version);
if ((m = PyImport_AddModule(name)) == NULL) { if ((m = PyImport_AddModule(name)) == NULL)
fprintf(stderr, "initializing module: %s\n", name); return NULL;
Py_FatalError("can't create a module");
}
d = PyModule_GetDict(m); d = PyModule_GetDict(m);
for (ml = methods; ml->ml_name != NULL; ml++) { for (ml = methods; ml->ml_name != NULL; ml++) {
v = PyCFunction_New(ml, passthrough); v = PyCFunction_New(ml, passthrough);
if (v == NULL || if (v == NULL)
PyDict_SetItemString(d, ml->ml_name, v) != 0) return NULL;
{ if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
fprintf(stderr, "initializing module: %s\n", name); return NULL;
Py_FatalError("can't initialize module");
}
Py_DECREF(v); Py_DECREF(v);
} }
if (doc != NULL) { if (doc != NULL) {
v = PyString_FromString(doc); v = PyString_FromString(doc);
if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
Py_FatalError("can't add doc string"); return NULL;
Py_DECREF(v); Py_DECREF(v);
} }
return m; return m;