blob: 2cc1c7363e4df3148ee30afca8902821035fb10d [file] [log] [blame]
Larry Hastings402b73f2010-03-25 00:54:54 +00001
2/* Capsule objects let you wrap a C "void *" pointer in a Python
3 object. They're a way of passing data through the Python interpreter
4 without creating your own custom type.
5
6 Capsules are used for communication between extension modules.
7 They provide a way for an extension module to export a C interface
8 to other extension modules, so that extension modules can use the
9 Python import mechanism to link to one another.
10
11 For more information, please see "c-api/capsule.html" in the
12 documentation.
13*/
14
15#ifndef Py_CAPSULE_H
16#define Py_CAPSULE_H
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21PyAPI_DATA(PyTypeObject) PyCapsule_Type;
22
23typedef void (*PyCapsule_Destructor)(PyObject *);
24
25#define PyCapsule_CheckExact(op) (Py_TYPE(op) == &PyCapsule_Type)
26
27
28PyAPI_FUNC(PyObject *) PyCapsule_New(
29 void *pointer,
30 const char *name,
31 PyCapsule_Destructor destructor);
32
33PyAPI_FUNC(void *) PyCapsule_GetPointer(PyObject *capsule, const char *name);
34
35PyAPI_FUNC(PyCapsule_Destructor) PyCapsule_GetDestructor(PyObject *capsule);
36
37PyAPI_FUNC(const char *) PyCapsule_GetName(PyObject *capsule);
38
39PyAPI_FUNC(void *) PyCapsule_GetContext(PyObject *capsule);
40
41PyAPI_FUNC(int) PyCapsule_IsValid(PyObject *capsule, const char *name);
42
43PyAPI_FUNC(int) PyCapsule_SetPointer(PyObject *capsule, void *pointer);
44
45PyAPI_FUNC(int) PyCapsule_SetDestructor(PyObject *capsule, PyCapsule_Destructor destructor);
46
47PyAPI_FUNC(int) PyCapsule_SetName(PyObject *capsule, const char *name);
48
49PyAPI_FUNC(int) PyCapsule_SetContext(PyObject *capsule, void *context);
50
51PyAPI_FUNC(void *) PyCapsule_Import(const char *name, int no_block);
52
53
54#define PYTHON_USING_CAPSULE
55
56#define PYCAPSULE_INSTANTIATE_DESTRUCTOR(name, destructor) \
57static void pycapsule_destructor_ ## name(PyObject *ptr) \
58{ \
59 void *p = PyCapsule_GetPointer(ptr, name); \
60 if (p) { \
61 destructor(p); \
62 } \
63} \
64
65#define PYCAPSULE_NEW(pointer, name) \
66 (PyCapsule_New(pointer, name, capsule_destructor_ ## name))
67
68#define PYCAPSULE_ISVALID(capsule, name) \
69 (PyCapsule_IsValid(capsule, name))
70
71#define PYCAPSULE_DEREFERENCE(capsule, name) \
72 (PyCapsule_GetPointer(capsule, name))
73
74#define PYCAPSULE_SET(capsule, name, value) \
75 (PyCapsule_IsValid(capsule, name) && PyCapsule_SetPointer(capsule, value))
76
77/* module and attribute should be specified as string constants */
78#define PYCAPSULE_IMPORT(module, attribute) \
79 (PyCapsule_Import(module "." attribute, 0))
80
81
82/* begin public-domain code */
83/*
84** This code was written by Larry Hastings,
85** and is dedicated to the public domain.
86** It's designed to make it easy to switch
87** from CObject to Capsule objects without losing
88** backwards compatibility with prior versions
89** of CPython. You're encouraged to copy this code
90** (including this documentation) into your
91** Python C extension.
92**
93** To use:
94** * #define a name for the pointer you store in
95** the CObject. If you make the CObject available
96** as part of your module's API, this name should
97** be "modulename.attributename", and it should be
98** considered part of your API (so put it in your
99** header file).
100** * Specify a PYCAPSULE_INSTANTIATE_DESTRUCTOR(), in
101** every C file that creates these CObjects. This
102** is where you specify your object's destructor.
103** * Change all calls to CObject_FromVoidPtr()
104** and CObject_FromVoidPointerAndDesc() into
105** PYCAPSULE_NEW() calls.
106** * Change all calls to PyCObject_AsVoidPtr()
107** into PYCAPSULE_DEREFERENCE() calls.
108** * Change all calls to PyCObject_SetVoidPtr()
109** into PYCAPSULE_SET() calls.
110** * Change all calls to PyCObject_Import()
111** into PYCAPSULE_IMPORT() calls. Note that
112** the two arguments to PYCAPSULE_IMPORT()
113** should both be string constants; that is,
114** you should call
115** PYCAPSULE_IMPORT("modulename", "attributename"),
116** not PYCAPSULE_IMPORT(charstar1, charstar2).
117*/
118#ifndef PYTHON_USING_CAPSULE
119
120#define PYCAPSULE_INSTANTIATE_DESTRUCTOR(name, destructor) \
121static void pycapsule_destructor_ ## name(void *ptr) \
122{ \
123 destructor(p); \
124} \
125
126#define PYCAPSULE_NEW(pointer, name) \
127 (PyCObject_FromVoidPtr(pointer, pycapsule_destructor_ ## name))
128
129#define PYCAPSULE_ISVALID(capsule, name) \
130 (PyCObject_Check(capsule))
131
132#define PYCAPSULE_DEREFERENCE(capsule, name) \
133 (PyCObject_AsVoidPtr(capsule))
134
135#define PYCAPSULE_SET(capsule, name, value) \
136 (PyCObject_SetVoidPtr(capsule, value))
137
138/* module and attribute should be specified as string constants */
139#define PYCAPSULE_IMPORT(module, attribute) \
140 (PyCObject_Import(module, attribute))
141
142#endif /* PYTHON_USING_CAPSULE */
143/* end public-domain code */
144
145#ifdef __cplusplus
146}
147#endif
148#endif /* !Py_CAPSULE_H */