1 | n/a | #include "Python.h" |
---|
2 | n/a | #include "frameobject.h" |
---|
3 | n/a | |
---|
4 | n/a | #include <ffi.h> |
---|
5 | n/a | #ifdef MS_WIN32 |
---|
6 | n/a | #include <windows.h> |
---|
7 | n/a | #endif |
---|
8 | n/a | #include "ctypes.h" |
---|
9 | n/a | |
---|
10 | n/a | /**************************************************************/ |
---|
11 | n/a | |
---|
12 | n/a | static void |
---|
13 | n/a | CThunkObject_dealloc(PyObject *myself) |
---|
14 | n/a | { |
---|
15 | n/a | CThunkObject *self = (CThunkObject *)myself; |
---|
16 | n/a | PyObject_GC_UnTrack(self); |
---|
17 | n/a | Py_XDECREF(self->converters); |
---|
18 | n/a | Py_XDECREF(self->callable); |
---|
19 | n/a | Py_XDECREF(self->restype); |
---|
20 | n/a | if (self->pcl_write) |
---|
21 | n/a | ffi_closure_free(self->pcl_write); |
---|
22 | n/a | PyObject_GC_Del(self); |
---|
23 | n/a | } |
---|
24 | n/a | |
---|
25 | n/a | static int |
---|
26 | n/a | CThunkObject_traverse(PyObject *myself, visitproc visit, void *arg) |
---|
27 | n/a | { |
---|
28 | n/a | CThunkObject *self = (CThunkObject *)myself; |
---|
29 | n/a | Py_VISIT(self->converters); |
---|
30 | n/a | Py_VISIT(self->callable); |
---|
31 | n/a | Py_VISIT(self->restype); |
---|
32 | n/a | return 0; |
---|
33 | n/a | } |
---|
34 | n/a | |
---|
35 | n/a | static int |
---|
36 | n/a | CThunkObject_clear(PyObject *myself) |
---|
37 | n/a | { |
---|
38 | n/a | CThunkObject *self = (CThunkObject *)myself; |
---|
39 | n/a | Py_CLEAR(self->converters); |
---|
40 | n/a | Py_CLEAR(self->callable); |
---|
41 | n/a | Py_CLEAR(self->restype); |
---|
42 | n/a | return 0; |
---|
43 | n/a | } |
---|
44 | n/a | |
---|
45 | n/a | PyTypeObject PyCThunk_Type = { |
---|
46 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
---|
47 | n/a | "_ctypes.CThunkObject", |
---|
48 | n/a | sizeof(CThunkObject), /* tp_basicsize */ |
---|
49 | n/a | sizeof(ffi_type), /* tp_itemsize */ |
---|
50 | n/a | CThunkObject_dealloc, /* tp_dealloc */ |
---|
51 | n/a | 0, /* tp_print */ |
---|
52 | n/a | 0, /* tp_getattr */ |
---|
53 | n/a | 0, /* tp_setattr */ |
---|
54 | n/a | 0, /* tp_reserved */ |
---|
55 | n/a | 0, /* tp_repr */ |
---|
56 | n/a | 0, /* tp_as_number */ |
---|
57 | n/a | 0, /* tp_as_sequence */ |
---|
58 | n/a | 0, /* tp_as_mapping */ |
---|
59 | n/a | 0, /* tp_hash */ |
---|
60 | n/a | 0, /* tp_call */ |
---|
61 | n/a | 0, /* tp_str */ |
---|
62 | n/a | 0, /* tp_getattro */ |
---|
63 | n/a | 0, /* tp_setattro */ |
---|
64 | n/a | 0, /* tp_as_buffer */ |
---|
65 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
66 | n/a | "CThunkObject", /* tp_doc */ |
---|
67 | n/a | CThunkObject_traverse, /* tp_traverse */ |
---|
68 | n/a | CThunkObject_clear, /* tp_clear */ |
---|
69 | n/a | 0, /* tp_richcompare */ |
---|
70 | n/a | 0, /* tp_weaklistoffset */ |
---|
71 | n/a | 0, /* tp_iter */ |
---|
72 | n/a | 0, /* tp_iternext */ |
---|
73 | n/a | 0, /* tp_methods */ |
---|
74 | n/a | 0, /* tp_members */ |
---|
75 | n/a | }; |
---|
76 | n/a | |
---|
77 | n/a | /**************************************************************/ |
---|
78 | n/a | |
---|
79 | n/a | static void |
---|
80 | n/a | PrintError(const char *msg, ...) |
---|
81 | n/a | { |
---|
82 | n/a | char buf[512]; |
---|
83 | n/a | PyObject *f = PySys_GetObject("stderr"); |
---|
84 | n/a | va_list marker; |
---|
85 | n/a | |
---|
86 | n/a | va_start(marker, msg); |
---|
87 | n/a | vsnprintf(buf, sizeof(buf), msg, marker); |
---|
88 | n/a | va_end(marker); |
---|
89 | n/a | if (f != NULL && f != Py_None) |
---|
90 | n/a | PyFile_WriteString(buf, f); |
---|
91 | n/a | PyErr_Print(); |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | |
---|
95 | n/a | #ifdef MS_WIN32 |
---|
96 | n/a | /* |
---|
97 | n/a | * We must call AddRef() on non-NULL COM pointers we receive as arguments |
---|
98 | n/a | * to callback functions - these functions are COM method implementations. |
---|
99 | n/a | * The Python instances we create have a __del__ method which calls Release(). |
---|
100 | n/a | * |
---|
101 | n/a | * The presence of a class attribute named '_needs_com_addref_' triggers this |
---|
102 | n/a | * behaviour. It would also be possible to call the AddRef() Python method, |
---|
103 | n/a | * after checking for PyObject_IsTrue(), but this would probably be somewhat |
---|
104 | n/a | * slower. |
---|
105 | n/a | */ |
---|
106 | n/a | static void |
---|
107 | n/a | TryAddRef(StgDictObject *dict, CDataObject *obj) |
---|
108 | n/a | { |
---|
109 | n/a | IUnknown *punk; |
---|
110 | n/a | |
---|
111 | n/a | if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_")) |
---|
112 | n/a | return; |
---|
113 | n/a | |
---|
114 | n/a | punk = *(IUnknown **)obj->b_ptr; |
---|
115 | n/a | if (punk) |
---|
116 | n/a | punk->lpVtbl->AddRef(punk); |
---|
117 | n/a | return; |
---|
118 | n/a | } |
---|
119 | n/a | #endif |
---|
120 | n/a | |
---|
121 | n/a | /****************************************************************************** |
---|
122 | n/a | * |
---|
123 | n/a | * Call the python object with all arguments |
---|
124 | n/a | * |
---|
125 | n/a | */ |
---|
126 | n/a | static void _CallPythonObject(void *mem, |
---|
127 | n/a | ffi_type *restype, |
---|
128 | n/a | SETFUNC setfunc, |
---|
129 | n/a | PyObject *callable, |
---|
130 | n/a | PyObject *converters, |
---|
131 | n/a | int flags, |
---|
132 | n/a | void **pArgs) |
---|
133 | n/a | { |
---|
134 | n/a | Py_ssize_t i; |
---|
135 | n/a | PyObject *result; |
---|
136 | n/a | PyObject *arglist = NULL; |
---|
137 | n/a | Py_ssize_t nArgs; |
---|
138 | n/a | PyObject *error_object = NULL; |
---|
139 | n/a | int *space; |
---|
140 | n/a | #ifdef WITH_THREAD |
---|
141 | n/a | PyGILState_STATE state = PyGILState_Ensure(); |
---|
142 | n/a | #endif |
---|
143 | n/a | |
---|
144 | n/a | nArgs = PySequence_Length(converters); |
---|
145 | n/a | /* Hm. What to return in case of error? |
---|
146 | n/a | For COM, 0xFFFFFFFF seems better than 0. |
---|
147 | n/a | */ |
---|
148 | n/a | if (nArgs < 0) { |
---|
149 | n/a | PrintError("BUG: PySequence_Length"); |
---|
150 | n/a | goto Done; |
---|
151 | n/a | } |
---|
152 | n/a | |
---|
153 | n/a | arglist = PyTuple_New(nArgs); |
---|
154 | n/a | if (!arglist) { |
---|
155 | n/a | PrintError("PyTuple_New()"); |
---|
156 | n/a | goto Done; |
---|
157 | n/a | } |
---|
158 | n/a | for (i = 0; i < nArgs; ++i) { |
---|
159 | n/a | /* Note: new reference! */ |
---|
160 | n/a | PyObject *cnv = PySequence_GetItem(converters, i); |
---|
161 | n/a | StgDictObject *dict; |
---|
162 | n/a | if (cnv) |
---|
163 | n/a | dict = PyType_stgdict(cnv); |
---|
164 | n/a | else { |
---|
165 | n/a | PrintError("Getting argument converter %d\n", i); |
---|
166 | n/a | goto Done; |
---|
167 | n/a | } |
---|
168 | n/a | |
---|
169 | n/a | if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) { |
---|
170 | n/a | PyObject *v = dict->getfunc(*pArgs, dict->size); |
---|
171 | n/a | if (!v) { |
---|
172 | n/a | PrintError("create argument %d:\n", i); |
---|
173 | n/a | Py_DECREF(cnv); |
---|
174 | n/a | goto Done; |
---|
175 | n/a | } |
---|
176 | n/a | PyTuple_SET_ITEM(arglist, i, v); |
---|
177 | n/a | /* XXX XXX XX |
---|
178 | n/a | We have the problem that c_byte or c_short have dict->size of |
---|
179 | n/a | 1 resp. 4, but these parameters are pushed as sizeof(int) bytes. |
---|
180 | n/a | BTW, the same problem occurs when they are pushed as parameters |
---|
181 | n/a | */ |
---|
182 | n/a | } else if (dict) { |
---|
183 | n/a | /* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */ |
---|
184 | n/a | CDataObject *obj = (CDataObject *)_PyObject_CallNoArg(cnv); |
---|
185 | n/a | if (!obj) { |
---|
186 | n/a | PrintError("create argument %d:\n", i); |
---|
187 | n/a | Py_DECREF(cnv); |
---|
188 | n/a | goto Done; |
---|
189 | n/a | } |
---|
190 | n/a | if (!CDataObject_Check(obj)) { |
---|
191 | n/a | Py_DECREF(obj); |
---|
192 | n/a | Py_DECREF(cnv); |
---|
193 | n/a | PrintError("unexpected result of create argument %d:\n", i); |
---|
194 | n/a | goto Done; |
---|
195 | n/a | } |
---|
196 | n/a | memcpy(obj->b_ptr, *pArgs, dict->size); |
---|
197 | n/a | PyTuple_SET_ITEM(arglist, i, (PyObject *)obj); |
---|
198 | n/a | #ifdef MS_WIN32 |
---|
199 | n/a | TryAddRef(dict, obj); |
---|
200 | n/a | #endif |
---|
201 | n/a | } else { |
---|
202 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
203 | n/a | "cannot build parameter"); |
---|
204 | n/a | PrintError("Parsing argument %d\n", i); |
---|
205 | n/a | Py_DECREF(cnv); |
---|
206 | n/a | goto Done; |
---|
207 | n/a | } |
---|
208 | n/a | Py_DECREF(cnv); |
---|
209 | n/a | /* XXX error handling! */ |
---|
210 | n/a | pArgs++; |
---|
211 | n/a | } |
---|
212 | n/a | |
---|
213 | n/a | #define CHECK(what, x) \ |
---|
214 | n/a | if (x == NULL) _PyTraceback_Add(what, "_ctypes/callbacks.c", __LINE__ - 1), PyErr_Print() |
---|
215 | n/a | |
---|
216 | n/a | if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) { |
---|
217 | n/a | error_object = _ctypes_get_errobj(&space); |
---|
218 | n/a | if (error_object == NULL) |
---|
219 | n/a | goto Done; |
---|
220 | n/a | if (flags & FUNCFLAG_USE_ERRNO) { |
---|
221 | n/a | int temp = space[0]; |
---|
222 | n/a | space[0] = errno; |
---|
223 | n/a | errno = temp; |
---|
224 | n/a | } |
---|
225 | n/a | #ifdef MS_WIN32 |
---|
226 | n/a | if (flags & FUNCFLAG_USE_LASTERROR) { |
---|
227 | n/a | int temp = space[1]; |
---|
228 | n/a | space[1] = GetLastError(); |
---|
229 | n/a | SetLastError(temp); |
---|
230 | n/a | } |
---|
231 | n/a | #endif |
---|
232 | n/a | } |
---|
233 | n/a | |
---|
234 | n/a | result = PyObject_CallObject(callable, arglist); |
---|
235 | n/a | CHECK("'calling callback function'", result); |
---|
236 | n/a | |
---|
237 | n/a | #ifdef MS_WIN32 |
---|
238 | n/a | if (flags & FUNCFLAG_USE_LASTERROR) { |
---|
239 | n/a | int temp = space[1]; |
---|
240 | n/a | space[1] = GetLastError(); |
---|
241 | n/a | SetLastError(temp); |
---|
242 | n/a | } |
---|
243 | n/a | #endif |
---|
244 | n/a | if (flags & FUNCFLAG_USE_ERRNO) { |
---|
245 | n/a | int temp = space[0]; |
---|
246 | n/a | space[0] = errno; |
---|
247 | n/a | errno = temp; |
---|
248 | n/a | } |
---|
249 | n/a | Py_XDECREF(error_object); |
---|
250 | n/a | |
---|
251 | n/a | if ((restype != &ffi_type_void) && result) { |
---|
252 | n/a | PyObject *keep; |
---|
253 | n/a | assert(setfunc); |
---|
254 | n/a | #ifdef WORDS_BIGENDIAN |
---|
255 | n/a | /* See the corresponding code in callproc.c, around line 961 */ |
---|
256 | n/a | if (restype->type != FFI_TYPE_FLOAT && restype->size < sizeof(ffi_arg)) |
---|
257 | n/a | mem = (char *)mem + sizeof(ffi_arg) - restype->size; |
---|
258 | n/a | #endif |
---|
259 | n/a | keep = setfunc(mem, result, 0); |
---|
260 | n/a | CHECK("'converting callback result'", keep); |
---|
261 | n/a | /* keep is an object we have to keep alive so that the result |
---|
262 | n/a | stays valid. If there is no such object, the setfunc will |
---|
263 | n/a | have returned Py_None. |
---|
264 | n/a | |
---|
265 | n/a | If there is such an object, we have no choice than to keep |
---|
266 | n/a | it alive forever - but a refcount and/or memory leak will |
---|
267 | n/a | be the result. EXCEPT when restype is py_object - Python |
---|
268 | n/a | itself knows how to manage the refcount of these objects. |
---|
269 | n/a | */ |
---|
270 | n/a | if (keep == NULL) /* Could not convert callback result. */ |
---|
271 | n/a | PyErr_WriteUnraisable(callable); |
---|
272 | n/a | else if (keep == Py_None) /* Nothing to keep */ |
---|
273 | n/a | Py_DECREF(keep); |
---|
274 | n/a | else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) { |
---|
275 | n/a | if (-1 == PyErr_WarnEx(PyExc_RuntimeWarning, |
---|
276 | n/a | "memory leak in callback function.", |
---|
277 | n/a | 1)) |
---|
278 | n/a | PyErr_WriteUnraisable(callable); |
---|
279 | n/a | } |
---|
280 | n/a | } |
---|
281 | n/a | Py_XDECREF(result); |
---|
282 | n/a | Done: |
---|
283 | n/a | Py_XDECREF(arglist); |
---|
284 | n/a | #ifdef WITH_THREAD |
---|
285 | n/a | PyGILState_Release(state); |
---|
286 | n/a | #endif |
---|
287 | n/a | } |
---|
288 | n/a | |
---|
289 | n/a | static void closure_fcn(ffi_cif *cif, |
---|
290 | n/a | void *resp, |
---|
291 | n/a | void **args, |
---|
292 | n/a | void *userdata) |
---|
293 | n/a | { |
---|
294 | n/a | CThunkObject *p = (CThunkObject *)userdata; |
---|
295 | n/a | |
---|
296 | n/a | _CallPythonObject(resp, |
---|
297 | n/a | p->ffi_restype, |
---|
298 | n/a | p->setfunc, |
---|
299 | n/a | p->callable, |
---|
300 | n/a | p->converters, |
---|
301 | n/a | p->flags, |
---|
302 | n/a | args); |
---|
303 | n/a | } |
---|
304 | n/a | |
---|
305 | n/a | static CThunkObject* CThunkObject_new(Py_ssize_t nArgs) |
---|
306 | n/a | { |
---|
307 | n/a | CThunkObject *p; |
---|
308 | n/a | Py_ssize_t i; |
---|
309 | n/a | |
---|
310 | n/a | p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs); |
---|
311 | n/a | if (p == NULL) { |
---|
312 | n/a | PyErr_NoMemory(); |
---|
313 | n/a | return NULL; |
---|
314 | n/a | } |
---|
315 | n/a | |
---|
316 | n/a | p->pcl_write = NULL; |
---|
317 | n/a | p->pcl_exec = NULL; |
---|
318 | n/a | memset(&p->cif, 0, sizeof(p->cif)); |
---|
319 | n/a | p->flags = 0; |
---|
320 | n/a | p->converters = NULL; |
---|
321 | n/a | p->callable = NULL; |
---|
322 | n/a | p->restype = NULL; |
---|
323 | n/a | p->setfunc = NULL; |
---|
324 | n/a | p->ffi_restype = NULL; |
---|
325 | n/a | |
---|
326 | n/a | for (i = 0; i < nArgs + 1; ++i) |
---|
327 | n/a | p->atypes[i] = NULL; |
---|
328 | n/a | PyObject_GC_Track((PyObject *)p); |
---|
329 | n/a | return p; |
---|
330 | n/a | } |
---|
331 | n/a | |
---|
332 | n/a | CThunkObject *_ctypes_alloc_callback(PyObject *callable, |
---|
333 | n/a | PyObject *converters, |
---|
334 | n/a | PyObject *restype, |
---|
335 | n/a | int flags) |
---|
336 | n/a | { |
---|
337 | n/a | int result; |
---|
338 | n/a | CThunkObject *p; |
---|
339 | n/a | Py_ssize_t nArgs, i; |
---|
340 | n/a | ffi_abi cc; |
---|
341 | n/a | |
---|
342 | n/a | nArgs = PySequence_Size(converters); |
---|
343 | n/a | p = CThunkObject_new(nArgs); |
---|
344 | n/a | if (p == NULL) |
---|
345 | n/a | return NULL; |
---|
346 | n/a | |
---|
347 | n/a | assert(CThunk_CheckExact((PyObject *)p)); |
---|
348 | n/a | |
---|
349 | n/a | p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure), |
---|
350 | n/a | &p->pcl_exec); |
---|
351 | n/a | if (p->pcl_write == NULL) { |
---|
352 | n/a | PyErr_NoMemory(); |
---|
353 | n/a | goto error; |
---|
354 | n/a | } |
---|
355 | n/a | |
---|
356 | n/a | p->flags = flags; |
---|
357 | n/a | for (i = 0; i < nArgs; ++i) { |
---|
358 | n/a | PyObject *cnv = PySequence_GetItem(converters, i); |
---|
359 | n/a | if (cnv == NULL) |
---|
360 | n/a | goto error; |
---|
361 | n/a | p->atypes[i] = _ctypes_get_ffi_type(cnv); |
---|
362 | n/a | Py_DECREF(cnv); |
---|
363 | n/a | } |
---|
364 | n/a | p->atypes[i] = NULL; |
---|
365 | n/a | |
---|
366 | n/a | Py_INCREF(restype); |
---|
367 | n/a | p->restype = restype; |
---|
368 | n/a | if (restype == Py_None) { |
---|
369 | n/a | p->setfunc = NULL; |
---|
370 | n/a | p->ffi_restype = &ffi_type_void; |
---|
371 | n/a | } else { |
---|
372 | n/a | StgDictObject *dict = PyType_stgdict(restype); |
---|
373 | n/a | if (dict == NULL || dict->setfunc == NULL) { |
---|
374 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
375 | n/a | "invalid result type for callback function"); |
---|
376 | n/a | goto error; |
---|
377 | n/a | } |
---|
378 | n/a | p->setfunc = dict->setfunc; |
---|
379 | n/a | p->ffi_restype = &dict->ffi_type_pointer; |
---|
380 | n/a | } |
---|
381 | n/a | |
---|
382 | n/a | cc = FFI_DEFAULT_ABI; |
---|
383 | n/a | #if defined(MS_WIN32) && !defined(_WIN32_WCE) && !defined(MS_WIN64) |
---|
384 | n/a | if ((flags & FUNCFLAG_CDECL) == 0) |
---|
385 | n/a | cc = FFI_STDCALL; |
---|
386 | n/a | #endif |
---|
387 | n/a | result = ffi_prep_cif(&p->cif, cc, |
---|
388 | n/a | Py_SAFE_DOWNCAST(nArgs, Py_ssize_t, int), |
---|
389 | n/a | _ctypes_get_ffi_type(restype), |
---|
390 | n/a | &p->atypes[0]); |
---|
391 | n/a | if (result != FFI_OK) { |
---|
392 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
393 | n/a | "ffi_prep_cif failed with %d", result); |
---|
394 | n/a | goto error; |
---|
395 | n/a | } |
---|
396 | n/a | #if defined(X86_DARWIN) || defined(POWERPC_DARWIN) |
---|
397 | n/a | result = ffi_prep_closure(p->pcl_write, &p->cif, closure_fcn, p); |
---|
398 | n/a | #else |
---|
399 | n/a | result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn, |
---|
400 | n/a | p, |
---|
401 | n/a | p->pcl_exec); |
---|
402 | n/a | #endif |
---|
403 | n/a | if (result != FFI_OK) { |
---|
404 | n/a | PyErr_Format(PyExc_RuntimeError, |
---|
405 | n/a | "ffi_prep_closure failed with %d", result); |
---|
406 | n/a | goto error; |
---|
407 | n/a | } |
---|
408 | n/a | |
---|
409 | n/a | Py_INCREF(converters); |
---|
410 | n/a | p->converters = converters; |
---|
411 | n/a | Py_INCREF(callable); |
---|
412 | n/a | p->callable = callable; |
---|
413 | n/a | return p; |
---|
414 | n/a | |
---|
415 | n/a | error: |
---|
416 | n/a | Py_XDECREF(p); |
---|
417 | n/a | return NULL; |
---|
418 | n/a | } |
---|
419 | n/a | |
---|
420 | n/a | #ifdef MS_WIN32 |
---|
421 | n/a | |
---|
422 | n/a | static void LoadPython(void) |
---|
423 | n/a | { |
---|
424 | n/a | if (!Py_IsInitialized()) { |
---|
425 | n/a | #ifdef WITH_THREAD |
---|
426 | n/a | PyEval_InitThreads(); |
---|
427 | n/a | #endif |
---|
428 | n/a | Py_Initialize(); |
---|
429 | n/a | } |
---|
430 | n/a | } |
---|
431 | n/a | |
---|
432 | n/a | /******************************************************************/ |
---|
433 | n/a | |
---|
434 | n/a | long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv) |
---|
435 | n/a | { |
---|
436 | n/a | PyObject *mod, *func, *result; |
---|
437 | n/a | long retval; |
---|
438 | n/a | static PyObject *context; |
---|
439 | n/a | |
---|
440 | n/a | if (context == NULL) |
---|
441 | n/a | context = PyUnicode_InternFromString("_ctypes.DllGetClassObject"); |
---|
442 | n/a | |
---|
443 | n/a | mod = PyImport_ImportModuleNoBlock("ctypes"); |
---|
444 | n/a | if (!mod) { |
---|
445 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
446 | n/a | /* There has been a warning before about this already */ |
---|
447 | n/a | return E_FAIL; |
---|
448 | n/a | } |
---|
449 | n/a | |
---|
450 | n/a | func = PyObject_GetAttrString(mod, "DllGetClassObject"); |
---|
451 | n/a | Py_DECREF(mod); |
---|
452 | n/a | if (!func) { |
---|
453 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
454 | n/a | return E_FAIL; |
---|
455 | n/a | } |
---|
456 | n/a | |
---|
457 | n/a | { |
---|
458 | n/a | PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid); |
---|
459 | n/a | PyObject *py_riid = PyLong_FromVoidPtr((void *)riid); |
---|
460 | n/a | PyObject *py_ppv = PyLong_FromVoidPtr(ppv); |
---|
461 | n/a | if (!py_rclsid || !py_riid || !py_ppv) { |
---|
462 | n/a | Py_XDECREF(py_rclsid); |
---|
463 | n/a | Py_XDECREF(py_riid); |
---|
464 | n/a | Py_XDECREF(py_ppv); |
---|
465 | n/a | Py_DECREF(func); |
---|
466 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
467 | n/a | return E_FAIL; |
---|
468 | n/a | } |
---|
469 | n/a | result = PyObject_CallFunctionObjArgs(func, |
---|
470 | n/a | py_rclsid, |
---|
471 | n/a | py_riid, |
---|
472 | n/a | py_ppv, |
---|
473 | n/a | NULL); |
---|
474 | n/a | Py_DECREF(py_rclsid); |
---|
475 | n/a | Py_DECREF(py_riid); |
---|
476 | n/a | Py_DECREF(py_ppv); |
---|
477 | n/a | } |
---|
478 | n/a | Py_DECREF(func); |
---|
479 | n/a | if (!result) { |
---|
480 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
481 | n/a | return E_FAIL; |
---|
482 | n/a | } |
---|
483 | n/a | |
---|
484 | n/a | retval = PyLong_AsLong(result); |
---|
485 | n/a | if (PyErr_Occurred()) { |
---|
486 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
487 | n/a | retval = E_FAIL; |
---|
488 | n/a | } |
---|
489 | n/a | Py_DECREF(result); |
---|
490 | n/a | return retval; |
---|
491 | n/a | } |
---|
492 | n/a | |
---|
493 | n/a | STDAPI DllGetClassObject(REFCLSID rclsid, |
---|
494 | n/a | REFIID riid, |
---|
495 | n/a | LPVOID *ppv) |
---|
496 | n/a | { |
---|
497 | n/a | long result; |
---|
498 | n/a | #ifdef WITH_THREAD |
---|
499 | n/a | PyGILState_STATE state; |
---|
500 | n/a | #endif |
---|
501 | n/a | |
---|
502 | n/a | LoadPython(); |
---|
503 | n/a | #ifdef WITH_THREAD |
---|
504 | n/a | state = PyGILState_Ensure(); |
---|
505 | n/a | #endif |
---|
506 | n/a | result = Call_GetClassObject(rclsid, riid, ppv); |
---|
507 | n/a | #ifdef WITH_THREAD |
---|
508 | n/a | PyGILState_Release(state); |
---|
509 | n/a | #endif |
---|
510 | n/a | return result; |
---|
511 | n/a | } |
---|
512 | n/a | |
---|
513 | n/a | long Call_CanUnloadNow(void) |
---|
514 | n/a | { |
---|
515 | n/a | PyObject *mod, *func, *result; |
---|
516 | n/a | long retval; |
---|
517 | n/a | static PyObject *context; |
---|
518 | n/a | |
---|
519 | n/a | if (context == NULL) |
---|
520 | n/a | context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow"); |
---|
521 | n/a | |
---|
522 | n/a | mod = PyImport_ImportModuleNoBlock("ctypes"); |
---|
523 | n/a | if (!mod) { |
---|
524 | n/a | /* OutputDebugString("Could not import ctypes"); */ |
---|
525 | n/a | /* We assume that this error can only occur when shutting |
---|
526 | n/a | down, so we silently ignore it */ |
---|
527 | n/a | PyErr_Clear(); |
---|
528 | n/a | return E_FAIL; |
---|
529 | n/a | } |
---|
530 | n/a | /* Other errors cannot be raised, but are printed to stderr */ |
---|
531 | n/a | func = PyObject_GetAttrString(mod, "DllCanUnloadNow"); |
---|
532 | n/a | Py_DECREF(mod); |
---|
533 | n/a | if (!func) { |
---|
534 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
535 | n/a | return E_FAIL; |
---|
536 | n/a | } |
---|
537 | n/a | |
---|
538 | n/a | result = _PyObject_CallNoArg(func); |
---|
539 | n/a | Py_DECREF(func); |
---|
540 | n/a | if (!result) { |
---|
541 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
542 | n/a | return E_FAIL; |
---|
543 | n/a | } |
---|
544 | n/a | |
---|
545 | n/a | retval = PyLong_AsLong(result); |
---|
546 | n/a | if (PyErr_Occurred()) { |
---|
547 | n/a | PyErr_WriteUnraisable(context ? context : Py_None); |
---|
548 | n/a | retval = E_FAIL; |
---|
549 | n/a | } |
---|
550 | n/a | Py_DECREF(result); |
---|
551 | n/a | return retval; |
---|
552 | n/a | } |
---|
553 | n/a | |
---|
554 | n/a | /* |
---|
555 | n/a | DllRegisterServer and DllUnregisterServer still missing |
---|
556 | n/a | */ |
---|
557 | n/a | |
---|
558 | n/a | STDAPI DllCanUnloadNow(void) |
---|
559 | n/a | { |
---|
560 | n/a | long result; |
---|
561 | n/a | #ifdef WITH_THREAD |
---|
562 | n/a | PyGILState_STATE state = PyGILState_Ensure(); |
---|
563 | n/a | #endif |
---|
564 | n/a | result = Call_CanUnloadNow(); |
---|
565 | n/a | #ifdef WITH_THREAD |
---|
566 | n/a | PyGILState_Release(state); |
---|
567 | n/a | #endif |
---|
568 | n/a | return result; |
---|
569 | n/a | } |
---|
570 | n/a | |
---|
571 | n/a | #ifndef Py_NO_ENABLE_SHARED |
---|
572 | n/a | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvRes) |
---|
573 | n/a | { |
---|
574 | n/a | switch(fdwReason) { |
---|
575 | n/a | case DLL_PROCESS_ATTACH: |
---|
576 | n/a | DisableThreadLibraryCalls(hinstDLL); |
---|
577 | n/a | break; |
---|
578 | n/a | } |
---|
579 | n/a | return TRUE; |
---|
580 | n/a | } |
---|
581 | n/a | #endif |
---|
582 | n/a | |
---|
583 | n/a | #endif |
---|
584 | n/a | |
---|
585 | n/a | /* |
---|
586 | n/a | Local Variables: |
---|
587 | n/a | compile-command: "cd .. && python setup.py -q build_ext" |
---|
588 | n/a | End: |
---|
589 | n/a | */ |
---|