* Get the value of a Python unicode string object, returned as a newly * allocated char *. * * @param[in] py_str The unicode string object. * @param[out] outstr ptr to char * storage to be filled in. * * @return SRD_OK upon success, a (negative) error code otherwise. * The 'outstr' argument points to a malloc()ed string upon success. * * @private */
| 389 | * @private |
| 390 | */ |
| 391 | SRD_PRIV int py_str_as_str(PyObject *py_str, char **outstr) |
| 392 | { |
| 393 | PyObject *py_bytes; |
| 394 | char *str; |
| 395 | PyGILState_STATE gstate; |
| 396 | |
| 397 | gstate = PyGILState_Ensure(); |
| 398 | |
| 399 | if (!PyUnicode_Check(py_str)) { |
| 400 | srd_dbg("Object is not a string object."); |
| 401 | PyGILState_Release(gstate); |
| 402 | return SRD_ERR_PYTHON; |
| 403 | } |
| 404 | |
| 405 | py_bytes = PyUnicode_AsUTF8String(py_str); |
| 406 | if (py_bytes) { |
| 407 | str = g_strdup(PyBytes_AsString(py_bytes)); |
| 408 | Py_DECREF(py_bytes); |
| 409 | if (str) { |
| 410 | *outstr = str; |
| 411 | PyGILState_Release(gstate); |
| 412 | return SRD_OK; |
| 413 | } |
| 414 | } |
| 415 | srd_exception_catch(NULL, "Failed to extract string"); |
| 416 | |
| 417 | PyGILState_Release(gstate); |
| 418 | |
| 419 | return SRD_ERR_PYTHON; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | */ |
no test coverage detected