Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | * History: First version dated from 3/97, derived from my SCMLIB version |
| 3 | * for win16. |
| 4 | */ |
| 5 | /* |
| 6 | * Related Work: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 7 | * - calldll http://www.nightmare.com/software.html |
| 8 | * - libffi http://sourceware.cygnus.com/libffi/ |
| 9 | * - ffcall http://clisp.cons.org/~haible/packages-ffcall.html |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 10 | * and, of course, Don Beaudry's MESS package, but this is more ctypes |
| 11 | * related. |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | /* |
| 16 | How are functions called, and how are parameters converted to C ? |
| 17 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 18 | 1. _ctypes.c::PyCFuncPtr_call receives an argument tuple 'inargs' and a |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 19 | keyword dictionary 'kwds'. |
| 20 | |
| 21 | 2. After several checks, _build_callargs() is called which returns another |
| 22 | tuple 'callargs'. This may be the same tuple as 'inargs', a slice of |
Terry Jan Reedy | 0158af3 | 2013-03-11 17:42:46 -0400 | [diff] [blame] | 23 | 'inargs', or a completely fresh tuple, depending on several things (is it a |
| 24 | COM method?, are 'paramflags' available?). |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 25 | |
| 26 | 3. _build_callargs also calculates bitarrays containing indexes into |
| 27 | the callargs tuple, specifying how to build the return value(s) of |
| 28 | the function. |
| 29 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 30 | 4. _ctypes_callproc is then called with the 'callargs' tuple. _ctypes_callproc first |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 31 | allocates two arrays. The first is an array of 'struct argument' items, the |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 32 | second array has 'void *' entries. |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 33 | |
| 34 | 5. If 'converters' are present (converters is a sequence of argtypes' |
| 35 | from_param methods), for each item in 'callargs' converter is called and the |
| 36 | result passed to ConvParam. If 'converters' are not present, each argument |
| 37 | is directly passed to ConvParm. |
| 38 | |
| 39 | 6. For each arg, ConvParam stores the contained C data (or a pointer to it, |
| 40 | for structures) into the 'struct argument' array. |
| 41 | |
| 42 | 7. Finally, a loop fills the 'void *' array so that each item points to the |
| 43 | data contained in or pointed to by the 'struct argument' array. |
| 44 | |
| 45 | 8. The 'void *' argument array is what _call_function_pointer |
| 46 | expects. _call_function_pointer then has very little to do - only some |
| 47 | libffi specific stuff, then it calls ffi_call. |
| 48 | |
| 49 | So, there are 4 data structures holding processed arguments: |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 50 | - the inargs tuple (in PyCFuncPtr_call) |
| 51 | - the callargs tuple (in PyCFuncPtr_call) |
Ezio Melotti | 42da663 | 2011-03-15 05:18:48 +0200 | [diff] [blame] | 52 | - the 'struct arguments' array |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 53 | - the 'void *' array |
| 54 | |
| 55 | */ |
| 56 | |
| 57 | #include "Python.h" |
| 58 | #include "structmember.h" |
| 59 | |
| 60 | #ifdef MS_WIN32 |
| 61 | #include <windows.h> |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | #include <tchar.h> |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 63 | #else |
| 64 | #include "ctypes_dlfcn.h" |
| 65 | #endif |
| 66 | |
| 67 | #ifdef MS_WIN32 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 68 | #include <malloc.h> |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 69 | #endif |
| 70 | |
| 71 | #include <ffi.h> |
| 72 | #include "ctypes.h" |
Christian Heimes | f0400ba | 2013-06-18 13:22:17 +0200 | [diff] [blame] | 73 | #ifdef HAVE_ALLOCA_H |
| 74 | /* AIX needs alloca.h for alloca() */ |
Victor Stinner | 43b2639 | 2013-06-17 22:01:53 +0200 | [diff] [blame] | 75 | #include <alloca.h> |
Christian Heimes | f0400ba | 2013-06-18 13:22:17 +0200 | [diff] [blame] | 76 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 77 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 78 | #if defined(_DEBUG) || defined(__MINGW32__) |
| 79 | /* Don't use structured exception handling on Windows if this is defined. |
| 80 | MingW, AFAIK, doesn't support it. |
| 81 | */ |
| 82 | #define DONT_USE_SEH |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 83 | #endif |
| 84 | |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 85 | #define CTYPES_CAPSULE_NAME_PYMEM "_ctypes pymem" |
| 86 | |
| 87 | static void pymem_destructor(PyObject *ptr) |
| 88 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 89 | void *p = PyCapsule_GetPointer(ptr, CTYPES_CAPSULE_NAME_PYMEM); |
| 90 | if (p) { |
| 91 | PyMem_Free(p); |
| 92 | } |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 95 | /* |
| 96 | ctypes maintains thread-local storage that has space for two error numbers: |
| 97 | private copies of the system 'errno' value and, on Windows, the system error code |
| 98 | accessed by the GetLastError() and SetLastError() api functions. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 99 | |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 100 | Foreign functions created with CDLL(..., use_errno=True), when called, swap |
| 101 | the system 'errno' value with the private copy just before the actual |
| 102 | function call, and swapped again immediately afterwards. The 'use_errno' |
| 103 | parameter defaults to False, in this case 'ctypes_errno' is not touched. |
| 104 | |
| 105 | On Windows, foreign functions created with CDLL(..., use_last_error=True) or |
| 106 | WinDLL(..., use_last_error=True) swap the system LastError value with the |
| 107 | ctypes private copy. |
| 108 | |
| 109 | The values are also swapped immeditately before and after ctypes callback |
| 110 | functions are called, if the callbacks are constructed using the new |
| 111 | optional use_errno parameter set to True: CFUNCTYPE(..., use_errno=TRUE) or |
| 112 | WINFUNCTYPE(..., use_errno=True). |
| 113 | |
| 114 | New ctypes functions are provided to access the ctypes private copies from |
| 115 | Python: |
| 116 | |
| 117 | - ctypes.set_errno(value) and ctypes.set_last_error(value) store 'value' in |
| 118 | the private copy and returns the previous value. |
| 119 | |
| 120 | - ctypes.get_errno() and ctypes.get_last_error() returns the current ctypes |
| 121 | private copies value. |
| 122 | */ |
| 123 | |
| 124 | /* |
| 125 | This function creates and returns a thread-local Python object that has |
| 126 | space to store two integer error numbers; once created the Python object is |
| 127 | kept alive in the thread state dictionary as long as the thread itself. |
| 128 | */ |
| 129 | PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 130 | _ctypes_get_errobj(int **pspace) |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 131 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | PyObject *dict = PyThreadState_GetDict(); |
| 133 | PyObject *errobj; |
| 134 | static PyObject *error_object_name; |
| 135 | if (dict == 0) { |
| 136 | PyErr_SetString(PyExc_RuntimeError, |
| 137 | "cannot get thread state"); |
| 138 | return NULL; |
| 139 | } |
| 140 | if (error_object_name == NULL) { |
| 141 | error_object_name = PyUnicode_InternFromString("ctypes.error_object"); |
| 142 | if (error_object_name == NULL) |
| 143 | return NULL; |
| 144 | } |
| 145 | errobj = PyDict_GetItem(dict, error_object_name); |
| 146 | if (errobj) { |
| 147 | if (!PyCapsule_IsValid(errobj, CTYPES_CAPSULE_NAME_PYMEM)) { |
| 148 | PyErr_SetString(PyExc_RuntimeError, |
| 149 | "ctypes.error_object is an invalid capsule"); |
| 150 | return NULL; |
| 151 | } |
| 152 | Py_INCREF(errobj); |
| 153 | } |
| 154 | else { |
| 155 | void *space = PyMem_Malloc(sizeof(int) * 2); |
| 156 | if (space == NULL) |
| 157 | return NULL; |
| 158 | memset(space, 0, sizeof(int) * 2); |
| 159 | errobj = PyCapsule_New(space, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor); |
Serhiy Storchaka | adef646 | 2016-06-16 22:08:46 +0300 | [diff] [blame] | 160 | if (errobj == NULL) { |
| 161 | PyMem_Free(space); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | return NULL; |
Serhiy Storchaka | adef646 | 2016-06-16 22:08:46 +0300 | [diff] [blame] | 163 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 164 | if (-1 == PyDict_SetItem(dict, error_object_name, |
| 165 | errobj)) { |
| 166 | Py_DECREF(errobj); |
| 167 | return NULL; |
| 168 | } |
| 169 | } |
| 170 | *pspace = (int *)PyCapsule_GetPointer(errobj, CTYPES_CAPSULE_NAME_PYMEM); |
| 171 | return errobj; |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | static PyObject * |
| 175 | get_error_internal(PyObject *self, PyObject *args, int index) |
| 176 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 177 | int *space; |
| 178 | PyObject *errobj = _ctypes_get_errobj(&space); |
| 179 | PyObject *result; |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 181 | if (errobj == NULL) |
| 182 | return NULL; |
| 183 | result = PyLong_FromLong(space[index]); |
| 184 | Py_DECREF(errobj); |
| 185 | return result; |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | static PyObject * |
| 189 | set_error_internal(PyObject *self, PyObject *args, int index) |
| 190 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 191 | int new_errno, old_errno; |
| 192 | PyObject *errobj; |
| 193 | int *space; |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | if (!PyArg_ParseTuple(args, "i", &new_errno)) |
| 196 | return NULL; |
| 197 | errobj = _ctypes_get_errobj(&space); |
| 198 | if (errobj == NULL) |
| 199 | return NULL; |
| 200 | old_errno = space[index]; |
| 201 | space[index] = new_errno; |
| 202 | Py_DECREF(errobj); |
| 203 | return PyLong_FromLong(old_errno); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static PyObject * |
| 207 | get_errno(PyObject *self, PyObject *args) |
| 208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | return get_error_internal(self, args, 0); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static PyObject * |
| 213 | set_errno(PyObject *self, PyObject *args) |
| 214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 215 | return set_error_internal(self, args, 0); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 218 | #ifdef MS_WIN32 |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 219 | |
| 220 | static PyObject * |
| 221 | get_last_error(PyObject *self, PyObject *args) |
| 222 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 223 | return get_error_internal(self, args, 1); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static PyObject * |
| 227 | set_last_error(PyObject *self, PyObject *args) |
| 228 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 229 | return set_error_internal(self, args, 1); |
Thomas Heller | 9cac7b6 | 2008-06-06 09:31:40 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 232 | PyObject *ComError; |
| 233 | |
Thomas Heller | 71fb513 | 2008-11-26 08:45:36 +0000 | [diff] [blame] | 234 | static WCHAR *FormatError(DWORD code) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 235 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 236 | WCHAR *lpMsgBuf; |
| 237 | DWORD n; |
| 238 | n = FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 239 | NULL, |
| 240 | code, |
| 241 | MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ |
| 242 | (LPWSTR) &lpMsgBuf, |
| 243 | 0, |
| 244 | NULL); |
| 245 | if (n) { |
| 246 | while (iswspace(lpMsgBuf[n-1])) |
| 247 | --n; |
| 248 | lpMsgBuf[n] = L'\0'; /* rstrip() */ |
| 249 | } |
| 250 | return lpMsgBuf; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 253 | #ifndef DONT_USE_SEH |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 254 | static void SetException(DWORD code, EXCEPTION_RECORD *pr) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 256 | /* The 'code' is a normal win32 error code so it could be handled by |
| 257 | PyErr_SetFromWindowsErr(). However, for some errors, we have additional |
| 258 | information not included in the error code. We handle those here and |
| 259 | delegate all others to the generic function. */ |
| 260 | switch (code) { |
| 261 | case EXCEPTION_ACCESS_VIOLATION: |
| 262 | /* The thread attempted to read from or write |
| 263 | to a virtual address for which it does not |
| 264 | have the appropriate access. */ |
| 265 | if (pr->ExceptionInformation[0] == 0) |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 266 | PyErr_Format(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | "exception: access violation reading %p", |
| 268 | pr->ExceptionInformation[1]); |
| 269 | else |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 270 | PyErr_Format(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 271 | "exception: access violation writing %p", |
| 272 | pr->ExceptionInformation[1]); |
| 273 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 275 | case EXCEPTION_BREAKPOINT: |
| 276 | /* A breakpoint was encountered. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 277 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | "exception: breakpoint encountered"); |
| 279 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 281 | case EXCEPTION_DATATYPE_MISALIGNMENT: |
| 282 | /* The thread attempted to read or write data that is |
| 283 | misaligned on hardware that does not provide |
| 284 | alignment. For example, 16-bit values must be |
| 285 | aligned on 2-byte boundaries, 32-bit values on |
| 286 | 4-byte boundaries, and so on. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 287 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 288 | "exception: datatype misalignment"); |
| 289 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 290 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 291 | case EXCEPTION_SINGLE_STEP: |
| 292 | /* A trace trap or other single-instruction mechanism |
| 293 | signaled that one instruction has been executed. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 294 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | "exception: single step"); |
| 296 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: |
| 299 | /* The thread attempted to access an array element |
| 300 | that is out of bounds, and the underlying hardware |
| 301 | supports bounds checking. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 302 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 303 | "exception: array bounds exceeded"); |
| 304 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | case EXCEPTION_FLT_DENORMAL_OPERAND: |
| 307 | /* One of the operands in a floating-point operation |
| 308 | is denormal. A denormal value is one that is too |
| 309 | small to represent as a standard floating-point |
| 310 | value. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 311 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 312 | "exception: floating-point operand denormal"); |
| 313 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | case EXCEPTION_FLT_DIVIDE_BY_ZERO: |
| 316 | /* The thread attempted to divide a floating-point |
| 317 | value by a floating-point divisor of zero. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 318 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 319 | "exception: float divide by zero"); |
| 320 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | case EXCEPTION_FLT_INEXACT_RESULT: |
| 323 | /* The result of a floating-point operation cannot be |
| 324 | represented exactly as a decimal fraction. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 325 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | "exception: float inexact"); |
| 327 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 329 | case EXCEPTION_FLT_INVALID_OPERATION: |
| 330 | /* This exception represents any floating-point |
| 331 | exception not included in this list. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 332 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | "exception: float invalid operation"); |
| 334 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | case EXCEPTION_FLT_OVERFLOW: |
| 337 | /* The exponent of a floating-point operation is |
| 338 | greater than the magnitude allowed by the |
| 339 | corresponding type. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 340 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | "exception: float overflow"); |
| 342 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | case EXCEPTION_FLT_STACK_CHECK: |
| 345 | /* The stack overflowed or underflowed as the result |
| 346 | of a floating-point operation. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 347 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 348 | "exception: stack over/underflow"); |
| 349 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 350 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 351 | case EXCEPTION_STACK_OVERFLOW: |
| 352 | /* The stack overflowed or underflowed as the result |
| 353 | of a floating-point operation. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 354 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | "exception: stack overflow"); |
| 356 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 358 | case EXCEPTION_FLT_UNDERFLOW: |
| 359 | /* The exponent of a floating-point operation is less |
| 360 | than the magnitude allowed by the corresponding |
| 361 | type. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 362 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 363 | "exception: float underflow"); |
| 364 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | case EXCEPTION_INT_DIVIDE_BY_ZERO: |
| 367 | /* The thread attempted to divide an integer value by |
| 368 | an integer divisor of zero. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 369 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | "exception: integer divide by zero"); |
| 371 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | case EXCEPTION_INT_OVERFLOW: |
| 374 | /* The result of an integer operation caused a carry |
| 375 | out of the most significant bit of the result. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 376 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 377 | "exception: integer overflow"); |
| 378 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | case EXCEPTION_PRIV_INSTRUCTION: |
| 381 | /* The thread attempted to execute an instruction |
| 382 | whose operation is not allowed in the current |
| 383 | machine mode. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 384 | PyErr_SetString(PyExc_OSError, |
Martin Panter | e2e7168 | 2016-06-02 10:13:47 +0000 | [diff] [blame] | 385 | "exception: privileged instruction"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 386 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 387 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | case EXCEPTION_NONCONTINUABLE_EXCEPTION: |
| 389 | /* The thread attempted to continue execution after a |
| 390 | noncontinuable exception occurred. */ |
Andrew Svetlov | 2606a6f | 2012-12-19 14:33:35 +0200 | [diff] [blame] | 391 | PyErr_SetString(PyExc_OSError, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | "exception: nocontinuable"); |
| 393 | break; |
Thomas Heller | 84c97af | 2009-04-25 16:49:23 +0000 | [diff] [blame] | 394 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 395 | default: |
| 396 | PyErr_SetFromWindowsErr(code); |
| 397 | break; |
| 398 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static DWORD HandleException(EXCEPTION_POINTERS *ptrs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | DWORD *pdw, EXCEPTION_RECORD *record) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 404 | *pdw = ptrs->ExceptionRecord->ExceptionCode; |
| 405 | *record = *ptrs->ExceptionRecord; |
Kristján Valur Jónsson | 5aed330 | 2013-03-19 15:24:10 -0700 | [diff] [blame] | 406 | /* We don't want to catch breakpoint exceptions, they are used to attach |
| 407 | * a debugger to the process. |
| 408 | */ |
| 409 | if (*pdw == EXCEPTION_BREAKPOINT) |
| 410 | return EXCEPTION_CONTINUE_SEARCH; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | return EXCEPTION_EXECUTE_HANDLER; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 412 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 413 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 414 | |
| 415 | static PyObject * |
| 416 | check_hresult(PyObject *self, PyObject *args) |
| 417 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 418 | HRESULT hr; |
| 419 | if (!PyArg_ParseTuple(args, "i", &hr)) |
| 420 | return NULL; |
| 421 | if (FAILED(hr)) |
| 422 | return PyErr_SetFromWindowsErr(hr); |
| 423 | return PyLong_FromLong(hr); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | #endif |
| 427 | |
| 428 | /**************************************************************/ |
| 429 | |
| 430 | PyCArgObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 431 | PyCArgObject_new(void) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 432 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | PyCArgObject *p; |
| 434 | p = PyObject_New(PyCArgObject, &PyCArg_Type); |
| 435 | if (p == NULL) |
| 436 | return NULL; |
| 437 | p->pffi_type = NULL; |
| 438 | p->tag = '\0'; |
| 439 | p->obj = NULL; |
| 440 | memset(&p->value, 0, sizeof(p->value)); |
| 441 | return p; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | static void |
| 445 | PyCArg_dealloc(PyCArgObject *self) |
| 446 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 447 | Py_XDECREF(self->obj); |
| 448 | PyObject_Del(self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | static PyObject * |
| 452 | PyCArg_repr(PyCArgObject *self) |
| 453 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | char buffer[256]; |
| 455 | switch(self->tag) { |
| 456 | case 'b': |
| 457 | case 'B': |
| 458 | sprintf(buffer, "<cparam '%c' (%d)>", |
| 459 | self->tag, self->value.b); |
| 460 | break; |
| 461 | case 'h': |
| 462 | case 'H': |
| 463 | sprintf(buffer, "<cparam '%c' (%d)>", |
| 464 | self->tag, self->value.h); |
| 465 | break; |
| 466 | case 'i': |
| 467 | case 'I': |
| 468 | sprintf(buffer, "<cparam '%c' (%d)>", |
| 469 | self->tag, self->value.i); |
| 470 | break; |
| 471 | case 'l': |
| 472 | case 'L': |
| 473 | sprintf(buffer, "<cparam '%c' (%ld)>", |
| 474 | self->tag, self->value.l); |
| 475 | break; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | case 'q': |
| 478 | case 'Q': |
| 479 | sprintf(buffer, |
| 480 | #ifdef MS_WIN32 |
| 481 | "<cparam '%c' (%I64d)>", |
| 482 | #else |
| 483 | "<cparam '%c' (%qd)>", |
| 484 | #endif |
| 485 | self->tag, self->value.q); |
| 486 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | case 'd': |
| 488 | sprintf(buffer, "<cparam '%c' (%f)>", |
| 489 | self->tag, self->value.d); |
| 490 | break; |
| 491 | case 'f': |
| 492 | sprintf(buffer, "<cparam '%c' (%f)>", |
| 493 | self->tag, self->value.f); |
| 494 | break; |
| 495 | |
| 496 | case 'c': |
| 497 | sprintf(buffer, "<cparam '%c' (%c)>", |
| 498 | self->tag, self->value.c); |
| 499 | break; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 500 | |
| 501 | /* Hm, are these 'z' and 'Z' codes useful at all? |
| 502 | Shouldn't they be replaced by the functionality of c_string |
| 503 | and c_wstring ? |
| 504 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | case 'z': |
| 506 | case 'Z': |
| 507 | case 'P': |
| 508 | sprintf(buffer, "<cparam '%c' (%p)>", |
| 509 | self->tag, self->value.p); |
| 510 | break; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | default: |
| 513 | sprintf(buffer, "<cparam '%c' at %p>", |
| 514 | self->tag, self); |
| 515 | break; |
| 516 | } |
| 517 | return PyUnicode_FromString(buffer); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static PyMemberDef PyCArgType_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 521 | { "_obj", T_OBJECT, |
| 522 | offsetof(PyCArgObject, obj), READONLY, |
| 523 | "the wrapped object" }, |
| 524 | { NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | PyTypeObject PyCArg_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 528 | PyVarObject_HEAD_INIT(NULL, 0) |
| 529 | "CArgObject", |
| 530 | sizeof(PyCArgObject), |
| 531 | 0, |
| 532 | (destructor)PyCArg_dealloc, /* tp_dealloc */ |
| 533 | 0, /* tp_print */ |
| 534 | 0, /* tp_getattr */ |
| 535 | 0, /* tp_setattr */ |
| 536 | 0, /* tp_reserved */ |
| 537 | (reprfunc)PyCArg_repr, /* tp_repr */ |
| 538 | 0, /* tp_as_number */ |
| 539 | 0, /* tp_as_sequence */ |
| 540 | 0, /* tp_as_mapping */ |
| 541 | 0, /* tp_hash */ |
| 542 | 0, /* tp_call */ |
| 543 | 0, /* tp_str */ |
| 544 | 0, /* tp_getattro */ |
| 545 | 0, /* tp_setattro */ |
| 546 | 0, /* tp_as_buffer */ |
| 547 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 548 | 0, /* tp_doc */ |
| 549 | 0, /* tp_traverse */ |
| 550 | 0, /* tp_clear */ |
| 551 | 0, /* tp_richcompare */ |
| 552 | 0, /* tp_weaklistoffset */ |
| 553 | 0, /* tp_iter */ |
| 554 | 0, /* tp_iternext */ |
| 555 | 0, /* tp_methods */ |
| 556 | PyCArgType_members, /* tp_members */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 557 | }; |
| 558 | |
| 559 | /****************************************************************/ |
| 560 | /* |
| 561 | * Convert a PyObject * into a parameter suitable to pass to an |
| 562 | * C function call. |
| 563 | * |
| 564 | * 1. Python integers are converted to C int and passed by value. |
Thomas Heller | 69b639f | 2009-09-18 20:08:39 +0000 | [diff] [blame] | 565 | * Py_None is converted to a C NULL pointer. |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 566 | * |
| 567 | * 2. 3-tuples are expected to have a format character in the first |
| 568 | * item, which must be 'i', 'f', 'd', 'q', or 'P'. |
| 569 | * The second item will have to be an integer, float, double, long long |
| 570 | * or integer (denoting an address void *), will be converted to the |
| 571 | * corresponding C data type and passed by value. |
| 572 | * |
| 573 | * 3. Other Python objects are tested for an '_as_parameter_' attribute. |
| 574 | * The value of this attribute must be an integer which will be passed |
| 575 | * by value, or a 2-tuple or 3-tuple which will be used according |
| 576 | * to point 2 above. The third item (if any), is ignored. It is normally |
| 577 | * used to keep the object alive where this parameter refers to. |
| 578 | * XXX This convention is dangerous - you can construct arbitrary tuples |
| 579 | * in Python and pass them. Would it be safer to use a custom container |
| 580 | * datatype instead of a tuple? |
| 581 | * |
| 582 | * 4. Other Python objects cannot be passed as parameters - an exception is raised. |
| 583 | * |
| 584 | * 5. ConvParam will store the converted result in a struct containing format |
| 585 | * and value. |
| 586 | */ |
| 587 | |
| 588 | union result { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | char c; |
| 590 | char b; |
| 591 | short h; |
| 592 | int i; |
| 593 | long l; |
Benjamin Peterson | 47ff073 | 2016-09-08 09:15:54 -0700 | [diff] [blame] | 594 | long long q; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 595 | long double D; |
| 596 | double d; |
| 597 | float f; |
| 598 | void *p; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | struct argument { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | ffi_type *ffi_type; |
| 603 | PyObject *keep; |
| 604 | union result value; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | /* |
| 608 | * Convert a single Python object into a PyCArgObject and return it. |
| 609 | */ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 610 | static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 611 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | StgDictObject *dict; |
| 613 | pa->keep = NULL; /* so we cannot forget it later */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 615 | dict = PyObject_stgdict(obj); |
| 616 | if (dict) { |
| 617 | PyCArgObject *carg; |
| 618 | assert(dict->paramfunc); |
| 619 | /* If it has an stgdict, it is a CDataObject */ |
| 620 | carg = dict->paramfunc((CDataObject *)obj); |
Victor Stinner | 3f15cf0 | 2013-07-09 00:27:12 +0200 | [diff] [blame] | 621 | if (carg == NULL) |
| 622 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | pa->ffi_type = carg->pffi_type; |
| 624 | memcpy(&pa->value, &carg->value, sizeof(pa->value)); |
| 625 | pa->keep = (PyObject *)carg; |
| 626 | return 0; |
| 627 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | if (PyCArg_CheckExact(obj)) { |
| 630 | PyCArgObject *carg = (PyCArgObject *)obj; |
| 631 | pa->ffi_type = carg->pffi_type; |
| 632 | Py_INCREF(obj); |
| 633 | pa->keep = obj; |
| 634 | memcpy(&pa->value, &carg->value, sizeof(pa->value)); |
| 635 | return 0; |
| 636 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 637 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 638 | /* check for None, integer, string or unicode and use directly if successful */ |
| 639 | if (obj == Py_None) { |
| 640 | pa->ffi_type = &ffi_type_pointer; |
| 641 | pa->value.p = NULL; |
| 642 | return 0; |
| 643 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | if (PyLong_Check(obj)) { |
| 646 | pa->ffi_type = &ffi_type_sint; |
| 647 | pa->value.i = (long)PyLong_AsUnsignedLong(obj); |
| 648 | if (pa->value.i == -1 && PyErr_Occurred()) { |
| 649 | PyErr_Clear(); |
| 650 | pa->value.i = PyLong_AsLong(obj); |
| 651 | if (pa->value.i == -1 && PyErr_Occurred()) { |
| 652 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 653 | "int too long to convert"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 654 | return -1; |
| 655 | } |
| 656 | } |
| 657 | return 0; |
| 658 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 659 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 660 | if (PyBytes_Check(obj)) { |
| 661 | pa->ffi_type = &ffi_type_pointer; |
| 662 | pa->value.p = PyBytes_AsString(obj); |
| 663 | Py_INCREF(obj); |
| 664 | pa->keep = obj; |
| 665 | return 0; |
| 666 | } |
Thomas Heller | 7775c71 | 2007-07-12 19:19:43 +0000 | [diff] [blame] | 667 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 668 | #ifdef CTYPES_UNICODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | if (PyUnicode_Check(obj)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 670 | pa->ffi_type = &ffi_type_pointer; |
Victor Stinner | beb4135b | 2010-10-07 01:02:42 +0000 | [diff] [blame] | 671 | pa->value.p = PyUnicode_AsWideCharString(obj, NULL); |
Victor Stinner | 4c2e4fa | 2010-09-29 10:37:16 +0000 | [diff] [blame] | 672 | if (pa->value.p == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 674 | pa->keep = PyCapsule_New(pa->value.p, CTYPES_CAPSULE_NAME_PYMEM, pymem_destructor); |
| 675 | if (!pa->keep) { |
| 676 | PyMem_Free(pa->value.p); |
| 677 | return -1; |
| 678 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 679 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 680 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 681 | #endif |
| 682 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 683 | { |
| 684 | PyObject *arg; |
| 685 | arg = PyObject_GetAttrString(obj, "_as_parameter_"); |
| 686 | /* Which types should we exactly allow here? |
| 687 | integers are required for using Python classes |
| 688 | as parameters (they have to expose the '_as_parameter_' |
| 689 | attribute) |
| 690 | */ |
| 691 | if (arg) { |
| 692 | int result; |
| 693 | result = ConvParam(arg, index, pa); |
| 694 | Py_DECREF(arg); |
| 695 | return result; |
| 696 | } |
| 697 | PyErr_Format(PyExc_TypeError, |
| 698 | "Don't know how to convert parameter %d", |
| 699 | Py_SAFE_DOWNCAST(index, Py_ssize_t, int)); |
| 700 | return -1; |
| 701 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 705 | ffi_type *_ctypes_get_ffi_type(PyObject *obj) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 706 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 707 | StgDictObject *dict; |
| 708 | if (obj == NULL) |
| 709 | return &ffi_type_sint; |
| 710 | dict = PyType_stgdict(obj); |
| 711 | if (dict == NULL) |
| 712 | return &ffi_type_sint; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 713 | #if defined(MS_WIN32) && !defined(_WIN32_WCE) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | /* This little trick works correctly with MSVC. |
| 715 | It returns small structures in registers |
| 716 | */ |
| 717 | if (dict->ffi_type_pointer.type == FFI_TYPE_STRUCT) { |
| 718 | if (dict->ffi_type_pointer.size <= 4) |
| 719 | return &ffi_type_sint32; |
| 720 | else if (dict->ffi_type_pointer.size <= 8) |
| 721 | return &ffi_type_sint64; |
| 722 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 723 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 724 | return &dict->ffi_type_pointer; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
| 728 | /* |
| 729 | * libffi uses: |
| 730 | * |
| 731 | * ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 732 | * unsigned int nargs, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 733 | * ffi_type *rtype, |
| 734 | * ffi_type **atypes); |
| 735 | * |
| 736 | * and then |
| 737 | * |
| 738 | * void ffi_call(ffi_cif *cif, void *fn, void *rvalue, void **avalues); |
| 739 | */ |
| 740 | static int _call_function_pointer(int flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 741 | PPROC pProc, |
| 742 | void **avalues, |
| 743 | ffi_type **atypes, |
| 744 | ffi_type *restype, |
| 745 | void *resmem, |
| 746 | int argcount) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 747 | { |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 748 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 749 | PyThreadState *_save = NULL; /* For Py_BLOCK_THREADS and Py_UNBLOCK_THREADS */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 750 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | PyObject *error_object = NULL; |
| 752 | int *space; |
| 753 | ffi_cif cif; |
| 754 | int cc; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 755 | #ifdef MS_WIN32 |
Thomas Heller | b00697e | 2010-06-21 16:00:31 +0000 | [diff] [blame] | 756 | int delta; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 757 | #ifndef DONT_USE_SEH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 758 | DWORD dwExceptionCode = 0; |
| 759 | EXCEPTION_RECORD record; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 760 | #endif |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 761 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | /* XXX check before here */ |
| 763 | if (restype == NULL) { |
| 764 | PyErr_SetString(PyExc_RuntimeError, |
| 765 | "No ffi_type for result"); |
| 766 | return -1; |
| 767 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | cc = FFI_DEFAULT_ABI; |
| 770 | #if defined(MS_WIN32) && !defined(MS_WIN64) && !defined(_WIN32_WCE) |
| 771 | if ((flags & FUNCFLAG_CDECL) == 0) |
| 772 | cc = FFI_STDCALL; |
| 773 | #endif |
| 774 | if (FFI_OK != ffi_prep_cif(&cif, |
| 775 | cc, |
| 776 | argcount, |
| 777 | restype, |
| 778 | atypes)) { |
| 779 | PyErr_SetString(PyExc_RuntimeError, |
| 780 | "ffi_prep_cif failed"); |
| 781 | return -1; |
| 782 | } |
| 783 | |
| 784 | if (flags & (FUNCFLAG_USE_ERRNO | FUNCFLAG_USE_LASTERROR)) { |
| 785 | error_object = _ctypes_get_errobj(&space); |
| 786 | if (error_object == NULL) |
| 787 | return -1; |
| 788 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 789 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 790 | if ((flags & FUNCFLAG_PYTHONAPI) == 0) |
| 791 | Py_UNBLOCK_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 792 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 793 | if (flags & FUNCFLAG_USE_ERRNO) { |
| 794 | int temp = space[0]; |
| 795 | space[0] = errno; |
| 796 | errno = temp; |
| 797 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 798 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | if (flags & FUNCFLAG_USE_LASTERROR) { |
| 800 | int temp = space[1]; |
| 801 | space[1] = GetLastError(); |
| 802 | SetLastError(temp); |
| 803 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 804 | #ifndef DONT_USE_SEH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 805 | __try { |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 806 | #endif |
Thomas Heller | b00697e | 2010-06-21 16:00:31 +0000 | [diff] [blame] | 807 | delta = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 808 | #endif |
Thomas Heller | b00697e | 2010-06-21 16:00:31 +0000 | [diff] [blame] | 809 | ffi_call(&cif, (void *)pProc, resmem, avalues); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 810 | #ifdef MS_WIN32 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 811 | #ifndef DONT_USE_SEH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 812 | } |
| 813 | __except (HandleException(GetExceptionInformation(), |
| 814 | &dwExceptionCode, &record)) { |
| 815 | ; |
| 816 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 817 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | if (flags & FUNCFLAG_USE_LASTERROR) { |
| 819 | int temp = space[1]; |
| 820 | space[1] = GetLastError(); |
| 821 | SetLastError(temp); |
| 822 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 823 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 824 | if (flags & FUNCFLAG_USE_ERRNO) { |
| 825 | int temp = space[0]; |
| 826 | space[0] = errno; |
| 827 | errno = temp; |
| 828 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 829 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | if ((flags & FUNCFLAG_PYTHONAPI) == 0) |
| 831 | Py_BLOCK_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 832 | #endif |
Kristjan Valur Jonsson | 9946bd6 | 2012-12-21 09:41:25 +0000 | [diff] [blame] | 833 | Py_XDECREF(error_object); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 834 | #ifdef MS_WIN32 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 835 | #ifndef DONT_USE_SEH |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | if (dwExceptionCode) { |
| 837 | SetException(dwExceptionCode, &record); |
| 838 | return -1; |
| 839 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 840 | #endif |
Thomas Heller | b00697e | 2010-06-21 16:00:31 +0000 | [diff] [blame] | 841 | #ifdef MS_WIN64 |
| 842 | if (delta != 0) { |
| 843 | PyErr_Format(PyExc_RuntimeError, |
| 844 | "ffi_call failed with code %d", |
| 845 | delta); |
| 846 | return -1; |
| 847 | } |
| 848 | #else |
| 849 | if (delta < 0) { |
| 850 | if (flags & FUNCFLAG_CDECL) |
| 851 | PyErr_Format(PyExc_ValueError, |
| 852 | "Procedure called with not enough " |
| 853 | "arguments (%d bytes missing) " |
| 854 | "or wrong calling convention", |
| 855 | -delta); |
| 856 | else |
| 857 | PyErr_Format(PyExc_ValueError, |
| 858 | "Procedure probably called with not enough " |
| 859 | "arguments (%d bytes missing)", |
| 860 | -delta); |
| 861 | return -1; |
| 862 | } else if (delta > 0) { |
| 863 | PyErr_Format(PyExc_ValueError, |
| 864 | "Procedure probably called with too many " |
| 865 | "arguments (%d bytes in excess)", |
| 866 | delta); |
| 867 | return -1; |
| 868 | } |
| 869 | #endif |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 870 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 871 | if ((flags & FUNCFLAG_PYTHONAPI) && PyErr_Occurred()) |
| 872 | return -1; |
| 873 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* |
| 877 | * Convert the C value in result into a Python object, depending on restype. |
| 878 | * |
| 879 | * - If restype is NULL, return a Python integer. |
| 880 | * - If restype is None, return None. |
| 881 | * - If restype is a simple ctypes type (c_int, c_void_p), call the type's getfunc, |
| 882 | * pass the result to checker and return the result. |
| 883 | * - If restype is another ctypes type, return an instance of that. |
| 884 | * - Otherwise, call restype and return the result. |
| 885 | */ |
| 886 | static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker) |
| 887 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 888 | StgDictObject *dict; |
| 889 | PyObject *retval, *v; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 890 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | if (restype == NULL) |
| 892 | return PyLong_FromLong(*(int *)result); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 893 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | if (restype == Py_None) { |
| 895 | Py_INCREF(Py_None); |
| 896 | return Py_None; |
| 897 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 899 | dict = PyType_stgdict(restype); |
| 900 | if (dict == NULL) |
| 901 | return PyObject_CallFunction(restype, "i", *(int *)result); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 903 | if (dict->getfunc && !_ctypes_simple_instance(restype)) { |
| 904 | retval = dict->getfunc(result, dict->size); |
| 905 | /* If restype is py_object (detected by comparing getfunc with |
| 906 | O_get), we have to call Py_DECREF because O_get has already |
| 907 | called Py_INCREF. |
| 908 | */ |
| 909 | if (dict->getfunc == _ctypes_get_fielddesc("O")->getfunc) { |
| 910 | Py_DECREF(retval); |
| 911 | } |
| 912 | } else |
| 913 | retval = PyCData_FromBaseObj(restype, NULL, 0, result); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | if (!checker || !retval) |
| 916 | return retval; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 917 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | v = PyObject_CallFunctionObjArgs(checker, retval, NULL); |
| 919 | if (v == NULL) |
Antoine Pitrou | 0ddbf47 | 2014-10-08 20:00:09 +0200 | [diff] [blame] | 920 | _PyTraceback_Add("GetResult", "_ctypes/callproc.c", __LINE__-2); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | Py_DECREF(retval); |
| 922 | return v; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | /* |
| 926 | * Raise a new exception 'exc_class', adding additional text to the original |
| 927 | * exception string. |
| 928 | */ |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 929 | void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 930 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 931 | va_list vargs; |
| 932 | PyObject *tp, *v, *tb, *s, *cls_str, *msg_str; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 934 | va_start(vargs, fmt); |
| 935 | s = PyUnicode_FromFormatV(fmt, vargs); |
| 936 | va_end(vargs); |
| 937 | if (!s) |
| 938 | return; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 939 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 940 | PyErr_Fetch(&tp, &v, &tb); |
| 941 | PyErr_NormalizeException(&tp, &v, &tb); |
| 942 | cls_str = PyObject_Str(tp); |
| 943 | if (cls_str) { |
| 944 | PyUnicode_AppendAndDel(&s, cls_str); |
| 945 | PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": ")); |
| 946 | if (s == NULL) |
| 947 | goto error; |
| 948 | } else |
| 949 | PyErr_Clear(); |
| 950 | msg_str = PyObject_Str(v); |
| 951 | if (msg_str) |
| 952 | PyUnicode_AppendAndDel(&s, msg_str); |
| 953 | else { |
| 954 | PyErr_Clear(); |
| 955 | PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???")); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 956 | } |
Victor Stinner | 67002af | 2011-10-02 20:35:10 +0200 | [diff] [blame] | 957 | if (s == NULL) |
| 958 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | PyErr_SetObject(exc_class, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 960 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | Py_XDECREF(tp); |
| 962 | Py_XDECREF(v); |
| 963 | Py_XDECREF(tb); |
| 964 | Py_XDECREF(s); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | |
| 968 | #ifdef MS_WIN32 |
| 969 | |
| 970 | static PyObject * |
| 971 | GetComError(HRESULT errcode, GUID *riid, IUnknown *pIunk) |
| 972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 973 | HRESULT hr; |
| 974 | ISupportErrorInfo *psei = NULL; |
| 975 | IErrorInfo *pei = NULL; |
| 976 | BSTR descr=NULL, helpfile=NULL, source=NULL; |
| 977 | GUID guid; |
| 978 | DWORD helpcontext=0; |
| 979 | LPOLESTR progid; |
| 980 | PyObject *obj; |
| 981 | LPOLESTR text; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 982 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | /* We absolutely have to release the GIL during COM method calls, |
| 984 | otherwise we may get a deadlock! |
| 985 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 986 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 987 | Py_BEGIN_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 988 | #endif |
| 989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 990 | hr = pIunk->lpVtbl->QueryInterface(pIunk, &IID_ISupportErrorInfo, (void **)&psei); |
| 991 | if (FAILED(hr)) |
| 992 | goto failed; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | hr = psei->lpVtbl->InterfaceSupportsErrorInfo(psei, riid); |
| 995 | psei->lpVtbl->Release(psei); |
| 996 | if (FAILED(hr)) |
| 997 | goto failed; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 999 | hr = GetErrorInfo(0, &pei); |
| 1000 | if (hr != S_OK) |
| 1001 | goto failed; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1002 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | pei->lpVtbl->GetDescription(pei, &descr); |
| 1004 | pei->lpVtbl->GetGUID(pei, &guid); |
| 1005 | pei->lpVtbl->GetHelpContext(pei, &helpcontext); |
| 1006 | pei->lpVtbl->GetHelpFile(pei, &helpfile); |
| 1007 | pei->lpVtbl->GetSource(pei, &source); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1008 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1009 | pei->lpVtbl->Release(pei); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1010 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1011 | failed: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1012 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | Py_END_ALLOW_THREADS |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1014 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | progid = NULL; |
| 1017 | ProgIDFromCLSID(&guid, &progid); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1018 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | text = FormatError(errcode); |
| 1020 | obj = Py_BuildValue( |
| 1021 | "iu(uuuiu)", |
| 1022 | errcode, |
| 1023 | text, |
| 1024 | descr, source, helpfile, helpcontext, |
| 1025 | progid); |
| 1026 | if (obj) { |
| 1027 | PyErr_SetObject(ComError, obj); |
| 1028 | Py_DECREF(obj); |
| 1029 | } |
| 1030 | LocalFree(text); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | if (descr) |
| 1033 | SysFreeString(descr); |
| 1034 | if (helpfile) |
| 1035 | SysFreeString(helpfile); |
| 1036 | if (source) |
| 1037 | SysFreeString(source); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1038 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1040 | } |
| 1041 | #endif |
| 1042 | |
| 1043 | /* |
| 1044 | * Requirements, must be ensured by the caller: |
| 1045 | * - argtuple is tuple of arguments |
| 1046 | * - argtypes is either NULL, or a tuple of the same size as argtuple |
| 1047 | * |
| 1048 | * - XXX various requirements for restype, not yet collected |
| 1049 | */ |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1050 | PyObject *_ctypes_callproc(PPROC pProc, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | PyObject *argtuple, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1052 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | IUnknown *pIunk, |
| 1054 | GUID *iid, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1055 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1056 | int flags, |
| 1057 | PyObject *argtypes, /* misleading name: This is a tuple of |
| 1058 | methods, not types: the .from_param |
| 1059 | class methods of the types */ |
| 1060 | PyObject *restype, |
| 1061 | PyObject *checker) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1062 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | Py_ssize_t i, n, argcount, argtype_count; |
| 1064 | void *resbuf; |
| 1065 | struct argument *args, *pa; |
| 1066 | ffi_type **atypes; |
| 1067 | ffi_type *rtype; |
| 1068 | void **avalues; |
| 1069 | PyObject *retval = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1070 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | n = argcount = PyTuple_GET_SIZE(argtuple); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1072 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1073 | /* an optional COM object this pointer */ |
| 1074 | if (pIunk) |
| 1075 | ++argcount; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1076 | #endif |
| 1077 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | args = (struct argument *)alloca(sizeof(struct argument) * argcount); |
| 1079 | if (!args) { |
| 1080 | PyErr_NoMemory(); |
| 1081 | return NULL; |
| 1082 | } |
| 1083 | memset(args, 0, sizeof(struct argument) * argcount); |
| 1084 | argtype_count = argtypes ? PyTuple_GET_SIZE(argtypes) : 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1085 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1086 | if (pIunk) { |
| 1087 | args[0].ffi_type = &ffi_type_pointer; |
| 1088 | args[0].value.p = pIunk; |
| 1089 | pa = &args[1]; |
Eli Bendersky | ca83233 | 2013-03-06 06:30:23 -0800 | [diff] [blame] | 1090 | } else |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1091 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1092 | pa = &args[0]; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1093 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1094 | /* Convert the arguments */ |
| 1095 | for (i = 0; i < n; ++i, ++pa) { |
| 1096 | PyObject *converter; |
| 1097 | PyObject *arg; |
| 1098 | int err; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1099 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | arg = PyTuple_GET_ITEM(argtuple, i); /* borrowed ref */ |
| 1101 | /* For cdecl functions, we allow more actual arguments |
| 1102 | than the length of the argtypes tuple. |
| 1103 | This is checked in _ctypes::PyCFuncPtr_Call |
| 1104 | */ |
| 1105 | if (argtypes && argtype_count > i) { |
| 1106 | PyObject *v; |
| 1107 | converter = PyTuple_GET_ITEM(argtypes, i); |
Eli Bendersky | d867bad | 2013-03-06 05:45:57 -0800 | [diff] [blame] | 1108 | v = PyObject_CallFunctionObjArgs(converter, arg, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1109 | if (v == NULL) { |
| 1110 | _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1); |
| 1111 | goto cleanup; |
| 1112 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1114 | err = ConvParam(v, i+1, pa); |
| 1115 | Py_DECREF(v); |
| 1116 | if (-1 == err) { |
| 1117 | _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1); |
| 1118 | goto cleanup; |
| 1119 | } |
| 1120 | } else { |
| 1121 | err = ConvParam(arg, i+1, pa); |
| 1122 | if (-1 == err) { |
| 1123 | _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1); |
| 1124 | goto cleanup; /* leaking ? */ |
| 1125 | } |
| 1126 | } |
| 1127 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1129 | rtype = _ctypes_get_ffi_type(restype); |
| 1130 | resbuf = alloca(max(rtype->size, sizeof(ffi_arg))); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | avalues = (void **)alloca(sizeof(void *) * argcount); |
| 1133 | atypes = (ffi_type **)alloca(sizeof(ffi_type *) * argcount); |
| 1134 | if (!resbuf || !avalues || !atypes) { |
| 1135 | PyErr_NoMemory(); |
| 1136 | goto cleanup; |
| 1137 | } |
| 1138 | for (i = 0; i < argcount; ++i) { |
| 1139 | atypes[i] = args[i].ffi_type; |
Victor Stinner | 4c2e4fa | 2010-09-29 10:37:16 +0000 | [diff] [blame] | 1140 | if (atypes[i]->type == FFI_TYPE_STRUCT |
Thomas Heller | b00697e | 2010-06-21 16:00:31 +0000 | [diff] [blame] | 1141 | ) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1142 | avalues[i] = (void *)args[i].value.p; |
| 1143 | else |
| 1144 | avalues[i] = (void *)&args[i].value; |
| 1145 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1147 | if (-1 == _call_function_pointer(flags, pProc, avalues, atypes, |
| 1148 | rtype, resbuf, |
| 1149 | Py_SAFE_DOWNCAST(argcount, |
| 1150 | Py_ssize_t, |
| 1151 | int))) |
| 1152 | goto cleanup; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1153 | |
| 1154 | #ifdef WORDS_BIGENDIAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1155 | /* libffi returns the result in a buffer with sizeof(ffi_arg). This |
| 1156 | causes problems on big endian machines, since the result buffer |
| 1157 | address cannot simply be used as result pointer, instead we must |
| 1158 | adjust the pointer value: |
| 1159 | */ |
| 1160 | /* |
| 1161 | XXX I should find out and clarify why this is needed at all, |
| 1162 | especially why adjusting for ffi_type_float must be avoided on |
| 1163 | 64-bit platforms. |
| 1164 | */ |
| 1165 | if (rtype->type != FFI_TYPE_FLOAT |
| 1166 | && rtype->type != FFI_TYPE_STRUCT |
| 1167 | && rtype->size < sizeof(ffi_arg)) |
| 1168 | resbuf = (char *)resbuf + sizeof(ffi_arg) - rtype->size; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1169 | #endif |
| 1170 | |
| 1171 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1172 | if (iid && pIunk) { |
| 1173 | if (*(int *)resbuf & 0x80000000) |
| 1174 | retval = GetComError(*(HRESULT *)resbuf, iid, pIunk); |
| 1175 | else |
| 1176 | retval = PyLong_FromLong(*(int *)resbuf); |
| 1177 | } else if (flags & FUNCFLAG_HRESULT) { |
| 1178 | if (*(int *)resbuf & 0x80000000) |
| 1179 | retval = PyErr_SetFromWindowsErr(*(int *)resbuf); |
| 1180 | else |
| 1181 | retval = PyLong_FromLong(*(int *)resbuf); |
Eli Bendersky | ca83233 | 2013-03-06 06:30:23 -0800 | [diff] [blame] | 1182 | } else |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1183 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | retval = GetResult(restype, resbuf, checker); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1185 | cleanup: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | for (i = 0; i < argcount; ++i) |
| 1187 | Py_XDECREF(args[i].keep); |
| 1188 | return retval; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1191 | static int |
| 1192 | _parse_voidp(PyObject *obj, void **address) |
| 1193 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | *address = PyLong_AsVoidPtr(obj); |
| 1195 | if (*address == NULL) |
| 1196 | return 0; |
| 1197 | return 1; |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1200 | #ifdef MS_WIN32 |
| 1201 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1202 | static const char format_error_doc[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1203 | "FormatError([integer]) -> string\n\ |
| 1204 | \n\ |
| 1205 | Convert a win32 error code into a string. If the error code is not\n\ |
| 1206 | given, the return value of a call to GetLastError() is used.\n"; |
| 1207 | static PyObject *format_error(PyObject *self, PyObject *args) |
| 1208 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1209 | PyObject *result; |
| 1210 | wchar_t *lpMsgBuf; |
| 1211 | DWORD code = 0; |
| 1212 | if (!PyArg_ParseTuple(args, "|i:FormatError", &code)) |
| 1213 | return NULL; |
| 1214 | if (code == 0) |
| 1215 | code = GetLastError(); |
| 1216 | lpMsgBuf = FormatError(code); |
| 1217 | if (lpMsgBuf) { |
| 1218 | result = PyUnicode_FromWideChar(lpMsgBuf, wcslen(lpMsgBuf)); |
| 1219 | LocalFree(lpMsgBuf); |
| 1220 | } else { |
| 1221 | result = PyUnicode_FromString("<no description>"); |
| 1222 | } |
| 1223 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1226 | static const char load_library_doc[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1227 | "LoadLibrary(name) -> handle\n\ |
| 1228 | \n\ |
| 1229 | Load an executable (usually a DLL), and return a handle to it.\n\ |
| 1230 | The handle may be used to locate exported functions in this\n\ |
| 1231 | module.\n"; |
| 1232 | static PyObject *load_library(PyObject *self, PyObject *args) |
| 1233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1234 | WCHAR *name; |
| 1235 | PyObject *nameobj; |
| 1236 | PyObject *ignored; |
| 1237 | HMODULE hMod; |
| 1238 | if (!PyArg_ParseTuple(args, "O|O:LoadLibrary", &nameobj, &ignored)) |
| 1239 | return NULL; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1240 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1241 | name = PyUnicode_AsUnicode(nameobj); |
| 1242 | if (!name) |
| 1243 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1245 | hMod = LoadLibraryW(name); |
| 1246 | if (!hMod) |
| 1247 | return PyErr_SetFromWindowsErr(GetLastError()); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1248 | #ifdef _WIN64 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1249 | return PyLong_FromVoidPtr(hMod); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1250 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1251 | return Py_BuildValue("i", hMod); |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1252 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1255 | static const char free_library_doc[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1256 | "FreeLibrary(handle) -> void\n\ |
| 1257 | \n\ |
| 1258 | Free the handle of an executable previously loaded by LoadLibrary.\n"; |
| 1259 | static PyObject *free_library(PyObject *self, PyObject *args) |
| 1260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1261 | void *hMod; |
| 1262 | if (!PyArg_ParseTuple(args, "O&:FreeLibrary", &_parse_voidp, &hMod)) |
| 1263 | return NULL; |
| 1264 | if (!FreeLibrary((HMODULE)hMod)) |
| 1265 | return PyErr_SetFromWindowsErr(GetLastError()); |
| 1266 | Py_INCREF(Py_None); |
| 1267 | return Py_None; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1270 | static const char copy_com_pointer_doc[] = |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1271 | "CopyComPointer(src, dst) -> HRESULT value\n"; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1272 | |
| 1273 | static PyObject * |
| 1274 | copy_com_pointer(PyObject *self, PyObject *args) |
| 1275 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1276 | PyObject *p1, *p2, *r = NULL; |
| 1277 | struct argument a, b; |
| 1278 | IUnknown *src, **pdst; |
| 1279 | if (!PyArg_ParseTuple(args, "OO:CopyComPointer", &p1, &p2)) |
| 1280 | return NULL; |
| 1281 | a.keep = b.keep = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1283 | if (-1 == ConvParam(p1, 0, &a) || -1 == ConvParam(p2, 1, &b)) |
| 1284 | goto done; |
| 1285 | src = (IUnknown *)a.value.p; |
| 1286 | pdst = (IUnknown **)b.value.p; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1288 | if (pdst == NULL) |
| 1289 | r = PyLong_FromLong(E_POINTER); |
| 1290 | else { |
| 1291 | if (src) |
| 1292 | src->lpVtbl->AddRef(src); |
| 1293 | *pdst = src; |
| 1294 | r = PyLong_FromLong(S_OK); |
| 1295 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1296 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1297 | Py_XDECREF(a.keep); |
| 1298 | Py_XDECREF(b.keep); |
| 1299 | return r; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1300 | } |
| 1301 | #else |
| 1302 | |
| 1303 | static PyObject *py_dl_open(PyObject *self, PyObject *args) |
| 1304 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1305 | PyObject *name, *name2; |
| 1306 | char *name_str; |
| 1307 | void * handle; |
Serhiy Storchaka | c2f7d87 | 2016-05-04 09:44:44 +0300 | [diff] [blame] | 1308 | #if HAVE_DECL_RTLD_LOCAL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | int mode = RTLD_NOW | RTLD_LOCAL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1310 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1311 | /* cygwin doesn't define RTLD_LOCAL */ |
| 1312 | int mode = RTLD_NOW; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1313 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1314 | if (!PyArg_ParseTuple(args, "O|i:dlopen", &name, &mode)) |
| 1315 | return NULL; |
| 1316 | mode |= RTLD_NOW; |
| 1317 | if (name != Py_None) { |
| 1318 | if (PyUnicode_FSConverter(name, &name2) == 0) |
| 1319 | return NULL; |
| 1320 | if (PyBytes_Check(name2)) |
| 1321 | name_str = PyBytes_AS_STRING(name2); |
| 1322 | else |
| 1323 | name_str = PyByteArray_AS_STRING(name2); |
| 1324 | } else { |
| 1325 | name_str = NULL; |
| 1326 | name2 = NULL; |
| 1327 | } |
| 1328 | handle = ctypes_dlopen(name_str, mode); |
| 1329 | Py_XDECREF(name2); |
| 1330 | if (!handle) { |
| 1331 | char *errmsg = ctypes_dlerror(); |
| 1332 | if (!errmsg) |
| 1333 | errmsg = "dlopen() error"; |
| 1334 | PyErr_SetString(PyExc_OSError, |
| 1335 | errmsg); |
| 1336 | return NULL; |
| 1337 | } |
| 1338 | return PyLong_FromVoidPtr(handle); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
| 1341 | static PyObject *py_dl_close(PyObject *self, PyObject *args) |
| 1342 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | void *handle; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | if (!PyArg_ParseTuple(args, "O&:dlclose", &_parse_voidp, &handle)) |
| 1346 | return NULL; |
| 1347 | if (dlclose(handle)) { |
| 1348 | PyErr_SetString(PyExc_OSError, |
| 1349 | ctypes_dlerror()); |
| 1350 | return NULL; |
| 1351 | } |
| 1352 | Py_INCREF(Py_None); |
| 1353 | return Py_None; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | static PyObject *py_dl_sym(PyObject *self, PyObject *args) |
| 1357 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | char *name; |
| 1359 | void *handle; |
| 1360 | void *ptr; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | if (!PyArg_ParseTuple(args, "O&s:dlsym", |
| 1363 | &_parse_voidp, &handle, &name)) |
| 1364 | return NULL; |
| 1365 | ptr = ctypes_dlsym((void*)handle, name); |
| 1366 | if (!ptr) { |
| 1367 | PyErr_SetString(PyExc_OSError, |
| 1368 | ctypes_dlerror()); |
| 1369 | return NULL; |
| 1370 | } |
| 1371 | return PyLong_FromVoidPtr(ptr); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1372 | } |
| 1373 | #endif |
| 1374 | |
| 1375 | /* |
| 1376 | * Only for debugging so far: So that we can call CFunction instances |
| 1377 | * |
| 1378 | * XXX Needs to accept more arguments: flags, argtypes, restype |
| 1379 | */ |
| 1380 | static PyObject * |
| 1381 | call_function(PyObject *self, PyObject *args) |
| 1382 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1383 | void *func; |
| 1384 | PyObject *arguments; |
| 1385 | PyObject *result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1387 | if (!PyArg_ParseTuple(args, |
| 1388 | "O&O!", |
| 1389 | &_parse_voidp, &func, |
| 1390 | &PyTuple_Type, &arguments)) |
| 1391 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1392 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1393 | result = _ctypes_callproc((PPROC)func, |
| 1394 | arguments, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1395 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | NULL, |
| 1397 | NULL, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1398 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1399 | 0, /* flags */ |
| 1400 | NULL, /* self->argtypes */ |
| 1401 | NULL, /* self->restype */ |
| 1402 | NULL); /* checker */ |
| 1403 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* |
| 1407 | * Only for debugging so far: So that we can call CFunction instances |
| 1408 | * |
| 1409 | * XXX Needs to accept more arguments: flags, argtypes, restype |
| 1410 | */ |
| 1411 | static PyObject * |
| 1412 | call_cdeclfunction(PyObject *self, PyObject *args) |
| 1413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | void *func; |
| 1415 | PyObject *arguments; |
| 1416 | PyObject *result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1417 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1418 | if (!PyArg_ParseTuple(args, |
| 1419 | "O&O!", |
| 1420 | &_parse_voidp, &func, |
| 1421 | &PyTuple_Type, &arguments)) |
| 1422 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1423 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | result = _ctypes_callproc((PPROC)func, |
| 1425 | arguments, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1426 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | NULL, |
| 1428 | NULL, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1429 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | FUNCFLAG_CDECL, /* flags */ |
Eli Bendersky | d867bad | 2013-03-06 05:45:57 -0800 | [diff] [blame] | 1431 | NULL, /* self->argtypes */ |
| 1432 | NULL, /* self->restype */ |
| 1433 | NULL); /* checker */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | /***************************************************************** |
| 1438 | * functions |
| 1439 | */ |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1440 | static const char sizeof_doc[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1441 | "sizeof(C type) -> integer\n" |
| 1442 | "sizeof(C instance) -> integer\n" |
| 1443 | "Return the size in bytes of a C instance"; |
| 1444 | |
| 1445 | static PyObject * |
| 1446 | sizeof_func(PyObject *self, PyObject *obj) |
| 1447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1448 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | dict = PyType_stgdict(obj); |
| 1451 | if (dict) |
| 1452 | return PyLong_FromSsize_t(dict->size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | if (CDataObject_Check(obj)) |
| 1455 | return PyLong_FromSsize_t(((CDataObject *)obj)->b_size); |
| 1456 | PyErr_SetString(PyExc_TypeError, |
| 1457 | "this type has no size"); |
| 1458 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1461 | static const char alignment_doc[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1462 | "alignment(C type) -> integer\n" |
| 1463 | "alignment(C instance) -> integer\n" |
| 1464 | "Return the alignment requirements of a C instance"; |
| 1465 | |
| 1466 | static PyObject * |
| 1467 | align_func(PyObject *self, PyObject *obj) |
| 1468 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1470 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1471 | dict = PyType_stgdict(obj); |
| 1472 | if (dict) |
| 1473 | return PyLong_FromSsize_t(dict->align); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1474 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1475 | dict = PyObject_stgdict(obj); |
| 1476 | if (dict) |
| 1477 | return PyLong_FromSsize_t(dict->align); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1479 | PyErr_SetString(PyExc_TypeError, |
| 1480 | "no alignment info"); |
| 1481 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1484 | static const char byref_doc[] = |
Thomas Heller | c5d0126 | 2008-06-10 15:08:51 +0000 | [diff] [blame] | 1485 | "byref(C instance[, offset=0]) -> byref-object\n" |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1486 | "Return a pointer lookalike to a C instance, only usable\n" |
| 1487 | "as function argument"; |
| 1488 | |
| 1489 | /* |
| 1490 | * We must return something which can be converted to a parameter, |
| 1491 | * but still has a reference to self. |
| 1492 | */ |
| 1493 | static PyObject * |
Thomas Heller | c5d0126 | 2008-06-10 15:08:51 +0000 | [diff] [blame] | 1494 | byref(PyObject *self, PyObject *args) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1495 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | PyCArgObject *parg; |
| 1497 | PyObject *obj; |
| 1498 | PyObject *pyoffset = NULL; |
| 1499 | Py_ssize_t offset = 0; |
Thomas Heller | c5d0126 | 2008-06-10 15:08:51 +0000 | [diff] [blame] | 1500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1501 | if (!PyArg_UnpackTuple(args, "byref", 1, 2, |
| 1502 | &obj, &pyoffset)) |
| 1503 | return NULL; |
| 1504 | if (pyoffset) { |
| 1505 | offset = PyNumber_AsSsize_t(pyoffset, NULL); |
| 1506 | if (offset == -1 && PyErr_Occurred()) |
| 1507 | return NULL; |
| 1508 | } |
| 1509 | if (!CDataObject_Check(obj)) { |
| 1510 | PyErr_Format(PyExc_TypeError, |
| 1511 | "byref() argument must be a ctypes instance, not '%s'", |
| 1512 | Py_TYPE(obj)->tp_name); |
| 1513 | return NULL; |
| 1514 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | parg = PyCArgObject_new(); |
| 1517 | if (parg == NULL) |
| 1518 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1519 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1520 | parg->tag = 'P'; |
| 1521 | parg->pffi_type = &ffi_type_pointer; |
| 1522 | Py_INCREF(obj); |
| 1523 | parg->obj = obj; |
| 1524 | parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset; |
| 1525 | return (PyObject *)parg; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Serhiy Storchaka | 2d06e84 | 2015-12-25 19:53:18 +0200 | [diff] [blame] | 1528 | static const char addressof_doc[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1529 | "addressof(C instance) -> integer\n" |
| 1530 | "Return the address of the C instance internal buffer"; |
| 1531 | |
| 1532 | static PyObject * |
| 1533 | addressof(PyObject *self, PyObject *obj) |
| 1534 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | if (CDataObject_Check(obj)) |
| 1536 | return PyLong_FromVoidPtr(((CDataObject *)obj)->b_ptr); |
| 1537 | PyErr_SetString(PyExc_TypeError, |
| 1538 | "invalid type"); |
| 1539 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | static int |
| 1543 | converter(PyObject *obj, void **address) |
| 1544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | *address = PyLong_AsVoidPtr(obj); |
| 1546 | return *address != NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
| 1549 | static PyObject * |
| 1550 | My_PyObj_FromPtr(PyObject *self, PyObject *args) |
| 1551 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1552 | PyObject *ob; |
| 1553 | if (!PyArg_ParseTuple(args, "O&:PyObj_FromPtr", converter, &ob)) |
| 1554 | return NULL; |
| 1555 | Py_INCREF(ob); |
| 1556 | return ob; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | static PyObject * |
| 1560 | My_Py_INCREF(PyObject *self, PyObject *arg) |
| 1561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1562 | Py_INCREF(arg); /* that's what this function is for */ |
| 1563 | Py_INCREF(arg); /* that for returning it */ |
| 1564 | return arg; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
| 1567 | static PyObject * |
| 1568 | My_Py_DECREF(PyObject *self, PyObject *arg) |
| 1569 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | Py_DECREF(arg); /* that's what this function is for */ |
| 1571 | Py_INCREF(arg); /* that's for returning it */ |
| 1572 | return arg; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1575 | static PyObject * |
| 1576 | resize(PyObject *self, PyObject *args) |
| 1577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1578 | CDataObject *obj; |
| 1579 | StgDictObject *dict; |
| 1580 | Py_ssize_t size; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1582 | if (!PyArg_ParseTuple(args, |
| 1583 | "On:resize", |
| 1584 | &obj, &size)) |
| 1585 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | dict = PyObject_stgdict((PyObject *)obj); |
| 1588 | if (dict == NULL) { |
| 1589 | PyErr_SetString(PyExc_TypeError, |
| 1590 | "excepted ctypes instance"); |
| 1591 | return NULL; |
| 1592 | } |
| 1593 | if (size < dict->size) { |
| 1594 | PyErr_Format(PyExc_ValueError, |
| 1595 | "minimum size is %zd", |
| 1596 | dict->size); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | if (obj->b_needsfree == 0) { |
| 1600 | PyErr_Format(PyExc_ValueError, |
| 1601 | "Memory cannot be resized because this object doesn't own it"); |
| 1602 | return NULL; |
| 1603 | } |
Victor Stinner | 706768c | 2014-08-16 01:03:39 +0200 | [diff] [blame] | 1604 | if ((size_t)size <= sizeof(obj->b_value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | /* internal default buffer is large enough */ |
| 1606 | obj->b_size = size; |
| 1607 | goto done; |
| 1608 | } |
Antoine Pitrou | 305e1a7 | 2012-12-08 11:05:50 +0100 | [diff] [blame] | 1609 | if (!_CDataObject_HasExternalBuffer(obj)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1610 | /* We are currently using the objects default buffer, but it |
| 1611 | isn't large enough any more. */ |
| 1612 | void *ptr = PyMem_Malloc(size); |
| 1613 | if (ptr == NULL) |
| 1614 | return PyErr_NoMemory(); |
| 1615 | memset(ptr, 0, size); |
| 1616 | memmove(ptr, obj->b_ptr, obj->b_size); |
| 1617 | obj->b_ptr = ptr; |
| 1618 | obj->b_size = size; |
| 1619 | } else { |
| 1620 | void * ptr = PyMem_Realloc(obj->b_ptr, size); |
| 1621 | if (ptr == NULL) |
| 1622 | return PyErr_NoMemory(); |
| 1623 | obj->b_ptr = ptr; |
| 1624 | obj->b_size = size; |
| 1625 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1626 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1627 | Py_INCREF(Py_None); |
| 1628 | return Py_None; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 1631 | static PyObject * |
| 1632 | unpickle(PyObject *self, PyObject *args) |
| 1633 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1634 | PyObject *typ; |
| 1635 | PyObject *state; |
| 1636 | PyObject *result; |
| 1637 | PyObject *tmp; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1638 | _Py_IDENTIFIER(__new__); |
| 1639 | _Py_IDENTIFIER(__setstate__); |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 1640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | if (!PyArg_ParseTuple(args, "OO", &typ, &state)) |
| 1642 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1643 | result = _PyObject_CallMethodId(typ, &PyId___new__, "O", typ); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1644 | if (result == NULL) |
| 1645 | return NULL; |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 1646 | tmp = _PyObject_CallMethodId(result, &PyId___setstate__, "O", state); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | if (tmp == NULL) { |
| 1648 | Py_DECREF(result); |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | Py_DECREF(tmp); |
| 1652 | return result; |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 1655 | static PyObject * |
| 1656 | POINTER(PyObject *self, PyObject *cls) |
| 1657 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | PyObject *result; |
| 1659 | PyTypeObject *typ; |
| 1660 | PyObject *key; |
| 1661 | char *buf; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 1662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | result = PyDict_GetItem(_ctypes_ptrtype_cache, cls); |
| 1664 | if (result) { |
| 1665 | Py_INCREF(result); |
| 1666 | return result; |
| 1667 | } |
| 1668 | if (PyUnicode_CheckExact(cls)) { |
Serhiy Storchaka | 144f77a | 2016-11-20 08:47:21 +0200 | [diff] [blame] | 1669 | const char *name = PyUnicode_AsUTF8(cls); |
| 1670 | if (name == NULL) |
| 1671 | return NULL; |
R David Murray | 817905b | 2014-10-12 13:54:48 -0400 | [diff] [blame] | 1672 | buf = PyMem_Malloc(strlen(name) + 3 + 1); |
| 1673 | if (buf == NULL) |
| 1674 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1675 | sprintf(buf, "LP_%s", name); |
| 1676 | result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type), |
| 1677 | "s(O){}", |
| 1678 | buf, |
| 1679 | &PyCPointer_Type); |
R David Murray | 817905b | 2014-10-12 13:54:48 -0400 | [diff] [blame] | 1680 | PyMem_Free(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1681 | if (result == NULL) |
| 1682 | return result; |
| 1683 | key = PyLong_FromVoidPtr(result); |
Serhiy Storchaka | adef646 | 2016-06-16 22:08:46 +0300 | [diff] [blame] | 1684 | if (key == NULL) { |
| 1685 | Py_DECREF(result); |
| 1686 | return NULL; |
| 1687 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1688 | } else if (PyType_Check(cls)) { |
| 1689 | typ = (PyTypeObject *)cls; |
R David Murray | 817905b | 2014-10-12 13:54:48 -0400 | [diff] [blame] | 1690 | buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1); |
| 1691 | if (buf == NULL) |
| 1692 | return PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | sprintf(buf, "LP_%s", typ->tp_name); |
| 1694 | result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type), |
| 1695 | "s(O){sO}", |
| 1696 | buf, |
| 1697 | &PyCPointer_Type, |
| 1698 | "_type_", cls); |
R David Murray | 817905b | 2014-10-12 13:54:48 -0400 | [diff] [blame] | 1699 | PyMem_Free(buf); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1700 | if (result == NULL) |
| 1701 | return result; |
| 1702 | Py_INCREF(cls); |
| 1703 | key = cls; |
| 1704 | } else { |
| 1705 | PyErr_SetString(PyExc_TypeError, "must be a ctypes type"); |
| 1706 | return NULL; |
| 1707 | } |
| 1708 | if (-1 == PyDict_SetItem(_ctypes_ptrtype_cache, key, result)) { |
| 1709 | Py_DECREF(result); |
| 1710 | Py_DECREF(key); |
| 1711 | return NULL; |
| 1712 | } |
| 1713 | Py_DECREF(key); |
| 1714 | return result; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | static PyObject * |
| 1718 | pointer(PyObject *self, PyObject *arg) |
| 1719 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1720 | PyObject *result; |
| 1721 | PyObject *typ; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 1722 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1723 | typ = PyDict_GetItem(_ctypes_ptrtype_cache, (PyObject *)Py_TYPE(arg)); |
| 1724 | if (typ) |
| 1725 | return PyObject_CallFunctionObjArgs(typ, arg, NULL); |
| 1726 | typ = POINTER(NULL, (PyObject *)Py_TYPE(arg)); |
| 1727 | if (typ == NULL) |
| 1728 | return NULL; |
| 1729 | result = PyObject_CallFunctionObjArgs(typ, arg, NULL); |
| 1730 | Py_DECREF(typ); |
| 1731 | return result; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 1732 | } |
| 1733 | |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1734 | static PyObject * |
| 1735 | buffer_info(PyObject *self, PyObject *arg) |
| 1736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1737 | StgDictObject *dict = PyType_stgdict(arg); |
| 1738 | PyObject *shape; |
| 1739 | Py_ssize_t i; |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1741 | if (dict == NULL) |
| 1742 | dict = PyObject_stgdict(arg); |
| 1743 | if (dict == NULL) { |
| 1744 | PyErr_SetString(PyExc_TypeError, |
| 1745 | "not a ctypes type or object"); |
| 1746 | return NULL; |
| 1747 | } |
| 1748 | shape = PyTuple_New(dict->ndim); |
| 1749 | if (shape == NULL) |
| 1750 | return NULL; |
| 1751 | for (i = 0; i < (int)dict->ndim; ++i) |
| 1752 | PyTuple_SET_ITEM(shape, i, PyLong_FromSsize_t(dict->shape[i])); |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1754 | if (PyErr_Occurred()) { |
| 1755 | Py_DECREF(shape); |
| 1756 | return NULL; |
| 1757 | } |
| 1758 | return Py_BuildValue("siN", dict->format, dict->ndim, shape); |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1761 | PyMethodDef _ctypes_module_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1762 | {"get_errno", get_errno, METH_NOARGS}, |
| 1763 | {"set_errno", set_errno, METH_VARARGS}, |
| 1764 | {"POINTER", POINTER, METH_O }, |
| 1765 | {"pointer", pointer, METH_O }, |
| 1766 | {"_unpickle", unpickle, METH_VARARGS }, |
| 1767 | {"buffer_info", buffer_info, METH_O, "Return buffer interface information"}, |
| 1768 | {"resize", resize, METH_VARARGS, "Resize the memory buffer of a ctypes instance"}, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1769 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | {"get_last_error", get_last_error, METH_NOARGS}, |
| 1771 | {"set_last_error", set_last_error, METH_VARARGS}, |
| 1772 | {"CopyComPointer", copy_com_pointer, METH_VARARGS, copy_com_pointer_doc}, |
| 1773 | {"FormatError", format_error, METH_VARARGS, format_error_doc}, |
| 1774 | {"LoadLibrary", load_library, METH_VARARGS, load_library_doc}, |
| 1775 | {"FreeLibrary", free_library, METH_VARARGS, free_library_doc}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1776 | {"_check_HRESULT", check_hresult, METH_VARARGS}, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1777 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | {"dlopen", py_dl_open, METH_VARARGS, |
| 1779 | "dlopen(name, flag={RTLD_GLOBAL|RTLD_LOCAL}) open a shared library"}, |
| 1780 | {"dlclose", py_dl_close, METH_VARARGS, "dlclose a library"}, |
| 1781 | {"dlsym", py_dl_sym, METH_VARARGS, "find symbol in shared library"}, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1782 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1783 | {"alignment", align_func, METH_O, alignment_doc}, |
| 1784 | {"sizeof", sizeof_func, METH_O, sizeof_doc}, |
| 1785 | {"byref", byref, METH_VARARGS, byref_doc}, |
| 1786 | {"addressof", addressof, METH_O, addressof_doc}, |
| 1787 | {"call_function", call_function, METH_VARARGS }, |
| 1788 | {"call_cdeclfunction", call_cdeclfunction, METH_VARARGS }, |
| 1789 | {"PyObj_FromPtr", My_PyObj_FromPtr, METH_VARARGS }, |
| 1790 | {"Py_INCREF", My_Py_INCREF, METH_O }, |
| 1791 | {"Py_DECREF", My_Py_DECREF, METH_O }, |
| 1792 | {NULL, NULL} /* Sentinel */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1793 | }; |
| 1794 | |
| 1795 | /* |
| 1796 | Local Variables: |
| 1797 | compile-command: "cd .. && python setup.py -q build -g && python setup.py -q build install --home ~" |
| 1798 | End: |
| 1799 | */ |