message: Create PyRaiseException() with common code to convert from a C string
  to Python exception.
diff: @@ -372,6 +372,16 @@ namespace {
  // Python subclass of Exception that is created on not ok Status.
  tensorflow::mutex exception_class_mutex(tensorflow::LINKER_INITIALIZED);
  PyObject* exception_class GUARDED_BY(exception_class_mutex) = nullptr;
  +
  +void PyRaiseException(TF_Code error_code, const char* msg) {
  + tensorflow::mutex_lock l(exception_class_mutex);
  + if (exception_class != nullptr) {
  + PyErr_SetObject(exception_class, Py_BuildValue("si", msg, error_code));
  + } else {
  + PyErr_SetString(PyExc_RuntimeError, msg);
  + }
  +}
  +
  } // namespace
  TFE_TensorHandle* TFE_Py_NumpyToTensorHandle(PyObject* obj) {
  @@ -380,16 +390,11 @@ TFE_TensorHandle* TFE_Py_NumpyToTensorHandle(PyObject* obj) {
  if (cppstatus.ok()) {
  return TFE_NewTensorHandle(t);
  } else {
  - tensorflow::mutex_lock l(exception_class_mutex);
  - auto msg = tensorflow::strings::StrCat(
  + PyRaiseException(TF_INVALID_ARGUMENT,
  + tensorflow::strings::StrCat(
  "failed to convert numpy ndarray to a Tensor (",
  - cppstatus.error_message(), ")");
  - if (exception_class != nullptr) {
  - PyErr_SetObject(exception_class,
  - Py_BuildValue("si", msg.c_str(), TF_INVALID_ARGUMENT));
  - } else {
  - PyErr_SetString(PyExc_RuntimeError, msg.c_str());
  - }
  + cppstatus.error_message(), ")")
  + .c_str());
  }
  return nullptr;
  }
  @@ -401,16 +406,7 @@ TFE_TensorHandle* TFE_Py_SequenceToTensorHandle(PyObject* obj,
  if (cppstatus.ok()) {
  return TFE_NewTensorHandle(t);
  } else {
  - tensorflow::mutex_lock l(exception_class_mutex);
  - auto msg =
  - tensorflow::strings::StrCat("failed to convert value to a Tensor (",
  - cppstatus.error_message(), ")");
  - if (exception_class != nullptr) {
  - PyErr_SetObject(exception_class,
  - Py_BuildValue("si", msg.c_str(), TF_INVALID_ARGUMENT));
  - } else {
  - PyErr_SetString(PyExc_RuntimeError, msg.c_str());
  - }
  + PyRaiseException(TF_INVALID_ARGUMENT, cppstatus.error_message().c_str());
  }
  return nullptr;
  }
  @@ -435,13 +431,7 @@ PyObject* TFE_Py_RegisterExceptionClass(PyObject* e) {
  int TFE_Py_MaybeRaiseException(TF_Status* status) {
  if (TF_GetCode(status) == TF_OK) return 0;
  - tensorflow::mutex_lock l(exception_class_mutex);
  - if (exception_class != nullptr) {
  - PyErr_SetObject(exception_class, Py_BuildValue("si", TF_Message(status),
  - TF_GetCode(status)));
  - } else {
  - PyErr_SetString(PyExc_RuntimeError, TF_Message(status));
  - }
  + PyRaiseException(TF_GetCode(status), TF_Message(status));
  return -1;
  }
  