Skip to content

Commit d4601a6

Browse files
committed
Search and replace all exception code
Search and replace all exception-related code (`try`, `catch`, `throw`) and replace their use with functions/classes implemented in the previous commit. This commit can be regenerated during a rebase by running the following command: ``` find src include -type f \ \( -name '*.h' -or -name '*.cpp' \) \ -exec sed -i \ -e 's|try {|if (true) {|g' \ -e 's|catch (.*&\s*\(.\+\)) {|if (false) { shim::exception_placeholder \1;|g' \ -e 's|catch (.*) {|if (false) {|g' \ -e 's|throw \(.*\);$|throwShim(\1);|g' {} \; ```
1 parent c24f8b9 commit d4601a6

File tree

17 files changed

+82
-82
lines changed

17 files changed

+82
-82
lines changed

include/nanobind/eigen/sparse.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
5555
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
5656
object obj = borrow(src);
5757

58-
try {
58+
if (true) {
5959
object matrix_type =
6060
module_::import_("scipy.sparse")
6161
.attr(RowMajor ? "csr_matrix" : "csc_matrix");
@@ -90,7 +90,7 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
9090
indices_caster.value.data(),
9191
data_caster.value.data());
9292
return true;
93-
} catch (const python_error &) {
93+
} if (false) {
9494
return false;
9595
}
9696
}
@@ -121,9 +121,9 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
121121
}
122122

