Salome HOME
salome_pluginsmanager less verbose
[modules/gui.git] / src / SUITApp / SUITApp_init_python.cxx
index bfbe0f217474d3ea12feee103200be972afaa201..7f5d7021c615fdec6cdc78e318dec148ba076a59 100644 (file)
 //  Date   : 22/06/2007
 //
 #include "SUITApp_init_python.hxx"
+#include <QString>
+
+#if PY_VERSION_HEX < 0x03050000
+static wchar_t*
+Py_DecodeLocale(const char *arg, size_t *size)
+{
+    wchar_t *res;
+    unsigned char *in;
+    wchar_t *out;
+    size_t argsize = strlen(arg) + 1;
+
+    if (argsize > PY_SSIZE_T_MAX/sizeof(wchar_t))
+        return NULL;
+    res = (wchar_t*) PyMem_RawMalloc(argsize*sizeof(wchar_t));
+    if (!res)
+        return NULL;
+
+    in = (unsigned char*)arg;
+    out = res;
+    while(*in)
+        if(*in < 128)
+            *out++ = *in++;
+        else
+            *out++ = 0xdc00 + *in++;
+    *out = 0;
+    if (size != NULL)
+        *size = out - res;
+    return res;
+}
+#endif
 
 bool SUIT_PYTHON::initialized                       = false;
 
@@ -30,14 +60,34 @@ void SUIT_PYTHON::init_python(int argc, char **argv)
   {
     return;
   }
-  Py_SetProgramName(argv[0]);
+
+  wchar_t **changed_argv = new wchar_t*[argc]; // Setting arguments
+  for (int i = 0; i < argc; i++)
+  {
+   changed_argv[i] = Py_DecodeLocale(argv[i], NULL);    
+  }
+
+  Py_SetProgramName(changed_argv[0]);
   Py_Initialize(); // Initialize the interpreter
-  PySys_SetArgv(argc, argv);
-  PyEval_InitThreads(); // Create (and acquire) the interpreter lock - can be called many times
 
+  PySys_SetArgv(argc, changed_argv);
+  PyRun_SimpleString("import threading\n");
+  // VSR (22/09/2016): This is a workaround to prevent invoking qFatal() from PyQt5
+  // causing application aborting
+  QString script;
+  script += "def _custom_except_hook(exc_type, exc_value, exc_traceback):\n";
+  script += "  import sys\n";
+  script += "  sys.__excepthook__(exc_type, exc_value, exc_traceback)\n";
+  script += "  pass\n";
+  script += "\n";
+  script += "import sys\n";
+  script += "sys.excepthook = _custom_except_hook\n";
+  script += "del _custom_except_hook, sys\n";
+  int res = PyRun_SimpleString(qPrintable(script));
+  // VSR (22/09/2016): end of workaround
+  PyEval_InitThreads(); // Create (and acquire) the interpreter lock - can be called many times
   // Py_InitThreads acquires the GIL
   PyThreadState *pts = PyGILState_GetThisThreadState(); 
   PyEval_ReleaseThread(pts);
   SUIT_PYTHON::initialized = true;
 }
-