Merge branch 'python_subinterpreter_issues'

* python_subinterpreter_issues:
  added comment in CHANGES.current
  always get the type_pointer from capsule instead of using a static variable as the value may change after re-initilization/due to subinterpreters
  added interpreter_counter to deinitialize only once in case of subinterpreters

Conflicts:
	CHANGES.current
	Lib/python/pyrun.swg
This commit is contained in:
William S Fulton 2022-10-05 23:18:15 +01:00
commit 45f4b4d936
2 changed files with 18 additions and 8 deletions

View File

@ -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-10-05: benjamin-sch
[Python] added an interpreter counter to fix deinitialization
issues if multiple subinterpreters are used
2022-10-05: olly, wsfulton
#672 Add support for parsing C++11 final classes such as:

View File

@ -1656,28 +1656,33 @@ 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
}
#else
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;
}
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) {
@ -1707,6 +1712,7 @@ SWIG_Python_SetModule(swig_module_info *swig_module) {
PyObject *pointer = PyCapsule_New((void *) swig_module, SWIGPY_CAPSULE_NAME, SWIG_Python_DestroyModule);
if (pointer && module) {
if (PyModule_AddObject(module, SWIGPY_CAPSULE_ATTR_NAME, pointer) == 0) {
++interpreter_counter;
Swig_Capsule_global = pointer;
} else {
Py_DECREF(pointer);