[libpeas] Print a critical warning when attempting to mix Python versions



commit d66094c5aaa494bf7246d857f2e93c1e404e8222
Author: Garrett Regier <garrettregier gmail com>
Date:   Tue Nov 12 23:00:22 2013 -0800

    Print a critical warning when attempting to mix Python versions
    
    This prevents people from trying to load python3 plugins using
    a python2 program and vice versa.

 loaders/python/peas-plugin-loader-python.c |   14 +++++++++
 tests/libpeas/extension-py.c               |   44 ++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 0 deletions(-)
---
diff --git a/loaders/python/peas-plugin-loader-python.c b/loaders/python/peas-plugin-loader-python.c
index b7a5881..f59a0bc 100644
--- a/loaders/python/peas-plugin-loader-python.c
+++ b/loaders/python/peas-plugin-loader-python.c
@@ -371,6 +371,7 @@ static gboolean
 peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
 {
   PeasPluginLoaderPython *pyloader = PEAS_PLUGIN_LOADER_PYTHON (loader);
+  long hexversion;
   PyObject *mdict, *gettext, *install, *gettext_args;
   const gchar *prgname;
 #if PY_VERSION_HEX < 0x03000000
@@ -412,6 +413,19 @@ peas_plugin_loader_python_initialize (PeasPluginLoader *loader)
       pyloader->priv->must_finalize_python = TRUE;
     }
 
+  hexversion = PyLong_AsLong (PySys_GetObject ((char *) "hexversion"));
+
+#if PY_VERSION_HEX < 0x03000000
+  if (hexversion >= 0x03000000)
+#else
+  if (hexversion < 0x03000000)
+#endif
+    {
+      g_critical ("Attempting to mix incompatible Python versions");
+
+      goto python_init_error;
+    }
+
   prgname = g_get_prgname ();
   prgname = prgname == NULL ? "" : prgname;
 
diff --git a/tests/libpeas/extension-py.c b/tests/libpeas/extension-py.c
index bbea982..4794d83 100644
--- a/tests/libpeas/extension-py.c
+++ b/tests/libpeas/extension-py.c
@@ -36,11 +36,14 @@
 
 #if PY_VERSION_HEX < 0x03000000
 #define PY_LOADER       python
+#define ALT_PY_LOADER   python3
 #else
 #define PY_LOADER       python3
+#define ALT_PY_LOADER   python
 #endif
 
 #define PY_LOADER_STR       G_STRINGIFY (PY_LOADER)
+#define ALT_PY_LOADER_STR   G_STRINGIFY (ALT_PY_LOADER)
 
 
 static void
@@ -156,6 +159,41 @@ test_extension_py_already_initialized_subprocess (void)
   g_assert (Py_IsInitialized ());
   Py_Finalize ();
 }
+
+#if ENABLE_PYTHON2 && ENABLE_PYTHON3
+static void
+test_extension_py_mixed_python (void)
+{
+  g_test_trap_subprocess (EXTENSION_TEST_NAME (PY_LOADER,
+                                               "mixed-python/subprocess"),
+                          0, 0);
+  g_test_trap_assert_passed ();
+  g_test_trap_assert_stderr ("");
+}
+
+static void
+test_extension_py_mixed_python_subprocess (void)
+{
+  PeasEngine *engine;
+  PeasPluginInfo *info;
+
+  testing_util_push_log_hook ("*mix incompatible Python versions*");
+  testing_util_push_log_hook ("*check the installation*");
+  testing_util_push_log_hook ("*'" ALT_PY_LOADER_STR
+                              "' is not a valid PeasPluginLoader*");
+  testing_util_push_log_hook ("*Could not find loader '"
+                              ALT_PY_LOADER_STR "'*");
+
+  engine = testing_engine_new ();
+  peas_engine_enable_loader (engine, ALT_PY_LOADER_STR);
+
+  info = peas_engine_get_plugin_info (engine, "extension-" ALT_PY_LOADER_STR);
+
+  g_assert (!peas_engine_load_plugin (engine, info));
+
+  testing_engine_free (engine);
+}
+#endif
 #endif
 
 int
@@ -186,6 +224,12 @@ main (int   argc,
   EXTENSION_TEST_FUNC (PY_LOADER, "already-initialized", already_initialized);
   EXTENSION_TEST_FUNC (PY_LOADER, "already-initialized/subprocess",
                        already_initialized_subprocess);
+
+#if ENABLE_PYTHON2 && ENABLE_PYTHON3
+  EXTENSION_TEST_FUNC (PY_LOADER, "mixed-python", mixed_python);
+  EXTENSION_TEST_FUNC (PY_LOADER, "mixed-python/subprocess",
+                       mixed_python_subprocess);
+#endif
 #endif
 
   return testing_extension_run_tests ();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]