From 5185ce67b59045e20c1975319527b1ffbe705b20 Mon Sep 17 00:00:00 2001 From: Jake Cobb Date: Wed, 23 Jan 2019 15:12:45 -0500 Subject: [PATCH] Python: Init container owner attribute in thread-safe way --- Lib/python/pycontainer.swg | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Lib/python/pycontainer.swg b/Lib/python/pycontainer.swg index f77c7cb34..b90068466 100644 --- a/Lib/python/pycontainer.swg +++ b/Lib/python/pycontainer.swg @@ -36,8 +36,17 @@ %include -%fragment("reference_container_owner", "header") { +%fragment("container_owner_attribute_init", "init") { + // thread safe initialization + swig::container_owner_attribute(); +} +%fragment("reference_container_owner", "header", fragment="container_owner_attribute_init") { namespace swig { + PyObject* container_owner_attribute() { + static PyObject* attr = SWIG_Python_str_FromChar("__owner"); + return attr; + } + /** * Call to add a back-reference to the owning object when returning a * reference from a container. Will only set the reference if child @@ -46,10 +55,9 @@ namespace swig { * @return if the reference was set or not */ bool reference_container_owner(PyObject* child, PyObject* owner) { - static PyObject* attr = SWIG_Python_str_FromChar("__owner"); SwigPyObject* swigThis = SWIG_Python_GetSwigThis(child); if (swigThis && (swigThis->own & SWIG_POINTER_OWN) != SWIG_POINTER_OWN) { - PyObject_SetAttr(child, attr, owner); + PyObject_SetAttr(child, container_owner_attribute(), owner); return true; } return false;