aboutsummaryrefslogtreecommitdiffstats
path: root/sources/shiboken2/libshiboken
diff options
context:
space:
mode:
authorFriedemann Kleint <Friedemann.Kleint@qt.io>2020-10-08 14:21:19 +0200
committerFriedemann Kleint <Friedemann.Kleint@qt.io>2020-10-08 14:21:19 +0200
commit00fa3966dbed373e341710f1145fd23a88f1bdb0 (patch)
tree331be762988f22221b40ed93486b9b05b657f0bd /sources/shiboken2/libshiboken
parente801fdab20ad4f5f47deaf49600af0d5f02ecc51 (diff)
parent2ed45ce8991408de91d7e02a2b80a09a2d9e5083 (diff)
Merge remote-tracking branch 'origin/5.15' into dev
Diffstat (limited to 'sources/shiboken2/libshiboken')
-rw-r--r--sources/shiboken2/libshiboken/sbkstring.cpp22
1 files changed, 17 insertions, 5 deletions
diff --git a/sources/shiboken2/libshiboken/sbkstring.cpp b/sources/shiboken2/libshiboken/sbkstring.cpp
index fff9d05ec..ca32f5919 100644
--- a/sources/shiboken2/libshiboken/sbkstring.cpp
+++ b/sources/shiboken2/libshiboken/sbkstring.cpp
@@ -42,6 +42,7 @@
#include "autodecref.h"
#include <vector>
+#include <unordered_set>
namespace Shiboken
{
@@ -183,7 +184,7 @@ Py_ssize_t len(PyObject *str)
// PyObject *attr = PyObject_GetAttr(obj, name());
//
-using StaticStrings = std::vector<PyObject *>;
+using StaticStrings = std::unordered_set<PyObject *>;
static void finalizeStaticStrings(); // forward
@@ -195,10 +196,12 @@ static StaticStrings &staticStrings()
static void finalizeStaticStrings()
{
- auto &list = staticStrings();
- for (PyObject *ob : list)
+ auto &set = staticStrings();
+ for (PyObject *ob : set) {
+ Py_REFCNT(ob) = 1;
Py_DECREF(ob);
- list.clear();
+ }
+ set.clear();
}
PyObject *createStaticString(const char *str)
@@ -218,7 +221,16 @@ PyObject *createStaticString(const char *str)
PyErr_Print();
Py_FatalError("unexpected error in createStaticString()");
}
- staticStrings().push_back(result);
+ auto it = staticStrings().find(result);
+ if (it == staticStrings().end())
+ staticStrings().insert(result);
+ /*
+ * Note: We always add one reference even if we have a new string.
+ * This makes the strings immortal, and we are safe if someone
+ * uses AutoDecRef, although the set cannot cope with deletions.
+ * The exit handler cleans that up, anyway.
+ */
+ Py_INCREF(result);
return result;
}