- list_len = PyList_Size((PyObject *)py_strlist);
- if (!(out = g_try_malloc(sizeof(char *) * (list_len + 1)))) {
- srd_err("Failed to g_malloc() 'out'.");
- return SRD_ERR_MALLOC;
- }
- for (i = 0; i < list_len; i++) {
- if (!(py_str = PyUnicode_AsEncodedString(
- PyList_GetItem((PyObject *)py_strlist, i), "utf-8", NULL)))
- return SRD_ERR_PYTHON;
- if (!(str = PyBytes_AS_STRING(py_str)))
- return SRD_ERR_PYTHON;
- out[i] = g_strdup(str);
+ if (PyUnicode_Check(py_obj)) { /* string */
+ PyObject *py_bytes;
+ const char *str;
+
+ py_bytes = PyUnicode_AsUTF8String(py_obj);
+ if (py_bytes) {
+ str = PyBytes_AsString(py_bytes);
+ if (str)
+ var = g_variant_new_string(str);
+ Py_DECREF(py_bytes);
+ }
+ if (!var)
+ srd_exception_catch("Failed to extract string value");
+
+ } else if (PyLong_Check(py_obj)) { /* integer */
+ int64_t val;
+
+ val = PyLong_AsLongLong(py_obj);
+ if (!PyErr_Occurred())
+ var = g_variant_new_int64(val);
+ else
+ srd_exception_catch("Failed to extract integer value");
+
+ } else if (PyFloat_Check(py_obj)) { /* float */
+ double val;
+
+ val = PyFloat_AsDouble(py_obj);
+ if (!PyErr_Occurred())
+ var = g_variant_new_double(val);
+ else
+ srd_exception_catch("Failed to extract float value");
+
+ } else {
+ srd_err("Failed to extract value of unsupported type.");