Skip to content

Commit

Permalink
Fix unhandled exception in set_caster (#772)
Browse files Browse the repository at this point in the history
`obj_iter` throws `python_error` on failure, but type casters aren't
allowed to throw! If you try to from-Python cast an object of a
non-iterable type, Python will abort from the unhandled exception
coming from a `noexcept` function

It looks like the intention here is to handle failure manually (a NULL
return from the CPython call), so I did just that
  • Loading branch information
tjstum authored Nov 3, 2024
1 parent fd22b8c commit 977cd37
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/nanobind/stl/detail/nb_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ template <typename Set, typename Key> struct set_caster {
bool from_python(handle src, uint8_t flags, cleanup_list *cleanup) noexcept {
value.clear();

PyObject* iter = obj_iter(src.ptr());
PyObject* iter = PyObject_GetIter(src.ptr());
if (!iter) {
PyErr_Clear();
return false;
Expand Down
4 changes: 4 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,3 +794,7 @@ def test71_null_input():
@skip_on_pypy # PyPy fails this test on Windows :-(
def test72_wstr():
assert t.pass_wstr('🎈') == '🎈'

def test73_bad_input_to_set():
with pytest.raises(TypeError):
t.set_in_value(None)

0 comments on commit 977cd37

Please sign in to comment.