From a9f76c89ec0f4b2b60f2edc3d705c14cc6aec5f6 Mon Sep 17 00:00:00 2001 From: benjamin-sch Date: Wed, 12 Jan 2022 14:22:11 +0100 Subject: added interpreter_counter to deinitialize only once in case of subinterpreters --- Lib/python/pyrun.swg | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index a49cbb5b3..d42dc0f0f 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1643,12 +1643,17 @@ SWIG_Python_GetModule(void *SWIGUNUSEDPARM(clientdata)) { return (swig_module_info *) type_pointer; } + +static int interpreter_counter = 0; // how many (sub-)interpreters are using swig_module's types + SWIGRUNTIME void SWIG_Python_DestroyModule(PyObject *obj) { swig_module_info *swig_module = (swig_module_info *) PyCapsule_GetPointer(obj, SWIGPY_CAPSULE_NAME); swig_type_info **types = swig_module->types; size_t i; + if (--interpreter_counter != 0) // another sub-interpreter may still be using the swig_module's types + return; for (i =0; i < swig_module->size; ++i) { swig_type_info *ty = types[i]; if (ty->owndata) { @@ -1676,6 +1681,7 @@ SWIG_Python_SetModule(swig_module_info *swig_module) { #endif PyObject *pointer = PyCapsule_New((void *) swig_module, SWIGPY_CAPSULE_NAME, SWIG_Python_DestroyModule); if (pointer && module) { + ++interpreter_counter; if (PyModule_AddObject(module, "type_pointer_capsule" SWIG_TYPE_TABLE_NAME, pointer) != 0) { Py_DECREF(pointer); } -- cgit v1.2.1 From a2992fe42e0a462dc9c08bbffa29c6b4fdbd07ff Mon Sep 17 00:00:00 2001 From: benjamin-sch Date: Tue, 8 Feb 2022 09:04:18 +0100 Subject: always get the type_pointer from capsule instead of using a static variable as the value may change after re-initilization/due to subinterpreters --- Lib/python/pyrun.swg | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Lib/python/pyrun.swg b/Lib/python/pyrun.swg index d42dc0f0f..2aef3ad0f 100644 --- a/Lib/python/pyrun.swg +++ b/Lib/python/pyrun.swg @@ -1627,19 +1627,19 @@ SWIG_Python_TypeCache(void) { SWIGRUNTIME swig_module_info * SWIG_Python_GetModule(void *SWIGUNUSEDPARM(clientdata)) { +#ifdef SWIG_LINK_RUNTIME static void *type_pointer = (void *)0; /* first check if module already created */ if (!type_pointer) { -#ifdef SWIG_LINK_RUNTIME type_pointer = SWIG_ReturnGlobalTypeList((void *)0); + } #else - type_pointer = PyCapsule_Import(SWIGPY_CAPSULE_NAME, 0); - if (PyErr_Occurred()) { - PyErr_Clear(); - type_pointer = (void *)0; - } -#endif + void *type_pointer = PyCapsule_Import(SWIGPY_CAPSULE_NAME, 0); + if (PyErr_Occurred()) { + PyErr_Clear(); + type_pointer = (void *)0; } +#endif return (swig_module_info *) type_pointer; } -- cgit v1.2.1 From 117a917dd155258891a4557526b0888284128d2d Mon Sep 17 00:00:00 2001 From: benjamin-sch Date: Tue, 8 Feb 2022 09:04:30 +0100 Subject: added comment in CHANGES.current --- CHANGES.current | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.current b/CHANGES.current index 287325483..4bb88acf5 100644 --- a/CHANGES.current +++ b/CHANGES.current @@ -7,6 +7,10 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/ Version 4.1.0 (in progress) =========================== +2022-02-08: benjamin-sch + [Python] added an interpreter counter to fix deinitialization + issues if multiple subinterpreters are used + 2022-02-07: sethrj #2196 Add alternative syntax for specifying fragments in typemaps. -- cgit v1.2.1