]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
- Expose the script filename in the Python module environment 'mod_env'
authorGeorge Thessalonikefs <yorgos@nlnetlabs.nl>
Mon, 16 Oct 2023 13:47:18 +0000 (15:47 +0200)
committerGeorge Thessalonikefs <yorgos@nlnetlabs.nl>
Mon, 16 Oct 2023 13:47:18 +0000 (15:47 +0200)
  instead of the config_file structure which includes the linked list
  of scripts in a multi Python module setup; fixes #79.

12 files changed:
doc/Changelog
pythonmod/doc/examples/example0-1.py
pythonmod/doc/examples/example0.rst
pythonmod/doc/modules/config.rst
pythonmod/doc/modules/env.rst
pythonmod/examples/edns.py
pythonmod/examples/inplace_callbacks.py
pythonmod/examples/log.py
pythonmod/interface.i
pythonmod/pythonmod.c
pythonmod/ubmodule-msg.py
pythonmod/ubmodule-tst.py

index eb2dae2ad75a09699b8c7403e2a92d2c4cbe8492..893dfc2d5d31cb49de2526c64fea111fbabe32c7 100644 (file)
@@ -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
index 7904f73a55e8a1cb1e5822e74cf6fff7c1ddfd71..506235eb4205361cc018c967d337b1e4f36d02e0 100644 (file)
@@ -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):
index 693972a141a4f2a863abdb36a864257f78c5b9f5..cee551de0259ee9f5c4f88425930825ff7dcef4e 100644 (file)
@@ -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
 
 
index 89afbef8ac2cef829849c1db659d806dd48dec9b..1298725f273ed15e9ce108d2f3f15ddf1b8e24d2 100644 (file)
@@ -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.
index eae4c73c7cfd9dff8d380a6b789be489f9c0b207..be5c3b1dbe6c227070f5cd7dad6c698d4c7d826f 100644 (file)
@@ -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
index ddcccc51c9bdf90b9bd2bd80977ca85ecd334c33..4e2eebd4fd1de4888f0470c1c1cbbcc851ada98e 100644 (file)
@@ -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,
index e1caaecc74e78d5eb6617d256c577c7e3e9c979d..42806daa107a606ab520d54844cb3a2f0bdafbc5 100644 (file)
@@ -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.
index c17106b0f268dd65979db6cc7695b2e7e9b405dd..03f74196264403291d14d79416296386de0a0be4 100644 (file)
@@ -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):
index a436389e1184ec1179b04fe22d2879bfec97314b..d31c7ae558b69ab24b4573064d2dfdb4aebb5469 100644 (file)
@@ -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
  * ************************************************************************************ */
index 628308612ac08f7813c7fbef9fa898fc747c3294..e655066cb21fac44d99a73d5a1d73d752467a595 100644 (file)
@@ -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");
index 648368080c07a2c463068d34cb15669bb5dd2ac5..6a690e281de5c947f9c87c70182f12378ce84b38 100644 (file)
@@ -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):
index 70729071c18124e8a93c28f09a4730fb606b8779..07543e399d0dd8f5bf696fcd95ce8ff8084b51de 100644 (file)
  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):