- 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.
This commit is contained in:
George Thessalonikefs 2023-10-16 15:47:18 +02:00
parent 07149f576a
commit 63a5280f8f
12 changed files with 38 additions and 18 deletions

View 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

View 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):

View 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

View 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.

View 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

View 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,

View 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.

View 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):

View 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
* ************************************************************************************ */

View 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");

View 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):

View File

@ -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):