#include "py2yacs.hxx"
#include "Proc.hxx"
#include "Executor.hxx"
+#include "PythonCppUtils.hxx"
#include "RuntimeSALOME.hxx"
#include "PythonPorts.hxx"
// verify the output port value
YACS::ENGINE::OutputPyPort* var = dynamic_cast<YACS::ENGINE::OutputPyPort*>(n->getOutputPort("x"));
CPPUNIT_ASSERT(var);
+ double x = -2.0;
PyObject* pyVal = var->get();
- CPPUNIT_ASSERT(pyVal);
- CPPUNIT_ASSERT(PyFloat_Check(pyVal));
- CPPUNIT_ASSERT_DOUBLES_EQUAL(42., PyFloat_AsDouble(pyVal), 1.E-12);
+ {
+ AutoGIL agil;
+ CPPUNIT_ASSERT(pyVal);
+ x = PyFloat_AsDouble(pyVal);
+ CPPUNIT_ASSERT( PyErr_Occurred()==nullptr );// check pyVal is interpretable as float
+ }
+ CPPUNIT_ASSERT_DOUBLES_EQUAL(42., x, 1.E-12);
p->shutdown(10); // kill all the containers
}
f1 -> f3 -> f1
"""
import testforeach
+ from SALOME_PyNode import UnProxyObjectSimple
expected_1, expected_2 = testforeach.main()
yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
yacs_build_command = "yacsbuild.py"
ex = pilot.ExecutorSwig()
proc = l.load(yacs_schema_file)
ex.RunW(proc,0)
- obtained_1 = proc.getChildByName("post_0").getOutputPort("s").getPyObj()
- obtained_2 = proc.getChildByName("f1_1").getOutputPort("r").getPyObj()
+ obtained_1 = UnProxyObjectSimple( proc.getChildByName("post_0").getOutputPort("s").getPyObj() )
+ obtained_2 = UnProxyObjectSimple( proc.getChildByName("f1_1").getOutputPort("r").getPyObj() )
self.assertEqual(expected_1, obtained_1)
self.assertEqual(expected_2, obtained_2)
Foreach initialized by value.
"""
import testforeach
+ from SALOME_PyNode import UnProxyObjectSimple
expected_1, expected_2 = testforeach.mainblock()
yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
yacs_build_command = "yacsbuild.py"
ex = pilot.ExecutorSwig()
proc = l.load(yacs_schema_file)
ex.RunW(proc,0)
- obtained_1 = proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj()
- obtained_2 = proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj()
+ obtained_1 = UnProxyObjectSimple( proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj() )
+ obtained_2 = UnProxyObjectSimple( proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj() )
self.assertEqual(expected_1, obtained_1)
self.assertEqual(expected_2, obtained_2)
Foreach on 2 levels.
"""
import testforeach
+ from SALOME_PyNode import UnProxyObjectSimple
expected = testforeach.maindoublefr()
yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
yacs_build_command = "yacsbuild.py"
ex = pilot.ExecutorSwig()
proc = l.load(yacs_schema_file)
ex.RunW(proc,0)
- obtained = proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj()
+ obtained = UnProxyObjectSimple( proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj() )
self.assertEqual(expected, obtained)
def test_t4(self):
def post(t):
s = 0
for e in t:
- s += e
+ s += int( e )
return s
@yacsdecorator.block
}
DEBTRACE( "++++++++++++++ ENDOF PyNode::executeRemote: " << getName() << " ++++++++++++++++++++" );
}
+void PythonNode::executeLocalInternal(const std::string& codeStr)
+{
+ DEBTRACE( code );
+ DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+ std::ostringstream stream;
+ stream << "/tmp/PythonNode_";
+ stream << getpid();
+ AutoPyRef code=Py_CompileString(codeStr.c_str(), stream.str().c_str(), Py_file_input);
+ if(code == NULL)
+ {
+ _errorDetails="";
+ AutoPyRef new_stderr = newPyStdOut(_errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ throw Exception("Error during execution");
+ }
+ {
+ AutoPyRef res = PyEval_EvalCode( code, _context, _context);
+ }
+ DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
+ fflush(stdout);
+ fflush(stderr);
+ if(PyErr_Occurred ())
+ {
+ _errorDetails="";
+ AutoPyRef new_stderr = newPyStdOut(_errorDetails);
+ PySys_SetObject((char*)"stderr", new_stderr);
+ ofstream errorfile(stream.str().c_str());
+ if (errorfile.is_open())
+ {
+ errorfile << codeStr;
+ errorfile.close();
+ }
+ PyErr_Print();
+ PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
+ throw Exception("Error during execution");
+ }
+}
void PythonNode::executeLocal()
{
DEBTRACE( "++++++++++++++ PyNode::executeLocal: " << getName() << " ++++++++++++++++++++" );
{
AutoGIL agil;
-
+ std::ostringstream unpxy; unpxy << "from SALOME_PyNode import UnProxyObjectSimple" << std::endl;
DEBTRACE( "---------------PyNode::inputs---------------" );
list<InputPort *>::iterator iter2;
for(iter2 = _setOfInputPort.begin(); iter2 != _setOfInputPort.end(); iter2++)
DEBTRACE( "port kind: " << p->edGetType()->kind() );
PyObject* ob=p->getPyObj();
DEBTRACE( "ob refcnt: " << ob->ob_refcnt );
+ unpxy << p->getName() << " = UnProxyObjectSimple( " << p->getName() << " )" << std::endl;
#ifdef _DEVDEBUG_
PyObject_Print(ob,stderr,Py_PRINT_RAW);
cerr << endl;
//calculation
DEBTRACE( "----------------PyNode::calculation---------------" );
- DEBTRACE( _script );
- DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
- std::ostringstream stream;
- stream << "/tmp/PythonNode_";
- stream << getpid();
+ executeLocalInternal( unpxy.str() );
- PyObject* code=Py_CompileString(_script.c_str(), stream.str().c_str(), Py_file_input);
- if(code == NULL)
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during execution");
- }
- PyObject *res = PyEval_EvalCode( code, _context, _context);
-
- Py_DECREF(code);
- Py_XDECREF(res);
- DEBTRACE( "_context refcnt: " << _context->ob_refcnt );
- fflush(stdout);
- fflush(stderr);
- if(PyErr_Occurred ())
- {
- _errorDetails="";
- PyObject* new_stderr = newPyStdOut(_errorDetails);
- PySys_SetObject((char*)"stderr", new_stderr);
- ofstream errorfile(stream.str().c_str());
- if (errorfile.is_open())
- {
- errorfile << _script;
- errorfile.close();
- }
- PyErr_Print();
- PySys_SetObject((char*)"stderr", PySys_GetObject((char*)"__stderr__"));
- Py_DECREF(new_stderr);
- throw Exception("Error during execution");
- }
+ executeLocalInternal( _script );
DEBTRACE( "-----------------PyNode::outputs-----------------" );
list<OutputPort *>::iterator iter;
void setSqueezeStatus(bool sqStatus) { _autoSqueeze=sqStatus; }
bool getSqueezeStatus() const { return _autoSqueeze; }
void squeezeMemorySafe();
+ private:
+ void executeLocalInternal(const std::string& codeStr);
protected:
void squeezeMemory();
void squeezeMemoryRemote();
static inline double convert(const TypeCode *t,PyObject* o,void*)
{
double x;
- if (PyFloat_Check(o))
- x=PyFloat_AS_DOUBLE(o);
- else if(PyLong_Check(o))
- x=PyLong_AsLong(o);
- else
+ x=PyFloat_AsDouble(o);
+ if( PyErr_Occurred() )
{
+ PyErr_Restore(nullptr,nullptr,nullptr);
stringstream msg;
msg << "Not a python double. ";
#ifdef _DEVDEBUG_
static inline long convert(const TypeCode *t,PyObject* o,void*)
{
long l;
- if(PyLong_Check(o))
- l=PyLong_AsLong(o);
- else
+ l=PyLong_AsLong(o);
+ if( PyErr_Occurred() )
{
+ PyErr_Restore(nullptr,nullptr,nullptr);
stringstream msg;
msg << "Not a python integer. ";
#ifdef _DEVDEBUG_
</container>
<bloc name="test/run">
<remote name="testgenerate_conditions" elementaryWeight="-1">
- <script><code><![CDATA[conditions_list=range(1000)
+ <script><code><![CDATA[conditions_list=list( range(1000) )
]]></code></script>
<load container="default_container"/>
<outport name="conditions_list" type="list[pyobj]"/>
import unittest
import tempfile
import os
+from SALOME_PyNode import UnProxyObjectSimple
dir_test = tempfile.mkdtemp(suffix=".yacstest")
#executor.RunB(proc,0) # always use the "old" executor
#executor.runWlm(proc,0) # always use the workload manager based executor
reloaded_res_port = reloaded_proc.getChildByName("n2").getOutputPort("v")
- self.assertEqual(reloaded_res_port.getPyObj(), 42)
+ self.assertEqual(UnProxyObjectSimple( reloaded_res_port.getPyObj() ), 42)
def test2(self):
""" Same as test1, but using the old executor instead of workload manager.
# you can also directly call the executor you wish, ignoring the property
#executor.RunB(proc,0) # always use the "old" executor
reloaded_res_port = reloaded_proc.getChildByName("n2").getOutputPort("v")
- self.assertEqual(reloaded_res_port.getPyObj(), 42)
+ self.assertEqual(UnProxyObjectSimple( reloaded_res_port.getPyObj() ), 42)
if __name__ == '__main__':
file_test = os.path.join(dir_test,"UnitTestsResult")
pass
def test15(self):
+ from SALOME_PyNode import UnProxyObjectSimple
#fname=os.path.join(self.workdir, "BugInConcurrentLaunchDftCont.xml")
p=self.r.createProc("pr")
ti=p.createType("int","int")
ex = pilot.ExecutorSwig()
self.assertEqual(p.getState(),pilot.READY)
ex.RunW(p,0)
- self.assertEqual(res.getPyObj(),6)
+ self.assertEqual(UnProxyObjectSimple( res.getPyObj() ),6)
self.assertEqual(p.getState(),pilot.DONE)
pass
fe0_end.setScript("""assert([elt[0] for elt in my_dpl_localization]==["FE0"])
assert(my_dpl_localization[0][1]>=0 and my_dpl_localization[0][1]<4)""")
n0=self.r.createScriptNode("Salome","n0") ; p.edAddChild(n0)
- n0.setScript("o1=range(10)")
+ n0.setScript("o1=list(range(10))")
a=n0.edAddOutputPort("o1",tdi)
p.edAddLink(a,fe0.edGetSeqOfSamplesPort()) ; p.edAddCFLink(n0,fe0)
# Level1
n1=self.r.createScriptNode("Salome","n1") ; b0.edAddChild(n1)
n1.setScript("""assert([elt[0] for elt in my_dpl_localization]==["FE0"])
assert(my_dpl_localization[0][1]>=0 and my_dpl_localization[0][1]<4)
-o1=range(10)""")
+o1=list(range(10))""")
b=n1.edAddOutputPort("o1",tdi)
fe1=self.r.createForEachLoop("FE1",ti) ; b0.edAddChild(fe1)
fe1.edGetNbOfBranchesPort().edInitInt(3)
b0=self.r.createBloc("test26/main") ; p.edAddChild(b0)
n0=self.r.createScriptNode("Salome","test26/n0") ; assignCont(n0,cont) # 1
n0.setScript("""import os
-dd=range(10)""")
+dd=list( range(10) )""")
dd=n0.edAddOutputPort("dd",sop) ; b0.edAddChild(n0)
fe0=self.r.createForEachLoop("test26/FE0",po) ; b0.edAddChild(fe0)
fe0.edGetNbOfBranchesPort().edInitInt(1) # very important for the test : 1 !