From 655b2663e7d2609b0f578b9ef3c1401de22dc5c2 Mon Sep 17 00:00:00 2001 From: Akshay Modi Date: Tue, 3 Apr 2018 10:04:47 -0700 Subject: Apply "Raise exception in SWIG on bad TF_Status" to base.i Minor fixes to make this work. PiperOrigin-RevId: 191457070 --- tensorflow/python/platform/base.i | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'tensorflow/python/platform') 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; + } +} -- cgit v1.2.3