[PATCH 8/10] rlm_python: 08-cleanup-python-load-function.patch

Paul P Komkoff Jr i at stingr.net
Mon Jan 30 23:25:35 CET 2006


Cleanup python_load_function

---
commit 3af430ff7cc509f59963279178e0909b9983af3a
tree 9eca021acb490e7b60269dd7f8d729be0f3c3a58
parent 97c0260f921dabc7d751a99c5ca7f07a1f916e8a
author <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:43:49 +0300
committer <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:43:49 +0300

 src/modules/rlm_python/rlm_python.c |   76 ++++++++++++++++++-----------------
 1 files changed, 40 insertions(+), 36 deletions(-)

diff --git a/src/modules/rlm_python/rlm_python.c b/src/modules/rlm_python/rlm_python.c
--- a/src/modules/rlm_python/rlm_python.c
+++ b/src/modules/rlm_python/rlm_python.c
@@ -425,36 +425,40 @@ failed:
 /*
  * Import a user module and load a function from it
  */
-static int load_python_function(const char* module, const char* func,
-				PyObject** pyModule, PyObject** pyFunc) {
 
-    if ((module==NULL) || (func==NULL)) {
-	*pyFunc=NULL;
-	*pyModule=NULL;
-    } else {
-	PyObject *pName;
-
-	pName = PyString_FromString(module);
-	Py_INCREF(pName);
-	*pyModule = PyImport_Import(pName);
-	Py_DECREF(pName);
-	if (*pyModule != NULL) {
-	    PyObject *pDict;
-
-	    pDict = PyModule_GetDict(*pyModule);
-	    /* pDict: borrowed reference */
-
-	    *pyFunc = PyDict_GetItemString(pDict, func);
-	    /* pFunc: Borrowed reference */
-	} else {
-	    python_error();
-
-	    radlog(L_ERR, "Failed to import python module \"%s\"\n", module);
-	    return -1;
-	}
-    }
-
-    return 0;
+static int python_load_function(char *module, const char *func, PyObject **pModule, PyObject **pFunc) {
+        const char      funcname[] = "python_load_function";
+        PyGILState_STATE gstate;
+
+        *pFunc = NULL;
+        *pModule = NULL;
+        gstate = PyGILState_Ensure();
+
+        if (module != NULL && func != NULL) {
+                if ((*pModule = PyImport_ImportModule(module)) == NULL) {
+                        radlog(L_ERR, "rlm_python:%s: module '%s' is not found", funcname, module);
+                        goto failed;
+                }
+                if ((*pFunc = PyObject_GetAttrString(*pModule, func)) == NULL) {
+                        radlog(L_ERR, "rlm_python:%s: function '%s.%s' is not found", funcname, module, func);
+                        goto failed;
+                }
+                if (!PyCallable_Check(*pFunc)) {
+                        radlog(L_ERR, "rlm_python:%s: function '%s.%s' is not callable", funcname, module, func);
+                        goto failed;
+                }
+        }
+	PyGILState_Release(gstate);
+        return 0;
+failed:
+	PyGILState_Release(gstate);
+        python_error();
+        radlog(L_ERR, "rlm_python:%s: failed to import python function '%s.%s'", funcname, module, func);
+        Py_XDECREF(*pFunc);
+        *pFunc = NULL;
+        Py_XDECREF(*pModule);
+        *pModule = NULL;
+        return -1;
 }
 
 
@@ -496,43 +500,43 @@ static int python_instantiate(CONF_SECTI
      * Import user modules.
      */
 
-    if (load_python_function(data->mod_instantiate, data->func_instantiate,
+    if (python_load_function(data->mod_instantiate, data->func_instantiate,
 		&data->pModule_instantiate, &data->pFunc_instantiate)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
     }
 
-    if (load_python_function(data->mod_authenticate, data->func_authenticate,
+    if (python_load_function(data->mod_authenticate, data->func_authenticate,
 		&data->pModule_authenticate, &data->pFunc_authenticate)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
     }
 
-    if (load_python_function(data->mod_authorize, data->func_authorize,
+    if (python_load_function(data->mod_authorize, data->func_authorize,
 		&data->pModule_authorize, &data->pFunc_authorize)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
     }
 
-    if (load_python_function(data->mod_preacct, data->func_preacct,
+    if (python_load_function(data->mod_preacct, data->func_preacct,
 		&data->pModule_preacct, &data->pFunc_preacct)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
     }
 
-    if (load_python_function(data->mod_accounting, data->func_accounting,
+    if (python_load_function(data->mod_accounting, data->func_accounting,
 		&data->pModule_accounting, &data->pFunc_accounting)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
     }
 
-    if (load_python_function(data->mod_checksimul, data->func_checksimul,
+    if (python_load_function(data->mod_checksimul, data->func_checksimul,
 		&data->pModule_checksimul, &data->pFunc_checksimul)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
     }
 
-    if (load_python_function(data->mod_detach, data->func_detach,
+    if (python_load_function(data->mod_detach, data->func_detach,
 		&data->pModule_detach, &data->pFunc_detach)==-1) {
 	/* TODO: check if we need to cleanup data */
 	return -1;
-- 
Paul P 'Stingray' Komkoff Jr // http://stingr.net/key <- my pgp key
 This message represents the official view of the voices in my head



More information about the Freeradius-Devel mailing list