From 35b80f7d5aeb616cd32840efd3b5267b3a57e7b1 Mon Sep 17 00:00:00 2001
From: Marco Biasini <marco.biasini@unibas.ch>
Date: Wed, 22 Dec 2010 11:28:02 +0100
Subject: [PATCH] fix race condition in PythonInterpreterWorker that shows up
 when two nested event loops trigger the Wake slot.

---
 modules/gui/src/file_browser.cc                           | 3 ++-
 modules/gui/src/file_loader.cc                            | 3 ++-
 modules/gui/src/python_shell/python_interpreter_worker.cc | 7 ++++++-
 modules/gui/src/python_shell/python_interpreter_worker.hh | 1 +
 4 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/modules/gui/src/file_browser.cc b/modules/gui/src/file_browser.cc
index ca7092684..96f87677e 100644
--- a/modules/gui/src/file_browser.cc
+++ b/modules/gui/src/file_browser.cc
@@ -112,7 +112,8 @@ void FileBrowser::Init(const QString& path)
   view_->setRootIndex(model_->index(path));
   view_->setAttribute(Qt::WA_MacShowFocusRect, false);
   view_->setContextMenuPolicy(Qt::CustomContextMenu);
-  connect(view_,SIGNAL(customContextMenuRequested(const QPoint&)),this,SLOT(ShowContextMenu(const QPoint&)));
+  connect(view_,SIGNAL(customContextMenuRequested(const QPoint&)),this,
+          SLOT(ShowContextMenu(const QPoint&)));
   UpdateMenu(path);
 
   QVBoxLayout* l=new QVBoxLayout(this);
diff --git a/modules/gui/src/file_loader.cc b/modules/gui/src/file_loader.cc
index b013bdfdf..2517e7909 100644
--- a/modules/gui/src/file_loader.cc
+++ b/modules/gui/src/file_loader.cc
@@ -371,7 +371,8 @@ void FileLoader::RunScript(const QString& filename)
   pi.RunCommand("sys.argv.append('"+QFileInfo(filename).fileName()+"')");
   pi.RunCommand("_dir=os.getcwd()");
   pi.RunCommand("os.chdir('"+QFileInfo(filename).absolutePath()+"')");
-  pi.RunCommand("execfile('"+QFileInfo(filename).fileName()+"')");
+  pi.RunScript(QFileInfo(filename).fileName());
+  //pi.RunCommand("execfile('"+QFileInfo(filename).fileName()+"')");
   pi.RunCommand("os.chdir(_dir)");
   pi.RunCommand("del(_dir)");
   pi.RunCommand("sys.argv=_sys_argv_backup");
diff --git a/modules/gui/src/python_shell/python_interpreter_worker.cc b/modules/gui/src/python_shell/python_interpreter_worker.cc
index 494df6c0b..b89859142 100644
--- a/modules/gui/src/python_shell/python_interpreter_worker.cc
+++ b/modules/gui/src/python_shell/python_interpreter_worker.cc
@@ -32,7 +32,8 @@ PythonInterpreterWorker::PythonInterpreterWorker():
   parse_expr_cmd_(),
   repr_(),
   main_namespace_(),
-  current_id_()
+  current_id_(),
+  awake_(false)
 {
   Py_InitializeEx(1);
   parse_expr_cmd_=bp::import("parser").attr("expr");
@@ -52,10 +53,14 @@ PythonInterpreterWorker::PythonInterpreterWorker():
 
 void PythonInterpreterWorker::Wake()
 {
+  if (awake_) return;
+
+  awake_=true;
   while (!exec_queue_.isEmpty()){
     std::pair<unsigned int, QString> pair=exec_queue_.dequeue();
     run_command_(pair);
   }
+  awake_=false;
 }
 
 unsigned int PythonInterpreterWorker::AddCommand(const QString& command)
diff --git a/modules/gui/src/python_shell/python_interpreter_worker.hh b/modules/gui/src/python_shell/python_interpreter_worker.hh
index 77295ca86..7d38dd544 100644
--- a/modules/gui/src/python_shell/python_interpreter_worker.hh
+++ b/modules/gui/src/python_shell/python_interpreter_worker.hh
@@ -46,6 +46,7 @@ protected:
   bp::object repr_;
   bp::dict main_namespace_;
   unsigned int current_id_;
+  bool         awake_;
 };
 
 }} //ns
-- 
GitLab