CppSupport.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /////////////// CppExceptionConversion.proto ///////////////
  2. #ifndef __Pyx_CppExn2PyErr
  3. #include <new>
  4. #include <typeinfo>
  5. #include <stdexcept>
  6. #include <ios>
  7. static void __Pyx_CppExn2PyErr() {
  8. // Catch a handful of different errors here and turn them into the
  9. // equivalent Python errors.
  10. try {
  11. if (PyErr_Occurred())
  12. ; // let the latest Python exn pass through and ignore the current one
  13. else
  14. throw;
  15. } catch (const std::bad_alloc& exn) {
  16. PyErr_SetString(PyExc_MemoryError, exn.what());
  17. } catch (const std::bad_cast& exn) {
  18. PyErr_SetString(PyExc_TypeError, exn.what());
  19. } catch (const std::bad_typeid& exn) {
  20. PyErr_SetString(PyExc_TypeError, exn.what());
  21. } catch (const std::domain_error& exn) {
  22. PyErr_SetString(PyExc_ValueError, exn.what());
  23. } catch (const std::invalid_argument& exn) {
  24. PyErr_SetString(PyExc_ValueError, exn.what());
  25. } catch (const std::ios_base::failure& exn) {
  26. // Unfortunately, in standard C++ we have no way of distinguishing EOF
  27. // from other errors here; be careful with the exception mask
  28. PyErr_SetString(PyExc_IOError, exn.what());
  29. } catch (const std::out_of_range& exn) {
  30. // Change out_of_range to IndexError
  31. PyErr_SetString(PyExc_IndexError, exn.what());
  32. } catch (const std::overflow_error& exn) {
  33. PyErr_SetString(PyExc_OverflowError, exn.what());
  34. } catch (const std::range_error& exn) {
  35. PyErr_SetString(PyExc_ArithmeticError, exn.what());
  36. } catch (const std::underflow_error& exn) {
  37. PyErr_SetString(PyExc_ArithmeticError, exn.what());
  38. } catch (const std::exception& exn) {
  39. PyErr_SetString(PyExc_RuntimeError, exn.what());
  40. }
  41. catch (...)
  42. {
  43. PyErr_SetString(PyExc_RuntimeError, "Unknown exception");
  44. }
  45. }
  46. #endif
  47. /////////////// PythranConversion.proto ///////////////
  48. template <class T>
  49. auto __Pyx_pythran_to_python(T &&value) -> decltype(to_python(
  50. typename pythonic::returnable<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::type{std::forward<T>(value)}))
  51. {
  52. using returnable_type = typename pythonic::returnable<typename std::remove_cv<typename std::remove_reference<T>::type>::type>::type;
  53. return to_python(returnable_type{std::forward<T>(value)});
  54. }