Skip to content

Conversation

@rwgk
Copy link
Collaborator

@rwgk rwgk commented Mar 27, 2025

Description

This was an experiment/demo:

import scipy.integrate

llc = scipy.LowLevelCallable(
    your_extension_module.foo.__self__.get_capsule_for_scipy_LowLevelCallable_NO_ABI_OR_TYPE_SAFETY(
        signature="double (double)"
    )
)
integrate.quad(llc, 0, 1)

This works for any pybind11-wrapped function that is "stateless", but ABI safety and type safety are missing completely.

Slightly safer alternatives are easy to implement. See e.g.

Suggested changelog entry:

rwgk added 7 commits March 24, 2025 20:40
…ch")`

macos-13 • brew install llvm

```
/Users/runner/work/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:40:26: error: cast from 'PyObject *(*)(PyObject *, PyObject *, PyObject *)' (aka '_object *(*)(_object *, _object *, _object *)') to 'PyCFunction' (aka '_object *(*)(_object *, _object *)') converts to incompatible function type [-Werror,-Wcast-function-type-mismatch]
   40 |     = {{"__reduce_ex__", (PyCFunction) reduce_ex_impl, METH_VARARGS | METH_KEYWORDS, nullptr},
      |                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
rwgk added 13 commits March 27, 2025 20:13
```
/__w/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:215:10: error: implicit conversion 'int' -> 'bool' [readability-implicit-bool-conversion,-warnings-as-errors]
  215 |     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", const_cast<char **>(kwlist), &signature)) {
      |         ~^
      |                                                                                                  == 0
/__w/pybind11/pybind11/include/pybind11/detail/function_record_pyobject.h:230:5: error: 'auto cap' can be declared as 'auto *cap' [readability-qualified-auto,-warnings-as-errors]
  230 |     auto cap = reinterpret_cast<capture *>(&rec->data);
      |     ^~~~
      |     auto *
```
…he function pointer. Also make the function a bit more unique (square → square21) for easier pin-pointing.

Related ChatGPT conversation regarding `extern "C"`: https://chatgpt.com/share/67e6481e-0994-8008-a64a-a07ce2b781ea
…_LowLevelCallable_NO_TYPE_SAFETY. Make the reinterpret_cast from function pointer to void pointer explicit for clarity.
…d11/functional.h, pybind11/detail/function_record_pyobject.h

Also `static_assert(std::is_standard_layout<capture>::value, "")` in all three places.

ChatGPT conversation guiding gh-rwgk through the subtle changes: https://chatgpt.com/share/67e66f74-cc80-8008-8b19-1d02e86acf71
…_capsule_for_scipy_LowLevelCallable_NO_ABI_OR_TYPE_SAFETY`)
…PTIMIZATIONS macro, for readability/maintainability.
…D11_DETAIL_FUNCTION_RECORD_ABI_ID, PYBIND11_INTERNALS_ID
@rwgk rwgk closed this Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant