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