aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/lib/core/py_func.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/python/lib/core/py_func.cc')
-rw-r--r--tensorflow/python/lib/core/py_func.cc19
1 files changed, 15 insertions, 4 deletions
diff --git a/tensorflow/python/lib/core/py_func.cc b/tensorflow/python/lib/core/py_func.cc
index dc56b39486..d3bfa0ee33 100644
--- a/tensorflow/python/lib/core/py_func.cc
+++ b/tensorflow/python/lib/core/py_func.cc
@@ -31,6 +31,7 @@ limitations under the License.
#include "tensorflow/python/lib/core/ndarray_tensor_bridge.h"
#include "tensorflow/python/lib/core/py_util.h"
#include "tensorflow/python/lib/core/safe_ptr.h"
+
#include <Python.h>
namespace tensorflow {
@@ -141,7 +142,8 @@ bool IsSingleNone(PyObject* obj) {
return false;
}
std::array<npy_intp, 0> indices;
- char* item_ptr = static_cast<char*>(PyArray_GetPtr(array_obj, indices.data()));
+ char* item_ptr =
+ static_cast<char*>(PyArray_GetPtr(array_obj, indices.data()));
PyObject* item = PyArray_GETITEM(array_obj, item_ptr);
CHECK(item);
return item == Py_None;
@@ -301,13 +303,22 @@ Status ConvertNdarrayToTensor(PyObject* obj, Tensor* ret) {
if (PyBytes_AsStringAndSize(input_data[i], &el, &el_size) == -1) {
#if PY_MAJOR_VERSION >= 3
el = PyUnicode_AsUTF8AndSize(input_data[i], &el_size);
- if (!el) {
+#else
+ el = nullptr;
+ if (PyUnicode_Check(input_data[i])) {
+ PyObject* unicode = PyUnicode_AsUTF8String(input_data[i]);
+ if (unicode) {
+ if (PyString_AsStringAndSize(unicode, &el, &el_size) == -1) {
+ Py_DECREF(unicode);
+ el = nullptr;
+ }
+ }
+ }
#endif
+ if (!el) {
return errors::Unimplemented("Unsupported object type ",
input_data[i]->ob_type->tp_name);
-#if PY_MAJOR_VERSION >= 3
}
-#endif
}
tflat(i) = string(el, el_size);
}