From 1ac754fa10f5d199d19911e21185d0970cb3073f Mon Sep 17 00:00:00 2001
From: Neal Norwitz <nnorwitz@gmail.com>
Date: Thu, 19 Jan 2006 06:09:39 +0000
Subject: [PATCH] Check return result from Py_InitModule*().  This API can
 fail.

Probably should be backported.
---
 Modules/_bsddb.c            | 2 ++
 Modules/_curses_panel.c     | 2 ++
 Modules/_cursesmodule.c     | 2 ++
 Modules/_elementtree.c      | 2 ++
 Modules/_heapqmodule.c      | 2 ++
 Modules/_localemodule.c     | 2 ++
 Modules/_randommodule.c     | 2 ++
 Modules/_sre.c              | 2 ++
 Modules/_ssl.c              | 2 ++
 Modules/_testcapimodule.c   | 2 ++
 Modules/_tkinter.c          | 2 ++
 Modules/almodule.c          | 2 ++
 Modules/arraymodule.c       | 2 ++
 Modules/audioop.c           | 2 ++
 Modules/binascii.c          | 2 ++
 Modules/bsddbmodule.c       | 2 ++
 Modules/bz2module.c         | 2 ++
 Modules/cPickle.c           | 2 ++
 Modules/cStringIO.c         | 1 +
 Modules/cdmodule.c          | 2 ++
 Modules/clmodule.c          | 2 ++
 Modules/cmathmodule.c       | 2 ++
 Modules/collectionsmodule.c | 2 ++
 Modules/datetimemodule.c    | 2 ++
 Modules/dbmmodule.c         | 2 ++
 Modules/dlmodule.c          | 2 ++
 Modules/errnomodule.c       | 2 ++
 Modules/fcntlmodule.c       | 2 ++
 Modules/flmodule.c          | 2 ++
 Modules/fmmodule.c          | 2 ++
 Modules/fpectlmodule.c      | 2 ++
 Modules/fpetestmodule.c     | 2 ++
 Modules/functionalmodule.c  | 2 ++
 Modules/gcmodule.c          | 2 ++
 Modules/gdbmmodule.c        | 2 ++
 Modules/grpmodule.c         | 2 ++
 Modules/imageop.c           | 2 ++
 Modules/imgfile.c           | 2 ++
 Modules/itertoolsmodule.c   | 2 ++
 Modules/linuxaudiodev.c     | 2 ++
 Modules/mathmodule.c        | 2 ++
 Modules/md5module.c         | 2 ++
 Modules/mmapmodule.c        | 2 ++
 Modules/nismodule.c         | 2 ++
 Modules/operator.c          | 2 ++
 Modules/ossaudiodev.c       | 2 ++
 Modules/parsermodule.c      | 2 ++
 Modules/posixmodule.c       | 2 ++
 Modules/puremodule.c        | 2 ++
 Modules/pwdmodule.c         | 2 ++
 Modules/pyexpat.c           | 2 ++
 Modules/readline.c          | 2 ++
 Modules/regexmodule.c       | 2 ++
 Modules/resource.c          | 2 ++
 Modules/rgbimgmodule.c      | 2 ++
 Modules/selectmodule.c      | 2 ++
 Modules/sha256module.c      | 2 ++
 Modules/sha512module.c      | 2 ++
 Modules/shamodule.c         | 2 ++
 Modules/signalmodule.c      | 2 ++
 Modules/socketmodule.c      | 2 ++
 Modules/spwdmodule.c        | 2 ++
 Modules/stropmodule.c       | 2 ++
 Modules/structmodule.c      | 2 ++
 Modules/sunaudiodev.c       | 2 ++
 Modules/svmodule.c          | 2 ++
 Modules/symtablemodule.c    | 2 ++
 Modules/syslogmodule.c      | 2 ++
 Modules/termios.c           | 2 ++
 Modules/threadmodule.c      | 2 ++
 Modules/timemodule.c        | 2 ++
 Modules/xxmodule.c          | 2 ++
 Modules/zlibmodule.c        | 2 ++
 PC/_subprocess.c            | 2 ++
 PC/_winreg.c                | 2 ++
 PC/msvcrtmodule.c           | 2 ++
 PC/winsound.c               | 2 ++
 Python/import.c             | 2 ++
 Python/marshal.c            | 2 ++
 Python/sysmodule.c          | 2 ++
 80 files changed, 159 insertions(+)

diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c
index 6f4da7ee61..aa4a1617cb 100644
--- a/Modules/_bsddb.c
+++ b/Modules/_bsddb.c
@@ -5034,6 +5034,8 @@ DL_EXPORT(void) init_bsddb(void)
 
     /* Create the module and add the functions */
     m = Py_InitModule(_bsddbModuleName, bsddb_methods);
+    if (m == NULL)
+    	return;
 
     /* Add some symbolic constants to the module */
     d = PyModule_GetDict(m);
diff --git a/Modules/_curses_panel.c b/Modules/_curses_panel.c
index b5f30cb85e..c3f313a334 100644
--- a/Modules/_curses_panel.c
+++ b/Modules/_curses_panel.c
@@ -462,6 +462,8 @@ init_curses_panel(void)
 
     /* Create the module and add the functions */
     m = Py_InitModule("_curses_panel", PyCurses_methods);
