1 | #ifndef __CAPSULETHUNK_H
|
---|
2 | #define __CAPSULETHUNK_H
|
---|
3 |
|
---|
4 | #if ( (PY_VERSION_HEX < 0x02070000) \
|
---|
5 | || ((PY_VERSION_HEX >= 0x03000000) \
|
---|
6 | && (PY_VERSION_HEX < 0x03010000)) )
|
---|
7 |
|
---|
8 | #define __PyCapsule_GetField(capsule, field, default_value) \
|
---|
9 | ( PyCapsule_CheckExact(capsule) \
|
---|
10 | ? (((PyCObject *)capsule)->field) \
|
---|
11 | : (default_value) \
|
---|
12 | ) \
|
---|
13 |
|
---|
14 | #define __PyCapsule_SetField(capsule, field, value) \
|
---|
15 | ( PyCapsule_CheckExact(capsule) \
|
---|
16 | ? (((PyCObject *)capsule)->field = value), 1 \
|
---|
17 | : 0 \
|
---|
18 | ) \
|
---|
19 |
|
---|
20 |
|
---|
21 | #define PyCapsule_Type PyCObject_Type
|
---|
22 |
|
---|
23 | #define PyCapsule_CheckExact(capsule) (PyCObject_Check(capsule))
|
---|
24 | #define PyCapsule_IsValid(capsule, name) (PyCObject_Check(capsule))
|
---|
25 |
|
---|
26 |
|
---|
27 | #define PyCapsule_New(pointer, name, destructor) \
|
---|
28 | (PyCObject_FromVoidPtr(pointer, destructor))
|
---|
29 |
|
---|
30 |
|
---|
31 | #define PyCapsule_GetPointer(capsule, name) \
|
---|
32 | (PyCObject_AsVoidPtr(capsule))
|
---|
33 |
|
---|
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)
|
---|
37 |
|
---|
38 |
|
---|
39 | #define PyCapsule_GetDestructor(capsule) \
|
---|
40 | __PyCapsule_GetField(capsule, destructor)
|
---|
41 |
|
---|
42 | #define PyCapsule_SetDestructor(capsule, dtor) \
|
---|
43 | __PyCapsule_SetField(capsule, destructor, dtor)
|
---|
44 |
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * Sorry, there's simply no place
|
---|
48 | * to store a Capsule "name" in a CObject.
|
---|
49 | */
|
---|
50 | #define PyCapsule_GetName(capsule) NULL
|
---|
51 |
|
---|
52 | static int
|
---|
53 | PyCapsule_SetName(PyObject *capsule, const char *unused)
|
---|
54 | {
|
---|
55 | unused = unused;
|
---|
56 | PyErr_SetString(PyExc_NotImplementedError,
|
---|
57 | "can't use PyCapsule_SetName with CObjects");
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | #define PyCapsule_GetContext(capsule) \
|
---|
64 | __PyCapsule_GetField(capsule, descr)
|
---|
65 |
|
---|
66 | #define PyCapsule_SetContext(capsule, context) \
|
---|
67 | __PyCapsule_SetField(capsule, descr, context)
|
---|
68 |
|
---|
69 |
|
---|
70 | static void *
|
---|
71 | PyCapsule_Import(const char *name, int no_block)
|
---|
72 | {
|
---|
73 | PyObject *object = NULL;
|
---|
74 | void *return_value = NULL;
|
---|
75 | char *trace;
|
---|
76 | size_t name_length = (strlen(name) + 1) * sizeof(char);
|
---|
77 | char *name_dup = (char *)PyMem_MALLOC(name_length);
|
---|
78 |
|
---|
79 | if (!name_dup) {
|
---|
80 | return NULL;
|
---|
81 | }
|
---|
82 |
|
---|
83 | memcpy(name_dup, name, name_length);
|
---|
84 |
|
---|
85 | trace = name_dup;
|
---|
86 | while (trace) {
|
---|
87 | char *dot = strchr(trace, '.');
|
---|
88 | if (dot) {
|
---|
89 | *dot++ = '\0';
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (object == NULL) {
|
---|
93 | if (no_block) {
|
---|
94 | object = PyImport_ImportModuleNoBlock(trace);
|
---|
95 | } else {
|
---|
96 | object = PyImport_ImportModule(trace);
|
---|
97 | if (!object) {
|
---|
98 | PyErr_Format(PyExc_ImportError,
|
---|
99 | "PyCapsule_Import could not "
|
---|
100 | "import module \"%s\"", trace);
|
---|
101 | }
|
---|
102 | }
|
---|
103 | } else {
|
---|
104 | PyObject *object2 = PyObject_GetAttrString(object, trace);
|
---|
105 | Py_DECREF(object);
|
---|
106 | object = object2;
|
---|
107 | }
|
---|
108 | if (!object) {
|
---|
109 | goto EXIT;
|
---|
110 | }
|
---|
111 |
|
---|
112 | trace = dot;
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (PyCObject_Check(object)) {
|
---|
116 | PyCObject *cobject = (PyCObject *)object;
|
---|
117 | return_value = cobject->cobject;
|
---|
118 | } else {
|
---|
119 | PyErr_Format(PyExc_AttributeError,
|
---|
120 | "PyCapsule_Import \"%s\" is not valid",
|
---|
121 | name);
|
---|
122 | }
|
---|
123 |
|
---|
124 | EXIT:
|
---|
125 | Py_XDECREF(object);
|
---|
126 | if (name_dup) {
|
---|
127 | PyMem_FREE(name_dup);
|
---|
128 | }
|
---|
129 | return return_value;
|
---|
130 | }
|
---|
131 |
|
---|
132 | #endif /* #if PY_VERSION_HEX < 0x02070000 */
|
---|
133 |
|
---|
134 | #endif /* __CAPSULETHUNK_H */
|
---|