From 63a5280f8f56b71ac73b3df50bf2e393626c0b20 Mon Sep 17 00:00:00 2001 From: George Thessalonikefs Date: Mon, 16 Oct 2023 15:47:18 +0200 Subject: [PATCH] - Expose the script filename in the Python module environment 'mod_env' instead of the config_file structure which includes the linked list of scripts in a multi Python module setup; fixes #79. --- doc/Changelog | 5 +++++ pythonmod/doc/examples/example0-1.py | 4 ++-- pythonmod/doc/examples/example0.rst | 4 ++-- pythonmod/doc/modules/config.rst | 3 ++- pythonmod/doc/modules/env.rst | 7 +++++-- pythonmod/examples/edns.py | 2 +- pythonmod/examples/inplace_callbacks.py | 2 +- pythonmod/examples/log.py | 2 +- pythonmod/interface.i | 8 ++++++++ pythonmod/pythonmod.c | 10 +++++++++- pythonmod/ubmodule-msg.py | 2 +- pythonmod/ubmodule-tst.py | 7 +------ 12 files changed, 38 insertions(+), 18 deletions(-) diff --git a/doc/Changelog b/doc/Changelog index eb2dae2ad..893dfc2d5 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,8 @@ +16 October 2023: George + - Expose the script filename in the Python module environment 'mod_env' + instead of the config_file structure which includes the linked list + of scripts in a multi Python module setup; fixes #79. + 13 October 2023: George - Better fix for infinite loop when reading multiple lines of input on a broken remote control socket, by treating a zero byte line the diff --git a/pythonmod/doc/examples/example0-1.py b/pythonmod/doc/examples/example0-1.py index 7904f73a5..506235eb4 100644 --- a/pythonmod/doc/examples/example0-1.py +++ b/pythonmod/doc/examples/example0-1.py @@ -1,9 +1,9 @@ def init(id, cfg): - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, cfg.python_script)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script'])) return True def init_standard(id, env): - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, env.cfg.port, env.cfg.python_script)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, env.cfg.port, mod_env['script'])) return True def deinit(id): diff --git a/pythonmod/doc/examples/example0.rst b/pythonmod/doc/examples/example0.rst index 693972a14..cee551de0 100644 --- a/pythonmod/doc/examples/example0.rst +++ b/pythonmod/doc/examples/example0.rst @@ -50,7 +50,7 @@ Script file must contain four compulsory functions: :: def init(id, cfg): - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, cfg.python_script)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script'])) return True @@ -69,7 +69,7 @@ Script file must contain four compulsory functions: :: def init_standard(id, env): - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, env.cfg.port, env.cfg.python_script)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, env.cfg.port, mod_env['script'])) return True diff --git a/pythonmod/doc/modules/config.rst b/pythonmod/doc/modules/config.rst index 89afbef8a..1298725f2 100644 --- a/pythonmod/doc/modules/config.rst +++ b/pythonmod/doc/modules/config.rst @@ -339,4 +339,5 @@ config_file .. attribute:: python_script - Python script file. + Linked list of Python script files. + Deprecated; `mod_env['script']` should be used instead. diff --git a/pythonmod/doc/modules/env.rst b/pythonmod/doc/modules/env.rst index eae4c73c7..be5c3b1db 100644 --- a/pythonmod/doc/modules/env.rst +++ b/pythonmod/doc/modules/env.rst @@ -6,8 +6,11 @@ Global variables .. envvar:: mod_env - Module environment, contains data pointer for module-specific data. - See :class:`pythonmod_env`. + Module environment, it is the 'data' pointer for module-specific data + in :class:`pythonmod_env`. + It is initialized as a dictionary with the 'script' key pointing to the + module's python script. + It can be further populated during runtime for module-specific data. Predefined constants diff --git a/pythonmod/examples/edns.py b/pythonmod/examples/edns.py index ddcccc51c..4e2eebd4f 100644 --- a/pythonmod/examples/edns.py +++ b/pythonmod/examples/edns.py @@ -80,7 +80,7 @@ def init_standard(id, env): ..note:: The previously accessible configuration options can now be found in env.cfg. """ - log_info("python: inited script {}".format(env.cfg.python_script)) + log_info("python: inited script {}".format(mod_env['script'])) # Register EDNS option 65001 as a known EDNS option. if not register_edns_option(env, 65001, bypass_cache_stage=True, diff --git a/pythonmod/examples/inplace_callbacks.py b/pythonmod/examples/inplace_callbacks.py index e1caaecc7..42806daa1 100644 --- a/pythonmod/examples/inplace_callbacks.py +++ b/pythonmod/examples/inplace_callbacks.py @@ -287,7 +287,7 @@ def init_standard(id, env): env.cfg. """ - log_info("python: inited script {}".format(env.cfg.python_script)) + log_info("python: inited script {}".format(mod_env['script'])) # Register the inplace_reply_callback function as an inplace callback # function when answering a resolved query. diff --git a/pythonmod/examples/log.py b/pythonmod/examples/log.py index c17106b0f..03f741962 100644 --- a/pythonmod/examples/log.py +++ b/pythonmod/examples/log.py @@ -87,7 +87,7 @@ def logDnsMsg(qstate): print "-"*100 def init(id, cfg): - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, cfg.python_script)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script'])) return True def deinit(id): diff --git a/pythonmod/interface.i b/pythonmod/interface.i index a436389e1..d31c7ae55 100644 --- a/pythonmod/interface.i +++ b/pythonmod/interface.i @@ -952,6 +952,7 @@ struct config_str2list { /* ************************************************************************************ * Structure config_file * ************************************************************************************ */ +%ignore config_file::python_script; struct config_file { int verbosity; int stat_interval; @@ -1035,6 +1036,13 @@ struct config_file { struct config_strlist* python_script; }; +%extend config_file { + %pythoncode %{ + def _deprecated_python_script(self): return "cfg.python_script is deprecated, you can use `mod_env['script']` instead." + python_script = property(_deprecated_python_script) + %} +} + /* ************************************************************************************ * ASN: Adding structures related to forwards_lookup and dns_cache_find_delegation * ************************************************************************************ */ diff --git a/pythonmod/pythonmod.c b/pythonmod/pythonmod.c index 628308612..e655066cb 100644 --- a/pythonmod/pythonmod.c +++ b/pythonmod/pythonmod.c @@ -269,7 +269,7 @@ int pythonmod_init(struct module_env* env, int id) /* Initialize module */ FILE* script_py = NULL; - PyObject* py_init_arg = NULL, *res = NULL; + PyObject* py_init_arg = NULL, *res = NULL, *fname = NULL; PyGILState_STATE gil; int init_standard = 1, i = 0; #if PY_MAJOR_VERSION < 3 @@ -419,6 +419,14 @@ int pythonmod_init(struct module_env* env, int id) pe->dict = PyModule_GetDict(pe->module); Py_XINCREF(pe->dict); pe->data = PyDict_New(); + /* add the script filename to the global "mod_env" for trivial access */ + fname = PyString_FromString(pe->fname); + if(PyDict_SetItemString(pe->data, "script", fname) < 0) { + log_err("pythonmod: could not add item to dictionary"); + Py_XDECREF(fname); + goto python_init_fail; + } + Py_XDECREF(fname); Py_XINCREF(pe->data); /* reference will be stolen below */ if(PyModule_AddObject(pe->module, "mod_env", pe->data) < 0) { log_err("pythonmod: could not add mod_env object"); diff --git a/pythonmod/ubmodule-msg.py b/pythonmod/ubmodule-msg.py index 648368080..6a690e281 100644 --- a/pythonmod/ubmodule-msg.py +++ b/pythonmod/ubmodule-msg.py @@ -35,7 +35,7 @@ import os def init(id, cfg): - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, cfg.python_script)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script'])) return True def deinit(id): diff --git a/pythonmod/ubmodule-tst.py b/pythonmod/ubmodule-tst.py index 70729071c..07543e399 100644 --- a/pythonmod/ubmodule-tst.py +++ b/pythonmod/ubmodule-tst.py @@ -33,12 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. ''' def init(id, cfg): - scripts=[] - s = cfg.python_script - while s != None: - scripts.append(s.str) - s = s.next - log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, scripts)) + log_info("pythonmod: init called, module id is %d port: %d script: %s" % (id, cfg.port, mod_env['script'])) return True def deinit(id):