blob: 7d542fb50d876705acac452039c2f2bd360930cf [file] [log] [blame]
Thomas Hellerd4c93202006-03-08 19:35:11 +00001/*
2 * History: First version dated from 3/97, derived from my SCMLIB version
3 * for win16.
4 */
5/*
6 * Related Work:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00007 * - 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 Hellerd4c93202006-03-08 19:35:11 +000010 * 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 Heller34596a92009-04-24 20:50:00 +000018 1. _ctypes.c::PyCFuncPtr_call receives an argument tuple 'inargs' and a
Thomas Hellerd4c93202006-03-08 19:35:11 +000019 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 Reedy0158af32013-03-11 17:42:46 -040023 'inargs', or a completely fresh tuple, depending on several things (is it a
24 COM method?, are 'paramflags' available?).
Thomas Hellerd4c93202006-03-08 19:35:11 +000025
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 Heller34596a92009-04-24 20:50:00 +000030 4. _ctypes_callproc is then called with the 'callargs' tuple. _ctypes_callproc first
Thomas Hellerd4c93202006-03-08 19:35:11 +000031 allocates two arrays. The first is an array of 'struct argument' items, the
Ezio Melotti13925002011-03-16 11:05:33 +020032 second array has 'void *' entries.
Thomas Hellerd4c93202006-03-08 19:35:11 +000033
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 Heller34596a92009-04-24 20:50:00 +000050 - the inargs tuple (in PyCFuncPtr_call)
51 - the callargs tuple (in PyCFuncPtr_call)
Ezio Melotti42da6632011-03-15 05:18:48 +020052 - the 'struct arguments' array
Thomas Hellerd4c93202006-03-08 19:35:11 +000053 - 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 Rossumd8faa362007-04-27 19:54:29 +000062#include <tchar.h>
Thomas Hellerd4c93202006-03-08 19:35:11 +000063#else
64#include "ctypes_dlfcn.h"
65#endif
66
67#ifdef MS_WIN32
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000068#include <malloc.h>
Thomas Hellerd4c93202006-03-08 19:35:11 +000069#endif
70
71#include <ffi.h>
72#include "ctypes.h"
Christian Heimesf0400ba2013-06-18 13:22:17 +020073#ifdef HAVE_ALLOCA_H
74/* AIX needs alloca.h for alloca() */
Victor Stinner43b26392013-06-17 22:01:53 +020075#include <alloca.h>
Christian Heimesf0400ba2013-06-18 13:22:17 +020076#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +000077
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078#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 Hellerd4c93202006-03-08 19:35:11 +000083#endif
84
Benjamin Petersonb173f782009-05-05 22:31:58 +000085#define CTYPES_CAPSULE_NAME_PYMEM "_ctypes pymem"
86
87static void pymem_destructor(PyObject *ptr)
88{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 void *p = PyCapsule_GetPointer(ptr, CTYPES_CAPSULE_NAME_PYMEM);
90 if (p) {
91 PyMem_Free(p);
92 }
Benjamin Petersonb173f782009-05-05 22:31:58 +000093}
94
Thomas Heller9cac7b62008-06-06 09:31:40 +000095/*
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 Pitrouf95a1b32010-05-09 15:52:27 +000099
Thomas Heller9cac7b62008-06-06 09:31:40 +0000100 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*/
129PyObject *
Thomas Heller34596a92009-04-24 20:50:00 +0000130_ctypes_get_errobj(int **pspace)
Thomas Heller9cac7b62008-06-06 09:31:40 +0000131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 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 Storchakaadef6462016-06-16 22:08:46 +0300160 if (errobj == NULL) {
161 PyMem_Free(space);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 return NULL;
Serhiy Storchakaadef6462016-06-16 22:08:46 +0300163 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000164 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 Heller9cac7b62008-06-06 09:31:40 +0000172}
173
174static PyObject *
175get_error_internal(PyObject *self, PyObject *args, int index)
176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 int *space;
178 PyObject *errobj = _ctypes_get_errobj(&space);
179 PyObject *result;
Thomas Heller9cac7b62008-06-06 09:31:40 +0000180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 if (errobj == NULL)
182 return NULL;
183 result = PyLong_FromLong(space[index]);
184 Py_DECREF(errobj);
185 return result;
Thomas Heller9cac7b62008-06-06 09:31:40 +0000186}
187
188static PyObject *
189set_error_internal(PyObject *self, PyObject *args, int index)
190{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 int new_errno, old_errno;
192 PyObject *errobj;
193 int *space;
Thomas Heller9cac7b62008-06-06 09:31:40 +0000194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195 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 Heller9cac7b62008-06-06 09:31:40 +0000204}
205
206static PyObject *
207get_errno(PyObject *self, PyObject *args)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return get_error_internal(self, args, 0);
Thomas Heller9cac7b62008-06-06 09:31:40 +0000210}
211
212static PyObject *
213set_errno(PyObject *self, PyObject *args)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 return set_error_internal(self, args, 0);
Thomas Heller9cac7b62008-06-06 09:31:40 +0000216}
217
Thomas Hellerd4c93202006-03-08 19:35:11 +0000218#ifdef MS_WIN32
Thomas Heller9cac7b62008-06-06 09:31:40 +0000219
220static PyObject *
221get_last_error(PyObject *self, PyObject *args)
222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 return get_error_internal(self, args, 1);
Thomas Heller9cac7b62008-06-06 09:31:40 +0000224}
225
226static PyObject *
227set_last_error(PyObject *self, PyObject *args)
228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 return set_error_internal(self, args, 1);
Thomas Heller9cac7b62008-06-06 09:31:40 +0000230}
231
Thomas Hellerd4c93202006-03-08 19:35:11 +0000232PyObject *ComError;
233
Thomas Heller71fb5132008-11-26 08:45:36 +0000234static WCHAR *FormatError(DWORD code)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000235{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 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 Hellerd4c93202006-03-08 19:35:11 +0000251}
252
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000253#ifndef DONT_USE_SEH
Thomas Heller84c97af2009-04-25 16:49:23 +0000254static void SetException(DWORD code, EXCEPTION_RECORD *pr)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000255{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 /* 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 Svetlov2606a6f2012-12-19 14:33:35 +0200266 PyErr_Format(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 "exception: access violation reading %p",
268 pr->ExceptionInformation[1]);
269 else
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200270 PyErr_Format(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 "exception: access violation writing %p",
272 pr->ExceptionInformation[1]);
273 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 case EXCEPTION_BREAKPOINT:
276 /* A breakpoint was encountered. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200277 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 "exception: breakpoint encountered");
279 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 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 Svetlov2606a6f2012-12-19 14:33:35 +0200287 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 "exception: datatype misalignment");
289 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000290
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000291 case EXCEPTION_SINGLE_STEP:
292 /* A trace trap or other single-instruction mechanism
293 signaled that one instruction has been executed. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200294 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 "exception: single step");
296 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 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 Svetlov2606a6f2012-12-19 14:33:35 +0200302 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 "exception: array bounds exceeded");
304 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 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 Svetlov2606a6f2012-12-19 14:33:35 +0200311 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 "exception: floating-point operand denormal");
313 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000315 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 Svetlov2606a6f2012-12-19 14:33:35 +0200318 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 "exception: float divide by zero");
320 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 case EXCEPTION_FLT_INEXACT_RESULT:
323 /* The result of a floating-point operation cannot be
324 represented exactly as a decimal fraction. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200325 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 "exception: float inexact");
327 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 case EXCEPTION_FLT_INVALID_OPERATION:
330 /* This exception represents any floating-point
331 exception not included in this list. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200332 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 "exception: float invalid operation");
334 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 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 Svetlov2606a6f2012-12-19 14:33:35 +0200340 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 "exception: float overflow");
342 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 case EXCEPTION_FLT_STACK_CHECK:
345 /* The stack overflowed or underflowed as the result
346 of a floating-point operation. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200347 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 "exception: stack over/underflow");
349 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 case EXCEPTION_STACK_OVERFLOW:
352 /* The stack overflowed or underflowed as the result
353 of a floating-point operation. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200354 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 "exception: stack overflow");
356 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 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 Svetlov2606a6f2012-12-19 14:33:35 +0200362 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 "exception: float underflow");
364 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 case EXCEPTION_INT_DIVIDE_BY_ZERO:
367 /* The thread attempted to divide an integer value by
368 an integer divisor of zero. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200369 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 "exception: integer divide by zero");
371 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 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 Svetlov2606a6f2012-12-19 14:33:35 +0200376 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 "exception: integer overflow");
378 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 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 Svetlov2606a6f2012-12-19 14:33:35 +0200384 PyErr_SetString(PyExc_OSError,
Martin Pantere2e71682016-06-02 10:13:47 +0000385 "exception: privileged instruction");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 case EXCEPTION_NONCONTINUABLE_EXCEPTION:
389 /* The thread attempted to continue execution after a
390 noncontinuable exception occurred. */
Andrew Svetlov2606a6f2012-12-19 14:33:35 +0200391 PyErr_SetString(PyExc_OSError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 "exception: nocontinuable");
393 break;
Thomas Heller84c97af2009-04-25 16:49:23 +0000394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 default:
396 PyErr_SetFromWindowsErr(code);
397 break;
398 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000399}
400
401static DWORD HandleException(EXCEPTION_POINTERS *ptrs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 DWORD *pdw, EXCEPTION_RECORD *record)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000403{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 *pdw = ptrs->ExceptionRecord->ExceptionCode;
405 *record = *ptrs->ExceptionRecord;
Kristján Valur Jónsson5aed3302013-03-19 15:24:10 -0700406 /* 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 Pitrouf95a1b32010-05-09 15:52:27 +0000411 return EXCEPTION_EXECUTE_HANDLER;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000412}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000413#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +0000414
415static PyObject *
416check_hresult(PyObject *self, PyObject *args)
417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 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 Hellerd4c93202006-03-08 19:35:11 +0000424}
425
426#endif
427
428/**************************************************************/
429
430PyCArgObject *
Thomas Heller34596a92009-04-24 20:50:00 +0000431PyCArgObject_new(void)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 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 Hellerd4c93202006-03-08 19:35:11 +0000442}
443
444static void
445PyCArg_dealloc(PyCArgObject *self)
446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 Py_XDECREF(self->obj);
448 PyObject_Del(self);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000449}
450
451static PyObject *
452PyCArg_repr(PyCArgObject *self)
453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 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 Hellerd4c93202006-03-08 19:35:11 +0000476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 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 Pitrouf95a1b32010-05-09 15:52:27 +0000487 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 Hellerd4c93202006-03-08 19:35:11 +0000500
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 Pitrouf95a1b32010-05-09 15:52:27 +0000505 case 'z':
506 case 'Z':
507 case 'P':
508 sprintf(buffer, "<cparam '%c' (%p)>",
509 self->tag, self->value.p);
510 break;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 default:
513 sprintf(buffer, "<cparam '%c' at %p>",
514 self->tag, self);
515 break;
516 }
517 return PyUnicode_FromString(buffer);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000518}
519
520static PyMemberDef PyCArgType_members[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 { "_obj", T_OBJECT,
522 offsetof(PyCArgObject, obj), READONLY,
523 "the wrapped object" },
524 { NULL },
Thomas Hellerd4c93202006-03-08 19:35:11 +0000525};
526
527PyTypeObject PyCArg_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 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 Hellerd4c93202006-03-08 19:35:11 +0000557};
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 Heller69b639f2009-09-18 20:08:39 +0000565 * Py_None is converted to a C NULL pointer.
Thomas Hellerd4c93202006-03-08 19:35:11 +0000566 *
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
588union result {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 char c;
590 char b;
591 short h;
592 int i;
593 long l;
Benjamin Peterson47ff0732016-09-08 09:15:54 -0700594 long long q;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 long double D;
596 double d;
597 float f;
598 void *p;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000599};
600
601struct argument {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 ffi_type *ffi_type;
603 PyObject *keep;
604 union result value;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000605};
606
607/*
608 * Convert a single Python object into a PyCArgObject and return it.
609 */
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000610static int ConvParam(PyObject *obj, Py_ssize_t index, struct argument *pa)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612 StgDictObject *dict;
613 pa->keep = NULL; /* so we cannot forget it later */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 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 Stinner3f15cf02013-07-09 00:27:12 +0200621 if (carg == NULL)
622 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 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 Wouters00ee7ba2006-08-21 19:07:27 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 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 Hellerd4c93202006-03-08 19:35:11 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* 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 Hellerd4c93202006-03-08 19:35:11 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 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 Storchaka95949422013-08-27 19:40:23 +0300653 "int too long to convert");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 return -1;
655 }
656 }
657 return 0;
658 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 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 Heller7775c712007-07-12 19:19:43 +0000667
Thomas Hellerd4c93202006-03-08 19:35:11 +0000668#ifdef CTYPES_UNICODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 if (PyUnicode_Check(obj)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000670 pa->ffi_type = &ffi_type_pointer;
Victor Stinnerbeb4135b2010-10-07 01:02:42 +0000671 pa->value.p = PyUnicode_AsWideCharString(obj, NULL);
Victor Stinner4c2e4fa2010-09-29 10:37:16 +0000672 if (pa->value.p == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 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 Pitrouf95a1b32010-05-09 15:52:27 +0000679 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000681#endif
682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 {
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 Hellerd4c93202006-03-08 19:35:11 +0000702}
703
704
Thomas Heller34596a92009-04-24 20:50:00 +0000705ffi_type *_ctypes_get_ffi_type(PyObject *obj)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 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 Hellerd4c93202006-03-08 19:35:11 +0000713#if defined(MS_WIN32) && !defined(_WIN32_WCE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 /* 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 Hellerd4c93202006-03-08 19:35:11 +0000723#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 return &dict->ffi_type_pointer;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000725}
726
727
728/*
729 * libffi uses:
730 *
731 * ffi_status ffi_prep_cif(ffi_cif *cif, ffi_abi abi,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 * unsigned int nargs,
Thomas Hellerd4c93202006-03-08 19:35:11 +0000733 * 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 */
740static int _call_function_pointer(int flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 PPROC pProc,
742 void **avalues,
743 ffi_type **atypes,
744 ffi_type *restype,
745 void *resmem,
746 int argcount)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000747{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000748#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000749 PyThreadState *_save = NULL; /* For Py_BLOCK_THREADS and Py_UNBLOCK_THREADS */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000750#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 PyObject *error_object = NULL;
752 int *space;
753 ffi_cif cif;
754 int cc;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000755#ifdef MS_WIN32
Thomas Hellerb00697e2010-06-21 16:00:31 +0000756 int delta;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757#ifndef DONT_USE_SEH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 DWORD dwExceptionCode = 0;
759 EXCEPTION_RECORD record;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000760#endif
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 /* XXX check before here */
763 if (restype == NULL) {
764 PyErr_SetString(PyExc_RuntimeError,
765 "No ffi_type for result");
766 return -1;
767 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 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 Wouters0e3f5912006-08-11 14:57:12 +0000789#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 if ((flags & FUNCFLAG_PYTHONAPI) == 0)
791 Py_UNBLOCK_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000792#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 if (flags & FUNCFLAG_USE_ERRNO) {
794 int temp = space[0];
795 space[0] = errno;
796 errno = temp;
797 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000798#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 if (flags & FUNCFLAG_USE_LASTERROR) {
800 int temp = space[1];
801 space[1] = GetLastError();
802 SetLastError(temp);
803 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000804#ifndef DONT_USE_SEH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 __try {
Thomas Hellerd4c93202006-03-08 19:35:11 +0000806#endif
Thomas Hellerb00697e2010-06-21 16:00:31 +0000807 delta =
Thomas Hellerd4c93202006-03-08 19:35:11 +0000808#endif
Thomas Hellerb00697e2010-06-21 16:00:31 +0000809 ffi_call(&cif, (void *)pProc, resmem, avalues);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000810#ifdef MS_WIN32
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000811#ifndef DONT_USE_SEH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 }
813 __except (HandleException(GetExceptionInformation(),
814 &dwExceptionCode, &record)) {
815 ;
816 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000817#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (flags & FUNCFLAG_USE_LASTERROR) {
819 int temp = space[1];
820 space[1] = GetLastError();
821 SetLastError(temp);
822 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000823#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 if (flags & FUNCFLAG_USE_ERRNO) {
825 int temp = space[0];
826 space[0] = errno;
827 errno = temp;
828 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000829#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if ((flags & FUNCFLAG_PYTHONAPI) == 0)
831 Py_BLOCK_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000832#endif
Kristjan Valur Jonsson9946bd62012-12-21 09:41:25 +0000833 Py_XDECREF(error_object);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000834#ifdef MS_WIN32
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000835#ifndef DONT_USE_SEH
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 if (dwExceptionCode) {
837 SetException(dwExceptionCode, &record);
838 return -1;
839 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000840#endif
Thomas Hellerb00697e2010-06-21 16:00:31 +0000841#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 Wouters89f507f2006-12-13 04:49:30 +0000870#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 if ((flags & FUNCFLAG_PYTHONAPI) && PyErr_Occurred())
872 return -1;
873 return 0;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000874}
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 */
886static PyObject *GetResult(PyObject *restype, void *result, PyObject *checker)
887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 StgDictObject *dict;
889 PyObject *retval, *v;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 if (restype == NULL)
892 return PyLong_FromLong(*(int *)result);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 if (restype == Py_None) {
895 Py_INCREF(Py_None);
896 return Py_None;
897 }
Thomas Hellerd4c93202006-03-08 19:35:11 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 dict = PyType_stgdict(restype);
900 if (dict == NULL)
901 return PyObject_CallFunction(restype, "i", *(int *)result);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 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 Hellerd4c93202006-03-08 19:35:11 +0000914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 if (!checker || !retval)
916 return retval;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 v = PyObject_CallFunctionObjArgs(checker, retval, NULL);
919 if (v == NULL)
Antoine Pitrou0ddbf472014-10-08 20:00:09 +0200920 _PyTraceback_Add("GetResult", "_ctypes/callproc.c", __LINE__-2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 Py_DECREF(retval);
922 return v;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000923}
924
925/*
926 * Raise a new exception 'exc_class', adding additional text to the original
927 * exception string.
928 */
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200929void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
Thomas Hellerd4c93202006-03-08 19:35:11 +0000930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 va_list vargs;
932 PyObject *tp, *v, *tb, *s, *cls_str, *msg_str;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 va_start(vargs, fmt);
935 s = PyUnicode_FromFormatV(fmt, vargs);
936 va_end(vargs);
937 if (!s)
938 return;
Thomas Hellerd4c93202006-03-08 19:35:11 +0000939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 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 Pitrouf95a1b32010-05-09 15:52:27 +0000956 }
Victor Stinner67002af2011-10-02 20:35:10 +0200957 if (s == NULL)
958 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 PyErr_SetObject(exc_class, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000960error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 Py_XDECREF(tp);
962 Py_XDECREF(v);
963 Py_XDECREF(tb);
964 Py_XDECREF(s);
Thomas Hellerd4c93202006-03-08 19:35:11 +0000965}
966
967
968#ifdef MS_WIN32
969
970static PyObject *
971GetComError(HRESULT errcode, GUID *riid, IUnknown *pIunk)
972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973 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 Hellerd4c93202006-03-08 19:35:11 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 /* We absolutely have to release the GIL during COM method calls,
984 otherwise we may get a deadlock!
985 */
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000986#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 Py_BEGIN_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000988#endif
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 hr = pIunk->lpVtbl->QueryInterface(pIunk, &IID_ISupportErrorInfo, (void **)&psei);
991 if (FAILED(hr))
992 goto failed;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 hr = psei->lpVtbl->InterfaceSupportsErrorInfo(psei, riid);
995 psei->lpVtbl->Release(psei);
996 if (FAILED(hr))
997 goto failed;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 hr = GetErrorInfo(0, &pei);
1000 if (hr != S_OK)
1001 goto failed;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 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 Hellerd4c93202006-03-08 19:35:11 +00001008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 pei->lpVtbl->Release(pei);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001010
Thomas Hellerd4c93202006-03-08 19:35:11 +00001011 failed:
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001012#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Py_END_ALLOW_THREADS
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001014#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 progid = NULL;
1017 ProgIDFromCLSID(&guid, &progid);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 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 Hellerd4c93202006-03-08 19:35:11 +00001031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 if (descr)
1033 SysFreeString(descr);
1034 if (helpfile)
1035 SysFreeString(helpfile);
1036 if (source)
1037 SysFreeString(source);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001040}
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 Heller34596a92009-04-24 20:50:00 +00001050PyObject *_ctypes_callproc(PPROC pProc,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 PyObject *argtuple,
Thomas Hellerd4c93202006-03-08 19:35:11 +00001052#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 IUnknown *pIunk,
1054 GUID *iid,
Thomas Hellerd4c93202006-03-08 19:35:11 +00001055#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 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 Hellerd4c93202006-03-08 19:35:11 +00001062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 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 Hellerd4c93202006-03-08 19:35:11 +00001070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 n = argcount = PyTuple_GET_SIZE(argtuple);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001072#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 /* an optional COM object this pointer */
1074 if (pIunk)
1075 ++argcount;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001076#endif
1077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 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 Hellerd4c93202006-03-08 19:35:11 +00001085#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 if (pIunk) {
1087 args[0].ffi_type = &ffi_type_pointer;
1088 args[0].value.p = pIunk;
1089 pa = &args[1];
Eli Benderskyca832332013-03-06 06:30:23 -08001090 } else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001091#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 pa = &args[0];
Thomas Hellerd4c93202006-03-08 19:35:11 +00001093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 /* Convert the arguments */
1095 for (i = 0; i < n; ++i, ++pa) {
1096 PyObject *converter;
1097 PyObject *arg;
1098 int err;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 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 Benderskyd867bad2013-03-06 05:45:57 -08001108 v = PyObject_CallFunctionObjArgs(converter, arg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (v == NULL) {
1110 _ctypes_extend_error(PyExc_ArgError, "argument %d: ", i+1);
1111 goto cleanup;
1112 }
Thomas Hellerd4c93202006-03-08 19:35:11 +00001113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 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 Hellerd4c93202006-03-08 19:35:11 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 rtype = _ctypes_get_ffi_type(restype);
1130 resbuf = alloca(max(rtype->size, sizeof(ffi_arg)));
Thomas Hellerd4c93202006-03-08 19:35:11 +00001131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 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 Stinner4c2e4fa2010-09-29 10:37:16 +00001140 if (atypes[i]->type == FFI_TYPE_STRUCT
Thomas Hellerb00697e2010-06-21 16:00:31 +00001141 )
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 avalues[i] = (void *)args[i].value.p;
1143 else
1144 avalues[i] = (void *)&args[i].value;
1145 }
Thomas Hellerd4c93202006-03-08 19:35:11 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 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 Hellerd4c93202006-03-08 19:35:11 +00001153
1154#ifdef WORDS_BIGENDIAN
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 /* 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 Hellerd4c93202006-03-08 19:35:11 +00001169#endif
1170
1171#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 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 Benderskyca832332013-03-06 06:30:23 -08001182 } else
Thomas Hellerd4c93202006-03-08 19:35:11 +00001183#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 retval = GetResult(restype, resbuf, checker);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001185 cleanup:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 for (i = 0; i < argcount; ++i)
1187 Py_XDECREF(args[i].keep);
1188 return retval;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001189}
1190
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001191static int
1192_parse_voidp(PyObject *obj, void **address)
1193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 *address = PyLong_AsVoidPtr(obj);
1195 if (*address == NULL)
1196 return 0;
1197 return 1;
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001198}
1199
Thomas Hellerd4c93202006-03-08 19:35:11 +00001200#ifdef MS_WIN32
1201
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001202static const char format_error_doc[] =
Thomas Hellerd4c93202006-03-08 19:35:11 +00001203"FormatError([integer]) -> string\n\
1204\n\
1205Convert a win32 error code into a string. If the error code is not\n\
1206given, the return value of a call to GetLastError() is used.\n";
1207static PyObject *format_error(PyObject *self, PyObject *args)
1208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 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 Hellerd4c93202006-03-08 19:35:11 +00001224}
1225
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001226static const char load_library_doc[] =
Thomas Hellerd4c93202006-03-08 19:35:11 +00001227"LoadLibrary(name) -> handle\n\
1228\n\
1229Load an executable (usually a DLL), and return a handle to it.\n\
1230The handle may be used to locate exported functions in this\n\
1231module.\n";
1232static PyObject *load_library(PyObject *self, PyObject *args)
1233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 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 Wouters00ee7ba2006-08-21 19:07:27 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 name = PyUnicode_AsUnicode(nameobj);
1242 if (!name)
1243 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 hMod = LoadLibraryW(name);
1246 if (!hMod)
1247 return PyErr_SetFromWindowsErr(GetLastError());
Thomas Wouters89f507f2006-12-13 04:49:30 +00001248#ifdef _WIN64
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 return PyLong_FromVoidPtr(hMod);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001250#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 return Py_BuildValue("i", hMod);
Thomas Wouters89f507f2006-12-13 04:49:30 +00001252#endif
Thomas Hellerd4c93202006-03-08 19:35:11 +00001253}
1254
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001255static const char free_library_doc[] =
Thomas Hellerd4c93202006-03-08 19:35:11 +00001256"FreeLibrary(handle) -> void\n\
1257\n\
1258Free the handle of an executable previously loaded by LoadLibrary.\n";
1259static PyObject *free_library(PyObject *self, PyObject *args)
1260{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 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 Hellerd4c93202006-03-08 19:35:11 +00001268}
1269
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001270static const char copy_com_pointer_doc[] =
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001271"CopyComPointer(src, dst) -> HRESULT value\n";
Thomas Hellerd4c93202006-03-08 19:35:11 +00001272
1273static PyObject *
1274copy_com_pointer(PyObject *self, PyObject *args)
1275{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 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 Hellerd4c93202006-03-08 19:35:11 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 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 Hellerd4c93202006-03-08 19:35:11 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 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 Hellerd4c93202006-03-08 19:35:11 +00001296 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 Py_XDECREF(a.keep);
1298 Py_XDECREF(b.keep);
1299 return r;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001300}
1301#else
1302
1303static PyObject *py_dl_open(PyObject *self, PyObject *args)
1304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 PyObject *name, *name2;
1306 char *name_str;
1307 void * handle;
Serhiy Storchakac2f7d872016-05-04 09:44:44 +03001308#if HAVE_DECL_RTLD_LOCAL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 int mode = RTLD_NOW | RTLD_LOCAL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001310#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* cygwin doesn't define RTLD_LOCAL */
1312 int mode = RTLD_NOW;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001313#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 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 Hellerd4c93202006-03-08 19:35:11 +00001339}
1340
1341static PyObject *py_dl_close(PyObject *self, PyObject *args)
1342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 void *handle;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 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 Hellerd4c93202006-03-08 19:35:11 +00001354}
1355
1356static PyObject *py_dl_sym(PyObject *self, PyObject *args)
1357{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 char *name;
1359 void *handle;
1360 void *ptr;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 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 Hellerd4c93202006-03-08 19:35:11 +00001372}
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 */
1380static PyObject *
1381call_function(PyObject *self, PyObject *args)
1382{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 void *func;
1384 PyObject *arguments;
1385 PyObject *result;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 if (!PyArg_ParseTuple(args,
1388 "O&O!",
1389 &_parse_voidp, &func,
1390 &PyTuple_Type, &arguments))
1391 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 result = _ctypes_callproc((PPROC)func,
1394 arguments,
Thomas Hellerd4c93202006-03-08 19:35:11 +00001395#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 NULL,
1397 NULL,
Thomas Hellerd4c93202006-03-08 19:35:11 +00001398#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 0, /* flags */
1400 NULL, /* self->argtypes */
1401 NULL, /* self->restype */
1402 NULL); /* checker */
1403 return result;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001404}
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 */
1411static PyObject *
1412call_cdeclfunction(PyObject *self, PyObject *args)
1413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 void *func;
1415 PyObject *arguments;
1416 PyObject *result;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 if (!PyArg_ParseTuple(args,
1419 "O&O!",
1420 &_parse_voidp, &func,
1421 &PyTuple_Type, &arguments))
1422 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 result = _ctypes_callproc((PPROC)func,
1425 arguments,
Thomas Hellerd4c93202006-03-08 19:35:11 +00001426#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 NULL,
1428 NULL,
Thomas Hellerd4c93202006-03-08 19:35:11 +00001429#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 FUNCFLAG_CDECL, /* flags */
Eli Benderskyd867bad2013-03-06 05:45:57 -08001431 NULL, /* self->argtypes */
1432 NULL, /* self->restype */
1433 NULL); /* checker */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 return result;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001435}
1436
1437/*****************************************************************
1438 * functions
1439 */
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001440static const char sizeof_doc[] =
Thomas Hellerd4c93202006-03-08 19:35:11 +00001441"sizeof(C type) -> integer\n"
1442"sizeof(C instance) -> integer\n"
1443"Return the size in bytes of a C instance";
1444
1445static PyObject *
1446sizeof_func(PyObject *self, PyObject *obj)
1447{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 StgDictObject *dict;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 dict = PyType_stgdict(obj);
1451 if (dict)
1452 return PyLong_FromSsize_t(dict->size);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 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 Hellerd4c93202006-03-08 19:35:11 +00001459}
1460
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001461static const char alignment_doc[] =
Thomas Hellerd4c93202006-03-08 19:35:11 +00001462"alignment(C type) -> integer\n"
1463"alignment(C instance) -> integer\n"
1464"Return the alignment requirements of a C instance";
1465
1466static PyObject *
1467align_func(PyObject *self, PyObject *obj)
1468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 StgDictObject *dict;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 dict = PyType_stgdict(obj);
1472 if (dict)
1473 return PyLong_FromSsize_t(dict->align);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 dict = PyObject_stgdict(obj);
1476 if (dict)
1477 return PyLong_FromSsize_t(dict->align);
Thomas Hellerd4c93202006-03-08 19:35:11 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 PyErr_SetString(PyExc_TypeError,
1480 "no alignment info");
1481 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001482}
1483
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001484static const char byref_doc[] =
Thomas Hellerc5d01262008-06-10 15:08:51 +00001485"byref(C instance[, offset=0]) -> byref-object\n"
Thomas Hellerd4c93202006-03-08 19:35:11 +00001486"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 */
1493static PyObject *
Thomas Hellerc5d01262008-06-10 15:08:51 +00001494byref(PyObject *self, PyObject *args)
Thomas Hellerd4c93202006-03-08 19:35:11 +00001495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 PyCArgObject *parg;
1497 PyObject *obj;
1498 PyObject *pyoffset = NULL;
1499 Py_ssize_t offset = 0;
Thomas Hellerc5d01262008-06-10 15:08:51 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 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 Hellerd4c93202006-03-08 19:35:11 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 parg = PyCArgObject_new();
1517 if (parg == NULL)
1518 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 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 Hellerd4c93202006-03-08 19:35:11 +00001526}
1527
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001528static const char addressof_doc[] =
Thomas Hellerd4c93202006-03-08 19:35:11 +00001529"addressof(C instance) -> integer\n"
1530"Return the address of the C instance internal buffer";
1531
1532static PyObject *
1533addressof(PyObject *self, PyObject *obj)
1534{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 if (CDataObject_Check(obj))
1536 return PyLong_FromVoidPtr(((CDataObject *)obj)->b_ptr);
1537 PyErr_SetString(PyExc_TypeError,
1538 "invalid type");
1539 return NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001540}
1541
1542static int
1543converter(PyObject *obj, void **address)
1544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 *address = PyLong_AsVoidPtr(obj);
1546 return *address != NULL;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001547}
1548
1549static PyObject *
1550My_PyObj_FromPtr(PyObject *self, PyObject *args)
1551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 PyObject *ob;
1553 if (!PyArg_ParseTuple(args, "O&:PyObj_FromPtr", converter, &ob))
1554 return NULL;
1555 Py_INCREF(ob);
1556 return ob;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001557}
1558
1559static PyObject *
1560My_Py_INCREF(PyObject *self, PyObject *arg)
1561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 Py_INCREF(arg); /* that's what this function is for */
1563 Py_INCREF(arg); /* that for returning it */
1564 return arg;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001565}
1566
1567static PyObject *
1568My_Py_DECREF(PyObject *self, PyObject *arg)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 Py_DECREF(arg); /* that's what this function is for */
1571 Py_INCREF(arg); /* that's for returning it */
1572 return arg;
Thomas Hellerd4c93202006-03-08 19:35:11 +00001573}
1574
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001575static PyObject *
1576resize(PyObject *self, PyObject *args)
1577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 CDataObject *obj;
1579 StgDictObject *dict;
1580 Py_ssize_t size;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 if (!PyArg_ParseTuple(args,
1583 "On:resize",
1584 &obj, &size))
1585 return NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 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 Stinner706768c2014-08-16 01:03:39 +02001604 if ((size_t)size <= sizeof(obj->b_value)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 /* internal default buffer is large enough */
1606 obj->b_size = size;
1607 goto done;
1608 }
Antoine Pitrou305e1a72012-12-08 11:05:50 +01001609 if (!_CDataObject_HasExternalBuffer(obj)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 /* 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 Wouters0e3f5912006-08-11 14:57:12 +00001626 done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 Py_INCREF(Py_None);
1628 return Py_None;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001629}
1630
Thomas Heller13394e92008-02-13 20:40:44 +00001631static PyObject *
1632unpickle(PyObject *self, PyObject *args)
1633{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 PyObject *typ;
1635 PyObject *state;
1636 PyObject *result;
1637 PyObject *tmp;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001638 _Py_IDENTIFIER(__new__);
1639 _Py_IDENTIFIER(__setstate__);
Thomas Heller13394e92008-02-13 20:40:44 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 if (!PyArg_ParseTuple(args, "OO", &typ, &state))
1642 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001643 result = _PyObject_CallMethodId(typ, &PyId___new__, "O", typ);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 if (result == NULL)
1645 return NULL;
Martin v. Löwisafe55bb2011-10-09 10:38:36 +02001646 tmp = _PyObject_CallMethodId(result, &PyId___setstate__, "O", state);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 if (tmp == NULL) {
1648 Py_DECREF(result);
1649 return NULL;
1650 }
1651 Py_DECREF(tmp);
1652 return result;
Thomas Heller13394e92008-02-13 20:40:44 +00001653}
1654
Thomas Heller3071f812008-04-14 16:17:33 +00001655static PyObject *
1656POINTER(PyObject *self, PyObject *cls)
1657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658 PyObject *result;
1659 PyTypeObject *typ;
1660 PyObject *key;
1661 char *buf;
Thomas Heller3071f812008-04-14 16:17:33 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 result = PyDict_GetItem(_ctypes_ptrtype_cache, cls);
1664 if (result) {
1665 Py_INCREF(result);
1666 return result;
1667 }
1668 if (PyUnicode_CheckExact(cls)) {
Serhiy Storchaka144f77a2016-11-20 08:47:21 +02001669 const char *name = PyUnicode_AsUTF8(cls);
1670 if (name == NULL)
1671 return NULL;
R David Murray817905b2014-10-12 13:54:48 -04001672 buf = PyMem_Malloc(strlen(name) + 3 + 1);
1673 if (buf == NULL)
1674 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 sprintf(buf, "LP_%s", name);
1676 result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
1677 "s(O){}",
1678 buf,
1679 &PyCPointer_Type);
R David Murray817905b2014-10-12 13:54:48 -04001680 PyMem_Free(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 if (result == NULL)
1682 return result;
1683 key = PyLong_FromVoidPtr(result);
Serhiy Storchakaadef6462016-06-16 22:08:46 +03001684 if (key == NULL) {
1685 Py_DECREF(result);
1686 return NULL;
1687 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 } else if (PyType_Check(cls)) {
1689 typ = (PyTypeObject *)cls;
R David Murray817905b2014-10-12 13:54:48 -04001690 buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1);
1691 if (buf == NULL)
1692 return PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 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 Murray817905b2014-10-12 13:54:48 -04001699 PyMem_Free(buf);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 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 Heller3071f812008-04-14 16:17:33 +00001715}
1716
1717static PyObject *
1718pointer(PyObject *self, PyObject *arg)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 PyObject *result;
1721 PyObject *typ;
Thomas Heller3071f812008-04-14 16:17:33 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 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 Heller3071f812008-04-14 16:17:33 +00001732}
1733
Thomas Hellerb041fda2008-04-30 17:11:46 +00001734static PyObject *
1735buffer_info(PyObject *self, PyObject *arg)
1736{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 StgDictObject *dict = PyType_stgdict(arg);
1738 PyObject *shape;
1739 Py_ssize_t i;
Thomas Hellerb041fda2008-04-30 17:11:46 +00001740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 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 Hellerb041fda2008-04-30 17:11:46 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 if (PyErr_Occurred()) {
1755 Py_DECREF(shape);
1756 return NULL;
1757 }
1758 return Py_BuildValue("siN", dict->format, dict->ndim, shape);
Thomas Hellerb041fda2008-04-30 17:11:46 +00001759}
1760
Thomas Heller34596a92009-04-24 20:50:00 +00001761PyMethodDef _ctypes_module_methods[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 {"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 Hellerd4c93202006-03-08 19:35:11 +00001769#ifdef MS_WIN32
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 {"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 Pitrouf95a1b32010-05-09 15:52:27 +00001776 {"_check_HRESULT", check_hresult, METH_VARARGS},
Thomas Hellerd4c93202006-03-08 19:35:11 +00001777#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 {"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 Hellerd4c93202006-03-08 19:35:11 +00001782#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 {"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 Hellerd4c93202006-03-08 19:35:11 +00001793};
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*/