From c0c837b1be56056f8e6e3fe8934224b6bd9cc7aa Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Sun, 29 Dec 2024 16:20:40 +0100 Subject: [PATCH] python: set m_size for created modules to 0 This value determines the size of the per-module memory area. Setting this value to -1 as it was before this change means that the module has global state and therefore does not support subinterpreters. However, subinterpreters are used to run the Python scripts, so the weechat module has to support subinterpreters. Therefore we should set this value to 0 as no per-module memory is required. This seems to fix the crash reported in #2046 without the need for the workaround added in commit 85c7494dc (it does for me when testing with Python 3.12.0 at least). This change came up as a suggestion in cpython's issue tracker where it was pointed out that using modules with m_size set to -1 is not supported in subinterpreters. See these two comments: https://github.com/python/cpython/issues/116510#issuecomment-2377915771 https://github.com/python/cpython/issues/116510#issuecomment-2389485369 It's not completely clear to me what is required for a module to support subinterpreters and re-initialization (which is required for setting m_size to 0), but https://peps.pythondiscord.com/pep-0489/ says: A simple rule of thumb is: Do not define any static data, except built-in types with no mutable or user-settable class attributes. The only static data we define is of type int and str, so I think it should be fine. --- src/plugins/python/weechat-python.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/plugins/python/weechat-python.c b/src/plugins/python/weechat-python.c index 599d16bb6..903e5e559 100644 --- a/src/plugins/python/weechat-python.c +++ b/src/plugins/python/weechat-python.c @@ -86,7 +86,7 @@ static struct PyModuleDef moduleDef = { PyModuleDef_HEAD_INIT, "weechat", NULL, - -1, + 0, weechat_python_funcs, NULL, NULL, @@ -97,7 +97,7 @@ static struct PyModuleDef moduleDefOutputs = { PyModuleDef_HEAD_INIT, "weechatOutputs", NULL, - -1, + 0, weechat_python_output_funcs, NULL, NULL,