Version: 9.16.0
YACS::ENGINE::PythonEntry Class Referenceabstract

#include <PythonNode.hxx>

Inheritance diagram for YACS::ENGINE::PythonEntry:
Collaboration diagram for YACS::ENGINE::PythonEntry:

Static Public Member Functions

static bool IsProxy (PyObject *ob)
 
static bool GetDestroyStatus (PyObject *ob)
 
static void DoNotTouchFileIfProxy (PyObject *ob)
 
static void UnlinkOnDestructorIfProxy (PyObject *ob)
 
static void IfProxyDoSomething (PyObject *ob, const char *meth)
 

Static Public Attributes

static const char SCRIPT_FOR_SIMPLE_SERIALIZATION []
 

Protected Member Functions

 PythonEntry ()
 
 ~PythonEntry ()
 
virtual void assignRemotePyInterpretor (Engines::PyNodeBase_var remoteInterp)=0
 
virtual Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny (Engines::Container_ptr objContainer) const =0
 returns (if any) an object, you have to deal with (UnRegister) More...
 
virtual void createRemoteAdaptedPyInterpretor (Engines::Container_ptr objContainer)=0
 returns an object, you have to deal with (UnRegister) More...
 
virtual Engines::PyNodeBase_var getRemoteInterpreterHandle ()=0
 
virtual const char * getSerializationScript () const =0
 
void commonRemoteLoad (InlineNode *reqNode)
 
void loadRemoteContainer (InlineNode *reqNode)
 
Engines::Container_var loadPythonAdapter (InlineNode *reqNode, bool &isInitializeRequested)
 
