aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/platform
diff options
context:
space:
mode:
authorGravatar Akshay Modi <nareshmodi@google.com>2018-04-03 10:04:47 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-04-03 10:09:05 -0700
commit655b2663e7d2609b0f578b9ef3c1401de22dc5c2 (patch)
tree61ac9426556a65f99edf99a23fa491d329d70016 /tensorflow/python/platform
parentcfc886ac6064a04c71dd6c52e8c21ebec91eae50 (diff)
Apply "Raise exception in SWIG on bad TF_Status" to base.i
Minor fixes to make this work. PiperOrigin-RevId: 191457070
Diffstat (limited to 'tensorflow/python/platform')
-rw-r--r--tensorflow/python/platform/base.i22
1 files changed, 22 insertions, 0 deletions
diff --git a/tensorflow/python/platform/base.i b/tensorflow/python/platform/base.i
index dbefca2be9..478dd46f7e 100644
--- a/tensorflow/python/platform/base.i
+++ b/tensorflow/python/platform/base.i
@@ -229,3 +229,25 @@ _COPY_TYPEMAPS(unsigned int, mode_t);
%define final %enddef
%define override %enddef
#endif
+
+// Typemaps to automatically raise a Python exception from bad output TF_Status.
+// TODO(b/77295559): expand this to all TF_Status* output params and deprecate
+// raise_exception_on_not_ok_status (currently it only affects the C API).
+%typemap(in, numinputs=0) TF_Status* status (TF_Status* status) {
+ $1 = TF_NewStatus();
+}
+
+%typemap(freearg) (TF_Status* status) {
+ TF_DeleteStatus($1);
+}
+
+%typemap(argout) TF_Status* status {
+ TF_Code code = TF_GetCode($1);
+ if (code != TF_OK) {
+ PyObject* exc = tensorflow::PyExceptionRegistry::Lookup(code);
+ // Arguments to OpError.
+ PyObject* exc_args = Py_BuildValue("sss", nullptr, nullptr, TF_Message($1));
+ SWIG_SetErrorObj(exc, exc_args);
+ SWIG_fail;
+ }
+}