HPy: a better API for Python¶
HPy provides a new API for extending Python in C.
There are several advantages to writing C extensions in HPy:
Speed: it runs much faster on PyPy, GraalPy, and at native speed on CPython
Deployment: it is possible to compile a single binary which runs unmodified on all supported Python implementations and versions – think “stable ABI” on steroids
Simplicity: it is simpler and more manageable than the
Python.h
API, both for the users and the Pythons implementing itDebugging: it provides an improved debugging experience. Debug mode can be turned on at runtime without the need to recompile the extension or the Python running it. HPy design is more suitable for automated checks.
The official Python/C API,
also informally known as #include <Python.h>
, is
specific to the current implementation of CPython: it exposes a lot of
internal details which makes it hard to:
implement it for other Python implementations (e.g. PyPy, GraalPy, Jython, …)
experiment with new approaches inside CPython itself, for example:
use a tracing garbage collection instead of reference counting
remove the global interpreter lock (GIL) to take full advantage of multicore architectures
use tagged pointers to reduce memory footprint
Where to go next:¶
Full table of contents:¶
- HPy Quickstart
- HPy Overview
- HPy API Introduction
- Porting Guide
- Porting
PyObject *
to HPy API constructs - Direct C API to HPy mappings
- Reference Counting
Py_INCREF
andPy_DECREF
- Calling functions
PyObject_Call
andPyObject_CallObject
- Calling Protocol
- PyModule_AddObject
- Deallocator slot
Py_tp_dealloc
- Special slots
Py_tp_methods
,Py_tp_members
, andPy_tp_getset
- Creating lists and tuples
- Buffers
- Multi-phase Module Initialization
- Porting
- Porting Example
- Debug Mode
- Trace Mode
- API Reference
- Contributing
- Misc Notes
- Changelog