]> SALOME platform Git repositories - modules/yacs.git/commitdiff
Salome HOME
Merge remote branch 'origin/V7_dev'
authorvsr <vsr@opencascade.com>
Thu, 14 Apr 2016 07:32:40 +0000 (10:32 +0300)
committervsr <vsr@opencascade.com>
Thu, 14 Apr 2016 07:32:40 +0000 (10:32 +0300)
src/evalyfx/YACSEvalSession.cxx
src/evalyfx/YACSEvalSession.hxx
src/evalyfx/YACSEvalYFXPattern.cxx
src/evalyfx_swig/evalyfx.i

index b4f1e2a5bf0fbc3cdb41084962f8ba2ecbc5d7e8..f89f076c0351bc2b22dc526227f5ea2a44a10e71 100644 (file)
 #include "YACSEvalSession.hxx"
 #include "YACSEvalSessionInternal.hxx"
 
+#include "AutoGIL.hxx"
 #include "Exception.hxx"
 
 #include <Python.h>
 
+#include <sstream>
+
 const char YACSEvalSession::KERNEL_ROOT_DIR[]="KERNEL_ROOT_DIR";
 
 const char YACSEvalSession::CORBA_CONFIG_ENV_VAR_NAME[]="OMNIORB_CONFIG";
 
