Python initialization code tidy up

I've moved the initialization of statics a little earlier on - a little
safer as it is hard to follow exactly when some of these were being
used, such as SWIG_Py_None which sometimes replaces Py_None.
This commit is contained in:
William S Fulton 2018-06-30 19:18:06 +01:00
commit 4b4e0180f4
3 changed files with 24 additions and 67 deletions

View file

@ -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) 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 2018-06-15: wsfulton
[Python] Fix seg fault using Python 2 when passing a Python string, containing [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: invalid utf-8 content, to a wstring or wchar * parameter. A TypeError is thrown instead, eg:

View file

@ -379,6 +379,14 @@ SWIG_init(void) {
assert(metatype); assert(metatype);
#endif #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 */ /* Fix SwigMethods to carry the callback ptrs when needed */
SWIG_Python_FixMethods(SwigMethods, swig_const_table, swig_types, swig_type_initial); 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); SwigPyBuiltin_AddPublicSymbol(public_interface, SwigMethods[i].ml_name);
for (i = 0; swig_const_table[i].name != 0; ++i) for (i = 0; swig_const_table[i].name != 0; ++i)
SwigPyBuiltin_AddPublicSymbol(public_interface, swig_const_table[i].name); SwigPyBuiltin_AddPublicSymbol(public_interface, swig_const_table[i].name);
#else
SwigPyObject_type_global = SwigPyObject_TypeOnce();
#endif #endif
SWIG_Py_None_global_Init(); SwigPyPacked_type_global = SwigPyPacked_TypeOnce();
SwigPyObject_type_global_Init();
SwigPyPacked_type_global_Init();
SWIG_Python_TypeCache_global_Init();
SWIG_InstallConstants(d,swig_const_table); SWIG_InstallConstants(d,swig_const_table);
%} %}

View file

@ -241,42 +241,20 @@ extern "C" {
# undef Py_None # undef Py_None
# define Py_None SWIG_Py_None() # define Py_None SWIG_Py_None()
# endif # 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; static PyObject *SWIG_Py_None_global = 0;
SWIGRUNTIME void SWIG_Py_None_global_Init(void) {
SWIG_Py_None_global = _SWIG_Py_None();
}
SWIGRUNTIME PyObject * SWIGRUNTIME PyObject *
SWIG_Py_None(void) SWIG_Py_None(void) {
{
assert(SWIG_Py_None_global); assert(SWIG_Py_None_global);
return SWIG_Py_None_global; return SWIG_Py_None_global;
} }
#else
SWIGRUNTIME void SWIG_Py_None_global_Init(void)
{
// Do nothing
}
#endif #endif
/* The python void return value */ /* The python void return value */
SWIGRUNTIMEINLINE PyObject * SWIGRUNTIMEINLINE PyObject *
SWIG_Py_Void(void) SWIG_Py_Void(void) {
{
PyObject *none = Py_None; PyObject *none = Py_None;
Py_INCREF(none); Py_INCREF(none);
return none; return none;
@ -485,6 +463,7 @@ SWIGRUNTIME PyTypeObject* SwigPyObject_TypeOnce(void);
#ifdef SWIGPYTHON_BUILTIN #ifdef SWIGPYTHON_BUILTIN
static swig_type_info *SwigPyObject_stype = 0; static swig_type_info *SwigPyObject_stype = 0;
SWIGRUNTIME PyTypeObject* SWIGRUNTIME PyTypeObject*
SwigPyObject_type(void) { SwigPyObject_type(void) {
SwigPyClientData *cd; SwigPyClientData *cd;
@ -494,20 +473,9 @@ SwigPyObject_type(void) {
assert(cd->pytype); assert(cd->pytype);
return cd->pytype; return cd->pytype;
} }
SWIGRUNTIME void SwigPyObject_type_global_Init(void) {
// Do nothing
}
#else #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; static PyTypeObject *SwigPyObject_type_global = 0;
SWIGRUNTIME void SwigPyObject_type_global_Init(void) {
SwigPyObject_type_global = SwigPyObject_TypeOnce();
}
SWIGRUNTIME PyTypeObject* SWIGRUNTIME PyTypeObject*
SwigPyObject_type(void) { SwigPyObject_type(void) {
assert(SwigPyObject_type_global); assert(SwigPyObject_type_global);
@ -855,15 +823,8 @@ SwigPyPacked_compare(SwigPyPacked *v, SwigPyPacked *w)
SWIGRUNTIME PyTypeObject* SwigPyPacked_TypeOnce(void); 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; static PyTypeObject* SwigPyPacked_type_global = 0;
SWIGRUNTIME void SwigPyPacked_type_global_Init(void) {
SwigPyPacked_type_global = SwigPyPacked_TypeOnce();
}
SWIGRUNTIME PyTypeObject* SWIGRUNTIME PyTypeObject*
SwigPyPacked_type(void) { SwigPyPacked_type(void) {
assert(SwigPyPacked_type_global); assert(SwigPyPacked_type_global);
@ -1004,20 +965,12 @@ SwigPyPacked_UnpackData(PyObject *obj, void *ptr, size_t size)
* pointers/data manipulation * pointers/data manipulation
* ----------------------------------------------------------------------------- */ * ----------------------------------------------------------------------------- */
SWIGRUNTIMEINLINE PyObject * static PyObject *SWIG_This_global = NULL;
_SWIG_This(void)
{
return SWIG_Python_str_FromChar("this");
}
static PyObject *swig_this = NULL;
SWIGRUNTIME PyObject * SWIGRUNTIME PyObject *
SWIG_This(void) SWIG_This(void) {
{ assert(SWIG_This_global);
if (swig_this == NULL) return SWIG_This_global;
swig_this = _SWIG_This();
return swig_this;
} }
/* #define SWIG_PYTHON_SLOW_GETSET_THIS */ /* #define SWIG_PYTHON_SLOW_GETSET_THIS */
@ -1446,7 +1399,7 @@ SWIG_Python_DestroyModule(PyObject *obj)
} }
} }
Py_DECREF(SWIG_This()); Py_DECREF(SWIG_This());
swig_this = NULL; SWIG_This_global = NULL;
} }
SWIGRUNTIME void 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; 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 */ /* The python cached type query */
SWIGRUNTIME PyObject * SWIGRUNTIME PyObject *
SWIG_Python_TypeCache(void) { SWIG_Python_TypeCache(void) {