[glom/glom-1-20] Button scripts: Check and warn about pygtk2 instead of crashing.



commit bca9449a17d6a3d5993a4e66095d049ec8aa3dbd
Author: Murray Cumming <murrayc murrayc com>
Date:   Fri Feb 3 17:29:12 2012 +0100

    Button scripts: Check and warn about pygtk2 instead of crashing.
    
    * glom/libglom/utils.[h|cc]: Add script_check_for_pygtk().
    * Makefile_tests.am:
    * tests/test_script_check_for_problems.cc: Add a test for the check
    function.
    * glom/mode_data/box_data.cc: execute_button_script(): Show a
    warning dialog, and do not run the script, if it seems to use pygtk.
    This should help with but #669196 (alien) and ##661766 (Andre Klapper) .
    
    Conflicts:
    
    	Makefile_tests.am
    	glom/mode_data/box_data.cc

 ChangeLog                               |   12 ++++++
 Makefile_tests.am                       |    8 ++++-
 glom/libglom/utils.cc                   |   10 +++++
 glom/libglom/utils.h                    |    6 +++
 glom/mode_data/box_data.cc              |   38 ++++++++++++--------
 tests/test_document_load_and_change.cc  |    2 +-
 tests/test_script_check_for_problems.cc |   59 +++++++++++++++++++++++++++++++
 7 files changed, 118 insertions(+), 17 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 0d894a0..d87ef02 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,17 @@
 2012-02-03  Murray Cumming  <murrayc murrayc com>
 
+	Button scripts: Check and warn about pygtk2 instead of crashing.
+
+	* glom/libglom/utils.[h|cc]: Add script_check_for_pygtk().
+	* Makefile_tests.am:
+	* tests/test_script_check_for_problems.cc: Add a test for the check
+	function.
+	* glom/mode_data/box_data.cc: execute_button_script(): Show a 
+	warning dialog, and do not run the script, if it seems to use pygtk.
+	This should help with but #669196 (alien) and ##661766 (Andre Klapper) .
+
+2012-02-03  Murray Cumming  <murrayc murrayc com>
+
 	ReportBuilder: Add error checking.
 
 	* glom/libglom/report_builder.[h|cc]: Return bool
diff --git a/Makefile_tests.am b/Makefile_tests.am
index a2090ce..fc97716 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -32,6 +32,7 @@ check_PROGRAMS =						\
 	tests/python/test_load_python_library\
 	tests/python/test_python_module \
 	tests/test_fake_connection \
+	tests/test_script_check_for_problems \
 	tests/test_selfhosting_new_empty \
 	tests/test_selfhosting_new_empty_then_users \
 	tests/test_selfhosting_new_from_example \
@@ -62,6 +63,7 @@ TESTS =	tests/test_document_load	\
 	tests/python/test_load_python_library \
 	tests/python/test_python_module \
 	tests/test_fake_connection \
+	tests/test_script_check_for_problems \
 	tests/test_selfhosting_new_empty \
 	tests/test_selfhosting_new_empty_then_users \
 	tests/test_selfhosting_new_from_example \
@@ -182,7 +184,11 @@ tests_test_fake_connection_SOURCES = tests/test_fake_connection.cc
 tests_test_fake_connection_LDADD = $(tests_ldadd)
 tests_test_fake_connection_CPPFLAGS = $(tests_cppflags)
 
-tests_test_selfhosting_new_empty_SOURCES = tests/test_selfhosting_new_empty.cc
+tests_test_script_check_for_problems_SOURCES = tests/test_script_check_for_problems.cc
+tests_test_script_check_for_problems_LDADD = $(tests_ldadd)
+tests_test_script_check_for_problems_CPPFLAGS = $(tests_cppflags)
+
+tests_test_selfhosting_new_empty_SOURCES = tests/test_selfhosting_new_empty.cc $(sources_test_selfhosting_utils)
 tests_test_selfhosting_new_empty_LDADD = $(tests_ldadd)
 tests_test_selfhosting_new_empty_CPPFLAGS = $(tests_cppflags)
 
diff --git a/glom/libglom/utils.cc b/glom/libglom/utils.cc
index d91cba9..78b2453 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -1478,5 +1478,15 @@ LayoutGroup::type_list_items Utils::get_layout_items_plus_primary_key(const Layo
   return items_plus_pk;
 }
 
+bool Utils::script_check_for_pygtk2(const Glib::ustring& script)
+{
+  //There are probably other code that this will not catch,
+  //but this is better than nothing.
+  //TODO: Instead override python's import mechanism somehow?
+  if(script.find("import pygtk") != std::string::npos)
+    return false;
+
+  return true;
+}
 
 } //namespace Glom
