pythoncapi_compat.h API¶
The pythoncapi_compat.h header file provides implementations of recent
functions for old Python versions.
Supported Python versions:
Python 3.6 - 3.14
PyPy 2.7 and PyPy 3.6 - 3.10
Python 2.7 and Python 3.5 are no longer officially supported since GitHub Actions doesn’t support them anymore: only best effort support is provided.
C++03 and C++11 are supported on Python 3.6 and newer.
A C11 subset (without optional features) is required, like static inline
functions: see PEP 7. ISO C90
is partially supported for Python 2.7.
Some functions related to frame objects and PyThreadState are not available
on PyPy.
Latest version of the header file: pythoncapi_compat.h.
Python 3.15¶
-
PyBytesWriter *PyBytesWriter_Create(Py_ssize_t size)¶
-
void *PyBytesWriter_GetData(PyBytesWriter *writer)¶
-
Py_ssize_t PyBytesWriter_GetSize(PyBytesWriter *writer)¶
-
PyObject *PyBytesWriter_FinishWithSize(PyBytesWriter *writer, Py_ssize_t size)¶
-
PyObject *PyBytesWriter_Finish(PyBytesWriter *writer)¶
-
PyObject *PyBytesWriter_FinishWithPointer(PyBytesWriter *writer, void *buf)¶
-
void PyBytesWriter_Discard(PyBytesWriter *writer)¶
-
int PyBytesWriter_Resize(PyBytesWriter *writer, Py_ssize_t size)¶
-
int PyBytesWriter_Grow(PyBytesWriter *writer, Py_ssize_t size)¶
-
void *PyBytesWriter_GrowAndUpdatePointer(PyBytesWriter *writer, Py_ssize_t size, void *buf)¶
-
int PyBytesWriter_WriteBytes(PyBytesWriter *writer, const void *bytes, Py_ssize_t size)¶
-
int PyBytesWriter_Format(PyBytesWriter *writer, const char *format, ...)¶
-
PyObject *PySys_GetAttr(const char *name)¶
-
PyObject *PySys_GetAttrString(const char *name)¶
-
PyObject *PySys_GetOptionalAttr(const char *name)¶
-
PyObject *PySys_GetOptionalAttrString(const char *name)¶
-
PyObject *PyTuple_FromArray(PyObject *const *array, Py_ssize_t size)¶
-
Py_hash_t PyUnstable_Unicode_GET_CACHED_HASH(PyObject *op)¶
See PyUnstable_Unicode_GET_CACHED_HASH() documentation.
On PyPy, always returns
-1.
Python 3.14¶
-
struct PyLongLayout¶
-
const PyLongLayout *PyLong_GetNativeLayout(void)¶
-
struct PyLongExport¶
-
int PyLong_Export(PyObject *obj, PyLongExport *export_long)¶
-
void PyLong_FreeExport(PyLongExport *export_long)¶
-
struct PyLongWriter¶
-
PyLongWriter *PyLongWriter_Create(int negative, Py_ssize_t ndigits, void **digits)¶
-
PyObject *PyLongWriter_Finish(PyLongWriter *writer)¶
-
void PyLongWriter_Discard(PyLongWriter *writer)¶
-
int PyLong_IsPositive(PyObject *obj)¶
-
int PyLong_IsNegative(PyObject *obj)¶
-
int PyLong_IsZero(PyObject *obj)¶
-
int PyLong_GetSign(PyObject *obj, int *sign)¶
-
PyObject *PyIter_NextItem(PyObject *sep, PyObject *iterable)¶
-
PyObject *PyBytes_Join(PyObject *sep, PyObject *iterable)¶
-
Py_hash_t Py_HashBuffer(const void *ptr, Py_ssize_t len)¶
-
int PyUnicode_Equal(PyObject *str1, PyObject *str2)¶
-
PyUnicodeWriter *PyUnicodeWriter_Create(Py_ssize_t length)¶
-
PyObject *PyUnicodeWriter_Finish(PyUnicodeWriter *writer)¶
-
void PyUnicodeWriter_Discard(PyUnicodeWriter *writer)¶
-
int PyUnicodeWriter_WriteChar(PyUnicodeWriter *writer, Py_UCS4 ch)¶
-
int PyUnicodeWriter_WriteUTF8(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)¶
-
int PyUnicodeWriter_WriteASCII(PyUnicodeWriter *writer, const char *str, Py_ssize_t size)¶
-
int PyUnicodeWriter_WriteWideChar(PyUnicodeWriter *writer, const wchar_t *str, Py_ssize_t size)¶
-
int PyUnicodeWriter_WriteStr(PyUnicodeWriter *writer, PyObject *obj)¶
-
int PyUnicodeWriter_WriteRepr(PyUnicodeWriter *writer, PyObject *obj)¶
-
int PyUnicodeWriter_WriteSubstring(PyUnicodeWriter *writer, PyObject *str, Py_ssize_t start, Py_ssize_t end)¶
-
int PyUnicodeWriter_Format(PyUnicodeWriter *writer, const char *format, ...)¶
-
int PyLong_AsInt32(PyObject *obj, int32_t *pvalue)¶
-
int PyLong_AsInt64(PyObject *obj, int64_t *pvalue)¶
-
int PyLong_AsUInt32(PyObject *obj, uint32_t *pvalue)¶
-
int PyLong_AsUInt64(PyObject *obj, uint64_t *pvalue)¶
-
PyObject *PyLong_FromInt32(int32_t value)¶
-
PyObject *PyLong_FromInt64(int64_t value)¶
-
PyObject *PyLong_FromUInt32(uint32_t value)¶
-
PyObject *PyLong_FromUInt64(uint64_t value)¶
-
FILE *Py_fopen(PyObject *path, const char *mode)¶
-
int Py_fclose(FILE *file)¶
-
PyObject *PyConfig_Get(const char *name)¶
-
int PyConfig_GetInt(const char *name, int *value)¶
-
int PyUnstable_Object_IsUniquelyReferenced(PyObject *op)¶
Not supported:
PyConfig_Names()PyConfig_Set()PyInitConfig_AddModule()PyInitConfig_Create()PyInitConfig_Free()PyInitConfig_FreeStrList()PyInitConfig_GetError()PyInitConfig_GetExitCode()PyInitConfig_GetInt()PyInitConfig_GetStr()PyInitConfig_GetStrList()PyInitConfig_HasOption()PyInitConfig_SetInt()PyInitConfig_SetStr()PyInitConfig_SetStrList()PyType_GetBaseByToken()PyUnicodeWriter_DecodeUTF8Stateful()Py_InitializeFromInitConfig()
Python 3.13¶
-
int PyDict_GetItemRef(PyObject *p, PyObject *key, PyObject **result)¶
-
int PyDict_GetItemStringRef(PyObject *p, const char *key, PyObject **result)¶
-
PyObject *PyImport_AddModuleRef(const char *name)¶
-
int PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result)¶
-
int PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result)¶
-
int PyObject_HasAttrWithError(PyObject *obj, PyObject *attr_name)¶
-
int PyObject_HasAttrStringWithError(PyObject *obj, const char *attr_name)¶
-
int PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)¶
-
int PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)¶
-
int PyMapping_HasKeyWithError(PyObject *obj, PyObject *key)¶
-
int PyMapping_HasKeyStringWithError(PyObject *obj, const char *key)¶
-
int PyModule_Add(PyObject *module, const char *name, PyObject *value)¶
-
int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)¶
-
int Py_IsFinalizing()¶
Return non-zero if the Python interpreter is shutting down, return 0 otherwise.
Availability: Python 3.3 and newer, PyPy 7.3 and newer.
-
int PyDict_ContainsString(PyObject *p, const char *key)¶
-
int PyLong_AsInt(PyObject *obj)¶
-
int PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)¶
-
void PyObject_ClearManagedDict(PyObject *obj)¶
-
PyThreadState *PyThreadState_GetUnchecked(void)¶
See PyThreadState_GetUnchecked() documentation.
Available on Python 3.5.2 and newer.
-
int PyUnicode_EqualToUTF8(PyObject *unicode, const char *str)¶
-
int PyUnicode_EqualToUTF8AndSize(PyObject *unicode, const char *str, Py_ssize_t size)¶
-
int PyList_Extend(PyObject *list, PyObject *iterable)¶
-
int PyList_Clear(PyObject *list)¶
-
int PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result)¶
-
int PyDict_PopString(PyObject *dict, const char *key, PyObject **result)¶
-
Py_hash_t Py_HashPointer(const void *ptr)¶
-
type PyTime_t¶
A timestamp or duration in nanoseconds, represented as a signed 64-bit integer.
-
PyObject *PyList_GetItemRef(PyObject *op, Py_ssize_t index)¶
-
int PyDict_SetDefaultRef(PyObject *d, PyObject *key, PyObject *default_value, PyObject **result)¶
Not supported:
PyErr_FormatUnraisable().PyLong_AsNativeBytes()PyLong_FromNativeBytes()PyLong_FromUnsignedNativeBytes()PyObject_GenericHash().PySys_Audit().PySys_AuditTuple().PyType_GetFullyQualifiedName()PyType_GetModuleName()
Python 3.12¶
-
PyObject *PyFrame_GetVar(PyFrameObject *frame, PyObject *name)¶
See PyFrame_GetVar() documentation.
Not available on PyPy.
-
PyObject *PyFrame_GetVarString(PyFrameObject *frame, const char *name)¶
See PyFrame_GetVarString() documentation.
Not available on PyPy.
-
PyObject *Py_GetConstant(unsigned int constant_id)¶
-
PyObject *Py_GetConstantBorrowed(unsigned int constant_id)¶
Not supported:
PyDict_AddWatcher(),PyDict_Watch().PyCode_AddWatcher(),PyCode_ClearWatcher().PyErr_GetRaisedException(),PyErr_SetRaisedException()._PyErr_ChainExceptions1().PyErr_DisplayException()._Py_IsImmortal().Py_NewInterpreterFromConfig().PyException_GetArgs(),PyException_SetArgs().PyEval_SetProfileAllThreads(),PyEval_SetTraceAllThreads().PyFunction_SetVectorcall().PyType_FromMetaclass(): implementation too big to be backported.PyVectorcall_Call().
Python 3.11¶
-
PyObject *PyCode_GetCellvars(PyCodeObject *code)¶
See PyCode_GetCellvars() documentation.
Not available on PyPy.
-
PyObject *PyCode_GetCode(PyCodeObject *code)¶
See PyCode_GetCode() documentation.
Not available on PyPy.
-
PyObject *PyCode_GetFreevars(PyCodeObject *code)¶
See PyCode_GetFreevars() documentation.
Not available on PyPy.
-
PyObject *PyCode_GetVarnames(PyCodeObject *code)¶
See PyCode_GetVarnames() documentation.
Not available on PyPy.
-
PyObject *PyFrame_GetBuiltins(PyFrameObject *frame)¶
See PyFrame_GetBuiltins() documentation.
Not available on PyPy.
-
PyObject *PyFrame_GetGlobals(PyFrameObject *frame)¶
See PyFrame_GetGlobals() documentation.
Not available on PyPy.
-
int PyFrame_GetLasti(PyFrameObject *frame)¶
See PyFrame_GetLasti() documentation.
Not available on PyPy.
-
PyObject *PyFrame_GetLocals(PyFrameObject *frame)¶
See PyFrame_GetLocals() documentation.
Not available on PyPy.
-
void PyThreadState_EnterTracing(PyThreadState *tstate)¶
See PyThreadState_EnterTracing() documentation.
Not available on PyPy.
-
void PyThreadState_LeaveTracing(PyThreadState *tstate)¶
See PyThreadState_LeaveTracing() documentation.
Not available on PyPy
-
int PyFloat_Pack2(double x, unsigned char *p, int le)¶
Pack a C double as the IEEE 754 binary16 half-precision format.
Availability: Python 3.6 and newer. Not available on PyPy
-
int PyFloat_Pack4(double x, unsigned char *p, int le)¶
Pack a C double as the IEEE 754 binary32 single precision format.
Not available on PyPy
-
int PyFloat_Pack8(double x, unsigned char *p, int le)¶
Pack a C double as the IEEE 754 binary64 double precision format.
Not available on PyPy
-
double PyFloat_Unpack2(const unsigned char *p, int le)¶
Unpack the IEEE 754 binary16 half-precision format as a C double.
Availability: Python 3.6 and newer. Not available on PyPy
-
double PyFloat_Unpack4(const unsigned char *p, int le)¶
Unpack the IEEE 754 binary32 single precision format as a C double.
Not available on PyPy
-
double PyFloat_Unpack8(const unsigned char *p, int le)¶
Unpack the IEEE 754 binary64 double precision format as a C double.
Not available on PyPy
Not supported:
PyType_GetModuleByDef().PyType_GetName().PyType_GetQualName().Py_Versionconstant.PyErr_GetHandledException(),PyErr_SetHandledException().PyFrame_GetGenerator().
Python 3.10¶
-
PyObject *Py_NewRef(PyObject *obj)¶
-
PyObject *Py_XNewRef(PyObject *obj)¶
-
int Py_Is(PyObject *x, PyObject *y)¶
-
int Py_IsNone(PyObject *x)¶
-
int Py_IsTrue(PyObject *x)¶
-
int Py_IsFalse(PyObject *x)¶
-
int PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)¶
Not supported:
PyCodec_Unregister().PyDateTime_DATE_GET_TZINFO(),PyDateTime_TIME_GET_TZINFO().PyErr_SetInterruptEx().PyGC_Enable(),PyGC_Disable()andPyGC_IsEnabled().PyIter_Send().PySet_CheckExact().Py_TPFLAGS_DISALLOW_INSTANTIATIONconstant.Py_TPFLAGS_IMMUTABLETYPEconstant.
Python 3.9¶
PyObject¶
-
void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)¶
-
void Py_SET_TYPE(PyObject *ob, PyTypeObject *type)¶
-
void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)¶
-
int Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type)¶
-
PyObject *PyObject_CallNoArgs(PyObject *func)¶
-
PyObject *PyObject_CallOneArg(PyObject *func, PyObject *arg)¶
-
PyObject *PyObject_Vectorcall(PyObject *callable, PyObject *const *args, size_t nargsf, PyObject *kwnames)¶
-
Py_ssize_t PyVectorcall_NARGS(size_t nargsf)¶
-
PY_VECTORCALL_ARGUMENTS_OFFSET¶
Not supported:
PyVectorcall_CallMethod().PyType_FromModuleAndSpec()
PyFrameObject¶
-
PyCodeObject *PyFrame_GetCode(PyFrameObject *frame)¶
-
PyFrameObject *PyFrame_GetBack(PyFrameObject *frame)¶
See PyFrame_GetBack() documentation.
Not available on PyPy
PyThreadState¶
-
PyFrameObject *PyThreadState_GetFrame(PyThreadState *tstate)¶
See PyThreadState_GetFrame() documentation.
Not available on PyPy
-
PyInterpreterState *PyThreadState_GetInterpreter(PyThreadState *tstate)¶
-
uint64_t PyThreadState_GetID(PyThreadState *tstate)¶
See PyThreadState_GetID() documentation.
Availability: Python 3.7. Not available on PyPy.
PyInterpreterState¶
-
PyInterpreterState *PyInterpreterState_Get(void)¶
GC protocol¶
-
int PyObject_GC_IsTracked(PyObject *obj)¶
See PyObject_GC_IsTracked() documentation.
Not available on PyPy.
-
int PyObject_GC_IsFinalized(PyObject *obj)¶
See PyObject_GC_IsFinalized() documentation.
Availability: Python 3.4. Not available on PyPy.
Module helper¶
-
int PyModule_AddType(PyObject *module, PyTypeObject *type)¶
Python 3.8¶
Not supported:
PyCode_NewWithPosOnlyArgs().
Python 3.7¶
Not supported:
PyImport_GetModule().PyInterpreterState_GetID().PySlice_Unpack(),PySlice_AdjustIndices().PyTimeZone_FromOffset(),PyTimeZone_FromOffsetAndName().Py_RETURN_RICHCOMPARE().Py_UNREACHABLEmacro.
Python 3.6¶
Not supported:
PyErr_ResourceWarning().PyErr_SetImportErrorSubclass().PyOS_FSPath().Py_FinalizeEx().
Python 3.5.2¶
-
Py_SETREF(op, op2)¶
-
Py_XSETREF(op, op2)¶
Not supported:
PyCodec_NameReplaceErrors().PyErr_FormatV().PyExc_RecursionError.PyModule_FromDefAndSpec(),PyModule_FromDefAndSpec2(), andPyModule_ExecDef().PyNumber_MatrixMultiply()andPyNumber_InPlaceMatrixMultiply().
Python 3.4¶
-
Py_UNUSED(name)¶
Python 3.2¶
Not supported:
Py_VA_COPY.PySys_SetArgvEx().PyLong_AsLongLongAndOverflow().PyErr_NewExceptionWithDoc().
Python 3.1¶
Not supported:
PyOS_string_to_double().PyCapsuleAPI.
Borrow variant¶
To ease migration of C extensions to the new C API, a variant is provided to return borrowed references rather than strong references.
These functions are only available in pythoncapi_compat.h and are not
part of the Python C API.
-
PyObject *_Py_StealRef(PyObject *ob)¶
Similar to
Py_DECREF(ob); return ob;.
-
PyObject *_Py_XStealRef(PyObject *ob)¶
Similar to
Py_XDECREF(ob); return ob;.
-
PyFrameObject *_PyThreadState_GetFrameBorrow(PyThreadState *tstate)¶
PyThreadState_GetFrame()variant. Not available on PyPy.
-
PyCodeObject *_PyFrame_GetCodeBorrow(PyFrameObject *frame)¶
PyFrame_GetCode()variant.
-
PyFrameObject *_PyFrame_GetBackBorrow(PyFrameObject *frame)¶
PyFrame_GetBack()variant Not available on PyPy.
For example, tstate->frame can be replaced with
_PyThreadState_GetFrameBorrow(tstate) to avoid accessing directly
PyThreadState.frame member.