better __Pyx_check_binary_version()
authorLisandro Dalcin <dalcinl@gmail.com>
Sun, 20 Feb 2011 21:34:11 +0000 (18:34 -0300)
committerLisandro Dalcin <dalcinl@gmail.com>
Sun, 20 Feb 2011 21:34:11 +0000 (18:34 -0300)
Cython/Compiler/ModuleNode.py

index c363433b0ec424cf615c7e1ce83352ff9cb84298..40010aed869316eda12f9d5c49e99dc8e02d0e80 100644 (file)
@@ -1758,7 +1758,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         code.putln("#endif")
 
         env.use_utility_code(check_binary_version_utility_code)
-        code.putln("__Pyx_check_binary_version();")
+        code.putln("if ( __Pyx_check_binary_version() < 0) %s" % code.error_goto(self.pos))
 
         code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
         code.putln("%s = PyBytes_FromStringAndSize(\"\", 0); %s" % (Naming.empty_bytes, code.error_goto_if_null(Naming.empty_bytes, self.pos)));
@@ -2858,34 +2858,24 @@ check_binary_version_utility_code = UtilityCode(proto="""
 static int __Pyx_check_binary_version(void);
 """, impl="""
 static int __Pyx_check_binary_version(void) {
-    int res = -1;
-    PyObject *major_info = NULL;
-    PyObject *minor_info = NULL;
-    PyObject *version_info = PySys_GetObject("version_info");
-    if (version_info == NULL) goto bad;
-    major_info = PySequence_GetItem(version_info, 0);
-    if (major_info == NULL || !PyInt_CheckExact(major_info)) goto bad;
-    minor_info = PySequence_GetItem(version_info, 1);
-    if (minor_info == NULL || !PyInt_CheckExact(minor_info)) goto bad;
-    if (PyInt_AsLong(major_info) == PY_MAJOR_VERSION && PyInt_AsLong(minor_info) == PY_MINOR_VERSION) {
-        res = 0;
-    } else {
-        char warning[200];
-        PyOS_snprintf(warning, sizeof(warning),
-            "compiletime version (%d, %d) of module %s does not match runtime version (%ld, %ld)",
-            PY_MAJOR_VERSION, PY_MINOR_VERSION,
-            __Pyx_MODULE_NAME,
-            PyInt_AsLong(major_info), PyInt_AsLong(minor_info));
+    long version_hex, major_version, minor_version;
+    PyObject *sys_hexversion = PySys_GetObject((char*)"hexversion");
+    if (sys_hexversion == NULL) return -1;
+    version_hex = PyInt_AsLong(sys_hexversion);
+    if (version_hex == -1 && PyErr_Occurred()) return -1;
+    major_version = ((unsigned long)version_hex >> 24);
+    minor_version = ((unsigned long)version_hex >> 16) & 0x00FF;
+    if (!(major_version == PY_MAJOR_VERSION &&  minor_version == PY_MINOR_VERSION)) {
+        char message[200];
+        PyOS_snprintf(message, sizeof(message),
+            "compiletime version (%d, %d) of module '%.100s' does not match runtime version (%d, %d).",
+            PY_MAJOR_VERSION, PY_MINOR_VERSION, __Pyx_MODULE_NAME, (int)major_version, (int)minor_version);
         #if PY_VERSION_HEX < 0x02050000
-        PyErr_Warn(NULL, warning);
+        return PyErr_Warn(NULL, message);
         #else
-        PyErr_WarnEx(NULL, warning, 0);
+        return PyErr_WarnEx(NULL, message, 1);
         #endif
-        res = 1;
     }
-bad:
-    Py_XDECREF(major_info);
-    Py_XDECREF(minor_info);
-    return res;
+    return 0;
 }
 """)