| 186 | } |
| 187 | |
| 188 | Status PyObjectToString(PyObject* obj, const char** ptr, Py_ssize_t* len, |
| 189 | PyObject** ptr_owner) { |
| 190 | *ptr_owner = nullptr; |
| 191 | if (PyBytes_Check(obj)) { |
| 192 | char* buf; |
| 193 | if (PyBytes_AsStringAndSize(obj, &buf, len) != 0) { |
| 194 | return errors::Internal("Unable to get element as bytes."); |
| 195 | } |
| 196 | *ptr = buf; |
| 197 | return Status::OK(); |
| 198 | } else if (PyUnicode_Check(obj)) { |
| 199 | #if (PY_MAJOR_VERSION > 3 || (PY_MAJOR_VERSION == 3 && PY_MINOR_VERSION >= 3)) |
| 200 | *ptr = PyUnicode_AsUTF8AndSize(obj, len); |
| 201 | if (*ptr != nullptr) return Status::OK(); |
| 202 | #else |
| 203 | PyObject* utemp = PyUnicode_AsUTF8String(obj); |
| 204 | char* buf; |
| 205 | if (utemp != nullptr && PyBytes_AsStringAndSize(utemp, &buf, len) != -1) { |
| 206 | *ptr = buf; |
| 207 | *ptr_owner = utemp; |
| 208 | return Status::OK(); |
| 209 | } |
| 210 | Py_XDECREF(utemp); |
| 211 | #endif |
| 212 | return errors::Internal("Unable to convert element to UTF-8"); |
| 213 | } else { |
| 214 | return errors::Internal("Unsupported object type ", obj->ob_type->tp_name); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Iterate over the string array 'array', extract the ptr and len of each string |
| 219 | // element and call f(ptr, len). |
no test coverage detected