@@ -31,6 +31,8 @@ void tp_dealloc_impl(PyObject *self);
31
31
void tp_free_impl (void *self);
32
32
33
33
static PyObject *reduce_ex_impl (PyObject *self, PyObject *, PyObject *);
34
+ static PyObject *
35
+ get_capsule_for_scipy_LowLevelCallable_impl (PyObject *self, PyObject *, PyObject *);
34
36
35
37
PYBIND11_WARNING_PUSH
36
38
#if defined(__GNUC__) && __GNUC__ >= 8
@@ -41,6 +43,10 @@ PYBIND11_WARNING_DISABLE_CLANG("-Wcast-function-type-mismatch")
41
43
#endif
42
44
static PyMethodDef tp_methods_impl[]
43
45
= {{" __reduce_ex__" , (PyCFunction) reduce_ex_impl, METH_VARARGS | METH_KEYWORDS, nullptr },
46
+ {" get_capsule_for_scipy_LowLevelCallable" ,
47
+ (PyCFunction) get_capsule_for_scipy_LowLevelCallable_impl,
48
+ METH_VARARGS | METH_KEYWORDS,
49
+ " for use with scipy.LowLevelCallable()" },
44
50
{nullptr , nullptr , 0 , nullptr }};
45
51
PYBIND11_WARNING_POP
46
52
@@ -202,6 +208,29 @@ inline PyObject *reduce_ex_impl(PyObject *self, PyObject *, PyObject *) {
202
208
return nullptr ;
203
209
}
204
210
211
+ inline PyObject *
212
+ get_capsule_for_scipy_LowLevelCallable_impl (PyObject *self, PyObject *args, PyObject *kwargs) {
213
+ static const char *kwlist[] = {" signature" , nullptr };
214
+ const char *signature = nullptr ;
215
+ if (!PyArg_ParseTupleAndKeywords (args, kwargs, " s" , const_cast <char **>(kwlist), &signature)) {
216
+ return nullptr ;
217
+ }
218
+ function_record *rec = function_record_ptr_from_PyObject (self);
219
+ if (rec == nullptr ) {
220
+ pybind11_fail (" FATAL: get_capsule_for_scipy_LowLevelCallable_impl(): cannot obtain C++ "
221
+ " function_record." );
222
+ }
223
+ if (!rec->is_stateless ) {
224
+ set_error (PyExc_TypeError, repr (self) + str (" is not a stateless function." ));
225
+ return nullptr ;
226
+ }
227
+ struct capture {
228
+ void *f; // DANGER: TYPE SAFETY IS LOST COMPLETELY.
229
+ };
230
+ auto cap = reinterpret_cast <capture *>(&rec->data );
231
+ return capsule (cap->f , signature).release ().ptr ();
232
+ }
233
+
205
234
PYBIND11_NAMESPACE_END (function_record_PyTypeObject_methods)
206
235
207
236
PYBIND11_NAMESPACE_END(detail)
0 commit comments