mercurial/parsers.c
changeset 20155 21dafd8546d1
parent 20111 9bfa86746c9c
child 20159 96b2dd77c85d
--- a/mercurial/parsers.c	Wed Nov 27 16:43:02 2013 -0500
+++ b/mercurial/parsers.c	Fri Nov 29 12:36:28 2013 -0800
@@ -1941,6 +1941,25 @@
 	dirstate_unset = Py_BuildValue("ciii", 'n', 0, -1, -1);
 }
 
+static int check_python_version()
+{
+	PyObject *sys = PyImport_ImportModule("sys");
+	PyObject *hexversion = PyObject_GetAttrString(sys, "hexversion");
+	long version = PyInt_AsLong(hexversion);
+	/* sys.hexversion is a 32-bit number by default, so the -1 case
+	 * should only occur in unusual circumstances (e.g. if sys.hexversion
+	 * is manually set to an invalid value). */
+	if ((version == -1) || (version >> 16 != PY_VERSION_HEX >> 16)) {
+		PyErr_Format(PyExc_ImportError, "Python minor version mismatch: "
+			"The Mercurial extension modules were compiled with Python "
+			PY_VERSION ", but Mercurial is currently using Python with "
+			"sys.hexversion=%ld: Python %s\n at: %s", version,
+			Py_GetVersion(), Py_GetProgramFullPath());
+		return -1;
+	}
+	return 0;
+}
+
 #ifdef IS_PY3K
 static struct PyModuleDef parsers_module = {
 	PyModuleDef_HEAD_INIT,
@@ -1952,6 +1971,8 @@
 
 PyMODINIT_FUNC PyInit_parsers(void)
 {
+	if (check_python_version() == -1)
+		return;
 	PyObject *mod = PyModule_Create(&parsers_module);
 	module_init(mod);
 	return mod;
@@ -1959,6 +1980,8 @@
 #else
 PyMODINIT_FUNC initparsers(void)
 {
+	if (check_python_version() == -1)
+		return;
 	PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc);
 	module_init(mod);
 }