diff --git a/CHANGES.current b/CHANGES.current index 9fb57f9f0..2a5c1c9ac 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,11 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.0.0 (in progress) =========================== +2018-06-30: petrmitrichev + [Python] #1275 #1279 Remove function-local statics that call Python code + in order to avoid deadlocks with multi-threaded usage. These are now + file scope static variables and are initialized during module initialization. + 2018-06-15: wsfulton [Python] Fix seg fault using Python 2 when passing a Python string, containing invalid utf-8 content, to a wstring or wchar * parameter. A TypeError is thrown instead, eg: diff --git a/Lib/python/pyinit.swg b/Lib/python/pyinit.swg index ea865c066..e15eb0309 100644 --- a/Lib/python/pyinit.swg +++ b/Lib/python/pyinit.swg @@ -379,6 +379,14 @@ SWIG_init(void) { assert(metatype); #endif +#ifdef SWIG_PYTHON_BUILD_NONE + SWIG_Py_None_global = Py_BuildValue(""); + Py_DECREF(SWIG_Py_None_global); +#endif + + SWIG_This_global = SWIG_Python_str_FromChar("this"); + SWIG_Python_TypeCache_global = PyDict_New(); + /* Fix SwigMethods to carry the callback ptrs when needed */ SWIG_Python_FixMethods(SwigMethods, swig_const_table, swig_types, swig_type_initial); @@ -429,13 +437,11 @@ SWIG_init(void) { SwigPyBuiltin_AddPublicSymbol(public_interface, SwigMethods[i].ml_name); for (i = 0; swig_const_table[i].name != 0; ++i) SwigPyBuiltin_AddPublicSymbol(public_interface, swig_const_table[i].name); +#else + SwigPyObject_type_global = SwigPyObject_TypeOnce(); #endif - SWIG_Py_None_global_Init(); - SwigPyObject_type_global_Init(); - SwigPyPacked_type_global_Init(); - SWIG_Python_TypeCache_global_Init(); - + SwigPyPacked_type_global = SwigPyPacked_TypeOnce(); SWIG_InstallConstants(d,swig_const_table); %} diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index 51ea81306..5ad5713fd 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -241,42 +241,20 @@ extern "C" { # undef Py_None # define Py_None SWIG_Py_None() # endif -SWIGRUNTIMEINLINE PyObject * -_SWIG_Py_None(void) -{ - PyObject *none = Py_BuildValue(""); - Py_DECREF(none); - return none; -} -/* This used to be a function-local static variable, but accessing Python - * C API from inside a function-local static initializer can lead to deadlocks. - * https://github.com/swig/swig/issues/1275 */ static PyObject *SWIG_Py_None_global = 0; -SWIGRUNTIME void SWIG_Py_None_global_Init(void) { - SWIG_Py_None_global = _SWIG_Py_None(); -} - SWIGRUNTIME PyObject * -SWIG_Py_None(void) -{ +SWIG_Py_None(void) { assert(SWIG_Py_None_global); return SWIG_Py_None_global; } - -#else -SWIGRUNTIME void SWIG_Py_None_global_Init(void) -{ - // Do nothing -} #endif /* The python void return value */ SWIGRUNTIMEINLINE PyObject * -SWIG_Py_Void(void) -{ +SWIG_Py_Void(void) { PyObject *none = Py_None; Py_INCREF(none); return none; @@ -485,6 +463,7 @@ SWIGRUNTIME PyTypeObject* SwigPyObject_TypeOnce(void); #ifdef SWIGPYTHON_BUILTIN static swig_type_info *SwigPyObject_stype = 0; + SWIGRUNTIME PyTypeObject* SwigPyObject_type(void) { SwigPyClientData *cd; @@ -494,20 +473,9 @@ SwigPyObject_type(void) { assert(cd->pytype); return cd->pytype; } - -SWIGRUNTIME void SwigPyObject_type_global_Init(void) { - // Do nothing -} #else -/* This used to be a function-local static variable, but accessing Python - * C API from inside a function-local static initializer can lead to deadlocks. - * https://github.com/swig/swig/issues/1275 */ static PyTypeObject *SwigPyObject_type_global = 0; -SWIGRUNTIME void SwigPyObject_type_global_Init(void) { - SwigPyObject_type_global = SwigPyObject_TypeOnce(); -} - SWIGRUNTIME PyTypeObject* SwigPyObject_type(void) { assert(SwigPyObject_type_global); @@ -855,15 +823,8 @@ SwigPyPacked_compare(SwigPyPacked *v, SwigPyPacked *w) SWIGRUNTIME PyTypeObject* SwigPyPacked_TypeOnce(void); -/* This used to be a function-local static variable, but accessing Python - * C API from inside a function-local static initializer can lead to deadlocks. - * https://github.com/swig/swig/issues/1275 */ static PyTypeObject* SwigPyPacked_type_global = 0; -SWIGRUNTIME void SwigPyPacked_type_global_Init(void) { - SwigPyPacked_type_global = SwigPyPacked_TypeOnce(); -} - SWIGRUNTIME PyTypeObject* SwigPyPacked_type(void) { assert(SwigPyPacked_type_global); @@ -1004,20 +965,12 @@ SwigPyPacked_UnpackData(PyObject *obj, void *ptr, size_t size) * pointers/data manipulation * ----------------------------------------------------------------------------- */ -SWIGRUNTIMEINLINE PyObject * -_SWIG_This(void) -{ - return SWIG_Python_str_FromChar("this"); -} - -static PyObject *swig_this = NULL; +static PyObject *SWIG_This_global = NULL; SWIGRUNTIME PyObject * -SWIG_This(void) -{ - if (swig_this == NULL) - swig_this = _SWIG_This(); - return swig_this; +SWIG_This(void) { + assert(SWIG_This_global); + return SWIG_This_global; } /* #define SWIG_PYTHON_SLOW_GETSET_THIS */ @@ -1446,7 +1399,7 @@ SWIG_Python_DestroyModule(PyObject *obj) } } Py_DECREF(SWIG_This()); - swig_this = NULL; + SWIG_This_global = NULL; } SWIGRUNTIME void @@ -1466,15 +1419,8 @@ SWIG_Python_SetModule(swig_module_info *swig_module) { } } -/* This used to be a function-local static variable, but accessing Python - * C API from inside a function-local static initializer can lead to deadlocks. - * https://github.com/swig/swig/issues/1275 */ static PyObject *SWIG_Python_TypeCache_global = 0; -SWIGRUNTIME void SWIG_Python_TypeCache_global_Init(void) { - SWIG_Python_TypeCache_global = PyDict_New(); -} - /* The python cached type query */ SWIGRUNTIME PyObject * SWIG_Python_TypeCache(void) {