diff --git a/doc/Changelog b/doc/Changelog index 195e3b170..ff12a6b46 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -5,6 +5,9 @@ - Expose the configured listening and outgoing interfaces, if any, as a list of strings in the Python 'config_file' class instead of the current Swig object proxy; fixes #79. + - For multi Python module setups, clean previously parsed module + functions in __main__'s dictionary, if any, so that only current + module functions are registered. 13 October 2023: George - Better fix for infinite loop when reading multiple lines of input on diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c index e655066cb..c6294a1d5 100644 --- a/pythonmod/pythonmod.c +++ b/pythonmod/pythonmod.c @@ -112,6 +112,34 @@ struct pythonmod_qstate { PyObject* data; }; +/* The dict from __main__ could have remnants from a previous script + * invocation, in a multi python module setup. Usually this is fine since newer + * scripts will update their values. The obvious erroneous case is when mixing + * python scripts that make use of both 'init' and 'init_standard'. This + * results in 'init_standard' to persist on following scripts that don't use it + * (thus not replacing it). This is also problematic in case where a script + * does not define a required function but a previously loaded script did. The + * current solution is to make sure to clean offensive remnants that influence + * further parsing of the individual scripts. + */ +static void +clean_python_function_objects(PyObject* dict) { + const char* function_names[] = { + "init", + "init_standard", + "deinit", + "operate", + "inform_super" + }; + size_t i; + + for(i=0; imodule); pe->dict = PyModule_GetDict(pe->module); Py_XINCREF(pe->dict); + clean_python_function_objects(pe->dict); + pe->data = PyDict_New(); /* add the script filename to the global "mod_env" for trivial access */ fname = PyString_FromString(pe->fname);