Skip to content

Commit

Permalink
Merge pull request #101 from cwalther/sdlsetindex-huru
Browse files Browse the repository at this point in the history
Don’t modify tuples that others may already have references to
  • Loading branch information
branan committed Nov 26, 2011
2 parents d5bd503 + e2905b6 commit 1c776f3
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,22 @@ void plPythonSDLModifier::SetItemIdx(const char* key, int idx, PyObject* value,
return;
}

if (pyTuple && pyTuple->ob_refcnt != 1)
{
// others already have references to the tuple and expect it to be immutable, must make a copy
int n = PyTuple_Size(pyTuple);
PyObject* newTuple = PyTuple_New(n);
for (int j = 0; j < n; j++)
{
PyObject* item = PyTuple_GetItem(pyTuple, j);
Py_INCREF(item);
PyTuple_SetItem(newTuple, j, item);
}
Py_DECREF(pyTuple);
pyTuple = newTuple;
it->second.obj = newTuple;
}

if (pyTuple)
{
if (PyTuple_Size(pyTuple) <= idx)
Expand All @@ -221,6 +237,8 @@ void plPythonSDLModifier::SetItemIdx(const char* key, int idx, PyObject* value,
Py_INCREF(Py_None);
PyTuple_SetItem(pyTuple, j, Py_None);
}
// _PyTuple_Resize may have changed pyTuple
it->second.obj = pyTuple;
}
}
else
Expand Down

0 comments on commit 1c776f3

Please sign in to comment.