exception.cc 4.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
2

L
Luo Tao 已提交
3 4 5
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
Y
Yu Yang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/pybind/exception.h"
16

17
#include "paddle/phi/api/ext/exception.h"
Y
Yu Yang 已提交
18 19 20
namespace paddle {
namespace pybind {

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/* Paddle Exception mapping rules:
 *   - InvalidArgumentError -> ValueError
 *   - NotFoundError -> RuntimeError
 *   - OutOfRangeError -> IndexError
 *   - AlreadyExistsError -> RuntimeError
 *   - ResourceExhaustedError -> MemoryError
 *   - PreconditionNotMetError -> RuntimeError
 *   - PermissionDeniedError -> RuntimeError
 *   - ExecutionTimeoutError -> RuntimeError
 *   - UnimplementedError -> NotImplementedError
 *   - UnavailableError -> RuntimeError
 *   - FatalError -> SystemError
 *   - ExternalError -> OSError
 */

36
void BindException(pybind11::module* m) {
37
  static pybind11::exception<platform::EOFException> eof(*m, "EOFException");
38
  static pybind11::exception<platform::EnforceNotMet> exc(*m, "EnforceNotMet");
Y
Yu Yang 已提交
39 40 41
  pybind11::register_exception_translator([](std::exception_ptr p) {
    try {
      if (p) std::rethrow_exception(p);
42 43
    } catch (const platform::EOFException& e) {
      eof(e.what());
Y
Yu Yang 已提交
44
    } catch (const platform::EnforceNotMet& e) {
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      switch (e.code()) {
        case paddle::platform::error::INVALID_ARGUMENT:
          PyErr_SetString(PyExc_ValueError, e.what());
          break;
        case paddle::platform::error::NOT_FOUND:
        case paddle::platform::error::ALREADY_EXISTS:
        case paddle::platform::error::PRECONDITION_NOT_MET:
        case paddle::platform::error::PERMISSION_DENIED:
        case paddle::platform::error::EXECUTION_TIMEOUT:
        case paddle::platform::error::UNAVAILABLE:
          PyErr_SetString(PyExc_RuntimeError, e.what());
          break;
        case paddle::platform::error::OUT_OF_RANGE:
          PyErr_SetString(PyExc_IndexError, e.what());
          break;
        case paddle::platform::error::RESOURCE_EXHAUSTED:
          PyErr_SetString(PyExc_MemoryError, e.what());
          break;
        case paddle::platform::error::UNIMPLEMENTED:
          PyErr_SetString(PyExc_NotImplementedError, e.what());
          break;
        case paddle::platform::error::FATAL:
          PyErr_SetString(PyExc_SystemError, e.what());
          break;
        case paddle::platform::error::EXTERNAL:
          PyErr_SetString(PyExc_OSError, e.what());
          break;
        default:
          exc(e.what());
          break;
      }
Y
Yu Yang 已提交
76 77 78
    }
  });

79 80 81 82
  m->def("__unittest_throw_exception__", [] {
    PADDLE_THROW(
        platform::errors::PermissionDenied("This is a test of exception"));
  });
Y
Yu Yang 已提交
83 84
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
void ThrowExceptionToPython(std::exception_ptr p) {
  static PyObject* EOFExceptionException =
      PyErr_NewException("paddle.EOFException", PyExc_Exception, NULL);
  static PyObject* EnforceNotMetException =
      PyErr_NewException("paddle.EnforceNotMet", PyExc_Exception, NULL);
  try {
    if (p) std::rethrow_exception(p);
  } catch (const platform::EOFException& e) {
    PyErr_SetString(EOFExceptionException, e.what());
  } catch (const platform::EnforceNotMet& e) {
    switch (e.code()) {
      case paddle::platform::error::INVALID_ARGUMENT:
        PyErr_SetString(PyExc_ValueError, e.what());
        break;
      case paddle::platform::error::NOT_FOUND:
      case paddle::platform::error::ALREADY_EXISTS:
      case paddle::platform::error::PRECONDITION_NOT_MET:
      case paddle::platform::error::PERMISSION_DENIED:
      case paddle::platform::error::EXECUTION_TIMEOUT:
      case paddle::platform::error::UNAVAILABLE:
        PyErr_SetString(PyExc_RuntimeError, e.what());
        break;
      case paddle::platform::error::OUT_OF_RANGE:
        PyErr_SetString(PyExc_IndexError, e.what());
        break;
      case paddle::platform::error::RESOURCE_EXHAUSTED:
        PyErr_SetString(PyExc_MemoryError, e.what());
        break;
      case paddle::platform::error::UNIMPLEMENTED:
        PyErr_SetString(PyExc_NotImplementedError, e.what());
        break;
      case paddle::platform::error::FATAL:
        PyErr_SetString(PyExc_SystemError, e.what());
        break;
      case paddle::platform::error::EXTERNAL:
        PyErr_SetString(PyExc_OSError, e.what());
        break;
      default:
        PyErr_SetString(EnforceNotMetException, e.what());
        break;
    }
126 127
  } catch (const paddle::PD_Exception& e) {
    PyErr_SetString(PyExc_OSError, e.what());
128 129
  }
}
Y
Yu Yang 已提交
130 131
}  // namespace pybind
}  // namespace paddle