Skip to content

Commit 9aabe52

Browse files
committed
Add long comment to pybind11/compat/pybind11_conduit_v1.h
1 parent c169057 commit 9aabe52

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

include/pybind11/compat/README.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ The C++ code here
88
and nothing else.
99

1010
DO NOT ADD CODE WITH OTHER EXTERNAL DEPENDENCIES TO THIS DIRECTORY.
11+
12+
Read on:
13+
14+
pybind11_conduit_v1.h — Type-safe interoperability between different
15+
independent Python/C++ bindings systems.

include/pybind11/compat/pybind11_conduit_v1.h

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
11
// Copyright (c) 2024 The pybind Community.
22

3+
/* The pybind11_conduit_v1 feature enables type-safe interoperability between
4+
5+
* different independent Python/C++ bindings systems,
6+
7+
* including pybind11 versions with different PYBIND11_INTERNALS_VERSION's.
8+
9+
The naming of the feature is a bit misleading:
10+
11+
* The feature is in no way tied to pybind11 internals.
12+
13+
* It just happens to originate from pybind11 and currently still lives there.
14+
15+
* The only external dependency is <Python.h>.
16+
17+
The implementation is a VERY light-weight dependency. It is designed to be
18+
compatible with any ISO C++11 (or higher) compiler, and does NOT require
19+
C++ Exception Handling to be enabled.
20+
21+
Please see https://github.com/pybind/pybind11/pull/5296 for more background.
22+
23+
The implementation involves a
24+
25+
def _pybind11_conduit_v1_(
26+
self,
27+
pybind11_platform_abi_id: bytes,
28+
cpp_type_info_capsule: capsule,
29+
pointer_kind: bytes) -> capsule
30+
31+
method that is meant to be added to Python objects wrapping C++ objects
32+
(e.g. pybind11::class_-wrapped types).
33+
34+
The design of the _pybind11_conduit_v1_ feature provides two layers of
35+
protection against C++ ABI mismatches:
36+
37+
* The first and most important layer is that the pybind11_platform_abi_id's
38+
must match between extensions. — This will never be perfect, but is the same
39+
pragmatic approach used in pybind11 since 2017
40+
(https://github.com/pybind/pybind11/commit/96997a4b9d4ec3d389a570604394af5d5eee2557,
41+
PYBIND11_INTERNALS_ID).
42+
43+
* The second layer is that the typeid(std::type_info).name()'s must match
44+
between extensions.
45+
46+
The implementation below (which is shorter than this comment!), serves as a
47+
battle-tested specification. The main API is this one function:
48+
49+
auto *cpp_pointer = pybind11_conduit_v1::get_type_pointer_ephemeral<YourType>(py_obj);
50+
51+
It is meant to be a minimalistic reference implementation, intentionally
52+
without comprehensive error reporting. It is expected that major bindings
53+
systems will roll their own, compatible implementations, potentially with
54+
system-specific error reporting. The essential specifications all bindings
55+
systems need to agree on are merely:
56+
57+
* PYBIND11_PLATFORM_ABI_ID (const char* literal).
58+
59+
* The cpp_type_info capsule (see below: a void *ptr and a const char *name).
60+
61+
* The cpp_conduit capsule (see below: a void *ptr and a const char *name).
62+
63+
* "raw_pointer_ephemeral" means: the lifetime of the pointer is the lifetime
64+
of the py_obj.
65+
66+
*/
67+
368
// THIS MUST STAY AT THE TOP!
469
#include "pybind11_platform_abi_id.h"
570

0 commit comments

Comments
 (0)