void loadRemoteContext (InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
 
virtual bool hasImposedResource () const
 

Static Protected Member Functions

static std::string GetContainerLog (const std::string &mode, Container *container, const Task *askingTask)
 

Protected Attributes

PyObject * _context
 
PyObject * _pyfuncSer
 
PyObject * _pyfuncUnser
 
PyObject * _pyfuncSimpleSer
 
std::string _imposedResource
 
std::string _imposedContainer
 

Static Protected Attributes

static PyObject * _pyClsBigObject = nullptr
 

Detailed Description

Definition at line 37 of file PythonNode.hxx.

Constructor & Destructor Documentation

◆ PythonEntry()

PythonEntry::PythonEntry ( )
protected

Definition at line 93 of file PythonNode.cxx.

◆ ~PythonEntry()

PythonEntry::~PythonEntry ( )
protected

Definition at line 97 of file PythonNode.cxx.

98{
99 AutoGIL agil;
100 DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
101 // not Py_XDECREF of _pyfuncUnser because it is returned by PyDict_GetItem -> borrowed
102 // not Py_XDECREF of _pyfuncSer because it is returned by PyDict_GetItem -> borrowed
103 Py_XDECREF(_context);
104}
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31

References _context, and DEBTRACE.

Member Function Documentation

◆ assignRemotePyInterpretor()

virtual void YACS::ENGINE::PythonEntry::assignRemotePyInterpretor ( Engines::PyNodeBase_var  remoteInterp)
protectedpure virtual

◆ commonRemoteLoad()

void PythonEntry::commonRemoteLoad ( InlineNode reqNode)
protected

Definition at line 318 of file PythonNode.cxx.

319{
320 loadRemoteContainer(reqNode);
321 bool isInitializeRequested;
322 Engines::Container_var objContainer(loadPythonAdapter(reqNode,isInitializeRequested));
323 loadRemoteContext(reqNode,objContainer,isInitializeRequested);
324}
void loadRemoteContext(InlineNode *reqNode, Engines::Container_ptr objContainer, bool isInitializeRequested)
Definition: PythonNode.cxx:195
Engines::Container_var loadPythonAdapter(InlineNode *reqNode, bool &isInitializeRequested)
Definition: PythonNode.cxx:165
void loadRemoteContainer(InlineNode *reqNode)
Definition: PythonNode.cxx:106

References loadPythonAdapter(), loadRemoteContainer(), and loadRemoteContext().

Referenced by YACS::ENGINE::PythonNode::loadRemote(), and YACS::ENGINE::PyFuncNode::loadRemote().

◆ createRemoteAdaptedPyInterpretor()

virtual void YACS::ENGINE::PythonEntry::createRemoteAdaptedPyInterpretor ( Engines::Container_ptr  objContainer)
protectedpure virtual

returns an object, you have to deal with (UnRegister)

Implemented in YACS::ENGINE::PythonNode, and YACS::ENGINE::PyFuncNode.

Referenced by loadPythonAdapter().

◆ DoNotTouchFileIfProxy()

void PythonEntry::DoNotTouchFileIfProxy ( PyObject *  ob)
static

Definition at line 408 of file PythonNode.cxx.

409{
410 IfProxyDoSomething(ob,"doNotTouchFile");
411}
static void IfProxyDoSomething(PyObject *ob, const char *meth)
Definition: PythonNode.cxx:385

References IfProxyDoSomething().

Referenced by YACS::ENGINE::convertToYacsObjref< PYTHONImpl, PyObject *, void *, IMPLOUT, TOUT >::convert().

◆ GetContainerLog()

std::string PythonEntry::GetContainerLog ( const std::string &  mode,
Container container,
const Task askingTask 
)
staticprotected

Definition at line 283 of file PythonNode.cxx.

284{
285 if(mode=="local")
286 return "";
287
288 std::string msg;
289 try
290 {
291 SalomeContainer *containerCast(dynamic_cast<SalomeContainer *>(container));
292 SalomeHPContainer *objContainer2(dynamic_cast<SalomeHPContainer *>(container));
293 if(containerCast)
294 {
295 Engines::Container_var objContainer(containerCast->getContainerPtr(askingTask));
296 CORBA::String_var logname = objContainer->logfilename();
297 DEBTRACE(logname);
298 msg=logname;
299 std::string::size_type pos = msg.find(":");
300 msg=msg.substr(pos+1);
301 }
302 else if(objContainer2)
303 {
304 msg="Remote PythonNode is on HP Container : no log because no info of the location by definition of HP Container !";
305 }
306 else
307 {
308 msg="Not implemented yet for container log for that type of container !";
309 }
310 }
311 catch(...)
312 {
313 msg = "Container no longer reachable";
314 }
315 return msg;
316}

References DEBTRACE, and YACS::ENGINE::SalomeContainer::getContainerPtr().

Referenced by YACS::ENGINE::PythonNode::getContainerLog(), and YACS::ENGINE::PyFuncNode::getContainerLog().

◆ GetDestroyStatus()

bool PythonEntry::GetDestroyStatus ( PyObject *  ob)
static

return true only if ob is a proxy and destroy flag set to true

Definition at line 355 of file PythonNode.cxx.

356{
357 if(!_pyClsBigObject)
358 return false;
359 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
360 {
361 AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,"getDestroyStatus");
362 AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
363 if( PyBool_Check(tmp.get()) )
364 {
365 return tmp.get() == Py_True;
366 }
367 return false;
368 }
369 else
370 {
371 if( PyList_Check( ob ) )
372 {
373 auto sz = PyList_Size( ob );
374 for( auto i = 0 ; i < sz ; ++i )
375 {
376 PyObject *elt = PyList_GetItem( ob, i );
378 return true;
379 }
380 }
381 }
382 return false;
383}
static bool GetDestroyStatus(PyObject *ob)
Definition: PythonNode.cxx:355
static PyObject * _pyClsBigObject
Definition: PythonNode.hxx:68

References _pyClsBigObject, GetDestroyStatus(), and yacsorb.CORBAEngineTest::i.

Referenced by YACS::ENGINE::convertToYacsObjref< PYTHONImpl, PyObject *, void *, IMPLOUT, TOUT >::convert(), and GetDestroyStatus().

◆ getRemoteInterpreterHandle()

virtual Engines::PyNodeBase_var YACS::ENGINE::PythonEntry::getRemoteInterpreterHandle ( )
protectedpure virtual

◆ getSerializationScript()

virtual const char * YACS::ENGINE::PythonEntry::getSerializationScript ( ) const
protectedpure virtual

◆ hasImposedResource()

bool PythonEntry::hasImposedResource ( ) const
protectedvirtual

◆ IfProxyDoSomething()

void PythonEntry::IfProxyDoSomething ( PyObject *  ob,
const char *  meth 
)
static

Definition at line 385 of file PythonNode.cxx.

386{
387 if(!_pyClsBigObject)
388 return ;
389 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
390 {
391 AutoPyRef unlinkOnDestructor = PyObject_GetAttrString(ob,meth);
392 AutoPyRef tmp = PyObject_CallFunctionObjArgs(unlinkOnDestructor,nullptr);
393 }
394 else
395 {
396 if( PyList_Check( ob ) )
397 {
398 auto sz = PyList_Size( ob );
399 for( auto i = 0 ; i < sz ; ++i )
400 {
401 PyObject *elt = PyList_GetItem( ob, i );
403 }
404 }
405 }
406}

