How pybind/pybind11 works
Purpose and Consumer Entry Points
pybind11 is a header-only C++11+ library for building Python extension modules and embedding/interacting with Python. Consumers include its headers in C++ binding code; no pybind11 runtime library is linked. Primary public APIs are include/pybind11/pybind11.h, include/pybind11/embed.h, and conversion/feature headers such as include/pybind11/stl.h and include/pybind11/numpy.h.
The installed Python package supports build-system discovery and integration: pybind11/__init__.py, pybind11/__main__.py, and pybind11/setup_helpers.py.
Binding Definition and Python Object Layer
include/pybind11/pybind11.h provides module, function, class, enum, and attribute binding declarations. It builds on Python C-API-oriented object wrappers in include/pybind11/pytypes.h and shared API/ABI definitions in include/pybind11/common.h.
At extension initialization, user binding declarations register metadata and callable wrappers with CPython. Python calls enter generated wrapper functions, which marshal Python arguments, invoke native C++, and convert return values back to Python objects.
Type Conversion and Call Dispatch
The central boundary is Python objects ↔ native C++ types. include/pybind11/cast.h supplies generic type-caster dispatch; include/pybind11/detail/type_caster_base.h handles registered/custom class types; include/pybind11/detail/argument_vector.h supports argument collection and overload resolution.
Feature-specific casters extend this mechanism without requiring a separate runtime: include/pybind11/stl.h, include/pybind11/chrono.h, and include/pybind11/eigen.h. NumPy arrays and buffer protocol exchange are handled through include/pybind11/numpy.h and include/pybind11/buffer_info.h.
Class Lifetime, Inheritance, and Virtual Dispatch
C++ class bindings use instance registration, holder ownership, and Python/C++ inheritance machinery concentrated in include/pybind11/detail/class.h, include/pybind11/detail/value_and_holder.h, and include/pybind11/trampoline_self_life_support.h.
This layer governs reference ownership, smart pointers, Python subclass trampolines, and cross-language virtual calls. Smart-holder support is a distinct ownership model in include/pybind11/detail/struct_smart_holder.h and include/pybind11/detail/using_smart_holder.h.
Interpreter State, Errors, and Concurrency
Per-interpreter/shared internal registries and Python type metadata reside in include/pybind11/detail/internals.h. C++ exceptions are translated at the language boundary through include/pybind11/detail/exception_translation.h; public exception-related APIs are exposed by include/pybind11/pybind11.h.
GIL management and newer synchronization facilities are explicit APIs: include/pybind11/gil.h, include/pybind11/critical_section.h, and include/pybind11/subinterpreter.h. These form the principal correctness boundary for threaded code and multiple Python interpreters.
Build, Packaging, and Distribution Integration
CMake is the main native integration route: CMakeLists.txt defines project/install behavior, while tools/pybind11Config.cmake.in and tools/pybind11Tools.cmake expose imported targets and helper commands to downstream projects. CMakePresets.json provides repository build presets.
Python packaging is defined in pyproject.toml; the distributable package primarily ships headers plus CMake integration. Supported external runtimes are CPython 3.9+, PyPy, and GraalPy; optional interoperability targets include NumPy and Eigen.
Verification and Delivery Boundaries
The test architecture compiles C++ binding modules and drives them from Python/pytest, rooted at tests/CMakeLists.txt, tests/pybind11_tests.cpp, and tests/conftest.py. This validates the compiled-extension boundary rather than a standalone service runtime.
CI and release delivery use GitHub Actions (.github/workflows/ci.yml, .github/workflows/tests-cibw.yml) and AppVeyor (.appveyor.yml) across Python implementations, OSes, compilers, and wheel builds. Documentation is built from docs/index.rst and the Sphinx configuration docs/conf.py.
Main entry points: pybind11.h Binding declarations · cast.h Caster and overload dispatch · class.h Class lifetime and inheritance · pybind11Config.cmake.in CMake integration
Components 18
Consumer Interfaces
- C++ binding code · Downstream code defines bindings and native implementations.
- Python runtimes · CPython, PyPy, or GraalPy loads generated extensions.
- Binding declarations · Modules, functions, classes, enums, and attributes.
include/pybind11/pybind11.h - Embedding API
include/pybind11/embed.h
Header-Only Binding Core
- Python object wrappers
include/pybind11/pytypes.h - Shared API and ABI
include/pybind11/common.h - Caster and overload dispatch · Marshals Python arguments and native return values.
include/pybind11/cast.h - Custom class casters
include/pybind11/detail/type_caster_base.h - Argument resolution
include/pybind11/detail/argument_vector.h - STL, chrono, and Eigen · Optional native-type conversion extensions.
include/pybind11/stl.h - NumPy and buffers · NumPy arrays and buffer protocol exchange.
include/pybind11/numpy.h - Class lifetime and inheritance · Instance registration, holders, trampolines, and virtual dispatch.
include/pybind11/detail/class.h - Smart-holder ownership
include/pybind11/detail/struct_smart_holder.h - Interpreter registries · Per-interpreter state and Python type metadata.
include/pybind11/detail/internals.h - Errors and concurrency · Exception translation, GIL, critical sections, and subinterpreters.
include/pybind11/gil.h
Distribution Integration
- Python integration package · Exposes discovery, CLI, and setup helpers.
pybind11/__init__.py - CMake integration · Exports targets and helper commands for consumers.
tools/pybind11Config.cmake.in - Package and install metadata · Ships headers and CMake integration.
pyproject.toml
Connections 19
- C++ binding code → Binding declarations: includes
- C++ binding code → Embedding API: uses when embedding
- Binding declarations → Python object wrappers: builds on
- Binding declarations → Shared API and ABI: uses
- Binding declarations → Caster and overload dispatch: generates wrappers through
- Python runtimes → Binding declarations: initializes and invokes
- Caster and overload dispatch → Custom class casters: dispatches registered classes
- Caster and overload dispatch → Argument resolution: collects overload arguments
- STL, chrono, and Eigen → Caster and overload dispatch: extends
- NumPy and buffers → Caster and overload dispatch: extends
- Binding declarations → Class lifetime and inheritance: registers class bindings
- Class lifetime and inheritance → Smart-holder ownership: supports
- Class lifetime and inheritance → Interpreter registries: registers instances in
- Errors and concurrency → Interpreter registries: coordinates interpreter access
- Binding declarations → Errors and concurrency: exposes boundary APIs
- CMake integration → C++ binding code: configures downstream builds
- Python integration package → CMake integration: supports build discovery
- Package and install metadata → Python integration package: packages
- Package and install metadata → CMake integration: ships