Skip to content

Commit

Permalink
Fix memory leaks Python >= 3 (#80)
Browse files Browse the repository at this point in the history
Fix memory leaks Python >= 3
  • Loading branch information
alinbalutoiu authored and JumpingYang001 committed Nov 30, 2017
1 parent cee03d4 commit e4d95e6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion python/client_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,13 @@ Client_Wrapper::init (
++i)
{
PyObject* pPathItem = PyList_GET_ITEM (pSysPath, i);
strm << " " << PyString_AsString (pPathItem);
#if PY_MAJOR_VERSION >= 3
PyObject* tmp = PyUnicode_AsEncodedString (pPathItem, "utf-8", "strict");
strm << " " << PyBytes_AsString (tmp);
Py_DECREF(tmp);
#else
strm << " " << PyString_AsString (pPathItem);
#endif
CLIENT_INIT_BOOKEND_PRINT (strm.str ().c_str ());
}
}
Expand Down
8 changes: 7 additions & 1 deletion python/py_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,13 @@ PyConverter<MI_Type<MI_STRING>::type_t>::fromPyObject (
assert (NULL != pValueOut);
if (PyString_Check (pSource))
{
*pValueOut = PyString_AsString (pSource);
#if PY_MAJOR_VERSION >= 3
PyObject* tmp = PyUnicode_AsEncodedString (pSource, "utf-8", "strict");
*pValueOut = PyBytes_AsString (tmp);
Py_DECREF(tmp);
#else
*pValueOut = PyString_AsString (pSource);
#endif
rval = PY_SUCCESS;
}
else
Expand Down
9 changes: 8 additions & 1 deletion python/python_compatibility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@

// Python 3 has now separate types for String, which doesn't exist anymore.
// The new types are PyBytes and PyUnicode.
#define PyString_AsString(x) PyBytes_AsString(PyUnicode_AsEncodedString(x, "utf-8", "strict"))
// Using the following line will cause a memory leak on Python 3.
// PyUnicode_AsEncodedString returns a new reference which needs to be
// freed after the conversion
// #define PyString_AsString(x) PyBytes_AsString(PyUnicode_AsEncodedString(x, "utf-8", "strict"))
// Correct version to be used in the code:
// PyObject* tmp = PyUnicode_AsEncodedString(x, "utf-8", "strict");
// result = PyBytes_AsString(tmp);
// Py_DECREF(tmp);
#define PyString_Check PyUnicode_Check
#define PyString_FromString PyUnicode_FromString
#endif
Expand Down

0 comments on commit e4d95e6

Please sign in to comment.