]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add functions to fetch module instance from python state
authorNick Porter <nick@portercomputing.co.uk>
Wed, 16 Apr 2025 10:23:02 +0000 (11:23 +0100)
committerNick Porter <nick@portercomputing.co.uk>
Mon, 21 Apr 2025 11:30:21 +0000 (12:30 +0100)
The dict `__State` entry will be added later

src/modules/rlm_python/rlm_python.c

index cba7acfd3d09fd7cdb35a0de5d0159bc4de4b182..9b75d4059b2393291d57af433041abc135b9c826 100644 (file)
@@ -230,6 +230,38 @@ static struct {
 /*
  *     radiusd Python functions
  */
+/** Return the module instance object associated with the thread state or interpreter state
+ *
+ */
+static inline CC_HINT(always_inline) py_freeradius_state_t *rlm_python_state_obj(void)
+{
+       PyObject *dict;
+
+       dict = PyThreadState_GetDict(); /* If this is NULL, we're dealing with the main interpreter */
+       if (!dict) {
+               PyObject *module;
+
+               module = PyState_FindModule(&py_freeradius_def);
+               if (unlikely(!module)) return NULL;
+
+               dict = PyModule_GetDict(module);
+               if (unlikely(!dict)) return NULL;
+       }
+
+       return (py_freeradius_state_t *)PyDict_GetItemString(dict, "__State");
+}
+
+/** Return the rlm_python instance associated with the current interpreter
+ *
+ */
+static rlm_python_t const *rlm_python_get_inst(void)
+{
+       py_freeradius_state_t const *p_state;
+
+       p_state = rlm_python_state_obj();
+       if (unlikely(!p_state)) return NULL;
+       return p_state->inst;
+}
 
 /** Allow fr_log to be called from python
  *