[PATCH 6/10] rlm_python: 06-cleanup-python-vptuple-ex-add-vp-tuple.patch
Paul P Komkoff Jr
i at stingr.net
Mon Jan 30 23:22:36 CET 2006
Cleanup python_vptuple (ex add_vp_tuple)
---
commit 8b5cb6f9d73fc724c0ad822fc83523e13d84801e
tree 399b39a24da14201a108d1875610894c0d620ab7
parent 113545c895789bc9e16b3321b7636bef01289d90
author <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:26:36 +0300
committer <stingray at boxster64.stingr.net> Sun, 29 Jan 2006 15:26:36 +0300
src/modules/rlm_python/rlm_python.c | 142 +++++++++++++----------------------
1 files changed, 52 insertions(+), 90 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
@@ -238,96 +238,58 @@ static int python_destroy() {
return 0;
}
-/* Tuple to value pair conversion */
-static void add_vp_tuple(VALUE_PAIR **vpp, PyObject *pValue,
- const char *function_name) {
- int i, outertuplesize;
- VALUE_PAIR *vp;
-
- /* If the Python function gave us None for the tuple, then just return. */
- if (pValue == Py_None) {
- return;
- }
-
- if (!PyTuple_Check(pValue)) {
- radlog(L_ERR, "%s: non-tuple passed", function_name);
- }
-
- /* Get the tuple size. */
- outertuplesize = PyTuple_Size(pValue);
-
- for (i = 0; i < outertuplesize; i++) {
- PyObject *pTupleElement = PyTuple_GetItem(pValue, i);
-
- if ((pTupleElement != NULL) &&
- (PyTuple_Check(pTupleElement))) {
-
- /* Check if it's a pair */
- int tuplesize;
-
- if ((tuplesize = PyTuple_Size(pTupleElement)) != 2) {
- radlog(L_ERR, "%s: tuple element %d is a tuple "
- " of size %d. must be 2\n", function_name,
- i, tuplesize);
- }
- else {
- PyObject *pString1, *pString2;
-
- pString1 = PyTuple_GetItem(pTupleElement, 0);
- pString2 = PyTuple_GetItem(pTupleElement, 1);
-
- /* xxx PyString_Check does not compile here */
- if ((pString1 != NULL) &&
- (pString2 != NULL) &&
- PyObject_TypeCheck(pString1,&PyString_Type) &&
- PyObject_TypeCheck(pString2,&PyString_Type)) {
-
-
- const char *s1, *s2;
-
- /* pairmake() will convert and find any
- * errors in the pair.
- */
-
- s1 = PyString_AsString(pString1);
- s2 = PyString_AsString(pString2);
-
- if ((s1 != NULL) && (s2 != NULL)) {
- radlog(L_DBG, "%s: %s = %s ",
- function_name, s1, s2);
-
- /* xxx Might need to support other T_OP */
- vp = pairmake(s1, s2, T_OP_EQ);
- if (vp != NULL) {
- pairadd(vpp, vp);
- radlog(L_DBG, "%s: s1, s2 OK\n",
- function_name);
- }
- else {
- radlog(L_DBG, "%s: s1, s2 FAILED\n",
- function_name);
- }
- }
- else {
- radlog(L_ERR, "%s: string conv failed\n",
- function_name);
- }
-
- }
- else {
- radlog(L_ERR, "%s: tuple element %d must be "
- "(string, string)", function_name, i);
- }
- }
- }
- else {
- radlog(L_ERR, "%s: tuple element %d is not a tuple\n",
- function_name, i);
- }
- }
-
+static void python_vptuple(VALUE_PAIR **vpp, PyObject *pValue, const char *funcname) {
+ int i;
+ int tuplesize;
+ VALUE_PAIR *vp;
+
+ /* If the Python function gave us None for the tuple, then just return. */
+ if (pValue == Py_None)
+ return;
+
+ if (!PyTuple_CheckExact(pValue)) {
+ radlog(L_ERR, "rlm_python:%s: non-tuple passed", funcname);
+ return;
+ }
+ /* Get the tuple tuplesize. */
+ tuplesize = PyTuple_GET_SIZE(pValue);
+ for (i = 0; i < tuplesize; i++) {
+ PyObject *pTupleElement = PyTuple_GET_ITEM(pValue, i);
+ PyObject *pStr1;
+ PyObject *pStr2;
+ int pairsize;
+ const char *s1;
+ const char *s2;
+
+ if (!PyTuple_CheckExact(pTupleElement)) {
+ radlog(L_ERR, "rlm_python:%s: tuple element %d is not a tuple", funcname, i);
+ continue;
+ }
+ /* Check if it's a pair */
+ if ((pairsize = PyTuple_GET_SIZE(pTupleElement)) != 2) {
+ radlog(L_ERR, "rlm_python:%s: tuple element %d is a tuple of size %d. Must be 2", funcname, i, pairsize);
+ continue;
+ }
+ pStr1 = PyTuple_GET_ITEM(pTupleElement, 0);
+ pStr2 = PyTuple_GET_ITEM(pTupleElement, 1);
+ if ((!PyString_CheckExact(pStr1)) || (!PyString_CheckExact(pStr2))) {
+ radlog(L_ERR, "rlm_python:%s: tuple element %d must be as (str, str)", funcname, i);
+ continue;
+ }
+ s1 = PyString_AsString(pStr1);
+ s2 = PyString_AsString(pStr2);
+ /* xxx Might need to support other T_OP */
+ vp = pairmake(s1, s2, T_OP_EQ);
+ if (vp != NULL) {
+ pairadd(vpp, vp);
+ radlog(L_DBG, "rlm_python:%s: '%s' = '%s'", funcname, s1, s2);
+ } else {
+ radlog(L_DBG, "rlm_python:%s: Failed: '%s' = '%s'", funcname, s1, s2);
+ }
+ }
}
+
/* This is the core Python function that the others wrap around.
* Pass the value-pair print strings in a tuple.
* xxx We're not checking the errors. If we have errors, what do we do?
@@ -483,11 +445,11 @@ static int python_function(REQUEST *requ
return_value = PyInt_AsLong(pTupleInt);
/* Reply item tuple */
- add_vp_tuple(&request->reply->vps,
+ python_vptuple(&request->reply->vps,
PyTuple_GetItem(pValue, 1), function_name);
/* Config item tuple */
- add_vp_tuple(&request->config_items,
+ python_vptuple(&request->config_items,
PyTuple_GetItem(pValue, 2), function_name);
}
}
--
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