Skip to content

Commit d80e994

Browse files
committed
added an API to enable/disable implicit cast failure warnings
1 parent e525898 commit d80e994

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

Diff for: include/nanobind/nb_lib.h

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ NB_CORE void decref_checked(PyObject *o) noexcept;
413413
// ========================================================================
414414

415415
NB_CORE void set_leak_warnings(bool value) noexcept;
416+
NB_CORE void set_implicit_cast_warnings(bool value) noexcept;
416417

417418
// ========================================================================
418419

Diff for: include/nanobind/nb_misc.h

+4
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ inline void set_leak_warnings(bool value) noexcept {
5858
detail::set_leak_warnings(value);
5959
}
6060

61+
inline void set_implicit_cast_warnings(bool value) noexcept {
62+
detail::set_implicit_cast_warnings(value);
63+
}
64+
6165
NAMESPACE_END(NB_NAMESPACE)

Diff for: src/common.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,10 @@ void set_leak_warnings(bool value) noexcept {
931931
internals_get().print_leak_warnings = value;
932932
}
933933

934+
void set_implicit_cast_warnings(bool value) noexcept {
935+
internals_get().print_implicit_cast_warnings = value;
936+
}
937+
934938
// ========================================================================
935939

936940
void slice_compute(PyObject *slice, Py_ssize_t size, Py_ssize_t &start,

Diff for: src/nb_internals.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ struct nb_internals {
203203
/// Registered C++ -> Python exception translators
204204
std::vector<std::pair<exception_translator, void *>> exception_translators;
205205

206-
/// Boolean specifying whether to print leak warnings on exit
207-
bool print_leak_warnings{true};
206+
/// Should nanobind print leak warnings on exit?
207+
bool print_leak_warnings = true;
208+
209+
/// Should nanobind print warnings after implicit cast failures?
210+
bool print_implicit_cast_warnings = true;
208211
};
209212

210213
struct current_method {

Diff for: src/nb_type.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -690,11 +690,13 @@ static NB_NOINLINE bool nb_type_get_implicit(PyObject *src,
690690
} else {
691691
PyErr_Clear();
692692

693-
PyObject *name = nb_inst_name(src);
694-
PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
695-
"nanobind: implicit conversion from type '%U' "
696-
"to type '%s' failed!", name, dst_type->name);
697-
Py_DECREF(name);
693+
if (internals.print_implicit_cast_warnings) {
694+
PyObject *name = nb_inst_name(src);
695+
PyErr_WarnFormat(PyExc_RuntimeWarning, 1,
696+
"nanobind: implicit conversion from type '%U' "
697+
"to type '%s' failed!", name, dst_type->name);
698+
Py_DECREF(name);
699+
}
698700

699701
return false;
700702
}

0 commit comments

Comments
 (0)