Bug #742342: make Python stop segfaulting on infinitely-recursive reload()s. Fixed by patch #922167.
Backported from r54291.
diff --git a/Python/import.c b/Python/import.c
index 390f9e3..b62eeef 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -339,6 +339,25 @@
 	return Py_None;
 }
 
+PyObject *
+PyImport_GetModulesReloading(void)
+{
+	PyInterpreterState *interp = PyThreadState_Get()->interp;
+	if (interp->modules_reloading == NULL)
+		Py_FatalError("PyImport_GetModuleDict: no modules_reloading dictionary!");
+	return interp->modules_reloading;
+}
+
+static void
+imp_modules_reloading_clear (void)
+{
+	PyInterpreterState *interp = PyThreadState_Get()->interp;
+	if (interp->modules_reloading == NULL)
+		return;
+	PyDict_Clear(interp->modules_reloading);
+	return;
+}
+
 /* Helper for sys */
 
 PyObject *
@@ -498,6 +517,7 @@
 	PyDict_Clear(modules);
 	interp->modules = NULL;
 	Py_DECREF(modules);
+	Py_CLEAR(interp->modules_reloading);
 }
 
 
@@ -2400,8 +2420,9 @@
 PyObject *
 PyImport_ReloadModule(PyObject *m)
 {
+	PyObject *modules_reloading = PyImport_GetModulesReloading();
 	PyObject *modules = PyImport_GetModuleDict();
-	PyObject *path = NULL, *loader = NULL;
+	PyObject *path = NULL, *loader = NULL, *existing_m = NULL;
 	char *name, *subname;
 	char buf[MAXPATHLEN+1];
 	struct filedescr *fdp;
@@ -2422,20 +2443,30 @@
 			     name);
 		return NULL;
 	}
+	if ((existing_m = PyDict_GetItemString(modules_reloading, name)) != NULL) {
+		/* Due to a recursive reload, this module is already being reloaded. */
+		Py_INCREF(existing_m);
+		return existing_m;
+	}
+ 	PyDict_SetItemString(modules_reloading, name, m);
+
 	subname = strrchr(name, '.');
 	if (subname == NULL)
 		subname = name;
 	else {
 		PyObject *parentname, *parent;
 		parentname = PyString_FromStringAndSize(name, (subname-name));
-		if (parentname == NULL)
+		if (parentname == NULL) {
+			imp_modules_reloading_clear();
 			return NULL;
+		}
 		parent = PyDict_GetItem(modules, parentname);
 		if (parent == NULL) {
 			PyErr_Format(PyExc_ImportError,
 			    "reload(): parent %.200s not in sys.modules",
 			    PyString_AS_STRING(parentname));
 			Py_DECREF(parentname);
+			imp_modules_reloading_clear();
 			return NULL;
 		}
 		Py_DECREF(parentname);
@@ -2450,6 +2481,7 @@
 
 	if (fdp == NULL) {
 		Py_XDECREF(loader);
+		imp_modules_reloading_clear();
 		return NULL;
 	}
 
@@ -2466,6 +2498,7 @@
 		 */
 		PyDict_SetItemString(modules, name, m);
 	}
+	imp_modules_reloading_clear();
 	return newm;
 }