From ea515e58f293e0eabea438895796820f6ab6f231 Mon Sep 17 00:00:00 2001 From: Christian Walther Date: Mon, 21 Nov 2011 22:45:34 +0100 Subject: [PATCH] =?UTF-8?q?Don=E2=80=99t=20modify=20tuples=20that=20others?= =?UTF-8?q?=20may=20already=20have=20references=20to.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes egg room private chat channels and entering Teledahn buckets with Python 2.7. These and other uses of ptSDL.setIndex() only worked by chance with Python 2.3 because the tuples happened to have reference counts of 1. --HG-- branch : sdlsetindex --- .../pfPython/plPythonSDLModifier.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.cpp b/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.cpp index 9107ee55..14904436 100644 --- a/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.cpp +++ b/MOULOpenSourceClientPlugin/Plasma20/Sources/Plasma/FeatureLib/pfPython/plPythonSDLModifier.cpp @@ -192,6 +192,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) @@ -205,6 +221,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