Salome HOME
Merge branch 'omu/update_doc_77'
[modules/yacs.git] / src / runtime / RuntimeSALOME.cxx
index cacfd292e89f21e3d6a0e9192bf299a673f674f2..0ac34e183a676cd83e037f493d6d6e1f60bf011e 100644 (file)
@@ -1,9 +1,9 @@
-// Copyright (C) 2006-2012  CEA/DEN, EDF R&D
+// Copyright (C) 2006-2015  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -35,6 +35,7 @@
 #include "TypeCode.hxx"
 #include "WhileLoop.hxx"
 #include "ForLoop.hxx"
+#include "ForEachLoop.hxx"
 #include "SalomeOptimizerLoop.hxx"
 #include "Bloc.hxx"
 #include "InputPort.hxx"
@@ -42,6 +43,7 @@
 #include "PresetPorts.hxx"
 #include "InputDataStreamPort.hxx"
 #include "OutputDataStreamPort.hxx"
+#include "Switch.hxx"
 #include "SalomeProc.hxx"
 #include "PyStdout.hxx"
 //Catalog Loaders
 //Components
 #include "CORBAComponent.hxx"
 #include "SalomeComponent.hxx"
+#include "SalomeHPComponent.hxx"
 #include "SalomePythonComponent.hxx"
 #include "CppComponent.hxx"
 
 #include "SalomeContainer.hxx"
 #include "CppContainer.hxx"
+#include "SalomeHPContainer.hxx"
 
 //Nodes
 #include "PythonNode.hxx"
 using namespace std;
 using namespace YACS::ENGINE;
 
-void RuntimeSALOME::setRuntime(long flags) // singleton creation (not thread safe!)
+void RuntimeSALOME::setRuntime(long flags, int argc, char* argv[]) // singleton creation (not thread safe!)
 {
   if (! Runtime::_singleton)
     {
-      RuntimeSALOME* r=new RuntimeSALOME(flags);
+      RuntimeSALOME* r=new RuntimeSALOME(flags, argc, argv);
       Runtime::_singleton = r;
       r->initBuiltins();
     }
@@ -177,12 +181,22 @@ void RuntimeSALOME::initBuiltins()
   typeMap["seqboolvec"]= createSequenceTc("seqboolvec","seqboolvec",typeMap["boolvec"]);
   std::list<TypeCodeObjref *> ltc;
   typeMap["pyobj"]= createInterfaceTc("python:obj:1.0","pyobj",ltc);
+  typeMap["seqpyobj"]= createSequenceTc("seqpyobj","seqpyobj",typeMap["pyobj"]);
+  composednodeMap["Bloc"]=createBloc("Bloc");
+  composednodeMap["Switch"]=createSwitch("Switch");
+  composednodeMap["WhileLoop"]=createWhileLoop("WhileLoop");
+  composednodeMap["ForLoop"]=createForLoop("ForLoop");
+  composednodeMap["ForEachLoop_double"]=createForEachLoop("ForEachLoop_double",Runtime::_tc_double);
+  composednodeMap["ForEachLoop_string"]=createForEachLoop("ForEachLoop_string",Runtime::_tc_string);
+  composednodeMap["ForEachLoop_int"]=createForEachLoop("ForEachLoop_int",Runtime::_tc_int);
+  composednodeMap["ForEachLoop_bool"]=createForEachLoop("ForEachLoop_bool",Runtime::_tc_bool);
+  composednodeMap["ForEachLoop_pyobj"]=createForEachLoop("ForEachLoop_pyobj",typeMap["pyobj"]);;
   ENGINE::TypeCodeStruct *t = createStructTc("","Engines/dataref");
   t->addMember("ref",_tc_string);
   typeMap["dataref"]= t;
 }
 
-RuntimeSALOME::RuntimeSALOME(long flags)
+RuntimeSALOME::RuntimeSALOME(long flags, int argc, char* argv[])
 {
   // If all flags (apart the IsPyExt flags) are unset, force them to true
   if ((flags - flags & RuntimeSALOME::IsPyExt) == 0)
@@ -209,7 +223,7 @@ RuntimeSALOME::RuntimeSALOME(long flags)
   if (_usePython) _setOfImplementation.insert(PythonNode::IMPL_NAME);
   if (_useCorba)  _setOfImplementation.insert(CORBANode::IMPL_NAME);
   if (_useXml)    _setOfImplementation.insert(XmlNode::IMPL_NAME);
-  init(flags);
+  init(flags, argc, argv);
 }
 
 RuntimeSALOME::~RuntimeSALOME()