diff --git a/glom/libglom/utils.h b/glom/libglom/utils.h
index 30680bc..5bc8c4f 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -232,6 +232,12 @@ std::string get_temp_directory_path(const std::string& prefix = std::string());
  */
 Glib::ustring get_temp_directory_uri(const std::string& prefix = std::string());
 
+/** @returns true if the script is OK, or 
+ * false if the script uses pygtk2, which would cause a crash,
+ * because Glom itself uses GTK+ 3.
+ */
+bool script_check_for_pygtk2(const Glib::ustring& script);
+
 } //namespace Utils
 
 } //namespace Glom
diff --git a/glom/mode_data/box_data.cc b/glom/mode_data/box_data.cc
index bee7bb4..d326f00 100644
--- a/glom/mode_data/box_data.cc
+++ b/glom/mode_data/box_data.cc
@@ -350,27 +350,35 @@ Glib::ustring Box_Data::get_layout_name() const
 
 void Box_Data::execute_button_script(const sharedptr<const LayoutItem_Button>& layout_item, const Gnome::Gda::Value& primary_key_value)
 {
+  const Glib::ustring script = layout_item->get_script();
+  if(!Utils::script_check_for_pygtk2(script))
+  {
+    Utils::show_ok_dialog(_("Script Uses PyGTK 2"),
+      _("Glom cannot run this script because it uses pygtk 2, but Glom uses GTK+ 3, and attempting to use pygtk 2 would cause Glom to crash."), *get_app_window(), Gtk::MESSAGE_ERROR);
+    return;
+  }
+
   const sharedptr<Field> field_primary_key = get_field_primary_key();
   const type_map_fields field_values = get_record_field_values_for_calculation(m_table_name, field_primary_key, primary_key_value);
 
   //We need the connection when we run the script, so that the script may use it.
   sharedptr<SharedConnection> sharedconnection = connect_to_server(0 /* parent window */);
 
-    //Allow this UI to respond to UI change requests from the Python code:
-    AppPythonUICallbacks callbacks;
-
-    Glib::ustring error_message;
-    glom_execute_python_function_implementation(layout_item->get_script(),
-      field_values, //TODO: Maybe use the field's type here.
-      get_document(),
-      get_table_name(), field_primary_key, primary_key_value,
-      sharedconnection->get_gda_connection(),
-      callbacks,
-      error_message);
-    if(!error_message.empty())
-    {
-      std::cerr << "Python Error: " << error_message << std::endl;
-    }
+  //Allow this UI to respond to UI change requests from the Python code:
+  AppPythonUICallbacks callbacks;
+
+  Glib::ustring error_message;
+  glom_execute_python_function_implementation(script,
+    field_values, //TODO: Maybe use the field's type here.
+    get_document(),
+    get_table_name(), field_primary_key, primary_key_value,
+    sharedconnection->get_gda_connection(),
+    callbacks,
+    error_message);
+  if(!error_message.empty())
+  {
+    std::cerr << "Python Error: " << error_message << std::endl;
+  }
 }
 
 } //namespace Glom
diff --git a/tests/test_document_load_and_change.cc b/tests/test_document_load_and_change.cc
index ef95689..45de06f 100644
--- a/tests/test_document_load_and_change.cc
+++ b/tests/test_document_load_and_change.cc
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public
  * License along with this program; if not, write to the
-71 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  * Boston, MA 02111-1307, USA.
  */
 
diff --git a/tests/test_script_check_for_problems.cc b/tests/test_script_check_for_problems.cc
new file mode 100644
index 0000000..104ee2e
--- /dev/null
+++ b/tests/test_script_check_for_problems.cc
@@ -0,0 +1,59 @@
+/* Glom
+ *
+ * Copyright (C) 2012 Openismus GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <libglom/utils.h>
+#include <libglom/init.h>
+#include <iostream>
+
+int main()
+{
+  Glom::libglom_init();
+
+  Glib::ustring script;
+
+  if(!Glom::Utils::script_check_for_pygtk2(script))
+  {
+    std::cerr << "script_check_for_pygtk2() failed unexpectedly." << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  script =
+    "from gi.repository import Gtk";
+
+  if(!Glom::Utils::script_check_for_pygtk2(script))
+  {
+    std::cerr << "script_check_for_pygtk2() failed unexpectedly." << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  script =
+    "import pygtk\n" \
+    "pygtk.require('2.0')\n" \
+    "import gtk";
+
+  if(Glom::Utils::script_check_for_pygtk2(script))
+  {
+    std::cerr << "script_check_for_pygtk2() succeeded unexpectedly." << std::endl;
+    return EXIT_FAILURE;
+  }
+
+  return EXIT_SUCCESS;
+}
+



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