Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2 | #include "frameobject.h" |
| 3 | |
| 4 | #include <ffi.h> |
| 5 | #ifdef MS_WIN32 |
| 6 | #include <windows.h> |
| 7 | #endif |
| 8 | #include "ctypes.h" |
| 9 | |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 10 | /**************************************************************/ |
| 11 | |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 12 | static void |
| 13 | CThunkObject_dealloc(PyObject *_self) |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 14 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | CThunkObject *self = (CThunkObject *)_self; |
| 16 | Py_XDECREF(self->converters); |
| 17 | Py_XDECREF(self->callable); |
| 18 | Py_XDECREF(self->restype); |
Thomas Heller | 864cc67 | 2010-08-08 17:58:53 +0000 | [diff] [blame] | 19 | if (self->pcl_write) |
| 20 | ffi_closure_free(self->pcl_write); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 21 | PyObject_GC_Del(self); |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | static int |
| 25 | CThunkObject_traverse(PyObject *_self, visitproc visit, void *arg) |
| 26 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 27 | CThunkObject *self = (CThunkObject *)_self; |
| 28 | Py_VISIT(self->converters); |
| 29 | Py_VISIT(self->callable); |
| 30 | Py_VISIT(self->restype); |
| 31 | return 0; |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static int |
| 35 | CThunkObject_clear(PyObject *_self) |
| 36 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 37 | CThunkObject *self = (CThunkObject *)_self; |
| 38 | Py_CLEAR(self->converters); |
| 39 | Py_CLEAR(self->callable); |
| 40 | Py_CLEAR(self->restype); |
| 41 | return 0; |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 44 | PyTypeObject PyCThunk_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | 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 Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /**************************************************************/ |
| 77 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 78 | static void |
| 79 | PrintError(char *msg, ...) |
| 80 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | char buf[512]; |
| 82 | PyObject *f = PySys_GetObject("stderr"); |
| 83 | va_list marker; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 84 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 85 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
| 94 | /* after code that pyrex generates */ |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 95 | void _ctypes_add_traceback(char *funcname, char *filename, int lineno) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 96 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 97 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 114 | bad: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | Py_XDECREF(py_globals); |
| 116 | Py_XDECREF(py_code); |
| 117 | Py_XDECREF(py_frame); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 118 | } |
| 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 | */ |
| 131 | static void |
| 132 | TryAddRef(StgDictObject *dict, CDataObject *obj) |
| 133 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | IUnknown *punk; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | if (NULL == PyDict_GetItemString((PyObject *)dict, "_needs_com_addref_")) |
| 137 | return; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | punk = *(IUnknown **)obj->b_ptr; |
| 140 | if (punk) |
| 141 | punk->lpVtbl->AddRef(punk); |
| 142 | return; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 143 | } |
| 144 | #endif |
| 145 | |
| 146 | /****************************************************************************** |
| 147 | * |
| 148 | * Call the python object with all arguments |
| 149 | * |
| 150 | */ |
| 151 | static void _CallPythonObject(void *mem, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 152 | ffi_type *restype, |
| 153 | SETFUNC setfunc, |
| 154 | PyObject *callable, |
| 155 | PyObject *converters, |
| 156 | int flags, |
| 157 | void **pArgs) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 158 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | 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 Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 165 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | PyGILState_STATE state = PyGILState_Ensure(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 167 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 169 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 193 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 194 | 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 Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 205 | BTW, the same problem occurs when they are pushed as parameters |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 206 | */ |
| 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 223 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | TryAddRef(dict, obj); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 225 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | } 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 237 | |
| 238 | #define CHECK(what, x) \ |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 239 | if (x == NULL) _ctypes_add_traceback(what, "_ctypes/callbacks.c", __LINE__ - 1), PyErr_Print() |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | 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 Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 250 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 251 | if (flags & FUNCFLAG_USE_LASTERROR) { |
| 252 | int temp = space[1]; |
| 253 | space[1] = GetLastError(); |
| 254 | SetLastError(temp); |
| 255 | } |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 256 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 257 | } |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 258 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | result = PyObject_CallObject(callable, arglist); |
| 260 | CHECK("'calling callback function'", result); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 261 | |
| 262 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | if (flags & FUNCFLAG_USE_LASTERROR) { |
| 264 | int temp = space[1]; |
| 265 | space[1] = GetLastError(); |
| 266 | SetLastError(temp); |
| 267 | } |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 268 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 269 | if (flags & FUNCFLAG_USE_ERRNO) { |
| 270 | int temp = space[0]; |
| 271 | space[0] = errno; |
| 272 | errno = temp; |
| 273 | } |
| 274 | Py_XDECREF(error_object); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 275 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 276 | if ((restype != &ffi_type_void) && result) { |
| 277 | PyObject *keep; |
| 278 | assert(setfunc); |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 279 | #ifdef WORDS_BIGENDIAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 280 | /* 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 283 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 290 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 307 | Done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | Py_XDECREF(arglist); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 309 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 310 | PyGILState_Release(state); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 311 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 314 | static void closure_fcn(ffi_cif *cif, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | void *resp, |
| 316 | void **args, |
| 317 | void *userdata) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 318 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | CThunkObject *p = (CThunkObject *)userdata; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | _CallPythonObject(resp, |
| 322 | p->ffi_restype, |
| 323 | p->setfunc, |
| 324 | p->callable, |
| 325 | p->converters, |
| 326 | p->flags, |
| 327 | args); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 330 | static CThunkObject* CThunkObject_new(Py_ssize_t nArgs) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | CThunkObject *p; |
| 333 | int i; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 334 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs); |
| 336 | if (p == NULL) { |
| 337 | PyErr_NoMemory(); |
| 338 | return NULL; |
| 339 | } |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 340 | |
Thomas Heller | 864cc67 | 2010-08-08 17:58:53 +0000 | [diff] [blame] | 341 | p->pcl_exec = NULL; |
| 342 | p->pcl_write = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 343 | 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 Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 355 | CThunkObject *_ctypes_alloc_callback(PyObject *callable, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 356 | PyObject *converters, |
| 357 | PyObject *restype, |
| 358 | int flags) |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 359 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | int result; |
| 361 | CThunkObject *p; |
| 362 | Py_ssize_t nArgs, i; |
| 363 | ffi_abi cc; |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 365 | nArgs = PySequence_Size(converters); |
| 366 | p = CThunkObject_new(nArgs); |
| 367 | if (p == NULL) |
| 368 | return NULL; |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | assert(CThunk_CheckExact((PyObject *)p)); |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 371 | |
Thomas Heller | 864cc67 | 2010-08-08 17:58:53 +0000 | [diff] [blame] | 372 | p->pcl_write = ffi_closure_alloc(sizeof(ffi_closure), |
| 373 | &p->pcl_exec); |
| 374 | if (p->pcl_write == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | PyErr_NoMemory(); |
| 376 | goto error; |
| 377 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 379 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 388 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 389 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 404 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 405 | cc = FFI_DEFAULT_ABI; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 406 | #if defined(MS_WIN32) && !defined(_WIN32_WCE) && !defined(MS_WIN64) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 407 | if ((flags & FUNCFLAG_CDECL) == 0) |
| 408 | cc = FFI_STDCALL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 409 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 410 | 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 Oussoren | 2decf22 | 2010-09-05 18:25:59 +0000 | [diff] [blame] | 419 | #if defined(X86_DARWIN) || defined(POWERPC_DARWIN) |
| 420 | result = ffi_prep_closure(p->pcl_write, &p->cif, closure_fcn, p); |
| 421 | #else |
Thomas Heller | 864cc67 | 2010-08-08 17:58:53 +0000 | [diff] [blame] | 422 | result = ffi_prep_closure_loc(p->pcl_write, &p->cif, closure_fcn, |
| 423 | p, |
| 424 | p->pcl_exec); |
Ronald Oussoren | 2decf22 | 2010-09-05 18:25:59 +0000 | [diff] [blame] | 425 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 426 | if (result != FFI_OK) { |
| 427 | PyErr_Format(PyExc_RuntimeError, |
| 428 | "ffi_prep_closure failed with %d", result); |
| 429 | goto error; |
| 430 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | Py_INCREF(converters); |
| 433 | p->converters = converters; |
| 434 | Py_INCREF(callable); |
| 435 | p->callable = callable; |
| 436 | return p; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 437 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 438 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | Py_XDECREF(p); |
| 440 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 443 | #ifdef MS_WIN32 |
| 444 | |
| 445 | static void LoadPython(void) |
| 446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | if (!Py_IsInitialized()) { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 448 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | PyEval_InitThreads(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 450 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | Py_Initialize(); |
| 452 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /******************************************************************/ |
| 456 | |
| 457 | long Call_GetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv) |
| 458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | PyObject *mod, *func, *result; |
| 460 | long retval; |
| 461 | static PyObject *context; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | if (context == NULL) |
| 464 | context = PyUnicode_InternFromString("_ctypes.DllGetClassObject"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 465 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 466 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 472 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 473 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | { |
| 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 507 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | STDAPI DllGetClassObject(REFCLSID rclsid, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | REFIID riid, |
| 518 | LPVOID *ppv) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 520 | long result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 521 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 522 | PyGILState_STATE state; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 523 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | LoadPython(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 526 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 527 | state = PyGILState_Ensure(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 528 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 529 | result = Call_GetClassObject(rclsid, riid, ppv); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 530 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 531 | PyGILState_Release(state); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 532 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 533 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | long Call_CanUnloadNow(void) |
| 537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 538 | PyObject *mod, *func, *result; |
| 539 | long retval; |
| 540 | static PyObject *context; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 541 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 542 | if (context == NULL) |
| 543 | context = PyUnicode_InternFromString("_ctypes.DllCanUnloadNow"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 544 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 545 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 560 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 567 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 568 | 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 Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | DllRegisterServer and DllUnregisterServer still missing |
| 579 | */ |
| 580 | |
| 581 | STDAPI DllCanUnloadNow(void) |
| 582 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 583 | long result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 584 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 585 | PyGILState_STATE state = PyGILState_Ensure(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 586 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | result = Call_CanUnloadNow(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 588 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | PyGILState_Release(state); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 590 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 591 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | #ifndef Py_NO_ENABLE_SHARED |
| 595 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvRes) |
| 596 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 597 | switch(fdwReason) { |
| 598 | case DLL_PROCESS_ATTACH: |
| 599 | DisableThreadLibraryCalls(hinstDLL); |
| 600 | break; |
| 601 | } |
| 602 | return TRUE; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 603 | } |
| 604 | #endif |
| 605 | |
| 606 | #endif |
| 607 | |
| 608 | /* |
| 609 | Local Variables: |
| 610 | compile-command: "cd .. && python setup.py -q build_ext" |
| 611 | End: |
| 612 | */ |