Turi Create  4.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cython_cpp_error_handler.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #define PY_SSIZE_T_CLEAN
7 #include <Python.h>
8 #include <string>
9 #include <exception>
10 #include <ios>
11 #include <new>
12 #include <typeinfo>
13 #include <stdexcept>
14 #define __Pyx_CppExn2PyErr __Pyx_CppExn2PyErr2
15 static void __Pyx_CppExn2PyErr() {
16  // Catch a handful of different errors here and turn them into the
17  // equivalent Python errors.
18  try {
19  if (PyErr_Occurred())
20  ; // let the latest Python exn pass through and ignore the current one
21  else
22  throw;
23  } catch (const std::bad_alloc& exn) {
24  PyErr_SetString(PyExc_MemoryError, exn.what());
25  } catch (const std::bad_cast& exn) {
26  PyErr_SetString(PyExc_TypeError, exn.what());
27  } catch (const std::bad_typeid& exn) {
28  PyErr_SetString(PyExc_TypeError, exn.what());
29  } catch (const std::domain_error& exn) {
30  PyErr_SetString(PyExc_ValueError, exn.what());
31  } catch (const std::invalid_argument& exn) {
32  PyErr_SetString(PyExc_ValueError, exn.what());
33  } catch (const std::ios_base::failure& exn) {
34  // Unfortunately, in standard C++ we have no way of distinguishing EOF
35  // from other errors here; be careful with the exception mask
36  PyErr_SetString(PyExc_IOError, exn.what());
37  } catch (const std::out_of_range& exn) {
38  // Change out_of_range to IndexError
39  PyErr_SetString(PyExc_IndexError, exn.what());
40  } catch (const std::overflow_error& exn) {
41  PyErr_SetString(PyExc_OverflowError, exn.what());
42  } catch (const std::range_error& exn) {
43  PyErr_SetString(PyExc_ArithmeticError, exn.what());
44  } catch (const std::underflow_error& exn) {
45  PyErr_SetString(PyExc_ArithmeticError, exn.what());
46  } catch (const std::exception& exn) {
47  PyErr_SetString(PyExc_RuntimeError, exn.what());
48  } catch (const std::string& exn) {
49  PyErr_SetString(PyExc_RuntimeError, exn.c_str());
50  } catch (const char* exn) {
51  PyErr_SetString(PyExc_RuntimeError, exn);
52  } catch (...)
53  {
54  PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
55  }
56 }