The Python C API provides the interface for embedding the Python interpreter and extending Python with native code. PyO3 exposes this work through Rust types and helpers. section 6.5
Working with the interface requires:
- Acquiring Python’s GIL with Python::with_gil before using core interpreter data structures. section 6.5
- Creating a PyDict for locals and importing Python modules through the GIL handle. section 6.5
- Passing Rust values into Python and executing code with py.run. section 6.5
Benchmarking a Python extension
A Criterion benchmark loads json and a rust_json extension into a Python locals dictionary, supplies JSON input, and compares pure-Python execution with the Rust extension. The measured example reports 25.623 μs for pure Python and 21.987 μs for the extension. section 6.5