-YACSEvalSession::YACSEvalSession():_isLaunched(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
+const char YACSEvalSession::NSPORT_VAR_NAME[]="NSPORT";
+
+YACSEvalSession::YACSEvalSession():_isAttached(false),_isLaunched(false),_port(-1),_salomeInstanceModule(0),_salomeInstance(0),_internal(new YACSEvalSessionInternal)
 {
   if(!Py_IsInitialized())
     Py_Initialize();
   //
-  _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
+  {
+    YACS::ENGINE::AutoGIL gal;
+    _salomeInstanceModule=PyImport_ImportModule(const_cast<char *>("salome_instance"));
+  }
 }
 
 YACSEvalSession::~YACSEvalSession()
 {
   delete _internal;
-  if(isLaunched())
+  YACS::ENGINE::AutoGIL gal;
+  if(isLaunched() && !isAttached())
     {
-      PyObject *terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
-      PyObject *res(PyObject_CallObject(terminateSession,0));
-      Py_XDECREF(res);
-      Py_XDECREF(terminateSession);
+      YACS::ENGINE::AutoPyRef terminateSession(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("stop")));//new
+      YACS::ENGINE::AutoPyRef res(PyObject_CallObject(terminateSession,0));
     }
   Py_XDECREF(_salomeInstance);
   Py_XDECREF(_salomeInstanceModule);
@@ -55,31 +62,30 @@ void YACSEvalSession::launch()
 {
   if(isLaunched())
     return ;
+  YACS::ENGINE::AutoGIL gal;
   PyObject *salomeInstance(PyObject_GetAttrString(_salomeInstanceModule,const_cast<char *>("SalomeInstance")));//new
   PyObject *startMeth(PyObject_GetAttrString(salomeInstance,const_cast<char *>("start")));
   Py_XDECREF(salomeInstance);
-  PyObject *myArgs(PyTuple_New(0));//new
-  PyObject *myKWArgs(PyDict_New());//new
+  YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(0));//new
+  YACS::ENGINE::AutoPyRef myKWArgs(PyDict_New());//new
   PyDict_SetItemString(myKWArgs,"shutdown_servers",Py_True);//Py_True ref not stolen
   _salomeInstance=PyObject_Call(startMeth,myArgs,myKWArgs);//new
-  PyObject *getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
-  PyObject *portPy(PyObject_CallObject(getPortMeth,0));//new
+  YACS::ENGINE::AutoPyRef getPortMeth(PyObject_GetAttrString(_salomeInstance,const_cast<char *>("get_port")));//new
+  YACS::ENGINE::AutoPyRef portPy(PyObject_CallObject(getPortMeth,0));//new
   _port=PyInt_AsLong(portPy);
-  Py_XDECREF(portPy);
-  Py_XDECREF(getPortMeth);
-  Py_XDECREF(myKWArgs);
-  Py_XDECREF(myArgs);
   //
-  PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
-  PyObject *environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
-  PyObject *corbaConfigStr(PyString_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
-  PyObject *corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
-  _corbaConfigFileName=PyString_AsString(corbaConfigFileNamePy);
-  Py_XDECREF(corbaConfigFileNamePy);
-  Py_XDECREF(corbaConfigStr);
-  Py_XDECREF(environPy);
-  Py_XDECREF(osPy);
-  _isLaunched=true;
+  int dummy;
+  _corbaConfigFileName=GetConfigAndPort(dummy);
+  _isAttached=false; _isLaunched=true;
+}
+
+void YACSEvalSession::launchUsingCurrentSession()
+{
+  if(isLaunched())
+    return ;
+  YACS::ENGINE::AutoGIL gal;
+  _corbaConfigFileName=GetConfigAndPort(_port);
+  _isAttached=true; _isLaunched=true;
 }
 
 void YACSEvalSession::checkLaunched() const
@@ -103,31 +109,42 @@ std::string YACSEvalSession::getCorbaConfigFileName() const
 std::string YACSEvalSession::GetPathToAdd()
 {
   std::string ret;
-  PyObject *osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
+  YACS::ENGINE::AutoGIL gal;
+  YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
   PyObject *kernelRootDir(0);// os.environ["KERNEL_ROOT_DIR"]
   {
-    PyObject *environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
-    PyObject *kernelRootDirStr(PyString_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
+    YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
+    YACS::ENGINE::AutoPyRef kernelRootDirStr(PyString_FromString(const_cast<char *>(KERNEL_ROOT_DIR)));//new
     kernelRootDir=PyObject_GetItem(environPy,kernelRootDirStr);//new
-    Py_XDECREF(kernelRootDirStr);
-    Py_XDECREF(environPy);
   }
   {
-    PyObject *pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
-    PyObject *joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
-    PyObject *myArgs(PyTuple_New(4));
+    YACS::ENGINE::AutoPyRef pathPy(PyObject_GetAttrString(osPy,const_cast<char *>("path")));//new
+    YACS::ENGINE::AutoPyRef joinPy(PyObject_GetAttrString(pathPy,const_cast<char *>("join")));//new
+    YACS::ENGINE::AutoPyRef myArgs(PyTuple_New(4));
     Py_XINCREF(kernelRootDir); PyTuple_SetItem(myArgs,0,kernelRootDir);
     PyTuple_SetItem(myArgs,1,PyString_FromString(const_cast<char *>("bin")));
     PyTuple_SetItem(myArgs,2,PyString_FromString(const_cast<char *>("salome")));
     PyTuple_SetItem(myArgs,3,PyString_FromString(const_cast<char *>("appliskel")));
-    PyObject *res(PyObject_CallObject(joinPy,myArgs));
+    YACS::ENGINE::AutoPyRef res(PyObject_CallObject(joinPy,myArgs));
     ret=PyString_AsString(res);
-    Py_XDECREF(res);
-    Py_XDECREF(myArgs);
-    Py_XDECREF(joinPy);
-    Py_XDECREF(pathPy);
   }
   Py_XDECREF(kernelRootDir);
-  Py_XDECREF(osPy);
+  return ret;
+}
+
+std::string YACSEvalSession::GetConfigAndPort(int& port)
+{
+  YACS::ENGINE::AutoPyRef osPy(PyImport_ImportModule(const_cast<char *>("os")));//new
+  YACS::ENGINE::AutoPyRef environPy(PyObject_GetAttrString(osPy,const_cast<char *>("environ")));//new
+  //
+  YACS::ENGINE::AutoPyRef corbaConfigStr(PyString_FromString(const_cast<char *>(CORBA_CONFIG_ENV_VAR_NAME)));//new
+  YACS::ENGINE::AutoPyRef corbaConfigFileNamePy(PyObject_GetItem(environPy,corbaConfigStr));//new
+  std::string ret(PyString_AsString(corbaConfigFileNamePy));
+  //
+  YACS::ENGINE::AutoPyRef nsPortStr(PyString_FromString(const_cast<char *>(NSPORT_VAR_NAME)));//new
+  YACS::ENGINE::AutoPyRef nsPortValuePy(PyObject_GetItem(environPy,nsPortStr));//new
+  std::string portStr(PyString_AsString(nsPortValuePy));
+  std::istringstream iss(portStr);
+  iss >> port;
   return ret;
 }
index a58c340114943962d6eb66e35bfc8fdd273caa22..1b0f60414d485556093b1a7d317fd731ab18706d 100644 (file)
@@ -36,7 +36,9 @@ public:
   YACSEVALYFX_EXPORT YACSEvalSession();
   YACSEVALYFX_EXPORT ~YACSEvalSession();
   YACSEVALYFX_EXPORT void launch();
+  YACSEVALYFX_EXPORT void launchUsingCurrentSession();
   YACSEVALYFX_EXPORT bool isLaunched() const { return _isLaunched; }
+  YACSEVALYFX_EXPORT bool isAttached() const { return _isAttached; }
   YACSEVALYFX_EXPORT void checkLaunched() const;
   YACSEVALYFX_EXPORT int getPort() const;
   YACSEVALYFX_EXPORT std::string getCorbaConfigFileName() const;
@@ -44,10 +46,13 @@ public:
   YACSEvalSessionInternal *getInternal() { checkLaunched(); return _internal; }
 private:
   static std::string GetPathToAdd();
+  static std::string GetConfigAndPort(int& port);
 public:
   static const char KERNEL_ROOT_DIR[];
   static const char CORBA_CONFIG_ENV_VAR_NAME[];
+  static const char NSPORT_VAR_NAME[];
 private:
+  bool _isAttached;
   bool _isLaunched;
   int _port;
   std::string _corbaConfigFileName;
index 3c00612bf3222eacf19f6cd3090d916a2ba013f6..6de18f31fc5dff0696a7cd6131cf0f49dd55b495 100644 (file)
@@ -71,9 +71,10 @@ const char YACSEvalYFXGraphGen::GATHER_NODE_NAME[]="__gather__";
 class MyAutoThreadSaver
 {
 public:
-  MyAutoThreadSaver():_save(PyEval_SaveThread()) { }
-  ~MyAutoThreadSaver() { PyEval_RestoreThread(_save); }
+  MyAutoThreadSaver(bool isToSave):_isToSave(isToSave),_save(0) { if(_isToSave) _save=PyEval_SaveThread(); }
+  ~MyAutoThreadSaver() { if(_isToSave) PyEval_RestoreThread(_save); }
 private:
+  bool _isToSave;
   PyThreadState *_save;
 };
 
@@ -796,7 +797,7 @@ bool YACSEvalYFXGraphGenInteractive::go(bool stopASAP, YACSEvalSession *session)
   YACS::ENGINE::Executor exe;
   exe.setKeepGoingProperty(!stopASAP);
   {
-    MyAutoThreadSaver locker;
+    MyAutoThreadSaver locker(!session->isAttached());
     exe.RunW(getUndergroundGeneratedGraph());
   }
   return getUndergroundGeneratedGraph()->getState()==YACS::DONE;
index 3c2c12e4aa25ce5902e3384cc7f07fdf1d1665be..3297d0bc524b7bc1580246ab1b55ec2a5b36c85d 100644 (file)
@@ -391,6 +391,7 @@ public:
   ~YACSEvalSession();
   void launch();
   bool isLaunched() const;
+  bool isAttached() const;
   void checkLaunched() const;
   int getPort() const;
   std::string getCorbaConfigFileName() const;