[glom] Button scripts: Check and warn about pygtk2 instead of crashing.



commit 6b5f2993a8675a28e853bbdd64e9eccec9c4a7dc
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) .

 ChangeLog                               |   12 ++++++
 Makefile_tests.am                       |    6 +++
 glom/libglom/utils.cc                   |   10 +++++
 glom/libglom/utils.h                    |    6 +++
 glom/mode_data/box_data.cc              |   10 +++++-
 tests/test_document_load_and_change.cc  |    2 +-
 tests/test_script_check_for_problems.cc |   59 +++++++++++++++++++++++++++++++
 7 files changed, 103 insertions(+), 2 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 8d21dfb..650bcc2 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 c9cdb05..b1aaaef 100644
--- a/Makefile_tests.am
+++ b/Makefile_tests.am
@@ -34,6 +34,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_change_sysprefs \
 	tests/test_selfhosting_new_empty_then_users \
@@ -71,6 +72,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_change_sysprefs \
 	tests/test_selfhosting_new_empty_then_users \
@@ -220,6 +222,10 @@ 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_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 4133e7b..64f274b 100644
--- a/glom/libglom/utils.cc
+++ b/glom/libglom/utils.cc
@@ -1501,5 +1501,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 58498c7..cc48666 100644
--- a/glom/libglom/utils.h
+++ b/glom/libglom/utils.h
@@ -236,6 +236,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 df821c4..c3c701b 100644
--- a/glom/mode_data/box_data.cc
+++ b/glom/mode_data/box_data.cc
@@ -352,6 +352,14 @@ 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);
 
@@ -362,7 +370,7 @@ void Box_Data::execute_button_script(const sharedptr<const LayoutItem_Button>& l
   AppPythonUICallbacks callbacks;
 
   Glib::ustring error_message;
-  glom_execute_python_function_implementation(layout_item->get_script(),
+  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,
diff --git a/tests/test_document_load_and_change.cc b/tests/test_document_load_and_change.cc
index d8a1917..0653b8a 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]