From: Nick Porter Date: Wed, 16 Apr 2025 10:24:53 +0000 (+0100) Subject: Update Python freeradius.log() to use optional args X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a63d4c081f456025474ad2b8228cf0bcce98f05d;p=thirdparty%2Ffreeradius-server.git Update Python freeradius.log() to use optional args And move module definition to global static --- diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c index 9b75d4059b2..007beacb685 100644 --- a/src/modules/rlm_python/rlm_python.c +++ b/src/modules/rlm_python/rlm_python.c @@ -127,6 +127,8 @@ static PyThreadState *global_interpreter; //!< Our first interpreter. static rlm_python_t const *current_inst = NULL; //!< Used for communication with inittab functions. static CONF_SECTION *current_conf; //!< Used for communication with inittab functions. +static PyObject *py_freeradius_log(UNUSED PyObject *self, PyObject *args, PyObject *kwds); + static libpython_global_config_t libpython_global_config = { .path = NULL, .path_include_default = true @@ -227,9 +229,41 @@ static struct { { NULL, 0 }, }; +#ifndef _PyCFunction_CAST +#define _PyCFunction_CAST(func) (PyCFunction)((void(*)(void))(func)) +#endif + /* - * radiusd Python functions + * freeradius Python functions */ +static PyMethodDef py_freeradius_methods[] = { + { "log", _PyCFunction_CAST(py_freeradius_log), METH_VARARGS | METH_KEYWORDS, + "freeradius.log(msg[, type, lvl])\n\n" + "Print a message using the freeradius daemon's logging system.\n" + "type should be one of the following constants:\n" + " freeradius.L_DBG\n" + " freeradius.L_INFO\n" + " freeradius.L_WARN\n" + " freeradius.L_ERR\n" + "lvl should be one of the following constants:\n" + " freeradius.L_DBG_LVL_OFF\n" + " freeradius.L_DBG_LVL_1\n" + " freeradius.L_DBG_LVL_2\n" + " freeradius.L_DBG_LVL_3\n" + " freeradius.L_DBG_LVL_4\n" + " freeradius.L_DBG_LVL_MAX\n" + }, + { NULL, NULL, 0, NULL }, +}; + +static PyModuleDef py_freeradius_def = { + PyModuleDef_HEAD_INIT, + .m_name = "freeradius", + .m_doc = "FreeRADIUS python module", + .m_size = 0, + .m_methods = py_freeradius_methods +}; + /** Return the module instance object associated with the thread state or interpreter state * */ @@ -266,29 +300,24 @@ static rlm_python_t const *rlm_python_get_inst(void) /** Allow fr_log to be called from python * */ -static PyObject *mod_log(UNUSED PyObject *module, PyObject *args) +static PyObject *py_freeradius_log(UNUSED PyObject *self, PyObject *args, PyObject *kwds) { - int status; - char *msg; + static char const *kwlist[] = { "msg", "type", "lvl", NULL }; + char *msg; + int type = L_DBG; + int lvl = L_DBG_LVL_2; + rlm_python_t const *inst = rlm_python_get_inst(); - if (!PyArg_ParseTuple(args, "is", &status, &msg)) { - Py_RETURN_NONE; - } + if (fr_debug_lvl < lvl) Py_RETURN_NONE; /* Don't bother parsing args */ - fr_log(&default_log, status, __FILE__, __LINE__, "%s", msg); + if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|ii", (char **)((uintptr_t)kwlist), + &msg, &type, &lvl)) Py_RETURN_NONE; + + fr_log(&default_log, type, __FILE__, __LINE__, "rlm_python (%s) - %s", inst->name, msg); Py_RETURN_NONE; } -static PyMethodDef module_methods[] = { - { "log", &mod_log, METH_VARARGS, - "freeradius.log(level, msg)\n\n" \ - "Print a message using the freeradius daemon's logging system. level should be one of the\n" \ - "following constants L_DBG, L_WARN, L_INFO, L_ERR, L_DBG_WARN, L_DBG_ERR, L_DBG_WARN_REQ, L_DBG_ERR_REQ\n" - }, - { NULL, NULL, 0, NULL }, -}; - /** Print out the current error * * Must be called with a valid thread state set @@ -933,17 +962,9 @@ static PyObject *python_module_init(void) { PyObject *module; - static struct PyModuleDef py_module_def = { - PyModuleDef_HEAD_INIT, - .m_name = "freeradius", - .m_doc = "freeRADIUS python module", - .m_size = 0, - .m_methods = module_methods - }; - fr_assert(current_inst); - module = PyModule_Create(&py_module_def); + module = PyModule_Create(&py_freeradius_def); if (!module) { python_error_log(current_inst, NULL); Py_RETURN_NONE; diff --git a/src/tests/modules/python/mod_shared_storage.py b/src/tests/modules/python/mod_shared_storage.py index 2ea8d898fdf..24a63d7230a 100644 --- a/src/tests/modules/python/mod_shared_storage.py +++ b/src/tests/modules/python/mod_shared_storage.py @@ -4,8 +4,8 @@ import shared def authorize(p): freeradius.log( - freeradius.L_DBG, "Python - shared_attribute=" + str(hasattr(shared, "shared_attribute")), + freeradius.L_DBG ) if not hasattr(shared, "shared_attribute"): setattr(shared, "shared_attribute", True) diff --git a/src/tests/modules/python/mod_thread_local_storage.py b/src/tests/modules/python/mod_thread_local_storage.py index 0f307ad1c50..52c23f8354e 100644 --- a/src/tests/modules/python/mod_thread_local_storage.py +++ b/src/tests/modules/python/mod_thread_local_storage.py @@ -8,7 +8,7 @@ local = threading.local() def authorize(p): global local freeradius.log( - freeradius.L_DBG, "Python - threading.local.tls()=" + str(hasattr(local, "tls")) + "Python - threading.local.tls()=" + str(hasattr(local, "tls")), freeradius.L_DBG ) if hasattr(local, "tls"): return freeradius.RLM_MODULE_OK