1 /** 2 * Copyright 2020-2022 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // NOTICE: This header file should only be included once in the whole project. 18 // We change the cpp file to header file, to avoid MSVC compiler problem. 19 #ifndef MINDSPORE_CCSRC_PYBINDAPI_IR_LOG_ADAPTER_PY_H_ 20 #define MINDSPORE_CCSRC_PYBINDAPI_IR_LOG_ADAPTER_PY_H_ 21 22 #include "utils/log_adapter.h" 23 24 #include <string> 25 #include "pybind11/pybind11.h" 26 #include "pybind_api/pybind_patch.h" 27 28 namespace py = pybind11; 29 namespace mindspore { 30 class PyExceptionInitializer { 31 public: PyExceptionInitializer()32 PyExceptionInitializer() { 33 MS_LOG(INFO) << "Set exception handler"; 34 mindspore::LogWriter::SetExceptionHandler(HandleExceptionPy); 35 } 36 37 ~PyExceptionInitializer() = default; 38 39 private: HandleExceptionPy(ExceptionType exception_type,const std::string & str)40 static void HandleExceptionPy(ExceptionType exception_type, const std::string &str) { 41 if (exception_type == IndexError) { 42 throw py::index_error(str); 43 } 44 if (exception_type == ValueError) { 45 throw py::value_error(str); 46 } 47 if (exception_type == TypeError) { 48 throw py::type_error(str); 49 } 50 if (exception_type == KeyError) { 51 throw py::key_error(str); 52 } 53 if (exception_type == AttributeError) { 54 throw py::attribute_error(str); 55 } 56 if (exception_type == NameError) { 57 throw py::name_error(str); 58 } 59 if (exception_type == AssertionError) { 60 throw py::assertion_error(str); 61 } 62 if (exception_type == BaseException) { 63 throw py::base_exception(str); 64 } 65 if (exception_type == KeyboardInterrupt) { 66 throw py::keyboard_interrupt(str); 67 } 68 if (exception_type == StopIteration) { 69 throw py::stop_iteration(str); 70 } 71 if (exception_type == OverflowError) { 72 throw py::overflow_error(str); 73 } 74 if (exception_type == ZeroDivisionError) { 75 throw py::zero_division_error(str); 76 } 77 if (exception_type == EnvironmentError) { 78 throw py::environment_error(str); 79 } 80 if (exception_type == IOError) { 81 throw py::io_error(str); 82 } 83 if (exception_type == OSError) { 84 throw py::os_error(str); 85 } 86 if (exception_type == MemoryError) { 87 throw py::memory_error(str); 88 } 89 if (exception_type == UnboundLocalError) { 90 throw py::unbound_local_error(str); 91 } 92 if (exception_type == NotImplementedError) { 93 throw py::not_implemented_error(str); 94 } 95 if (exception_type == IndentationError) { 96 throw py::indentation_error(str); 97 } 98 if (exception_type == RuntimeWarning) { 99 throw py::runtime_warning(str); 100 } 101 py::pybind11_fail(str); 102 } 103 }; 104 105 static PyExceptionInitializer py_exception_initializer; 106 } // namespace mindspore 107 #endif // MINDSPORE_CCSRC_PYBINDAPI_IR_LOG_ADAPTER_PY_H_ 108