blob: d23509fbe2c5c2b4cdf23a555351f6a3053c5fde [file] [log] [blame]
Thomas Hellerd4c93202006-03-08 19:35:11 +00001#include "Python.h"
Thomas Hellerd4c93202006-03-08 19:35:11 +00002#include "frameobject.h"
3
4#include <ffi.h>
5#ifdef MS_WIN32
6#include <windows.h>
7#endif
8#include "ctypes.h"
9
Thomas Hellere106dc32008-04-24 18:39:36 +000010/**************************************************************/
11
Thomas Hellerb041fda2008-04-30 17:11:46 +000012static void
13CThunkObject_dealloc(PyObject *_self)
Thomas Hellere106dc32008-04-24 18:39:36 +000014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000015 CThunkObject *self = (CThunkObject *)_self;
16 Py_XDECREF(self->converters);
17 Py_XDECREF(self->callable);
18 Py_XDECREF(self->restype);
Thomas Heller864cc672010-08-08 17:58:53 +000019 if (self->pcl_write)
20 ffi_closure_free(self->pcl_write);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000021 PyObject_GC_Del(self);
Thomas Hellere106dc32008-04-24 18:39:36 +000022}
23
24static int
25CThunkObject_traverse(PyObject *_self, visitproc visit, void *arg)
26{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000027 CThunkObject *self = (CThunkObject *)_self;
28 Py_VISIT(self->converters);
29 Py_VISIT(self->callable);
30 Py_VISIT(self->restype);
31 return 0;
Thomas Hellere106dc32008-04-24 18:39:36 +000032}
33
34static int
35CThunkObject_clear(PyObject *_self)
36{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 CThunkObject *self = (CThunkObject *)_self;
38 Py_CLEAR(self->converters);
39 Py_CLEAR(self->callable);
40 Py_CLEAR(self->restype);
41 return 0;
Thomas Hellere106dc32008-04-24 18:39:36 +000042}
43
Thomas Heller34596a92009-04-24 20:50:00 +000044PyTypeObject PyCThunk_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 PyVarObject_HEAD_INIT(NULL, 0)
46 "_ctypes.CThunkObject",
47 sizeof(CThunkObject), /* tp_basicsize */
48 sizeof(ffi_type), /* tp_itemsize */
49 CThunkObject_dealloc, /* tp_dealloc */
50 0, /* tp_print */
51 0, /* tp_getattr */
52 0, /* tp_setattr */
53 0, /* tp_reserved */
54 0, /* tp_repr */
55 0, /* tp_as_number */
56 0, /* tp_as_sequence */
57 0, /* tp_as_mapping */
58 0, /* tp_hash */
59 0, /* tp_call */
60 0, /* tp_str */
61 0, /* tp_getattro */
62 0, /* tp_setattro */
63 0, /* tp_as_buffer */
64 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
65 "CThunkObject", /* tp_doc */
66 CThunkObject_traverse, /* tp_traverse */
67 CThunkObject_clear, /* tp_clear */
68 0, /* tp_richcompare */
69 0, /* tp_weaklistoffset */
70 0, /* tp_iter */
71 0, /* tp_iternext */
72 0, /* tp_methods */
73 0, /* tp_members */
Thomas Hellere106dc32008-04-24 18:39:36 +000074};
75
76/**************************************************************/
77
Thomas Hellerd4c93202006-03-08 19:35:11 +000078static void
79PrintError(char *msg, ...)
80{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 char buf[512];
82 PyObject *f = PySys_GetObject("stderr");
83 va_list marker;
Thomas Hellerd4c93202006-03-08 19:35:11 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 va_start(marker, msg);
86 vsnprintf(buf, sizeof(buf), msg, marker);
87 va_end(marker);
88 if (f != NULL && f != Py_None)
89 PyFile_WriteString(buf, f);
90 PyErr_Print();
Thomas Hellerd4c93202006-03-08 19:35:11 +000091}
92
93
94/* after code that pyrex generates */
Thomas Heller34596a92009-04-24 20:50:00 +000095void _ctypes_add_traceback(char *funcname, char *filename, int lineno)
Thomas Hellerd4c93202006-03-08 19:35:11 +000096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 PyObject *py_globals = 0;
98 PyCodeObject *py_code = 0;
99 PyFrameObject *py_frame = 0;
100
101 py_globals = PyDict_New();
102 if (!py_globals) goto bad;
103 py_code = PyCode_NewEmpty(filename, funcname, lineno);
104 if (!py_code) goto bad;
105 py_frame = PyFrame_New(
106 PyThreadState_Get(), /*PyThreadState *tstate,*/
107 py_code, /*PyCodeObject *code,*/
108 py_globals, /*PyObject *globals,*/
109 0 /*PyObject *locals*/
110 );
111 if (!py_frame) goto bad;
112 py_frame->f_lineno = lineno;
113 PyTraceBack_Here(py_frame);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000114 bad:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 Py_XDECREF(py_globals);
116 Py_XDECREF(py_code);
117 Py_XDECREF(py_frame);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000118}
119
120#ifdef MS_WIN32
121/*
122 * We must call AddRef() on non-NULL COM pointers we receive as arguments
123 * to callback functions - these functions are COM method implementations.
124 * The Python instances we create have a __del__ method which calls Release().
125 *
126 * The presence of a class attribute named '_needs_com_addref_' triggers this
127 * behaviour. It would also be possible to call the AddRef() Python method,
128 * after checking for PyObject_IsTrue(), but this would probably be somewhat
129 * slower.
130 */
131static void
132TryAddRef(StgDictObject *dict, CDataObject *obj)
133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 IUnknown *punk;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_"))
137 return;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 punk = *(IUnknown **)obj->b_ptr;
140 if (punk)
141 punk->lpVtbl->AddRef(punk);
142 return;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000143}
144#endif
145
146/******************************************************************************
147 *
148 * Call the python object with all arguments
149 *
150 */
151static void _CallPythonObject(void *mem,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 ffi_type *restype,
153 SETFUNC setfunc,
154 PyObject *callable,
155 PyObject *converters,
156 int flags,
157 void **pArgs)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 Py_ssize_t i;
160 PyObject *result;
161 PyObject *arglist = NULL;
162 Py_ssize_t nArgs;
163 PyObject *error_object = NULL;
164 int *space;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000165#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000166 PyGILState_STATE state = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000167#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 nArgs = PySequence_Length(converters);
170 /* Hm. What to return in case of error?
171 For COM, 0xFFFFFFFF seems better than 0.
172 */
173 if (nArgs < 0) {
174 PrintError("BUG: PySequence_Length");
175 goto Done;
176 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000178 arglist = PyTuple_New(nArgs);
179 if (!arglist) {
180 PrintError("PyTuple_New()");
181 goto Done;
182 }
183 for (i = 0; i < nArgs; ++i) {
184 /* Note: new reference! */
185 PyObject *cnv = PySequence_GetItem(converters, i);
186 StgDictObject *dict;
187 if (cnv)
188 dict = PyType_stgdict(cnv);
189 else {
190 PrintError("Getting argument converter %d\n", i);
191 goto Done;
192 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 if (dict && dict->getfunc && !_ctypes_simple_instance(cnv)) {
195 PyObject *v = dict->getfunc(*pArgs, dict->size);
196 if (!v) {
197 PrintError("create argument %d:\n", i);
198 Py_DECREF(cnv);
199 goto Done;
200 }
201 PyTuple_SET_ITEM(arglist, i, v);
202 /* XXX XXX XX
203 We have the problem that c_byte or c_short have dict->size of
204 1 resp. 4, but these parameters are pushed as sizeof(int) bytes.
Ezio Melotti13925002011-03-16 11:05:33 +0200205 BTW, the same problem occurs when they are pushed as parameters
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 */
207 } else if (dict) {
208 /* Hm, shouldn't we use PyCData_AtAddress() or something like that instead? */
209 CDataObject *obj = (CDataObject *)PyObject_CallFunctionObjArgs(cnv, NULL);
210 if (!obj) {
211 PrintError("create argument %d:\n", i);
212 Py_DECREF(cnv);
213 goto Done;
214 }
215 if (!CDataObject_Check(obj)) {
216 Py_DECREF(obj);
217 Py_DECREF(cnv);
218 PrintError("unexpected result of create argument %d:\n", i);
219 goto Done;
220 }
221 memcpy(obj->b_ptr, *pArgs, dict->size);
222 PyTuple_SET_ITEM(arglist, i, (PyObject *)obj);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000223#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 TryAddRef(dict, obj);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000225#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 } else {
227 PyErr_SetString(PyExc_TypeError,
228 "cannot build parameter");
229 PrintError("Parsing argument %d\n", i);
230 Py_DECREF(cnv);
231 goto Done;
232 }
233 Py_DECREF(cnv);
234 /* XXX error handling! */
235 pArgs++;
236 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000237
238#define CHECK(what, x) \
Thomas Heller34596a92009-04-24 20:50:00 +0000239if (x == NULL) _ctypes_add_traceback(what, "_ctypes/callbacks.c", __LINE__ - 1), PyErr_Print()
Thomas Hellerd4c93202006-03-08 19:35:11 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) {
242 error_object = _ctypes_get_errobj(&space);
243 if (error_object == NULL)
244 goto Done;
245 if (flags & FUNCFLAG_USE_ERRNO) {
246 int temp = space[0];
247 space[0] = errno;
248 errno = temp;
249 }
Thomas Heller9cac7b62008-06-06 09:31:40 +0000250#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 if (flags & FUNCFLAG_USE_LASTERROR) {
252 int temp = space[1];
253 space[1] = GetLastError();
254 SetLastError(temp);
255 }
Thomas Heller9cac7b62008-06-06 09:31:40 +0000256#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 }
Thomas Heller9cac7b62008-06-06 09:31:40 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 result = PyObject_CallObject(callable, arglist);
260 CHECK("'calling callback function'", result);
Thomas Heller9cac7b62008-06-06 09:31:40 +0000261
262#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 if (flags & FUNCFLAG_USE_LASTERROR) {
264 int temp = space[1];
265 space[1] = GetLastError();
266 SetLastError(temp);
267 }
Thomas Heller9cac7b62008-06-06 09:31:40 +0000268#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 if (flags & FUNCFLAG_USE_ERRNO) {
270 int temp = space[0];
271 space[0] = errno;
272 errno = temp;
273 }
274 Py_XDECREF(error_object);
Thomas Heller9cac7b62008-06-06 09:31:40 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 if ((restype != &ffi_type_void) && result) {
277 PyObject *keep;
278 assert(setfunc);
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000279#ifdef WORDS_BIGENDIAN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 /* See the corresponding code in callproc.c, around line 961 */
281 if (restype->type != FFI_TYPE_FLOAT && restype->size < sizeof(ffi_arg))
282 mem = (char *)mem + sizeof(ffi_arg) - restype->size;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000283#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 keep = setfunc(mem, result, 0);
285 CHECK("'converting callback result'", keep);
286 /* keep is an object we have to keep alive so that the result
287 stays valid. If there is no such object, the setfunc will
288 have returned Py_None.
Thomas Hellerd4c93202006-03-08 19:35:11 +0000289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 If there is such an object, we have no choice than to keep
291 it alive forever - but a refcount and/or memory leak will
292 be the result. EXCEPT when restype is py_object - Python
293 itself knows how to manage the refcount of these objects.
294 */
295 if (keep == NULL) /* Could not convert callback result. */
296 PyErr_WriteUnraisable(callable);
297 else if (keep == Py_None) /* Nothing to keep */
298 Py_DECREF(keep);
299 else if (setfunc != _ctypes_get_fielddesc("O")->setfunc) {
300 if (-1 == PyErr_WarnEx(PyExc_RuntimeWarning,
301 "memory leak in callback function.",
302 1))
303 PyErr_WriteUnraisable(callable);
304 }
305 }
306 Py_XDECREF(result);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000307 Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 Py_XDECREF(arglist);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000309#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 PyGILState_Release(state);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000311#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000312}
313
Thomas Hellerd4c93202006-03-08 19:35:11 +0000314static void closure_fcn(ffi_cif *cif,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 void *resp,
316 void **args,
317 void *userdata)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 CThunkObject *p = (CThunkObject *)userdata;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 _CallPythonObject(resp,
322 p->ffi_restype,
323 p->setfunc,
324 p->callable,
325 p->converters,
326 p->flags,
327 args);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000328}
329
Thomas Hellere106dc32008-04-24 18:39:36 +0000330static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 CThunkObject *p;
333 int i;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);
336 if (p == NULL) {
337 PyErr_NoMemory();
338 return NULL;
339 }
Thomas Hellere106dc32008-04-24 18:39:36 +0000340
Thomas Heller864cc672010-08-08 17:58:53 +0000341 p->pcl_exec = NULL;
342 p->pcl_write = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 memset(&p->cif, 0, sizeof(p->cif));
344 p->converters = NULL;
345 p->callable = NULL;
346 p->setfunc = NULL;
347 p->ffi_restype = NULL;
348
349 for (i = 0; i < nArgs + 1; ++i)
350 p->atypes[i] = NULL;
351 PyObject_GC_Track((PyObject *)p);
352 return p;
Thomas Hellere106dc32008-04-24 18:39:36 +0000353}
354
Thomas Heller34596a92009-04-24 20:50:00 +0000355CThunkObject *_ctypes_alloc_callback(PyObject *callable,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 PyObject *converters,
357 PyObject *restype,
358 int flags)
Thomas Hellere106dc32008-04-24 18:39:36 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 int result;
361 CThunkObject *p;
362 Py_ssize_t nArgs, i;
363 ffi_abi cc;
Thomas Hellere106dc32008-04-24 18:39:36 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 nArgs = PySequence_Size(converters);
366 p = CThunkObject_new(nArgs);
367 if (p == NULL)
368 return NULL;
Thomas Hellere106dc32008-04-24 18:39:36 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 assert(CThunk_CheckExact((PyObject *)p));
Thomas Hellere106dc32008-04-24 18:39:36 +0000371
Thomas Heller864cc672010-08-08 17:58:53 +0000372 p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure),
373 &p->pcl_exec);
374 if (p->pcl_write == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyErr_NoMemory();
376 goto error;
377 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 p->flags = flags;
380 for (i = 0; i < nArgs; ++i) {
381 PyObject *cnv = PySequence_GetItem(converters, i);
382 if (cnv == NULL)
383 goto error;
384 p->atypes[i] = _ctypes_get_ffi_type(cnv);
385 Py_DECREF(cnv);
386 }
387 p->atypes[i] = NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 Py_INCREF(restype);
390 p->restype = restype;
391 if (restype == Py_None) {
392 p->setfunc = NULL;
393 p->ffi_restype = &ffi_type_void;
394 } else {
395 StgDictObject *dict = PyType_stgdict(restype);
396 if (dict == NULL || dict->setfunc == NULL) {
397 PyErr_SetString(PyExc_TypeError,
398 "invalid result type for callback function");
399 goto error;
400 }
401 p->setfunc = dict->setfunc;
402 p->ffi_restype = &dict->ffi_type_pointer;
403 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 cc = FFI_DEFAULT_ABI;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000406#if defined(MS_WIN32) && !defined(_WIN32_WCE) && !defined(MS_WIN64)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 if ((flags & FUNCFLAG_CDECL) == 0)
408 cc = FFI_STDCALL;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000409#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 result = ffi_prep_cif(&p->cif, cc,
411 Py_SAFE_DOWNCAST(nArgs, Py_ssize_t, int),
412 _ctypes_get_ffi_type(restype),
413 &p->atypes[0]);
414 if (result != FFI_OK) {
415 PyErr_Format(PyExc_RuntimeError,
416 "ffi_prep_cif failed with %d", result);
417 goto error;
418 }
Ronald Oussoren2decf222010-09-05 18:25:59 +0000419#if defined(X86_DARWIN) || defined(POWERPC_DARWIN)
420 result = ffi_prep_closure(p->pcl_write, &p->cif, closure_fcn, p);
421#else
Thomas Heller864cc672010-08-08 17:58:53 +0000422 result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn,
423 p,
424 p->pcl_exec);
Ronald Oussoren2decf222010-09-05 18:25:59 +0000425#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (result != FFI_OK) {
427 PyErr_Format(PyExc_RuntimeError,
428 "ffi_prep_closure failed with %d", result);
429 goto error;
430 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 Py_INCREF(converters);
433 p->converters = converters;
434 Py_INCREF(callable);
435 p->callable = callable;
436 return p;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000437
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000438 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 Py_XDECREF(p);
440 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000441}
442
Thomas Hellerd4c93202006-03-08 19:35:11 +0000443#ifdef MS_WIN32
444
445static void LoadPython(void)
446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (!Py_IsInitialized()) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000448#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 PyEval_InitThreads();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000450#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 Py_Initialize();
452 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000453}
454
455/******************************************************************/
456
457long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
458{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 PyObject *mod, *func, *result;
460 long retval;
461 static PyObject *context;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 if (context == NULL)
464 context = PyUnicode_InternFromString("_ctypes.DllGetClassObject");
Thomas Hellerd4c93202006-03-08 19:35:11 +0000465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000466 mod = PyImport_ImportModuleNoBlock("ctypes");
467 if (!mod) {
468 PyErr_WriteUnraisable(context ? context : Py_None);
469 /* There has been a warning before about this already */
470 return E_FAIL;
471 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 func = PyObject_GetAttrString(mod, "DllGetClassObject");
474 Py_DECREF(mod);
475 if (!func) {
476 PyErr_WriteUnraisable(context ? context : Py_None);
477 return E_FAIL;
478 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 {
481 PyObject *py_rclsid = PyLong_FromVoidPtr((void *)rclsid);
482 PyObject *py_riid = PyLong_FromVoidPtr((void *)riid);
483 PyObject *py_ppv = PyLong_FromVoidPtr(ppv);
484 if (!py_rclsid || !py_riid || !py_ppv) {
485 Py_XDECREF(py_rclsid);
486 Py_XDECREF(py_riid);
487 Py_XDECREF(py_ppv);
488 Py_DECREF(func);
489 PyErr_WriteUnraisable(context ? context : Py_None);
490 return E_FAIL;
491 }
492 result = PyObject_CallFunctionObjArgs(func,
493 py_rclsid,
494 py_riid,
495 py_ppv,
496 NULL);
497 Py_DECREF(py_rclsid);
498 Py_DECREF(py_riid);
499 Py_DECREF(py_ppv);
500 }
501 Py_DECREF(func);
502 if (!result) {
503 PyErr_WriteUnraisable(context ? context : Py_None);
504 return E_FAIL;
505 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 retval = PyLong_AsLong(result);
508 if (PyErr_Occurred()) {
509 PyErr_WriteUnraisable(context ? context : Py_None);
510 retval = E_FAIL;
511 }
512 Py_DECREF(result);
513 return retval;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000514}
515
516STDAPI DllGetClassObject(REFCLSID rclsid,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 REFIID riid,
518 LPVOID *ppv)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 long result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000521#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 PyGILState_STATE state;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000523#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 LoadPython();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000526#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 state = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000528#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 result = Call_GetClassObject(rclsid, riid, ppv);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000530#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 PyGILState_Release(state);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000532#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000533 return result;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000534}
535
536long Call_CanUnloadNow(void)
537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000538 PyObject *mod, *func, *result;
539 long retval;
540 static PyObject *context;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 if (context == NULL)
543 context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow");
Thomas Hellerd4c93202006-03-08 19:35:11 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 mod = PyImport_ImportModuleNoBlock("ctypes");
546 if (!mod) {
547/* OutputDebugString("Could not import ctypes"); */
548 /* We assume that this error can only occur when shutting
549 down, so we silently ignore it */
550 PyErr_Clear();
551 return E_FAIL;
552 }
553 /* Other errors cannot be raised, but are printed to stderr */
554 func = PyObject_GetAttrString(mod, "DllCanUnloadNow");
555 Py_DECREF(mod);
556 if (!func) {
557 PyErr_WriteUnraisable(context ? context : Py_None);
558 return E_FAIL;
559 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 result = PyObject_CallFunction(func, NULL);
562 Py_DECREF(func);
563 if (!result) {
564 PyErr_WriteUnraisable(context ? context : Py_None);
565 return E_FAIL;
566 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 retval = PyLong_AsLong(result);
569 if (PyErr_Occurred()) {
570 PyErr_WriteUnraisable(context ? context : Py_None);
571 retval = E_FAIL;
572 }
573 Py_DECREF(result);
574 return retval;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000575}
576
577/*
578 DllRegisterServer and DllUnregisterServer still missing
579*/
580
581STDAPI DllCanUnloadNow(void)
582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 long result;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000584#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000585 PyGILState_STATE state = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000586#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 result = Call_CanUnloadNow();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000588#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 PyGILState_Release(state);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000590#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return result;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000592}
593
594#ifndef Py_NO_ENABLE_SHARED
595BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvRes)
596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 switch(fdwReason) {
598 case DLL_PROCESS_ATTACH:
599 DisableThreadLibraryCalls(hinstDLL);
600 break;
601 }
602 return TRUE;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000603}
604#endif
605
606#endif
607
608/*
609 Local Variables:
610 compile-command: "cd .. && python setup.py -q build_ext"
611 End:
612*/