Skip to content

Commit

Permalink
[fix] python object memory leak when we fail on an attempt to coerce …
Browse files Browse the repository at this point in the history
…a python unicode to a string.
  • Loading branch information
Jim Carroll committed Oct 2, 2012
1 parent 6cf5b9d commit d925641
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 18 additions & 8 deletions xbmc/interfaces/python/swig.cpp
Expand Up @@ -33,8 +33,18 @@ namespace PythonBindings
memset(typeInfo, 0, sizeof(TypeInfo));
}

int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString,
const char* argumentName, const char* methodname) throw (XBMCAddon::WrongTypeException)
class PyObjectDecrementor
{
PyObject* obj;
public:
inline PyObjectDecrementor(PyObject* pyobj) : obj(pyobj) {}
inline ~PyObjectDecrementor() { Py_XDECREF(obj); }

inline PyObject* get() { return obj; }
};

void PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString,
const char* argumentName, const char* methodname) throw (XBMCAddon::WrongTypeException)
{
// TODO: UTF-8: Does python use UTF-16?
// Do we need to convert from the string charset to UTF-8
Expand All @@ -50,24 +60,24 @@ namespace PythonBindings
{
buf = PyString_AsString(utf8_pyString);
Py_DECREF(utf8_pyString);
return 1;
return;
}
}
if (PyString_Check(pObject))
{
buf = PyString_AsString(pObject);
return 1;
return;
}

// if we got here then we need to coerce the value to a string
if (coerceToString)
{
PyObject* pyStrCast = PyObject_Str(pObject);
PyObjectDecrementor dec(PyObject_Str(pObject));
PyObject* pyStrCast = dec.get();
if (pyStrCast)
{
int ret = PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname);
Py_DECREF(pyStrCast);
return ret;
PyXBMCGetUnicodeString(buf,pyStrCast,false,argumentName,methodname);
return;
}
}

Expand Down
6 changes: 3 additions & 3 deletions xbmc/interfaces/python/swig.h
Expand Up @@ -30,9 +30,9 @@

namespace PythonBindings
{
int PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString = false,
const char* pos = "unknown",
const char* methodname = "unknown") throw (XBMCAddon::WrongTypeException);
void PyXBMCGetUnicodeString(std::string& buf, PyObject* pObject, bool coerceToString = false,
const char* pos = "unknown",
const char* methodname = "unknown") throw (XBMCAddon::WrongTypeException);

// This is for casting from child class to base class
struct TypeConverterBase
Expand Down

0 comments on commit d925641

Please sign in to comment.