[Bf-blender-cvs] [a9dea9cfaa7] master: PyAPI: don't raise & clear exceptions when setting context members

Campbell Barton noreply at git.blender.org
Thu Jan 7 04:45:58 CET 2021


Commit: a9dea9cfaa756396153223f1eb2d8dc3161ded01
Author: Campbell Barton
Date:   Thu Jan 7 14:31:23 2021 +1100
Branches: master
https://developer.blender.org/rBa9dea9cfaa756396153223f1eb2d8dc3161ded01

PyAPI: don't raise & clear exceptions when setting context members

BPY_context_dict_clear_members_array used PyDict_DelItemString
which raised & cleared the exception when the key didn't exist.

Even though setting/clearing the exception is supported,
it's worth avoiding where possible as it adds some overhead as well as
overwriting the previous error which can free PyObject's which are
unrelated to the code being executed.

Possible fix for T82552, crashing on Windows when setting the exception.

===================================================================

M	source/blender/python/intern/bpy_interface.c

===================================================================

diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 0b11ac639c7..2bd71c27320 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -195,10 +195,14 @@ void BPY_context_dict_clear_members_array(void **dict_p,
 
   PyObject *dict = *dict_p;
   BLI_assert(PyDict_Check(dict));
+
+  /* Use #PyDict_Pop instead of #PyDict_DelItemString to avoid setting the exception,
+   * while supported it's good to avoid for low level functions like this that run often. */
   for (uint i = 0; i < context_members_len; i++) {
-    if (PyDict_DelItemString(dict, context_members[i])) {
-      PyErr_Clear();
-    }
+    PyObject *key = PyUnicode_FromString(context_members[i]);
+    PyObject *item = _PyDict_Pop(dict, key, Py_None);
+    Py_DECREF(key);
+    Py_DECREF(item);
   }
 
   if (use_gil) {



More information about the Bf-blender-cvs mailing list