Isurwars/Correlation / .agents/skills/pybind11
pybind11
VerifiedDirect high-performance Python bindings creation, zero-copy NumPy buffer transfers, GIL management, and C++/Python memory safety. Use when editing or reviewing Python bindings in src/bindings/.
Pybind11 Python Interoperability
This skill governs the implementation, optimization, and maintenance of Python C++ extension bindings located in src/bindings/.
1. Zero-Copy NumPy Buffer Integration
- Direct Pointer Passing: Prefer
py::array_t<T, py::array::c_style | py::array::forcecast>to receive or export multi-dimensional array data without copying underlying memory. - Buffer Protocol Interface: Map C++
CellandTrajectorycoordinate buffers directly to Python usingpy::buffer_info.
// GOOD: True zero-copy NumPy array exposure via std::span view
m.def("compute_distances", [](py::array_t<double, py::array::c_style> coords) {
if (coords.ndim() != 2 || coords.shape(1) != 3) {
throw std::invalid_argument("Input array must have shape (N, 3)");
}
// Zero-copy non-owning view over contiguous buffer memory
const auto* ptr = coords.data();
const auto total_doubles = static_cast<size_t>(coords.size());
std::span<const double> raw_span(ptr, total_doubles);
return DistanceCalculator::compute_from_flat_span(raw_span);
});
2. Global Interpreter Lock (GIL) Management
- Release GIL During Heavy Computations: Always instantiate
py::gil_scoped_releasebefore calling CPU-intensive OpenMP, TBB, or SYCL GPU calculations in C++. - Re-acquire GIL on Callback: If C++ code invokes a Python progress callback or logger, re-acquire the GIL using
py::gil_scoped_acquire.
// GOOD: Releasing GIL during compute phase
m.def("calculate_rdf", [](Trajectory& traj, const RDFNormalizationParams& params) {
py::gil_scoped_release release;
RDFCalculator calc;
return calc.calculate(traj, params); // Multi-threaded TBB compute loop
});
3. Class Exposure & Memory Ownership
- Holders & Shared Pointers: Use
std::shared_ptr<T>as the holder type (py::class_<T, std::shared_ptr<T>>) when instances are co-owned by Python and C++ runtimes. - Implicit Conversions & Type Safety: Keep bindings strictly type-checked and raise informative Python exceptions (
std::invalid_argument->ValueError,std::out_of_range->IndexError).
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 319k | Updated | Claude CodeCodex | Verified | Install |