Skip to content
Snippets Groups Projects
Commit 676a9948 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

Removed wrong automatic DECREF while converting PyObject to string.

parent ad5bcd11
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,7 @@
#include <iostream>
std::string PyEmbeddedUtils::toString(PyObject* obj, bool decref)
std::string PyEmbeddedUtils::toString(PyObject* obj)
{
std::string result;
#if PY_MAJOR_VERSION >= 3
......@@ -32,9 +32,6 @@ std::string PyEmbeddedUtils::toString(PyObject* obj, bool decref)
result = std::string(PyString_AsString(obj));
#endif
if (decref)
Py_DECREF(obj);
return result;
}
......@@ -45,13 +42,13 @@ std::vector<std::string> PyEmbeddedUtils::toVectorString(PyObject* obj)
if (PyTuple_Check(obj)) {
for (Py_ssize_t i = 0; i < PyTuple_Size(obj); i++) {
PyObject *value = PyTuple_GetItem(obj, i);
result.push_back( toString(value, false) );
result.push_back( toString(value) );
}
} else if (PyList_Check(obj)) {
for (Py_ssize_t i = 0; i < PyList_Size(obj); i++) {
PyObject *value = PyList_GetItem(obj, i);
result.push_back( toString(value, false) );
result.push_back( toString(value) );
}
} else {
......
......@@ -28,11 +28,9 @@ class MultiLayer;
namespace PyEmbeddedUtils {
//! Converts PyObject into string, if possible, or throws exception.
//! @param decref To automatically decrease reference counter after data is processed.
BA_CORE_API_ std::string toString(PyObject* obj, bool decref = true);
BA_CORE_API_ std::string toString(PyObject* obj);
//! Converts PyObject into vector of strings, if possible, or throws exception.
//! @param decref To automatically decrease reference counter after data is processed.
BA_CORE_API_ std::vector<std::string> toVectorString(PyObject* obj);
//! Converts char to string. In the case of nullptr will return an empty string.
......
......@@ -65,6 +65,7 @@ bool ImportNumpy::runTest()
throw std::runtime_error("Can't get a variable");
auto version_string = PyEmbeddedUtils::toString(pvar);
Py_DECREF(pvar);
std::cout << "numpy_version_string=" << version_string << std::endl;
Py_Finalize();
......@@ -104,6 +105,7 @@ bool FunctionCall::runTest()
throw std::runtime_error("Error while calling function");
auto str = PyEmbeddedUtils::toString(result);
Py_DECREF(result);
Py_Finalize();
......@@ -237,6 +239,7 @@ bool CompiledFunction::runTest()
// convert the result to a string
PyObject* pResultRepr = PyObject_Repr( pResult ) ;
std::string result = PyEmbeddedUtils::toString(pResultRepr);
Py_DECREF(pResultRepr);
Py_Finalize();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment