Fix CreateWrapperCPPFromBuffer properly by aselle@.
authorYifei Feng <yifeif@google.com>
Wed, 30 May 2018 20:57:18 +0000 (13:57 -0700)
committerYifei Feng <yifeif@google.com>
Wed, 30 May 2018 20:57:18 +0000 (13:57 -0700)
tensorflow/contrib/lite/python/interpreter.py
tensorflow/contrib/lite/python/interpreter_wrapper/interpreter_wrapper.cc
tensorflow/contrib/lite/python/interpreter_wrapper/interpreter_wrapper.h

index 2196805..779bda4 100644 (file)
@@ -54,7 +54,7 @@ class Interpreter(object):
     elif model_content and not model_path:
       self._interpreter = (
           _interpreter_wrapper.InterpreterWrapper_CreateWrapperCPPFromBuffer(
-              str(model_content), len(model_content)))
+              model_content))
       if not self._interpreter:
         raise ValueError(
             'Failed to create model from {} bytes'.format(len(model_content)))
index 6b12c91..5f304ad 100644 (file)
@@ -333,9 +333,14 @@ InterpreterWrapper* InterpreterWrapper::CreateWrapperCPPFromFile(
 }
 
 InterpreterWrapper* InterpreterWrapper::CreateWrapperCPPFromBuffer(
-    const char* data, size_t len) {
+    PyObject* data) {
+  char * buf = nullptr;
+  Py_ssize_t length;
+  if (PY_TO_CPPSTRING(data, &buf, &length) == -1) {
+    return nullptr;
+  }
   std::unique_ptr<tflite::FlatBufferModel> model =
-      tflite::FlatBufferModel::BuildFromBuffer(data, len);
+      tflite::FlatBufferModel::BuildFromBuffer(buf, length);
   return model ? new InterpreterWrapper(std::move(model)) : nullptr;
 }
 
index 0972c57..01320af 100644 (file)
@@ -40,8 +40,7 @@ class InterpreterWrapper {
   static InterpreterWrapper* CreateWrapperCPPFromFile(const char* model_path);
 
   // SWIG caller takes ownership of pointer.
-  static InterpreterWrapper* CreateWrapperCPPFromBuffer(const char* data,
-                                                        size_t len);
+  static InterpreterWrapper* CreateWrapperCPPFromBuffer(PyObject* data);
 
   ~InterpreterWrapper();
   bool AllocateTensors();