Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1 | /* |
| 2 | ToDo: |
| 3 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 4 | Get rid of the checker (and also the converters) field in PyCFuncPtrObject and |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5 | StgDictObject, and replace them by slot functions in StgDictObject. |
| 6 | |
| 7 | think about a buffer-like object (memory? bytes?) |
| 8 | |
| 9 | Should POINTER(c_char) and POINTER(c_wchar) have a .value property? |
| 10 | What about c_char and c_wchar arrays then? |
| 11 | |
| 12 | Add from_mmap, from_file, from_string metaclass methods. |
| 13 | |
| 14 | Maybe we can get away with from_file (calls read) and with a from_buffer |
| 15 | method? |
| 16 | |
| 17 | And what about the to_mmap, to_file, to_str(?) methods? They would clobber |
| 18 | the namespace, probably. So, functions instead? And we already have memmove... |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 23 | Name methods, members, getsets |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 24 | ============================================================================== |
| 25 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 26 | PyCStructType_Type __new__(), from_address(), __mul__(), from_param() |
| 27 | UnionType_Type __new__(), from_address(), __mul__(), from_param() |
| 28 | PyCPointerType_Type __new__(), from_address(), __mul__(), from_param(), set_type() |
| 29 | PyCArrayType_Type __new__(), from_address(), __mul__(), from_param() |
| 30 | PyCSimpleType_Type __new__(), from_address(), __mul__(), from_param() |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 31 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 32 | PyCData_Type |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 33 | Struct_Type __new__(), __init__() |
| 34 | PyCPointer_Type __new__(), __init__(), _as_parameter_, contents |
| 35 | PyCArray_Type __new__(), __init__(), _as_parameter_, __get/setitem__(), __len__() |
| 36 | Simple_Type __new__(), __init__(), _as_parameter_ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 37 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 38 | PyCField_Type |
| 39 | PyCStgDict_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 40 | |
| 41 | ============================================================================== |
| 42 | |
| 43 | class methods |
| 44 | ------------- |
| 45 | |
| 46 | It has some similarity to the byref() construct compared to pointer() |
| 47 | from_address(addr) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | - construct an instance from a given memory block (sharing this memory block) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 49 | |
| 50 | from_param(obj) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | - typecheck and convert a Python object into a C function call parameter |
| 52 | the result may be an instance of the type, or an integer or tuple |
| 53 | (typecode, value[, obj]) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 54 | |
| 55 | instance methods/properties |
| 56 | --------------------------- |
| 57 | |
| 58 | _as_parameter_ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 59 | - convert self into a C function call parameter |
| 60 | This is either an integer, or a 3-tuple (typecode, value, obj) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 61 | |
| 62 | functions |
| 63 | --------- |
| 64 | |
| 65 | sizeof(cdata) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | - return the number of bytes the buffer contains |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 67 | |
| 68 | sizeof(ctype) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | - return the number of bytes the buffer of an instance would contain |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 70 | |
| 71 | byref(cdata) |
| 72 | |
| 73 | addressof(cdata) |
| 74 | |
| 75 | pointer(cdata) |
| 76 | |
| 77 | POINTER(ctype) |
| 78 | |
| 79 | bytes(cdata) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | - return the buffer contents as a sequence of bytes (which is currently a string) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 81 | |
| 82 | */ |
| 83 | |
| 84 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 85 | * PyCStgDict_Type |
| 86 | * PyCStructType_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 87 | * UnionType_Type |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 88 | * PyCPointerType_Type |
| 89 | * PyCArrayType_Type |
| 90 | * PyCSimpleType_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 91 | * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 92 | * PyCData_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 93 | * Struct_Type |
| 94 | * Union_Type |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 95 | * PyCArray_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 96 | * Simple_Type |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 97 | * PyCPointer_Type |
| 98 | * PyCField_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 99 | * |
| 100 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 101 | |
Christian Heimes | f6cd967 | 2008-03-26 13:45:42 +0000 | [diff] [blame] | 102 | #define PY_SSIZE_T_CLEAN |
| 103 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 104 | #include "Python.h" |
| 105 | #include "structmember.h" |
| 106 | |
| 107 | #include <ffi.h> |
| 108 | #ifdef MS_WIN32 |
| 109 | #include <windows.h> |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 110 | #include <malloc.h> |
| 111 | #ifndef IS_INTRESOURCE |
| 112 | #define IS_INTRESOURCE(x) (((size_t)(x) >> 16) == 0) |
| 113 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 114 | # ifdef _WIN32_WCE |
| 115 | /* Unlike desktop Windows, WinCE has both W and A variants of |
| 116 | GetProcAddress, but the default W version is not what we want */ |
| 117 | # undef GetProcAddress |
| 118 | # define GetProcAddress GetProcAddressA |
| 119 | # endif |
| 120 | #else |
| 121 | #include "ctypes_dlfcn.h" |
| 122 | #endif |
| 123 | #include "ctypes.h" |
| 124 | |
| 125 | PyObject *PyExc_ArgError; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 126 | |
| 127 | /* This dict maps ctypes types to POINTER types */ |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 128 | PyObject *_ctypes_ptrtype_cache; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 129 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 130 | static PyTypeObject Simple_Type; |
| 131 | |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 132 | /* a callable object used for unpickling */ |
| 133 | static PyObject *_unpickle; |
| 134 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 137 | /****************************************************************/ |
| 138 | |
| 139 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | PyObject_HEAD |
| 141 | PyObject *key; |
| 142 | PyObject *dict; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 143 | } DictRemoverObject; |
| 144 | |
| 145 | static void |
| 146 | _DictRemover_dealloc(PyObject *_self) |
| 147 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 148 | DictRemoverObject *self = (DictRemoverObject *)_self; |
| 149 | Py_XDECREF(self->key); |
| 150 | Py_XDECREF(self->dict); |
| 151 | Py_TYPE(self)->tp_free(_self); |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static PyObject * |
| 155 | _DictRemover_call(PyObject *_self, PyObject *args, PyObject *kw) |
| 156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | DictRemoverObject *self = (DictRemoverObject *)_self; |
| 158 | if (self->key && self->dict) { |
| 159 | if (-1 == PyDict_DelItem(self->dict, self->key)) |
| 160 | /* XXX Error context */ |
| 161 | PyErr_WriteUnraisable(Py_None); |
| 162 | Py_DECREF(self->key); |
| 163 | self->key = NULL; |
| 164 | Py_DECREF(self->dict); |
| 165 | self->dict = NULL; |
| 166 | } |
| 167 | Py_INCREF(Py_None); |
| 168 | return Py_None; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static PyTypeObject DictRemover_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | PyVarObject_HEAD_INIT(NULL, 0) |
| 173 | "_ctypes.DictRemover", /* tp_name */ |
| 174 | sizeof(DictRemoverObject), /* tp_basicsize */ |
| 175 | 0, /* tp_itemsize */ |
| 176 | _DictRemover_dealloc, /* tp_dealloc */ |
| 177 | 0, /* tp_print */ |
| 178 | 0, /* tp_getattr */ |
| 179 | 0, /* tp_setattr */ |
| 180 | 0, /* tp_reserved */ |
| 181 | 0, /* tp_repr */ |
| 182 | 0, /* tp_as_number */ |
| 183 | 0, /* tp_as_sequence */ |
| 184 | 0, /* tp_as_mapping */ |
| 185 | 0, /* tp_hash */ |
| 186 | _DictRemover_call, /* tp_call */ |
| 187 | 0, /* tp_str */ |
| 188 | 0, /* tp_getattro */ |
| 189 | 0, /* tp_setattro */ |
| 190 | 0, /* tp_as_buffer */ |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 191 | /* XXX should participate in GC? */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 192 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 193 | "deletes a key from a dictionary", /* tp_doc */ |
| 194 | 0, /* tp_traverse */ |
| 195 | 0, /* tp_clear */ |
| 196 | 0, /* tp_richcompare */ |
| 197 | 0, /* tp_weaklistoffset */ |
| 198 | 0, /* tp_iter */ |
| 199 | 0, /* tp_iternext */ |
| 200 | 0, /* tp_methods */ |
| 201 | 0, /* tp_members */ |
| 202 | 0, /* tp_getset */ |
| 203 | 0, /* tp_base */ |
| 204 | 0, /* tp_dict */ |
| 205 | 0, /* tp_descr_get */ |
| 206 | 0, /* tp_descr_set */ |
| 207 | 0, /* tp_dictoffset */ |
| 208 | 0, /* tp_init */ |
| 209 | 0, /* tp_alloc */ |
| 210 | 0, /* tp_new */ |
| 211 | 0, /* tp_free */ |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | int |
| 215 | PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item) |
| 216 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 217 | PyObject *obj; |
| 218 | DictRemoverObject *remover; |
| 219 | PyObject *proxy; |
| 220 | int result; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 221 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 222 | obj = PyObject_CallObject((PyObject *)&DictRemover_Type, NULL); |
| 223 | if (obj == NULL) |
| 224 | return -1; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 226 | remover = (DictRemoverObject *)obj; |
| 227 | assert(remover->key == NULL); |
| 228 | assert(remover->dict == NULL); |
| 229 | Py_INCREF(key); |
| 230 | remover->key = key; |
| 231 | Py_INCREF(dict); |
| 232 | remover->dict = dict; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | proxy = PyWeakref_NewProxy(item, obj); |
| 235 | Py_DECREF(obj); |
| 236 | if (proxy == NULL) |
| 237 | return -1; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 238 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | result = PyDict_SetItem(dict, key, proxy); |
| 240 | Py_DECREF(proxy); |
| 241 | return result; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | PyObject * |
| 245 | PyDict_GetItemProxy(PyObject *dict, PyObject *key) |
| 246 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | PyObject *result; |
| 248 | PyObject *item = PyDict_GetItem(dict, key); |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | if (item == NULL) |
| 251 | return NULL; |
| 252 | if (!PyWeakref_CheckProxy(item)) |
| 253 | return item; |
| 254 | result = PyWeakref_GET_OBJECT(item); |
| 255 | if (result == Py_None) |
| 256 | return NULL; |
| 257 | return result; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 260 | /******************************************************************/ |
| 261 | /* |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 262 | Allocate a memory block for a pep3118 format string, copy prefix (if |
| 263 | non-null) and suffix into it. Returns NULL on failure, with the error |
| 264 | indicator set. If called with a suffix of NULL the error indicator must |
| 265 | already be set. |
| 266 | */ |
| 267 | char * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 268 | _ctypes_alloc_format_string(const char *prefix, const char *suffix) |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 269 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 270 | size_t len; |
| 271 | char *result; |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | if (suffix == NULL) { |
| 274 | assert(PyErr_Occurred()); |
| 275 | return NULL; |
| 276 | } |
| 277 | len = strlen(suffix); |
| 278 | if (prefix) |
| 279 | len += strlen(prefix); |
| 280 | result = PyMem_Malloc(len + 1); |
| 281 | if (result == NULL) |
| 282 | return NULL; |
| 283 | if (prefix) |
| 284 | strcpy(result, prefix); |
| 285 | else |
| 286 | result[0] = '\0'; |
| 287 | strcat(result, suffix); |
| 288 | return result; |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 292 | PyCStructType_Type - a meta type/class. Creating a new class using this one as |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 293 | __metaclass__ will call the contructor StructUnionType_new. It replaces the |
| 294 | tp_dict member with a new instance of StgDict, and initializes the C |
| 295 | accessible fields somehow. |
| 296 | */ |
| 297 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 298 | static PyCArgObject * |
| 299 | StructUnionType_paramfunc(CDataObject *self) |
| 300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | PyCArgObject *parg; |
| 302 | StgDictObject *stgdict; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 304 | parg = PyCArgObject_new(); |
| 305 | if (parg == NULL) |
| 306 | return NULL; |
| 307 | |
| 308 | parg->tag = 'V'; |
| 309 | stgdict = PyObject_stgdict((PyObject *)self); |
| 310 | assert(stgdict); /* Cannot be NULL for structure/union instances */ |
| 311 | parg->pffi_type = &stgdict->ffi_type_pointer; |
| 312 | /* For structure parameters (by value), parg->value doesn't contain the structure |
| 313 | data itself, instead parg->value.p *points* to the structure's data |
| 314 | See also _ctypes.c, function _call_function_pointer(). |
| 315 | */ |
| 316 | parg->value.p = self->b_ptr; |
| 317 | parg->size = self->b_size; |
| 318 | Py_INCREF(self); |
| 319 | parg->obj = (PyObject *)self; |
| 320 | return parg; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 323 | static PyObject * |
| 324 | StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct) |
| 325 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | PyTypeObject *result; |
| 327 | PyObject *fields; |
| 328 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 330 | /* create the new instance (which is a class, |
| 331 | since we are a metatype!) */ |
| 332 | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
| 333 | if (!result) |
| 334 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | /* keep this for bw compatibility */ |
| 337 | if (PyDict_GetItemString(result->tp_dict, "_abstract_")) |
| 338 | return (PyObject *)result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | dict = (StgDictObject *)PyObject_CallObject((PyObject *)&PyCStgDict_Type, NULL); |
| 341 | if (!dict) { |
| 342 | Py_DECREF(result); |
| 343 | return NULL; |
| 344 | } |
| 345 | /* replace the class dict by our updated stgdict, which holds info |
| 346 | about storage requirements of the instances */ |
| 347 | if (-1 == PyDict_Update((PyObject *)dict, result->tp_dict)) { |
| 348 | Py_DECREF(result); |
| 349 | Py_DECREF((PyObject *)dict); |
| 350 | return NULL; |
| 351 | } |
| 352 | Py_DECREF(result->tp_dict); |
| 353 | result->tp_dict = (PyObject *)dict; |
| 354 | dict->format = _ctypes_alloc_format_string(NULL, "B"); |
| 355 | if (dict->format == NULL) { |
| 356 | Py_DECREF(result); |
| 357 | return NULL; |
| 358 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 359 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 360 | dict->paramfunc = StructUnionType_paramfunc; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 362 | fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); |
| 363 | if (!fields) { |
| 364 | StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (basedict == NULL) |
| 367 | return (PyObject *)result; |
| 368 | /* copy base dict */ |
| 369 | if (-1 == PyCStgDict_clone(dict, basedict)) { |
| 370 | Py_DECREF(result); |
| 371 | return NULL; |
| 372 | } |
| 373 | dict->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass dict */ |
| 374 | basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */ |
| 375 | return (PyObject *)result; |
| 376 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 378 | if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) { |
| 379 | Py_DECREF(result); |
| 380 | return NULL; |
| 381 | } |
| 382 | return (PyObject *)result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 386 | PyCStructType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 387 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 388 | return StructUnionType_new(type, args, kwds, 1); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static PyObject * |
| 392 | UnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 393 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 394 | return StructUnionType_new(type, args, kwds, 0); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | static char from_address_doc[] = |
| 398 | "C.from_address(integer) -> C instance\naccess a C instance at the specified address"; |
| 399 | |
| 400 | static PyObject * |
| 401 | CDataType_from_address(PyObject *type, PyObject *value) |
| 402 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 403 | void *buf; |
| 404 | if (!PyLong_Check(value)) { |
| 405 | PyErr_SetString(PyExc_TypeError, |
| 406 | "integer expected"); |
| 407 | return NULL; |
| 408 | } |
| 409 | buf = (void *)PyLong_AsVoidPtr(value); |
| 410 | if (PyErr_Occurred()) |
| 411 | return NULL; |
| 412 | return PyCData_AtAddress(type, buf); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 415 | static char from_buffer_doc[] = |
| 416 | "C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a writeable buffer"; |
| 417 | |
| 418 | static int |
| 419 | KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep); |
| 420 | |
| 421 | static PyObject * |
| 422 | CDataType_from_buffer(PyObject *type, PyObject *args) |
| 423 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 424 | void *buffer; |
| 425 | Py_ssize_t buffer_len; |
| 426 | Py_ssize_t offset = 0; |
| 427 | PyObject *obj, *result; |
| 428 | StgDictObject *dict = PyType_stgdict(type); |
| 429 | assert (dict); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 430 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | if (!PyArg_ParseTuple(args, |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 432 | #if (PY_VERSION_HEX < 0x02050000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | "O|i:from_buffer", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 434 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 435 | "O|n:from_buffer", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 436 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | &obj, &offset)) |
| 438 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 439 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | if (-1 == PyObject_AsWriteBuffer(obj, &buffer, &buffer_len)) |
| 441 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 442 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | if (offset < 0) { |
| 444 | PyErr_SetString(PyExc_ValueError, |
| 445 | "offset cannot be negative"); |
| 446 | return NULL; |
| 447 | } |
| 448 | if (dict->size > buffer_len - offset) { |
| 449 | PyErr_Format(PyExc_ValueError, |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 450 | #if (PY_VERSION_HEX < 0x02050000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 451 | "Buffer size too small (%d instead of at least %d bytes)", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 452 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 453 | "Buffer size too small (%zd instead of at least %zd bytes)", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 454 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 455 | buffer_len, dict->size + offset); |
| 456 | return NULL; |
| 457 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 458 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 459 | result = PyCData_AtAddress(type, (char *)buffer + offset); |
| 460 | if (result == NULL) |
| 461 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | Py_INCREF(obj); |
| 464 | if (-1 == KeepRef((CDataObject *)result, -1, obj)) { |
| 465 | Py_DECREF(result); |
| 466 | return NULL; |
| 467 | } |
| 468 | return result; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static char from_buffer_copy_doc[] = |
| 472 | "C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer"; |
| 473 | |
| 474 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 475 | GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 476 | |
| 477 | static PyObject * |
| 478 | CDataType_from_buffer_copy(PyObject *type, PyObject *args) |
| 479 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | const void *buffer; |
| 481 | Py_ssize_t buffer_len; |
| 482 | Py_ssize_t offset = 0; |
| 483 | PyObject *obj, *result; |
| 484 | StgDictObject *dict = PyType_stgdict(type); |
| 485 | assert (dict); |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 487 | if (!PyArg_ParseTuple(args, |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 488 | #if (PY_VERSION_HEX < 0x02050000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | "O|i:from_buffer", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 490 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | "O|n:from_buffer", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 492 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 493 | &obj, &offset)) |
| 494 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | if (-1 == PyObject_AsReadBuffer(obj, (const void**)&buffer, &buffer_len)) |
| 497 | return NULL; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | if (offset < 0) { |
| 500 | PyErr_SetString(PyExc_ValueError, |
| 501 | "offset cannot be negative"); |
| 502 | return NULL; |
| 503 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | if (dict->size > buffer_len - offset) { |
| 506 | PyErr_Format(PyExc_ValueError, |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 507 | #if (PY_VERSION_HEX < 0x02050000) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | "Buffer size too small (%d instead of at least %d bytes)", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 509 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 510 | "Buffer size too small (%zd instead of at least %zd bytes)", |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 511 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | buffer_len, dict->size + offset); |
| 513 | return NULL; |
| 514 | } |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 515 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 516 | result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL); |
| 517 | if (result == NULL) |
| 518 | return NULL; |
| 519 | memcpy(((CDataObject *)result)->b_ptr, |
| 520 | (char *)buffer+offset, dict->size); |
| 521 | return result; |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 522 | } |
| 523 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 524 | static char in_dll_doc[] = |
| 525 | "C.in_dll(dll, name) -> C instance\naccess a C instance in a dll"; |
| 526 | |
| 527 | static PyObject * |
| 528 | CDataType_in_dll(PyObject *type, PyObject *args) |
| 529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 530 | PyObject *dll; |
| 531 | char *name; |
| 532 | PyObject *obj; |
| 533 | void *handle; |
| 534 | void *address; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name)) |
| 537 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 538 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 539 | obj = PyObject_GetAttrString(dll, "_handle"); |
| 540 | if (!obj) |
| 541 | return NULL; |
| 542 | if (!PyLong_Check(obj)) { |
| 543 | PyErr_SetString(PyExc_TypeError, |
| 544 | "the _handle attribute of the second argument must be an integer"); |
| 545 | Py_DECREF(obj); |
| 546 | return NULL; |
| 547 | } |
| 548 | handle = (void *)PyLong_AsVoidPtr(obj); |
| 549 | Py_DECREF(obj); |
| 550 | if (PyErr_Occurred()) { |
| 551 | PyErr_SetString(PyExc_ValueError, |
| 552 | "could not convert the _handle attribute to a pointer"); |
| 553 | return NULL; |
| 554 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 555 | |
| 556 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 557 | address = (void *)GetProcAddress(handle, name); |
| 558 | if (!address) { |
| 559 | PyErr_Format(PyExc_ValueError, |
| 560 | "symbol '%s' not found", |
| 561 | name); |
| 562 | return NULL; |
| 563 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 564 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | address = (void *)ctypes_dlsym(handle, name); |
| 566 | if (!address) { |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 567 | #ifdef __CYGWIN__ |
| 568 | /* dlerror() isn't very helpful on cygwin */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | PyErr_Format(PyExc_ValueError, |
| 570 | "symbol '%s' not found (%s) ", |
| 571 | name); |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 572 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | PyErr_SetString(PyExc_ValueError, ctypes_dlerror()); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 574 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 575 | return NULL; |
| 576 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 577 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | return PyCData_AtAddress(type, address); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static char from_param_doc[] = |
| 582 | "Convert a Python object into a function call parameter."; |
| 583 | |
| 584 | static PyObject * |
| 585 | CDataType_from_param(PyObject *type, PyObject *value) |
| 586 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 587 | PyObject *as_parameter; |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 588 | int res = PyObject_IsInstance(value, type); |
| 589 | if (res == -1) |
| 590 | return NULL; |
| 591 | if (res) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 592 | Py_INCREF(value); |
| 593 | return value; |
| 594 | } |
| 595 | if (PyCArg_CheckExact(value)) { |
| 596 | PyCArgObject *p = (PyCArgObject *)value; |
| 597 | PyObject *ob = p->obj; |
| 598 | const char *ob_name; |
| 599 | StgDictObject *dict; |
| 600 | dict = PyType_stgdict(type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 601 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 602 | /* If we got a PyCArgObject, we must check if the object packed in it |
| 603 | is an instance of the type's dict->proto */ |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 604 | if(dict && ob) { |
| 605 | res = PyObject_IsInstance(ob, dict->proto); |
| 606 | if (res == -1) |
| 607 | return NULL; |
| 608 | if (res) { |
| 609 | Py_INCREF(value); |
| 610 | return value; |
| 611 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 612 | } |
| 613 | ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???"; |
| 614 | PyErr_Format(PyExc_TypeError, |
| 615 | "expected %s instance instead of pointer to %s", |
| 616 | ((PyTypeObject *)type)->tp_name, ob_name); |
| 617 | return NULL; |
| 618 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
| 621 | if (as_parameter) { |
| 622 | value = CDataType_from_param(type, as_parameter); |
| 623 | Py_DECREF(as_parameter); |
| 624 | return value; |
| 625 | } |
| 626 | PyErr_Format(PyExc_TypeError, |
| 627 | "expected %s instance instead of %s", |
| 628 | ((PyTypeObject *)type)->tp_name, |
| 629 | Py_TYPE(value)->tp_name); |
| 630 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | static PyMethodDef CDataType_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | { "from_param", CDataType_from_param, METH_O, from_param_doc }, |
| 635 | { "from_address", CDataType_from_address, METH_O, from_address_doc }, |
| 636 | { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, }, |
| 637 | { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, }, |
| 638 | { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc }, |
| 639 | { NULL, NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 640 | }; |
| 641 | |
| 642 | static PyObject * |
Thomas Heller | fe8f862 | 2006-03-14 19:53:09 +0000 | [diff] [blame] | 643 | CDataType_repeat(PyObject *self, Py_ssize_t length) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 644 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | if (length < 0) |
| 646 | return PyErr_Format(PyExc_ValueError, |
| 647 | "Array length must be >= 0, not %zd", |
| 648 | length); |
| 649 | return PyCArrayType_from_ctype(self, length); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | static PySequenceMethods CDataType_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 653 | 0, /* inquiry sq_length; */ |
| 654 | 0, /* binaryfunc sq_concat; */ |
| 655 | CDataType_repeat, /* intargfunc sq_repeat; */ |
| 656 | 0, /* intargfunc sq_item; */ |
| 657 | 0, /* intintargfunc sq_slice; */ |
| 658 | 0, /* intobjargproc sq_ass_item; */ |
| 659 | 0, /* intintobjargproc sq_ass_slice; */ |
| 660 | 0, /* objobjproc sq_contains; */ |
| 661 | |
| 662 | 0, /* binaryfunc sq_inplace_concat; */ |
| 663 | 0, /* intargfunc sq_inplace_repeat; */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 664 | }; |
| 665 | |
| 666 | static int |
| 667 | CDataType_clear(PyTypeObject *self) |
| 668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 669 | StgDictObject *dict = PyType_stgdict((PyObject *)self); |
| 670 | if (dict) |
| 671 | Py_CLEAR(dict->proto); |
| 672 | return PyType_Type.tp_clear((PyObject *)self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static int |
| 676 | CDataType_traverse(PyTypeObject *self, visitproc visit, void *arg) |
| 677 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 678 | StgDictObject *dict = PyType_stgdict((PyObject *)self); |
| 679 | if (dict) |
| 680 | Py_VISIT(dict->proto); |
| 681 | return PyType_Type.tp_traverse((PyObject *)self, visit, arg); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 685 | PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 686 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 687 | /* XXX Should we disallow deleting _fields_? */ |
| 688 | if (-1 == PyType_Type.tp_setattro(self, key, value)) |
| 689 | return -1; |
| 690 | |
| 691 | if (value && PyUnicode_Check(key) && |
| 692 | /* XXX struni _PyUnicode_AsString can fail (also in other places)! */ |
| 693 | 0 == strcmp(_PyUnicode_AsString(key), "_fields_")) |
| 694 | return PyCStructUnionType_update_stgdict(self, value, 1); |
| 695 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | |
| 699 | static int |
| 700 | UnionType_setattro(PyObject *self, PyObject *key, PyObject *value) |
| 701 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 702 | /* XXX Should we disallow deleting _fields_? */ |
| 703 | if (-1 == PyObject_GenericSetAttr(self, key, value)) |
| 704 | return -1; |
| 705 | |
| 706 | if (PyUnicode_Check(key) && |
| 707 | 0 == strcmp(_PyUnicode_AsString(key), "_fields_")) |
| 708 | return PyCStructUnionType_update_stgdict(self, value, 0); |
| 709 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 713 | PyTypeObject PyCStructType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 714 | PyVarObject_HEAD_INIT(NULL, 0) |
| 715 | "_ctypes.PyCStructType", /* tp_name */ |
| 716 | 0, /* tp_basicsize */ |
| 717 | 0, /* tp_itemsize */ |
| 718 | 0, /* tp_dealloc */ |
| 719 | 0, /* tp_print */ |
| 720 | 0, /* tp_getattr */ |
| 721 | 0, /* tp_setattr */ |
| 722 | 0, /* tp_reserved */ |
| 723 | 0, /* tp_repr */ |
| 724 | 0, /* tp_as_number */ |
| 725 | &CDataType_as_sequence, /* tp_as_sequence */ |
| 726 | 0, /* tp_as_mapping */ |
| 727 | 0, /* tp_hash */ |
| 728 | 0, /* tp_call */ |
| 729 | 0, /* tp_str */ |
| 730 | 0, /* tp_getattro */ |
| 731 | PyCStructType_setattro, /* tp_setattro */ |
| 732 | 0, /* tp_as_buffer */ |
| 733 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 734 | "metatype for the CData Objects", /* tp_doc */ |
| 735 | (traverseproc)CDataType_traverse, /* tp_traverse */ |
| 736 | (inquiry)CDataType_clear, /* tp_clear */ |
| 737 | 0, /* tp_richcompare */ |
| 738 | 0, /* tp_weaklistoffset */ |
| 739 | 0, /* tp_iter */ |
| 740 | 0, /* tp_iternext */ |
| 741 | CDataType_methods, /* tp_methods */ |
| 742 | 0, /* tp_members */ |
| 743 | 0, /* tp_getset */ |
| 744 | 0, /* tp_base */ |
| 745 | 0, /* tp_dict */ |
| 746 | 0, /* tp_descr_get */ |
| 747 | 0, /* tp_descr_set */ |
| 748 | 0, /* tp_dictoffset */ |
| 749 | 0, /* tp_init */ |
| 750 | 0, /* tp_alloc */ |
| 751 | PyCStructType_new, /* tp_new */ |
| 752 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 753 | }; |
| 754 | |
| 755 | static PyTypeObject UnionType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 756 | PyVarObject_HEAD_INIT(NULL, 0) |
| 757 | "_ctypes.UnionType", /* tp_name */ |
| 758 | 0, /* tp_basicsize */ |
| 759 | 0, /* tp_itemsize */ |
| 760 | 0, /* tp_dealloc */ |
| 761 | 0, /* tp_print */ |
| 762 | 0, /* tp_getattr */ |
| 763 | 0, /* tp_setattr */ |
| 764 | 0, /* tp_reserved */ |
| 765 | 0, /* tp_repr */ |
| 766 | 0, /* tp_as_number */ |
| 767 | &CDataType_as_sequence, /* tp_as_sequence */ |
| 768 | 0, /* tp_as_mapping */ |
| 769 | 0, /* tp_hash */ |
| 770 | 0, /* tp_call */ |
| 771 | 0, /* tp_str */ |
| 772 | 0, /* tp_getattro */ |
| 773 | UnionType_setattro, /* tp_setattro */ |
| 774 | 0, /* tp_as_buffer */ |
| 775 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 776 | "metatype for the CData Objects", /* tp_doc */ |
| 777 | (traverseproc)CDataType_traverse, /* tp_traverse */ |
| 778 | (inquiry)CDataType_clear, /* tp_clear */ |
| 779 | 0, /* tp_richcompare */ |
| 780 | 0, /* tp_weaklistoffset */ |
| 781 | 0, /* tp_iter */ |
| 782 | 0, /* tp_iternext */ |
| 783 | CDataType_methods, /* tp_methods */ |
| 784 | 0, /* tp_members */ |
| 785 | 0, /* tp_getset */ |
| 786 | 0, /* tp_base */ |
| 787 | 0, /* tp_dict */ |
| 788 | 0, /* tp_descr_get */ |
| 789 | 0, /* tp_descr_set */ |
| 790 | 0, /* tp_dictoffset */ |
| 791 | 0, /* tp_init */ |
| 792 | 0, /* tp_alloc */ |
| 793 | UnionType_new, /* tp_new */ |
| 794 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 795 | }; |
| 796 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 798 | /******************************************************************/ |
| 799 | |
| 800 | /* |
| 801 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 802 | The PyCPointerType_Type metaclass must ensure that the subclass of Pointer can be |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 803 | created. It must check for a _type_ attribute in the class. Since are no |
| 804 | runtime created properties, a CField is probably *not* needed ? |
| 805 | |
| 806 | class IntPointer(Pointer): |
| 807 | _type_ = "i" |
| 808 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 809 | The PyCPointer_Type provides the functionality: a contents method/property, a |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 810 | size property/method, and the sequence protocol. |
| 811 | |
| 812 | */ |
| 813 | |
| 814 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 815 | PyCPointerType_SetProto(StgDictObject *stgdict, PyObject *proto) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 816 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 817 | if (!proto || !PyType_Check(proto)) { |
| 818 | PyErr_SetString(PyExc_TypeError, |
| 819 | "_type_ must be a type"); |
| 820 | return -1; |
| 821 | } |
| 822 | if (!PyType_stgdict(proto)) { |
| 823 | PyErr_SetString(PyExc_TypeError, |
| 824 | "_type_ must have storage info"); |
| 825 | return -1; |
| 826 | } |
| 827 | Py_INCREF(proto); |
| 828 | Py_XDECREF(stgdict->proto); |
| 829 | stgdict->proto = proto; |
| 830 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 833 | static PyCArgObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 834 | PyCPointerType_paramfunc(CDataObject *self) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 835 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 836 | PyCArgObject *parg; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 838 | parg = PyCArgObject_new(); |
| 839 | if (parg == NULL) |
| 840 | return NULL; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 842 | parg->tag = 'P'; |
| 843 | parg->pffi_type = &ffi_type_pointer; |
| 844 | Py_INCREF(self); |
| 845 | parg->obj = (PyObject *)self; |
| 846 | parg->value.p = *(void **)self->b_ptr; |
| 847 | return parg; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 850 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 851 | PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 853 | PyTypeObject *result; |
| 854 | StgDictObject *stgdict; |
| 855 | PyObject *proto; |
| 856 | PyObject *typedict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 857 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | typedict = PyTuple_GetItem(args, 2); |
| 859 | if (!typedict) |
| 860 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 861 | /* |
| 862 | stgdict items size, align, length contain info about pointers itself, |
| 863 | stgdict->proto has info about the pointed to type! |
| 864 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 865 | stgdict = (StgDictObject *)PyObject_CallObject( |
| 866 | (PyObject *)&PyCStgDict_Type, NULL); |
| 867 | if (!stgdict) |
| 868 | return NULL; |
| 869 | stgdict->size = sizeof(void *); |
| 870 | stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; |
| 871 | stgdict->length = 1; |
| 872 | stgdict->ffi_type_pointer = ffi_type_pointer; |
| 873 | stgdict->paramfunc = PyCPointerType_paramfunc; |
| 874 | stgdict->flags |= TYPEFLAG_ISPOINTER; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */ |
| 877 | if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) { |
| 878 | Py_DECREF((PyObject *)stgdict); |
| 879 | return NULL; |
| 880 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 881 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | if (proto) { |
| 883 | StgDictObject *itemdict = PyType_stgdict(proto); |
| 884 | assert(itemdict); |
| 885 | /* If itemdict->format is NULL, then this is a pointer to an |
| 886 | incomplete type. We create a generic format string |
| 887 | 'pointer to bytes' in this case. XXX Better would be to |
| 888 | fix the format string later... |
| 889 | */ |
| 890 | stgdict->format = _ctypes_alloc_format_string("&", |
| 891 | itemdict->format ? itemdict->format : "B"); |
| 892 | if (stgdict->format == NULL) { |
| 893 | Py_DECREF((PyObject *)stgdict); |
| 894 | return NULL; |
| 895 | } |
| 896 | } |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | /* create the new instance (which is a class, |
| 899 | since we are a metatype!) */ |
| 900 | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
| 901 | if (result == NULL) { |
| 902 | Py_DECREF((PyObject *)stgdict); |
| 903 | return NULL; |
| 904 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 906 | /* replace the class dict by our updated spam dict */ |
| 907 | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
| 908 | Py_DECREF(result); |
| 909 | Py_DECREF((PyObject *)stgdict); |
| 910 | return NULL; |
| 911 | } |
| 912 | Py_DECREF(result->tp_dict); |
| 913 | result->tp_dict = (PyObject *)stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 915 | return (PyObject *)result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | |
| 919 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 920 | PyCPointerType_set_type(PyTypeObject *self, PyObject *type) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 921 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 922 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 923 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 924 | dict = PyType_stgdict((PyObject *)self); |
| 925 | assert(dict); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 926 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | if (-1 == PyCPointerType_SetProto(dict, type)) |
| 928 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type)) |
| 931 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | Py_INCREF(Py_None); |
| 934 | return Py_None; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Neal Norwitz | 227b533 | 2006-03-22 09:28:35 +0000 | [diff] [blame] | 937 | static PyObject *_byref(PyObject *); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 938 | |
| 939 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 940 | PyCPointerType_from_param(PyObject *type, PyObject *value) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 941 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 942 | StgDictObject *typedict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 943 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 944 | if (value == Py_None) { |
| 945 | /* ConvParam will convert to a NULL pointer later */ |
| 946 | Py_INCREF(value); |
| 947 | return value; |
| 948 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 950 | typedict = PyType_stgdict(type); |
| 951 | assert(typedict); /* Cannot be NULL for pointer types */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 953 | /* If we expect POINTER(<type>), but receive a <type> instance, accept |
| 954 | it by calling byref(<type>). |
| 955 | */ |
| 956 | switch (PyObject_IsInstance(value, typedict->proto)) { |
| 957 | case 1: |
| 958 | Py_INCREF(value); /* _byref steals a refcount */ |
| 959 | return _byref(value); |
| 960 | case -1: |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 961 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 962 | default: |
| 963 | break; |
| 964 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | if (PointerObject_Check(value) || ArrayObject_Check(value)) { |
| 967 | /* Array instances are also pointers when |
| 968 | the item types are the same. |
| 969 | */ |
| 970 | StgDictObject *v = PyObject_stgdict(value); |
| 971 | assert(v); /* Cannot be NULL for pointer or array objects */ |
| 972 | if (PyObject_IsSubclass(v->proto, typedict->proto)) { |
| 973 | Py_INCREF(value); |
| 974 | return value; |
| 975 | } |
| 976 | } |
| 977 | return CDataType_from_param(type, value); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 978 | } |
| 979 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 980 | static PyMethodDef PyCPointerType_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 981 | { "from_address", CDataType_from_address, METH_O, from_address_doc }, |
| 982 | { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, }, |
| 983 | { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, }, |
| 984 | { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc}, |
| 985 | { "from_param", (PyCFunction)PyCPointerType_from_param, METH_O, from_param_doc}, |
| 986 | { "set_type", (PyCFunction)PyCPointerType_set_type, METH_O }, |
| 987 | { NULL, NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 988 | }; |
| 989 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 990 | PyTypeObject PyCPointerType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | PyVarObject_HEAD_INIT(NULL, 0) |
| 992 | "_ctypes.PyCPointerType", /* tp_name */ |
| 993 | 0, /* tp_basicsize */ |
| 994 | 0, /* tp_itemsize */ |
| 995 | 0, /* tp_dealloc */ |
| 996 | 0, /* tp_print */ |
| 997 | 0, /* tp_getattr */ |
| 998 | 0, /* tp_setattr */ |
| 999 | 0, /* tp_reserved */ |
| 1000 | 0, /* tp_repr */ |
| 1001 | 0, /* tp_as_number */ |
| 1002 | &CDataType_as_sequence, /* tp_as_sequence */ |
| 1003 | 0, /* tp_as_mapping */ |
| 1004 | 0, /* tp_hash */ |
| 1005 | 0, /* tp_call */ |
| 1006 | 0, /* tp_str */ |
| 1007 | 0, /* tp_getattro */ |
| 1008 | 0, /* tp_setattro */ |
| 1009 | 0, /* tp_as_buffer */ |
| 1010 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 1011 | "metatype for the Pointer Objects", /* tp_doc */ |
| 1012 | (traverseproc)CDataType_traverse, /* tp_traverse */ |
| 1013 | (inquiry)CDataType_clear, /* tp_clear */ |
| 1014 | 0, /* tp_richcompare */ |
| 1015 | 0, /* tp_weaklistoffset */ |
| 1016 | 0, /* tp_iter */ |
| 1017 | 0, /* tp_iternext */ |
| 1018 | PyCPointerType_methods, /* tp_methods */ |
| 1019 | 0, /* tp_members */ |
| 1020 | 0, /* tp_getset */ |
| 1021 | 0, /* tp_base */ |
| 1022 | 0, /* tp_dict */ |
| 1023 | 0, /* tp_descr_get */ |
| 1024 | 0, /* tp_descr_set */ |
| 1025 | 0, /* tp_dictoffset */ |
| 1026 | 0, /* tp_init */ |
| 1027 | 0, /* tp_alloc */ |
| 1028 | PyCPointerType_new, /* tp_new */ |
| 1029 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1030 | }; |
| 1031 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1032 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1033 | /******************************************************************/ |
| 1034 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1035 | PyCArrayType_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1036 | */ |
| 1037 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1038 | PyCArrayType_new ensures that the new Array subclass created has a _length_ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1039 | attribute, and a _type_ attribute. |
| 1040 | */ |
| 1041 | |
| 1042 | static int |
| 1043 | CharArray_set_raw(CDataObject *self, PyObject *value) |
| 1044 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1045 | char *ptr; |
| 1046 | Py_ssize_t size; |
| 1047 | Py_buffer view; |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1048 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1049 | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
Travis E. Oliphant | b99f762 | 2007-08-18 11:21:56 +0000 | [diff] [blame] | 1050 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1051 | size = view.len; |
| 1052 | ptr = view.buf; |
| 1053 | if (size > self->b_size) { |
| 1054 | PyErr_SetString(PyExc_ValueError, |
| 1055 | "string too long"); |
| 1056 | goto fail; |
| 1057 | } |
| 1058 | |
| 1059 | memcpy(self->b_ptr, ptr, size); |
| 1060 | |
| 1061 | PyBuffer_Release(&view); |
| 1062 | return 0; |
| 1063 | fail: |
| 1064 | PyBuffer_Release(&view); |
| 1065 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | static PyObject * |
| 1069 | CharArray_get_raw(CDataObject *self) |
| 1070 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1071 | return PyBytes_FromStringAndSize(self->b_ptr, self->b_size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
| 1074 | static PyObject * |
| 1075 | CharArray_get_value(CDataObject *self) |
| 1076 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | int i; |
| 1078 | char *ptr = self->b_ptr; |
| 1079 | for (i = 0; i < self->b_size; ++i) |
| 1080 | if (*ptr++ == '\0') |
| 1081 | break; |
| 1082 | return PyBytes_FromStringAndSize(self->b_ptr, i); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | static int |
| 1086 | CharArray_set_value(CDataObject *self, PyObject *value) |
| 1087 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1088 | char *ptr; |
| 1089 | Py_ssize_t size; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1091 | if (value == NULL) { |
| 1092 | PyErr_SetString(PyExc_TypeError, |
| 1093 | "can't delete attribute"); |
| 1094 | return -1; |
| 1095 | } |
Christian Heimes | 99170a5 | 2007-12-19 02:07:34 +0000 | [diff] [blame] | 1096 | |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 1097 | if (!PyBytes_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1098 | PyErr_Format(PyExc_TypeError, |
Meador Inge | 65992c1 | 2012-01-26 08:44:00 -0600 | [diff] [blame] | 1099 | "bytes expected instead of %s instance", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1100 | Py_TYPE(value)->tp_name); |
| 1101 | return -1; |
| 1102 | } else |
| 1103 | Py_INCREF(value); |
| 1104 | size = PyBytes_GET_SIZE(value); |
| 1105 | if (size > self->b_size) { |
| 1106 | PyErr_SetString(PyExc_ValueError, |
| 1107 | "string too long"); |
| 1108 | Py_DECREF(value); |
| 1109 | return -1; |
| 1110 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1112 | ptr = PyBytes_AS_STRING(value); |
| 1113 | memcpy(self->b_ptr, ptr, size); |
| 1114 | if (size < self->b_size) |
| 1115 | self->b_ptr[size] = '\0'; |
| 1116 | Py_DECREF(value); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1118 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | static PyGetSetDef CharArray_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1122 | { "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw, |
| 1123 | "value", NULL }, |
| 1124 | { "value", (getter)CharArray_get_value, (setter)CharArray_set_value, |
| 1125 | "string value"}, |
| 1126 | { NULL, NULL } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1127 | }; |
| 1128 | |
| 1129 | #ifdef CTYPES_UNICODE |
| 1130 | static PyObject * |
| 1131 | WCharArray_get_value(CDataObject *self) |
| 1132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1133 | unsigned int i; |
| 1134 | wchar_t *ptr = (wchar_t *)self->b_ptr; |
| 1135 | for (i = 0; i < self->b_size/sizeof(wchar_t); ++i) |
| 1136 | if (*ptr++ == (wchar_t)0) |
| 1137 | break; |
| 1138 | return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | static int |
| 1142 | WCharArray_set_value(CDataObject *self, PyObject *value) |
| 1143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | Py_ssize_t result = 0; |
Victor Stinner | ea90e0f | 2011-11-21 02:11:26 +0100 | [diff] [blame] | 1145 | Py_UNICODE *wstr; |
| 1146 | Py_ssize_t len; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1147 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | if (value == NULL) { |
| 1149 | PyErr_SetString(PyExc_TypeError, |
| 1150 | "can't delete attribute"); |
| 1151 | return -1; |
| 1152 | } |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 1153 | if (!PyUnicode_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | PyErr_Format(PyExc_TypeError, |
| 1155 | "unicode string expected instead of %s instance", |
| 1156 | Py_TYPE(value)->tp_name); |
| 1157 | return -1; |
| 1158 | } else |
| 1159 | Py_INCREF(value); |
Victor Stinner | ea90e0f | 2011-11-21 02:11:26 +0100 | [diff] [blame] | 1160 | |
| 1161 | wstr = PyUnicode_AsUnicodeAndSize(value, &len); |
| 1162 | if (wstr == NULL) |
| 1163 | return -1; |
| 1164 | if ((unsigned)len > self->b_size/sizeof(wchar_t)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1165 | PyErr_SetString(PyExc_ValueError, |
| 1166 | "string too long"); |
| 1167 | result = -1; |
| 1168 | goto done; |
| 1169 | } |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 1170 | result = PyUnicode_AsWideChar(value, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | (wchar_t *)self->b_ptr, |
| 1172 | self->b_size/sizeof(wchar_t)); |
| 1173 | if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t)) |
| 1174 | ((wchar_t *)self->b_ptr)[result] = (wchar_t)0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1175 | done: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1176 | Py_DECREF(value); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | return result >= 0 ? 0 : -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | static PyGetSetDef WCharArray_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1182 | { "value", (getter)WCharArray_get_value, (setter)WCharArray_set_value, |
| 1183 | "string value"}, |
| 1184 | { NULL, NULL } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1185 | }; |
| 1186 | #endif |
| 1187 | |
| 1188 | /* |
| 1189 | The next three functions copied from Python's typeobject.c. |
| 1190 | |
| 1191 | They are used to attach methods, members, or getsets to a type *after* it |
| 1192 | has been created: Arrays of characters have additional getsets to treat them |
| 1193 | as strings. |
| 1194 | */ |
| 1195 | /* |
| 1196 | static int |
| 1197 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 1198 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1199 | PyObject *dict = type->tp_dict; |
| 1200 | for (; meth->ml_name != NULL; meth++) { |
| 1201 | PyObject *descr; |
| 1202 | descr = PyDescr_NewMethod(type, meth); |
| 1203 | if (descr == NULL) |
| 1204 | return -1; |
| 1205 | if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0) |
| 1206 | return -1; |
| 1207 | Py_DECREF(descr); |
| 1208 | } |
| 1209 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | static int |
| 1213 | add_members(PyTypeObject *type, PyMemberDef *memb) |
| 1214 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1215 | PyObject *dict = type->tp_dict; |
| 1216 | for (; memb->name != NULL; memb++) { |
| 1217 | PyObject *descr; |
| 1218 | descr = PyDescr_NewMember(type, memb); |
| 1219 | if (descr == NULL) |
| 1220 | return -1; |
| 1221 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 1222 | return -1; |
| 1223 | Py_DECREF(descr); |
| 1224 | } |
| 1225 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1226 | } |
| 1227 | */ |
| 1228 | |
| 1229 | static int |
| 1230 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
| 1231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | PyObject *dict = type->tp_dict; |
| 1233 | for (; gsp->name != NULL; gsp++) { |
| 1234 | PyObject *descr; |
| 1235 | descr = PyDescr_NewGetSet(type, gsp); |
| 1236 | if (descr == NULL) |
| 1237 | return -1; |
| 1238 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 1239 | return -1; |
| 1240 | Py_DECREF(descr); |
| 1241 | } |
| 1242 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1243 | } |
| 1244 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1245 | static PyCArgObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1246 | PyCArrayType_paramfunc(CDataObject *self) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1248 | PyCArgObject *p = PyCArgObject_new(); |
| 1249 | if (p == NULL) |
| 1250 | return NULL; |
| 1251 | p->tag = 'P'; |
| 1252 | p->pffi_type = &ffi_type_pointer; |
| 1253 | p->value.p = (char *)self->b_ptr; |
| 1254 | Py_INCREF(self); |
| 1255 | p->obj = (PyObject *)self; |
| 1256 | return p; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1257 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1258 | |
| 1259 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1260 | PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1261 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1262 | PyTypeObject *result; |
| 1263 | StgDictObject *stgdict; |
| 1264 | StgDictObject *itemdict; |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1265 | PyObject *length_attr, *type_attr; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1266 | long length; |
| 1267 | int overflow; |
| 1268 | Py_ssize_t itemsize, itemalign; |
| 1269 | char buf[32]; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1270 | |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1271 | /* create the new instance (which is a class, |
| 1272 | since we are a metatype!) */ |
| 1273 | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
| 1274 | if (result == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1275 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1276 | |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1277 | /* Initialize these variables to NULL so that we can simplify error |
| 1278 | handling by using Py_XDECREF. */ |
| 1279 | stgdict = NULL; |
| 1280 | type_attr = NULL; |
| 1281 | |
| 1282 | length_attr = PyObject_GetAttrString((PyObject *)result, "_length_"); |
| 1283 | if (!length_attr || !PyLong_Check(length_attr)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1284 | PyErr_SetString(PyExc_AttributeError, |
| 1285 | "class must define a '_length_' attribute, " |
| 1286 | "which must be a positive integer"); |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1287 | Py_XDECREF(length_attr); |
| 1288 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1289 | } |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1290 | length = PyLong_AsLongAndOverflow(length_attr, &overflow); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1291 | if (overflow) { |
| 1292 | PyErr_SetString(PyExc_OverflowError, |
| 1293 | "The '_length_' attribute is too large"); |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1294 | Py_DECREF(length_attr); |
| 1295 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1296 | } |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1297 | Py_DECREF(length_attr); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1298 | |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1299 | type_attr = PyObject_GetAttrString((PyObject *)result, "_type_"); |
| 1300 | if (!type_attr) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1301 | PyErr_SetString(PyExc_AttributeError, |
| 1302 | "class must define a '_type_' attribute"); |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1303 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1304 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1306 | stgdict = (StgDictObject *)PyObject_CallObject( |
| 1307 | (PyObject *)&PyCStgDict_Type, NULL); |
| 1308 | if (!stgdict) |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1309 | goto error; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1310 | |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1311 | itemdict = PyType_stgdict(type_attr); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1312 | if (!itemdict) { |
| 1313 | PyErr_SetString(PyExc_TypeError, |
| 1314 | "_type_ must have storage info"); |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1315 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1316 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1317 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1318 | assert(itemdict->format); |
| 1319 | if (itemdict->format[0] == '(') { |
| 1320 | sprintf(buf, "(%ld,", length); |
| 1321 | stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format+1); |
| 1322 | } else { |
| 1323 | sprintf(buf, "(%ld)", length); |
| 1324 | stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format); |
| 1325 | } |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1326 | if (stgdict->format == NULL) |
| 1327 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | stgdict->ndim = itemdict->ndim + 1; |
| 1329 | stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim); |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1330 | if (stgdict->shape == NULL) |
| 1331 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1332 | stgdict->shape[0] = length; |
| 1333 | memmove(&stgdict->shape[1], itemdict->shape, |
| 1334 | sizeof(Py_ssize_t) * (stgdict->ndim - 1)); |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1336 | itemsize = itemdict->size; |
| 1337 | if (length * itemsize < 0) { |
| 1338 | PyErr_SetString(PyExc_OverflowError, |
| 1339 | "array too large"); |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1340 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1341 | } |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1342 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1343 | itemalign = itemdict->align; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1344 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1345 | if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER)) |
| 1346 | stgdict->flags |= TYPEFLAG_HASPOINTER; |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1348 | stgdict->size = itemsize * length; |
| 1349 | stgdict->align = itemalign; |
| 1350 | stgdict->length = length; |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1351 | stgdict->proto = type_attr; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1353 | stgdict->paramfunc = &PyCArrayType_paramfunc; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1354 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1355 | /* Arrays are passed as pointers to function calls. */ |
| 1356 | stgdict->ffi_type_pointer = ffi_type_pointer; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1357 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1358 | /* replace the class dict by our updated spam dict */ |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1359 | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) |
| 1360 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1361 | Py_DECREF(result->tp_dict); |
| 1362 | result->tp_dict = (PyObject *)stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1363 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1364 | /* Special case for character arrays. |
| 1365 | A permanent annoyance: char arrays are also strings! |
| 1366 | */ |
| 1367 | if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
| 1368 | if (-1 == add_getset(result, CharArray_getsets)) |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1369 | goto error; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1370 | #ifdef CTYPES_UNICODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1371 | } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
| 1372 | if (-1 == add_getset(result, WCharArray_getsets)) |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1373 | goto error; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1374 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1376 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1377 | return (PyObject *)result; |
Amaury Forgeot d'Arc | faecc38 | 2011-08-30 22:02:51 +0200 | [diff] [blame] | 1378 | error: |
| 1379 | Py_XDECREF((PyObject*)stgdict); |
| 1380 | Py_XDECREF(type_attr); |
| 1381 | Py_DECREF(result); |
| 1382 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1385 | PyTypeObject PyCArrayType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1386 | PyVarObject_HEAD_INIT(NULL, 0) |
| 1387 | "_ctypes.PyCArrayType", /* tp_name */ |
| 1388 | 0, /* tp_basicsize */ |
| 1389 | 0, /* tp_itemsize */ |
| 1390 | 0, /* tp_dealloc */ |
| 1391 | 0, /* tp_print */ |
| 1392 | 0, /* tp_getattr */ |
| 1393 | 0, /* tp_setattr */ |
| 1394 | 0, /* tp_reserved */ |
| 1395 | 0, /* tp_repr */ |
| 1396 | 0, /* tp_as_number */ |
| 1397 | &CDataType_as_sequence, /* tp_as_sequence */ |
| 1398 | 0, /* tp_as_mapping */ |
| 1399 | 0, /* tp_hash */ |
| 1400 | 0, /* tp_call */ |
| 1401 | 0, /* tp_str */ |
| 1402 | 0, /* tp_getattro */ |
| 1403 | 0, /* tp_setattro */ |
| 1404 | 0, /* tp_as_buffer */ |
| 1405 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1406 | "metatype for the Array Objects", /* tp_doc */ |
| 1407 | 0, /* tp_traverse */ |
| 1408 | 0, /* tp_clear */ |
| 1409 | 0, /* tp_richcompare */ |
| 1410 | 0, /* tp_weaklistoffset */ |
| 1411 | 0, /* tp_iter */ |
| 1412 | 0, /* tp_iternext */ |
| 1413 | CDataType_methods, /* tp_methods */ |
| 1414 | 0, /* tp_members */ |
| 1415 | 0, /* tp_getset */ |
| 1416 | 0, /* tp_base */ |
| 1417 | 0, /* tp_dict */ |
| 1418 | 0, /* tp_descr_get */ |
| 1419 | 0, /* tp_descr_set */ |
| 1420 | 0, /* tp_dictoffset */ |
| 1421 | 0, /* tp_init */ |
| 1422 | 0, /* tp_alloc */ |
| 1423 | PyCArrayType_new, /* tp_new */ |
| 1424 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1425 | }; |
| 1426 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1427 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1428 | /******************************************************************/ |
| 1429 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1430 | PyCSimpleType_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1431 | */ |
| 1432 | /* |
| 1433 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1434 | PyCSimpleType_new ensures that the new Simple_Type subclass created has a valid |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1435 | _type_ attribute. |
| 1436 | |
| 1437 | */ |
| 1438 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 1439 | static char *SIMPLE_TYPE_CHARS = "cbBhHiIlLdfuzZqQPXOv?g"; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1440 | |
| 1441 | static PyObject * |
| 1442 | c_wchar_p_from_param(PyObject *type, PyObject *value) |
| 1443 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | PyObject *as_parameter; |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 1445 | int res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1446 | if (value == Py_None) { |
| 1447 | Py_INCREF(Py_None); |
| 1448 | return Py_None; |
| 1449 | } |
| 1450 | if (PyUnicode_Check(value) || PyBytes_Check(value)) { |
| 1451 | PyCArgObject *parg; |
| 1452 | struct fielddesc *fd = _ctypes_get_fielddesc("Z"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1454 | parg = PyCArgObject_new(); |
| 1455 | if (parg == NULL) |
| 1456 | return NULL; |
| 1457 | parg->pffi_type = &ffi_type_pointer; |
| 1458 | parg->tag = 'Z'; |
| 1459 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1460 | if (parg->obj == NULL) { |
| 1461 | Py_DECREF(parg); |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | return (PyObject *)parg; |
| 1465 | } |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 1466 | res = PyObject_IsInstance(value, type); |
| 1467 | if (res == -1) |
| 1468 | return NULL; |
| 1469 | if (res) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1470 | Py_INCREF(value); |
| 1471 | return value; |
| 1472 | } |
| 1473 | if (ArrayObject_Check(value) || PointerObject_Check(value)) { |
| 1474 | /* c_wchar array instance or pointer(c_wchar(...)) */ |
| 1475 | StgDictObject *dt = PyObject_stgdict(value); |
| 1476 | StgDictObject *dict; |
| 1477 | assert(dt); /* Cannot be NULL for pointer or array objects */ |
| 1478 | dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; |
| 1479 | if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) { |
| 1480 | Py_INCREF(value); |
| 1481 | return value; |
| 1482 | } |
| 1483 | } |
| 1484 | if (PyCArg_CheckExact(value)) { |
| 1485 | /* byref(c_char(...)) */ |
| 1486 | PyCArgObject *a = (PyCArgObject *)value; |
| 1487 | StgDictObject *dict = PyObject_stgdict(a->obj); |
| 1488 | if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) { |
| 1489 | Py_INCREF(value); |
| 1490 | return value; |
| 1491 | } |
| 1492 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
| 1495 | if (as_parameter) { |
| 1496 | value = c_wchar_p_from_param(type, as_parameter); |
| 1497 | Py_DECREF(as_parameter); |
| 1498 | return value; |
| 1499 | } |
| 1500 | /* XXX better message */ |
| 1501 | PyErr_SetString(PyExc_TypeError, |
| 1502 | "wrong type"); |
| 1503 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | static PyObject * |
| 1507 | c_char_p_from_param(PyObject *type, PyObject *value) |
| 1508 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | PyObject *as_parameter; |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 1510 | int res; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | if (value == Py_None) { |
| 1512 | Py_INCREF(Py_None); |
| 1513 | return Py_None; |
| 1514 | } |
Victor Stinner | cf44883 | 2010-07-28 00:15:03 +0000 | [diff] [blame] | 1515 | if (PyBytes_Check(value)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | PyCArgObject *parg; |
| 1517 | struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1518 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1519 | parg = PyCArgObject_new(); |
| 1520 | if (parg == NULL) |
| 1521 | return NULL; |
| 1522 | parg->pffi_type = &ffi_type_pointer; |
| 1523 | parg->tag = 'z'; |
| 1524 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1525 | if (parg->obj == NULL) { |
| 1526 | Py_DECREF(parg); |
| 1527 | return NULL; |
| 1528 | } |
| 1529 | return (PyObject *)parg; |
| 1530 | } |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 1531 | res = PyObject_IsInstance(value, type); |
| 1532 | if (res == -1) |
| 1533 | return NULL; |
| 1534 | if (res) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | Py_INCREF(value); |
| 1536 | return value; |
| 1537 | } |
| 1538 | if (ArrayObject_Check(value) || PointerObject_Check(value)) { |
| 1539 | /* c_char array instance or pointer(c_char(...)) */ |
| 1540 | StgDictObject *dt = PyObject_stgdict(value); |
| 1541 | StgDictObject *dict; |
| 1542 | assert(dt); /* Cannot be NULL for pointer or array objects */ |
| 1543 | dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; |
| 1544 | if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) { |
| 1545 | Py_INCREF(value); |
| 1546 | return value; |
| 1547 | } |
| 1548 | } |
| 1549 | if (PyCArg_CheckExact(value)) { |
| 1550 | /* byref(c_char(...)) */ |
| 1551 | PyCArgObject *a = (PyCArgObject *)value; |
| 1552 | StgDictObject *dict = PyObject_stgdict(a->obj); |
| 1553 | if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) { |
| 1554 | Py_INCREF(value); |
| 1555 | return value; |
| 1556 | } |
| 1557 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1559 | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
| 1560 | if (as_parameter) { |
| 1561 | value = c_char_p_from_param(type, as_parameter); |
| 1562 | Py_DECREF(as_parameter); |
| 1563 | return value; |
| 1564 | } |
| 1565 | /* XXX better message */ |
| 1566 | PyErr_SetString(PyExc_TypeError, |
| 1567 | "wrong type"); |
| 1568 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | static PyObject * |
| 1572 | c_void_p_from_param(PyObject *type, PyObject *value) |
| 1573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1574 | StgDictObject *stgd; |
| 1575 | PyObject *as_parameter; |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 1576 | int res; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1577 | |
| 1578 | /* None */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1579 | if (value == Py_None) { |
| 1580 | Py_INCREF(Py_None); |
| 1581 | return Py_None; |
| 1582 | } |
| 1583 | /* Should probably allow buffer interface as well */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1584 | /* int, long */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1585 | if (PyLong_Check(value)) { |
| 1586 | PyCArgObject *parg; |
| 1587 | struct fielddesc *fd = _ctypes_get_fielddesc("P"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1588 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1589 | parg = PyCArgObject_new(); |
| 1590 | if (parg == NULL) |
| 1591 | return NULL; |
| 1592 | parg->pffi_type = &ffi_type_pointer; |
| 1593 | parg->tag = 'P'; |
| 1594 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1595 | if (parg->obj == NULL) { |
| 1596 | Py_DECREF(parg); |
| 1597 | return NULL; |
| 1598 | } |
| 1599 | return (PyObject *)parg; |
| 1600 | } |
| 1601 | /* XXX struni: remove later */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1602 | /* string */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1603 | if (PyBytes_Check(value)) { |
| 1604 | PyCArgObject *parg; |
| 1605 | struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1606 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1607 | parg = PyCArgObject_new(); |
| 1608 | if (parg == NULL) |
| 1609 | return NULL; |
| 1610 | parg->pffi_type = &ffi_type_pointer; |
| 1611 | parg->tag = 'z'; |
| 1612 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1613 | if (parg->obj == NULL) { |
| 1614 | Py_DECREF(parg); |
| 1615 | return NULL; |
| 1616 | } |
| 1617 | return (PyObject *)parg; |
| 1618 | } |
Thomas Heller | 3821e31 | 2007-07-12 19:38:33 +0000 | [diff] [blame] | 1619 | /* bytes */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1620 | if (PyByteArray_Check(value)) { |
| 1621 | PyCArgObject *parg; |
| 1622 | struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
Thomas Heller | 3821e31 | 2007-07-12 19:38:33 +0000 | [diff] [blame] | 1623 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1624 | parg = PyCArgObject_new(); |
| 1625 | if (parg == NULL) |
| 1626 | return NULL; |
| 1627 | parg->pffi_type = &ffi_type_pointer; |
| 1628 | parg->tag = 'z'; |
| 1629 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1630 | if (parg->obj == NULL) { |
| 1631 | Py_DECREF(parg); |
| 1632 | return NULL; |
| 1633 | } |
| 1634 | return (PyObject *)parg; |
| 1635 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1636 | /* unicode */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1637 | if (PyUnicode_Check(value)) { |
| 1638 | PyCArgObject *parg; |
| 1639 | struct fielddesc *fd = _ctypes_get_fielddesc("Z"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1640 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1641 | parg = PyCArgObject_new(); |
| 1642 | if (parg == NULL) |
| 1643 | return NULL; |
| 1644 | parg->pffi_type = &ffi_type_pointer; |
| 1645 | parg->tag = 'Z'; |
| 1646 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1647 | if (parg->obj == NULL) { |
| 1648 | Py_DECREF(parg); |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | return (PyObject *)parg; |
| 1652 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1653 | /* c_void_p instance (or subclass) */ |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 1654 | res = PyObject_IsInstance(value, type); |
| 1655 | if (res == -1) |
| 1656 | return NULL; |
| 1657 | if (res) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1658 | /* c_void_p instances */ |
| 1659 | Py_INCREF(value); |
| 1660 | return value; |
| 1661 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1662 | /* ctypes array or pointer instance */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1663 | if (ArrayObject_Check(value) || PointerObject_Check(value)) { |
| 1664 | /* Any array or pointer is accepted */ |
| 1665 | Py_INCREF(value); |
| 1666 | return value; |
| 1667 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1668 | /* byref(...) */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1669 | if (PyCArg_CheckExact(value)) { |
| 1670 | /* byref(c_xxx()) */ |
| 1671 | PyCArgObject *a = (PyCArgObject *)value; |
| 1672 | if (a->tag == 'P') { |
| 1673 | Py_INCREF(value); |
| 1674 | return value; |
| 1675 | } |
| 1676 | } |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1677 | /* function pointer */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | if (PyCFuncPtrObject_Check(value)) { |
| 1679 | PyCArgObject *parg; |
| 1680 | PyCFuncPtrObject *func; |
| 1681 | func = (PyCFuncPtrObject *)value; |
| 1682 | parg = PyCArgObject_new(); |
| 1683 | if (parg == NULL) |
| 1684 | return NULL; |
| 1685 | parg->pffi_type = &ffi_type_pointer; |
| 1686 | parg->tag = 'P'; |
| 1687 | Py_INCREF(value); |
| 1688 | parg->value.p = *(void **)func->b_ptr; |
| 1689 | parg->obj = value; |
| 1690 | return (PyObject *)parg; |
| 1691 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1692 | /* c_char_p, c_wchar_p */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1693 | stgd = PyObject_stgdict(value); |
| 1694 | if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd->proto)) { |
| 1695 | PyCArgObject *parg; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1697 | switch (_PyUnicode_AsString(stgd->proto)[0]) { |
| 1698 | case 'z': /* c_char_p */ |
| 1699 | case 'Z': /* c_wchar_p */ |
| 1700 | parg = PyCArgObject_new(); |
| 1701 | if (parg == NULL) |
| 1702 | return NULL; |
| 1703 | parg->pffi_type = &ffi_type_pointer; |
| 1704 | parg->tag = 'Z'; |
| 1705 | Py_INCREF(value); |
| 1706 | parg->obj = value; |
| 1707 | /* Remember: b_ptr points to where the pointer is stored! */ |
| 1708 | parg->value.p = *(void **)(((CDataObject *)value)->b_ptr); |
| 1709 | return (PyObject *)parg; |
| 1710 | } |
| 1711 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1712 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1713 | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
| 1714 | if (as_parameter) { |
| 1715 | value = c_void_p_from_param(type, as_parameter); |
| 1716 | Py_DECREF(as_parameter); |
| 1717 | return value; |
| 1718 | } |
| 1719 | /* XXX better message */ |
| 1720 | PyErr_SetString(PyExc_TypeError, |
| 1721 | "wrong type"); |
| 1722 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1723 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1724 | |
| 1725 | static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O }; |
| 1726 | static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_O }; |
| 1727 | static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH_O }; |
| 1728 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1729 | static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | PyObject *proto, struct fielddesc *fmt) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1731 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1732 | PyTypeObject *result; |
| 1733 | StgDictObject *stgdict; |
| 1734 | PyObject *name = PyTuple_GET_ITEM(args, 0); |
| 1735 | PyObject *newname; |
| 1736 | PyObject *swapped_args; |
| 1737 | static PyObject *suffix; |
| 1738 | Py_ssize_t i; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1739 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1740 | swapped_args = PyTuple_New(PyTuple_GET_SIZE(args)); |
| 1741 | if (!swapped_args) |
| 1742 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1744 | if (suffix == NULL) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1745 | #ifdef WORDS_BIGENDIAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1746 | suffix = PyUnicode_InternFromString("_le"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1747 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1748 | suffix = PyUnicode_InternFromString("_be"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1749 | #endif |
| 1750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1751 | newname = PyUnicode_Concat(name, suffix); |
| 1752 | if (newname == NULL) { |
| 1753 | return NULL; |
| 1754 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1755 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1756 | PyTuple_SET_ITEM(swapped_args, 0, newname); |
| 1757 | for (i=1; i<PyTuple_GET_SIZE(args); ++i) { |
| 1758 | PyObject *v = PyTuple_GET_ITEM(args, i); |
| 1759 | Py_INCREF(v); |
| 1760 | PyTuple_SET_ITEM(swapped_args, i, v); |
| 1761 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1763 | /* create the new instance (which is a class, |
| 1764 | since we are a metatype!) */ |
| 1765 | result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds); |
| 1766 | Py_DECREF(swapped_args); |
| 1767 | if (result == NULL) |
| 1768 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1769 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1770 | stgdict = (StgDictObject *)PyObject_CallObject( |
| 1771 | (PyObject *)&PyCStgDict_Type, NULL); |
| 1772 | if (!stgdict) /* XXX leaks result! */ |
| 1773 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1774 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | stgdict->ffi_type_pointer = *fmt->pffi_type; |
| 1776 | stgdict->align = fmt->pffi_type->alignment; |
| 1777 | stgdict->length = 0; |
| 1778 | stgdict->size = fmt->pffi_type->size; |
| 1779 | stgdict->setfunc = fmt->setfunc_swapped; |
| 1780 | stgdict->getfunc = fmt->getfunc_swapped; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | Py_INCREF(proto); |
| 1783 | stgdict->proto = proto; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | /* replace the class dict by our updated spam dict */ |
| 1786 | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
| 1787 | Py_DECREF(result); |
| 1788 | Py_DECREF((PyObject *)stgdict); |
| 1789 | return NULL; |
| 1790 | } |
| 1791 | Py_DECREF(result->tp_dict); |
| 1792 | result->tp_dict = (PyObject *)stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1793 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | return (PyObject *)result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1797 | static PyCArgObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1798 | PyCSimpleType_paramfunc(CDataObject *self) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1799 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1800 | StgDictObject *dict; |
| 1801 | char *fmt; |
| 1802 | PyCArgObject *parg; |
| 1803 | struct fielddesc *fd; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1805 | dict = PyObject_stgdict((PyObject *)self); |
| 1806 | assert(dict); /* Cannot be NULL for CDataObject instances */ |
| 1807 | fmt = _PyUnicode_AsString(dict->proto); |
| 1808 | assert(fmt); |
| 1809 | |
| 1810 | fd = _ctypes_get_fielddesc(fmt); |
| 1811 | assert(fd); |
| 1812 | |
| 1813 | parg = PyCArgObject_new(); |
| 1814 | if (parg == NULL) |
| 1815 | return NULL; |
| 1816 | |
| 1817 | parg->tag = fmt[0]; |
| 1818 | parg->pffi_type = fd->pffi_type; |
| 1819 | Py_INCREF(self); |
| 1820 | parg->obj = (PyObject *)self; |
| 1821 | memcpy(&parg->value, self->b_ptr, self->b_size); |
| 1822 | return parg; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1823 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1824 | |
| 1825 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 1826 | PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1827 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1828 | PyTypeObject *result; |
| 1829 | StgDictObject *stgdict; |
| 1830 | PyObject *proto; |
| 1831 | const char *proto_str; |
| 1832 | Py_ssize_t proto_len; |
| 1833 | PyMethodDef *ml; |
| 1834 | struct fielddesc *fmt; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | /* create the new instance (which is a class, |
| 1837 | since we are a metatype!) */ |
| 1838 | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
| 1839 | if (result == NULL) |
| 1840 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */ |
| 1843 | if (!proto) { |
| 1844 | PyErr_SetString(PyExc_AttributeError, |
| 1845 | "class must define a '_type_' attribute"); |
Guido van Rossum | 5e23d57 | 2007-07-09 11:17:33 +0000 | [diff] [blame] | 1846 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1847 | Py_XDECREF(proto); |
| 1848 | Py_XDECREF(result); |
| 1849 | return NULL; |
| 1850 | } |
| 1851 | if (PyUnicode_Check(proto)) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1852 | proto_str = PyUnicode_AsUTF8AndSize(proto, &proto_len); |
| 1853 | if (!proto_str) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1855 | } else { |
| 1856 | PyErr_SetString(PyExc_TypeError, |
| 1857 | "class must define a '_type_' string attribute"); |
| 1858 | goto error; |
| 1859 | } |
| 1860 | if (proto_len != 1) { |
| 1861 | PyErr_SetString(PyExc_ValueError, |
| 1862 | "class must define a '_type_' attribute " |
| 1863 | "which must be a string of length 1"); |
| 1864 | goto error; |
| 1865 | } |
| 1866 | if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) { |
| 1867 | PyErr_Format(PyExc_AttributeError, |
| 1868 | "class must define a '_type_' attribute which must be\n" |
| 1869 | "a single character string containing one of '%s'.", |
| 1870 | SIMPLE_TYPE_CHARS); |
| 1871 | goto error; |
| 1872 | } |
| 1873 | fmt = _ctypes_get_fielddesc(proto_str); |
| 1874 | if (fmt == NULL) { |
| 1875 | PyErr_Format(PyExc_ValueError, |
| 1876 | "_type_ '%s' not supported", proto_str); |
| 1877 | goto error; |
| 1878 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 1879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1880 | stgdict = (StgDictObject *)PyObject_CallObject( |
| 1881 | (PyObject *)&PyCStgDict_Type, NULL); |
| 1882 | if (!stgdict) |
| 1883 | goto error; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1885 | stgdict->ffi_type_pointer = *fmt->pffi_type; |
| 1886 | stgdict->align = fmt->pffi_type->alignment; |
| 1887 | stgdict->length = 0; |
| 1888 | stgdict->size = fmt->pffi_type->size; |
| 1889 | stgdict->setfunc = fmt->setfunc; |
| 1890 | stgdict->getfunc = fmt->getfunc; |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1891 | #ifdef WORDS_BIGENDIAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1892 | stgdict->format = _ctypes_alloc_format_string(">", proto_str); |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1893 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1894 | stgdict->format = _ctypes_alloc_format_string("<", proto_str); |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 1895 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | if (stgdict->format == NULL) { |
| 1897 | Py_DECREF(result); |
| 1898 | Py_DECREF(proto); |
| 1899 | Py_DECREF((PyObject *)stgdict); |
| 1900 | return NULL; |
| 1901 | } |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1903 | stgdict->paramfunc = PyCSimpleType_paramfunc; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1904 | /* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1905 | if (result->tp_base != &Simple_Type) { |
| 1906 | stgdict->setfunc = NULL; |
| 1907 | stgdict->getfunc = NULL; |
| 1908 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1909 | */ |
| 1910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | /* This consumes the refcount on proto which we have */ |
| 1912 | stgdict->proto = proto; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | /* replace the class dict by our updated spam dict */ |
| 1915 | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
| 1916 | Py_DECREF(result); |
| 1917 | Py_DECREF((PyObject *)stgdict); |
| 1918 | return NULL; |
| 1919 | } |
| 1920 | Py_DECREF(result->tp_dict); |
| 1921 | result->tp_dict = (PyObject *)stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | /* Install from_param class methods in ctypes base classes. |
| 1924 | Overrides the PyCSimpleType_from_param generic method. |
| 1925 | */ |
| 1926 | if (result->tp_base == &Simple_Type) { |
| 1927 | switch (*proto_str) { |
| 1928 | case 'z': /* c_char_p */ |
| 1929 | ml = &c_char_p_method; |
| 1930 | stgdict->flags |= TYPEFLAG_ISPOINTER; |
| 1931 | break; |
| 1932 | case 'Z': /* c_wchar_p */ |
| 1933 | ml = &c_wchar_p_method; |
| 1934 | stgdict->flags |= TYPEFLAG_ISPOINTER; |
| 1935 | break; |
| 1936 | case 'P': /* c_void_p */ |
| 1937 | ml = &c_void_p_method; |
| 1938 | stgdict->flags |= TYPEFLAG_ISPOINTER; |
| 1939 | break; |
| 1940 | case 's': |
| 1941 | case 'X': |
| 1942 | case 'O': |
| 1943 | ml = NULL; |
| 1944 | stgdict->flags |= TYPEFLAG_ISPOINTER; |
| 1945 | break; |
| 1946 | default: |
| 1947 | ml = NULL; |
| 1948 | break; |
| 1949 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1950 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | if (ml) { |
| 1952 | PyObject *meth; |
| 1953 | int x; |
| 1954 | meth = PyDescr_NewClassMethod(result, ml); |
| 1955 | if (!meth) |
| 1956 | return NULL; |
| 1957 | x = PyDict_SetItemString(result->tp_dict, |
| 1958 | ml->ml_name, |
| 1959 | meth); |
| 1960 | Py_DECREF(meth); |
| 1961 | if (x == -1) { |
| 1962 | Py_DECREF(result); |
| 1963 | return NULL; |
| 1964 | } |
| 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | if (type == &PyCSimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) { |
| 1969 | PyObject *swapped = CreateSwappedType(type, args, kwds, |
| 1970 | proto, fmt); |
| 1971 | StgDictObject *sw_dict; |
| 1972 | if (swapped == NULL) { |
| 1973 | Py_DECREF(result); |
| 1974 | return NULL; |
| 1975 | } |
| 1976 | sw_dict = PyType_stgdict(swapped); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1977 | #ifdef WORDS_BIGENDIAN |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1978 | PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped); |
| 1979 | PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result); |
| 1980 | PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result); |
| 1981 | PyObject_SetAttrString(swapped, "__ctype_le__", swapped); |
| 1982 | /* We are creating the type for the OTHER endian */ |
| 1983 | sw_dict->format = _ctypes_alloc_format_string("<", stgdict->format+1); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1984 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1985 | PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped); |
| 1986 | PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result); |
| 1987 | PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result); |
| 1988 | PyObject_SetAttrString(swapped, "__ctype_be__", swapped); |
| 1989 | /* We are creating the type for the OTHER endian */ |
| 1990 | sw_dict->format = _ctypes_alloc_format_string(">", stgdict->format+1); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1991 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1992 | Py_DECREF(swapped); |
| 1993 | if (PyErr_Occurred()) { |
| 1994 | Py_DECREF(result); |
| 1995 | return NULL; |
| 1996 | } |
| 1997 | }; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 1998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | return (PyObject *)result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
| 2002 | /* |
| 2003 | * This is a *class method*. |
| 2004 | * Convert a parameter into something that ConvParam can handle. |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2005 | */ |
| 2006 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2007 | PyCSimpleType_from_param(PyObject *type, PyObject *value) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2008 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2009 | StgDictObject *dict; |
| 2010 | char *fmt; |
| 2011 | PyCArgObject *parg; |
| 2012 | struct fielddesc *fd; |
| 2013 | PyObject *as_parameter; |
Benjamin Peterson | 8d6c62d | 2011-03-26 17:56:28 -0500 | [diff] [blame] | 2014 | int res; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2015 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | /* If the value is already an instance of the requested type, |
| 2017 | we can use it as is */ |
Benjamin Peterson | 8d6c62d | 2011-03-26 17:56:28 -0500 | [diff] [blame] | 2018 | res = PyObject_IsInstance(value, type); |
| 2019 | if (res == -1) |
| 2020 | return NULL; |
| 2021 | if (res) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2022 | Py_INCREF(value); |
| 2023 | return value; |
| 2024 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2026 | dict = PyType_stgdict(type); |
| 2027 | assert(dict); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2029 | /* I think we can rely on this being a one-character string */ |
| 2030 | fmt = _PyUnicode_AsString(dict->proto); |
| 2031 | assert(fmt); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | fd = _ctypes_get_fielddesc(fmt); |
| 2034 | assert(fd); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2035 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | parg = PyCArgObject_new(); |
| 2037 | if (parg == NULL) |
| 2038 | return NULL; |
| 2039 | |
| 2040 | parg->tag = fmt[0]; |
| 2041 | parg->pffi_type = fd->pffi_type; |
| 2042 | parg->obj = fd->setfunc(&parg->value, value, 0); |
| 2043 | if (parg->obj) |
| 2044 | return (PyObject *)parg; |
| 2045 | PyErr_Clear(); |
| 2046 | Py_DECREF(parg); |
| 2047 | |
| 2048 | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
| 2049 | if (as_parameter) { |
Benjamin Peterson | 8d6c62d | 2011-03-26 17:56:28 -0500 | [diff] [blame] | 2050 | if (Py_EnterRecursiveCall("while processing _as_parameter_")) { |
| 2051 | Py_DECREF(as_parameter); |
| 2052 | return NULL; |
| 2053 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | value = PyCSimpleType_from_param(type, as_parameter); |
Benjamin Peterson | 8d6c62d | 2011-03-26 17:56:28 -0500 | [diff] [blame] | 2055 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2056 | Py_DECREF(as_parameter); |
| 2057 | return value; |
| 2058 | } |
| 2059 | PyErr_SetString(PyExc_TypeError, |
| 2060 | "wrong type"); |
| 2061 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2062 | } |
| 2063 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2064 | static PyMethodDef PyCSimpleType_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2065 | { "from_param", PyCSimpleType_from_param, METH_O, from_param_doc }, |
| 2066 | { "from_address", CDataType_from_address, METH_O, from_address_doc }, |
| 2067 | { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, }, |
| 2068 | { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, }, |
| 2069 | { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc}, |
| 2070 | { NULL, NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2071 | }; |
| 2072 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2073 | PyTypeObject PyCSimpleType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2075 | "_ctypes.PyCSimpleType", /* tp_name */ |
| 2076 | 0, /* tp_basicsize */ |
| 2077 | 0, /* tp_itemsize */ |
| 2078 | 0, /* tp_dealloc */ |
| 2079 | 0, /* tp_print */ |
| 2080 | 0, /* tp_getattr */ |
| 2081 | 0, /* tp_setattr */ |
| 2082 | 0, /* tp_reserved */ |
| 2083 | 0, /* tp_repr */ |
| 2084 | 0, /* tp_as_number */ |
| 2085 | &CDataType_as_sequence, /* tp_as_sequence */ |
| 2086 | 0, /* tp_as_mapping */ |
| 2087 | 0, /* tp_hash */ |
| 2088 | 0, /* tp_call */ |
| 2089 | 0, /* tp_str */ |
| 2090 | 0, /* tp_getattro */ |
| 2091 | 0, /* tp_setattro */ |
| 2092 | 0, /* tp_as_buffer */ |
| 2093 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2094 | "metatype for the PyCSimpleType Objects", /* tp_doc */ |
| 2095 | 0, /* tp_traverse */ |
| 2096 | 0, /* tp_clear */ |
| 2097 | 0, /* tp_richcompare */ |
| 2098 | 0, /* tp_weaklistoffset */ |
| 2099 | 0, /* tp_iter */ |
| 2100 | 0, /* tp_iternext */ |
| 2101 | PyCSimpleType_methods, /* tp_methods */ |
| 2102 | 0, /* tp_members */ |
| 2103 | 0, /* tp_getset */ |
| 2104 | 0, /* tp_base */ |
| 2105 | 0, /* tp_dict */ |
| 2106 | 0, /* tp_descr_get */ |
| 2107 | 0, /* tp_descr_set */ |
| 2108 | 0, /* tp_dictoffset */ |
| 2109 | 0, /* tp_init */ |
| 2110 | 0, /* tp_alloc */ |
| 2111 | PyCSimpleType_new, /* tp_new */ |
| 2112 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2113 | }; |
| 2114 | |
| 2115 | /******************************************************************/ |
| 2116 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2117 | PyCFuncPtrType_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2118 | */ |
| 2119 | |
| 2120 | static PyObject * |
| 2121 | converters_from_argtypes(PyObject *ob) |
| 2122 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2123 | PyObject *converters; |
| 2124 | Py_ssize_t i; |
| 2125 | Py_ssize_t nArgs; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2126 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | ob = PySequence_Tuple(ob); /* new reference */ |
| 2128 | if (!ob) { |
| 2129 | PyErr_SetString(PyExc_TypeError, |
| 2130 | "_argtypes_ must be a sequence of types"); |
| 2131 | return NULL; |
| 2132 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2133 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2134 | nArgs = PyTuple_GET_SIZE(ob); |
| 2135 | converters = PyTuple_New(nArgs); |
| 2136 | if (!converters) |
| 2137 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2139 | /* I have to check if this is correct. Using c_char, which has a size |
| 2140 | of 1, will be assumed to be pushed as only one byte! |
| 2141 | Aren't these promoted to integers by the C compiler and pushed as 4 bytes? |
| 2142 | */ |
| 2143 | |
| 2144 | for (i = 0; i < nArgs; ++i) { |
| 2145 | PyObject *tp = PyTuple_GET_ITEM(ob, i); |
| 2146 | PyObject *cnv = PyObject_GetAttrString(tp, "from_param"); |
| 2147 | if (!cnv) |
| 2148 | goto argtypes_error_1; |
| 2149 | PyTuple_SET_ITEM(converters, i, cnv); |
| 2150 | } |
| 2151 | Py_DECREF(ob); |
| 2152 | return converters; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2153 | |
| 2154 | argtypes_error_1: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2155 | Py_XDECREF(converters); |
| 2156 | Py_DECREF(ob); |
| 2157 | PyErr_Format(PyExc_TypeError, |
| 2158 | "item %zd in _argtypes_ has no from_param method", |
| 2159 | i+1); |
| 2160 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | static int |
| 2164 | make_funcptrtype_dict(StgDictObject *stgdict) |
| 2165 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2166 | PyObject *ob; |
| 2167 | PyObject *converters = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2168 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2169 | stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; |
| 2170 | stgdict->length = 1; |
| 2171 | stgdict->size = sizeof(void *); |
| 2172 | stgdict->setfunc = NULL; |
| 2173 | stgdict->getfunc = NULL; |
| 2174 | stgdict->ffi_type_pointer = ffi_type_pointer; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2175 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2176 | ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_"); |
| 2177 | if (!ob || !PyLong_Check(ob)) { |
| 2178 | PyErr_SetString(PyExc_TypeError, |
| 2179 | "class must define _flags_ which must be an integer"); |
| 2180 | return -1; |
| 2181 | } |
| 2182 | stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2183 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2184 | /* _argtypes_ is optional... */ |
| 2185 | ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_"); |
| 2186 | if (ob) { |
| 2187 | converters = converters_from_argtypes(ob); |
| 2188 | if (!converters) |
| 2189 | goto error; |
| 2190 | Py_INCREF(ob); |
| 2191 | stgdict->argtypes = ob; |
| 2192 | stgdict->converters = converters; |
| 2193 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2194 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2195 | ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_"); |
| 2196 | if (ob) { |
| 2197 | if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) { |
| 2198 | PyErr_SetString(PyExc_TypeError, |
| 2199 | "_restype_ must be a type, a callable, or None"); |
| 2200 | return -1; |
| 2201 | } |
| 2202 | Py_INCREF(ob); |
| 2203 | stgdict->restype = ob; |
| 2204 | stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_"); |
| 2205 | if (stgdict->checker == NULL) |
| 2206 | PyErr_Clear(); |
| 2207 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2208 | /* XXX later, maybe. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2209 | ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_"); |
| 2210 | if (ob) { |
| 2211 | if (!PyCallable_Check(ob)) { |
| 2212 | PyErr_SetString(PyExc_TypeError, |
| 2213 | "_errcheck_ must be callable"); |
| 2214 | return -1; |
| 2215 | } |
| 2216 | Py_INCREF(ob); |
| 2217 | stgdict->errcheck = ob; |
| 2218 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2219 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2220 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2221 | |
| 2222 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2223 | Py_XDECREF(converters); |
| 2224 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2225 | |
| 2226 | } |
| 2227 | |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2228 | static PyCArgObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2229 | PyCFuncPtrType_paramfunc(CDataObject *self) |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2230 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2231 | PyCArgObject *parg; |
| 2232 | |
| 2233 | parg = PyCArgObject_new(); |
| 2234 | if (parg == NULL) |
| 2235 | return NULL; |
| 2236 | |
| 2237 | parg->tag = 'P'; |
| 2238 | parg->pffi_type = &ffi_type_pointer; |
| 2239 | Py_INCREF(self); |
| 2240 | parg->obj = (PyObject *)self; |
| 2241 | parg->value.p = *(void **)self->b_ptr; |
| 2242 | return parg; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2243 | } |
| 2244 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2245 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2246 | PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2247 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | PyTypeObject *result; |
| 2249 | StgDictObject *stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2250 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2251 | stgdict = (StgDictObject *)PyObject_CallObject( |
| 2252 | (PyObject *)&PyCStgDict_Type, NULL); |
| 2253 | if (!stgdict) |
| 2254 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2256 | stgdict->paramfunc = PyCFuncPtrType_paramfunc; |
| 2257 | /* We do NOT expose the function signature in the format string. It |
| 2258 | is impossible, generally, because the only requirement for the |
| 2259 | argtypes items is that they have a .from_param method - we do not |
| 2260 | know the types of the arguments (although, in practice, most |
| 2261 | argtypes would be a ctypes type). |
| 2262 | */ |
| 2263 | stgdict->format = _ctypes_alloc_format_string(NULL, "X{}"); |
| 2264 | stgdict->flags |= TYPEFLAG_ISPOINTER; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 2265 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | /* create the new instance (which is a class, |
| 2267 | since we are a metatype!) */ |
| 2268 | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
| 2269 | if (result == NULL) { |
| 2270 | Py_DECREF((PyObject *)stgdict); |
| 2271 | return NULL; |
| 2272 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | /* replace the class dict by our updated storage dict */ |
| 2275 | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
| 2276 | Py_DECREF(result); |
| 2277 | Py_DECREF((PyObject *)stgdict); |
| 2278 | return NULL; |
| 2279 | } |
| 2280 | Py_DECREF(result->tp_dict); |
| 2281 | result->tp_dict = (PyObject *)stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2282 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2283 | if (-1 == make_funcptrtype_dict(stgdict)) { |
| 2284 | Py_DECREF(result); |
| 2285 | return NULL; |
| 2286 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2287 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2288 | return (PyObject *)result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2291 | PyTypeObject PyCFuncPtrType_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2292 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2293 | "_ctypes.PyCFuncPtrType", /* tp_name */ |
| 2294 | 0, /* tp_basicsize */ |
| 2295 | 0, /* tp_itemsize */ |
| 2296 | 0, /* tp_dealloc */ |
| 2297 | 0, /* tp_print */ |
| 2298 | 0, /* tp_getattr */ |
| 2299 | 0, /* tp_setattr */ |
| 2300 | 0, /* tp_reserved */ |
| 2301 | 0, /* tp_repr */ |
| 2302 | 0, /* tp_as_number */ |
| 2303 | &CDataType_as_sequence, /* tp_as_sequence */ |
| 2304 | 0, /* tp_as_mapping */ |
| 2305 | 0, /* tp_hash */ |
| 2306 | 0, /* tp_call */ |
| 2307 | 0, /* tp_str */ |
| 2308 | 0, /* tp_getattro */ |
| 2309 | 0, /* tp_setattro */ |
| 2310 | 0, /* tp_as_buffer */ |
| 2311 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 2312 | "metatype for C function pointers", /* tp_doc */ |
| 2313 | (traverseproc)CDataType_traverse, /* tp_traverse */ |
| 2314 | (inquiry)CDataType_clear, /* tp_clear */ |
| 2315 | 0, /* tp_richcompare */ |
| 2316 | 0, /* tp_weaklistoffset */ |
| 2317 | 0, /* tp_iter */ |
| 2318 | 0, /* tp_iternext */ |
| 2319 | CDataType_methods, /* tp_methods */ |
| 2320 | 0, /* tp_members */ |
| 2321 | 0, /* tp_getset */ |
| 2322 | 0, /* tp_base */ |
| 2323 | 0, /* tp_dict */ |
| 2324 | 0, /* tp_descr_get */ |
| 2325 | 0, /* tp_descr_set */ |
| 2326 | 0, /* tp_dictoffset */ |
| 2327 | 0, /* tp_init */ |
| 2328 | 0, /* tp_alloc */ |
| 2329 | PyCFuncPtrType_new, /* tp_new */ |
| 2330 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2331 | }; |
| 2332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2333 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2334 | /***************************************************************** |
| 2335 | * Code to keep needed objects alive |
| 2336 | */ |
| 2337 | |
| 2338 | static CDataObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2339 | PyCData_GetContainer(CDataObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2340 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | while (self->b_base) |
| 2342 | self = self->b_base; |
| 2343 | if (self->b_objects == NULL) { |
| 2344 | if (self->b_length) { |
| 2345 | self->b_objects = PyDict_New(); |
| 2346 | } else { |
| 2347 | Py_INCREF(Py_None); |
| 2348 | self->b_objects = Py_None; |
| 2349 | } |
| 2350 | } |
| 2351 | return self; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | static PyObject * |
| 2355 | GetKeepedObjects(CDataObject *target) |
| 2356 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | return PyCData_GetContainer(target)->b_objects; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | static PyObject * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2361 | unique_key(CDataObject *target, Py_ssize_t index) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2362 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2363 | char string[256]; |
| 2364 | char *cp = string; |
| 2365 | size_t bytes_left; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2366 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2367 | assert(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2); |
| 2368 | cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int)); |
| 2369 | while (target->b_base) { |
| 2370 | bytes_left = sizeof(string) - (cp - string) - 1; |
| 2371 | /* Hex format needs 2 characters per byte */ |
| 2372 | if (bytes_left < sizeof(Py_ssize_t) * 2) { |
| 2373 | PyErr_SetString(PyExc_ValueError, |
| 2374 | "ctypes object structure too deep"); |
| 2375 | return NULL; |
| 2376 | } |
| 2377 | cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, int)); |
| 2378 | target = target->b_base; |
| 2379 | } |
| 2380 | return PyUnicode_FromStringAndSize(string, cp-string); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2381 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2382 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2383 | /* |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 2384 | * Keep a reference to 'keep' in the 'target', at index 'index'. |
| 2385 | * |
| 2386 | * If 'keep' is None, do nothing. |
| 2387 | * |
| 2388 | * Otherwise create a dictionary (if it does not yet exist) id the root |
| 2389 | * objects 'b_objects' item, which will store the 'keep' object under a unique |
| 2390 | * key. |
| 2391 | * |
| 2392 | * The unique_key helper travels the target's b_base pointer down to the root, |
| 2393 | * building a string containing hex-formatted indexes found during traversal, |
| 2394 | * separated by colons. |
| 2395 | * |
| 2396 | * The index tuple is used as a key into the root object's b_objects dict. |
| 2397 | * |
| 2398 | * Note: This function steals a refcount of the third argument, even if it |
| 2399 | * fails! |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2400 | */ |
| 2401 | static int |
Thomas Heller | fe8f862 | 2006-03-14 19:53:09 +0000 | [diff] [blame] | 2402 | KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2404 | int result; |
| 2405 | CDataObject *ob; |
| 2406 | PyObject *key; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2407 | |
| 2408 | /* Optimization: no need to store None */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2409 | if (keep == Py_None) { |
| 2410 | Py_DECREF(Py_None); |
| 2411 | return 0; |
| 2412 | } |
| 2413 | ob = PyCData_GetContainer(target); |
| 2414 | if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) { |
| 2415 | Py_XDECREF(ob->b_objects); |
| 2416 | ob->b_objects = keep; /* refcount consumed */ |
| 2417 | return 0; |
| 2418 | } |
| 2419 | key = unique_key(target, index); |
| 2420 | if (key == NULL) { |
| 2421 | Py_DECREF(keep); |
| 2422 | return -1; |
| 2423 | } |
| 2424 | result = PyDict_SetItem(ob->b_objects, key, keep); |
| 2425 | Py_DECREF(key); |
| 2426 | Py_DECREF(keep); |
| 2427 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2428 | } |
| 2429 | |
| 2430 | /******************************************************************/ |
| 2431 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2432 | PyCData_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2433 | */ |
| 2434 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2435 | PyCData_traverse(CDataObject *self, visitproc visit, void *arg) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2436 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2437 | Py_VISIT(self->b_objects); |
| 2438 | Py_VISIT((PyObject *)self->b_base); |
| 2439 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2440 | } |
| 2441 | |
| 2442 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2443 | PyCData_clear(CDataObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2444 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2445 | Py_CLEAR(self->b_objects); |
| 2446 | if ((self->b_needsfree) |
Antoine Pitrou | 305e1a7 | 2012-12-08 11:05:50 +0100 | [diff] [blame] | 2447 | && _CDataObject_HasExternalBuffer(self)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2448 | PyMem_Free(self->b_ptr); |
| 2449 | self->b_ptr = NULL; |
| 2450 | Py_CLEAR(self->b_base); |
| 2451 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
| 2454 | static void |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2455 | PyCData_dealloc(PyObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2457 | PyCData_clear((CDataObject *)self); |
| 2458 | Py_TYPE(self)->tp_free(self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2459 | } |
| 2460 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2461 | static PyMemberDef PyCData_members[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2462 | { "_b_base_", T_OBJECT, |
| 2463 | offsetof(CDataObject, b_base), READONLY, |
| 2464 | "the base object" }, |
| 2465 | { "_b_needsfree_", T_INT, |
| 2466 | offsetof(CDataObject, b_needsfree), READONLY, |
| 2467 | "whether the object owns the memory or not" }, |
| 2468 | { "_objects", T_OBJECT, |
| 2469 | offsetof(CDataObject, b_objects), READONLY, |
| 2470 | "internal objects tree (NEVER CHANGE THIS OBJECT!)"}, |
| 2471 | { NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2472 | }; |
| 2473 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2474 | static int PyCData_NewGetBuffer(PyObject *_self, Py_buffer *view, int flags) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2476 | CDataObject *self = (CDataObject *)_self; |
| 2477 | StgDictObject *dict = PyObject_stgdict(_self); |
| 2478 | Py_ssize_t i; |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 2479 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2480 | if (view == NULL) return 0; |
Thomas Heller | b041fda | 2008-04-30 17:11:46 +0000 | [diff] [blame] | 2481 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2482 | view->buf = self->b_ptr; |
| 2483 | view->obj = _self; |
| 2484 | Py_INCREF(_self); |
| 2485 | view->len = self->b_size; |
| 2486 | view->readonly = 0; |
| 2487 | /* use default format character if not set */ |
| 2488 | view->format = dict->format ? dict->format : "B"; |
| 2489 | view->ndim = dict->ndim; |
| 2490 | view->shape = dict->shape; |
| 2491 | view->itemsize = self->b_size; |
Amaury Forgeot d'Arc | 326e189 | 2011-08-30 21:40:20 +0200 | [diff] [blame] | 2492 | if (view->itemsize) { |
| 2493 | for (i = 0; i < view->ndim; ++i) { |
| 2494 | view->itemsize /= dict->shape[i]; |
| 2495 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2496 | } |
| 2497 | view->strides = NULL; |
| 2498 | view->suboffsets = NULL; |
| 2499 | view->internal = NULL; |
| 2500 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2501 | } |
| 2502 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2503 | static PyBufferProcs PyCData_as_buffer = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2504 | PyCData_NewGetBuffer, |
| 2505 | NULL, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2506 | }; |
| 2507 | |
| 2508 | /* |
| 2509 | * CData objects are mutable, so they cannot be hashable! |
| 2510 | */ |
Georg Brandl | d49bf5e | 2010-10-18 07:30:06 +0000 | [diff] [blame] | 2511 | static Py_hash_t |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2512 | PyCData_nohash(PyObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2514 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 2515 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 2518 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2519 | PyCData_reduce(PyObject *_self, PyObject *args) |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 2520 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2521 | CDataObject *self = (CDataObject *)_self; |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 2522 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2523 | if (PyObject_stgdict(_self)->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTER)) { |
| 2524 | PyErr_SetString(PyExc_ValueError, |
| 2525 | "ctypes objects containing pointers cannot be pickled"); |
| 2526 | return NULL; |
| 2527 | } |
| 2528 | return Py_BuildValue("O(O(NN))", |
| 2529 | _unpickle, |
| 2530 | Py_TYPE(_self), |
| 2531 | PyObject_GetAttrString(_self, "__dict__"), |
| 2532 | PyBytes_FromStringAndSize(self->b_ptr, self->b_size)); |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
| 2535 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2536 | PyCData_setstate(PyObject *_self, PyObject *args) |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 2537 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2538 | void *data; |
| 2539 | Py_ssize_t len; |
| 2540 | int res; |
| 2541 | PyObject *dict, *mydict; |
| 2542 | CDataObject *self = (CDataObject *)_self; |
| 2543 | if (!PyArg_ParseTuple(args, "Os#", &dict, &data, &len)) |
| 2544 | return NULL; |
| 2545 | if (len > self->b_size) |
| 2546 | len = self->b_size; |
| 2547 | memmove(self->b_ptr, data, len); |
| 2548 | mydict = PyObject_GetAttrString(_self, "__dict__"); |
| 2549 | res = PyDict_Update(mydict, dict); |
| 2550 | Py_DECREF(mydict); |
| 2551 | if (res == -1) |
| 2552 | return NULL; |
| 2553 | Py_INCREF(Py_None); |
| 2554 | return Py_None; |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2557 | /* |
| 2558 | * default __ctypes_from_outparam__ method returns self. |
| 2559 | */ |
| 2560 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2561 | PyCData_from_outparam(PyObject *self, PyObject *args) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2563 | Py_INCREF(self); |
| 2564 | return self; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2565 | } |
| 2566 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2567 | static PyMethodDef PyCData_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2568 | { "__ctypes_from_outparam__", PyCData_from_outparam, METH_NOARGS, }, |
| 2569 | { "__reduce__", PyCData_reduce, METH_NOARGS, }, |
| 2570 | { "__setstate__", PyCData_setstate, METH_VARARGS, }, |
| 2571 | { NULL, NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2572 | }; |
| 2573 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2574 | PyTypeObject PyCData_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | PyVarObject_HEAD_INIT(NULL, 0) |
| 2576 | "_ctypes._CData", |
| 2577 | sizeof(CDataObject), /* tp_basicsize */ |
| 2578 | 0, /* tp_itemsize */ |
| 2579 | PyCData_dealloc, /* tp_dealloc */ |
| 2580 | 0, /* tp_print */ |
| 2581 | 0, /* tp_getattr */ |
| 2582 | 0, /* tp_setattr */ |
| 2583 | 0, /* tp_reserved */ |
| 2584 | 0, /* tp_repr */ |
| 2585 | 0, /* tp_as_number */ |
| 2586 | 0, /* tp_as_sequence */ |
| 2587 | 0, /* tp_as_mapping */ |
| 2588 | PyCData_nohash, /* tp_hash */ |
| 2589 | 0, /* tp_call */ |
| 2590 | 0, /* tp_str */ |
| 2591 | 0, /* tp_getattro */ |
| 2592 | 0, /* tp_setattro */ |
| 2593 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 2594 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 2595 | "XXX to be provided", /* tp_doc */ |
| 2596 | (traverseproc)PyCData_traverse, /* tp_traverse */ |
| 2597 | (inquiry)PyCData_clear, /* tp_clear */ |
| 2598 | 0, /* tp_richcompare */ |
| 2599 | 0, /* tp_weaklistoffset */ |
| 2600 | 0, /* tp_iter */ |
| 2601 | 0, /* tp_iternext */ |
| 2602 | PyCData_methods, /* tp_methods */ |
| 2603 | PyCData_members, /* tp_members */ |
| 2604 | 0, /* tp_getset */ |
| 2605 | 0, /* tp_base */ |
| 2606 | 0, /* tp_dict */ |
| 2607 | 0, /* tp_descr_get */ |
| 2608 | 0, /* tp_descr_set */ |
| 2609 | 0, /* tp_dictoffset */ |
| 2610 | 0, /* tp_init */ |
| 2611 | 0, /* tp_alloc */ |
| 2612 | 0, /* tp_new */ |
| 2613 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2614 | }; |
| 2615 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2616 | static int PyCData_MallocBuffer(CDataObject *obj, StgDictObject *dict) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2617 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | if ((size_t)dict->size <= sizeof(obj->b_value)) { |
| 2619 | /* No need to call malloc, can use the default buffer */ |
| 2620 | obj->b_ptr = (char *)&obj->b_value; |
| 2621 | /* The b_needsfree flag does not mean that we actually did |
| 2622 | call PyMem_Malloc to allocate the memory block; instead it |
| 2623 | means we are the *owner* of the memory and are responsible |
| 2624 | for freeing resources associated with the memory. This is |
| 2625 | also the reason that b_needsfree is exposed to Python. |
| 2626 | */ |
| 2627 | obj->b_needsfree = 1; |
| 2628 | } else { |
| 2629 | /* In python 2.4, and ctypes 0.9.6, the malloc call took about |
| 2630 | 33% of the creation time for c_int(). |
| 2631 | */ |
| 2632 | obj->b_ptr = (char *)PyMem_Malloc(dict->size); |
| 2633 | if (obj->b_ptr == NULL) { |
| 2634 | PyErr_NoMemory(); |
| 2635 | return -1; |
| 2636 | } |
| 2637 | obj->b_needsfree = 1; |
| 2638 | memset(obj->b_ptr, 0, dict->size); |
| 2639 | } |
| 2640 | obj->b_size = dict->size; |
| 2641 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2645 | PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2646 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2647 | CDataObject *cmem; |
| 2648 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2649 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2650 | assert(PyType_Check(type)); |
| 2651 | dict = PyType_stgdict(type); |
| 2652 | if (!dict) { |
| 2653 | PyErr_SetString(PyExc_TypeError, |
| 2654 | "abstract class"); |
| 2655 | return NULL; |
| 2656 | } |
| 2657 | dict->flags |= DICTFLAG_FINAL; |
| 2658 | cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0); |
| 2659 | if (cmem == NULL) |
| 2660 | return NULL; |
| 2661 | assert(CDataObject_Check(cmem)); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2662 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | cmem->b_length = dict->length; |
| 2664 | cmem->b_size = dict->size; |
| 2665 | if (base) { /* use base's buffer */ |
| 2666 | assert(CDataObject_Check(base)); |
| 2667 | cmem->b_ptr = adr; |
| 2668 | cmem->b_needsfree = 0; |
| 2669 | Py_INCREF(base); |
| 2670 | cmem->b_base = (CDataObject *)base; |
| 2671 | cmem->b_index = index; |
| 2672 | } else { /* copy contents of adr */ |
| 2673 | if (-1 == PyCData_MallocBuffer(cmem, dict)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | Py_DECREF(cmem); |
Christian Heimes | 8c4c1f6 | 2013-07-21 16:24:51 +0200 | [diff] [blame] | 2675 | return NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | } |
| 2677 | memcpy(cmem->b_ptr, adr, dict->size); |
| 2678 | cmem->b_index = index; |
| 2679 | } |
| 2680 | return (PyObject *)cmem; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2681 | } |
| 2682 | |
| 2683 | /* |
| 2684 | Box a memory block into a CData instance. |
| 2685 | */ |
| 2686 | PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2687 | PyCData_AtAddress(PyObject *type, void *buf) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2688 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | CDataObject *pd; |
| 2690 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2691 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | assert(PyType_Check(type)); |
| 2693 | dict = PyType_stgdict(type); |
| 2694 | if (!dict) { |
| 2695 | PyErr_SetString(PyExc_TypeError, |
| 2696 | "abstract class"); |
| 2697 | return NULL; |
| 2698 | } |
| 2699 | dict->flags |= DICTFLAG_FINAL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2700 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2701 | pd = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0); |
| 2702 | if (!pd) |
| 2703 | return NULL; |
| 2704 | assert(CDataObject_Check(pd)); |
| 2705 | pd->b_ptr = (char *)buf; |
| 2706 | pd->b_length = dict->length; |
| 2707 | pd->b_size = dict->size; |
| 2708 | return (PyObject *)pd; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2709 | } |
| 2710 | |
| 2711 | /* |
| 2712 | This function returns TRUE for c_int, c_void_p, and these kind of |
| 2713 | classes. FALSE otherwise FALSE also for subclasses of c_int and |
| 2714 | such. |
| 2715 | */ |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2716 | int _ctypes_simple_instance(PyObject *obj) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2717 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2718 | PyTypeObject *type = (PyTypeObject *)obj; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2719 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2720 | if (PyCSimpleTypeObject_Check(type)) |
| 2721 | return type->tp_base != &Simple_Type; |
| 2722 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2723 | } |
| 2724 | |
| 2725 | PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2726 | PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2727 | Py_ssize_t index, Py_ssize_t size, char *adr) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2728 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2729 | StgDictObject *dict; |
| 2730 | if (getfunc) |
| 2731 | return getfunc(adr, size); |
| 2732 | assert(type); |
| 2733 | dict = PyType_stgdict(type); |
| 2734 | if (dict && dict->getfunc && !_ctypes_simple_instance(type)) |
| 2735 | return dict->getfunc(adr, size); |
| 2736 | return PyCData_FromBaseObj(type, src, index, adr); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2737 | } |
| 2738 | |
| 2739 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2740 | Helper function for PyCData_set below. |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2741 | */ |
| 2742 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2743 | _PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | Py_ssize_t size, char *ptr) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2746 | CDataObject *src; |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 2747 | int err; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2749 | if (setfunc) |
| 2750 | return setfunc(ptr, value, size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | if (!CDataObject_Check(value)) { |
| 2753 | StgDictObject *dict = PyType_stgdict(type); |
| 2754 | if (dict && dict->setfunc) |
| 2755 | return dict->setfunc(ptr, value, size); |
| 2756 | /* |
| 2757 | If value is a tuple, we try to call the type with the tuple |
| 2758 | and use the result! |
| 2759 | */ |
| 2760 | assert(PyType_Check(type)); |
| 2761 | if (PyTuple_Check(value)) { |
| 2762 | PyObject *ob; |
| 2763 | PyObject *result; |
| 2764 | ob = PyObject_CallObject(type, value); |
| 2765 | if (ob == NULL) { |
| 2766 | _ctypes_extend_error(PyExc_RuntimeError, "(%s) ", |
| 2767 | ((PyTypeObject *)type)->tp_name); |
| 2768 | return NULL; |
| 2769 | } |
| 2770 | result = _PyCData_set(dst, type, setfunc, ob, |
| 2771 | size, ptr); |
| 2772 | Py_DECREF(ob); |
| 2773 | return result; |
| 2774 | } else if (value == Py_None && PyCPointerTypeObject_Check(type)) { |
| 2775 | *(void **)ptr = NULL; |
| 2776 | Py_INCREF(Py_None); |
| 2777 | return Py_None; |
| 2778 | } else { |
| 2779 | PyErr_Format(PyExc_TypeError, |
| 2780 | "expected %s instance, got %s", |
| 2781 | ((PyTypeObject *)type)->tp_name, |
| 2782 | Py_TYPE(value)->tp_name); |
| 2783 | return NULL; |
| 2784 | } |
| 2785 | } |
| 2786 | src = (CDataObject *)value; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2787 | |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 2788 | err = PyObject_IsInstance(value, type); |
| 2789 | if (err == -1) |
| 2790 | return NULL; |
| 2791 | if (err) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2792 | memcpy(ptr, |
| 2793 | src->b_ptr, |
| 2794 | size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2796 | if (PyCPointerTypeObject_Check(type)) |
| 2797 | /* XXX */; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2799 | value = GetKeepedObjects(src); |
| 2800 | Py_INCREF(value); |
| 2801 | return value; |
| 2802 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2803 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2804 | if (PyCPointerTypeObject_Check(type) |
| 2805 | && ArrayObject_Check(value)) { |
| 2806 | StgDictObject *p1, *p2; |
| 2807 | PyObject *keep; |
| 2808 | p1 = PyObject_stgdict(value); |
| 2809 | assert(p1); /* Cannot be NULL for array instances */ |
| 2810 | p2 = PyType_stgdict(type); |
| 2811 | assert(p2); /* Cannot be NULL for pointer types */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2812 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2813 | if (p1->proto != p2->proto) { |
| 2814 | PyErr_Format(PyExc_TypeError, |
| 2815 | "incompatible types, %s instance instead of %s instance", |
| 2816 | Py_TYPE(value)->tp_name, |
| 2817 | ((PyTypeObject *)type)->tp_name); |
| 2818 | return NULL; |
| 2819 | } |
| 2820 | *(void **)ptr = src->b_ptr; |
| 2821 | |
| 2822 | keep = GetKeepedObjects(src); |
| 2823 | /* |
| 2824 | We are assigning an array object to a field which represents |
| 2825 | a pointer. This has the same effect as converting an array |
| 2826 | into a pointer. So, again, we have to keep the whole object |
| 2827 | pointed to (which is the array in this case) alive, and not |
| 2828 | only it's object list. So we create a tuple, containing |
| 2829 | b_objects list PLUS the array itself, and return that! |
| 2830 | */ |
| 2831 | return PyTuple_Pack(2, keep, value); |
| 2832 | } |
| 2833 | PyErr_Format(PyExc_TypeError, |
| 2834 | "incompatible types, %s instance instead of %s instance", |
| 2835 | Py_TYPE(value)->tp_name, |
| 2836 | ((PyTypeObject *)type)->tp_name); |
| 2837 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
| 2840 | /* |
| 2841 | * Set a slice in object 'dst', which has the type 'type', |
| 2842 | * to the value 'value'. |
| 2843 | */ |
| 2844 | int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2845 | PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2846 | Py_ssize_t index, Py_ssize_t size, char *ptr) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2848 | CDataObject *mem = (CDataObject *)dst; |
| 2849 | PyObject *result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | if (!CDataObject_Check(dst)) { |
| 2852 | PyErr_SetString(PyExc_TypeError, |
| 2853 | "not a ctype instance"); |
| 2854 | return -1; |
| 2855 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2857 | result = _PyCData_set(mem, type, setfunc, value, |
| 2858 | size, ptr); |
| 2859 | if (result == NULL) |
| 2860 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2861 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | /* KeepRef steals a refcount from it's last argument */ |
| 2863 | /* If KeepRef fails, we are stumped. The dst memory block has already |
| 2864 | been changed */ |
| 2865 | return KeepRef(mem, index, result); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2869 | /******************************************************************/ |
| 2870 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2871 | GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2872 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2873 | CDataObject *obj; |
| 2874 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | dict = PyType_stgdict((PyObject *)type); |
| 2877 | if (!dict) { |
| 2878 | PyErr_SetString(PyExc_TypeError, |
| 2879 | "abstract class"); |
| 2880 | return NULL; |
| 2881 | } |
| 2882 | dict->flags |= DICTFLAG_FINAL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2884 | obj = (CDataObject *)type->tp_alloc(type, 0); |
| 2885 | if (!obj) |
| 2886 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | obj->b_base = NULL; |
| 2889 | obj->b_index = 0; |
| 2890 | obj->b_objects = NULL; |
| 2891 | obj->b_length = dict->length; |
| 2892 | |
| 2893 | if (-1 == PyCData_MallocBuffer(obj, dict)) { |
| 2894 | Py_DECREF(obj); |
| 2895 | return NULL; |
| 2896 | } |
| 2897 | return (PyObject *)obj; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2898 | } |
| 2899 | /*****************************************************************/ |
| 2900 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2901 | PyCFuncPtr_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2902 | */ |
| 2903 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2904 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2905 | PyCFuncPtr_set_errcheck(PyCFuncPtrObject *self, PyObject *ob) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2906 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | if (ob && !PyCallable_Check(ob)) { |
| 2908 | PyErr_SetString(PyExc_TypeError, |
| 2909 | "the errcheck attribute must be callable"); |
| 2910 | return -1; |
| 2911 | } |
| 2912 | Py_XDECREF(self->errcheck); |
| 2913 | Py_XINCREF(ob); |
| 2914 | self->errcheck = ob; |
| 2915 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
| 2918 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2919 | PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | if (self->errcheck) { |
| 2922 | Py_INCREF(self->errcheck); |
| 2923 | return self->errcheck; |
| 2924 | } |
| 2925 | Py_INCREF(Py_None); |
| 2926 | return Py_None; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2930 | PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2931 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2932 | if (ob == NULL) { |
| 2933 | Py_XDECREF(self->restype); |
| 2934 | self->restype = NULL; |
| 2935 | Py_XDECREF(self->checker); |
| 2936 | self->checker = NULL; |
| 2937 | return 0; |
| 2938 | } |
| 2939 | if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) { |
| 2940 | PyErr_SetString(PyExc_TypeError, |
| 2941 | "restype must be a type, a callable, or None"); |
| 2942 | return -1; |
| 2943 | } |
| 2944 | Py_XDECREF(self->checker); |
| 2945 | Py_XDECREF(self->restype); |
| 2946 | Py_INCREF(ob); |
| 2947 | self->restype = ob; |
| 2948 | self->checker = PyObject_GetAttrString(ob, "_check_retval_"); |
| 2949 | if (self->checker == NULL) |
| 2950 | PyErr_Clear(); |
| 2951 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2952 | } |
| 2953 | |
| 2954 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2955 | PyCFuncPtr_get_restype(PyCFuncPtrObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | StgDictObject *dict; |
| 2958 | if (self->restype) { |
| 2959 | Py_INCREF(self->restype); |
| 2960 | return self->restype; |
| 2961 | } |
| 2962 | dict = PyObject_stgdict((PyObject *)self); |
| 2963 | assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */ |
| 2964 | if (dict->restype) { |
| 2965 | Py_INCREF(dict->restype); |
| 2966 | return dict->restype; |
| 2967 | } else { |
| 2968 | Py_INCREF(Py_None); |
| 2969 | return Py_None; |
| 2970 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
| 2973 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2974 | PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2975 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2976 | PyObject *converters; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2978 | if (ob == NULL || ob == Py_None) { |
| 2979 | Py_XDECREF(self->converters); |
| 2980 | self->converters = NULL; |
| 2981 | Py_XDECREF(self->argtypes); |
| 2982 | self->argtypes = NULL; |
| 2983 | } else { |
| 2984 | converters = converters_from_argtypes(ob); |
| 2985 | if (!converters) |
| 2986 | return -1; |
| 2987 | Py_XDECREF(self->converters); |
| 2988 | self->converters = converters; |
| 2989 | Py_XDECREF(self->argtypes); |
| 2990 | Py_INCREF(ob); |
| 2991 | self->argtypes = ob; |
| 2992 | } |
| 2993 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2994 | } |
| 2995 | |
| 2996 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 2997 | PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 2998 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | StgDictObject *dict; |
| 3000 | if (self->argtypes) { |
| 3001 | Py_INCREF(self->argtypes); |
| 3002 | return self->argtypes; |
| 3003 | } |
| 3004 | dict = PyObject_stgdict((PyObject *)self); |
| 3005 | assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */ |
| 3006 | if (dict->argtypes) { |
| 3007 | Py_INCREF(dict->argtypes); |
| 3008 | return dict->argtypes; |
| 3009 | } else { |
| 3010 | Py_INCREF(Py_None); |
| 3011 | return Py_None; |
| 3012 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3015 | static PyGetSetDef PyCFuncPtr_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3016 | { "errcheck", (getter)PyCFuncPtr_get_errcheck, (setter)PyCFuncPtr_set_errcheck, |
| 3017 | "a function to check for errors", NULL }, |
| 3018 | { "restype", (getter)PyCFuncPtr_get_restype, (setter)PyCFuncPtr_set_restype, |
| 3019 | "specify the result type", NULL }, |
| 3020 | { "argtypes", (getter)PyCFuncPtr_get_argtypes, |
| 3021 | (setter)PyCFuncPtr_set_argtypes, |
| 3022 | "specify the argument types", NULL }, |
| 3023 | { NULL, NULL } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3024 | }; |
| 3025 | |
| 3026 | #ifdef MS_WIN32 |
| 3027 | static PPROC FindAddress(void *handle, char *name, PyObject *type) |
| 3028 | { |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3029 | #ifdef MS_WIN64 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3030 | /* win64 has no stdcall calling conv, so it should |
| 3031 | also not have the name mangling of it. |
| 3032 | */ |
| 3033 | return (PPROC)GetProcAddress(handle, name); |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3034 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3035 | PPROC address; |
| 3036 | char *mangled_name; |
| 3037 | int i; |
| 3038 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3039 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3040 | address = (PPROC)GetProcAddress(handle, name); |
| 3041 | if (address) |
| 3042 | return address; |
| 3043 | if (((size_t)name & ~0xFFFF) == 0) { |
| 3044 | return NULL; |
| 3045 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3046 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3047 | dict = PyType_stgdict((PyObject *)type); |
| 3048 | /* It should not happen that dict is NULL, but better be safe */ |
| 3049 | if (dict==NULL || dict->flags & FUNCFLAG_CDECL) |
| 3050 | return address; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3051 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | /* for stdcall, try mangled names: |
| 3053 | funcname -> _funcname@<n> |
| 3054 | where n is 0, 4, 8, 12, ..., 128 |
| 3055 | */ |
| 3056 | mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */ |
| 3057 | if (!mangled_name) |
| 3058 | return NULL; |
| 3059 | for (i = 0; i < 32; ++i) { |
| 3060 | sprintf(mangled_name, "_%s@%d", name, i*4); |
| 3061 | address = (PPROC)GetProcAddress(handle, mangled_name); |
| 3062 | if (address) |
| 3063 | return address; |
| 3064 | } |
| 3065 | return NULL; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 3066 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3067 | } |
| 3068 | #endif |
| 3069 | |
| 3070 | /* Return 1 if usable, 0 else and exception set. */ |
| 3071 | static int |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 3072 | _check_outarg_type(PyObject *arg, Py_ssize_t index) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3073 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | StgDictObject *dict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3075 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | if (PyCPointerTypeObject_Check(arg)) |
| 3077 | return 1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3078 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | if (PyCArrayTypeObject_Check(arg)) |
| 3080 | return 1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3081 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3082 | dict = PyType_stgdict(arg); |
| 3083 | if (dict |
| 3084 | /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ |
| 3085 | && PyUnicode_Check(dict->proto) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3086 | /* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter type */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3087 | && (strchr("PzZ", _PyUnicode_AsString(dict->proto)[0]))) { |
| 3088 | return 1; |
| 3089 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3090 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | PyErr_Format(PyExc_TypeError, |
| 3092 | "'out' parameter %d must be a pointer type, not %s", |
| 3093 | Py_SAFE_DOWNCAST(index, Py_ssize_t, int), |
| 3094 | PyType_Check(arg) ? |
| 3095 | ((PyTypeObject *)arg)->tp_name : |
| 3096 | Py_TYPE(arg)->tp_name); |
| 3097 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /* Returns 1 on success, 0 on error */ |
| 3101 | static int |
| 3102 | _validate_paramflags(PyTypeObject *type, PyObject *paramflags) |
| 3103 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3104 | Py_ssize_t i, len; |
| 3105 | StgDictObject *dict; |
| 3106 | PyObject *argtypes; |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 3107 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | dict = PyType_stgdict((PyObject *)type); |
| 3109 | assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */ |
| 3110 | argtypes = dict->argtypes; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3111 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | if (paramflags == NULL || dict->argtypes == NULL) |
| 3113 | return 1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | if (!PyTuple_Check(paramflags)) { |
| 3116 | PyErr_SetString(PyExc_TypeError, |
| 3117 | "paramflags must be a tuple or None"); |
| 3118 | return 0; |
| 3119 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | len = PyTuple_GET_SIZE(paramflags); |
| 3122 | if (len != PyTuple_GET_SIZE(dict->argtypes)) { |
| 3123 | PyErr_SetString(PyExc_ValueError, |
| 3124 | "paramflags must have the same length as argtypes"); |
| 3125 | return 0; |
| 3126 | } |
| 3127 | |
| 3128 | for (i = 0; i < len; ++i) { |
| 3129 | PyObject *item = PyTuple_GET_ITEM(paramflags, i); |
| 3130 | int flag; |
| 3131 | char *name; |
| 3132 | PyObject *defval; |
| 3133 | PyObject *typ; |
| 3134 | if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) { |
| 3135 | PyErr_SetString(PyExc_TypeError, |
| 3136 | "paramflags must be a sequence of (int [,string [,value]]) tuples"); |
| 3137 | return 0; |
| 3138 | } |
| 3139 | typ = PyTuple_GET_ITEM(argtypes, i); |
| 3140 | switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { |
| 3141 | case 0: |
| 3142 | case PARAMFLAG_FIN: |
| 3143 | case PARAMFLAG_FIN | PARAMFLAG_FLCID: |
| 3144 | case PARAMFLAG_FIN | PARAMFLAG_FOUT: |
| 3145 | break; |
| 3146 | case PARAMFLAG_FOUT: |
| 3147 | if (!_check_outarg_type(typ, i+1)) |
| 3148 | return 0; |
| 3149 | break; |
| 3150 | default: |
| 3151 | PyErr_Format(PyExc_TypeError, |
| 3152 | "paramflag value %d not supported", |
| 3153 | flag); |
| 3154 | return 0; |
| 3155 | } |
| 3156 | } |
| 3157 | return 1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3158 | } |
| 3159 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3160 | static int |
| 3161 | _get_name(PyObject *obj, char **pname) |
| 3162 | { |
| 3163 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | if (PyLong_Check(obj)) { |
| 3165 | /* We have to use MAKEINTRESOURCEA for Windows CE. |
| 3166 | Works on Windows as well, of course. |
| 3167 | */ |
| 3168 | *pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF); |
| 3169 | return 1; |
| 3170 | } |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3171 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | if (PyBytes_Check(obj)) { |
| 3173 | *pname = PyBytes_AS_STRING(obj); |
| 3174 | return *pname ? 1 : 0; |
| 3175 | } |
| 3176 | if (PyUnicode_Check(obj)) { |
| 3177 | *pname = _PyUnicode_AsString(obj); |
| 3178 | return *pname ? 1 : 0; |
| 3179 | } |
| 3180 | PyErr_SetString(PyExc_TypeError, |
| 3181 | "function name must be string or integer"); |
| 3182 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3186 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3187 | PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3188 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | char *name; |
| 3190 | int (* address)(void); |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3191 | PyObject *ftuple; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | PyObject *dll; |
| 3193 | PyObject *obj; |
| 3194 | PyCFuncPtrObject *self; |
| 3195 | void *handle; |
| 3196 | PyObject *paramflags = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3197 | |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3198 | if (!PyArg_ParseTuple(args, "O|O", &ftuple, ¶mflags)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3199 | return NULL; |
| 3200 | if (paramflags == Py_None) |
| 3201 | paramflags = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3202 | |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3203 | ftuple = PySequence_Tuple(ftuple); |
| 3204 | if (!ftuple) |
| 3205 | /* Here ftuple is a borrowed reference */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3206 | return NULL; |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3207 | |
| 3208 | if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) { |
| 3209 | Py_DECREF(ftuple); |
| 3210 | return NULL; |
| 3211 | } |
| 3212 | |
| 3213 | obj = PyObject_GetAttrString(dll, "_handle"); |
| 3214 | if (!obj) { |
| 3215 | Py_DECREF(ftuple); |
| 3216 | return NULL; |
| 3217 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | if (!PyLong_Check(obj)) { |
| 3219 | PyErr_SetString(PyExc_TypeError, |
| 3220 | "the _handle attribute of the second argument must be an integer"); |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3221 | Py_DECREF(ftuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3222 | Py_DECREF(obj); |
| 3223 | return NULL; |
| 3224 | } |
| 3225 | handle = (void *)PyLong_AsVoidPtr(obj); |
| 3226 | Py_DECREF(obj); |
| 3227 | if (PyErr_Occurred()) { |
| 3228 | PyErr_SetString(PyExc_ValueError, |
| 3229 | "could not convert the _handle attribute to a pointer"); |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3230 | Py_DECREF(ftuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | return NULL; |
| 3232 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3233 | |
| 3234 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | address = FindAddress(handle, name, (PyObject *)type); |
| 3236 | if (!address) { |
| 3237 | if (!IS_INTRESOURCE(name)) |
| 3238 | PyErr_Format(PyExc_AttributeError, |
| 3239 | "function '%s' not found", |
| 3240 | name); |
| 3241 | else |
| 3242 | PyErr_Format(PyExc_AttributeError, |
| 3243 | "function ordinal %d not found", |
| 3244 | (WORD)(size_t)name); |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3245 | Py_DECREF(ftuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3246 | return NULL; |
| 3247 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3248 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | address = (PPROC)ctypes_dlsym(handle, name); |
| 3250 | if (!address) { |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3251 | #ifdef __CYGWIN__ |
| 3252 | /* dlerror() isn't very helpful on cygwin */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | PyErr_Format(PyExc_AttributeError, |
| 3254 | "function '%s' not found (%s) ", |
| 3255 | name); |
Benjamin Peterson | d75fcb4 | 2009-02-19 04:22:03 +0000 | [diff] [blame] | 3256 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3257 | PyErr_SetString(PyExc_AttributeError, ctypes_dlerror()); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3258 | #endif |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3259 | Py_DECREF(ftuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | return NULL; |
| 3261 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3262 | #endif |
Serhiy Storchaka | 19c4e0d | 2013-02-04 12:47:24 +0200 | [diff] [blame] | 3263 | Py_INCREF(dll); /* for KeepRef */ |
| 3264 | Py_DECREF(ftuple); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | if (!_validate_paramflags(type, paramflags)) |
| 3266 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3267 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds); |
| 3269 | if (!self) |
| 3270 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3271 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3272 | Py_XINCREF(paramflags); |
| 3273 | self->paramflags = paramflags; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | *(void **)self->b_ptr = address; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3276 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3277 | if (-1 == KeepRef((CDataObject *)self, 0, dll)) { |
| 3278 | Py_DECREF((PyObject *)self); |
| 3279 | return NULL; |
| 3280 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3281 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | Py_INCREF(self); |
| 3283 | self->callable = (PyObject *)self; |
| 3284 | return (PyObject *)self; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3285 | } |
| 3286 | |
| 3287 | #ifdef MS_WIN32 |
| 3288 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3289 | PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3290 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | PyCFuncPtrObject *self; |
| 3292 | int index; |
| 3293 | char *name = NULL; |
| 3294 | PyObject *paramflags = NULL; |
| 3295 | GUID *iid = NULL; |
| 3296 | Py_ssize_t iid_len = 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3298 | if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, ¶mflags, &iid, &iid_len)) |
| 3299 | return NULL; |
| 3300 | if (paramflags == Py_None) |
| 3301 | paramflags = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3302 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3303 | if (!_validate_paramflags(type, paramflags)) |
| 3304 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3305 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3306 | self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds); |
| 3307 | self->index = index + 0x1000; |
| 3308 | Py_XINCREF(paramflags); |
| 3309 | self->paramflags = paramflags; |
| 3310 | if (iid_len == sizeof(GUID)) |
| 3311 | self->iid = iid; |
| 3312 | return (PyObject *)self; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3313 | } |
| 3314 | #endif |
| 3315 | |
| 3316 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3317 | PyCFuncPtr_new accepts different argument lists in addition to the standard |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3318 | _basespec_ keyword arg: |
| 3319 | |
| 3320 | one argument form |
| 3321 | "i" - function address |
| 3322 | "O" - must be a callable, creates a C callable function |
| 3323 | |
| 3324 | two or more argument forms (the third argument is a paramflags tuple) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3325 | "(sO)|..." - (function name, dll object (with an integer handle)), paramflags |
| 3326 | "(iO)|..." - (function ordinal, dll object (with an integer handle)), paramflags |
| 3327 | "is|..." - vtable index, method name, creates callable calling COM vtbl |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3328 | */ |
| 3329 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3330 | PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3331 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3332 | PyCFuncPtrObject *self; |
| 3333 | PyObject *callable; |
| 3334 | StgDictObject *dict; |
| 3335 | CThunkObject *thunk; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3336 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3337 | if (PyTuple_GET_SIZE(args) == 0) |
| 3338 | return GenericPyCData_new(type, args, kwds); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0))) |
| 3341 | return PyCFuncPtr_FromDll(type, args, kwds); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3342 | |
| 3343 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3344 | if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0))) |
| 3345 | return PyCFuncPtr_FromVtblIndex(type, args, kwds); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3346 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3347 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3348 | if (1 == PyTuple_GET_SIZE(args) |
| 3349 | && (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) { |
| 3350 | CDataObject *ob; |
| 3351 | void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0)); |
| 3352 | if (ptr == NULL && PyErr_Occurred()) |
| 3353 | return NULL; |
| 3354 | ob = (CDataObject *)GenericPyCData_new(type, args, kwds); |
| 3355 | if (ob == NULL) |
| 3356 | return NULL; |
| 3357 | *(void **)ob->b_ptr = ptr; |
| 3358 | return (PyObject *)ob; |
| 3359 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3361 | if (!PyArg_ParseTuple(args, "O", &callable)) |
| 3362 | return NULL; |
| 3363 | if (!PyCallable_Check(callable)) { |
| 3364 | PyErr_SetString(PyExc_TypeError, |
| 3365 | "argument must be callable or integer function address"); |
| 3366 | return NULL; |
| 3367 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3368 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3369 | /* XXX XXX This would allow to pass additional options. For COM |
| 3370 | method *implementations*, we would probably want different |
| 3371 | behaviour than in 'normal' callback functions: return a HRESULT if |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 3372 | an exception occurs in the callback, and print the traceback not |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3373 | only on the console, but also to OutputDebugString() or something |
| 3374 | like that. |
| 3375 | */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3376 | /* |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3377 | if (kwds && PyDict_GetItemString(kwds, "options")) { |
| 3378 | ... |
| 3379 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3380 | */ |
| 3381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3382 | dict = PyType_stgdict((PyObject *)type); |
| 3383 | /* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */ |
| 3384 | if (!dict || !dict->argtypes) { |
| 3385 | PyErr_SetString(PyExc_TypeError, |
| 3386 | "cannot construct instance of this class:" |
| 3387 | " no argtypes"); |
| 3388 | return NULL; |
| 3389 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3390 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3391 | thunk = _ctypes_alloc_callback(callable, |
| 3392 | dict->argtypes, |
| 3393 | dict->restype, |
| 3394 | dict->flags); |
| 3395 | if (!thunk) |
| 3396 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds); |
| 3399 | if (self == NULL) { |
| 3400 | Py_DECREF(thunk); |
| 3401 | return NULL; |
| 3402 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | Py_INCREF(callable); |
| 3405 | self->callable = callable; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3407 | self->thunk = thunk; |
Thomas Heller | 864cc67 | 2010-08-08 17:58:53 +0000 | [diff] [blame] | 3408 | *(void **)self->b_ptr = (void *)thunk->pcl_exec; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3409 | |
| 3410 | Py_INCREF((PyObject *)thunk); /* for KeepRef */ |
| 3411 | if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) { |
| 3412 | Py_DECREF((PyObject *)self); |
| 3413 | return NULL; |
| 3414 | } |
| 3415 | return (PyObject *)self; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3416 | } |
| 3417 | |
| 3418 | |
| 3419 | /* |
| 3420 | _byref consumes a refcount to its argument |
| 3421 | */ |
| 3422 | static PyObject * |
| 3423 | _byref(PyObject *obj) |
| 3424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3425 | PyCArgObject *parg; |
| 3426 | if (!CDataObject_Check(obj)) { |
| 3427 | PyErr_SetString(PyExc_TypeError, |
| 3428 | "expected CData instance"); |
| 3429 | return NULL; |
| 3430 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3431 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | parg = PyCArgObject_new(); |
| 3433 | if (parg == NULL) { |
| 3434 | Py_DECREF(obj); |
| 3435 | return NULL; |
| 3436 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3438 | parg->tag = 'P'; |
| 3439 | parg->pffi_type = &ffi_type_pointer; |
| 3440 | parg->obj = obj; |
| 3441 | parg->value.p = ((CDataObject *)obj)->b_ptr; |
| 3442 | return (PyObject *)parg; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3443 | } |
| 3444 | |
| 3445 | static PyObject * |
Guido van Rossum | 97f9d4f | 2007-10-24 18:41:19 +0000 | [diff] [blame] | 3446 | _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3447 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3448 | PyObject *v; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3449 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3450 | if (*pindex < PyTuple_GET_SIZE(inargs)) { |
| 3451 | v = PyTuple_GET_ITEM(inargs, *pindex); |
| 3452 | ++*pindex; |
| 3453 | Py_INCREF(v); |
| 3454 | return v; |
| 3455 | } |
Christian Heimes | 6ca8a05 | 2013-07-26 23:04:29 +0200 | [diff] [blame] | 3456 | if (kwds && name && (v = PyDict_GetItem(kwds, name))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3457 | ++*pindex; |
| 3458 | Py_INCREF(v); |
| 3459 | return v; |
| 3460 | } |
| 3461 | if (defval) { |
| 3462 | Py_INCREF(defval); |
| 3463 | return defval; |
| 3464 | } |
| 3465 | /* we can't currently emit a better error message */ |
| 3466 | if (name) |
| 3467 | PyErr_Format(PyExc_TypeError, |
| 3468 | "required argument '%S' missing", name); |
| 3469 | else |
| 3470 | PyErr_Format(PyExc_TypeError, |
| 3471 | "not enough arguments"); |
| 3472 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | /* |
| 3476 | This function implements higher level functionality plus the ability to call |
| 3477 | functions with keyword arguments by looking at parameter flags. parameter |
| 3478 | flags is a tuple of 1, 2 or 3-tuples. The first entry in each is an integer |
| 3479 | specifying the direction of the data transfer for this parameter - 'in', |
| 3480 | 'out' or 'inout' (zero means the same as 'in'). The second entry is the |
| 3481 | parameter name, and the third is the default value if the parameter is |
| 3482 | missing in the function call. |
| 3483 | |
| 3484 | This function builds and returns a new tuple 'callargs' which contains the |
| 3485 | parameters to use in the call. Items on this tuple are copied from the |
| 3486 | 'inargs' tuple for 'in' and 'in, out' parameters, and constructed from the |
| 3487 | 'argtypes' tuple for 'out' parameters. It also calculates numretvals which |
| 3488 | is the number of return values for the function, outmask/inoutmask are |
| 3489 | bitmasks containing indexes into the callargs tuple specifying which |
| 3490 | parameters have to be returned. _build_result builds the return value of the |
| 3491 | function. |
| 3492 | */ |
| 3493 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3494 | _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3495 | PyObject *inargs, PyObject *kwds, |
| 3496 | int *poutmask, int *pinoutmask, unsigned int *pnumretvals) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3497 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3498 | PyObject *paramflags = self->paramflags; |
| 3499 | PyObject *callargs; |
| 3500 | StgDictObject *dict; |
| 3501 | Py_ssize_t i, len; |
| 3502 | int inargs_index = 0; |
| 3503 | /* It's a little bit difficult to determine how many arguments the |
| 3504 | function call requires/accepts. For simplicity, we count the consumed |
| 3505 | args and compare this to the number of supplied args. */ |
| 3506 | Py_ssize_t actual_args; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3508 | *poutmask = 0; |
| 3509 | *pinoutmask = 0; |
| 3510 | *pnumretvals = 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3512 | /* Trivial cases, where we either return inargs itself, or a slice of it. */ |
| 3513 | if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) { |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3514 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | if (self->index) |
| 3516 | return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs)); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3517 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3518 | Py_INCREF(inargs); |
| 3519 | return inargs; |
| 3520 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3521 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3522 | len = PyTuple_GET_SIZE(argtypes); |
| 3523 | callargs = PyTuple_New(len); /* the argument tuple we build */ |
| 3524 | if (callargs == NULL) |
| 3525 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3526 | |
| 3527 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3528 | /* For a COM method, skip the first arg */ |
| 3529 | if (self->index) { |
| 3530 | inargs_index = 1; |
| 3531 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3532 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | for (i = 0; i < len; ++i) { |
| 3534 | PyObject *item = PyTuple_GET_ITEM(paramflags, i); |
| 3535 | PyObject *ob; |
| 3536 | int flag; |
| 3537 | PyObject *name = NULL; |
| 3538 | PyObject *defval = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3539 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3540 | /* This way seems to be ~2 us faster than the PyArg_ParseTuple |
| 3541 | calls below. */ |
| 3542 | /* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */ |
| 3543 | Py_ssize_t tsize = PyTuple_GET_SIZE(item); |
| 3544 | flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0)); |
| 3545 | name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL; |
| 3546 | defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3548 | switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { |
| 3549 | case PARAMFLAG_FIN | PARAMFLAG_FLCID: |
| 3550 | /* ['in', 'lcid'] parameter. Always taken from defval, |
| 3551 | if given, else the integer 0. */ |
| 3552 | if (defval == NULL) { |
| 3553 | defval = PyLong_FromLong(0); |
| 3554 | if (defval == NULL) |
| 3555 | goto error; |
| 3556 | } else |
| 3557 | Py_INCREF(defval); |
| 3558 | PyTuple_SET_ITEM(callargs, i, defval); |
| 3559 | break; |
| 3560 | case (PARAMFLAG_FIN | PARAMFLAG_FOUT): |
| 3561 | *pinoutmask |= (1 << i); /* mark as inout arg */ |
| 3562 | (*pnumretvals)++; |
| 3563 | /* fall through to PARAMFLAG_FIN... */ |
| 3564 | case 0: |
| 3565 | case PARAMFLAG_FIN: |
| 3566 | /* 'in' parameter. Copy it from inargs. */ |
| 3567 | ob =_get_arg(&inargs_index, name, defval, inargs, kwds); |
| 3568 | if (ob == NULL) |
| 3569 | goto error; |
| 3570 | PyTuple_SET_ITEM(callargs, i, ob); |
| 3571 | break; |
| 3572 | case PARAMFLAG_FOUT: |
| 3573 | /* XXX Refactor this code into a separate function. */ |
| 3574 | /* 'out' parameter. |
| 3575 | argtypes[i] must be a POINTER to a c type. |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3577 | Cannot by supplied in inargs, but a defval will be used |
| 3578 | if available. XXX Should we support getting it from kwds? |
| 3579 | */ |
| 3580 | if (defval) { |
| 3581 | /* XXX Using mutable objects as defval will |
| 3582 | make the function non-threadsafe, unless we |
| 3583 | copy the object in each invocation */ |
| 3584 | Py_INCREF(defval); |
| 3585 | PyTuple_SET_ITEM(callargs, i, defval); |
| 3586 | *poutmask |= (1 << i); /* mark as out arg */ |
| 3587 | (*pnumretvals)++; |
| 3588 | break; |
| 3589 | } |
| 3590 | ob = PyTuple_GET_ITEM(argtypes, i); |
| 3591 | dict = PyType_stgdict(ob); |
| 3592 | if (dict == NULL) { |
| 3593 | /* Cannot happen: _validate_paramflags() |
| 3594 | would not accept such an object */ |
| 3595 | PyErr_Format(PyExc_RuntimeError, |
| 3596 | "NULL stgdict unexpected"); |
| 3597 | goto error; |
| 3598 | } |
| 3599 | if (PyUnicode_Check(dict->proto)) { |
| 3600 | PyErr_Format( |
| 3601 | PyExc_TypeError, |
| 3602 | "%s 'out' parameter must be passed as default value", |
| 3603 | ((PyTypeObject *)ob)->tp_name); |
| 3604 | goto error; |
| 3605 | } |
| 3606 | if (PyCArrayTypeObject_Check(ob)) |
| 3607 | ob = PyObject_CallObject(ob, NULL); |
| 3608 | else |
| 3609 | /* Create an instance of the pointed-to type */ |
| 3610 | ob = PyObject_CallObject(dict->proto, NULL); |
| 3611 | /* |
| 3612 | XXX Is the following correct any longer? |
| 3613 | We must not pass a byref() to the array then but |
| 3614 | the array instance itself. Then, we cannot retrive |
| 3615 | the result from the PyCArgObject. |
| 3616 | */ |
| 3617 | if (ob == NULL) |
| 3618 | goto error; |
| 3619 | /* The .from_param call that will ocurr later will pass this |
| 3620 | as a byref parameter. */ |
| 3621 | PyTuple_SET_ITEM(callargs, i, ob); |
| 3622 | *poutmask |= (1 << i); /* mark as out arg */ |
| 3623 | (*pnumretvals)++; |
| 3624 | break; |
| 3625 | default: |
| 3626 | PyErr_Format(PyExc_ValueError, |
| 3627 | "paramflag %d not yet implemented", flag); |
| 3628 | goto error; |
| 3629 | break; |
| 3630 | } |
| 3631 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3632 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3633 | /* We have counted the arguments we have consumed in 'inargs_index'. This |
| 3634 | must be the same as len(inargs) + len(kwds), otherwise we have |
| 3635 | either too much or not enough arguments. */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3636 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0); |
| 3638 | if (actual_args != inargs_index) { |
| 3639 | /* When we have default values or named parameters, this error |
| 3640 | message is misleading. See unittests/test_paramflags.py |
| 3641 | */ |
| 3642 | PyErr_Format(PyExc_TypeError, |
| 3643 | "call takes exactly %d arguments (%zd given)", |
| 3644 | inargs_index, actual_args); |
| 3645 | goto error; |
| 3646 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3648 | /* outmask is a bitmask containing indexes into callargs. Items at |
| 3649 | these indexes contain values to return. |
| 3650 | */ |
| 3651 | return callargs; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3652 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3653 | Py_DECREF(callargs); |
| 3654 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3655 | } |
| 3656 | |
| 3657 | /* See also: |
| 3658 | http://msdn.microsoft.com/library/en-us/com/html/769127a1-1a14-4ed4-9d38-7cf3e571b661.asp |
| 3659 | */ |
| 3660 | /* |
| 3661 | Build return value of a function. |
| 3662 | |
| 3663 | Consumes the refcount on result and callargs. |
| 3664 | */ |
| 3665 | static PyObject * |
| 3666 | _build_result(PyObject *result, PyObject *callargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3667 | int outmask, int inoutmask, unsigned int numretvals) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3668 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3669 | unsigned int i, index; |
| 3670 | int bit; |
| 3671 | PyObject *tup = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3673 | if (callargs == NULL) |
| 3674 | return result; |
| 3675 | if (result == NULL || numretvals == 0) { |
| 3676 | Py_DECREF(callargs); |
| 3677 | return result; |
| 3678 | } |
| 3679 | Py_DECREF(result); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3680 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3681 | /* tup will not be allocated if numretvals == 1 */ |
| 3682 | /* allocate tuple to hold the result */ |
| 3683 | if (numretvals > 1) { |
| 3684 | tup = PyTuple_New(numretvals); |
| 3685 | if (tup == NULL) { |
| 3686 | Py_DECREF(callargs); |
| 3687 | return NULL; |
| 3688 | } |
| 3689 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3690 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3691 | index = 0; |
| 3692 | for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) { |
| 3693 | PyObject *v; |
| 3694 | if (bit & inoutmask) { |
| 3695 | v = PyTuple_GET_ITEM(callargs, i); |
| 3696 | Py_INCREF(v); |
| 3697 | if (numretvals == 1) { |
| 3698 | Py_DECREF(callargs); |
| 3699 | return v; |
| 3700 | } |
| 3701 | PyTuple_SET_ITEM(tup, index, v); |
| 3702 | index++; |
| 3703 | } else if (bit & outmask) { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3704 | _Py_IDENTIFIER(__ctypes_from_outparam__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3705 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3706 | v = PyTuple_GET_ITEM(callargs, i); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3707 | v = _PyObject_CallMethodId(v, &PyId___ctypes_from_outparam__, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3708 | if (v == NULL || numretvals == 1) { |
| 3709 | Py_DECREF(callargs); |
| 3710 | return v; |
| 3711 | } |
| 3712 | PyTuple_SET_ITEM(tup, index, v); |
| 3713 | index++; |
| 3714 | } |
| 3715 | if (index == numretvals) |
| 3716 | break; |
| 3717 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3718 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3719 | Py_DECREF(callargs); |
| 3720 | return tup; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3724 | PyCFuncPtr_call(PyCFuncPtrObject *self, PyObject *inargs, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3725 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3726 | PyObject *restype; |
| 3727 | PyObject *converters; |
| 3728 | PyObject *checker; |
| 3729 | PyObject *argtypes; |
| 3730 | StgDictObject *dict = PyObject_stgdict((PyObject *)self); |
| 3731 | PyObject *result; |
| 3732 | PyObject *callargs; |
| 3733 | PyObject *errcheck; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3734 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3735 | IUnknown *piunk = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3736 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3737 | void *pProc = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3739 | int inoutmask; |
| 3740 | int outmask; |
| 3741 | unsigned int numretvals; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3742 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3743 | assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */ |
| 3744 | restype = self->restype ? self->restype : dict->restype; |
| 3745 | converters = self->converters ? self->converters : dict->converters; |
| 3746 | checker = self->checker ? self->checker : dict->checker; |
| 3747 | argtypes = self->argtypes ? self->argtypes : dict->argtypes; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3748 | /* later, we probably want to have an errcheck field in stgdict */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3749 | errcheck = self->errcheck /* ? self->errcheck : dict->errcheck */; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3750 | |
| 3751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3752 | pProc = *(void **)self->b_ptr; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3753 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3754 | if (self->index) { |
| 3755 | /* It's a COM method */ |
| 3756 | CDataObject *this; |
| 3757 | this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */ |
| 3758 | if (!this) { |
| 3759 | PyErr_SetString(PyExc_ValueError, |
| 3760 | "native com method call without 'this' parameter"); |
| 3761 | return NULL; |
| 3762 | } |
| 3763 | if (!CDataObject_Check(this)) { |
| 3764 | PyErr_SetString(PyExc_TypeError, |
| 3765 | "Expected a COM this pointer as first argument"); |
| 3766 | return NULL; |
| 3767 | } |
| 3768 | /* there should be more checks? No, in Python */ |
| 3769 | /* First arg is an pointer to an interface instance */ |
| 3770 | if (!this->b_ptr || *(void **)this->b_ptr == NULL) { |
| 3771 | PyErr_SetString(PyExc_ValueError, |
| 3772 | "NULL COM pointer access"); |
| 3773 | return NULL; |
| 3774 | } |
| 3775 | piunk = *(IUnknown **)this->b_ptr; |
| 3776 | if (NULL == piunk->lpVtbl) { |
| 3777 | PyErr_SetString(PyExc_ValueError, |
| 3778 | "COM method call without VTable"); |
| 3779 | return NULL; |
| 3780 | } |
| 3781 | pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000]; |
| 3782 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3783 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3784 | callargs = _build_callargs(self, argtypes, |
| 3785 | inargs, kwds, |
| 3786 | &outmask, &inoutmask, &numretvals); |
| 3787 | if (callargs == NULL) |
| 3788 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3789 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | if (converters) { |
| 3791 | int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters), |
| 3792 | Py_ssize_t, int); |
| 3793 | int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs), |
| 3794 | Py_ssize_t, int); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3795 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3796 | if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) { |
| 3797 | /* For cdecl functions, we allow more actual arguments |
| 3798 | than the length of the argtypes tuple. |
| 3799 | */ |
| 3800 | if (required > actual) { |
| 3801 | Py_DECREF(callargs); |
| 3802 | PyErr_Format(PyExc_TypeError, |
| 3803 | "this function takes at least %d argument%s (%d given)", |
| 3804 | required, |
| 3805 | required == 1 ? "" : "s", |
| 3806 | actual); |
| 3807 | return NULL; |
| 3808 | } |
| 3809 | } else if (required != actual) { |
| 3810 | Py_DECREF(callargs); |
| 3811 | PyErr_Format(PyExc_TypeError, |
| 3812 | "this function takes %d argument%s (%d given)", |
| 3813 | required, |
| 3814 | required == 1 ? "" : "s", |
| 3815 | actual); |
| 3816 | return NULL; |
| 3817 | } |
| 3818 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3820 | result = _ctypes_callproc(pProc, |
| 3821 | callargs, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3822 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | piunk, |
| 3824 | self->iid, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3825 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3826 | dict->flags, |
| 3827 | converters, |
| 3828 | restype, |
| 3829 | checker); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3830 | /* The 'errcheck' protocol */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3831 | if (result != NULL && errcheck) { |
| 3832 | PyObject *v = PyObject_CallFunctionObjArgs(errcheck, |
| 3833 | result, |
| 3834 | self, |
| 3835 | callargs, |
| 3836 | NULL); |
| 3837 | /* If the errcheck funtion failed, return NULL. |
| 3838 | If the errcheck function returned callargs unchanged, |
| 3839 | continue normal processing. |
| 3840 | If the errcheck function returned something else, |
| 3841 | use that as result. |
| 3842 | */ |
| 3843 | if (v == NULL || v != callargs) { |
| 3844 | Py_DECREF(result); |
| 3845 | Py_DECREF(callargs); |
| 3846 | return v; |
| 3847 | } |
| 3848 | Py_DECREF(v); |
| 3849 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3851 | return _build_result(result, callargs, |
| 3852 | outmask, inoutmask, numretvals); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3853 | } |
| 3854 | |
| 3855 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3856 | PyCFuncPtr_traverse(PyCFuncPtrObject *self, visitproc visit, void *arg) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3858 | Py_VISIT(self->callable); |
| 3859 | Py_VISIT(self->restype); |
| 3860 | Py_VISIT(self->checker); |
| 3861 | Py_VISIT(self->errcheck); |
| 3862 | Py_VISIT(self->argtypes); |
| 3863 | Py_VISIT(self->converters); |
| 3864 | Py_VISIT(self->paramflags); |
| 3865 | Py_VISIT(self->thunk); |
| 3866 | return PyCData_traverse((CDataObject *)self, visit, arg); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3867 | } |
| 3868 | |
| 3869 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3870 | PyCFuncPtr_clear(PyCFuncPtrObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3871 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | Py_CLEAR(self->callable); |
| 3873 | Py_CLEAR(self->restype); |
| 3874 | Py_CLEAR(self->checker); |
| 3875 | Py_CLEAR(self->errcheck); |
| 3876 | Py_CLEAR(self->argtypes); |
| 3877 | Py_CLEAR(self->converters); |
| 3878 | Py_CLEAR(self->paramflags); |
| 3879 | Py_CLEAR(self->thunk); |
| 3880 | return PyCData_clear((CDataObject *)self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | static void |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3884 | PyCFuncPtr_dealloc(PyCFuncPtrObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3885 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3886 | PyCFuncPtr_clear(self); |
| 3887 | Py_TYPE(self)->tp_free((PyObject *)self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3888 | } |
| 3889 | |
| 3890 | static PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3891 | PyCFuncPtr_repr(PyCFuncPtrObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3892 | { |
| 3893 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3894 | if (self->index) |
| 3895 | return PyUnicode_FromFormat("<COM method offset %d: %s at %p>", |
| 3896 | self->index - 0x1000, |
| 3897 | Py_TYPE(self)->tp_name, |
| 3898 | self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3899 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | return PyUnicode_FromFormat("<%s object at %p>", |
| 3901 | Py_TYPE(self)->tp_name, |
| 3902 | self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3903 | } |
| 3904 | |
Thomas Heller | 7dca3eb | 2008-05-29 19:54:39 +0000 | [diff] [blame] | 3905 | static int |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3906 | PyCFuncPtr_bool(PyCFuncPtrObject *self) |
Thomas Heller | 7dca3eb | 2008-05-29 19:54:39 +0000 | [diff] [blame] | 3907 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3908 | return ((*(void **)self->b_ptr != NULL) |
Thomas Heller | f5049fc | 2008-08-19 19:49:49 +0000 | [diff] [blame] | 3909 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3910 | || (self->index != 0) |
Thomas Heller | f5049fc | 2008-08-19 19:49:49 +0000 | [diff] [blame] | 3911 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3912 | ); |
Thomas Heller | 7dca3eb | 2008-05-29 19:54:39 +0000 | [diff] [blame] | 3913 | } |
| 3914 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3915 | static PyNumberMethods PyCFuncPtr_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3916 | 0, /* nb_add */ |
| 3917 | 0, /* nb_subtract */ |
| 3918 | 0, /* nb_multiply */ |
| 3919 | 0, /* nb_remainder */ |
| 3920 | 0, /* nb_divmod */ |
| 3921 | 0, /* nb_power */ |
| 3922 | 0, /* nb_negative */ |
| 3923 | 0, /* nb_positive */ |
| 3924 | 0, /* nb_absolute */ |
| 3925 | (inquiry)PyCFuncPtr_bool, /* nb_bool */ |
Thomas Heller | 7dca3eb | 2008-05-29 19:54:39 +0000 | [diff] [blame] | 3926 | }; |
| 3927 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 3928 | PyTypeObject PyCFuncPtr_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3929 | PyVarObject_HEAD_INIT(NULL, 0) |
| 3930 | "_ctypes.PyCFuncPtr", |
| 3931 | sizeof(PyCFuncPtrObject), /* tp_basicsize */ |
| 3932 | 0, /* tp_itemsize */ |
| 3933 | (destructor)PyCFuncPtr_dealloc, /* tp_dealloc */ |
| 3934 | 0, /* tp_print */ |
| 3935 | 0, /* tp_getattr */ |
| 3936 | 0, /* tp_setattr */ |
| 3937 | 0, /* tp_reserved */ |
| 3938 | (reprfunc)PyCFuncPtr_repr, /* tp_repr */ |
| 3939 | &PyCFuncPtr_as_number, /* tp_as_number */ |
| 3940 | 0, /* tp_as_sequence */ |
| 3941 | 0, /* tp_as_mapping */ |
| 3942 | 0, /* tp_hash */ |
| 3943 | (ternaryfunc)PyCFuncPtr_call, /* tp_call */ |
| 3944 | 0, /* tp_str */ |
| 3945 | 0, /* tp_getattro */ |
| 3946 | 0, /* tp_setattro */ |
| 3947 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 3948 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 3949 | "Function Pointer", /* tp_doc */ |
| 3950 | (traverseproc)PyCFuncPtr_traverse, /* tp_traverse */ |
| 3951 | (inquiry)PyCFuncPtr_clear, /* tp_clear */ |
| 3952 | 0, /* tp_richcompare */ |
| 3953 | 0, /* tp_weaklistoffset */ |
| 3954 | 0, /* tp_iter */ |
| 3955 | 0, /* tp_iternext */ |
| 3956 | 0, /* tp_methods */ |
| 3957 | 0, /* tp_members */ |
| 3958 | PyCFuncPtr_getsets, /* tp_getset */ |
| 3959 | 0, /* tp_base */ |
| 3960 | 0, /* tp_dict */ |
| 3961 | 0, /* tp_descr_get */ |
| 3962 | 0, /* tp_descr_set */ |
| 3963 | 0, /* tp_dictoffset */ |
| 3964 | 0, /* tp_init */ |
| 3965 | 0, /* tp_alloc */ |
| 3966 | PyCFuncPtr_new, /* tp_new */ |
| 3967 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3968 | }; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3969 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 3970 | /*****************************************************************/ |
| 3971 | /* |
| 3972 | Struct_Type |
| 3973 | */ |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 3974 | /* |
| 3975 | This function is called to initialize a Structure or Union with positional |
| 3976 | arguments. It calls itself recursively for all Structure or Union base |
| 3977 | classes, then retrieves the _fields_ member to associate the argument |
| 3978 | position with the correct field name. |
| 3979 | |
| 3980 | Returns -1 on error, or the index of next argument on success. |
| 3981 | */ |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3982 | static Py_ssize_t |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 3983 | _init_pos_args(PyObject *self, PyTypeObject *type, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3984 | PyObject *args, PyObject *kwds, |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3985 | Py_ssize_t index) |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 3986 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3987 | StgDictObject *dict; |
| 3988 | PyObject *fields; |
Victor Stinner | 0fcab4a | 2011-01-04 12:59:15 +0000 | [diff] [blame] | 3989 | Py_ssize_t i; |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 3990 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3991 | if (PyType_stgdict((PyObject *)type->tp_base)) { |
| 3992 | index = _init_pos_args(self, type->tp_base, |
| 3993 | args, kwds, |
| 3994 | index); |
| 3995 | if (index == -1) |
| 3996 | return -1; |
| 3997 | } |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 3998 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3999 | dict = PyType_stgdict((PyObject *)type); |
| 4000 | fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); |
| 4001 | if (fields == NULL) |
| 4002 | return index; |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 4003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4004 | for (i = 0; |
| 4005 | i < dict->length && (i+index) < PyTuple_GET_SIZE(args); |
| 4006 | ++i) { |
| 4007 | PyObject *pair = PySequence_GetItem(fields, i); |
| 4008 | PyObject *name, *val; |
| 4009 | int res; |
| 4010 | if (!pair) |
| 4011 | return -1; |
| 4012 | name = PySequence_GetItem(pair, 0); |
| 4013 | if (!name) { |
| 4014 | Py_DECREF(pair); |
| 4015 | return -1; |
| 4016 | } |
| 4017 | val = PyTuple_GET_ITEM(args, i + index); |
| 4018 | if (kwds && PyDict_GetItem(kwds, name)) { |
| 4019 | char *field = PyBytes_AsString(name); |
| 4020 | if (field == NULL) { |
| 4021 | PyErr_Clear(); |
| 4022 | field = "???"; |
| 4023 | } |
| 4024 | PyErr_Format(PyExc_TypeError, |
| 4025 | "duplicate values for field '%s'", |
| 4026 | field); |
| 4027 | Py_DECREF(pair); |
| 4028 | Py_DECREF(name); |
| 4029 | return -1; |
| 4030 | } |
| 4031 | |
| 4032 | res = PyObject_SetAttr(self, name, val); |
| 4033 | Py_DECREF(pair); |
| 4034 | Py_DECREF(name); |
| 4035 | if (res == -1) |
| 4036 | return -1; |
| 4037 | } |
| 4038 | return index + dict->length; |
Thomas Heller | d7cb1b9 | 2009-09-18 19:05:13 +0000 | [diff] [blame] | 4039 | } |
| 4040 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4041 | static int |
| 4042 | Struct_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 4043 | { |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4044 | /* Optimization possible: Store the attribute names _fields_[x][0] |
| 4045 | * in C accessible fields somewhere ? |
| 4046 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4047 | if (!PyTuple_Check(args)) { |
| 4048 | PyErr_SetString(PyExc_TypeError, |
| 4049 | "args not a tuple?"); |
| 4050 | return -1; |
| 4051 | } |
| 4052 | if (PyTuple_GET_SIZE(args)) { |
| 4053 | int res = _init_pos_args(self, Py_TYPE(self), |
| 4054 | args, kwds, 0); |
| 4055 | if (res == -1) |
| 4056 | return -1; |
| 4057 | if (res < PyTuple_GET_SIZE(args)) { |
| 4058 | PyErr_SetString(PyExc_TypeError, |
| 4059 | "too many initializers"); |
| 4060 | return -1; |
| 4061 | } |
| 4062 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4063 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4064 | if (kwds) { |
| 4065 | PyObject *key, *value; |
| 4066 | Py_ssize_t pos = 0; |
| 4067 | while(PyDict_Next(kwds, &pos, &key, &value)) { |
| 4068 | if (-1 == PyObject_SetAttr(self, key, value)) |
| 4069 | return -1; |
| 4070 | } |
| 4071 | } |
| 4072 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4073 | } |
| 4074 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4075 | static PyTypeObject Struct_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4076 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4077 | "_ctypes.Structure", |
| 4078 | sizeof(CDataObject), /* tp_basicsize */ |
| 4079 | 0, /* tp_itemsize */ |
| 4080 | 0, /* tp_dealloc */ |
| 4081 | 0, /* tp_print */ |
| 4082 | 0, /* tp_getattr */ |
| 4083 | 0, /* tp_setattr */ |
| 4084 | 0, /* tp_reserved */ |
| 4085 | 0, /* tp_repr */ |
| 4086 | 0, /* tp_as_number */ |
| 4087 | 0, /* tp_as_sequence */ |
| 4088 | 0, /* tp_as_mapping */ |
| 4089 | 0, /* tp_hash */ |
| 4090 | 0, /* tp_call */ |
| 4091 | 0, /* tp_str */ |
| 4092 | 0, /* tp_getattro */ |
| 4093 | 0, /* tp_setattro */ |
| 4094 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 4095 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4096 | "Structure base class", /* tp_doc */ |
| 4097 | (traverseproc)PyCData_traverse, /* tp_traverse */ |
| 4098 | (inquiry)PyCData_clear, /* tp_clear */ |
| 4099 | 0, /* tp_richcompare */ |
| 4100 | 0, /* tp_weaklistoffset */ |
| 4101 | 0, /* tp_iter */ |
| 4102 | 0, /* tp_iternext */ |
| 4103 | 0, /* tp_methods */ |
| 4104 | 0, /* tp_members */ |
| 4105 | 0, /* tp_getset */ |
| 4106 | 0, /* tp_base */ |
| 4107 | 0, /* tp_dict */ |
| 4108 | 0, /* tp_descr_get */ |
| 4109 | 0, /* tp_descr_set */ |
| 4110 | 0, /* tp_dictoffset */ |
| 4111 | Struct_init, /* tp_init */ |
| 4112 | 0, /* tp_alloc */ |
| 4113 | GenericPyCData_new, /* tp_new */ |
| 4114 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4115 | }; |
| 4116 | |
| 4117 | static PyTypeObject Union_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4118 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4119 | "_ctypes.Union", |
| 4120 | sizeof(CDataObject), /* tp_basicsize */ |
| 4121 | 0, /* tp_itemsize */ |
| 4122 | 0, /* tp_dealloc */ |
| 4123 | 0, /* tp_print */ |
| 4124 | 0, /* tp_getattr */ |
| 4125 | 0, /* tp_setattr */ |
| 4126 | 0, /* tp_reserved */ |
| 4127 | 0, /* tp_repr */ |
| 4128 | 0, /* tp_as_number */ |
| 4129 | 0, /* tp_as_sequence */ |
| 4130 | 0, /* tp_as_mapping */ |
| 4131 | 0, /* tp_hash */ |
| 4132 | 0, /* tp_call */ |
| 4133 | 0, /* tp_str */ |
| 4134 | 0, /* tp_getattro */ |
| 4135 | 0, /* tp_setattro */ |
| 4136 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 4137 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4138 | "Union base class", /* tp_doc */ |
| 4139 | (traverseproc)PyCData_traverse, /* tp_traverse */ |
| 4140 | (inquiry)PyCData_clear, /* tp_clear */ |
| 4141 | 0, /* tp_richcompare */ |
| 4142 | 0, /* tp_weaklistoffset */ |
| 4143 | 0, /* tp_iter */ |
| 4144 | 0, /* tp_iternext */ |
| 4145 | 0, /* tp_methods */ |
| 4146 | 0, /* tp_members */ |
| 4147 | 0, /* tp_getset */ |
| 4148 | 0, /* tp_base */ |
| 4149 | 0, /* tp_dict */ |
| 4150 | 0, /* tp_descr_get */ |
| 4151 | 0, /* tp_descr_set */ |
| 4152 | 0, /* tp_dictoffset */ |
| 4153 | Struct_init, /* tp_init */ |
| 4154 | 0, /* tp_alloc */ |
| 4155 | GenericPyCData_new, /* tp_new */ |
| 4156 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4157 | }; |
| 4158 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4159 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4160 | /******************************************************************/ |
| 4161 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 4162 | PyCArray_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4163 | */ |
| 4164 | static int |
| 4165 | Array_init(CDataObject *self, PyObject *args, PyObject *kw) |
| 4166 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4167 | Py_ssize_t i; |
| 4168 | Py_ssize_t n; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4169 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4170 | if (!PyTuple_Check(args)) { |
| 4171 | PyErr_SetString(PyExc_TypeError, |
| 4172 | "args not a tuple?"); |
| 4173 | return -1; |
| 4174 | } |
| 4175 | n = PyTuple_GET_SIZE(args); |
| 4176 | for (i = 0; i < n; ++i) { |
| 4177 | PyObject *v; |
| 4178 | v = PyTuple_GET_ITEM(args, i); |
| 4179 | if (-1 == PySequence_SetItem((PyObject *)self, i, v)) |
| 4180 | return -1; |
| 4181 | } |
| 4182 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4183 | } |
| 4184 | |
| 4185 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4186 | Array_item(PyObject *_self, Py_ssize_t index) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4187 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4188 | CDataObject *self = (CDataObject *)_self; |
| 4189 | Py_ssize_t offset, size; |
| 4190 | StgDictObject *stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4191 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4192 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4193 | if (index < 0 || index >= self->b_length) { |
| 4194 | PyErr_SetString(PyExc_IndexError, |
| 4195 | "invalid index"); |
| 4196 | return NULL; |
| 4197 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4198 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4199 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4200 | assert(stgdict); /* Cannot be NULL for array instances */ |
| 4201 | /* Would it be clearer if we got the item size from |
| 4202 | stgdict->proto's stgdict? |
| 4203 | */ |
| 4204 | size = stgdict->size / stgdict->length; |
| 4205 | offset = index * size; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4206 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4207 | return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self, |
| 4208 | index, size, self->b_ptr + offset); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4209 | } |
| 4210 | |
| 4211 | static PyObject * |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4212 | Array_subscript(PyObject *_self, PyObject *item) |
| 4213 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4214 | CDataObject *self = (CDataObject *)_self; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4216 | if (PyIndex_Check(item)) { |
| 4217 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4218 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | if (i == -1 && PyErr_Occurred()) |
| 4220 | return NULL; |
| 4221 | if (i < 0) |
| 4222 | i += self->b_length; |
| 4223 | return Array_item(_self, i); |
| 4224 | } |
| 4225 | else if PySlice_Check(item) { |
| 4226 | StgDictObject *stgdict, *itemdict; |
| 4227 | PyObject *proto; |
| 4228 | PyObject *np; |
| 4229 | Py_ssize_t start, stop, step, slicelen, cur, i; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4230 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4231 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4232 | self->b_length, &start, &stop, |
| 4233 | &step, &slicelen) < 0) { |
| 4234 | return NULL; |
| 4235 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4237 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4238 | assert(stgdict); /* Cannot be NULL for array object instances */ |
| 4239 | proto = stgdict->proto; |
| 4240 | itemdict = PyType_stgdict(proto); |
| 4241 | assert(itemdict); /* proto is the item type of the array, a |
| 4242 | ctypes type, so this cannot be NULL */ |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4244 | if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
| 4245 | char *ptr = (char *)self->b_ptr; |
| 4246 | char *dest; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4248 | if (slicelen <= 0) |
| 4249 | return PyBytes_FromStringAndSize("", 0); |
| 4250 | if (step == 1) { |
| 4251 | return PyBytes_FromStringAndSize(ptr + start, |
| 4252 | slicelen); |
| 4253 | } |
| 4254 | dest = (char *)PyMem_Malloc(slicelen); |
| 4255 | |
| 4256 | if (dest == NULL) |
| 4257 | return PyErr_NoMemory(); |
| 4258 | |
| 4259 | for (cur = start, i = 0; i < slicelen; |
| 4260 | cur += step, i++) { |
| 4261 | dest[i] = ptr[cur]; |
| 4262 | } |
| 4263 | |
| 4264 | np = PyBytes_FromStringAndSize(dest, slicelen); |
| 4265 | PyMem_Free(dest); |
| 4266 | return np; |
| 4267 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4268 | #ifdef CTYPES_UNICODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4269 | if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
| 4270 | wchar_t *ptr = (wchar_t *)self->b_ptr; |
| 4271 | wchar_t *dest; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4272 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4273 | if (slicelen <= 0) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4274 | return PyUnicode_New(0, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4275 | if (step == 1) { |
| 4276 | return PyUnicode_FromWideChar(ptr + start, |
| 4277 | slicelen); |
| 4278 | } |
| 4279 | |
| 4280 | dest = (wchar_t *)PyMem_Malloc( |
| 4281 | slicelen * sizeof(wchar_t)); |
| 4282 | |
| 4283 | for (cur = start, i = 0; i < slicelen; |
| 4284 | cur += step, i++) { |
| 4285 | dest[i] = ptr[cur]; |
| 4286 | } |
| 4287 | |
| 4288 | np = PyUnicode_FromWideChar(dest, slicelen); |
| 4289 | PyMem_Free(dest); |
| 4290 | return np; |
| 4291 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4292 | #endif |
| 4293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4294 | np = PyList_New(slicelen); |
| 4295 | if (np == NULL) |
| 4296 | return NULL; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | for (cur = start, i = 0; i < slicelen; |
| 4299 | cur += step, i++) { |
| 4300 | PyObject *v = Array_item(_self, cur); |
| 4301 | PyList_SET_ITEM(np, i, v); |
| 4302 | } |
| 4303 | return np; |
| 4304 | } |
| 4305 | else { |
| 4306 | PyErr_SetString(PyExc_TypeError, |
| 4307 | "indices must be integers"); |
| 4308 | return NULL; |
| 4309 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4310 | |
| 4311 | } |
| 4312 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4313 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4314 | Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4315 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4316 | CDataObject *self = (CDataObject *)_self; |
| 4317 | Py_ssize_t size, offset; |
| 4318 | StgDictObject *stgdict; |
| 4319 | char *ptr; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4321 | if (value == NULL) { |
| 4322 | PyErr_SetString(PyExc_TypeError, |
| 4323 | "Array does not support item deletion"); |
| 4324 | return -1; |
| 4325 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4327 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4328 | assert(stgdict); /* Cannot be NULL for array object instances */ |
| 4329 | if (index < 0 || index >= stgdict->length) { |
| 4330 | PyErr_SetString(PyExc_IndexError, |
| 4331 | "invalid index"); |
| 4332 | return -1; |
| 4333 | } |
| 4334 | size = stgdict->size / stgdict->length; |
| 4335 | offset = index * size; |
| 4336 | ptr = self->b_ptr + offset; |
| 4337 | |
| 4338 | return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value, |
| 4339 | index, size, ptr); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | static int |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4343 | Array_ass_subscript(PyObject *_self, PyObject *item, PyObject *value) |
| 4344 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | CDataObject *self = (CDataObject *)_self; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4346 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4347 | if (value == NULL) { |
| 4348 | PyErr_SetString(PyExc_TypeError, |
| 4349 | "Array does not support item deletion"); |
| 4350 | return -1; |
| 4351 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4352 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | if (PyIndex_Check(item)) { |
| 4354 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 4355 | |
| 4356 | if (i == -1 && PyErr_Occurred()) |
| 4357 | return -1; |
| 4358 | if (i < 0) |
| 4359 | i += self->b_length; |
| 4360 | return Array_ass_item(_self, i, value); |
| 4361 | } |
| 4362 | else if (PySlice_Check(item)) { |
| 4363 | Py_ssize_t start, stop, step, slicelen, otherlen, i, cur; |
| 4364 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 4365 | if (PySlice_GetIndicesEx(item, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | self->b_length, &start, &stop, |
| 4367 | &step, &slicelen) < 0) { |
| 4368 | return -1; |
| 4369 | } |
| 4370 | if ((step < 0 && start < stop) || |
| 4371 | (step > 0 && start > stop)) |
| 4372 | stop = start; |
| 4373 | |
| 4374 | otherlen = PySequence_Length(value); |
| 4375 | if (otherlen != slicelen) { |
| 4376 | PyErr_SetString(PyExc_ValueError, |
| 4377 | "Can only assign sequence of same size"); |
| 4378 | return -1; |
| 4379 | } |
| 4380 | for (cur = start, i = 0; i < otherlen; cur += step, i++) { |
| 4381 | PyObject *item = PySequence_GetItem(value, i); |
| 4382 | int result; |
| 4383 | if (item == NULL) |
| 4384 | return -1; |
| 4385 | result = Array_ass_item(_self, cur, item); |
| 4386 | Py_DECREF(item); |
| 4387 | if (result == -1) |
| 4388 | return -1; |
| 4389 | } |
| 4390 | return 0; |
| 4391 | } |
| 4392 | else { |
| 4393 | PyErr_SetString(PyExc_TypeError, |
| 4394 | "indices must be integer"); |
| 4395 | return -1; |
| 4396 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4399 | static Py_ssize_t |
| 4400 | Array_length(PyObject *_self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | CDataObject *self = (CDataObject *)_self; |
| 4403 | return self->b_length; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4404 | } |
| 4405 | |
| 4406 | static PySequenceMethods Array_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | Array_length, /* sq_length; */ |
| 4408 | 0, /* sq_concat; */ |
| 4409 | 0, /* sq_repeat; */ |
| 4410 | Array_item, /* sq_item; */ |
| 4411 | 0, /* sq_slice; */ |
| 4412 | Array_ass_item, /* sq_ass_item; */ |
| 4413 | 0, /* sq_ass_slice; */ |
| 4414 | 0, /* sq_contains; */ |
| 4415 | |
| 4416 | 0, /* sq_inplace_concat; */ |
| 4417 | 0, /* sq_inplace_repeat; */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4418 | }; |
| 4419 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4420 | static PyMappingMethods Array_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4421 | Array_length, |
| 4422 | Array_subscript, |
| 4423 | Array_ass_subscript, |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4424 | }; |
| 4425 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 4426 | PyTypeObject PyCArray_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4427 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4428 | "_ctypes.Array", |
| 4429 | sizeof(CDataObject), /* tp_basicsize */ |
| 4430 | 0, /* tp_itemsize */ |
| 4431 | 0, /* tp_dealloc */ |
| 4432 | 0, /* tp_print */ |
| 4433 | 0, /* tp_getattr */ |
| 4434 | 0, /* tp_setattr */ |
| 4435 | 0, /* tp_reserved */ |
| 4436 | 0, /* tp_repr */ |
| 4437 | 0, /* tp_as_number */ |
| 4438 | &Array_as_sequence, /* tp_as_sequence */ |
| 4439 | &Array_as_mapping, /* tp_as_mapping */ |
| 4440 | 0, /* tp_hash */ |
| 4441 | 0, /* tp_call */ |
| 4442 | 0, /* tp_str */ |
| 4443 | 0, /* tp_getattro */ |
| 4444 | 0, /* tp_setattro */ |
| 4445 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 4446 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4447 | "XXX to be provided", /* tp_doc */ |
| 4448 | (traverseproc)PyCData_traverse, /* tp_traverse */ |
| 4449 | (inquiry)PyCData_clear, /* tp_clear */ |
| 4450 | 0, /* tp_richcompare */ |
| 4451 | 0, /* tp_weaklistoffset */ |
| 4452 | 0, /* tp_iter */ |
| 4453 | 0, /* tp_iternext */ |
| 4454 | 0, /* tp_methods */ |
| 4455 | 0, /* tp_members */ |
| 4456 | 0, /* tp_getset */ |
| 4457 | 0, /* tp_base */ |
| 4458 | 0, /* tp_dict */ |
| 4459 | 0, /* tp_descr_get */ |
| 4460 | 0, /* tp_descr_set */ |
| 4461 | 0, /* tp_dictoffset */ |
| 4462 | (initproc)Array_init, /* tp_init */ |
| 4463 | 0, /* tp_alloc */ |
| 4464 | GenericPyCData_new, /* tp_new */ |
| 4465 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4466 | }; |
| 4467 | |
| 4468 | PyObject * |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 4469 | PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4470 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4471 | static PyObject *cache; |
| 4472 | PyObject *key; |
| 4473 | PyObject *result; |
| 4474 | char name[256]; |
| 4475 | PyObject *len; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4476 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4477 | if (cache == NULL) { |
| 4478 | cache = PyDict_New(); |
| 4479 | if (cache == NULL) |
| 4480 | return NULL; |
| 4481 | } |
| 4482 | len = PyLong_FromSsize_t(length); |
| 4483 | if (len == NULL) |
| 4484 | return NULL; |
| 4485 | key = PyTuple_Pack(2, itemtype, len); |
| 4486 | Py_DECREF(len); |
| 4487 | if (!key) |
| 4488 | return NULL; |
| 4489 | result = PyDict_GetItemProxy(cache, key); |
| 4490 | if (result) { |
| 4491 | Py_INCREF(result); |
| 4492 | Py_DECREF(key); |
| 4493 | return result; |
| 4494 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4495 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4496 | if (!PyType_Check(itemtype)) { |
| 4497 | PyErr_SetString(PyExc_TypeError, |
| 4498 | "Expected a type object"); |
Meador Inge | b86ecf4 | 2011-09-27 20:45:30 -0500 | [diff] [blame] | 4499 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | return NULL; |
| 4501 | } |
Thomas Heller | fe8f862 | 2006-03-14 19:53:09 +0000 | [diff] [blame] | 4502 | #ifdef MS_WIN64 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4503 | sprintf(name, "%.200s_Array_%Id", |
| 4504 | ((PyTypeObject *)itemtype)->tp_name, length); |
Thomas Heller | fe8f862 | 2006-03-14 19:53:09 +0000 | [diff] [blame] | 4505 | #else |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4506 | sprintf(name, "%.200s_Array_%ld", |
| 4507 | ((PyTypeObject *)itemtype)->tp_name, (long)length); |
Thomas Heller | fe8f862 | 2006-03-14 19:53:09 +0000 | [diff] [blame] | 4508 | #endif |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4509 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4510 | result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type, |
Victor Stinner | 7eeb5b5 | 2010-06-07 19:57:46 +0000 | [diff] [blame] | 4511 | "s(O){s:n,s:O}", |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | name, |
| 4513 | &PyCArray_Type, |
| 4514 | "_length_", |
| 4515 | length, |
| 4516 | "_type_", |
| 4517 | itemtype |
| 4518 | ); |
| 4519 | if (result == NULL) { |
| 4520 | Py_DECREF(key); |
| 4521 | return NULL; |
| 4522 | } |
| 4523 | if (-1 == PyDict_SetItemProxy(cache, key, result)) { |
| 4524 | Py_DECREF(key); |
| 4525 | Py_DECREF(result); |
| 4526 | return NULL; |
| 4527 | } |
| 4528 | Py_DECREF(key); |
| 4529 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4532 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4533 | /******************************************************************/ |
| 4534 | /* |
| 4535 | Simple_Type |
| 4536 | */ |
| 4537 | |
| 4538 | static int |
| 4539 | Simple_set_value(CDataObject *self, PyObject *value) |
| 4540 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | PyObject *result; |
| 4542 | StgDictObject *dict = PyObject_stgdict((PyObject *)self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4544 | if (value == NULL) { |
| 4545 | PyErr_SetString(PyExc_TypeError, |
| 4546 | "can't delete attribute"); |
| 4547 | return -1; |
| 4548 | } |
| 4549 | assert(dict); /* Cannot be NULL for CDataObject instances */ |
| 4550 | assert(dict->setfunc); |
| 4551 | result = dict->setfunc(self->b_ptr, value, dict->size); |
| 4552 | if (!result) |
| 4553 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4554 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4555 | /* consumes the refcount the setfunc returns */ |
| 4556 | return KeepRef(self, 0, result); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4557 | } |
| 4558 | |
| 4559 | static int |
| 4560 | Simple_init(CDataObject *self, PyObject *args, PyObject *kw) |
| 4561 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4562 | PyObject *value = NULL; |
| 4563 | if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value)) |
| 4564 | return -1; |
| 4565 | if (value) |
| 4566 | return Simple_set_value(self, value); |
| 4567 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | static PyObject * |
| 4571 | Simple_get_value(CDataObject *self) |
| 4572 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4573 | StgDictObject *dict; |
| 4574 | dict = PyObject_stgdict((PyObject *)self); |
| 4575 | assert(dict); /* Cannot be NULL for CDataObject instances */ |
| 4576 | assert(dict->getfunc); |
| 4577 | return dict->getfunc(self->b_ptr, self->b_size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4578 | } |
| 4579 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4580 | static PyGetSetDef Simple_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | { "value", (getter)Simple_get_value, (setter)Simple_set_value, |
| 4582 | "current value", NULL }, |
| 4583 | { NULL, NULL } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4584 | }; |
| 4585 | |
| 4586 | static PyObject * |
| 4587 | Simple_from_outparm(PyObject *self, PyObject *args) |
| 4588 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) { |
| 4590 | Py_INCREF(self); |
| 4591 | return self; |
| 4592 | } |
| 4593 | /* call stgdict->getfunc */ |
| 4594 | return Simple_get_value((CDataObject *)self); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | static PyMethodDef Simple_methods[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | { "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, }, |
| 4599 | { NULL, NULL }, |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4600 | }; |
| 4601 | |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 4602 | static int Simple_bool(CDataObject *self) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | static PyNumberMethods Simple_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4608 | 0, /* nb_add */ |
| 4609 | 0, /* nb_subtract */ |
| 4610 | 0, /* nb_multiply */ |
| 4611 | 0, /* nb_remainder */ |
| 4612 | 0, /* nb_divmod */ |
| 4613 | 0, /* nb_power */ |
| 4614 | 0, /* nb_negative */ |
| 4615 | 0, /* nb_positive */ |
| 4616 | 0, /* nb_absolute */ |
| 4617 | (inquiry)Simple_bool, /* nb_bool */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4618 | }; |
| 4619 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4620 | /* "%s(%s)" % (self.__class__.__name__, self.value) */ |
| 4621 | static PyObject * |
| 4622 | Simple_repr(CDataObject *self) |
| 4623 | { |
Amaury Forgeot d'Arc | 864741b | 2011-11-06 15:10:48 +0100 | [diff] [blame] | 4624 | PyObject *val, *result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4626 | if (Py_TYPE(self)->tp_base != &Simple_Type) { |
| 4627 | return PyUnicode_FromFormat("<%s object at %p>", |
| 4628 | Py_TYPE(self)->tp_name, self); |
| 4629 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4631 | val = Simple_get_value(self); |
| 4632 | if (val == NULL) |
| 4633 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4634 | |
Amaury Forgeot d'Arc | 864741b | 2011-11-06 15:10:48 +0100 | [diff] [blame] | 4635 | result = PyUnicode_FromFormat("%s(%R)", |
| 4636 | Py_TYPE(self)->tp_name, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4637 | Py_DECREF(val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4638 | return result; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | static PyTypeObject Simple_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4642 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4643 | "_ctypes._SimpleCData", |
| 4644 | sizeof(CDataObject), /* tp_basicsize */ |
| 4645 | 0, /* tp_itemsize */ |
| 4646 | 0, /* tp_dealloc */ |
| 4647 | 0, /* tp_print */ |
| 4648 | 0, /* tp_getattr */ |
| 4649 | 0, /* tp_setattr */ |
| 4650 | 0, /* tp_reserved */ |
| 4651 | (reprfunc)&Simple_repr, /* tp_repr */ |
| 4652 | &Simple_as_number, /* tp_as_number */ |
| 4653 | 0, /* tp_as_sequence */ |
| 4654 | 0, /* tp_as_mapping */ |
| 4655 | 0, /* tp_hash */ |
| 4656 | 0, /* tp_call */ |
| 4657 | 0, /* tp_str */ |
| 4658 | 0, /* tp_getattro */ |
| 4659 | 0, /* tp_setattro */ |
| 4660 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 4661 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 4662 | "XXX to be provided", /* tp_doc */ |
| 4663 | (traverseproc)PyCData_traverse, /* tp_traverse */ |
| 4664 | (inquiry)PyCData_clear, /* tp_clear */ |
| 4665 | 0, /* tp_richcompare */ |
| 4666 | 0, /* tp_weaklistoffset */ |
| 4667 | 0, /* tp_iter */ |
| 4668 | 0, /* tp_iternext */ |
| 4669 | Simple_methods, /* tp_methods */ |
| 4670 | 0, /* tp_members */ |
| 4671 | Simple_getsets, /* tp_getset */ |
| 4672 | 0, /* tp_base */ |
| 4673 | 0, /* tp_dict */ |
| 4674 | 0, /* tp_descr_get */ |
| 4675 | 0, /* tp_descr_set */ |
| 4676 | 0, /* tp_dictoffset */ |
| 4677 | (initproc)Simple_init, /* tp_init */ |
| 4678 | 0, /* tp_alloc */ |
| 4679 | GenericPyCData_new, /* tp_new */ |
| 4680 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4681 | }; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4682 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4683 | /******************************************************************/ |
| 4684 | /* |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 4685 | PyCPointer_Type |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4686 | */ |
| 4687 | static PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4688 | Pointer_item(PyObject *_self, Py_ssize_t index) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4689 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | CDataObject *self = (CDataObject *)_self; |
| 4691 | Py_ssize_t size; |
| 4692 | Py_ssize_t offset; |
| 4693 | StgDictObject *stgdict, *itemdict; |
| 4694 | PyObject *proto; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4696 | if (*(void **)self->b_ptr == NULL) { |
| 4697 | PyErr_SetString(PyExc_ValueError, |
| 4698 | "NULL pointer access"); |
| 4699 | return NULL; |
| 4700 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4701 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4702 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4703 | assert(stgdict); /* Cannot be NULL for pointer object instances */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4705 | proto = stgdict->proto; |
| 4706 | assert(proto); |
| 4707 | itemdict = PyType_stgdict(proto); |
| 4708 | assert(itemdict); /* proto is the item type of the pointer, a ctypes |
| 4709 | type, so this cannot be NULL */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4710 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4711 | size = itemdict->size; |
| 4712 | offset = index * itemdict->size; |
| 4713 | |
| 4714 | return PyCData_get(proto, stgdict->getfunc, (PyObject *)self, |
| 4715 | index, size, (*(char **)self->b_ptr) + offset); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | static int |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 4719 | Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4720 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4721 | CDataObject *self = (CDataObject *)_self; |
| 4722 | Py_ssize_t size; |
| 4723 | Py_ssize_t offset; |
| 4724 | StgDictObject *stgdict, *itemdict; |
| 4725 | PyObject *proto; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4726 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4727 | if (value == NULL) { |
| 4728 | PyErr_SetString(PyExc_TypeError, |
| 4729 | "Pointer does not support item deletion"); |
| 4730 | return -1; |
| 4731 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | if (*(void **)self->b_ptr == NULL) { |
| 4734 | PyErr_SetString(PyExc_ValueError, |
| 4735 | "NULL pointer access"); |
| 4736 | return -1; |
| 4737 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4740 | assert(stgdict); /* Cannot be NULL fr pointer instances */ |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4742 | proto = stgdict->proto; |
| 4743 | assert(proto); |
Thomas Wouters | 00ee7ba | 2006-08-21 19:07:27 +0000 | [diff] [blame] | 4744 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | itemdict = PyType_stgdict(proto); |
| 4746 | assert(itemdict); /* Cannot be NULL because the itemtype of a pointer |
| 4747 | is always a ctypes type */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 4748 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4749 | size = itemdict->size; |
| 4750 | offset = index * itemdict->size; |
| 4751 | |
| 4752 | return PyCData_set((PyObject *)self, proto, stgdict->setfunc, value, |
| 4753 | index, size, (*(char **)self->b_ptr) + offset); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
| 4756 | static PyObject * |
| 4757 | Pointer_get_contents(CDataObject *self, void *closure) |
| 4758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4759 | StgDictObject *stgdict; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4761 | if (*(void **)self->b_ptr == NULL) { |
| 4762 | PyErr_SetString(PyExc_ValueError, |
| 4763 | "NULL pointer access"); |
| 4764 | return NULL; |
| 4765 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4766 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4767 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4768 | assert(stgdict); /* Cannot be NULL fr pointer instances */ |
| 4769 | return PyCData_FromBaseObj(stgdict->proto, |
| 4770 | (PyObject *)self, 0, |
| 4771 | *(void **)self->b_ptr); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | static int |
| 4775 | Pointer_set_contents(CDataObject *self, PyObject *value, void *closure) |
| 4776 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4777 | StgDictObject *stgdict; |
| 4778 | CDataObject *dst; |
| 4779 | PyObject *keep; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4781 | if (value == NULL) { |
| 4782 | PyErr_SetString(PyExc_TypeError, |
| 4783 | "Pointer does not support item deletion"); |
| 4784 | return -1; |
| 4785 | } |
| 4786 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4787 | assert(stgdict); /* Cannot be NULL fr pointer instances */ |
| 4788 | assert(stgdict->proto); |
Benjamin Peterson | 39530f8 | 2011-03-26 18:04:09 -0500 | [diff] [blame] | 4789 | if (!CDataObject_Check(value)) { |
| 4790 | int res = PyObject_IsInstance(value, stgdict->proto); |
| 4791 | if (res == -1) |
| 4792 | return -1; |
| 4793 | if (!res) { |
| 4794 | PyErr_Format(PyExc_TypeError, |
| 4795 | "expected %s instead of %s", |
| 4796 | ((PyTypeObject *)(stgdict->proto))->tp_name, |
| 4797 | Py_TYPE(value)->tp_name); |
| 4798 | return -1; |
| 4799 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4800 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4801 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4802 | dst = (CDataObject *)value; |
| 4803 | *(void **)self->b_ptr = dst->b_ptr; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4804 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4805 | /* |
| 4806 | A Pointer instance must keep a the value it points to alive. So, a |
| 4807 | pointer instance has b_length set to 2 instead of 1, and we set |
| 4808 | 'value' itself as the second item of the b_objects list, additionally. |
| 4809 | */ |
| 4810 | Py_INCREF(value); |
| 4811 | if (-1 == KeepRef(self, 1, value)) |
| 4812 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4813 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4814 | keep = GetKeepedObjects(dst); |
| 4815 | Py_INCREF(keep); |
| 4816 | return KeepRef(self, 0, keep); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4819 | static PyGetSetDef Pointer_getsets[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4820 | { "contents", (getter)Pointer_get_contents, |
| 4821 | (setter)Pointer_set_contents, |
| 4822 | "the object this pointer points to (read-write)", NULL }, |
| 4823 | { NULL, NULL } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4824 | }; |
| 4825 | |
| 4826 | static int |
| 4827 | Pointer_init(CDataObject *self, PyObject *args, PyObject *kw) |
| 4828 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4829 | PyObject *value = NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4831 | if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value)) |
| 4832 | return -1; |
| 4833 | if (value == NULL) |
| 4834 | return 0; |
| 4835 | return Pointer_set_contents(self, value, NULL); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | static PyObject * |
| 4839 | Pointer_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 4840 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4841 | StgDictObject *dict = PyType_stgdict((PyObject *)type); |
| 4842 | if (!dict || !dict->proto) { |
| 4843 | PyErr_SetString(PyExc_TypeError, |
| 4844 | "Cannot create instance: has no _type_"); |
| 4845 | return NULL; |
| 4846 | } |
| 4847 | return GenericPyCData_new(type, args, kw); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | static PyObject * |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4851 | Pointer_subscript(PyObject *_self, PyObject *item) |
| 4852 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4853 | CDataObject *self = (CDataObject *)_self; |
| 4854 | if (PyIndex_Check(item)) { |
| 4855 | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 4856 | if (i == -1 && PyErr_Occurred()) |
| 4857 | return NULL; |
| 4858 | return Pointer_item(_self, i); |
| 4859 | } |
| 4860 | else if (PySlice_Check(item)) { |
| 4861 | PySliceObject *slice = (PySliceObject *)item; |
| 4862 | Py_ssize_t start, stop, step; |
| 4863 | PyObject *np; |
| 4864 | StgDictObject *stgdict, *itemdict; |
| 4865 | PyObject *proto; |
| 4866 | Py_ssize_t i, len, cur; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4867 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4868 | /* Since pointers have no length, and we want to apply |
| 4869 | different semantics to negative indices than normal |
| 4870 | slicing, we have to dissect the slice object ourselves.*/ |
| 4871 | if (slice->step == Py_None) { |
| 4872 | step = 1; |
| 4873 | } |
| 4874 | else { |
| 4875 | step = PyNumber_AsSsize_t(slice->step, |
| 4876 | PyExc_ValueError); |
| 4877 | if (step == -1 && PyErr_Occurred()) |
| 4878 | return NULL; |
| 4879 | if (step == 0) { |
| 4880 | PyErr_SetString(PyExc_ValueError, |
| 4881 | "slice step cannot be zero"); |
| 4882 | return NULL; |
| 4883 | } |
| 4884 | } |
| 4885 | if (slice->start == Py_None) { |
| 4886 | if (step < 0) { |
| 4887 | PyErr_SetString(PyExc_ValueError, |
| 4888 | "slice start is required " |
| 4889 | "for step < 0"); |
| 4890 | return NULL; |
| 4891 | } |
| 4892 | start = 0; |
| 4893 | } |
| 4894 | else { |
| 4895 | start = PyNumber_AsSsize_t(slice->start, |
| 4896 | PyExc_ValueError); |
| 4897 | if (start == -1 && PyErr_Occurred()) |
| 4898 | return NULL; |
| 4899 | } |
| 4900 | if (slice->stop == Py_None) { |
| 4901 | PyErr_SetString(PyExc_ValueError, |
| 4902 | "slice stop is required"); |
| 4903 | return NULL; |
| 4904 | } |
| 4905 | stop = PyNumber_AsSsize_t(slice->stop, |
| 4906 | PyExc_ValueError); |
| 4907 | if (stop == -1 && PyErr_Occurred()) |
| 4908 | return NULL; |
| 4909 | if ((step > 0 && start > stop) || |
| 4910 | (step < 0 && start < stop)) |
| 4911 | len = 0; |
| 4912 | else if (step > 0) |
| 4913 | len = (stop - start - 1) / step + 1; |
| 4914 | else |
| 4915 | len = (stop - start + 1) / step + 1; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4916 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4917 | stgdict = PyObject_stgdict((PyObject *)self); |
| 4918 | assert(stgdict); /* Cannot be NULL for pointer instances */ |
| 4919 | proto = stgdict->proto; |
| 4920 | assert(proto); |
| 4921 | itemdict = PyType_stgdict(proto); |
| 4922 | assert(itemdict); |
| 4923 | if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
| 4924 | char *ptr = *(char **)self->b_ptr; |
| 4925 | char *dest; |
| 4926 | |
| 4927 | if (len <= 0) |
| 4928 | return PyBytes_FromStringAndSize("", 0); |
| 4929 | if (step == 1) { |
| 4930 | return PyBytes_FromStringAndSize(ptr + start, |
| 4931 | len); |
| 4932 | } |
| 4933 | dest = (char *)PyMem_Malloc(len); |
| 4934 | if (dest == NULL) |
| 4935 | return PyErr_NoMemory(); |
| 4936 | for (cur = start, i = 0; i < len; cur += step, i++) { |
| 4937 | dest[i] = ptr[cur]; |
| 4938 | } |
| 4939 | np = PyBytes_FromStringAndSize(dest, len); |
| 4940 | PyMem_Free(dest); |
| 4941 | return np; |
| 4942 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4943 | #ifdef CTYPES_UNICODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4944 | if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
| 4945 | wchar_t *ptr = *(wchar_t **)self->b_ptr; |
| 4946 | wchar_t *dest; |
| 4947 | |
| 4948 | if (len <= 0) |
Victor Stinner | 9d3b93b | 2011-11-22 02:27:30 +0100 | [diff] [blame] | 4949 | return PyUnicode_New(0, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4950 | if (step == 1) { |
| 4951 | return PyUnicode_FromWideChar(ptr + start, |
| 4952 | len); |
| 4953 | } |
| 4954 | dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t)); |
| 4955 | if (dest == NULL) |
| 4956 | return PyErr_NoMemory(); |
| 4957 | for (cur = start, i = 0; i < len; cur += step, i++) { |
| 4958 | dest[i] = ptr[cur]; |
| 4959 | } |
| 4960 | np = PyUnicode_FromWideChar(dest, len); |
| 4961 | PyMem_Free(dest); |
| 4962 | return np; |
| 4963 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4964 | #endif |
| 4965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4966 | np = PyList_New(len); |
| 4967 | if (np == NULL) |
| 4968 | return NULL; |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4969 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | for (cur = start, i = 0; i < len; cur += step, i++) { |
| 4971 | PyObject *v = Pointer_item(_self, cur); |
| 4972 | PyList_SET_ITEM(np, i, v); |
| 4973 | } |
| 4974 | return np; |
| 4975 | } |
| 4976 | else { |
| 4977 | PyErr_SetString(PyExc_TypeError, |
| 4978 | "Pointer indices must be integer"); |
| 4979 | return NULL; |
| 4980 | } |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4981 | } |
| 4982 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4983 | static PySequenceMethods Pointer_as_sequence = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4984 | 0, /* inquiry sq_length; */ |
| 4985 | 0, /* binaryfunc sq_concat; */ |
| 4986 | 0, /* intargfunc sq_repeat; */ |
| 4987 | Pointer_item, /* intargfunc sq_item; */ |
| 4988 | 0, /* intintargfunc sq_slice; */ |
| 4989 | Pointer_ass_item, /* intobjargproc sq_ass_item; */ |
| 4990 | 0, /* intintobjargproc sq_ass_slice; */ |
| 4991 | 0, /* objobjproc sq_contains; */ |
| 4992 | /* Added in release 2.0 */ |
| 4993 | 0, /* binaryfunc sq_inplace_concat; */ |
| 4994 | 0, /* intargfunc sq_inplace_repeat; */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 4995 | }; |
| 4996 | |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 4997 | static PyMappingMethods Pointer_as_mapping = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4998 | 0, |
| 4999 | Pointer_subscript, |
Thomas Wouters | 47b49bf | 2007-08-30 22:15:33 +0000 | [diff] [blame] | 5000 | }; |
| 5001 | |
Thomas Heller | f5049fc | 2008-08-19 19:49:49 +0000 | [diff] [blame] | 5002 | static int |
| 5003 | Pointer_bool(CDataObject *self) |
| 5004 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5005 | return (*(void **)self->b_ptr != NULL); |
Thomas Heller | f5049fc | 2008-08-19 19:49:49 +0000 | [diff] [blame] | 5006 | } |
| 5007 | |
| 5008 | static PyNumberMethods Pointer_as_number = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5009 | 0, /* nb_add */ |
| 5010 | 0, /* nb_subtract */ |
| 5011 | 0, /* nb_multiply */ |
| 5012 | 0, /* nb_remainder */ |
| 5013 | 0, /* nb_divmod */ |
| 5014 | 0, /* nb_power */ |
| 5015 | 0, /* nb_negative */ |
| 5016 | 0, /* nb_positive */ |
| 5017 | 0, /* nb_absolute */ |
| 5018 | (inquiry)Pointer_bool, /* nb_bool */ |
Thomas Heller | f5049fc | 2008-08-19 19:49:49 +0000 | [diff] [blame] | 5019 | }; |
| 5020 | |
Thomas Heller | 34596a9 | 2009-04-24 20:50:00 +0000 | [diff] [blame] | 5021 | PyTypeObject PyCPointer_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5022 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5023 | "_ctypes._Pointer", |
| 5024 | sizeof(CDataObject), /* tp_basicsize */ |
| 5025 | 0, /* tp_itemsize */ |
| 5026 | 0, /* tp_dealloc */ |
| 5027 | 0, /* tp_print */ |
| 5028 | 0, /* tp_getattr */ |
| 5029 | 0, /* tp_setattr */ |
| 5030 | 0, /* tp_reserved */ |
| 5031 | 0, /* tp_repr */ |
| 5032 | &Pointer_as_number, /* tp_as_number */ |
| 5033 | &Pointer_as_sequence, /* tp_as_sequence */ |
| 5034 | &Pointer_as_mapping, /* tp_as_mapping */ |
| 5035 | 0, /* tp_hash */ |
| 5036 | 0, /* tp_call */ |
| 5037 | 0, /* tp_str */ |
| 5038 | 0, /* tp_getattro */ |
| 5039 | 0, /* tp_setattro */ |
| 5040 | &PyCData_as_buffer, /* tp_as_buffer */ |
| 5041 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 5042 | "XXX to be provided", /* tp_doc */ |
| 5043 | (traverseproc)PyCData_traverse, /* tp_traverse */ |
| 5044 | (inquiry)PyCData_clear, /* tp_clear */ |
| 5045 | 0, /* tp_richcompare */ |
| 5046 | 0, /* tp_weaklistoffset */ |
| 5047 | 0, /* tp_iter */ |
| 5048 | 0, /* tp_iternext */ |
| 5049 | 0, /* tp_methods */ |
| 5050 | 0, /* tp_members */ |
| 5051 | Pointer_getsets, /* tp_getset */ |
| 5052 | 0, /* tp_base */ |
| 5053 | 0, /* tp_dict */ |
| 5054 | 0, /* tp_descr_get */ |
| 5055 | 0, /* tp_descr_set */ |
| 5056 | 0, /* tp_dictoffset */ |
| 5057 | (initproc)Pointer_init, /* tp_init */ |
| 5058 | 0, /* tp_alloc */ |
| 5059 | Pointer_new, /* tp_new */ |
| 5060 | 0, /* tp_free */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5061 | }; |
| 5062 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5064 | /******************************************************************/ |
| 5065 | /* |
| 5066 | * Module initialization. |
| 5067 | */ |
| 5068 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5069 | static const char module_docs[] = |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5070 | "Create and manipulate C compatible data types in Python."; |
| 5071 | |
| 5072 | #ifdef MS_WIN32 |
| 5073 | |
| 5074 | static char comerror_doc[] = "Raised when a COM method call failed."; |
| 5075 | |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5076 | int |
| 5077 | comerror_init(PyObject *self, PyObject *args, PyObject *kwds) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5078 | { |
| 5079 | PyObject *hresult, *text, *details; |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5080 | PyBaseExceptionObject *bself; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5081 | PyObject *a; |
| 5082 | int status; |
| 5083 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 5084 | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5085 | return -1; |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5086 | |
| 5087 | if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5088 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5089 | |
| 5090 | a = PySequence_GetSlice(args, 1, PySequence_Size(args)); |
| 5091 | if (!a) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5092 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5093 | status = PyObject_SetAttrString(self, "args", a); |
| 5094 | Py_DECREF(a); |
| 5095 | if (status < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5096 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5097 | |
| 5098 | if (PyObject_SetAttrString(self, "hresult", hresult) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5099 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5100 | |
| 5101 | if (PyObject_SetAttrString(self, "text", text) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5102 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5103 | |
| 5104 | if (PyObject_SetAttrString(self, "details", details) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5105 | return -1; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5106 | |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5107 | bself = (PyBaseExceptionObject *)self; |
| 5108 | Py_DECREF(bself->args); |
| 5109 | bself->args = args; |
| 5110 | Py_INCREF(bself->args); |
| 5111 | |
| 5112 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5115 | static PyTypeObject PyComError_Type = { |
| 5116 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5117 | "_ctypes.COMError", /* tp_name */ |
| 5118 | sizeof(PyBaseExceptionObject), /* tp_basicsize */ |
| 5119 | 0, /* tp_itemsize */ |
| 5120 | 0, /* tp_dealloc */ |
| 5121 | 0, /* tp_print */ |
| 5122 | 0, /* tp_getattr */ |
| 5123 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 5124 | 0, /* tp_reserved */ |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5125 | 0, /* tp_repr */ |
| 5126 | 0, /* tp_as_number */ |
| 5127 | 0, /* tp_as_sequence */ |
| 5128 | 0, /* tp_as_mapping */ |
| 5129 | 0, /* tp_hash */ |
| 5130 | 0, /* tp_call */ |
| 5131 | 0, /* tp_str */ |
| 5132 | 0, /* tp_getattro */ |
| 5133 | 0, /* tp_setattro */ |
| 5134 | 0, /* tp_as_buffer */ |
Thomas Heller | 60e5f5e | 2008-11-26 18:40:58 +0000 | [diff] [blame] | 5135 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5136 | PyDoc_STR(comerror_doc), /* tp_doc */ |
| 5137 | 0, /* tp_traverse */ |
| 5138 | 0, /* tp_clear */ |
| 5139 | 0, /* tp_richcompare */ |
| 5140 | 0, /* tp_weaklistoffset */ |
| 5141 | 0, /* tp_iter */ |
| 5142 | 0, /* tp_iternext */ |
| 5143 | 0, /* tp_methods */ |
| 5144 | 0, /* tp_members */ |
| 5145 | 0, /* tp_getset */ |
| 5146 | 0, /* tp_base */ |
| 5147 | 0, /* tp_dict */ |
| 5148 | 0, /* tp_descr_get */ |
| 5149 | 0, /* tp_descr_set */ |
| 5150 | 0, /* tp_dictoffset */ |
| 5151 | (initproc)comerror_init, /* tp_init */ |
| 5152 | 0, /* tp_alloc */ |
| 5153 | 0, /* tp_new */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5154 | }; |
| 5155 | |
Christian Heimes | c0ac106 | 2007-11-28 12:33:51 +0000 | [diff] [blame] | 5156 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5157 | static int |
| 5158 | create_comerror(void) |
| 5159 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5160 | PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception; |
| 5161 | if (PyType_Ready(&PyComError_Type) < 0) |
| 5162 | return -1; |
Hirokazu Yamamoto | 34aa30c | 2010-09-12 16:06:18 +0000 | [diff] [blame] | 5163 | Py_INCREF(&PyComError_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5164 | ComError = (PyObject*)&PyComError_Type; |
| 5165 | return 0; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | #endif |
| 5169 | |
| 5170 | static PyObject * |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5171 | string_at(const char *ptr, int size) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5172 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5173 | if (size == -1) |
| 5174 | return PyBytes_FromStringAndSize(ptr, strlen(ptr)); |
| 5175 | return PyBytes_FromStringAndSize(ptr, size); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5176 | } |
| 5177 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5178 | static int |
| 5179 | cast_check_pointertype(PyObject *arg) |
| 5180 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5181 | StgDictObject *dict; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5182 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5183 | if (PyCPointerTypeObject_Check(arg)) |
| 5184 | return 1; |
| 5185 | if (PyCFuncPtrTypeObject_Check(arg)) |
| 5186 | return 1; |
| 5187 | dict = PyType_stgdict(arg); |
| 5188 | if (dict) { |
| 5189 | if (PyUnicode_Check(dict->proto) |
| 5190 | && (strchr("sPzUZXO", _PyUnicode_AsString(dict->proto)[0]))) { |
| 5191 | /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ |
| 5192 | return 1; |
| 5193 | } |
| 5194 | } |
| 5195 | PyErr_Format(PyExc_TypeError, |
| 5196 | "cast() argument 2 must be a pointer type, not %s", |
| 5197 | PyType_Check(arg) |
| 5198 | ? ((PyTypeObject *)arg)->tp_name |
| 5199 | : Py_TYPE(arg)->tp_name); |
| 5200 | return 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | static PyObject * |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5204 | cast(void *ptr, PyObject *src, PyObject *ctype) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5205 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5206 | CDataObject *result; |
| 5207 | if (0 == cast_check_pointertype(ctype)) |
| 5208 | return NULL; |
| 5209 | result = (CDataObject *)PyObject_CallFunctionObjArgs(ctype, NULL); |
| 5210 | if (result == NULL) |
| 5211 | return NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5212 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5213 | /* |
| 5214 | The casted objects '_objects' member: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5216 | It must certainly contain the source objects one. |
| 5217 | It must contain the source object itself. |
| 5218 | */ |
| 5219 | if (CDataObject_Check(src)) { |
| 5220 | CDataObject *obj = (CDataObject *)src; |
| 5221 | /* PyCData_GetContainer will initialize src.b_objects, we need |
| 5222 | this so it can be shared */ |
| 5223 | PyCData_GetContainer(obj); |
| 5224 | /* But we need a dictionary! */ |
| 5225 | if (obj->b_objects == Py_None) { |
| 5226 | Py_DECREF(Py_None); |
| 5227 | obj->b_objects = PyDict_New(); |
| 5228 | if (obj->b_objects == NULL) |
| 5229 | goto failed; |
| 5230 | } |
| 5231 | Py_XINCREF(obj->b_objects); |
| 5232 | result->b_objects = obj->b_objects; |
| 5233 | if (result->b_objects && PyDict_CheckExact(result->b_objects)) { |
| 5234 | PyObject *index; |
| 5235 | int rc; |
| 5236 | index = PyLong_FromVoidPtr((void *)src); |
| 5237 | if (index == NULL) |
| 5238 | goto failed; |
| 5239 | rc = PyDict_SetItem(result->b_objects, index, src); |
| 5240 | Py_DECREF(index); |
| 5241 | if (rc == -1) |
| 5242 | goto failed; |
| 5243 | } |
| 5244 | } |
| 5245 | /* Should we assert that result is a pointer type? */ |
| 5246 | memcpy(result->b_ptr, &ptr, sizeof(void *)); |
| 5247 | return (PyObject *)result; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5248 | |
| 5249 | failed: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5250 | Py_DECREF(result); |
| 5251 | return NULL; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5252 | } |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5253 | |
| 5254 | #ifdef CTYPES_UNICODE |
| 5255 | static PyObject * |
| 5256 | wstring_at(const wchar_t *ptr, int size) |
| 5257 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5258 | Py_ssize_t ssize = size; |
| 5259 | if (ssize == -1) |
| 5260 | ssize = wcslen(ptr); |
| 5261 | return PyUnicode_FromWideChar(ptr, ssize); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5262 | } |
| 5263 | #endif |
| 5264 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5265 | |
| 5266 | static struct PyModuleDef _ctypesmodule = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | PyModuleDef_HEAD_INIT, |
| 5268 | "_ctypes", |
| 5269 | module_docs, |
| 5270 | -1, |
| 5271 | _ctypes_module_methods, |
| 5272 | NULL, |
| 5273 | NULL, |
| 5274 | NULL, |
| 5275 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5276 | }; |
| 5277 | |
Thomas Heller | fe8f862 | 2006-03-14 19:53:09 +0000 | [diff] [blame] | 5278 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 5279 | PyInit__ctypes(void) |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5280 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5281 | PyObject *m; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5282 | |
| 5283 | /* Note: |
| 5284 | ob_type is the metatype (the 'type'), defaults to PyType_Type, |
| 5285 | tp_base is the base type, defaults to 'object' aka PyBaseObject_Type. |
| 5286 | */ |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5287 | #ifdef WITH_THREAD |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | PyEval_InitThreads(); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5289 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | m = PyModule_Create(&_ctypesmodule); |
| 5291 | if (!m) |
| 5292 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5294 | _ctypes_ptrtype_cache = PyDict_New(); |
| 5295 | if (_ctypes_ptrtype_cache == NULL) |
| 5296 | return NULL; |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 5297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5298 | PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cache); |
Thomas Heller | 3071f81 | 2008-04-14 16:17:33 +0000 | [diff] [blame] | 5299 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5300 | _unpickle = PyObject_GetAttrString(m, "_unpickle"); |
| 5301 | if (_unpickle == NULL) |
| 5302 | return NULL; |
Thomas Heller | 13394e9 | 2008-02-13 20:40:44 +0000 | [diff] [blame] | 5303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | if (PyType_Ready(&PyCArg_Type) < 0) |
| 5305 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5306 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5307 | if (PyType_Ready(&PyCThunk_Type) < 0) |
| 5308 | return NULL; |
Thomas Heller | e106dc3 | 2008-04-24 18:39:36 +0000 | [diff] [blame] | 5309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | /* StgDict is derived from PyDict_Type */ |
| 5311 | PyCStgDict_Type.tp_base = &PyDict_Type; |
| 5312 | if (PyType_Ready(&PyCStgDict_Type) < 0) |
| 5313 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5314 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5315 | /************************************************* |
| 5316 | * |
| 5317 | * Metaclasses |
| 5318 | */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5320 | PyCStructType_Type.tp_base = &PyType_Type; |
| 5321 | if (PyType_Ready(&PyCStructType_Type) < 0) |
| 5322 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5323 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | UnionType_Type.tp_base = &PyType_Type; |
| 5325 | if (PyType_Ready(&UnionType_Type) < 0) |
| 5326 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5328 | PyCPointerType_Type.tp_base = &PyType_Type; |
| 5329 | if (PyType_Ready(&PyCPointerType_Type) < 0) |
| 5330 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5332 | PyCArrayType_Type.tp_base = &PyType_Type; |
| 5333 | if (PyType_Ready(&PyCArrayType_Type) < 0) |
| 5334 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5335 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5336 | PyCSimpleType_Type.tp_base = &PyType_Type; |
| 5337 | if (PyType_Ready(&PyCSimpleType_Type) < 0) |
| 5338 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5339 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5340 | PyCFuncPtrType_Type.tp_base = &PyType_Type; |
| 5341 | if (PyType_Ready(&PyCFuncPtrType_Type) < 0) |
| 5342 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5343 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5344 | /************************************************* |
| 5345 | * |
| 5346 | * Classes using a custom metaclass |
| 5347 | */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5348 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5349 | if (PyType_Ready(&PyCData_Type) < 0) |
| 5350 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5351 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5352 | Py_TYPE(&Struct_Type) = &PyCStructType_Type; |
| 5353 | Struct_Type.tp_base = &PyCData_Type; |
| 5354 | if (PyType_Ready(&Struct_Type) < 0) |
| 5355 | return NULL; |
Thomas Heller | bf4cc5d | 2010-08-08 18:16:20 +0000 | [diff] [blame] | 5356 | Py_INCREF(&Struct_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5357 | PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5358 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5359 | Py_TYPE(&Union_Type) = &UnionType_Type; |
| 5360 | Union_Type.tp_base = &PyCData_Type; |
| 5361 | if (PyType_Ready(&Union_Type) < 0) |
| 5362 | return NULL; |
Thomas Heller | bf4cc5d | 2010-08-08 18:16:20 +0000 | [diff] [blame] | 5363 | Py_INCREF(&Union_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5364 | PyModule_AddObject(m, "Union", (PyObject *)&Union_Type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5366 | Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type; |
| 5367 | PyCPointer_Type.tp_base = &PyCData_Type; |
| 5368 | if (PyType_Ready(&PyCPointer_Type) < 0) |
| 5369 | return NULL; |
Thomas Heller | bf4cc5d | 2010-08-08 18:16:20 +0000 | [diff] [blame] | 5370 | Py_INCREF(&PyCPointer_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5371 | PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5372 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5373 | Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type; |
| 5374 | PyCArray_Type.tp_base = &PyCData_Type; |
| 5375 | if (PyType_Ready(&PyCArray_Type) < 0) |
| 5376 | return NULL; |
Thomas Heller | bf4cc5d | 2010-08-08 18:16:20 +0000 | [diff] [blame] | 5377 | Py_INCREF(&PyCArray_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5378 | PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | Py_TYPE(&Simple_Type) = &PyCSimpleType_Type; |
| 5381 | Simple_Type.tp_base = &PyCData_Type; |
| 5382 | if (PyType_Ready(&Simple_Type) < 0) |
| 5383 | return NULL; |
Thomas Heller | bf4cc5d | 2010-08-08 18:16:20 +0000 | [diff] [blame] | 5384 | Py_INCREF(&Simple_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5385 | PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5387 | Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type; |
| 5388 | PyCFuncPtr_Type.tp_base = &PyCData_Type; |
| 5389 | if (PyType_Ready(&PyCFuncPtr_Type) < 0) |
| 5390 | return NULL; |
Thomas Heller | bf4cc5d | 2010-08-08 18:16:20 +0000 | [diff] [blame] | 5391 | Py_INCREF(&PyCFuncPtr_Type); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5392 | PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5393 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | /************************************************* |
| 5395 | * |
| 5396 | * Simple classes |
| 5397 | */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5398 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5399 | /* PyCField_Type is derived from PyBaseObject_Type */ |
| 5400 | if (PyType_Ready(&PyCField_Type) < 0) |
| 5401 | return NULL; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5402 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5403 | /************************************************* |
| 5404 | * |
| 5405 | * Other stuff |
| 5406 | */ |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5407 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5408 | DictRemover_Type.tp_new = PyType_GenericNew; |
| 5409 | if (PyType_Ready(&DictRemover_Type) < 0) |
| 5410 | return NULL; |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 5411 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5412 | #ifdef MS_WIN32 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5413 | if (create_comerror() < 0) |
| 5414 | return NULL; |
| 5415 | PyModule_AddObject(m, "COMError", ComError); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5416 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT)); |
| 5418 | PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL)); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5419 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5420 | PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL)); |
| 5421 | PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO)); |
| 5422 | PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR)); |
| 5423 | PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI)); |
| 5424 | PyModule_AddStringConstant(m, "__version__", "1.1.0"); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5425 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5426 | PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove)); |
| 5427 | PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset)); |
| 5428 | PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at)); |
| 5429 | PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast)); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5430 | #ifdef CTYPES_UNICODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5431 | PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at)); |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5432 | #endif |
| 5433 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 5434 | /* If RTLD_LOCAL is not defined (Windows!), set it to zero. */ |
| 5435 | #ifndef RTLD_LOCAL |
| 5436 | #define RTLD_LOCAL 0 |
| 5437 | #endif |
| 5438 | |
| 5439 | /* If RTLD_GLOBAL is not defined (cygwin), set it to the same value as |
| 5440 | RTLD_LOCAL. |
| 5441 | */ |
| 5442 | #ifndef RTLD_GLOBAL |
| 5443 | #define RTLD_GLOBAL RTLD_LOCAL |
| 5444 | #endif |
| 5445 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5446 | PyModule_AddObject(m, "RTLD_LOCAL", PyLong_FromLong(RTLD_LOCAL)); |
| 5447 | PyModule_AddObject(m, "RTLD_GLOBAL", PyLong_FromLong(RTLD_GLOBAL)); |
| 5448 | |
| 5449 | PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL); |
| 5450 | if (PyExc_ArgError) { |
| 5451 | Py_INCREF(PyExc_ArgError); |
| 5452 | PyModule_AddObject(m, "ArgumentError", PyExc_ArgError); |
| 5453 | } |
| 5454 | return m; |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5455 | } |
| 5456 | |
Thomas Heller | d4c9320 | 2006-03-08 19:35:11 +0000 | [diff] [blame] | 5457 | /* |
| 5458 | Local Variables: |
| 5459 | compile-command: "cd .. && python setup.py -q build -g && python setup.py -q build install --home ~" |
| 5460 | End: |
| 5461 | */ |