| 166 | |
| 167 | template <typename Function> |
| 168 | auto SafeCallIntoPythonVoid(Function&& func) -> decltype(func()) { |
| 169 | PyAcquireGIL lock; |
| 170 | PyObject* exc_type; |
| 171 | PyObject* exc_value; |
| 172 | PyObject* exc_traceback; |
| 173 | PyErr_Fetch(&exc_type, &exc_value, &exc_traceback); |
| 174 | func(); |
| 175 | if (exc_type != NULLPTR) { |
| 176 | PyErr_Restore(exc_type, exc_value, exc_traceback); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | // A RAII primitive that DECREFs the underlying PyObject* when it |
| 181 | // goes out of scope. |