]> granicus.if.org Git - python/commitdiff
Removed fatal errors from Py_Initmodule4() (and thus from
authorGuido van Rossum <guido@python.org>
Sat, 2 Aug 1997 03:07:46 +0000 (03:07 +0000)
committerGuido van Rossum <guido@python.org>
Sat, 2 Aug 1997 03:07:46 +0000 (03:07 +0000)
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.

Python/modsupport.c

index df5ebfaf1b5741918a49644c2167258ad2b3e864..854439bb94e09de1225c03e4674b3b127b6aa576 100644 (file)
@@ -39,13 +39,17 @@ typedef extended va_double;
 typedef double va_double;
 #endif
 
-/* initmodule4() parameters:
+/* Py_InitModule4() parameters:
    - name is the module name
    - methods is the list of top-level functions
    - doc is the documentation string
    - passthrough is passed as self to functions defined in the module
    - api_version is the value of PYTHON_API_VERSION at the time the
      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[] =
@@ -65,25 +69,21 @@ Py_InitModule4(name, methods, doc, passthrough, module_api_version)
        if (module_api_version != PYTHON_API_VERSION)
                fprintf(stderr, api_version_warning,
                        name, PYTHON_API_VERSION, name, module_api_version);
-       if ((m = PyImport_AddModule(name)) == NULL) {
-               fprintf(stderr, "initializing module: %s\n", name);
-               Py_FatalError("can't create a module");
-       }
+       if ((m = PyImport_AddModule(name)) == NULL)
+               return NULL;
        d = PyModule_GetDict(m);
        for (ml = methods; ml->ml_name != NULL; ml++) {
                v = PyCFunction_New(ml, passthrough);
-               if (v == NULL ||
-                   PyDict_SetItemString(d, ml->ml_name, v) != 0)
-               {
-                       fprintf(stderr, "initializing module: %s\n", name);
-                       Py_FatalError("can't initialize module");
-               }
+               if (v == NULL)
+                       return NULL;
+               if (PyDict_SetItemString(d, ml->ml_name, v) != 0)
+                       return NULL;
                Py_DECREF(v);
        }
        if (doc != NULL) {
                v = PyString_FromString(doc);
                if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0)
-                       Py_FatalError("can't add doc string");
+                       return NULL;
                Py_DECREF(v);
        }
        return m;