Caffe2 - C++ API
A deep learning, cross platform ML framework
Exceptions.h
1 #pragma once
2 
3 #include <exception>
4 #include <stdexcept>
5 #include <string>
6 
7 #include <torch/csrc/THP_export.h>
8 #include <c10/util/Exception.h>
9 #include <torch/csrc/utils/auto_gil.h>
10 #include <torch/csrc/utils/object_ptr.h>
11 #include <torch/csrc/WindowsTorchApiMacro.h>
12 
13 #define HANDLE_TH_ERRORS \
14  try {
15 #define END_HANDLE_TH_ERRORS_RET(retval) \
16  } \
17  catch (python_error & e) { \
18  return retval; \
19  } \
20  catch (const c10::IndexError& e) { \
21  auto msg = torch::processErrorMsg(e.what_without_backtrace()); \
22  PyErr_SetString(PyExc_IndexError, msg.c_str()); \
23  return retval; \
24  } \
25  catch (const c10::Error& e) { \
26  auto msg = torch::processErrorMsg(e.what_without_backtrace()); \
27  PyErr_SetString(PyExc_RuntimeError, msg.c_str()); \
28  return retval; \
29  } \
30  catch (torch::PyTorchError & e) { \
31  auto msg = torch::processErrorMsg(e.what()); \
32  PyErr_SetString(e.python_type(), msg.c_str()); \
33  return retval; \
34  } \
35  catch (const std::exception& e) { \
36  auto msg = torch::processErrorMsg(e.what()); \
37  PyErr_SetString(PyExc_RuntimeError, msg.c_str()); \
38  return retval; \
39  }
40 
41 #define END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS_RET(nullptr)
42 
43 extern PyObject *THPException_FatalError;
44 
45 // Throwing this exception means that the python error flags have been already
46 // set and control should be immediately returned to the interpreter.
47 struct python_error : public std::exception {
48  python_error() : type(nullptr), value(nullptr), traceback(nullptr) {}
49 
50  python_error(const python_error &other) : type(other.type), value(other.value), traceback(other.traceback) {
51  AutoGIL gil;
52  Py_XINCREF(type);
53  Py_XINCREF(value);
54  Py_XINCREF(traceback);
55  }
56 
57  python_error(python_error&& other) {
58  type = other.type;
59  value = other.value;
60  traceback = other.traceback;
61  other.type = nullptr;
62  other.value = nullptr;
63  other.traceback = nullptr;
64  }
65 
66  ~python_error() override {
67  if (type || value || traceback) {
68  AutoGIL gil;
69  Py_XDECREF(type);
70  Py_XDECREF(value);
71  Py_XDECREF(traceback);
72  }
73  }
74 
76  inline void persist() {
77  if (type) return; // Don't overwrite exceptions
78  // PyErr_Fetch overwrites the pointers
79  AutoGIL gil;
80  Py_XDECREF(type);
81  Py_XDECREF(value);
82  Py_XDECREF(traceback);
83  PyErr_Fetch(&type, &value, &traceback);
84  }
85 
87  inline void restore() {
88  if (!type) return;
89  // PyErr_Restore steals references
90  AutoGIL gil;
91  Py_XINCREF(type);
92  Py_XINCREF(value);
93  Py_XINCREF(traceback);
94  PyErr_Restore(type, value, traceback);
95  }
96 
97  PyObject* type;
98  PyObject* value;
99  PyObject* traceback;
100 };
101 
102 #ifdef _THP_CORE
103 
104 bool THPException_init(PyObject *module);
105 #endif
106 
107 namespace torch {
108 
109 THP_CLASS std::string processErrorMsg(std::string str);
110 
111 // Abstract base class for exceptions which translate to specific Python types
112 struct PyTorchError : public std::exception {
113  virtual PyObject* python_type() = 0;
114  const char* what() const noexcept override {
115  return msg.c_str();
116  }
117  std::string msg;
118 };
119 
120 // Translates to Python IndexError
121 struct IndexError : public PyTorchError {
122  IndexError(const char *format, ...);
123  PyObject* python_type() override {
124  return PyExc_IndexError;
125  }
126 };
127 
128 // Translates to Python TypeError
129 struct TypeError : public PyTorchError {
130  TORCH_API TypeError(const char *format, ...);
131  PyObject* python_type() override {
132  return PyExc_TypeError;
133  }
134 };
135 
136 // Translates to Python ValueError
137 struct ValueError : public PyTorchError {
138  ValueError(const char *format, ...);
139  PyObject* python_type() override {
140  return PyExc_ValueError;
141  }
142 };
143 
144 } // namespace torch
void restore()
Sets the current Python error from this exception.
Definition: Exceptions.h:87
void persist()
Saves the exception so that it can be re-thrown on a different thread.
Definition: Exceptions.h:76
Definition: jit_type.h:17