Fix string conversion for Python 3.

PiperOrigin-RevId: 265754427
This commit is contained in:
A. Unique TensorFlower 2019-08-27 13:32:38 -07:00 committed by TensorFlower Gardener
parent 5bd0e82817
commit e9c7e5357a

View File

@ -45,8 +45,19 @@ static bool PyListToStdVectorString(PyObject *list, std::vector<std::string> *st
strings->resize(list_size);
for (int k = 0; k < list_size; k++) {
PyObject *string_py = PyList_GetItem(list, k);
if (!PyString_Check(string_py)) return false;
(*strings)[k] = std::string(PyString_AsString(string_py));
if (PyString_Check(string_py)) {
(*strings)[k] = PyString_AsString(string_py);
} else if (PyUnicode_Check(string_py)) {
// First convert the PyUnicode to a PyString.
PyObject *utf8_string_py = PyUnicode_AsUTF8String(string_py);
if (!utf8_string_py) return false;
// Then convert it to a regular std::string.
(*strings)[k] = PyString_AsString(utf8_string_py);
Py_DECREF(utf8_string_py);
} else {
return false;
}
}
return true;
}