References _pyClsBigObject, yacsorb.CORBAEngineTest::i, and IfProxyDoSomething().

Referenced by YACS::ENGINE::convertToYacsObjref< PYTHONImpl, PyObject *, void *, IMPLOUT, TOUT >::convert(), YACS::ENGINE::convertToYacsSequence< PYTHONImpl, PyObject *, void *, IMPLOUT, TOUT >::convert(), DoNotTouchFileIfProxy(), IfProxyDoSomething(), and UnlinkOnDestructorIfProxy().

◆ IsProxy()

bool PythonEntry::IsProxy ( PyObject *  ob)
static

Definition at line 331 of file PythonNode.cxx.

332{
333 if(!_pyClsBigObject)
334 return false;
335 if( PyObject_IsInstance( ob, _pyClsBigObject) == 1 )
336 {
337 return true;
338 }
339 else
340 {
341 if( PyList_Check( ob ) )
342 {
343 auto sz = PyList_Size( ob );
344 for( auto i = 0 ; i < sz ; ++i )
345 {
346 PyObject *elt = PyList_GetItem( ob, i );
347 if( PythonEntry::IsProxy(elt) )
348 return true;
349 }
350 }
351 }
352 return false;
353}
static bool IsProxy(PyObject *ob)
Definition: PythonNode.cxx:331

References _pyClsBigObject, yacsorb.CORBAEngineTest::i, and IsProxy().

Referenced by YACS::ENGINE::convertToYacsSequence< PYTHONImpl, PyObject *, void *, IMPLOUT, TOUT >::convert(), YACS::ENGINE::convertFromYacsObjref< PYTHONImpl, PyObject * >::convert(), and IsProxy().

◆ loadPythonAdapter()

Engines::Container_var PythonEntry::loadPythonAdapter ( InlineNode reqNode,
bool &  isInitializeRequested 
)
protected

Definition at line 165 of file PythonNode.cxx.

166{
167 bool isStandardCont(true);
168 Engines::Container_var objContainer(GetContainerObj(reqNode,isStandardCont));
169 isInitializeRequested=false;
170 try
171 {
172 Engines::PyNodeBase_var dftPyScript(retrieveDftRemotePyInterpretorIfAny(objContainer));
173 if(CORBA::is_nil(dftPyScript))
174 {
175 isInitializeRequested=!isStandardCont;
177 }
178 else
179 assignRemotePyInterpretor(dftPyScript);
180 }
181 catch( const SALOME::SALOME_Exception& ex )
182 {
183 std::string msg="Exception on remote python node creation ";
184 msg += '\n';
185 msg += ex.details.text.in();
186 reqNode->setErrorDetails(msg);
187 throw Exception(msg);
188 }
189 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
190 if(CORBA::is_nil(pynode))
191 throw Exception("In PythonNode the ref in NULL ! ");
192 return objContainer;
193}
Engines::Container_var GetContainerObj(InlineNode *reqNode, bool &isStandardCont)
Definition: PythonNode.cxx:139
virtual void setErrorDetails(const std::string &error)
Definition: Node.hxx:191
virtual Engines::PyNodeBase_var retrieveDftRemotePyInterpretorIfAny(Engines::Container_ptr objContainer) const =0
returns (if any) an object, you have to deal with (UnRegister)
virtual void createRemoteAdaptedPyInterpretor(Engines::Container_ptr objContainer)=0
returns an object, you have to deal with (UnRegister)
virtual Engines::PyNodeBase_var getRemoteInterpreterHandle()=0
virtual void assignRemotePyInterpretor(Engines::PyNodeBase_var remoteInterp)=0

References assignRemotePyInterpretor(), createRemoteAdaptedPyInterpretor(), testCppPluginInvokation::ex, GetContainerObj(), getRemoteInterpreterHandle(), retrieveDftRemotePyInterpretorIfAny(), and YACS::ENGINE::Node::setErrorDetails().

Referenced by commonRemoteLoad(), YACS::ENGINE::PythonNode::executeRemote(), and YACS::ENGINE::PyFuncNode::executeRemote().

◆ loadRemoteContainer()

