diff --git a/python/paddle/fluid/compat.py b/python/paddle/fluid/compat.py index fe23a5929ad42abb7606f3e953e1dd4c389c0485..6cb59d50a7bc536a49db596e2968201abe5a70ad 100644 --- a/python/paddle/fluid/compat.py +++ b/python/paddle/fluid/compat.py @@ -72,3 +72,11 @@ def round(x, d=0): def floor_division(x, y): return x // y + +# exception related functions +def get_exception_message(exc): + if six.PY2: + return exc.message + else: + return str(exc) + diff --git a/python/paddle/fluid/tests/unittests/test_exception.py b/python/paddle/fluid/tests/unittests/test_exception.py index bb7c0f88f6027807394e15aa6803da2ddc22f4e2..6e4ea273a9ac41e6060cb24de4aa4c0e8b1b269c 100644 --- a/python/paddle/fluid/tests/unittests/test_exception.py +++ b/python/paddle/fluid/tests/unittests/test_exception.py @@ -12,19 +12,21 @@ # See the License for the specific language governing permissions and # limitations under the License. +import paddle.fluid.compat as cpt import paddle.fluid.core as core import unittest class TestException(unittest.TestCase): def test_exception(self): - ex = None + exception = None try: core.__unittest_throw_exception__() except core.EnforceNotMet as ex: - self.assertIn("test exception", ex.message) + self.assertIn("test exception", cpt.get_exception_message(ex)) + exception = ex - self.assertIsNotNone(ex) + self.assertIsNotNone(exception) if __name__ == "__main__":