* Convert a Python list of unicode strings to a C string vector. * On success, a pointer to a newly allocated NULL-terminated array of * allocated C strings is written to @a out_strv. The caller must g_free() * each string and the array itself. * * @param[in] py_strseq The sequence object. * @param[out] out_strv Address of string vector to be filled in. * * @return SRD_OK upon success, a (
| 487 | * @private |
| 488 | */ |
| 489 | SRD_PRIV int py_strseq_to_char(PyObject *py_strseq, char ***out_strv) |
| 490 | { |
| 491 | PyObject *py_item, *py_bytes; |
| 492 | char **strv, *str; |
| 493 | ssize_t seq_len, i; |
| 494 | PyGILState_STATE gstate; |
| 495 | int ret = SRD_ERR_PYTHON; |
| 496 | int lv = 0; |
| 497 | char dec_buf[15]; |
| 498 | |
| 499 | gstate = PyGILState_Ensure(); |
| 500 | |
| 501 | str = NULL; |
| 502 | strv = NULL; |
| 503 | if (!PySequence_Check(py_strseq)) { |
| 504 | srd_err("Object does not provide sequence protocol."); |
| 505 | goto err; |
| 506 | } |
| 507 | |
| 508 | seq_len = PySequence_Size(py_strseq); |
| 509 | if (seq_len < 0) { |
| 510 | srd_exception_catch(NULL, "Failed to obtain sequence size"); |
| 511 | goto err; |
| 512 | } |
| 513 | |
| 514 | strv = g_try_new0(char *, seq_len + 1); |
| 515 | if (!strv) { |
| 516 | srd_err("Failed to allocate result string vector."); |
| 517 | ret = SRD_ERR_MALLOC; |
| 518 | goto err; |
| 519 | } |
| 520 | |
| 521 | for (i = 0; i < seq_len; i++) { |
| 522 | py_item = PySequence_GetItem(py_strseq, i); |
| 523 | if (!py_item) |
| 524 | goto err; |
| 525 | |
| 526 | if (PyUnicode_Check(py_item)) |
| 527 | { |
| 528 | py_bytes = PyUnicode_AsUTF8String(py_item); |
| 529 | Py_DECREF(py_item); |
| 530 | if (!py_bytes) |
| 531 | goto err; |
| 532 | |
| 533 | str = g_strdup(PyBytes_AsString(py_bytes)); |
| 534 | Py_DECREF(py_bytes); |
| 535 | if (!str) |
| 536 | goto err; |
| 537 | } |
| 538 | else if (PyLong_Check(py_item)) |
| 539 | { |
| 540 | lv = PyLong_AsLong(py_item); |
| 541 | sprintf(dec_buf, "%d", lv); |
| 542 | str = g_strdup(dec_buf); |
| 543 | } |
| 544 | else{ |
| 545 | Py_DECREF(py_item); |
| 546 | goto err; |
no test coverage detected