void PythonEntry::loadRemoteContainer ( InlineNode reqNode)
protected

Definition at line 106 of file PythonNode.cxx.

107{
108 DEBTRACE( "---------------PythonEntry::CommonRemoteLoad function---------------" );
109 Container *container(reqNode->getContainer());
110 bool isContAlreadyStarted(false);
111 if(container)
112 {
113 try
114 {
116 container->start(reqNode, _imposedResource, _imposedContainer);
117 else
118 {
119 isContAlreadyStarted=container->isAlreadyStarted(reqNode);
120 if(!isContAlreadyStarted)
121 container->start(reqNode);
122 }
123 }
124 catch(Exception& e)
125 {
126 reqNode->setErrorDetails(e.what());
127 throw e;
128 }
129 }
130 else
131 {
132 std::string what("PythonEntry::CommonRemoteLoad : a load operation requested on \"");
133 what+=reqNode->getName(); what+="\" with no container specified.";
134 reqNode->setErrorDetails(what);
135 throw Exception(what);
136 }
137}
virtual Container * getContainer()
Definition: InlineNode.cxx:100
const std::string & getName() const
Definition: Node.hxx:125
virtual bool hasImposedResource() const
Definition: PythonNode.cxx:326

References _imposedContainer, _imposedResource, DEBTRACE, YACS::ENGINE::InlineNode::getContainer(), YACS::ENGINE::Node::getName(), hasImposedResource(), YACS::ENGINE::Container::isAlreadyStarted(), YACS::ENGINE::Node::setErrorDetails(), YACS::ENGINE::Container::start(), and YACS::Exception::what().

Referenced by commonRemoteLoad().

◆ loadRemoteContext()

void PythonEntry::loadRemoteContext ( InlineNode reqNode,
Engines::Container_ptr  objContainer,
bool  isInitializeRequested 
)
protected

Definition at line 195 of file PythonNode.cxx.

196{
197 Container *container(reqNode->getContainer());
198 Engines::PyNodeBase_var pynode(getRemoteInterpreterHandle());
200 {
201#if PY_VERSION_HEX < 0x03070000
202 std::unique_lock<std::mutex> lock(data_mutex);
203#endif
204 AutoGIL agil;
205 const char *picklizeScript(getSerializationScript());
206 PyObject *res=PyRun_String(picklizeScript,Py_file_input,_context,_context);
207 PyObject *res2(PyRun_String(SCRIPT_FOR_SIMPLE_SERIALIZATION,Py_file_input,_context,_context));
208 if(res == NULL || res2==NULL)
209 {
210 std::string errorDetails;
211 PyObject* new_stderr = newPyStdOut(errorDetails);
212 reqNode->setErrorDetails(errorDetails);
213 PySys_SetObject((char*)"stderr", new_stderr);
214 PyErr_Print();
215 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
216 Py_DECREF(new_stderr);
217 throw Exception("Error during load");
218 }
219 Py_DECREF(res); Py_DECREF(res2);
220 AutoPyRef res3(PyRun_String(SCRIPT_FOR_BIGOBJECT,Py_file_input,_context,_context));
221 _pyfuncSer=PyDict_GetItemString(_context,"pickleForDistPyth2009");
222 _pyfuncUnser=PyDict_GetItemString(_context,"unPickleForDistPyth2009");
223 _pyfuncSimpleSer=PyDict_GetItemString(_context,"pickleForVarSimplePyth2009");
224 if(! _pyClsBigObject )
225 {
226 _pyClsBigObject=PyDict_GetItemString(_context,"BigObjectOnDiskBase");
227 Py_INCREF(_pyClsBigObject);
228 }
229 if(_pyfuncSer == NULL)
230 {
231 std::string errorDetails;
232 PyObject *new_stderr(newPyStdOut(errorDetails));
233 reqNode->setErrorDetails(errorDetails);
234 PySys_SetObject((char*)"stderr", new_stderr);
235 PyErr_Print();
236 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
237 Py_DECREF(new_stderr);
238 throw Exception("Error during load");
239 }
240 if(_pyfuncUnser == NULL)
241 {
242 std::string errorDetails;
243 PyObject *new_stderr(newPyStdOut(errorDetails));
244 reqNode->setErrorDetails(errorDetails);
245 PySys_SetObject((char*)"stderr", new_stderr);
246 PyErr_Print();
247 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
248 Py_DECREF(new_stderr);
249 throw Exception("Error during load");
250 }
251 if(_pyfuncSimpleSer == NULL)
252 {
253 std::string errorDetails;
254 PyObject *new_stderr(newPyStdOut(errorDetails));
255 reqNode->setErrorDetails(errorDetails);
256 PySys_SetObject((char*)"stderr", new_stderr);
257 PyErr_Print();
258 PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
259 Py_DECREF(new_stderr);
260 throw Exception("Error during load");
261 }
262 }
263 if(isInitializeRequested)
264 {//This one is called only once at initialization in the container if an init-script is specified.
265 try
266 {
267 std::string zeInitScriptKey(container->getProperty(HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY));
268 if(!zeInitScriptKey.empty())
269 pynode->executeAnotherPieceOfCode(zeInitScriptKey.c_str());
270 }
271 catch( const SALOME::SALOME_Exception& ex )
272 {
273 std::string msg="Exception on PythonNode::loadRemote python invocation of initializisation py script !";
274 msg += '\n';
275 msg += ex.details.text.in();
276 reqNode->setErrorDetails(msg);
277 throw Exception(msg);
278 }
279 DEBTRACE( "---------------End PyNode::loadRemote function---------------" );
280 }
281}
static char SCRIPT_FOR_BIGOBJECT[]
Definition: PythonNode.cxx:84
static std::mutex data_mutex
Definition: PythonNode.cxx:90
virtual const char * getSerializationScript() const =0
static const char SCRIPT_FOR_SIMPLE_SERIALIZATION[]
Definition: PythonNode.hxx:72
PyObject * newPyStdOut(std::string &out)
Definition: PyStdout.cxx:129

