]> SALOME platform Git repositories - tools/py2cpp.git/commitdiff
Salome HOME
Deal with PyUnicode_AsUTF8 returning NULL.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Tue, 26 Feb 2019 09:23:11 +0000 (10:23 +0100)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Tue, 26 Feb 2019 09:23:11 +0000 (10:23 +0100)
According to https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsUTF8
PyUnicode_AsUTF8String may return NULL in case of error.

src/CMakeLists.txt
src/Errors.cxx
src/PyPtr.cxx
src/TypeConversions.cxx

index fbd6d768ba7c1cf923ad8812673cb377bf92e2f4..08bfb17e368fa370f03435c4b689ac70665c6ab6 100644 (file)
@@ -50,8 +50,8 @@ TARGET_INCLUDE_DIRECTORIES(py2cpp PUBLIC
                           $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
                           $<INSTALL_INTERFACE:include>)
 
-INSTALL(TARGETS py2cpp EXPORT FindPy2cpp LIBRARY DESTINATION lib)
+INSTALL(TARGETS py2cpp EXPORT Py2cppCfg LIBRARY DESTINATION lib)
 INSTALL(FILES ${_py2cpp_headers} DESTINATION include/py2cpp)
-INSTALL(EXPORT FindPy2cpp DESTINATION lib/cmake/py2cpp)
+INSTALL(EXPORT Py2cppCfg DESTINATION lib/cmake/py2cpp FILE Py2cppConfig.cmake)
 
 ADD_SUBDIRECTORY(Test)
index eb80814e69f12500f804c0d20879ac8f9d5bcb3a..f0984fcf821fe3b7f7e52d201ff3255f52fbe2c2 100644 (file)
@@ -31,7 +31,9 @@ std::string getLastPyError()
     PyErr_Fetch(&ptype, &pvalue, &ptraceback);
     PyErr_NormalizeException(&ptype, &pvalue, &ptraceback);
     pystr = PyObject_Str(pvalue);
-    result = std::string(PyUnicode_AsUTF8(pystr));
+    const char* charstr = PyUnicode_AsUTF8(pystr);
+    if(charstr != nullptr)
+      result = std::string(charstr);
     result += "\n";
     Py_DECREF(pystr);
     
@@ -54,7 +56,9 @@ std::string getLastPyError()
             for(int i=0; i<n; i++)
             {
               pystr = PyList_GetItem(pyList,i);
-              result += std::string(PyUnicode_AsUTF8(pystr));
+              const char* charstr = PyUnicode_AsUTF8(pystr);
+              if(charstr != nullptr)
+                result += charstr;
             }
             Py_DECREF(pyList);
           }
@@ -102,7 +106,11 @@ void ConversionCheck::addError(const std::string& expectedType, PyObject * obj)
     std::string pyRepr;
     PyObject* pyResult = PyObject_Repr(obj);
     if(pyResult && PyUnicode_Check(pyResult))
-      pyRepr = PyUnicode_AsUTF8(pyResult);
+    {
+      const char* charstr = PyUnicode_AsUTF8(pyResult);
+      if(charstr != nullptr)
+        pyRepr = charstr;
+    }
     else
       pyRepr = "unknown representation";
     Py_XDECREF(pyResult);
index c5624bce28d4c486c249e88064dad6ddcea03d6e..658c3d648b00f45080026ab977aab1f90a26a4b4 100644 (file)
@@ -75,7 +75,11 @@ std::string PyPtr::repr()const
   {
     PyObject* pyResult = PyObject_Repr(thisObj);
     if(pyResult && PyUnicode_Check(pyResult))
-      result = PyUnicode_AsUTF8(pyResult);
+    {
+      const char* charstr = PyUnicode_AsUTF8(pyResult);
+      if(charstr != nullptr)
+        result = charstr;
+    }
     Py_XDECREF(pyResult);
   }
   return result;
index d50b3a84741ea888644c0c830ed738ebabf545eb..1f5a254cce3da86f517bd47df4cf97ca36e0cede 100644 (file)
@@ -89,7 +89,13 @@ ConversionCheck fromPy(PyObject * po, std::string& result)
 {
   ConversionCheck check;
   if(po && PyUnicode_Check(po))
-    result = PyUnicode_AsUTF8(po);
+  {
+    const char* charstr = PyUnicode_AsUTF8(po);
+    if(charstr != nullptr)
+      result = charstr;
+    else
+      result = "";
+  }
   else
     check.addError("std::string", po);
   return check;