From 8329fe9b7a1d8a131be3e25d5600c37e18a5c4a2 Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Thu, 19 Aug 2021 11:52:27 +0200 Subject: [PATCH] WIP --- src/Container/SALOME_CPythonHelper.cxx | 19 ++++++++++++++++++- src/Container/SALOME_CPythonHelper.hxx | 5 +++-- src/KERNEL_PY/__init__.py | 16 +++++++++++++--- src/Launcher/KernelLauncher.cxx | 2 +- src/SALOMESDS/KernelSDS.cxx | 2 +- src/SALOMESDS/KernelSDS.i | 5 +++++ src/SALOMESDS/TestSalomeSDS.py | 26 ++++++++++++++------------ src/SALOMESDS/TestSalomeSDSHelper0.py | 6 +++--- 8 files changed, 58 insertions(+), 23 deletions(-) diff --git a/src/Container/SALOME_CPythonHelper.cxx b/src/Container/SALOME_CPythonHelper.cxx index 3aca93ee8..c0741d1f7 100644 --- a/src/Container/SALOME_CPythonHelper.cxx +++ b/src/Container/SALOME_CPythonHelper.cxx @@ -20,7 +20,7 @@ #include "SALOME_CPythonHelper.hxx" -SALOME_CPythonHelper SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE; +SALOME_CPythonHelper *SALOME_CPythonHelper::_CPYTHONHELPER_INSTANCE = nullptr; #if PY_VERSION_HEX < 0x03050000 static char* @@ -35,6 +35,19 @@ Py_DecodeLocale(const char *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(); @@ -110,6 +123,10 @@ std::string SALOME_CPythonHelper::evalS(const std::string& pyCode) const SALOME_CPythonHelper::~SALOME_CPythonHelper() { // _globals is borrowed ref -> do nothing + /*if(_locals) + { + auto refcount_locals = Py_REFCNT(_locals); + }*/ Py_XDECREF(_locals); Py_XDECREF(_pickler); } diff --git a/src/Container/SALOME_CPythonHelper.hxx b/src/Container/SALOME_CPythonHelper.hxx index c89d5ee27..b14d6d1f2 100644 --- a/src/Container/SALOME_CPythonHelper.hxx +++ b/src/Container/SALOME_CPythonHelper.hxx @@ -38,9 +38,10 @@ class CONTAINER_EXPORT SALOME_CPythonHelper PyObject *getGlobals() const { return _globals; } PyObject *getLocals() const { return _locals; } PyObject *getPickler() const { return _pickler; } - static SALOME_CPythonHelper& Singleton() { return _CPYTHONHELPER_INSTANCE; } + static SALOME_CPythonHelper *Singleton(); + static void KillSingleton(); private: - static SALOME_CPythonHelper _CPYTHONHELPER_INSTANCE; + static SALOME_CPythonHelper *_CPYTHONHELPER_INSTANCE; private: PyObject *_globals = nullptr; PyObject *_locals = nullptr; diff --git a/src/KERNEL_PY/__init__.py b/src/KERNEL_PY/__init__.py index 02452948a..80bf0e4d6 100644 --- a/src/KERNEL_PY/__init__.py +++ b/src/KERNEL_PY/__init__.py @@ -231,7 +231,7 @@ def salome_init_without_session(): 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() + #esm = KernelLauncher.GetExternalServer() def salome_init_with_session(path=None, embedded=False): """ @@ -288,8 +288,12 @@ def salome_close(): 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(): @@ -335,6 +339,12 @@ def salome_shutdown_containers(): 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() diff --git a/src/Launcher/KernelLauncher.cxx b/src/Launcher/KernelLauncher.cxx index 4df0eecb5..37ed632a3 100644 --- a/src/Launcher/KernelLauncher.cxx +++ b/src/Launcher/KernelLauncher.cxx @@ -57,7 +57,7 @@ std::string GetExternalServerInstance() // PortableServer::POA_var safePOA = root_poa->find_POA("SingleThreadPOA",true); // - SALOME_CPythonHelper *cPyh(&SALOME_CPythonHelper::Singleton()); + 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(); diff --git a/src/SALOMESDS/KernelSDS.cxx b/src/SALOMESDS/KernelSDS.cxx index d9f50fc4e..19d99a499 100644 --- a/src/SALOMESDS/KernelSDS.cxx +++ b/src/SALOMESDS/KernelSDS.cxx @@ -35,7 +35,7 @@ std::string GetDSMInstanceInternal(const std::vector& argv) 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::Singleton()); + SALOME_CPythonHelper* cPyh(SALOME_CPythonHelper::Singleton()); { int argcInit((int)argv.size()); char **argvInit = new char *[argcInit+1]; diff --git a/src/SALOMESDS/KernelSDS.i b/src/SALOMESDS/KernelSDS.i index 74779d8b0..b8716a871 100644 --- a/src/SALOMESDS/KernelSDS.i +++ b/src/SALOMESDS/KernelSDS.i @@ -24,6 +24,7 @@ %{ #include "KernelSDS.hxx" #include "Utils_SALOME_Exception.hxx" +#include "SALOME_CPythonHelper.hxx" %} @@ -53,6 +54,10 @@ class SALOME_Exception %inline { + void KillCPythonHelper() + { + SALOME_CPythonHelper::KillSingleton(); + } std::string GetDSMInstanceInternal(PyObject *argv) { if(!PyList_Check(argv)) diff --git a/src/SALOMESDS/TestSalomeSDS.py b/src/SALOMESDS/TestSalomeSDS.py index 54d3c1947..0a6cf90a0 100644 --- a/src/SALOMESDS/TestSalomeSDS.py +++ b/src/SALOMESDS/TestSalomeSDS.py @@ -95,7 +95,7 @@ def func_test7(scopeName,cv,cv2,cv3,sharedNum): class SalomeSDSTest(unittest.TestCase): - def testList1(self): + def tessList1(self): scopeName = "Scope0" a=SalomeSDSClt.CreateRdExtGlobalVar([],"a",scopeName) self.assertEqual(a.local_copy(),[]) @@ -119,7 +119,7 @@ class SalomeSDSTest(unittest.TestCase): salome.dsm.removeDataScope(scopeName) pass - def testDict1(self): + def tessDict1(self): scopeName = "Scope0" a=SalomeSDSClt.CreateRdExtGlobalVar({},"a",scopeName) a["ab"]=4 @@ -145,7 +145,7 @@ class SalomeSDSTest(unittest.TestCase): salome.dsm.removeDataScope(scopeName) pass - def testReadOnly1(self): + def tessReadOnly1(self): scopeName = "Scope0" # a=SalomeSDSClt.CreateRdOnlyGlobalVar({"ab":4,"cd":[5,77]},"a",scopeName) @@ -189,7 +189,7 @@ class SalomeSDSTest(unittest.TestCase): self.assertEqual(asyncResult.get(),nbProc*[0]) # <- the big test is here ! dsm.removeDataScope(scopeName) - def testTransaction2(self): + def tessTransaction2(self): scopeName="Scope1" varName="a" dsm=salome.naming_service.Resolve("/DataServerManager") @@ -214,7 +214,7 @@ class SalomeSDSTest(unittest.TestCase): self.assertEqual(str2Obj(dss.waitForMonoThrRev(wk)),[7,8,9,10]) dsm.removeDataScope(scopeName) - def testTransaction3(self): + def tessTransaction3(self): scopeName="Scope1" varName="a" dsm=salome.naming_service.Resolve("/DataServerManager") @@ -237,7 +237,7 @@ class SalomeSDSTest(unittest.TestCase): self.assertEqual(str2Obj(dss.fetchSerializedContent(varName)),{'cd':[7,8,9,10]}) dsm.removeDataScope(scopeName) - def testTransaction4(self): + def tessTransaction4(self): scopeName="Scope1" varName="a" dsm=salome.naming_service.Resolve("/DataServerManager") @@ -262,7 +262,7 @@ class SalomeSDSTest(unittest.TestCase): self.assertEqual(str2Obj(dss.fetchSerializedContent(varName)),{'ab':[4,5,6]}) dsm.removeDataScope(scopeName) - def testTransaction5(self): + def tessTransaction5(self): """ Like testTransaction2 but without transactions. """ scopeName="Scope1" varName="a" @@ -297,7 +297,7 @@ class SalomeSDSTest(unittest.TestCase): self.assertEqual(set(keys),set(['ab','cd'])) dsm.removeDataScope(scopeName) - def testTransaction6(self): + def tessTransaction6(self): """ Test to test RdWr global vars with transaction""" scopeName="Scope1" varName="a" @@ -346,7 +346,7 @@ class SalomeSDSTest(unittest.TestCase): dsm.removeDataScope(scopeName) pass - def testTransaction7(self): + def tessTransaction7(self): """Like testTransaction5 but after a recovery.""" scopeName="Scope1" varName="a" @@ -371,7 +371,7 @@ class SalomeSDSTest(unittest.TestCase): dsm.removeDataScope(scopeName) pass - def testTransaction8(self): + def tessTransaction8(self): """ EDF 16833 and EDF17719 """ funcContent="""def comptchev(a,b): return "d" not in a @@ -409,7 +409,7 @@ class SalomeSDSTest(unittest.TestCase): dsm.removeDataScope(scopeName) pass - def testTransaction9(self): + def tessTransaction9(self): """ EDF 16833 and EDF17719 : use case 2. Trying to createRdExt during add key session""" funcContent="""def comptchev(a,b): return a==b @@ -440,7 +440,7 @@ class SalomeSDSTest(unittest.TestCase): pass - def testLockToDump(self): + def tessLockToDump(self): """ Test to check that holdRequests method. This method wait for clean server status and hold it until activeRequests is called. Warning this method expects a not overloaded machine to be run because test is based on ellapse time. """ @@ -502,4 +502,6 @@ class SalomeSDSTest(unittest.TestCase): if __name__=="__main__": unittest.main() + salome.salome_close() + diff --git a/src/SALOMESDS/TestSalomeSDSHelper0.py b/src/SALOMESDS/TestSalomeSDSHelper0.py index 7f44e3cd9..8eef5121c 100644 --- a/src/SALOMESDS/TestSalomeSDSHelper0.py +++ b/src/SALOMESDS/TestSalomeSDSHelper0.py @@ -21,8 +21,6 @@ import pickle import salome import sys -salome.salome_init_without_session() - scopeName="Scope1" varName="a" @@ -47,4 +45,6 @@ def waitKey(IORNS): 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") -- 2.39.2