Capsules

Refer to Providing a C API for an Extension Module for more information on using these objects.

Added in version 3.1.

type PyCapsule

This subtype of PyObject represents an opaque value, useful for C extension modules which need to pass an opaque value (as a void* pointer) through Python code to other C code. It is often used to make a C function pointer defined in one module available to other modules, so the regular import mechanism can be used to access C APIs defined in dynamically loaded modules.

PyTypeObject PyCapsule_Type
Part of the Stable ABI.

The type object corresponding to capsule objects. This is the same object as types.CapsuleType in the Python layer.

type PyCapsule_Destructor
Part of the Stable ABI.

The type of a destructor callback for a capsule. Defined as:

typedef void (*PyCapsule_Destructor)(PyObject *);

See PyCapsule_New() for the semantics of PyCapsule_Destructor callbacks.

int PyCapsule_CheckExact(PyObject *p)
Thread safety: Atomic.

Return true if its argument is a PyCapsule. This function always succeeds.

PyObject *PyCapsule_New(void *pointer, const char *name, PyCapsule_Destructor destructor)
Return value: New reference. Part of the Stable ABI. Thread safety: Atomic.

Create a PyCapsule encapsulating the pointer. The pointer argument may not be NULL.

On failure, set an exception and return NULL.

The name string may either be NULL or a pointer to a valid C string. If non-NULL, this string must outlive the capsule. (Though it is permitted to free it inside the destructor.)

If the destructor argument is not NULL, it will be called with the capsule as its argument when it is destroyed.

If this capsule will be stored as an attribute of a module, the name should be specified as modulename.attributename. This will enable other modules to import the capsule using PyCapsule_Import().

void *PyCapsule_GetPointer(PyObject *capsule, const char *name)
Part of the Stable ABI. Thread safety: Safe to call without external synchronization on distinct objects.

Retrieve the pointer stored in the capsule. On failure, set an exception and return NULL.

The name parameter must compare exactly to the name stored in the capsule. If the name stored in the capsule is NULL, the name passed in must also be NULL. Python uses the C function strcmp() to compare capsule names.

PyCapsule_Destructor