References _context, _pyClsBigObject, _pyfuncSer, _pyfuncSimpleSer, _pyfuncUnser, data_mutex, DEBTRACE, testCppPluginInvokation::ex, YACS::ENGINE::InlineNode::getContainer(), YACS::ENGINE::Container::getProperty(), getRemoteInterpreterHandle(), getSerializationScript(), YACS::ENGINE::HomogeneousPoolContainer::INITIALIZE_SCRIPT_KEY, YACS::ENGINE::newPyStdOut(), SCRIPT_FOR_BIGOBJECT, SCRIPT_FOR_SIMPLE_SERIALIZATION, and YACS::ENGINE::Node::setErrorDetails().

Referenced by commonRemoteLoad().

◆ retrieveDftRemotePyInterpretorIfAny()

virtual Engines::PyNodeBase_var YACS::ENGINE::PythonEntry::retrieveDftRemotePyInterpretorIfAny ( Engines::Container_ptr  objContainer) const
protectedpure virtual

returns (if any) an object, you have to deal with (UnRegister)

Implemented in YACS::ENGINE::PythonNode, and YACS::ENGINE::PyFuncNode.

Referenced by loadPythonAdapter().

◆ UnlinkOnDestructorIfProxy()

Member Data Documentation

◆ _context

◆ _imposedContainer

std::string YACS::ENGINE::PythonEntry::_imposedContainer
protected

◆ _imposedResource

std::string YACS::ENGINE::PythonEntry::_imposedResource
protected

◆ _pyClsBigObject

PyObject * PythonEntry::_pyClsBigObject = nullptr
staticprotected

Definition at line 68 of file PythonNode.hxx.

Referenced by GetDestroyStatus(), IfProxyDoSomething(), IsProxy(), and loadRemoteContext().

◆ _pyfuncSer

PyObject* YACS::ENGINE::PythonEntry::_pyfuncSer
protected

◆ _pyfuncSimpleSer

PyObject* YACS::ENGINE::PythonEntry::_pyfuncSimpleSer
protected

Definition at line 67 of file PythonNode.hxx.

Referenced by YACS::ENGINE::PythonNode::applyDPLScope(), and loadRemoteContext().

◆ _pyfuncUnser

PyObject* YACS::ENGINE::PythonEntry::_pyfuncUnser
protected

◆ SCRIPT_FOR_SIMPLE_SERIALIZATION

const char PythonEntry::SCRIPT_FOR_SIMPLE_SERIALIZATION
static
Initial value:
="import pickle\n"
"def pickleForVarSimplePyth2009(val):\n"
" return pickle.dumps(val,-1)\n"
"\n"

Definition at line 72 of file PythonNode.hxx.

Referenced by loadRemoteContext().


The documentation for this class was generated from the following files: