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

@ -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);
%}

View file

@ -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) {