Salome HOME
Merge branch 'BR_PYTHON_PLUGIN' of newgeom:newgeom.git into Dev_0.6.1
[modules/shaper.git] / src / Config / Config_ModuleReader.cpp
index 83b16f27d5afe166f58d15d66e602cb4a6b8a494..21925af599b1b91fca64131371d931ed98ad6443 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
+
 /*
  * Config_ModuleReader.cpp
  *
@@ -96,16 +98,16 @@ std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
                                            const std::string& aPluginScript,
                                            const std::string& aPluginConf)
 {
-  PluginType aType = PluginType::Binary;
+  PluginType aType = Config_ModuleReader::Binary;
   std::string aPluginName;
   if (!aPluginLibrary.empty()) {
     aPluginName = aPluginLibrary;
     if (aPluginConf.empty()) {
-      aType = PluginType::Intrenal;
+      aType = Config_ModuleReader::Intrenal;
     }
   } else if (!aPluginScript.empty()) {
     aPluginName = aPluginScript;
-    aType = PluginType::Python;
+    aType = Config_ModuleReader::Python;
   }
   if(!aPluginName.empty()) {
     myPluginTypes[aPluginName] = aType;
@@ -116,16 +118,16 @@ std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary,
 
 void Config_ModuleReader::loadPlugin(const std::string thePluginName)
 {
-  PluginType aType = PluginType::Binary;
+  PluginType aType = Config_ModuleReader::Binary;
   if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) {
     aType = myPluginTypes.at(thePluginName);
   }
   switch (aType) {
-    case PluginType::Python:
+    case Config_ModuleReader::Python:
       loadScript(thePluginName);
       break;
-    case PluginType::Binary:
-    case PluginType::Intrenal:
+    case Config_ModuleReader::Binary:
+    case Config_ModuleReader::Intrenal:
     default:
       loadLibrary(thePluginName);
       break;
@@ -134,10 +136,27 @@ void Config_ModuleReader::loadPlugin(const std::string thePluginName)
 
 void Config_ModuleReader::loadScript(const std::string theFileName)
 {
-  std::string aPythonFile = theFileName + ".py";
   /* aquire python thread */
   PyGILState_STATE gstate = PyGILState_Ensure();
-  PyObject* module = PyImport_ImportModule(aPythonFile.c_str());
+  PyObject* module = PyImport_ImportModule(theFileName.c_str());
+
+  if (!module) {
+    std::string anErrorMsg = "An error occured while importing " + theFileName;
+    //Get detailed error message:
+    if (PyErr_Occurred()) {
+      PyObject *ptype, *pvalue, *ptraceback;
+      PyErr_Fetch(&ptype, &pvalue, &ptraceback);
+      std::string aPyError = std::string(PyString_AsString(pvalue));
+      if (!aPyError.empty()) {
+        anErrorMsg += ":\n" + aPyError;
+      }
+      Py_XDECREF(ptype);
+      Py_XDECREF(pvalue);
+      Py_XDECREF(ptraceback);
+    }
+    Events_Error::send(anErrorMsg);
+  }
+
   /* release python thread */
   PyGILState_Release(gstate);
 }