π Powered by Detailer - AI-first repository insights
This project is pybind11, a lightweight, header-only C++ library that exposes C++ types and functions to Python, enabling seamless interoperability between the two languages. It solves the problem of writing Python bindings for C++ code with minimal boilerplate, high performance, and modern C++ support.
- C++ developers who want to expose their libraries or applications to Python.
- Python developers requiring high-performance extensions implemented in C++.
- Use cases include scientific computing, machine learning, system programming, and embedding Python in C++ applications.
- The core domain is language interoperability, focusing on:
- Binding C++ classes, functions, enums, and STL containers to Python.
- Managing object lifetimes and ownership semantics across language boundaries.
- Supporting advanced C++ features like smart pointers, templates, and polymorphism.
- Embedding Python interpreters within C++ applications.
- Providing utilities for type casting, buffer protocols, and NumPy integration.
- Binding Layer: Header-only C++ library (
include/pybind11/) providing templates and utilities to expose C++ code to Python. - Embedding Layer: Facilities to embed Python interpreters in C++ (
include/pybind11/embed.h). - Conversion Layer: Type casters and converters for STL, Eigen, complex numbers, chrono, filesystem, and custom types.
- Testing Layer: Extensive C++ and Python test suites validating bindings, lifetime management, threading, and interpreter behavior (
tests/). - Build & CI Layer: CMake build scripts, GitHub workflows, and automation scripts for building, testing, and releasing (
CMakeLists.txt,.github/workflows/,tools/). - Documentation Layer: Sphinx-based documentation with tutorials, API references, and advanced usage guides (
docs/).
.
βββ .github/ (22 items)
β βββ ISSUE_TEMPLATE/
β β βββ bug-report.yml
β β βββ config.yml
β βββ matchers/
β β βββ pylint.json
β βββ workflows/ (10 items)
β β βββ ci.yml
β β βββ configure.yml
β β βββ docs-link.yml
β β βββ format.yml
β β βββ labeler.yml
β β βββ nightlies.yml
β β βββ pip.yml
β β βββ reusable-standard.yml
β β βββ tests-cibw.yml
β β βββ upstream.yml
β βββ CODEOWNERS
β βββ CONTRIBUTING.md
β βββ dependabot.yml
β βββ labeler.yml
β βββ labeler_merged.yml
β βββ pull_request_template.md
βββ docs/ (50 items)
β βββ _static/
β β βββ css/
β β βββ custom.css
β βββ advanced/ (22 items)
β β βββ cast/ (8 items)
β β βββ pycpp/ (4 items)
β β βββ classes.rst
β β βββ deadlock.md
β β βββ deprecated.rst
β β βββ embedding.rst
β β βββ exceptions.rst
β β βββ functions.rst
β β βββ misc.rst
β β βββ smart_ptrs.rst
β βββ cmake/
β βββ Doxyfile
β βββ basics.rst
β βββ benchmark.py
β βββ benchmark.rst
β βββ changelog.md
β βββ classes.rst
β βββ compiling.rst
β βββ ... (15 more files)
βββ include/ (57 items)
β βββ pybind11/ (56 items)
β βββ conduit/
β βββ detail/ (16 items)
β βββ eigen/
β βββ stl/
β βββ attr.h
β βββ buffer_info.h
β βββ cast.h
β βββ chrono.h
β βββ complex.h
β βββ ... (22 more files)
βββ pybind11/ (6 items)
β βββ __init__.py
β βββ __main__.py
β βββ _version.py
β βββ commands.py
β βββ py.typed
β βββ setup_helpers.py
βββ tests/ (215 items)
β βββ extra_python_package/
β βββ extra_setuptools/
β βββ pure_cpp/
β βββ test_cmake_build/
β βββ test_cross_module_rtti/
β βββ conftest.py
β βββ constructor_stats.h
β βββ cross_module_gil_utils.cpp
β βββ cross_module_interleaved_error_already_set.cpp
β βββ ... (168 more files)
βββ tools/ (17 items)
β βββ FindCatch.cmake
β βββ FindEigen3.cmake
β βββ FindPythonLibsNew.cmake
β βββ JoinPaths.cmake
β βββ check-style.sh
β βββ cmake_uninstall.cmake.in
β βββ codespell_ignore_lines_from_errors.py
β βββ libsize.py
β βββ make_changelog.py
β βββ make_global.py
β βββ pybind11.pc.in
β βββ pybind11Common.cmake
β βββ pybind11Config.cmake.in
β βββ pybind11GuessPythonExtSuffix.cmake
β βββ pybind11NewTools.cmake
β βββ pybind11Tools.cmake
β βββ test-pybind11GuessPythonExtSuffix.cmake
βββ .appveyor.yml
βββ .clang-format
βββ .clang-tidy
βββ .cmake-format.yaml
βββ .codespell-ignore-lines
βββ .gitattributes
βββ .gitignore
βββ .pre-commit-config.yaml
βββ .readthedocs.yml
βββ CMakeLists.txt
βββ CMakePresets.json
βββ LICENSE
βββ README.rst
βββ SECURITY.md
βββ noxfile.py
βββ pyproject.toml
- Core Headers (
include/pybind11/):- Provide template classes (
py::class_,py::module_,py::enum_) for binding C++ types to Python. - Type casters handle conversion between C++ and Python types, including STL containers, Eigen matrices, complex numbers, chrono types, and filesystem paths.
- Trampoline classes enable Python subclasses to override C++ virtual functions safely.
- Smart pointer support (
shared_ptr,unique_ptr, custom holders) with ownership and lifetime management. - Buffer protocol support for zero-copy data sharing with Python buffers and NumPy arrays.
- Embedding support for running Python interpreters inside C++ applications.
- Provide template classes (
- CMake Build Scripts:
- Modular CMakeLists for building pybind11 headers, tests, and example modules.
- Support for header-only library pattern with INTERFACE targets.
- Integration with Python interpreter detection and virtual environments.
- Support for multiple build modes: embedded modules, function modules, target modules.
- GitHub Actions Workflows:
- CI pipelines for building, testing, linting, packaging, and releasing.
- Cross-platform testing on Linux, Windows, macOS.
- Automated dependency updates and labeling.
- Python Build Helpers (
pybind11/setup_helpers.py):- Custom setuptools extension for building C++ extensions with pybind11.
- Automatic detection of compiler flags and parallel compilation support.
- Extensive C++ and Python Tests:
- Cover binding correctness, lifetime management, threading, GIL, exception translation, virtual function overrides, and interpreter embedding.
- Tests for STL container bindings, Eigen integration, NumPy interoperability.
- Cross-module RTTI and aliasing tests.
- Tests for custom type casters, opaque types, and vectorization.
- Testing Frameworks:
- Catch2 for C++ unit tests.
- pytest for Python tests with fixtures, parameterization, and environment-aware skipping.
- Test Utilities:
- ConstructorStats for tracking object lifecycle.
- Custom fixtures for output sanitization and environment setup.
- Sphinx-based Documentation:
- Tutorials, API references, advanced usage guides.
- Includes upgrade guides, limitations, and release procedures.
- Code Style & Quality:
- clang-format and clang-tidy configurations.
- Pre-commit hooks for automated formatting and linting.
- Issue Templates & GitHub Automation:
- Standardized bug report templates.
- Automated labeling and dependency management.
- Header-only Library:
- Core pybind11 code is header-only, facilitating easy inclusion and template-based binding.
- Modular Testing:
- Tests organized by feature (e.g., embedding, STL, Eigen, exceptions).
- Separate C++ and Python test files for layered validation.
- Template Metaprogramming:
- Extensive use of templates for type safety, type traits, and generic programming.
- RAII & Resource Management:
- Use of RAII for GIL management, object lifetime, and resource cleanup.
- Trampoline Classes:
- Enable Python overrides of C++ virtual functions with proper lifetime management.
- Unit and Integration Tests:
- Cover core binding functionality, edge cases, and interoperability.
- Cross-Platform & Multi-Interpreter Testing:
- Tests for subinterpreters, GIL behavior, and concurrency.
- Error Handling & Exception Translation:
- Tests verify correct propagation of exceptions across language boundaries.
- Performance & Scalability:
- Benchmarking scripts for compilation time and binary size comparisons.
- Exception Translation:
- Custom translators map C++ exceptions to Python exceptions.
- Error Scopes:
- RAII wrappers manage Python error states safely.
- Logging:
- Test utilities log constructor/destructor calls for debugging.
- CMake Options:
- Configurable build options for testing, installation, and feature toggling.
- Python Environment Detection:
- Support for virtual environments, conda, and system Python.
- Pre-commit & CI Integration:
- Automated code quality enforcement.
- Python C API:
- Core dependency for all Python interaction.
- Eigen:
- For linear algebra bindings.
- Catch2:
- C++ testing framework.
- pytest:
- Python testing framework.
- clang-format, clang-tidy:
- Code formatting and static analysis.
- GitHub Actions:
- CI/CD automation.
- NumPy & SciPy:
- For buffer and array interoperability tests.
pybind11Headers:- Core binding infrastructure.
- Test Utilities:
- ConstructorStats, environment helpers, test fixtures.
- Build Helpers:
- CMake modules, Python setuptools extensions.
- CMake:
- Build system.
- setuptools, distutils:
- Python packaging and build.
- nox:
- Automation for linting, testing, and building.
- Pre-commit:
- Git hooks for code quality.
- Installation:
- Use CMake, Meson, or setuptools to build and install pybind11.
- Multiple installation methods documented (
docs/installing.rst).
- Building Extensions:
- Use
pybind11_add_module()in CMake orPybind11Extensionin setuptools. - Configure compiler flags automatically via provided helpers.
- Use
- Embedding Python:
- Use
py::scoped_interpreterand related APIs to embed Python in C++.
- Use
- Define Modules:
- Use
PYBIND11_MODULEmacro to define Python modules.
- Use
- Bind Classes and Functions:
- Use
py::class_,m.def(),py::enum_for exposing C++ types.
- Use
- Manage Lifetimes:
- Use smart pointers and
keep_alivepolicies.
- Use smart pointers and
- Override Virtuals:
- Use trampoline classes and
PYBIND11_OVERRIDEmacros.
- Use trampoline classes and
- Custom Type Casters:
- Specialize
type_caster<T>for custom conversions.
- Specialize
- Run C++ Tests:
- Use CMake targets or
ctestto run Catch2 tests.
- Use CMake targets or
- Run Python Tests:
- Use
pytestwith provided fixtures.
- Use
- Continuous Integration:
- GitHub Actions automate testing on multiple platforms.
- Enable Verbose Logging:
- Use test utilities like
ConstructorStats.
- Use test utilities like
- Check GIL and Threading Issues:
- Use provided GIL management classes and tests.
- Use Benchmark Scripts:
docs/benchmark.pyfor compile-time and binary size analysis.
- Follow Contribution Guidelines:
- See
.github/CONTRIBUTING.md.
- See
- Use Pre-commit Hooks:
- Automated formatting and linting.
- Write Tests:
- Add tests for new features or bug fixes.
- Update Documentation:
- Maintain
docs/with new APIs or changes.
- Maintain
This repository implements pybind11, a modern C++ binding library for Python, with a comprehensive architecture spanning header-only binding code, embedding support, extensive testing, and robust build automation. The project emphasizes modularity, type safety, and performance, supporting advanced C++ features and Python interoperability. The repository includes rich documentation, CI/CD pipelines, and developer tooling to facilitate contribution and maintenance.
The complete repository structure is provided above to enable AI agents and developers to navigate the codebase efficiently. The detailed analysis of components, dependencies, and patterns supports rapid comprehension and effective development workflows.