From 42543f3a461105ccf591b2a941943ff1e5d54961 Mon Sep 17 00:00:00 2001 From: Wenzel Jakob Date: Wed, 21 Aug 2024 16:36:04 +0900 Subject: [PATCH] free threading compatibility for list casters --- include/nanobind/stl/detail/nb_dict.h | 2 ++ src/common.cpp | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/nanobind/stl/detail/nb_dict.h b/include/nanobind/stl/detail/nb_dict.h index eb0e2dea..24f77ea2 100644 --- a/include/nanobind/stl/detail/nb_dict.h +++ b/include/nanobind/stl/detail/nb_dict.h @@ -32,6 +32,8 @@ template 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; diff --git a/src/common.cpp b/src/common.cpp index e577bac6..54def643 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -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); @@ -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);