Skip to content

Commit

Permalink
don't accept bytes/unicode objects in sequence casters
Browse files Browse the repository at this point in the history
wjakob committed Oct 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent b431d04 commit 7e4a88b
Showing 4 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/common.cpp
Original file line number Diff line number Diff line change
@@ -577,6 +577,12 @@ PyObject **seq_get(PyObject *seq, size_t *size_out, PyObject **temp_out) noexcep
goes wrong, it fails gracefully without reporting errors. Other
overloads will then be tried. */

if (PyUnicode_CheckExact(seq) || PyBytes_CheckExact(seq)) {
*size_out = 0;
*temp_out = nullptr;
return nullptr;
}

#if !defined(Py_LIMITED_API) && !defined(PYPY_VERSION)
if (PyTuple_CheckExact(seq)) {
size = (size_t) PyTuple_GET_SIZE(seq);
1 change: 1 addition & 0 deletions src/nb_type.cpp
Original file line number Diff line number Diff line change
@@ -1123,6 +1123,7 @@ void keep_alive(PyObject *nurse, PyObject *patient) {
} else {
PyObject *callback =
PyCFunction_New(&keep_alive_callback_def, patient);

PyObject *weakref = PyWeakref_NewRef(nurse, callback);
if (!weakref) {
Py_DECREF(callback);
6 changes: 6 additions & 0 deletions tests/test_stl.cpp
Original file line number Diff line number Diff line change
@@ -439,4 +439,10 @@ NB_MODULE(test_stl_ext, m) {
return x;
});

m.def("vector_str", [](const std::vector<std::string>& x){
return x;
});
m.def("vector_str", [](std::string& x){
return x;
});
}
4 changes: 4 additions & 0 deletions tests/test_stl.py
Original file line number Diff line number Diff line change
@@ -820,3 +820,7 @@ def test69_complex_array():
assert t.complex_array_float(np.array([val1_64, -1j, val2_64],dtype=np.complex64)) == [val1_32, (-0-1j), val2_32]
except ImportError:
pass

def test70_vec_char():
assert isinstance(t.vector_str("123"), str)
assert isinstance(t.vector_str(["123", "345"]), list)

0 comments on commit 7e4a88b

Please sign in to comment.