aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Tismer <tismer@stackless.com>2022-08-14 12:59:41 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2022-08-15 09:39:01 +0000
commite898d7a3a12b2a97d796acd48441775c41faa994 (patch)
tree1de873cd9fb1f3253776c3028e8bef83f0bfcd23
parente0687b1d5157125ba67dfaaa6da48136594158b3 (diff)
PyEnum: Accelerate item access
With the transition to Python 3.11, access to enum items has become slower by the replacement of direct item access by Python properties, involving unnecessarily the use of Python code. When we are in charge of Enum item access, we circumvent the slower property access by using the internal mapping directly. Task-number: PYSIDE-1735 Change-Id: Iabe045be09df847d9587e9d3f6913e9610f5695e Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io> (cherry picked from commit 01e048763a372677c358172a66232f15b09a1021) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--sources/shiboken6/libshiboken/sbkenum.cpp18
1 files changed, 11 insertions, 7 deletions
diff --git a/sources/shiboken6/libshiboken/sbkenum.cpp b/sources/shiboken6/libshiboken/sbkenum.cpp
index 655fd9c58..58dafa497 100644
--- a/sources/shiboken6/libshiboken/sbkenum.cpp
+++ b/sources/shiboken6/libshiboken/sbkenum.cpp
@@ -731,13 +731,17 @@ newItem(PyTypeObject *enumType, long itemValue, const char *itemName)
if (useOldEnum)
return newItemOld(enumType, itemValue, itemName);
- if (!itemName) {
- //PyObject *enumObj = getEnumItemFromValue(enumType, itemValue);
- PyObject *enumObj = PyObject_CallFunction(reinterpret_cast<PyObject *>(enumType), "i", itemValue);
- //if (enumObj)
- return enumObj;
- }
- return PyObject_GetAttrString(reinterpret_cast<PyObject *>(enumType), itemName);
+ auto *obEnumType = reinterpret_cast<PyObject *>(enumType);
+ if (!itemName)
+ return PyObject_CallFunction(obEnumType, "i", itemValue);
+
+ static PyObject *const _member_map_ = String::createStaticString("_member_map_");
+ auto *member_map = PyDict_GetItem(enumType->tp_dict, _member_map_);
+ if (!(member_map && PyDict_Check(member_map)))
+ return nullptr;
+ auto *result = PyDict_GetItemString(member_map, itemName);
+ Py_XINCREF(result);
+ return result;
}
} // namespace Shiboken