From daac8ec6dc0403e20396d0be0da4feac22dda28d Mon Sep 17 00:00:00 2001 From: prascle Date: Fri, 13 May 2005 11:31:42 +0000 Subject: [PATCH] PR: C++ container used as a Python container --- src/Container/Container_i.cxx | 111 ++++++++++++++++++++++------ src/Container/SALOME_Container.py | 2 + src/TestContainer/TestContainer.cxx | 13 ++++ 3 files changed, 103 insertions(+), 23 deletions(-) diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index a4a0d12c6..dd2caf3b1 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -270,34 +270,63 @@ void Engines_Container_i::Shutdown() bool Engines_Container_i::load_component_Library(const char* componentName) { - string impl_name = string ("lib") + componentName + string("Engine.so"); - SCRUTE(impl_name); - _numInstanceMutex.lock(); // lock to be alone - // (see decInstanceCnt, finalize_removal)) - if (_toRemove_map[impl_name]) _toRemove_map.erase(impl_name); - if (_library_map[impl_name]) + string aCompName = componentName; + if (_library_map[aCompName]) { - MESSAGE("Library " << impl_name << " already loaded"); - _numInstanceMutex.unlock(); - return true; - } - void* handle; - handle = dlopen( impl_name.c_str() , RTLD_LAZY ) ; - if ( !handle ) - { - INFOS("Can't load shared library : " << impl_name); - INFOS("error dlopen: " << dlerror()); - _numInstanceMutex.unlock(); - return false; + return true; // Python Component, already imported } else { - _library_map[impl_name] = handle; - _numInstanceMutex.unlock(); - return true; + PyEval_RestoreThread(gtstate); + PyObject *mainmod = PyImport_AddModule("__main__"); + PyObject *globals = PyModule_GetDict(mainmod); + PyObject *pyCont = PyDict_GetItemString(globals, "pyCont"); + PyObject *result = PyObject_CallMethod(pyCont, + "import_component", + "s",componentName); + int ret= PyInt_AsLong(result); + SCRUTE(ret); + PyEval_ReleaseThread(gtstate); + + if (ret) // import possible: Python component + { + _library_map[aCompName] = (void *)pyCont; // any non O value OK + MESSAGE("import Python: "<string_to_object(iors.c_str()); + iobject = Engines::Component::_narrow( obj ) ; + return iobject._retn(); + } + + //--- try C++ + string impl_name = string ("lib") + genericRegisterName +string("Engine.so"); void* handle = _library_map[impl_name]; if ( !handle ) @@ -331,7 +397,6 @@ Engines_Container_i::create_component_instance(const char*genericRegisterName, } else { - Engines::Component_var iobject = Engines::Component::_nil() ; iobject = createInstance(genericRegisterName, handle, studyId); diff --git a/src/Container/SALOME_Container.py b/src/Container/SALOME_Container.py index f1bfd8290..fc4f468a5 100644 --- a/src/Container/SALOME_Container.py +++ b/src/Container/SALOME_Container.py @@ -75,6 +75,8 @@ class SALOME_Container_i: print "import ",componentName," successful" ret=1 except: + import traceback + traceback.print_exc() print "import ",componentName," not possible" return ret diff --git a/src/TestContainer/TestContainer.cxx b/src/TestContainer/TestContainer.cxx index 2e0c484b8..d0e0fe234 100644 --- a/src/TestContainer/TestContainer.cxx +++ b/src/TestContainer/TestContainer.cxx @@ -147,6 +147,19 @@ int main (int argc, char * argv[]) iGenFact->remove_impl(anInstance) ; //iGenFact->finalize_removal() ; // unpredictable results ... } + MESSAGE("------------------------------- PYTHON "); + { + bool isLib = + iGenFact->load_component_Library("SALOME_TestComponentPy"); + ASSERT(isLib); + CORBA::Object_var obj = + iGenFact->create_component_instance("SALOME_TestComponentPy", + 0); + Engines::TestComponent_var anInstance = + Engines::TestComponent::_narrow(obj); + MESSAGE("create anInstance"); + SCRUTE(anInstance->instanceName()); + } // Clean-up. iGenFact->finalize_removal() ; -- 2.39.2