Salome HOME
fix YacsPMMLBasicsTest: problem of convertPyObjectToString in TypeConversions.cxx V9_0_0
authorPaul RASCLE <paul.rascle@edf.fr>
Tue, 16 Jan 2018 20:12:13 +0000 (21:12 +0100)
committerPaul RASCLE <paul.rascle@edf.fr>
Tue, 16 Jan 2018 20:12:13 +0000 (21:12 +0100)
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() ?

src/runtime/TypeConversions.cxx
src/yacsloader/pmml/YACSPMMLBasicsTest1.cxx

index 895508f8f4c3f625151ed076f00e7c245ad9237a..2d52ea4c26aafd651e6f12e8753f4b82f35a4f5e 100644 (file)
@@ -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);
index d0a8be2c7397890e891f506fac59864d4acd1f31..3d984508e4eade79e109e359bb2c9cb9796c9b76 100755 (executable)
@@ -43,7 +43,7 @@
 #include <unistd.h>
 #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;     
     }