1 #ifndef __CAPSULETHUNK_H
2 #define __CAPSULETHUNK_H
4 #if ( (PY_VERSION_HEX < 0x02070000) \
5 || ((PY_VERSION_HEX >= 0x03000000) \
6 && (PY_VERSION_HEX < 0x03010000)) )
8 #define __PyCapsule_GetField(capsule, field, default_value) \
9 ( PyCapsule_CheckExact(capsule) \
10 ? (((PyCObject *)capsule)->field) \
14 #define __PyCapsule_SetField(capsule, field, value) \
15 ( PyCapsule_CheckExact(capsule) \
16 ? (((PyCObject *)capsule)->field = value), 1 \
21 #define PyCapsule_Type PyCObject_Type
23 #define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule))
24 #define PyCapsule_IsValid(capsule, name) (PyCObject_Check(capsule))
27 #define PyCapsule_New(pointer, name, destructor) \
28 (PyCObject_FromVoidPtr(pointer, destructor))
31 #define PyCapsule_GetPointer(capsule, name) \
32 (PyCObject_AsVoidPtr(capsule))
34 /* Don't call PyCObject_SetPointer here, it fails if there's a destructor */
35 #define PyCapsule_SetPointer(capsule, pointer) \
36 __PyCapsule_SetField(capsule, cobject, pointer)
39 #define PyCapsule_GetDestructor(capsule) \
40 __PyCapsule_GetField(capsule, destructor)
42 #define PyCapsule_SetDestructor(capsule, dtor) \
43 __PyCapsule_SetField(capsule, destructor, dtor)
47 * Sorry, there's simply no place
48 * to store a Capsule "name" in a CObject.
50 #define PyCapsule_GetName(capsule) NULL
53 PyCapsule_SetName(PyObject *capsule, const char *unused)
56 PyErr_SetString(PyExc_NotImplementedError,
57 "can't use PyCapsule_SetName with CObjects");
63 #define PyCapsule_GetContext(capsule) \
64 __PyCapsule_GetField(capsule, descr)
66 #define PyCapsule_SetContext(capsule, context) \
67 __PyCapsule_SetField(capsule, descr, context)
71 PyCapsule_Import(const char *name, int no_block)
73 PyObject *object = NULL;
74 void *return_value = NULL;
76 size_t name_length = (strlen(name) + 1) * sizeof(char);
77 char *name_dup = (char *)PyMem_MALLOC(name_length);
83 memcpy(name_dup, name, name_length);
87 char *dot = strchr(trace, '.');
94 object = PyImport_ImportModuleNoBlock(trace);
96 object = PyImport_ImportModule(trace);
98 PyErr_Format(PyExc_ImportError,
99 "PyCapsule_Import could not "
100 "import module \"%s\"", trace);
104 PyObject *object2 = PyObject_GetAttrString(object, trace);
115 if (PyCObject_Check(object)) {
116 PyCObject *cobject = (PyCObject *)object;
117 return_value = cobject->cobject;
119 PyErr_Format(PyExc_AttributeError,
120 "PyCapsule_Import \"%s\" is not valid",
127 PyMem_FREE(name_dup);
132 #endif /* #if PY_VERSION_HEX < 0x02070000 */
134 #endif /* __CAPSULETHUNK_H */