#include "SALOME_CPythonHelper.hxx"
+SALOME_CPythonHelper *SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE = nullptr;
+
#if PY_VERSION_HEX < 0x03050000
static char*
Py_EncodeLocale(const wchar_t *arg, size_t *size)
}
#endif
+SALOME_CPythonHelper *SALOME_CPythonHelper::Singleton()
+{
+ if(!_CPYTHONHELPER_INSTANCE)
+ _CPYTHONHELPER_INSTANCE = new SALOME_CPythonHelper;
+ return _CPYTHONHELPER_INSTANCE;
+}
+
+void SALOME_CPythonHelper::KillSingleton()
+{
+ delete _CPYTHONHELPER_INSTANCE;
+ _CPYTHONHELPER_INSTANCE = nullptr;
+}
+
void SALOME_CPythonHelper::initializePython(int argc, char *argv[])
{
Py_Initialize();
return ret;
}
+
SALOME_CPythonHelper::~SALOME_CPythonHelper()
{
// _globals is borrowed ref -> do nothing
+
+ /*if(_locals){ auto refcount_locals = Py_REFCNT(_locals); }*/
+
Py_XDECREF(_locals);
Py_XDECREF(_pickler);
}
PyObject *getGlobals() const { return _globals; }
PyObject *getLocals() const { return _locals; }
PyObject *getPickler() const { return _pickler; }
+ static SALOME_CPythonHelper *Singleton();
+ static void KillSingleton();
+ private:
+ static SALOME_CPythonHelper *_CPYTHONHELPER_INSTANCE;
private:
PyObject *_globals = nullptr;
PyObject *_locals = nullptr;
return self._rm
def salome_init_without_session():
- global lcc,naming_service,myStudy,orb,modulcat,sg,cm,dsm
+ global lcc,naming_service,myStudy,orb,modulcat,sg,cm,dsm,esm
import KernelBasis
KernelBasis.setSSLMode(True)
import KernelDS
from KernelSDS import GetDSMInstance
import sys
dsm = GetDSMInstance(sys.argv)
+ # esm inherits from SALOME_CPythonHelper singleton already initialized by GetDSMInstance
+ # esm inherits also from SALOME_ResourcesManager creation/initialization (concerning SingleThreadPOA POA) when KernelLauncher.GetContainerManager() has been called
+ esm = KernelLauncher.GetExternalServer()
def salome_init_with_session(path=None, embedded=False):
"""
salome_iapp_close()
salome_study_close()
myStudy, myStudyName = None, None
- import KernelDS
- KernelDS.KillGlobalSessionInstance()
+ import KernelBasis
+ if KernelBasis.getSSLMode():
+ import KernelDS
+ KernelDS.KillGlobalSessionInstance()
+ import KernelSDS
+ KernelSDS.KillCPythonHelper()
pass
def salome_NS():
naming_service.Destroy_Name(ref_in_ns)
print("Number of containers in NS after clean : {}".format( len( list(salome_walk_on_containers(ns,[""])) )))
+class SessionContextManager:
+ def __enter__(self):
+ standalone()
+ salome_init()
+ def __exit__(self, type, value, traceback):
+ salome_close()
#to expose all objects to pydoc
__all__=dir()
#include "SALOME_Fake_NamingService.hxx"
#include "SALOME_KernelServices.hxx"
#include "SALOME_ResourcesManager.hxx"
+#include "SALOME_ExternalServerLauncher.hxx"
+#include "SALOME_CPythonHelper.hxx"
#include <cstring>
CORBA::String_var ior = orb->object_to_string(cm);
return std::string(ior.in());
}
+
+std::string GetExternalServerInstance()
+{
+ CORBA::ORB_ptr orb = KERNEL::getORB();
+ CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
+ PortableServer::POA_var root_poa = PortableServer::POA::_narrow(obj);
+ //
+ PortableServer::POA_var safePOA = root_poa->find_POA("SingleThreadPOA",true);
+ //
+ SALOME_CPythonHelper *cPyh(SALOME_CPythonHelper::Singleton());
+ SALOME_Fake_NamingService *ns = new SALOME_Fake_NamingService;
+ SALOME_ExternalServerLauncher *esm(new SALOME_ExternalServerLauncher(cPyh,orb,safePOA,ns));
+ esm->_remove_ref();
+ //
+ CORBA::Object_var esmPtr = safePOA->servant_to_reference(esm);
+ SALOME::ExternalServerLauncher_var esmCPtr = SALOME::ExternalServerLauncher::_narrow(esmPtr);
+ //
+ CORBA::String_var ior = orb->object_to_string(esmCPtr);
+ return std::string(ior.in());
+}
std::string GetContainerManagerInstance();
std::string GetResourcesManagerInstance();
+std::string GetExternalServerInstance();
{
std::string GetContainerManagerInstance();
std::string GetResourcesManagerInstance();
+ std::string GetExternalServerInstance();
}
%pythoncode %{
import CORBA
orb=CORBA.ORB_init([''])
return orb.string_to_object(GetResourcesManagerInstance())
+
+def GetExternalServer():
+ import SALOME
+ import CORBA
+ orb=CORBA.ORB_init([''])
+ return orb.string_to_object(GetExternalServerInstance())
%}
unsigned SALOME_ExternalServerHandler::CNT = 0;
-SALOME_ExternalServerHandler::SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService *ns, long pid):_name(name),_pid(pid),_NS(ns),_boss(boss)
+SALOME_ExternalServerHandler::SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService_Abstract *ns, long pid):_name(name),_pid(pid),_NS(ns),_boss(boss)
{
}
#include <string>
-class SALOME_NamingService;
+class SALOME_NamingService_Abstract;
class SALOME_ExternalServerLauncher;
class SALOME_CPythonHelper;
class SALOMELAUNCHER_EXPORT SALOME_ExternalServerHandler : public POA_SALOME::ExternalServerHandler
{
public:
- SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService *ns, long pid);
+ SALOME_ExternalServerHandler(SALOME_ExternalServerLauncher *boss, const std::string& name, SALOME_NamingService_Abstract *ns, long pid);
virtual ~SALOME_ExternalServerHandler();
void registerToKill(const SALOME_CPythonHelper *pyHelper) const;
static void KillPID(long pid);
private:
std::string _name;
long _pid;
- SALOME_NamingService *_NS;
+ SALOME_NamingService_Abstract *_NS;
SALOME_ExternalServerLauncher *_boss;
static unsigned CNT;
};
unsigned SALOME_ExternalServerLauncher::CNT = 0;
-SALOME_ExternalServerLauncher::SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa):_pyHelper(pyHelper),_poa(poa)
+SALOME_ExternalServerLauncher::SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa,SALOME_NamingService_Abstract *ns):_pyHelper(pyHelper),_poa(poa)
{
- _NS = new SALOME_NamingService(orb);
+ _NS = ns == nullptr ? new SALOME_NamingService(orb) : ns;
PortableServer::ObjectId_var id(_poa->activate_object(this));
CORBA::Object_var obj(_poa->id_to_reference(id));
SALOME::ExternalServerLauncher_var refPtr(SALOME::ExternalServerLauncher::_narrow(obj));
return ret.release();
}
-std::vector<std::string> SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService *ns)
+std::vector<std::string> SALOME_ExternalServerLauncher::ListOfExternalServersCpp(SALOME_NamingService_Abstract *ns)
{
ns->Change_Directory(NAME_IN_NS);
std::vector<std::string> ret(ns->list_directory());
return ret;
}
-bool SALOME_ExternalServerLauncher::IsAliveAndKicking(SALOME_NamingService *ns, const std::string& serverName)
+bool SALOME_ExternalServerLauncher::IsAliveAndKicking(SALOME_NamingService_Abstract *ns, const std::string& serverName)
{
SALOME::ExternalServerHandler_var pt(GetServerHandlerGivenName(ns, serverName));
if( CORBA::is_nil(pt) )
return IsAliveAndKicking(pt);
}
-SALOME::ExternalServerHandler_var SALOME_ExternalServerLauncher::GetServerHandlerGivenName(SALOME_NamingService *ns, const std::string& serverName)
+SALOME::ExternalServerHandler_var SALOME_ExternalServerLauncher::GetServerHandlerGivenName(SALOME_NamingService_Abstract *ns, const std::string& serverName)
{
std::vector<std::string> serverNames(ListOfExternalServersCpp(ns));
if(std::find(serverNames.begin(),serverNames.end(),serverName)==serverNames.end())
#include <vector>
#include <string>
-class SALOME_NamingService;
+class SALOME_NamingService_Abstract;
class SALOME_CPythonHelper;
class SALOMELAUNCHER_EXPORT SALOME_ExternalServerLauncher : public POA_SALOME::ExternalServerLauncher
{
public:
- SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa);
+ SALOME_ExternalServerLauncher(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_var poa, SALOME_NamingService_Abstract *ns = nullptr);
virtual ~SALOME_ExternalServerLauncher();
public:
SALOME::ExternalServerHandler_ptr launchServer(const char *server_name, const char *working_dir, const SALOME::CmdList& command_list ) override;
const SALOME_CPythonHelper *getPyHelper() const { return _pyHelper; }
private:
static std::string CreateAbsNameInNSFromServerName(const std::string& scopeName);
- static std::vector<std::string> ListOfExternalServersCpp(SALOME_NamingService *ns);
+ static std::vector<std::string> ListOfExternalServersCpp(SALOME_NamingService_Abstract *ns);
static bool IsAliveAndKicking(SALOME::ExternalServerHandler_ptr server);
- static bool IsAliveAndKicking(SALOME_NamingService *ns, const std::string& serverName);
- static SALOME::ExternalServerHandler_var GetServerHandlerGivenName(SALOME_NamingService *ns, const std::string& serverName);
+ static bool IsAliveAndKicking(SALOME_NamingService_Abstract *ns, const std::string& serverName);
+ static SALOME::ExternalServerHandler_var GetServerHandlerGivenName(SALOME_NamingService_Abstract *ns, const std::string& serverName);
private:
const SALOME_CPythonHelper *_pyHelper = nullptr;
- SALOME_NamingService *_NS = nullptr;
+ SALOME_NamingService_Abstract *_NS = nullptr;
PortableServer::POA_var _poa;
static unsigned CNT;
std::vector<long> _list_of_pids_to_kill;
CORBA::Object_var obj = orb->resolve_initial_references("RootPOA");
if(!CORBA::is_nil(obj))
root_poa = PortableServer::POA::_narrow(obj);
- SALOME_CPythonHelper cPyh;
+ SALOME_CPythonHelper* cPyh(SALOME_CPythonHelper::Singleton());
{
int argcInit((int)argv.size());
char **argvInit = new char *[argcInit+1];
argvInit[argcInit] = nullptr;
for(int i = 0 ; i < argcInit ; ++i)
argvInit[i] = strdup(argv[i].c_str());
- cPyh.initializePython(argcInit,argvInit);
+ cPyh->initializePython(argcInit,argvInit);
for(int i = 0 ; i < argcInit ; ++i)
free(argvInit[i]);
delete [] argvInit;
}
SALOME_Fake_NamingService *ns(new SALOME_Fake_NamingService);
- SALOMESDS::DataServerManager *dsm(new SALOMESDS::DataServerManager(&cPyh,orb,root_poa,ns));
+ SALOMESDS::DataServerManager *dsm(new SALOMESDS::DataServerManager(cPyh,orb,root_poa,ns));
dsm->_remove_ref();
CORBA::Object_var objRef = ns->Resolve(SALOMESDS::DataServerManager::NAME_IN_NS);
_dsm_singleton = SALOME::DataServerManager::_narrow(objRef);
%{
#include "KernelSDS.hxx"
#include "Utils_SALOME_Exception.hxx"
+#include "SALOME_CPythonHelper.hxx"
%}
%inline
{
+ void KillCPythonHelper()
+ {
+ SALOME_CPythonHelper::KillSingleton();
+ }
std::string GetDSMInstanceInternal(PyObject *argv)
{
if(!PyList_Check(argv))
p.join()
dsm.removeDataScope(scopeName)
pass
-
- def setUp(self):
- salome.salome_init_without_session()
- pass
pass
if __name__=="__main__":
- unittest.main()
+ with salome.SessionContextManager():
+ unittest.main()
+
import salome
import sys
-salome.salome_init_without_session()
-
scopeName="Scope1"
varName="a"
if __name__=="__main__":
IORNS = sys.argv[-1]
- sys.exit(not waitKey(IORNS))
+ with salome.SessionContextManager():
+ if waitKey(IORNS) is not True:
+ raise AssertionError("Not the expected result")