| 444 | // String support |
| 445 | |
| 446 | const char* ConvertOneString(PyObject* v, string* out) { |
| 447 | if (PyBytes_Check(v)) { |
| 448 | out->assign(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v)); |
| 449 | return nullptr; |
| 450 | } |
| 451 | if (PyUnicode_Check(v)) { |
| 452 | #if PY_MAJOR_VERSION >= 3 |
| 453 | Py_ssize_t size; |
| 454 | const char* str = PyUnicode_AsUTF8AndSize(v, &size); |
| 455 | if (str == nullptr) return ErrorConvertingUnicodeString; |
| 456 | out->assign(str, size); |
| 457 | return nullptr; |
| 458 | #else |
| 459 | PyObject* py_str = PyUnicode_AsUTF8String(v); |
| 460 | if (py_str == nullptr) return ErrorConvertingUnicodeString; |
| 461 | out->assign(PyBytes_AS_STRING(py_str), PyBytes_GET_SIZE(py_str)); |
| 462 | Py_DECREF(py_str); |
| 463 | return nullptr; |
| 464 | #endif |
| 465 | } |
| 466 | return ErrorMixedTypes; |
| 467 | } |
| 468 | |
| 469 | DEFINE_HELPER(ConvertString, string, DT_STRING, ConvertOneString); |
| 470 | |