@@ -233,10 +247,12 @@ RuntimeSALOME::~RuntimeSALOME()
  *            bit1 (UseXml)    true if python nodes are needed
  *            bit1 (UseCpp)    true if C++ nodes are needed
  *            bit1 (UseSalome) true if Salome nodes are needed
+ *  \param argc number of command line arguments (used to initialize the Python interpreter)
+ *  \param argv command line arguments (used to initialize the Python interpreter)
  *
  */
 
-void RuntimeSALOME::init(long flags)
+void RuntimeSALOME::init(long flags, int argc, char* argv[])
 {
   bool ispyext = flags & RuntimeSALOME::IsPyExt;
   if (_useCorba)
@@ -270,6 +286,16 @@ void RuntimeSALOME::init(long flags)
 #else
           Py_InitializeEx(0); // do not install signal handlers
 #endif
+          if (argc > 0 && argv != NULL)
+            PySys_SetArgv(argc, argv);
+          else
+            {
+              int pyArgc = 1;
+              char* pyArgv[1];
+              char defaultName[] = "SALOME_YACS_RUNTIME";
+              pyArgv[0] = defaultName;
+              PySys_SetArgv(pyArgc, pyArgv);
+            }
           PyEval_InitThreads(); /* Create (and acquire) the interpreter lock (for threads)*/
           PyEval_SaveThread(); /* Release the thread state */
           //here we do not have the Global Interpreter Lock
@@ -404,7 +430,7 @@ void RuntimeSALOME::fini()
 
 std::string RuntimeSALOME::getVersion() const
 {
-#if YACS_DEVELOPMENT
+#ifdef YACS_DEVELOPMENT
   return CORBA::string_dup(YACS_VERSION_STR"dev");
 #else
   return CORBA::string_dup(YACS_VERSION_STR);
@@ -575,15 +601,19 @@ ComponentInstance* RuntimeSALOME::createComponentInstance(const std::string& nam
     return new SalomePythonComponent(name);
   else if (kind == CppComponent::KIND)
     return new CppComponent(name);
+  else if (kind == SalomeHPComponent::KIND)
+    return new SalomeHPComponent(name);
   std::string msg="Component Instance kind ("+kind+") unknown";
   throw Exception(msg);
 }
 
 Container *RuntimeSALOME::createContainer(const std::string& kind)
 {
-  if(kind == "" || kind == SalomeComponent::KIND)
+  if(kind == "" || kind == SalomeContainer::KIND)
     return new SalomeContainer;
-  else if (kind == CppComponent::KIND)
+  if(kind==SalomeHPContainer::KIND)
+    return new SalomeHPContainer;
+  else if (kind == CppContainer::KIND)
     return new CppContainer;
   std::string msg="Container kind ("+kind+") unknown";
   throw Exception(msg);
@@ -1787,9 +1817,21 @@ std::string RuntimeSALOME::convertNeutralAsString(TypeCode * type, Any *data)
   PyObject* ob;
   if(data)
     {
+      // The call to PyGILState_Ensure was moved here because there was also
+      // a crash when calling convertNeutralPyObject with a sequence of pyobj.
+      // see also the comment below.
+      PyGILState_STATE gstate = PyGILState_Ensure();
       ob=convertNeutralPyObject(type,data);
       std::string s=convertPyObjectToString(ob);
+
+      // Note (Renaud Barate, 8 jan 2013): With Python 2.7, this call to Py_DECREF causes a crash
+      // (SIGSEGV) when ob is a sequence and the call is not protected with the global interpreter
+      // lock. I thus added the call to PyGILState_Ensure / PyGILState_Release. It worked fine in
+      // Python 2.6 without this call. If anyone finds the real reason of this bug and another fix,
+      // feel free to change this code.
+      //PyGILState_STATE gstate = PyGILState_Ensure();
       Py_DECREF(ob);
+      PyGILState_Release(gstate);
       return s;
     }
   else