+    if (m == NULL)
+    	return;
     d = PyModule_GetDict(m);
 
     /* For exception _curses_panel.error */
diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
index 176f024d29..4c03602d56 100644
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -2481,6 +2481,8 @@ init_curses(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule("_curses", PyCurses_methods);
+	if (m == NULL)
+    		return;
 
 	/* Add some symbolic constants to the module */
 	d = PyModule_GetDict(m);
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 641f2728e0..ea5aa6cf08 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2590,6 +2590,8 @@ init_elementtree(void)
 #endif
 
     m = Py_InitModule("_elementtree", _functions);
+    if (m == NULL)
+    	return;
 
     /* python glue code */
 
diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c
index 5a78c453e1..999647e11a 100644
--- a/Modules/_heapqmodule.c
+++ b/Modules/_heapqmodule.c
@@ -610,6 +610,8 @@ init_heapq(void)
 	PyObject *m;
 
 	m = Py_InitModule3("_heapq", heapq_methods, module_doc);
+	if (m == NULL)
+    		return;
 	PyModule_AddObject(m, "__about__", PyString_FromString(__about__));
 }
 
diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
index bd57c2ff94..2d84d80ae7 100644
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -715,6 +715,8 @@ init_locale(void)
 #endif
 
     m = Py_InitModule("_locale", PyLocale_Methods);
+    if (m == NULL)
+    	return;
 
     d = PyModule_GetDict(m);
 
diff --git a/Modules/_randommodule.c b/Modules/_randommodule.c
index 8fe2b2bc99..bd1c9d39b1 100644
--- a/Modules/_randommodule.c
+++ b/Modules/_randommodule.c
@@ -573,6 +573,8 @@ init_random(void)
 	if (PyType_Ready(&Random_Type) < 0)
 		return;
 	m = Py_InitModule3("_random", NULL, module_doc);
+	if (m == NULL)
+		return;
 	Py_INCREF(&Random_Type);
 	PyModule_AddObject(m, "Random", (PyObject *)&Random_Type);
 }
diff --git a/Modules/_sre.c b/Modules/_sre.c
index 1f0a8bc939..4d9d1cd773 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -3389,6 +3389,8 @@ PyMODINIT_FUNC init_sre(void)
         Scanner_Type.ob_type = &PyType_Type;
 
     m = Py_InitModule("_" SRE_MODULE, _functions);
+    if (m == NULL)
+    	return;
     d = PyModule_GetDict(m);
 
     x = PyInt_FromLong(SRE_MAGIC);
diff --git a/Modules/_ssl.c b/Modules/_ssl.c
index 9c100abfcd..fd5e2c6295 100644
--- a/Modules/_ssl.c
+++ b/Modules/_ssl.c
@@ -634,6 +634,8 @@ init_ssl(void)
 	PySSL_Type.ob_type = &PyType_Type;
 
 	m = Py_InitModule3("_ssl", PySSL_methods, module_doc);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 
 	/* Load _socket module and its C API */
diff --git a/Modules/_testcapimodule.c b/Modules/_testcapimodule.c
index 9a5d885434..c008b874b3 100644
--- a/Modules/_testcapimodule.c
+++ b/Modules/_testcapimodule.c
@@ -627,6 +627,8 @@ init_testcapi(void)
 	PyObject *m;
 
 	m = Py_InitModule("_testcapi", TestMethods);
+	if (m == NULL)
+		return;
 
 	PyModule_AddObject(m, "UCHAR_MAX", PyInt_FromLong(UCHAR_MAX));
 	PyModule_AddObject(m, "USHRT_MAX", PyInt_FromLong(USHRT_MAX));
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index c3015b9f8a..70cd670b12 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -3088,6 +3088,8 @@ init_tkinter(void)
 #endif
 
 	m = Py_InitModule("_tkinter", moduleMethods);
+	if (m == NULL)
+		return;
 
 	d = PyModule_GetDict(m);
 	Tkinter_TclError = PyErr_NewException("_tkinter.TclError", NULL, NULL);
