From 40b33c648a2d777636603356c12b644dd4c92876 Mon Sep 17 00:00:00 2001
From: Guido van Rossum <guido@python.org>
Date: Sat, 2 Aug 1997 03:07:46 +0000
Subject: 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.
---
 Python/modsupport.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/Python/modsupport.c b/Python/modsupport.c
index df5ebfa..854439b 100644
--- a/Python/modsupport.c
+++ b/Python/modsupport.c
@@ -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;
-- 
cgit v0.12

