Skip to content

Commit

Permalink
free threading compatibility for list casters
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakob committed Aug 21, 2024
1 parent 360f34d commit 42543f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/nanobind/stl/detail/nb_dict.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ template <typename Dict, typename Key, typename Val> struct dict_caster {
return false;
}

// 'items' is safe to access without locking and reference counting, it
// is unique to this thread
Py_ssize_t size = NB_LIST_GET_SIZE(items);
bool success = size >= 0;

Expand Down
8 changes: 6 additions & 2 deletions src/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,13 +674,15 @@ PyObject **seq_get(PyObject *seq, size_t *size_out, PyObject **temp_out) noexcep
still trigger a segfault if dereferenced. */
if (size == 0)
result = (PyObject **) 1;
# if !defined(NB_FREE_THREADED) // Require immutable holder in free-threaded mode
} else if (PyList_CheckExact(seq)) {
size = (size_t) PyList_GET_SIZE(seq);
result = ((PyListObject *) seq)->ob_item;
if (size == 0) // ditto
result = (PyObject **) 1;
# endif
} else if (PySequence_Check(seq)) {
temp = PySequence_Fast(seq, "");
temp = PySequence_Tuple(seq);

if (temp)
result = seq_get(temp, &size, temp_out);
Expand Down Expand Up @@ -768,14 +770,16 @@ PyObject **seq_get_with_size(PyObject *seq, size_t size,
if (size == 0)
result = (PyObject **) 1;
}
# if !defined(NB_FREE_THREADED) // Require immutable holder in free-threaded mode
} else if (PyList_CheckExact(seq)) {
if (size == (size_t) PyList_GET_SIZE(seq)) {
result = ((PyListObject *) seq)->ob_item;
if (size == 0) // ditto
result = (PyObject **) 1;
}
# endif
} else if (PySequence_Check(seq)) {
temp = PySequence_Fast(seq, "");
temp = PySequence_Tuple(seq);

if (temp)
result = seq_get_with_size(temp, size, temp_out);
Expand Down

0 comments on commit 42543f3

Please sign in to comment.