From: Paul RASCLE Date: Tue, 16 Jan 2018 20:12:13 +0000 (+0100) Subject: fix YacsPMMLBasicsTest: problem of convertPyObjectToString in TypeConversions.cxx X-Git-Tag: V9_0_0^0 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=b0898fabd07c0b33ed8fd229e779264a67d9110a;p=modules%2Fyacs.git fix YacsPMMLBasicsTest: problem of convertPyObjectToString in TypeConversions.cxx The solution is not clear. TODO: A better understanding of what to do is necessary Do we need a separate treatment for string (maybe with bad encoding?) and other types of PyObject Do we need str() or repr() and what are the possible causes of failure of str() ? --- diff --git a/src/runtime/TypeConversions.cxx b/src/runtime/TypeConversions.cxx index 895508f8f..2d52ea4c2 100644 --- a/src/runtime/TypeConversions.cxx +++ b/src/runtime/TypeConversions.cxx @@ -2301,8 +2301,14 @@ namespace YACS { PyObject *s; PyGILState_STATE gstate = PyGILState_Ensure(); - //s=PyObject_Repr(ob); - s=PyObject_ASCII(ob); + // TODO: separate treatment for string (maybe with bad encoding?) and other types of PyObject ? + // Do we need str() or repr() and what are the possible causes of failure of str() ? + // not clear, needs more analysis. + s=PyObject_Str(ob); + if (s == NULL) // for instance string with bad encoding, non utf-8 + { + s=PyObject_ASCII(ob); // escape non ASCII characters and like repr(), which is not the same as str()... + } Py_ssize_t size; char* characters=PyUnicode_AsUTF8AndSize(s, &size); std::string ss( characters, size); diff --git a/src/yacsloader/pmml/YACSPMMLBasicsTest1.cxx b/src/yacsloader/pmml/YACSPMMLBasicsTest1.cxx index d0a8be2c7..3d984508e 100755 --- a/src/yacsloader/pmml/YACSPMMLBasicsTest1.cxx +++ b/src/yacsloader/pmml/YACSPMMLBasicsTest1.cxx @@ -43,7 +43,7 @@ #include #endif -//#define _DEVDEBUG_ +#define _DEVDEBUG_ #include "YacsTrace.hxx" using namespace YACS::ENGINE; @@ -146,6 +146,7 @@ void YACSPMMLBasicsTest1::testYACSdriverLinearRegression() YACS::ENGINE::Node* node = p->nodeMap[string("PyGetRes")]; YACS::ENGINE::OutputPort* outputPort = node->getOutputPort(string("res")); string str = outputPort->getAsString(); + DEBTRACE("str = " << str); CPPUNIT_ASSERT_EQUAL(str, string("True")); delete p; } @@ -164,6 +165,7 @@ void YACSPMMLBasicsTest1::testYACSdriverNeuralNetwork() YACS::ENGINE::Node* node = p->nodeMap[string("PyGetRes")]; YACS::ENGINE::OutputPort* outputPort = node->getOutputPort(string("res")); string str = outputPort->getAsString(); + DEBTRACE("str = " << str); CPPUNIT_ASSERT_EQUAL(str, string("True")); delete p; } @@ -182,6 +184,7 @@ void YACSPMMLBasicsTest1::testYACSdriver_LRANN() YACS::ENGINE::Node* node = p->nodeMap[string("PyGetRes")]; YACS::ENGINE::OutputPort* outputPort = node->getOutputPort(string("res")); string str = outputPort->getAsString(); + DEBTRACE("str = " << str); CPPUNIT_ASSERT_EQUAL(str, string("True")); delete p; } @@ -200,6 +203,7 @@ void YACSPMMLBasicsTest1::testYACSdriver_PmmlDoesNotExist() YACS::ENGINE::Node* node = p->nodeMap[string("PyGetRes")]; YACS::ENGINE::OutputPort* outputPort = node->getOutputPort(string("res")); string str = outputPort->getAsString(); + DEBTRACE("str = " << str); CPPUNIT_ASSERT_EQUAL(str, string("None")); delete p; }