|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | #pragma once
|
12 |
| - |
13 | 12 | #include "detail/class.h"
|
| 13 | +#include "detail/exception_translation.h" |
14 | 14 | #include "detail/init.h"
|
15 | 15 | #include "attr.h"
|
16 | 16 | #include "gil.h"
|
@@ -95,24 +95,6 @@ inline std::string replace_newlines_and_squash(const char *text) {
|
95 | 95 | return result.substr(str_begin, str_range);
|
96 | 96 | }
|
97 | 97 |
|
98 |
| -// Apply all the extensions translators from a list |
99 |
| -// Return true if one of the translators completed without raising an exception |
100 |
| -// itself. Return of false indicates that if there are other translators |
101 |
| -// available, they should be tried. |
102 |
| -inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) { |
103 |
| - auto last_exception = std::current_exception(); |
104 |
| - |
105 |
| - for (auto &translator : translators) { |
106 |
| - try { |
107 |
| - translator(last_exception); |
108 |
| - return true; |
109 |
| - } catch (...) { |
110 |
| - last_exception = std::current_exception(); |
111 |
| - } |
112 |
| - } |
113 |
| - return false; |
114 |
| -} |
115 |
| - |
116 | 98 | #if defined(_MSC_VER)
|
117 | 99 | # define PYBIND11_COMPAT_STRDUP _strdup
|
118 | 100 | #else
|
@@ -1038,40 +1020,7 @@ class cpp_function : public function {
|
1038 | 1020 | throw;
|
1039 | 1021 | #endif
|
1040 | 1022 | } catch (...) {
|
1041 |
| - /* When an exception is caught, give each registered exception |
1042 |
| - translator a chance to translate it to a Python exception. First |
1043 |
| - all module-local translators will be tried in reverse order of |
1044 |
| - registration. If none of the module-locale translators handle |
1045 |
| - the exception (or there are no module-locale translators) then |
1046 |
| - the global translators will be tried, also in reverse order of |
1047 |
| - registration. |
1048 |
| -
|
1049 |
| - A translator may choose to do one of the following: |
1050 |
| -
|
1051 |
| - - catch the exception and call py::set_error() |
1052 |
| - to set a standard (or custom) Python exception, or |
1053 |
| - - do nothing and let the exception fall through to the next translator, or |
1054 |
| - - delegate translation to the next translator by throwing a new type of exception. |
1055 |
| - */ |
1056 |
| - |
1057 |
| - bool handled = with_internals([&](internals &internals) { |
1058 |
| - auto &local_exception_translators |
1059 |
| - = get_local_internals().registered_exception_translators; |
1060 |
| - if (detail::apply_exception_translators(local_exception_translators)) { |
1061 |
| - return true; |
1062 |
| - } |
1063 |
| - auto &exception_translators = internals.registered_exception_translators; |
1064 |
| - if (detail::apply_exception_translators(exception_translators)) { |
1065 |
| - return true; |
1066 |
| - } |
1067 |
| - return false; |
1068 |
| - }); |
1069 |
| - |
1070 |
| - if (handled) { |
1071 |
| - return nullptr; |
1072 |
| - } |
1073 |
| - |
1074 |
| - set_error(PyExc_SystemError, "Exception escaped from default exception translator!"); |
| 1023 | + try_translate_exceptions(); |
1075 | 1024 | return nullptr;
|
1076 | 1025 | }
|
1077 | 1026 |
|
|
0 commit comments