]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add py_freeradius_pair_str
authorNick Porter <nick@portercomputing.co.uk>
Wed, 16 Apr 2025 16:07:30 +0000 (17:07 +0100)
committerNick Porter <nick@portercomputing.co.uk>
Mon, 21 Apr 2025 11:30:24 +0000 (12:30 +0100)
Get the string representation of a pair

src/modules/rlm_python/rlm_python.c

index 48794e74d9bfaf04519c1286479a8926d47b41cb..13ca220d6012eeb9cbfed323a59f825907acb17c 100644 (file)
@@ -1026,6 +1026,48 @@ static int py_freeradius_pair_setvalue(PyObject *self, PyObject *value, UNUSED v
        return 0;
 }
 
+/** Return the string representation of a leaf pair node
+ *
+ * Called when the attribute is accessed in print() or str() or selective other
+ * magical Python contexts.
+ */
+static PyObject *py_freeradius_pair_str(PyObject *self) {
+       py_freeradius_pair_t    *own_self = (py_freeradius_pair_t *)self;
+       PyObject                *value = NULL;
+       fr_pair_t               *vp = own_self->vp;
+
+       if (!vp) return PyObject_Str(Py_None);
+
+       switch(vp->vp_type) {
+       case FR_TYPE_STRING:
+               value = PyUnicode_FromStringAndSize(vp->vp_strvalue, vp->vp_length);
+               break;
+
+       case FR_TYPE_NON_LEAF:
+               fr_assert(0);
+               break;
+
+       default:
+       {
+               ssize_t         slen;
+               char            buffer[1024];
+
+               slen = fr_value_box_print(&FR_SBUFF_OUT(buffer, sizeof(buffer)), &vp->data, NULL);
+               if (slen < 0) {
+               error:
+                       PyErr_Format(PyExc_MemoryError, "Failed casting %s to Python string", vp->da->name);
+                       return NULL;
+               }
+               value = PyUnicode_FromStringAndSize(buffer, (size_t)slen);
+       }
+               break;
+       }
+
+       if (value == NULL) goto error;
+
+       return value;
+}
+
 /** Print out the current error
  *
  * Must be called with a valid thread state set