diff --git a/Modules/almodule.c b/Modules/almodule.c
index 12b265e90e..5254fca18e 100644
--- a/Modules/almodule.c
+++ b/Modules/almodule.c
@@ -1996,6 +1996,8 @@ inital(void)
 	m = Py_InitModule4("al", al_methods,
 		al_module_documentation,
 		(PyObject*)NULL,PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	d = PyModule_GetDict(m);
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 3c60350673..4c7cdf2197 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -2118,6 +2118,8 @@ initarray(void)
 	Arraytype.ob_type = &PyType_Type;
 	PyArrayIter_Type.ob_type = &PyType_Type;
 	m = Py_InitModule3("array", a_methods, module_doc);
+	if (m == NULL)
+		return;
 
         Py_INCREF((PyObject *)&Arraytype);
 	PyModule_AddObject(m, "ArrayType", (PyObject *)&Arraytype);
diff --git a/Modules/audioop.c b/Modules/audioop.c
index 52824b84b6..8d5a305525 100644
--- a/Modules/audioop.c
+++ b/Modules/audioop.c
@@ -1374,6 +1374,8 @@ initaudioop(void)
 {
 	PyObject *m, *d;
 	m = Py_InitModule("audioop", audioop_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	AudioopError = PyErr_NewException("audioop.error", NULL, NULL);
 	if (AudioopError != NULL)
diff --git a/Modules/binascii.c b/Modules/binascii.c
index eab90b3b74..4a2c268f2b 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -1335,6 +1335,8 @@ initbinascii(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule("binascii", binascii_module_methods);
+	if (m == NULL)
+		return;
 
 	d = PyModule_GetDict(m);
 	x = PyString_FromString(doc_binascii);
diff --git a/Modules/bsddbmodule.c b/Modules/bsddbmodule.c
index ac8c44395f..6bdffde7b2 100644
--- a/Modules/bsddbmodule.c
+++ b/Modules/bsddbmodule.c
@@ -849,6 +849,8 @@ initbsddb185(void) {
 
 	Bsddbtype.ob_type = &PyType_Type;
 	m = Py_InitModule("bsddb185", bsddbmodule_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	BsddbError = PyErr_NewException("bsddb.error", NULL, NULL);
 	if (BsddbError != NULL)
diff --git a/Modules/bz2module.c b/Modules/bz2module.c
index 82b3958b8f..9f30f8a988 100644
--- a/Modules/bz2module.c
+++ b/Modules/bz2module.c
@@ -2192,6 +2192,8 @@ initbz2(void)
 	BZ2Decomp_Type.ob_type = &PyType_Type;
 
 	m = Py_InitModule3("bz2", bz2_methods, bz2__doc__);
+	if (m == NULL)
+		return;
 
 	PyModule_AddObject(m, "__author__", PyString_FromString(__author__));
 
diff --git a/Modules/cPickle.c b/Modules/cPickle.c
index 865541ff5c..cc821fdf46 100644
--- a/Modules/cPickle.c
+++ b/Modules/cPickle.c
@@ -5730,6 +5730,8 @@ initcPickle(void)
 	m = Py_InitModule4("cPickle", cPickle_methods,
 			   cPickle_module_documentation,
 			   (PyObject*)NULL,PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	d = PyModule_GetDict(m);
diff --git a/Modules/cStringIO.c b/Modules/cStringIO.c
index 0d50459166..ad2f36b9b3 100644
--- a/Modules/cStringIO.c
+++ b/Modules/cStringIO.c
@@ -716,6 +716,7 @@ initcStringIO(void) {
   m = Py_InitModule4("cStringIO", IO_methods,
 		     cStringIO_module_documentation,
 		     (PyObject*)NULL,PYTHON_API_VERSION);
+  if (m == NULL) return;
 
   /* Add some symbolic constants to the module */
   d = PyModule_GetDict(m);
diff --git a/Modules/cdmodule.c b/Modules/cdmodule.c
index 25add3e1b6..f8efd395a1 100644
--- a/Modules/cdmodule.c
+++ b/Modules/cdmodule.c
@@ -760,6 +760,8 @@ initcd(void)
 	PyObject *m, *d;
 
 	m = Py_InitModule("cd", CD_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 
 	CdError = PyErr_NewException("cd.error", NULL, NULL);
diff --git a/Modules/clmodule.c b/Modules/clmodule.c
index 70e8f8e7d9..a535e031f5 100644
--- a/Modules/clmodule.c
+++ b/Modules/clmodule.c
@@ -963,6 +963,8 @@ initcl(void)
 	PyObject *m, *d, *x;
 
 	m = Py_InitModule("cl", cl_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 
 	ClError = PyErr_NewException("cl.error", NULL, NULL);
diff --git a/Modules/cmathmodule.c b/Modules/cmathmodule.c
index 78b9dd5f8f..ec48ce8d72 100644
--- a/Modules/cmathmodule.c
+++ b/Modules/cmathmodule.c
@@ -417,6 +417,8 @@ initcmath(void)
 	PyObject *m;
 
 	m = Py_InitModule3("cmath", cmath_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	PyModule_AddObject(m, "pi",
                            PyFloat_FromDouble(atan(1.0) * 4.0));
diff --git a/Modules/collectionsmodule.c b/Modules/collectionsmodule.c
index 1a8258ef7c..16f25dfaae 100644
--- a/Modules/collectionsmodule.c
+++ b/Modules/collectionsmodule.c
@@ -1077,6 +1077,8 @@ initcollections(void)
 	PyObject *m;
 
 	m = Py_InitModule3("collections", NULL, module_doc);
+	if (m == NULL)
+		return;
 
 	if (PyType_Ready(&deque_type) < 0)
 		return;
diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c
index 8a6fae2bc1..50f47d45f4 100644
--- a/Modules/datetimemodule.c
+++ b/Modules/datetimemodule.c
@@ -4615,6 +4615,8 @@ initdatetime(void)
 
 	m = Py_InitModule3("datetime", module_methods,
 			   "Fast implementation of the datetime type.");
+	if (m == NULL)
+		return;
 
 	if (PyType_Ready(&PyDateTime_DateType) < 0)
 		return;
diff --git a/Modules/dbmmodule.c b/Modules/dbmmodule.c
index 40d06fc268..cc963a2dfd 100644
--- a/Modules/dbmmodule.c
+++ b/Modules/dbmmodule.c
@@ -359,6 +359,8 @@ initdbm(void) {
 
 	Dbmtype.ob_type = &PyType_Type;
 	m = Py_InitModule("dbm", dbmmodule_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	if (DbmError == NULL)
 		DbmError = PyErr_NewException("dbm.error", NULL, NULL);
diff --git a/Modules/dlmodule.c b/Modules/dlmodule.c
index 927f4c0ac8..09556814f2 100644
--- a/Modules/dlmodule.c
+++ b/Modules/dlmodule.c
@@ -219,6 +219,8 @@ initdl(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule("dl", dl_methods);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	d = PyModule_GetDict(m);
diff --git a/Modules/errnomodule.c b/Modules/errnomodule.c
index e9c0990c28..696d396b07 100644
--- a/Modules/errnomodule.c
+++ b/Modules/errnomodule.c
@@ -57,6 +57,8 @@ initerrno(void)
 {
 	PyObject *m, *d, *de;
 	m = Py_InitModule3("errno", errno_methods, errno__doc__);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	de = PyDict_New();
 	if (!d || !de || PyDict_SetItemString(d, "errorcode", de) < 0)
diff --git a/Modules/fcntlmodule.c b/Modules/fcntlmodule.c
index 624019dfa5..4197339e2f 100644
--- a/Modules/fcntlmodule.c
+++ b/Modules/fcntlmodule.c
@@ -583,6 +583,8 @@ initfcntl(void)
 
 	/* Create the module and add the functions and documentation */
 	m = Py_InitModule3("fcntl", fcntl_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	d = PyModule_GetDict(m);
diff --git a/Modules/flmodule.c b/Modules/flmodule.c
index 1ae2dc85bb..aa0d04e405 100644
--- a/Modules/flmodule.c
+++ b/Modules/flmodule.c
@@ -2130,6 +2130,8 @@ PyMODINIT_FUNC
 initfl(void)
 {
 	Py_InitModule("fl", forms_methods);
+	if (m == NULL)
+		return;
 	foreground();
 	fl_init();
 }
diff --git a/Modules/fmmodule.c b/Modules/fmmodule.c
index 78a58772c2..0175390911 100644
--- a/Modules/fmmodule.c
+++ b/Modules/fmmodule.c
@@ -258,5 +258,7 @@ void
 initfm(void)
 {
 	Py_InitModule("fm", fm_methods);
+	if (m == NULL)
+		return;
 	fminit();
 }
diff --git a/Modules/fpectlmodule.c b/Modules/fpectlmodule.c
index 241c1c2620..c6d4f77c23 100644
--- a/Modules/fpectlmodule.c
+++ b/Modules/fpectlmodule.c
@@ -265,6 +265,8 @@ PyMODINIT_FUNC initfpectl(void)
 {
     PyObject *m, *d;
     m = Py_InitModule("fpectl", fpectl_methods);
+    if (m == NULL)
+    	return;
     d = PyModule_GetDict(m);
     fpe_error = PyErr_NewException("fpectl.error", NULL, NULL);
     if (fpe_error != NULL)
diff --git a/Modules/fpetestmodule.c b/Modules/fpetestmodule.c
index aa14dd82a6..22e95dbaef 100644
--- a/Modules/fpetestmodule.c
+++ b/Modules/fpetestmodule.c
@@ -177,6 +177,8 @@ PyMODINIT_FUNC initfpetest(void)
     PyObject *m, *d;
 
     m = Py_InitModule("fpetest", fpetest_methods);
+    if (m == NULL)
+    	return;
     d = PyModule_GetDict(m);
     fpe_error = PyErr_NewException("fpetest.error", NULL, NULL);
     if (fpe_error != NULL)
diff --git a/Modules/functionalmodule.c b/Modules/functionalmodule.c
index 95ffd5dde9..58b07d9e19 100644
--- a/Modules/functionalmodule.c
+++ b/Modules/functionalmodule.c
@@ -263,6 +263,8 @@ initfunctional(void)
 	};
 
 	m = Py_InitModule3("functional", module_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	for (i=0 ; typelist[i] != NULL ; i++) {
 		if (PyType_Ready(typelist[i]) < 0)
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index db9dd3268c..00239bd0c4 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1158,6 +1158,8 @@ initgc(void)
 			      gc__doc__,
 			      NULL,
 			      PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	if (garbage == NULL) {
 		garbage = PyList_New(0);
diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c
index 03e664d6bc..6045743df3 100644
--- a/Modules/gdbmmodule.c
+++ b/Modules/gdbmmodule.c
@@ -512,6 +512,8 @@ initgdbm(void) {
     m = Py_InitModule4("gdbm", dbmmodule_methods,
                        gdbmmodule__doc__, (PyObject *)NULL,
                        PYTHON_API_VERSION);
+    if (m == NULL)
+	return;
     d = PyModule_GetDict(m);
     DbmError = PyErr_NewException("gdbm.error", NULL, NULL);
     if (DbmError != NULL) {
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index 5f33fe972f..de849c98bd 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -171,6 +171,8 @@ initgrp(void)
 {
     PyObject *m, *d;
     m = Py_InitModule3("grp", grp_methods, grp__doc__);
+    if (m == NULL)
+        return;
     d = PyModule_GetDict(m);
     PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc);
     PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType);
diff --git a/Modules/imageop.c b/Modules/imageop.c
index 5b87898bbb..92f805a83b 100644
--- a/Modules/imageop.c
+++ b/Modules/imageop.c
@@ -776,6 +776,8 @@ initimageop(void)
 {
 	PyObject *m;
 	m = Py_InitModule("imageop", imageop_methods);
+	if (m == NULL)
+		return;
 	ImageopDict = PyModule_GetDict(m);
 	ImageopError = PyErr_NewException("imageop.error", NULL, NULL);
 	if (ImageopError != NULL)
diff --git a/Modules/imgfile.c b/Modules/imgfile.c
index f9fa25bc4e..bb85a78d5c 100644
--- a/Modules/imgfile.c
+++ b/Modules/imgfile.c
@@ -492,6 +492,8 @@ initimgfile(void)
 {
 	PyObject *m, *d;
 	m = Py_InitModule("imgfile", imgfile_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	ImgfileError = PyErr_NewException("imgfile.error", NULL, NULL);
 	if (ImgfileError != NULL)
diff --git a/Modules/itertoolsmodule.c b/Modules/itertoolsmodule.c
index c675bf4b30..a7ecf93e9b 100644
--- a/Modules/itertoolsmodule.c
+++ b/Modules/itertoolsmodule.c
@@ -2460,6 +2460,8 @@ inititertools(void)
 
 	teedataobject_type.ob_type = &PyType_Type;
 	m = Py_InitModule3("itertools", module_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	for (i=0 ; typelist[i] != NULL ; i++) {
 		if (PyType_Ready(typelist[i]) < 0)
diff --git a/Modules/linuxaudiodev.c b/Modules/linuxaudiodev.c
index a5ff367bfe..f6d5b7d44b 100644
--- a/Modules/linuxaudiodev.c
+++ b/Modules/linuxaudiodev.c
@@ -491,6 +491,8 @@ initlinuxaudiodev(void)
     PyObject *m;
   
     m = Py_InitModule("linuxaudiodev", linuxaudiodev_methods);
+    if (m == NULL)
+	return;
 
     LinuxAudioError = PyErr_NewException("linuxaudiodev.error", NULL, NULL);
     if (LinuxAudioError)
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index a5fec158d2..e7fc6dd709 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -355,6 +355,8 @@ initmath(void)
 	PyObject *m, *d, *v;
 
 	m = Py_InitModule3("math", math_methods, module_doc);
+	if (m == NULL)
+		goto finally;
 	d = PyModule_GetDict(m);
 
         if (!(v = PyFloat_FromDouble(atan(1.0) * 4.0)))
diff --git a/Modules/md5module.c b/Modules/md5module.c
index 9c647c5e13..e12bef8636 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -303,6 +303,8 @@ init_md5(void)
         if (PyType_Ready(&MD5type) < 0)
             return;
 	m = Py_InitModule3("_md5", md5_functions, module_doc);
+	if (m == NULL)
+	    return;
 	d = PyModule_GetDict(m);
 	PyDict_SetItemString(d, "MD5Type", (PyObject *)&MD5type);
 	PyModule_AddIntConstant(m, "digest_size", 16);
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 2ff4494304..1cd7d1709c 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -1092,6 +1092,8 @@ PyMODINIT_FUNC
 	mmap_object_type.ob_type = &PyType_Type;
 
 	module = Py_InitModule ("mmap", mmap_functions);
+	if (module == NULL)
+		return;
 	dict = PyModule_GetDict (module);
 	mmap_module_error = PyExc_EnvironmentError;
 	Py_INCREF(mmap_module_error);
diff --git a/Modules/nismodule.c b/Modules/nismodule.c
index 2494adbbed..207f7b8e7e 100644
--- a/Modules/nismodule.c
+++ b/Modules/nismodule.c
@@ -379,6 +379,8 @@ initnis (void)
 {
 	PyObject *m, *d;
 	m = Py_InitModule("nis", nis_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	NisError = PyErr_NewException("nis.error", NULL, NULL);
 	if (NisError != NULL)
diff --git a/Modules/operator.c b/Modules/operator.c
index ddd0252931..4817d3389c 100644
--- a/Modules/operator.c
+++ b/Modules/operator.c
@@ -578,6 +578,8 @@ initoperator(void)
 	/* Create the module and add the functions */
         m = Py_InitModule4("operator", operator_methods, operator_doc,
 		       (PyObject*)NULL, PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	if (PyType_Ready(&itemgetter_type) < 0)
 		return;
diff --git a/Modules/ossaudiodev.c b/Modules/ossaudiodev.c
index af3002dd3a..4c22b07fd8 100644
--- a/Modules/ossaudiodev.c
+++ b/Modules/ossaudiodev.c
@@ -963,6 +963,8 @@ initossaudiodev(void)
     PyObject *m;
 
     m = Py_InitModule("ossaudiodev", ossaudiodev_methods);
+    if (m == NULL)
+	return;
 
     OSSAudioError = PyErr_NewException("ossaudiodev.OSSAudioError",
 				       NULL, NULL);
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index e788fc930f..b2b32df549 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -3148,6 +3148,8 @@ initparser(void)
 
     PyST_Type.ob_type = &PyType_Type;
     module = Py_InitModule("parser", parser_functions);
+    if (module == NULL)
+    	return;
 
     if (parser_error == 0)
         parser_error = PyErr_NewException("parser.ParserError", NULL, NULL);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index b783573685..100dfcfc95 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -7967,6 +7967,8 @@ INITFUNC(void)
 	m = Py_InitModule3(MODNAME,
 			   posix_methods,
 			   posix__doc__);
+	if (m == NULL)
+    		return;
 
 	/* Initialize environ dictionary */
 	v = convertenviron();
diff --git a/Modules/puremodule.c b/Modules/puremodule.c
index 43c64414cb..95f4bdecdb 100644
--- a/Modules/puremodule.c
+++ b/Modules/puremodule.c
@@ -952,6 +952,8 @@ initpure()
 	PyObject *m, *d;
 
 	m = Py_InitModule("pure", pure_methods);
+	if (m == NULL)
+    		return;
 	d = PyModule_GetDict(m);
 
         /* this is bogus because we should be able to find this information
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index f418e43c9a..9e7b864b75 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -183,6 +183,8 @@ initpwd(void)
 {
 	PyObject *m;
 	m = Py_InitModule3("pwd", pwd_methods, pwd__doc__);
+	if (m == NULL)
+    		return;
 
 	PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc);
 	Py_INCREF((PyObject *) &StructPwdType);
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c
index c827581b1d..76b7cf9ba0 100644
--- a/Modules/pyexpat.c
+++ b/Modules/pyexpat.c
@@ -1854,6 +1854,8 @@ MODULE_INITFUNC(void)
     /* Create the module and add the functions */
     m = Py_InitModule3(MODULE_NAME, pyexpat_methods,
                        pyexpat_module_documentation);
+    if (m == NULL)
+	return;
 
     /* Add some symbolic constants to the module */
     if (ErrorObject == NULL) {
diff --git a/Modules/readline.c b/Modules/readline.c
index f039f1aaf6..8fda228133 100644
--- a/Modules/readline.c
+++ b/Modules/readline.c
@@ -925,6 +925,8 @@ initreadline(void)
 
 	m = Py_InitModule4("readline", readline_methods, doc_module,
 			   (PyObject *)NULL, PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	PyOS_ReadlineFunctionPointer = call_readline;
 	setup_readline();
diff --git a/Modules/regexmodule.c b/Modules/regexmodule.c
index 9f84032e4d..d44993262e 100644
--- a/Modules/regexmodule.c
+++ b/Modules/regexmodule.c
@@ -653,6 +653,8 @@ initregex(void)
 	Regextype.ob_type = &PyType_Type;
 
 	m = Py_InitModule("regex", regex_global_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 
 	if (PyErr_Warn(PyExc_DeprecationWarning,
diff --git a/Modules/resource.c b/Modules/resource.c
index c5bec79043..7cbd2c94b4 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -234,6 +234,8 @@ initresource(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule("resource", resource_methods);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	if (ResourceError == NULL) {
diff --git a/Modules/rgbimgmodule.c b/Modules/rgbimgmodule.c
index 904c64b642..8c70d95e74 100644
--- a/Modules/rgbimgmodule.c
+++ b/Modules/rgbimgmodule.c
@@ -756,6 +756,8 @@ initrgbimg(void)
 {
 	PyObject *m, *d;
 	m = Py_InitModule("rgbimg", rgbimg_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
 	if (ImgfileError != NULL)
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index ed2ea8197c..53c68c1a55 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -662,6 +662,8 @@ initselect(void)
 {
 	PyObject *m;
 	m = Py_InitModule3("select", select_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	SelectError = PyErr_NewException("select.error", NULL, NULL);
 	Py_INCREF(SelectError);
diff --git a/Modules/sha256module.c b/Modules/sha256module.c
index b40bb70322..e16338dc9e 100644
--- a/Modules/sha256module.c
+++ b/Modules/sha256module.c
@@ -706,4 +706,6 @@ init_sha256(void)
     if (PyType_Ready(&SHA256type) < 0)
         return;
     m = Py_InitModule("_sha256", SHA_functions);
+    if (m == NULL)
+	return;
 }
diff --git a/Modules/sha512module.c b/Modules/sha512module.c
index 44ed7a7764..3837795964 100644
--- a/Modules/sha512module.c
+++ b/Modules/sha512module.c
@@ -772,6 +772,8 @@ init_sha512(void)
     if (PyType_Ready(&SHA512type) < 0)
         return;
     m = Py_InitModule("_sha512", SHA_functions);
+    if (m == NULL)
+	return;
 }
 
 #endif
diff --git a/Modules/shamodule.c b/Modules/shamodule.c
index 1de61c40f5..058391d565 100644
--- a/Modules/shamodule.c
+++ b/Modules/shamodule.c
@@ -590,6 +590,8 @@ init_sha(void)
     if (PyType_Ready(&SHAtype) < 0)
         return;
     m = Py_InitModule("_sha", SHA_functions);
+    if (m == NULL)
+	return;
 
     /* Add some symbolic constants to the module */
     insint("blocksize", 1);  /* For future use, in case some hash
diff --git a/Modules/signalmodule.c b/Modules/signalmodule.c
index bec27293e9..a729604a31 100644
--- a/Modules/signalmodule.c
+++ b/Modules/signalmodule.c
@@ -317,6 +317,8 @@ initsignal(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule3("signal", signal_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	d = PyModule_GetDict(m);
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index b88703c7da..cdefc58d78 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -3871,6 +3871,8 @@ init_socket(void)
 	m = Py_InitModule3(PySocket_MODULE_NAME,
 			   socket_methods,
 			   socket_doc);
+	if (m == NULL)
+		return;
 
 	socket_error = PyErr_NewException("socket.error", NULL, NULL);
 	if (socket_error == NULL)
diff --git a/Modules/spwdmodule.c b/Modules/spwdmodule.c
index 36dd228683..7c618e7761 100644
--- a/Modules/spwdmodule.c
+++ b/Modules/spwdmodule.c
@@ -171,6 +171,8 @@ initspwd(void)
 {
 	PyObject *m;
 	m=Py_InitModule3("spwd", spwd_methods, spwd__doc__);
+	if (m == NULL)
+		return;
 	PyStructSequence_InitType(&StructSpwdType, &struct_spwd_type_desc);
 	Py_INCREF((PyObject *) &StructSpwdType);
 	PyModule_AddObject(m, "struct_spwd", (PyObject *) &StructSpwdType);
diff --git a/Modules/stropmodule.c b/Modules/stropmodule.c
index ed72a7163b..29172987d5 100644
--- a/Modules/stropmodule.c
+++ b/Modules/stropmodule.c
@@ -1210,6 +1210,8 @@ initstrop(void)
 	int c, n;
 	m = Py_InitModule4("strop", strop_methods, strop_module__doc__,
 			   (PyObject*)NULL, PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	/* Create 'whitespace' object */
 	n = 0;
diff --git a/Modules/structmodule.c b/Modules/structmodule.c
index 137b8988e0..f07f21a30c 100644
--- a/Modules/structmodule.c
+++ b/Modules/structmodule.c
@@ -1278,6 +1278,8 @@ initstruct(void)
 	/* Create the module and add the functions */
 	m = Py_InitModule4("struct", struct_methods, struct__doc__,
 			   (PyObject*)NULL, PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	if (StructError == NULL) {
diff --git a/Modules/sunaudiodev.c b/Modules/sunaudiodev.c
index 3269c761d6..802184d0d6 100644
--- a/Modules/sunaudiodev.c
+++ b/Modules/sunaudiodev.c
@@ -456,6 +456,8 @@ initsunaudiodev(void)
 	PyObject *m, *d;
 
 	m = Py_InitModule("sunaudiodev", sunaudiodev_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL);
 	if (SunAudioError)
diff --git a/Modules/svmodule.c b/Modules/svmodule.c
index 9bd79686c4..fb58f19cc3 100644
--- a/Modules/svmodule.c
+++ b/Modules/svmodule.c
@@ -956,6 +956,8 @@ initsv(void)
 	PyObject *m, *d;
 
 	m = Py_InitModule("sv", sv_methods);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 
 	SvError = PyErr_NewException("sv.error", NULL, NULL);
diff --git a/Modules/symtablemodule.c b/Modules/symtablemodule.c
index 7a52aae0e6..c90d7650a1 100644
--- a/Modules/symtablemodule.c
+++ b/Modules/symtablemodule.c
@@ -53,6 +53,8 @@ init_symtable(void)
 	PyObject *m;
 
 	m = Py_InitModule("_symtable", symtable_methods);
+	if (m == NULL)
+		return;
 	PyModule_AddIntConstant(m, "USE", USE);
 	PyModule_AddIntConstant(m, "DEF_GLOBAL", DEF_GLOBAL);
 	PyModule_AddIntConstant(m, "DEF_LOCAL", DEF_LOCAL);
diff --git a/Modules/syslogmodule.c b/Modules/syslogmodule.c
index 1f2b874fdb..dd35923139 100644
--- a/Modules/syslogmodule.c
+++ b/Modules/syslogmodule.c
@@ -163,6 +163,8 @@ initsyslog(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule("syslog", syslog_methods);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 
diff --git a/Modules/termios.c b/Modules/termios.c
index a1d14a18b8..c53566c12b 100644
--- a/Modules/termios.c
+++ b/Modules/termios.c
@@ -910,6 +910,8 @@ PyInit_termios(void)
 
 	m = Py_InitModule4("termios", termios_methods, termios__doc__,
                            (PyObject *)NULL, PYTHON_API_VERSION);
+	if (m == NULL)
+		return;
 
 	if (TermiosError == NULL) {
 		TermiosError = PyErr_NewException("termios.error", NULL, NULL);
diff --git a/Modules/threadmodule.c b/Modules/threadmodule.c
index 3025595df1..fccdd62644 100644
--- a/Modules/threadmodule.c
+++ b/Modules/threadmodule.c
@@ -638,6 +638,8 @@ initthread(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule3("thread", thread_methods, thread_doc);
+	if (m == NULL)
+		return;
 
 	/* Add a symbolic constant */
 	d = PyModule_GetDict(m);
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index 2cd9a571f3..ba93957b88 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -785,6 +785,8 @@ inittime(void)
 	PyObject *m;
 	char *p;
 	m = Py_InitModule3("time", time_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	/* Accept 2-digit dates unless PYTHONY2K is set and non-empty */
 	p = Py_GETENV("PYTHONY2K");
diff --git a/Modules/xxmodule.c b/Modules/xxmodule.c
index 5f75b6cf8f..ea66eefa0a 100644
--- a/Modules/xxmodule.c
+++ b/Modules/xxmodule.c
@@ -352,6 +352,8 @@ initxx(void)
 
 	/* Create the module and add the functions */
 	m = Py_InitModule3("xx", xx_methods, module_doc);
+	if (m == NULL)
+		return;
 
 	/* Add some symbolic constants to the module */
 	if (ErrorObject == NULL) {
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index a598ae31c3..725755d105 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -878,6 +878,8 @@ PyInit_zlib(void)
     m = Py_InitModule4("zlib", zlib_methods,
 		       zlib_module_documentation,
 		       (PyObject*)NULL,PYTHON_API_VERSION);
+    if (m == NULL)
+	return;
 
     ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
     if (ZlibError != NULL) {
diff --git a/PC/_subprocess.c b/PC/_subprocess.c
index 8ed4899d8c..b675b88462 100644
--- a/PC/_subprocess.c
+++ b/PC/_subprocess.c
@@ -553,6 +553,8 @@ init_subprocess()
 	sp_handle_as_number.nb_int = (unaryfunc) sp_handle_as_int;
 
 	m = Py_InitModule("_subprocess", sp_functions);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 
 	/* constants */
diff --git a/PC/_winreg.c b/PC/_winreg.c
index 34e4f6895a..965acf1e18 100644
--- a/PC/_winreg.c
+++ b/PC/_winreg.c
@@ -1459,6 +1459,8 @@ PyMODINIT_FUNC init_winreg(void)
 {
 	PyObject *m, *d;
 	m = Py_InitModule3("_winreg", winreg_methods, module_doc);
+	if (m == NULL)
+		return;
 	d = PyModule_GetDict(m);
 	PyHKEY_Type.ob_type = &PyType_Type;
 	PyHKEY_Type.tp_doc = PyHKEY_doc;
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c
index 84cf0c132e..4453023a6a 100755
--- a/PC/msvcrtmodule.c
+++ b/PC/msvcrtmodule.c
@@ -221,6 +221,8 @@ PyMODINIT_FUNC
 initmsvcrt(void)
 {
 	PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
+	if (m == NULL)
+		return;
 	PyObject *d = PyModule_GetDict(m);
 
 	/* constants for the locking() function's mode argument */
diff --git a/PC/winsound.c b/PC/winsound.c
index b94b322068..81e3917ed6 100644
--- a/PC/winsound.c
+++ b/PC/winsound.c
@@ -220,6 +220,8 @@ initwinsound(void)
 	PyObject *module = Py_InitModule3("winsound",
 					  sound_methods,
 					  sound_module_doc);
+	if (module == NULL)
+		return;
 	PyObject *dict = PyModule_GetDict(module);
 
 	ADD_DEFINE(SND_ASYNC);
diff --git a/Python/import.c b/Python/import.c
index f284ff4d21..8bd25f7764 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2817,6 +2817,8 @@ initimp(void)
 
 	m = Py_InitModule4("imp", imp_methods, doc_imp,
 			   NULL, PYTHON_API_VERSION);
+	if (m == NULL)
+		goto failure;
 	d = PyModule_GetDict(m);
 
 	if (setint(d, "SEARCH_ERROR", SEARCH_ERROR) < 0) goto failure;
diff --git a/Python/marshal.c b/Python/marshal.c
index ff8247c9f2..5617226787 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -1107,5 +1107,7 @@ PyMODINIT_FUNC
 PyMarshal_Init(void)
 {
 	PyObject *mod = Py_InitModule("marshal", marshal_methods);
+	if (mod == NULL)
+		return;
 	PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
 }
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 5bbe950224..f793b99348 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1027,6 +1027,8 @@ _PySys_Init(void)
 #endif
 
 	m = Py_InitModule3("sys", sys_methods, sys_doc);
+	if (m == NULL)
+		return NULL;
 	sysdict = PyModule_GetDict(m);
 
 	{
-- 
2.49.0