From 65c17b8dea0c45bbcf3daf4463d2855d4d50ac5e Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Tue, 26 Feb 2019 10:23:11 +0100 Subject: [PATCH] Deal with PyUnicode_AsUTF8 returning NULL. 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 | 4 ++-- src/Errors.cxx | 14 +++++++++++--- src/PyPtr.cxx | 6 +++++- src/TypeConversions.cxx | 8 +++++++- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fbd6d76..08bfb17 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -50,8 +50,8 @@ TARGET_INCLUDE_DIRECTORIES(py2cpp PUBLIC $ $) -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) diff --git a/src/Errors.cxx b/src/Errors.cxx index eb80814..f0984fc 100644 --- a/src/Errors.cxx +++ b/src/Errors.cxx @@ -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