MCPcopy Create free account
hub / github.com/DreamSourceLab/DSView / py_obj_to_variant

Function py_obj_to_variant

libsigrokdecode4DSL/util.c:574–617  ·  view source on GitHub ↗

* Convert a Python scalar object to a GLib variant. * Supported variant types are string, int64 and double. * * @param[in] py_obj The Python object. Must not be NULL. * @return A floating reference to a new variant, or NULL on failure. * * @private */

Source from the content-addressed store, hash-verified

572 * @private
573 */
574SRD_PRIV GVariant *py_obj_to_variant(PyObject *py_obj)
575{
576 GVariant *var = NULL;
577 PyGILState_STATE gstate;
578
579 gstate = PyGILState_Ensure();
580
581 if (PyUnicode_Check(py_obj)) { /* string */
582 PyObject *py_bytes;
583 const char *str;
584
585 py_bytes = PyUnicode_AsUTF8String(py_obj);
586 if (py_bytes) {
587 str = PyBytes_AsString(py_bytes);
588 if (str)
589 var = g_variant_new_string(str);
590 Py_DECREF(py_bytes);
591 }
592 if (!var)
593 srd_exception_catch(NULL, "Failed to extract string value");
594 } else if (PyLong_Check(py_obj)) { /* integer */
595 int64_t val;
596
597 val = PyLong_AsLongLong(py_obj);
598 if (!PyErr_Occurred())
599 var = g_variant_new_int64(val);
600 else
601 srd_exception_catch(NULL, "Failed to extract integer value");
602 } else if (PyFloat_Check(py_obj)) { /* float */
603 double val;
604
605 val = PyFloat_AsDouble(py_obj);
606 if (!PyErr_Occurred())
607 var = g_variant_new_double(val);
608 else
609 srd_exception_catch(NULL, "Failed to extract float value");
610 } else {
611 srd_err("Failed to extract value of unsupported type.");
612 }
613
614 PyGILState_Release(gstate);
615
616 return var;
617}

Callers 1

get_optionsFunction · 0.85

Calls 1

srd_exception_catchFunction · 0.85

Tested by

no test coverage detected