[PATCH 4/10] rlm_python: 04-properly-initialize-radiusd-module-in-python-init.patch

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


Properly initialize radiusd_module (in python_init)
Cleanup python_error

---
commit 2803bf1c6d966ed8b5a1ea6deb54ab728df32277
tree dfa2a7567c484f4a07a5d4bb9e27fbb87f2d067d
parent 850e29fc4532bed1394abde8aab371b14f92d5f7
author <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:18:36 +0300
committer <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:18:36 +0300

 src/modules/rlm_python/rlm_python.c |   90 ++++++++++++++++-------------------
 1 files changed, 42 insertions(+), 48 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
@@ -181,36 +181,55 @@ static PyMethodDef radiusd_methods[] = {
     {NULL, NULL, 0, NULL}
 };
 
-/*
- *	Do any per-module initialization.  e.g. set up connections
- *	to external databases, read configuration files, set up
- *	dictionary entries, etc.
- *
- *	Try to avoid putting too much stuff in here - it's better to
- *	do it in instantiate() where it is not global.
- */
-static int python_init(void)
+static void python_error() {
+    PyObject        *pType = NULL;
+    PyObject        *pValue = NULL;
+    PyObject        *pTraceback = NULL;
+    PyObject        *pStr1 = NULL;
+    PyObject        *pStr2 = NULL;
+
+    PyErr_Fetch(&pType, &pValue, &pTraceback);
+    if (pType == NULL || pValue == NULL)
+        goto failed;
+    if ((pStr1 = PyObject_Str(pType)) == NULL || (pStr2 = PyObject_Str(pValue)) == NULL)
+        goto failed;
+    radlog(L_ERR, "rlm_python:EXCEPT:%s: %s", PyString_AsString(pStr1), PyString_AsString(pStr2));
+
+failed:
+    Py_XDECREF(pStr1);
+    Py_XDECREF(pStr2);
+    Py_XDECREF(pType);
+    Py_XDECREF(pValue);
+    Py_XDECREF(pTraceback);
+}
+
+static int python_init()
 {
-    /*
-     * Initialize Python interpreter. Fatal error if this fails.
-     */
+    int i;
+
+    Py_SetProgramName("radiusd");
+
     Py_Initialize();
 
-    radlog(L_DBG, "python_init done");
+    PyEval_InitThreads(); // This also grabs a lock
 
-    return 0;
-}
+    if ((radiusd_module = Py_InitModule3("radiusd", radiusd_methods, "FreeRADIUS Module.")) == NULL)
+        goto failed;
 
-/* Extract string representation of Python error. */
-static void python_error(void) {
-    PyObject *pType, *pValue, *pTraceback, *pStr1, *pStr2;
+    for (i = 0; radiusd_constants[i].name; i++)
+        if ((PyModule_AddIntConstant(radiusd_module, radiusd_constants[i].name, radiusd_constants[i].value)) < 0)
+            goto failed;
 
-    PyErr_Fetch(&pType, &pValue, &pTraceback);
-    pStr1 = PyObject_Str(pType);
-    pStr2 = PyObject_Str(pValue);
+    PyEval_ReleaseLock(); // Drop lock grabbed by InitThreads
 
-    radlog(L_ERR, "%s: %s\n",
-	   PyString_AsString(pStr1), PyString_AsString(pStr2));
+    radlog(L_DBG, "python_init done");
+
+    return 0;
+
+failed:
+    python_error();
+    Py_Finalize();
+    return -1;
 }
 
 /* Tuple to value pair conversion */
@@ -566,7 +585,6 @@ static int load_python_function(const ch
 static int python_instantiate(CONF_SECTION *conf, void **instance)
 {
     struct rlm_python_t *data;
-    int idx;
 
     /*
 	 *	Set up a storage area for instance data
@@ -588,30 +606,6 @@ static int python_instantiate(CONF_SECTI
 
 
     /*
-     * Setup our 'radiusd' module.
-     */
-
-    /* Code */
-    if ((radiusd_module =
-	 Py_InitModule3("radiusd", radiusd_methods,
-			"FreeRADIUS Module.")) == NULL) {
-
-	radlog(L_ERR, "Python Py_InitModule3 failed");
-	free(data);
-	return -1;
-    }
-
-    /*
-     * Load constants into module
-     */
-    for (idx=0; radiusd_constants[idx].name; idx++)
-	if ((PyModule_AddIntConstant(radiusd_module, radiusd_constants[idx].name, radiusd_constants[idx].value)) == -1) {
-
-	    radlog(L_ERR, "Python AddIntConstant failed");
-	}
-
-
-    /*
      * Import user modules.
      */
 
-- 
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