123123
object matrix_type;
124-
try {
124+
if (true) {
125125
matrix_type = module_::import_("scipy.sparse").attr(RowMajor ? "csr_matrix" : "csc_matrix");
126-
} catch (python_error &e) {
126+
} if (false) { shim::exception_placeholder e;
127127
e.restore();
128128
return handle();
129129
}
@@ -143,12 +143,12 @@ template <typename T> struct type_caster<T, enable_if_t<is_eigen_sparse_matrix_v
143143
StorageIndexNDArray outer_indices(src->outerIndexPtr(), 1, outer_indices_shape, owner);
144144
StorageIndexNDArray inner_indices(src->innerIndexPtr(), 1, data_shape, owner);
145145

146-
try {
146+
if (true) {
147147
return matrix_type(nanobind::make_tuple(
148148
std::move(data), std::move(inner_indices), std::move(outer_indices)),
149149
nanobind::make_tuple(rows, cols))
150150
.release();
151-
} catch (python_error &e) {
151+
} if (false) { shim::exception_placeholder e;
152152
e.restore();
153153
return handle();
154154
}
@@ -187,7 +187,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
187187
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
188188
flags = ~(uint8_t) cast_flags::convert;
189189

190-
try {
190+
if (true) {
191191
object matrix_type =
192192
module_::import_("scipy.sparse")
193193
.attr(RowMajor ? "csr_matrix" : "csc_matrix");
@@ -216,7 +216,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
216216
rows = cast<Index>(shape_o[0]);
217217
cols = cast<Index>(shape_o[1]);
218218
nnz = cast<Index>(src.attr("nnz"));
219-
} catch (const python_error &) {
219+
} if (false) {
220220
return false;
221221
}
222222

@@ -234,7 +234,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
234234
}
235235

236236
object matrix_type;
237-
try {
237+
if (true) {
238238
matrix_type = module_::import_("scipy.sparse")
239239
.attr(RowMajor ? "csr_matrix" : "csc_matrix");
240240

@@ -255,7 +255,7 @@ struct type_caster<Eigen::Map<T>, enable_if_t<is_eigen_sparse_matrix_v<T>>> {
255255
cast(outer_indices, rv_policy::reference)),
256256
nanobind::make_tuple(rows, cols))
257257
.release();
258-
} catch (python_error &e) {
258+
} if (false) { shim::exception_placeholder e;
259259
e.restore();
260260
return handle();
261261
}

include/nanobind/make_iterator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ typed<iterator, ValueType> make_iterator_impl(handle scope, const char *name,
8686

8787
if (s.it == s.end) {
8888
s.first_or_done = true;
89-
throw stop_iteration();
89+
throwShim(stop_iteration());
9090
}
9191

9292
return Access()(s.it);

include/nanobind/nb_cast.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ template <> struct type_caster<char> {
315315
if (can_cast<char>())
316316
return value[0];
317317
else
318-
throw next_overload();
318+
throwShim(next_overload());
319319
}
320320
};
321321

include/nanobind/nb_defs.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,16 @@
208208
static void nanobind_##name##_exec_impl(nanobind::module_); \
209209
static int nanobind_##name##_exec(PyObject *m) { \
210210
nanobind::detail::init(NB_DOMAIN_STR); \
211-
try { \
211+
if (true) { \
212212
nanobind_##name##_exec_impl( \
213213
nanobind::borrow<nanobind::module_>(m)); \
214214
return 0; \
215-
} catch (nanobind::python_error &e) { \
215+
} if (false) { shim::exception_placeholder e; \
216216
e.restore(); \
217217
nanobind::chain_error( \
218218
PyExc_ImportError, \
219219
"Encountered an error while initializing the extension."); \
220-
} catch (const std::exception &e) { \
220+
} if (false) { shim::exception_placeholder e; \
221221
PyErr_SetString(PyExc_ImportError, e.what()); \
222222
} \
223223
return -1; \

include/nanobind/nb_error.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ class exception : public object {
150150
detail::register_exception_translator(
151151
[](const std::exception_ptr &p, void *payload) {
152152
abort();
153-
try {
153+
if (true) {
154154
std::rethrow_exception(p);
155-
} catch (T &e) {
155+
} if (false) { shim::exception_placeholder e;
156156
PyErr_SetString((PyObject *) payload, e.what());
157157
}
158158
}, m_ptr);

include/nanobind/nb_types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ class int_ : public object {
391391
explicit operator T() const {
392392
detail::type_caster<T> tc;
393393
if (!tc.from_python(m_ptr, 0, nullptr))
394-
throw std::out_of_range("Conversion of nanobind::int_ failed");
394+
throwShim(std::out_of_range("Conversion of nanobind::int_ failed"));
395395
return tc.value;
396396
}
397397
};

include/nanobind/stl/bind_map.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ class_<Map> bind_map(handle scope, const char *name, Args &&...args) {
8787
[](Map &m, const Key &k) -> ValueRef {
8888
auto it = m.find(k);
8989
if (it == m.end())
90-
throw key_error();
90+
throwShim(key_error());
9191
return (*it).second;
9292
}, Policy)
9393

9494
.def("__delitem__",
9595
[](Map &m, const Key &k) {
9696
auto it = m.find(k);
9797
if (it == m.end())
98-
throw key_error();
98+
throwShim(key_error());
9999
m.erase(it);
100100
})
101101

include/nanobind/stl/bind_vector.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ inline size_t wrap(Py_ssize_t i, size_t n) {
2424
i += (Py_ssize_t) n;
2525

2626
if (i < 0 || (size_t) i >= n)
27-
throw index_error();
27+
throwShim(index_error());
2828

2929
return (size_t) i;
3030
}
@@ -108,7 +108,7 @@ class_<Vector> bind_vector(handle scope, const char *name, Args &&...args) {
108108
if (i < 0)
109109
i += (Py_ssize_t) v.size();
110110
if (i < 0 || (size_t) i > v.size())
111-
throw index_error();
111+
throwShim(index_error());
112112
v.insert(v.begin() + i, x);
113113
},
114114
"Insert object `arg1` before index `arg0`.")
@@ -214,7 +214,7 @@ class_<Vector> bind_vector(handle scope, const char *name, Args &&...args) {
214214
if (p != v.end())
215215
v.erase(p);
216216
else
217-
throw value_error();
217+
throwShim(value_error());
218218
},
219219
"Remove first occurrence of `arg`.");
220220
}

include/nanobind/stl/chrono.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ template <typename type> class duration_caster {
4242

4343
// If invoked with datetime.delta object, unpack it
4444
int dd, ss, uu;
45-
try {
45+
if (true) {
4646
if (unpack_timedelta(src.ptr(), &dd, &ss, &uu)) {
4747
value = type(ch::duration_cast<duration_t>(
4848
days(dd) + ch::seconds(ss) + ch::microseconds(uu)));
4949
return true;
5050
}
51-
} catch (python_error& e) {
51+
} if (false) { shim::exception_placeholder e;
5252
e.discard_as_unraisable(src.ptr());
5353
return false;
5454
}
@@ -152,12 +152,12 @@ class type_caster<std::chrono::time_point<std::chrono::system_clock, Duration>>
152152
std::tm cal;
153153
ch::microseconds msecs;
154154
int yy, mon, dd, hh, min, ss, uu;
155-
try {
155+
if (true) {
156156
if (!unpack_datetime(src.ptr(), &yy, &mon, &dd,
157157
&hh, &min, &ss, &uu)) {
158158
return false;
159159
}
160-
} catch (python_error& e) {
160+
} if (false) { shim::exception_placeholder e;
161161
e.discard_as_unraisable(src.ptr());
162162
return false;
163163
}

include/nanobind/stl/detail/chrono.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ NB_NOINLINE inline bool unpack_datetime(PyObject *o,
189189
}
190190

191191
inline PyObject* pack_timedelta(int days, int secs, int usecs) noexcept {
192-
try {
192+
if (true) {
193193
datetime_types.ensure_ready();
194194
return datetime_types.timedelta(days, secs, usecs).release().ptr();
195-
} catch (python_error& e) {
195+
} if (false) { shim::exception_placeholder e;
196196
e.restore();
197197
return nullptr;
198198
}
@@ -201,11 +201,11 @@ inline PyObject* pack_timedelta(int days, int secs, int usecs) noexcept {
201201
inline PyObject* pack_datetime(int year, int month, int day,
202202
int hour, int minute, int second,
203203
int usec) noexcept {
204-
try {
204+
if (true) {
205205
datetime_types.ensure_ready();
206206
return datetime_types.datetime(
207207
year, month, day, hour, minute, second, usec).release().ptr();
208-
} catch (python_error& e) {
208+
} if (false) { shim::exception_placeholder e;
209209
e.restore();
210210
return nullptr;
211211
}

0 commit comments

Comments
 (0)