| 1 | n/a | /* |
|---|
| 2 | n/a | ToDo: |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | Get rid of the checker (and also the converters) field in PyCFuncPtrObject and |
|---|
| 5 | n/a | StgDictObject, and replace them by slot functions in StgDictObject. |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | think about a buffer-like object (memory? bytes?) |
|---|
| 8 | n/a | |
|---|
| 9 | n/a | Should POINTER(c_char) and POINTER(c_wchar) have a .value property? |
|---|
| 10 | n/a | What about c_char and c_wchar arrays then? |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | Add from_mmap, from_file, from_string metaclass methods. |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | Maybe we can get away with from_file (calls read) and with a from_buffer |
|---|
| 15 | n/a | method? |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | And what about the to_mmap, to_file, to_str(?) methods? They would clobber |
|---|
| 18 | n/a | the namespace, probably. So, functions instead? And we already have memmove... |
|---|
| 19 | n/a | */ |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | /* |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | Name methods, members, getsets |
|---|
| 24 | n/a | ============================================================================== |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | PyCStructType_Type __new__(), from_address(), __mul__(), from_param() |
|---|
| 27 | n/a | UnionType_Type __new__(), from_address(), __mul__(), from_param() |
|---|
| 28 | n/a | PyCPointerType_Type __new__(), from_address(), __mul__(), from_param(), set_type() |
|---|
| 29 | n/a | PyCArrayType_Type __new__(), from_address(), __mul__(), from_param() |
|---|
| 30 | n/a | PyCSimpleType_Type __new__(), from_address(), __mul__(), from_param() |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | PyCData_Type |
|---|
| 33 | n/a | Struct_Type __new__(), __init__() |
|---|
| 34 | n/a | PyCPointer_Type __new__(), __init__(), _as_parameter_, contents |
|---|
| 35 | n/a | PyCArray_Type __new__(), __init__(), _as_parameter_, __get/setitem__(), __len__() |
|---|
| 36 | n/a | Simple_Type __new__(), __init__(), _as_parameter_ |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | PyCField_Type |
|---|
| 39 | n/a | PyCStgDict_Type |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | ============================================================================== |
|---|
| 42 | n/a | |
|---|
| 43 | n/a | class methods |
|---|
| 44 | n/a | ------------- |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | It has some similarity to the byref() construct compared to pointer() |
|---|
| 47 | n/a | from_address(addr) |
|---|
| 48 | n/a | - construct an instance from a given memory block (sharing this memory block) |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | from_param(obj) |
|---|
| 51 | n/a | - typecheck and convert a Python object into a C function call parameter |
|---|
| 52 | n/a | The result may be an instance of the type, or an integer or tuple |
|---|
| 53 | n/a | (typecode, value[, obj]) |
|---|
| 54 | n/a | |
|---|
| 55 | n/a | instance methods/properties |
|---|
| 56 | n/a | --------------------------- |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | _as_parameter_ |
|---|
| 59 | n/a | - convert self into a C function call parameter |
|---|
| 60 | n/a | This is either an integer, or a 3-tuple (typecode, value, obj) |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | functions |
|---|
| 63 | n/a | --------- |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | sizeof(cdata) |
|---|
| 66 | n/a | - return the number of bytes the buffer contains |
|---|
| 67 | n/a | |
|---|
| 68 | n/a | sizeof(ctype) |
|---|
| 69 | n/a | - return the number of bytes the buffer of an instance would contain |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | byref(cdata) |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | addressof(cdata) |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | pointer(cdata) |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | POINTER(ctype) |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | bytes(cdata) |
|---|
| 80 | n/a | - return the buffer contents as a sequence of bytes (which is currently a string) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | */ |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | /* |
|---|
| 85 | n/a | * PyCStgDict_Type |
|---|
| 86 | n/a | * PyCStructType_Type |
|---|
| 87 | n/a | * UnionType_Type |
|---|
| 88 | n/a | * PyCPointerType_Type |
|---|
| 89 | n/a | * PyCArrayType_Type |
|---|
| 90 | n/a | * PyCSimpleType_Type |
|---|
| 91 | n/a | * |
|---|
| 92 | n/a | * PyCData_Type |
|---|
| 93 | n/a | * Struct_Type |
|---|
| 94 | n/a | * Union_Type |
|---|
| 95 | n/a | * PyCArray_Type |
|---|
| 96 | n/a | * Simple_Type |
|---|
| 97 | n/a | * PyCPointer_Type |
|---|
| 98 | n/a | * PyCField_Type |
|---|
| 99 | n/a | * |
|---|
| 100 | n/a | */ |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | #define PY_SSIZE_T_CLEAN |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | #include "Python.h" |
|---|
| 105 | n/a | #include "structmember.h" |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | #include <ffi.h> |
|---|
| 108 | n/a | #ifdef MS_WIN32 |
|---|
| 109 | n/a | #include <windows.h> |
|---|
| 110 | n/a | #include <malloc.h> |
|---|
| 111 | n/a | #ifndef IS_INTRESOURCE |
|---|
| 112 | n/a | #define IS_INTRESOURCE(x) (((size_t)(x) >> 16) == 0) |
|---|
| 113 | n/a | #endif |
|---|
| 114 | n/a | #else |
|---|
| 115 | n/a | #include "ctypes_dlfcn.h" |
|---|
| 116 | n/a | #endif |
|---|
| 117 | n/a | #include "ctypes.h" |
|---|
| 118 | n/a | |
|---|
| 119 | n/a | PyObject *PyExc_ArgError; |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | /* This dict maps ctypes types to POINTER types */ |
|---|
| 122 | n/a | PyObject *_ctypes_ptrtype_cache; |
|---|
| 123 | n/a | |
|---|
| 124 | n/a | static PyTypeObject Simple_Type; |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | /* a callable object used for unpickling */ |
|---|
| 127 | n/a | static PyObject *_unpickle; |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | /****************************************************************/ |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | typedef struct { |
|---|
| 134 | n/a | PyObject_HEAD |
|---|
| 135 | n/a | PyObject *key; |
|---|
| 136 | n/a | PyObject *dict; |
|---|
| 137 | n/a | } DictRemoverObject; |
|---|
| 138 | n/a | |
|---|
| 139 | n/a | static void |
|---|
| 140 | n/a | _DictRemover_dealloc(PyObject *myself) |
|---|
| 141 | n/a | { |
|---|
| 142 | n/a | DictRemoverObject *self = (DictRemoverObject *)myself; |
|---|
| 143 | n/a | Py_XDECREF(self->key); |
|---|
| 144 | n/a | Py_XDECREF(self->dict); |
|---|
| 145 | n/a | Py_TYPE(self)->tp_free(myself); |
|---|
| 146 | n/a | } |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | static PyObject * |
|---|
| 149 | n/a | _DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw) |
|---|
| 150 | n/a | { |
|---|
| 151 | n/a | DictRemoverObject *self = (DictRemoverObject *)myself; |
|---|
| 152 | n/a | if (self->key && self->dict) { |
|---|
| 153 | n/a | if (-1 == PyDict_DelItem(self->dict, self->key)) |
|---|
| 154 | n/a | /* XXX Error context */ |
|---|
| 155 | n/a | PyErr_WriteUnraisable(Py_None); |
|---|
| 156 | n/a | Py_CLEAR(self->key); |
|---|
| 157 | n/a | Py_CLEAR(self->dict); |
|---|
| 158 | n/a | } |
|---|
| 159 | n/a | Py_RETURN_NONE; |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | static PyTypeObject DictRemover_Type = { |
|---|
| 163 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 164 | n/a | "_ctypes.DictRemover", /* tp_name */ |
|---|
| 165 | n/a | sizeof(DictRemoverObject), /* tp_basicsize */ |
|---|
| 166 | n/a | 0, /* tp_itemsize */ |
|---|
| 167 | n/a | _DictRemover_dealloc, /* tp_dealloc */ |
|---|
| 168 | n/a | 0, /* tp_print */ |
|---|
| 169 | n/a | 0, /* tp_getattr */ |
|---|
| 170 | n/a | 0, /* tp_setattr */ |
|---|
| 171 | n/a | 0, /* tp_reserved */ |
|---|
| 172 | n/a | 0, /* tp_repr */ |
|---|
| 173 | n/a | 0, /* tp_as_number */ |
|---|
| 174 | n/a | 0, /* tp_as_sequence */ |
|---|
| 175 | n/a | 0, /* tp_as_mapping */ |
|---|
| 176 | n/a | 0, /* tp_hash */ |
|---|
| 177 | n/a | _DictRemover_call, /* tp_call */ |
|---|
| 178 | n/a | 0, /* tp_str */ |
|---|
| 179 | n/a | 0, /* tp_getattro */ |
|---|
| 180 | n/a | 0, /* tp_setattro */ |
|---|
| 181 | n/a | 0, /* tp_as_buffer */ |
|---|
| 182 | n/a | /* XXX should participate in GC? */ |
|---|
| 183 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|---|
| 184 | n/a | "deletes a key from a dictionary", /* tp_doc */ |
|---|
| 185 | n/a | 0, /* tp_traverse */ |
|---|
| 186 | n/a | 0, /* tp_clear */ |
|---|
| 187 | n/a | 0, /* tp_richcompare */ |
|---|
| 188 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 189 | n/a | 0, /* tp_iter */ |
|---|
| 190 | n/a | 0, /* tp_iternext */ |
|---|
| 191 | n/a | 0, /* tp_methods */ |
|---|
| 192 | n/a | 0, /* tp_members */ |
|---|
| 193 | n/a | 0, /* tp_getset */ |
|---|
| 194 | n/a | 0, /* tp_base */ |
|---|
| 195 | n/a | 0, /* tp_dict */ |
|---|
| 196 | n/a | 0, /* tp_descr_get */ |
|---|
| 197 | n/a | 0, /* tp_descr_set */ |
|---|
| 198 | n/a | 0, /* tp_dictoffset */ |
|---|
| 199 | n/a | 0, /* tp_init */ |
|---|
| 200 | n/a | 0, /* tp_alloc */ |
|---|
| 201 | n/a | 0, /* tp_new */ |
|---|
| 202 | n/a | 0, /* tp_free */ |
|---|
| 203 | n/a | }; |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | int |
|---|
| 206 | n/a | PyDict_SetItemProxy(PyObject *dict, PyObject *key, PyObject *item) |
|---|
| 207 | n/a | { |
|---|
| 208 | n/a | PyObject *obj; |
|---|
| 209 | n/a | DictRemoverObject *remover; |
|---|
| 210 | n/a | PyObject *proxy; |
|---|
| 211 | n/a | int result; |
|---|
| 212 | n/a | |
|---|
| 213 | n/a | obj = _PyObject_CallNoArg((PyObject *)&DictRemover_Type); |
|---|
| 214 | n/a | if (obj == NULL) |
|---|
| 215 | n/a | return -1; |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | remover = (DictRemoverObject *)obj; |
|---|
| 218 | n/a | assert(remover->key == NULL); |
|---|
| 219 | n/a | assert(remover->dict == NULL); |
|---|
| 220 | n/a | Py_INCREF(key); |
|---|
| 221 | n/a | remover->key = key; |
|---|
| 222 | n/a | Py_INCREF(dict); |
|---|
| 223 | n/a | remover->dict = dict; |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | proxy = PyWeakref_NewProxy(item, obj); |
|---|
| 226 | n/a | Py_DECREF(obj); |
|---|
| 227 | n/a | if (proxy == NULL) |
|---|
| 228 | n/a | return -1; |
|---|
| 229 | n/a | |
|---|
| 230 | n/a | result = PyDict_SetItem(dict, key, proxy); |
|---|
| 231 | n/a | Py_DECREF(proxy); |
|---|
| 232 | n/a | return result; |
|---|
| 233 | n/a | } |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | PyObject * |
|---|
| 236 | n/a | PyDict_GetItemProxy(PyObject *dict, PyObject *key) |
|---|
| 237 | n/a | { |
|---|
| 238 | n/a | PyObject *result; |
|---|
| 239 | n/a | PyObject *item = PyDict_GetItem(dict, key); |
|---|
| 240 | n/a | |
|---|
| 241 | n/a | if (item == NULL) |
|---|
| 242 | n/a | return NULL; |
|---|
| 243 | n/a | if (!PyWeakref_CheckProxy(item)) |
|---|
| 244 | n/a | return item; |
|---|
| 245 | n/a | result = PyWeakref_GET_OBJECT(item); |
|---|
| 246 | n/a | if (result == Py_None) |
|---|
| 247 | n/a | return NULL; |
|---|
| 248 | n/a | return result; |
|---|
| 249 | n/a | } |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | /******************************************************************/ |
|---|
| 252 | n/a | /* |
|---|
| 253 | n/a | Allocate a memory block for a pep3118 format string, copy prefix (if |
|---|
| 254 | n/a | non-null) and suffix into it. Returns NULL on failure, with the error |
|---|
| 255 | n/a | indicator set. If called with a suffix of NULL the error indicator must |
|---|
| 256 | n/a | already be set. |
|---|
| 257 | n/a | */ |
|---|
| 258 | n/a | char * |
|---|
| 259 | n/a | _ctypes_alloc_format_string(const char *prefix, const char *suffix) |
|---|
| 260 | n/a | { |
|---|
| 261 | n/a | size_t len; |
|---|
| 262 | n/a | char *result; |
|---|
| 263 | n/a | |
|---|
| 264 | n/a | if (suffix == NULL) { |
|---|
| 265 | n/a | assert(PyErr_Occurred()); |
|---|
| 266 | n/a | return NULL; |
|---|
| 267 | n/a | } |
|---|
| 268 | n/a | len = strlen(suffix); |
|---|
| 269 | n/a | if (prefix) |
|---|
| 270 | n/a | len += strlen(prefix); |
|---|
| 271 | n/a | result = PyMem_Malloc(len + 1); |
|---|
| 272 | n/a | if (result == NULL) { |
|---|
| 273 | n/a | PyErr_NoMemory(); |
|---|
| 274 | n/a | return NULL; |
|---|
| 275 | n/a | } |
|---|
| 276 | n/a | if (prefix) |
|---|
| 277 | n/a | strcpy(result, prefix); |
|---|
| 278 | n/a | else |
|---|
| 279 | n/a | result[0] = '\0'; |
|---|
| 280 | n/a | strcat(result, suffix); |
|---|
| 281 | n/a | return result; |
|---|
| 282 | n/a | } |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | /* |
|---|
| 285 | n/a | Allocate a memory block for a pep3118 format string, adding |
|---|
| 286 | n/a | the given prefix (if non-null), an additional shape prefix, and a suffix. |
|---|
| 287 | n/a | Returns NULL on failure, with the error indicator set. If called with |
|---|
| 288 | n/a | a suffix of NULL the error indicator must already be set. |
|---|
| 289 | n/a | */ |
|---|
| 290 | n/a | char * |
|---|
| 291 | n/a | _ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape, |
|---|
| 292 | n/a | const char *prefix, const char *suffix) |
|---|
| 293 | n/a | { |
|---|
| 294 | n/a | char *new_prefix; |
|---|
| 295 | n/a | char *result; |
|---|
| 296 | n/a | char buf[32]; |
|---|
| 297 | n/a | Py_ssize_t prefix_len; |
|---|
| 298 | n/a | int k; |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | prefix_len = 32 * ndim + 3; |
|---|
| 301 | n/a | if (prefix) |
|---|
| 302 | n/a | prefix_len += strlen(prefix); |
|---|
| 303 | n/a | new_prefix = PyMem_Malloc(prefix_len); |
|---|
| 304 | n/a | if (new_prefix == NULL) |
|---|
| 305 | n/a | return NULL; |
|---|
| 306 | n/a | new_prefix[0] = '\0'; |
|---|
| 307 | n/a | if (prefix) |
|---|
| 308 | n/a | strcpy(new_prefix, prefix); |
|---|
| 309 | n/a | if (ndim > 0) { |
|---|
| 310 | n/a | /* Add the prefix "(shape[0],shape[1],...,shape[ndim-1])" */ |
|---|
| 311 | n/a | strcat(new_prefix, "("); |
|---|
| 312 | n/a | for (k = 0; k < ndim; ++k) { |
|---|
| 313 | n/a | if (k < ndim-1) { |
|---|
| 314 | n/a | sprintf(buf, "%"PY_FORMAT_SIZE_T"d,", shape[k]); |
|---|
| 315 | n/a | } else { |
|---|
| 316 | n/a | sprintf(buf, "%"PY_FORMAT_SIZE_T"d)", shape[k]); |
|---|
| 317 | n/a | } |
|---|
| 318 | n/a | strcat(new_prefix, buf); |
|---|
| 319 | n/a | } |
|---|
| 320 | n/a | } |
|---|
| 321 | n/a | result = _ctypes_alloc_format_string(new_prefix, suffix); |
|---|
| 322 | n/a | PyMem_Free(new_prefix); |
|---|
| 323 | n/a | return result; |
|---|
| 324 | n/a | } |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | /* |
|---|
| 327 | n/a | PyCStructType_Type - a meta type/class. Creating a new class using this one as |
|---|
| 328 | n/a | __metaclass__ will call the constructor StructUnionType_new. It replaces the |
|---|
| 329 | n/a | tp_dict member with a new instance of StgDict, and initializes the C |
|---|
| 330 | n/a | accessible fields somehow. |
|---|
| 331 | n/a | */ |
|---|
| 332 | n/a | |
|---|
| 333 | n/a | static PyCArgObject * |
|---|
| 334 | n/a | StructUnionType_paramfunc(CDataObject *self) |
|---|
| 335 | n/a | { |
|---|
| 336 | n/a | PyCArgObject *parg; |
|---|
| 337 | n/a | StgDictObject *stgdict; |
|---|
| 338 | n/a | |
|---|
| 339 | n/a | parg = PyCArgObject_new(); |
|---|
| 340 | n/a | if (parg == NULL) |
|---|
| 341 | n/a | return NULL; |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | parg->tag = 'V'; |
|---|
| 344 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 345 | n/a | assert(stgdict); /* Cannot be NULL for structure/union instances */ |
|---|
| 346 | n/a | parg->pffi_type = &stgdict->ffi_type_pointer; |
|---|
| 347 | n/a | /* For structure parameters (by value), parg->value doesn't contain the structure |
|---|
| 348 | n/a | data itself, instead parg->value.p *points* to the structure's data |
|---|
| 349 | n/a | See also _ctypes.c, function _call_function_pointer(). |
|---|
| 350 | n/a | */ |
|---|
| 351 | n/a | parg->value.p = self->b_ptr; |
|---|
| 352 | n/a | parg->size = self->b_size; |
|---|
| 353 | n/a | Py_INCREF(self); |
|---|
| 354 | n/a | parg->obj = (PyObject *)self; |
|---|
| 355 | n/a | return parg; |
|---|
| 356 | n/a | } |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | static PyObject * |
|---|
| 359 | n/a | StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct) |
|---|
| 360 | n/a | { |
|---|
| 361 | n/a | PyTypeObject *result; |
|---|
| 362 | n/a | PyObject *fields; |
|---|
| 363 | n/a | StgDictObject *dict; |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | /* create the new instance (which is a class, |
|---|
| 366 | n/a | since we are a metatype!) */ |
|---|
| 367 | n/a | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
|---|
| 368 | n/a | if (!result) |
|---|
| 369 | n/a | return NULL; |
|---|
| 370 | n/a | |
|---|
| 371 | n/a | /* keep this for bw compatibility */ |
|---|
| 372 | n/a | if (PyDict_GetItemString(result->tp_dict, "_abstract_")) |
|---|
| 373 | n/a | return (PyObject *)result; |
|---|
| 374 | n/a | |
|---|
| 375 | n/a | dict = (StgDictObject *)_PyObject_CallNoArg((PyObject *)&PyCStgDict_Type); |
|---|
| 376 | n/a | if (!dict) { |
|---|
| 377 | n/a | Py_DECREF(result); |
|---|
| 378 | n/a | return NULL; |
|---|
| 379 | n/a | } |
|---|
| 380 | n/a | /* replace the class dict by our updated stgdict, which holds info |
|---|
| 381 | n/a | about storage requirements of the instances */ |
|---|
| 382 | n/a | if (-1 == PyDict_Update((PyObject *)dict, result->tp_dict)) { |
|---|
| 383 | n/a | Py_DECREF(result); |
|---|
| 384 | n/a | Py_DECREF((PyObject *)dict); |
|---|
| 385 | n/a | return NULL; |
|---|
| 386 | n/a | } |
|---|
| 387 | n/a | Py_SETREF(result->tp_dict, (PyObject *)dict); |
|---|
| 388 | n/a | dict->format = _ctypes_alloc_format_string(NULL, "B"); |
|---|
| 389 | n/a | if (dict->format == NULL) { |
|---|
| 390 | n/a | Py_DECREF(result); |
|---|
| 391 | n/a | return NULL; |
|---|
| 392 | n/a | } |
|---|
| 393 | n/a | |
|---|
| 394 | n/a | dict->paramfunc = StructUnionType_paramfunc; |
|---|
| 395 | n/a | |
|---|
| 396 | n/a | fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); |
|---|
| 397 | n/a | if (!fields) { |
|---|
| 398 | n/a | StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base); |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | if (basedict == NULL) |
|---|
| 401 | n/a | return (PyObject *)result; |
|---|
| 402 | n/a | /* copy base dict */ |
|---|
| 403 | n/a | if (-1 == PyCStgDict_clone(dict, basedict)) { |
|---|
| 404 | n/a | Py_DECREF(result); |
|---|
| 405 | n/a | return NULL; |
|---|
| 406 | n/a | } |
|---|
| 407 | n/a | dict->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass dict */ |
|---|
| 408 | n/a | basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */ |
|---|
| 409 | n/a | return (PyObject *)result; |
|---|
| 410 | n/a | } |
|---|
| 411 | n/a | |
|---|
| 412 | n/a | if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) { |
|---|
| 413 | n/a | Py_DECREF(result); |
|---|
| 414 | n/a | return NULL; |
|---|
| 415 | n/a | } |
|---|
| 416 | n/a | return (PyObject *)result; |
|---|
| 417 | n/a | } |
|---|
| 418 | n/a | |
|---|
| 419 | n/a | static PyObject * |
|---|
| 420 | n/a | PyCStructType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 421 | n/a | { |
|---|
| 422 | n/a | return StructUnionType_new(type, args, kwds, 1); |
|---|
| 423 | n/a | } |
|---|
| 424 | n/a | |
|---|
| 425 | n/a | static PyObject * |
|---|
| 426 | n/a | UnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 427 | n/a | { |
|---|
| 428 | n/a | return StructUnionType_new(type, args, kwds, 0); |
|---|
| 429 | n/a | } |
|---|
| 430 | n/a | |
|---|
| 431 | n/a | static const char from_address_doc[] = |
|---|
| 432 | n/a | "C.from_address(integer) -> C instance\naccess a C instance at the specified address"; |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | static PyObject * |
|---|
| 435 | n/a | CDataType_from_address(PyObject *type, PyObject *value) |
|---|
| 436 | n/a | { |
|---|
| 437 | n/a | void *buf; |
|---|
| 438 | n/a | if (!PyLong_Check(value)) { |
|---|
| 439 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 440 | n/a | "integer expected"); |
|---|
| 441 | n/a | return NULL; |
|---|
| 442 | n/a | } |
|---|
| 443 | n/a | buf = (void *)PyLong_AsVoidPtr(value); |
|---|
| 444 | n/a | if (PyErr_Occurred()) |
|---|
| 445 | n/a | return NULL; |
|---|
| 446 | n/a | return PyCData_AtAddress(type, buf); |
|---|
| 447 | n/a | } |
|---|
| 448 | n/a | |
|---|
| 449 | n/a | static const char from_buffer_doc[] = |
|---|
| 450 | n/a | "C.from_buffer(object, offset=0) -> C instance\ncreate a C instance from a writeable buffer"; |
|---|
| 451 | n/a | |
|---|
| 452 | n/a | static int |
|---|
| 453 | n/a | KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep); |
|---|
| 454 | n/a | |
|---|
| 455 | n/a | static PyObject * |
|---|
| 456 | n/a | CDataType_from_buffer(PyObject *type, PyObject *args) |
|---|
| 457 | n/a | { |
|---|
| 458 | n/a | PyObject *obj; |
|---|
| 459 | n/a | PyObject *mv; |
|---|
| 460 | n/a | PyObject *result; |
|---|
| 461 | n/a | Py_buffer *buffer; |
|---|
| 462 | n/a | Py_ssize_t offset = 0; |
|---|
| 463 | n/a | |
|---|
| 464 | n/a | StgDictObject *dict = PyType_stgdict(type); |
|---|
| 465 | n/a | if (!dict) { |
|---|
| 466 | n/a | PyErr_SetString(PyExc_TypeError, "abstract class"); |
|---|
| 467 | n/a | return NULL; |
|---|
| 468 | n/a | } |
|---|
| 469 | n/a | |
|---|
| 470 | n/a | if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset)) |
|---|
| 471 | n/a | return NULL; |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | mv = PyMemoryView_FromObject(obj); |
|---|
| 474 | n/a | if (mv == NULL) |
|---|
| 475 | n/a | return NULL; |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | buffer = PyMemoryView_GET_BUFFER(mv); |
|---|
| 478 | n/a | |
|---|
| 479 | n/a | if (buffer->readonly) { |
|---|
| 480 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 481 | n/a | "underlying buffer is not writable"); |
|---|
| 482 | n/a | Py_DECREF(mv); |
|---|
| 483 | n/a | return NULL; |
|---|
| 484 | n/a | } |
|---|
| 485 | n/a | |
|---|
| 486 | n/a | if (!PyBuffer_IsContiguous(buffer, 'C')) { |
|---|
| 487 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 488 | n/a | "underlying buffer is not C contiguous"); |
|---|
| 489 | n/a | Py_DECREF(mv); |
|---|
| 490 | n/a | return NULL; |
|---|
| 491 | n/a | } |
|---|
| 492 | n/a | |
|---|
| 493 | n/a | if (offset < 0) { |
|---|
| 494 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 495 | n/a | "offset cannot be negative"); |
|---|
| 496 | n/a | Py_DECREF(mv); |
|---|
| 497 | n/a | return NULL; |
|---|
| 498 | n/a | } |
|---|
| 499 | n/a | |
|---|
| 500 | n/a | if (dict->size > buffer->len - offset) { |
|---|
| 501 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 502 | n/a | "Buffer size too small " |
|---|
| 503 | n/a | "(%zd instead of at least %zd bytes)", |
|---|
| 504 | n/a | buffer->len, dict->size + offset); |
|---|
| 505 | n/a | Py_DECREF(mv); |
|---|
| 506 | n/a | return NULL; |
|---|
| 507 | n/a | } |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | result = PyCData_AtAddress(type, (char *)buffer->buf + offset); |
|---|
| 510 | n/a | if (result == NULL) { |
|---|
| 511 | n/a | Py_DECREF(mv); |
|---|
| 512 | n/a | return NULL; |
|---|
| 513 | n/a | } |
|---|
| 514 | n/a | |
|---|
| 515 | n/a | if (-1 == KeepRef((CDataObject *)result, -1, mv)) { |
|---|
| 516 | n/a | Py_DECREF(result); |
|---|
| 517 | n/a | return NULL; |
|---|
| 518 | n/a | } |
|---|
| 519 | n/a | |
|---|
| 520 | n/a | return result; |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | static const char from_buffer_copy_doc[] = |
|---|
| 524 | n/a | "C.from_buffer_copy(object, offset=0) -> C instance\ncreate a C instance from a readable buffer"; |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | static PyObject * |
|---|
| 527 | n/a | GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
|---|
| 528 | n/a | |
|---|
| 529 | n/a | static PyObject * |
|---|
| 530 | n/a | CDataType_from_buffer_copy(PyObject *type, PyObject *args) |
|---|
| 531 | n/a | { |
|---|
| 532 | n/a | Py_buffer buffer; |
|---|
| 533 | n/a | Py_ssize_t offset = 0; |
|---|
| 534 | n/a | PyObject *result; |
|---|
| 535 | n/a | StgDictObject *dict = PyType_stgdict(type); |
|---|
| 536 | n/a | if (!dict) { |
|---|
| 537 | n/a | PyErr_SetString(PyExc_TypeError, "abstract class"); |
|---|
| 538 | n/a | return NULL; |
|---|
| 539 | n/a | } |
|---|
| 540 | n/a | |
|---|
| 541 | n/a | if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset)) |
|---|
| 542 | n/a | return NULL; |
|---|
| 543 | n/a | |
|---|
| 544 | n/a | if (offset < 0) { |
|---|
| 545 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 546 | n/a | "offset cannot be negative"); |
|---|
| 547 | n/a | PyBuffer_Release(&buffer); |
|---|
| 548 | n/a | return NULL; |
|---|
| 549 | n/a | } |
|---|
| 550 | n/a | |
|---|
| 551 | n/a | if (dict->size > buffer.len - offset) { |
|---|
| 552 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 553 | n/a | "Buffer size too small (%zd instead of at least %zd bytes)", |
|---|
| 554 | n/a | buffer.len, dict->size + offset); |
|---|
| 555 | n/a | PyBuffer_Release(&buffer); |
|---|
| 556 | n/a | return NULL; |
|---|
| 557 | n/a | } |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | result = GenericPyCData_new((PyTypeObject *)type, NULL, NULL); |
|---|
| 560 | n/a | if (result != NULL) { |
|---|
| 561 | n/a | memcpy(((CDataObject *)result)->b_ptr, |
|---|
| 562 | n/a | (char *)buffer.buf + offset, dict->size); |
|---|
| 563 | n/a | } |
|---|
| 564 | n/a | PyBuffer_Release(&buffer); |
|---|
| 565 | n/a | return result; |
|---|
| 566 | n/a | } |
|---|
| 567 | n/a | |
|---|
| 568 | n/a | static const char in_dll_doc[] = |
|---|
| 569 | n/a | "C.in_dll(dll, name) -> C instance\naccess a C instance in a dll"; |
|---|
| 570 | n/a | |
|---|
| 571 | n/a | static PyObject * |
|---|
| 572 | n/a | CDataType_in_dll(PyObject *type, PyObject *args) |
|---|
| 573 | n/a | { |
|---|
| 574 | n/a | PyObject *dll; |
|---|
| 575 | n/a | char *name; |
|---|
| 576 | n/a | PyObject *obj; |
|---|
| 577 | n/a | void *handle; |
|---|
| 578 | n/a | void *address; |
|---|
| 579 | n/a | |
|---|
| 580 | n/a | if (!PyArg_ParseTuple(args, "Os:in_dll", &dll, &name)) |
|---|
| 581 | n/a | return NULL; |
|---|
| 582 | n/a | |
|---|
| 583 | n/a | obj = PyObject_GetAttrString(dll, "_handle"); |
|---|
| 584 | n/a | if (!obj) |
|---|
| 585 | n/a | return NULL; |
|---|
| 586 | n/a | if (!PyLong_Check(obj)) { |
|---|
| 587 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 588 | n/a | "the _handle attribute of the second argument must be an integer"); |
|---|
| 589 | n/a | Py_DECREF(obj); |
|---|
| 590 | n/a | return NULL; |
|---|
| 591 | n/a | } |
|---|
| 592 | n/a | handle = (void *)PyLong_AsVoidPtr(obj); |
|---|
| 593 | n/a | Py_DECREF(obj); |
|---|
| 594 | n/a | if (PyErr_Occurred()) { |
|---|
| 595 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 596 | n/a | "could not convert the _handle attribute to a pointer"); |
|---|
| 597 | n/a | return NULL; |
|---|
| 598 | n/a | } |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | #ifdef MS_WIN32 |
|---|
| 601 | n/a | address = (void *)GetProcAddress(handle, name); |
|---|
| 602 | n/a | if (!address) { |
|---|
| 603 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 604 | n/a | "symbol '%s' not found", |
|---|
| 605 | n/a | name); |
|---|
| 606 | n/a | return NULL; |
|---|
| 607 | n/a | } |
|---|
| 608 | n/a | #else |
|---|
| 609 | n/a | address = (void *)ctypes_dlsym(handle, name); |
|---|
| 610 | n/a | if (!address) { |
|---|
| 611 | n/a | #ifdef __CYGWIN__ |
|---|
| 612 | n/a | /* dlerror() isn't very helpful on cygwin */ |
|---|
| 613 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 614 | n/a | "symbol '%s' not found", |
|---|
| 615 | n/a | name); |
|---|
| 616 | n/a | #else |
|---|
| 617 | n/a | PyErr_SetString(PyExc_ValueError, ctypes_dlerror()); |
|---|
| 618 | n/a | #endif |
|---|
| 619 | n/a | return NULL; |
|---|
| 620 | n/a | } |
|---|
| 621 | n/a | #endif |
|---|
| 622 | n/a | return PyCData_AtAddress(type, address); |
|---|
| 623 | n/a | } |
|---|
| 624 | n/a | |
|---|
| 625 | n/a | static const char from_param_doc[] = |
|---|
| 626 | n/a | "Convert a Python object into a function call parameter."; |
|---|
| 627 | n/a | |
|---|
| 628 | n/a | static PyObject * |
|---|
| 629 | n/a | CDataType_from_param(PyObject *type, PyObject *value) |
|---|
| 630 | n/a | { |
|---|
| 631 | n/a | PyObject *as_parameter; |
|---|
| 632 | n/a | int res = PyObject_IsInstance(value, type); |
|---|
| 633 | n/a | if (res == -1) |
|---|
| 634 | n/a | return NULL; |
|---|
| 635 | n/a | if (res) { |
|---|
| 636 | n/a | Py_INCREF(value); |
|---|
| 637 | n/a | return value; |
|---|
| 638 | n/a | } |
|---|
| 639 | n/a | if (PyCArg_CheckExact(value)) { |
|---|
| 640 | n/a | PyCArgObject *p = (PyCArgObject *)value; |
|---|
| 641 | n/a | PyObject *ob = p->obj; |
|---|
| 642 | n/a | const char *ob_name; |
|---|
| 643 | n/a | StgDictObject *dict; |
|---|
| 644 | n/a | dict = PyType_stgdict(type); |
|---|
| 645 | n/a | |
|---|
| 646 | n/a | /* If we got a PyCArgObject, we must check if the object packed in it |
|---|
| 647 | n/a | is an instance of the type's dict->proto */ |
|---|
| 648 | n/a | if(dict && ob) { |
|---|
| 649 | n/a | res = PyObject_IsInstance(ob, dict->proto); |
|---|
| 650 | n/a | if (res == -1) |
|---|
| 651 | n/a | return NULL; |
|---|
| 652 | n/a | if (res) { |
|---|
| 653 | n/a | Py_INCREF(value); |
|---|
| 654 | n/a | return value; |
|---|
| 655 | n/a | } |
|---|
| 656 | n/a | } |
|---|
| 657 | n/a | ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???"; |
|---|
| 658 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 659 | n/a | "expected %s instance instead of pointer to %s", |
|---|
| 660 | n/a | ((PyTypeObject *)type)->tp_name, ob_name); |
|---|
| 661 | n/a | return NULL; |
|---|
| 662 | n/a | } |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
|---|
| 665 | n/a | if (as_parameter) { |
|---|
| 666 | n/a | value = CDataType_from_param(type, as_parameter); |
|---|
| 667 | n/a | Py_DECREF(as_parameter); |
|---|
| 668 | n/a | return value; |
|---|
| 669 | n/a | } |
|---|
| 670 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 671 | n/a | "expected %s instance instead of %s", |
|---|
| 672 | n/a | ((PyTypeObject *)type)->tp_name, |
|---|
| 673 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 674 | n/a | return NULL; |
|---|
| 675 | n/a | } |
|---|
| 676 | n/a | |
|---|
| 677 | n/a | static PyMethodDef CDataType_methods[] = { |
|---|
| 678 | n/a | { "from_param", CDataType_from_param, METH_O, from_param_doc }, |
|---|
| 679 | n/a | { "from_address", CDataType_from_address, METH_O, from_address_doc }, |
|---|
| 680 | n/a | { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, }, |
|---|
| 681 | n/a | { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, }, |
|---|
| 682 | n/a | { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc }, |
|---|
| 683 | n/a | { NULL, NULL }, |
|---|
| 684 | n/a | }; |
|---|
| 685 | n/a | |
|---|
| 686 | n/a | static PyObject * |
|---|
| 687 | n/a | CDataType_repeat(PyObject *self, Py_ssize_t length) |
|---|
| 688 | n/a | { |
|---|
| 689 | n/a | if (length < 0) |
|---|
| 690 | n/a | return PyErr_Format(PyExc_ValueError, |
|---|
| 691 | n/a | "Array length must be >= 0, not %zd", |
|---|
| 692 | n/a | length); |
|---|
| 693 | n/a | return PyCArrayType_from_ctype(self, length); |
|---|
| 694 | n/a | } |
|---|
| 695 | n/a | |
|---|
| 696 | n/a | static PySequenceMethods CDataType_as_sequence = { |
|---|
| 697 | n/a | 0, /* inquiry sq_length; */ |
|---|
| 698 | n/a | 0, /* binaryfunc sq_concat; */ |
|---|
| 699 | n/a | CDataType_repeat, /* intargfunc sq_repeat; */ |
|---|
| 700 | n/a | 0, /* intargfunc sq_item; */ |
|---|
| 701 | n/a | 0, /* intintargfunc sq_slice; */ |
|---|
| 702 | n/a | 0, /* intobjargproc sq_ass_item; */ |
|---|
| 703 | n/a | 0, /* intintobjargproc sq_ass_slice; */ |
|---|
| 704 | n/a | 0, /* objobjproc sq_contains; */ |
|---|
| 705 | n/a | |
|---|
| 706 | n/a | 0, /* binaryfunc sq_inplace_concat; */ |
|---|
| 707 | n/a | 0, /* intargfunc sq_inplace_repeat; */ |
|---|
| 708 | n/a | }; |
|---|
| 709 | n/a | |
|---|
| 710 | n/a | static int |
|---|
| 711 | n/a | CDataType_clear(PyTypeObject *self) |
|---|
| 712 | n/a | { |
|---|
| 713 | n/a | StgDictObject *dict = PyType_stgdict((PyObject *)self); |
|---|
| 714 | n/a | if (dict) |
|---|
| 715 | n/a | Py_CLEAR(dict->proto); |
|---|
| 716 | n/a | return PyType_Type.tp_clear((PyObject *)self); |
|---|
| 717 | n/a | } |
|---|
| 718 | n/a | |
|---|
| 719 | n/a | static int |
|---|
| 720 | n/a | CDataType_traverse(PyTypeObject *self, visitproc visit, void *arg) |
|---|
| 721 | n/a | { |
|---|
| 722 | n/a | StgDictObject *dict = PyType_stgdict((PyObject *)self); |
|---|
| 723 | n/a | if (dict) |
|---|
| 724 | n/a | Py_VISIT(dict->proto); |
|---|
| 725 | n/a | return PyType_Type.tp_traverse((PyObject *)self, visit, arg); |
|---|
| 726 | n/a | } |
|---|
| 727 | n/a | |
|---|
| 728 | n/a | static int |
|---|
| 729 | n/a | PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value) |
|---|
| 730 | n/a | { |
|---|
| 731 | n/a | /* XXX Should we disallow deleting _fields_? */ |
|---|
| 732 | n/a | if (-1 == PyType_Type.tp_setattro(self, key, value)) |
|---|
| 733 | n/a | return -1; |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | if (value && PyUnicode_Check(key) && |
|---|
| 736 | n/a | _PyUnicode_EqualToASCIIString(key, "_fields_")) |
|---|
| 737 | n/a | return PyCStructUnionType_update_stgdict(self, value, 1); |
|---|
| 738 | n/a | return 0; |
|---|
| 739 | n/a | } |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | |
|---|
| 742 | n/a | static int |
|---|
| 743 | n/a | UnionType_setattro(PyObject *self, PyObject *key, PyObject *value) |
|---|
| 744 | n/a | { |
|---|
| 745 | n/a | /* XXX Should we disallow deleting _fields_? */ |
|---|
| 746 | n/a | if (-1 == PyObject_GenericSetAttr(self, key, value)) |
|---|
| 747 | n/a | return -1; |
|---|
| 748 | n/a | |
|---|
| 749 | n/a | if (PyUnicode_Check(key) && |
|---|
| 750 | n/a | _PyUnicode_EqualToASCIIString(key, "_fields_")) |
|---|
| 751 | n/a | return PyCStructUnionType_update_stgdict(self, value, 0); |
|---|
| 752 | n/a | return 0; |
|---|
| 753 | n/a | } |
|---|
| 754 | n/a | |
|---|
| 755 | n/a | |
|---|
| 756 | n/a | PyTypeObject PyCStructType_Type = { |
|---|
| 757 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 758 | n/a | "_ctypes.PyCStructType", /* tp_name */ |
|---|
| 759 | n/a | 0, /* tp_basicsize */ |
|---|
| 760 | n/a | 0, /* tp_itemsize */ |
|---|
| 761 | n/a | 0, /* tp_dealloc */ |
|---|
| 762 | n/a | 0, /* tp_print */ |
|---|
| 763 | n/a | 0, /* tp_getattr */ |
|---|
| 764 | n/a | 0, /* tp_setattr */ |
|---|
| 765 | n/a | 0, /* tp_reserved */ |
|---|
| 766 | n/a | 0, /* tp_repr */ |
|---|
| 767 | n/a | 0, /* tp_as_number */ |
|---|
| 768 | n/a | &CDataType_as_sequence, /* tp_as_sequence */ |
|---|
| 769 | n/a | 0, /* tp_as_mapping */ |
|---|
| 770 | n/a | 0, /* tp_hash */ |
|---|
| 771 | n/a | 0, /* tp_call */ |
|---|
| 772 | n/a | 0, /* tp_str */ |
|---|
| 773 | n/a | 0, /* tp_getattro */ |
|---|
| 774 | n/a | PyCStructType_setattro, /* tp_setattro */ |
|---|
| 775 | n/a | 0, /* tp_as_buffer */ |
|---|
| 776 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 777 | n/a | "metatype for the CData Objects", /* tp_doc */ |
|---|
| 778 | n/a | (traverseproc)CDataType_traverse, /* tp_traverse */ |
|---|
| 779 | n/a | (inquiry)CDataType_clear, /* tp_clear */ |
|---|
| 780 | n/a | 0, /* tp_richcompare */ |
|---|
| 781 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 782 | n/a | 0, /* tp_iter */ |
|---|
| 783 | n/a | 0, /* tp_iternext */ |
|---|
| 784 | n/a | CDataType_methods, /* tp_methods */ |
|---|
| 785 | n/a | 0, /* tp_members */ |
|---|
| 786 | n/a | 0, /* tp_getset */ |
|---|
| 787 | n/a | 0, /* tp_base */ |
|---|
| 788 | n/a | 0, /* tp_dict */ |
|---|
| 789 | n/a | 0, /* tp_descr_get */ |
|---|
| 790 | n/a | 0, /* tp_descr_set */ |
|---|
| 791 | n/a | 0, /* tp_dictoffset */ |
|---|
| 792 | n/a | 0, /* tp_init */ |
|---|
| 793 | n/a | 0, /* tp_alloc */ |
|---|
| 794 | n/a | PyCStructType_new, /* tp_new */ |
|---|
| 795 | n/a | 0, /* tp_free */ |
|---|
| 796 | n/a | }; |
|---|
| 797 | n/a | |
|---|
| 798 | n/a | static PyTypeObject UnionType_Type = { |
|---|
| 799 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 800 | n/a | "_ctypes.UnionType", /* tp_name */ |
|---|
| 801 | n/a | 0, /* tp_basicsize */ |
|---|
| 802 | n/a | 0, /* tp_itemsize */ |
|---|
| 803 | n/a | 0, /* tp_dealloc */ |
|---|
| 804 | n/a | 0, /* tp_print */ |
|---|
| 805 | n/a | 0, /* tp_getattr */ |
|---|
| 806 | n/a | 0, /* tp_setattr */ |
|---|
| 807 | n/a | 0, /* tp_reserved */ |
|---|
| 808 | n/a | 0, /* tp_repr */ |
|---|
| 809 | n/a | 0, /* tp_as_number */ |
|---|
| 810 | n/a | &CDataType_as_sequence, /* tp_as_sequence */ |
|---|
| 811 | n/a | 0, /* tp_as_mapping */ |
|---|
| 812 | n/a | 0, /* tp_hash */ |
|---|
| 813 | n/a | 0, /* tp_call */ |
|---|
| 814 | n/a | 0, /* tp_str */ |
|---|
| 815 | n/a | 0, /* tp_getattro */ |
|---|
| 816 | n/a | UnionType_setattro, /* tp_setattro */ |
|---|
| 817 | n/a | 0, /* tp_as_buffer */ |
|---|
| 818 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 819 | n/a | "metatype for the CData Objects", /* tp_doc */ |
|---|
| 820 | n/a | (traverseproc)CDataType_traverse, /* tp_traverse */ |
|---|
| 821 | n/a | (inquiry)CDataType_clear, /* tp_clear */ |
|---|
| 822 | n/a | 0, /* tp_richcompare */ |
|---|
| 823 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 824 | n/a | 0, /* tp_iter */ |
|---|
| 825 | n/a | 0, /* tp_iternext */ |
|---|
| 826 | n/a | CDataType_methods, /* tp_methods */ |
|---|
| 827 | n/a | 0, /* tp_members */ |
|---|
| 828 | n/a | 0, /* tp_getset */ |
|---|
| 829 | n/a | 0, /* tp_base */ |
|---|
| 830 | n/a | 0, /* tp_dict */ |
|---|
| 831 | n/a | 0, /* tp_descr_get */ |
|---|
| 832 | n/a | 0, /* tp_descr_set */ |
|---|
| 833 | n/a | 0, /* tp_dictoffset */ |
|---|
| 834 | n/a | 0, /* tp_init */ |
|---|
| 835 | n/a | 0, /* tp_alloc */ |
|---|
| 836 | n/a | UnionType_new, /* tp_new */ |
|---|
| 837 | n/a | 0, /* tp_free */ |
|---|
| 838 | n/a | }; |
|---|
| 839 | n/a | |
|---|
| 840 | n/a | |
|---|
| 841 | n/a | /******************************************************************/ |
|---|
| 842 | n/a | |
|---|
| 843 | n/a | /* |
|---|
| 844 | n/a | |
|---|
| 845 | n/a | The PyCPointerType_Type metaclass must ensure that the subclass of Pointer can be |
|---|
| 846 | n/a | created. It must check for a _type_ attribute in the class. Since are no |
|---|
| 847 | n/a | runtime created properties, a CField is probably *not* needed ? |
|---|
| 848 | n/a | |
|---|
| 849 | n/a | class IntPointer(Pointer): |
|---|
| 850 | n/a | _type_ = "i" |
|---|
| 851 | n/a | |
|---|
| 852 | n/a | The PyCPointer_Type provides the functionality: a contents method/property, a |
|---|
| 853 | n/a | size property/method, and the sequence protocol. |
|---|
| 854 | n/a | |
|---|
| 855 | n/a | */ |
|---|
| 856 | n/a | |
|---|
| 857 | n/a | static int |
|---|
| 858 | n/a | PyCPointerType_SetProto(StgDictObject *stgdict, PyObject *proto) |
|---|
| 859 | n/a | { |
|---|
| 860 | n/a | if (!proto || !PyType_Check(proto)) { |
|---|
| 861 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 862 | n/a | "_type_ must be a type"); |
|---|
| 863 | n/a | return -1; |
|---|
| 864 | n/a | } |
|---|
| 865 | n/a | if (!PyType_stgdict(proto)) { |
|---|
| 866 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 867 | n/a | "_type_ must have storage info"); |
|---|
| 868 | n/a | return -1; |
|---|
| 869 | n/a | } |
|---|
| 870 | n/a | Py_INCREF(proto); |
|---|
| 871 | n/a | Py_XSETREF(stgdict->proto, proto); |
|---|
| 872 | n/a | return 0; |
|---|
| 873 | n/a | } |
|---|
| 874 | n/a | |
|---|
| 875 | n/a | static PyCArgObject * |
|---|
| 876 | n/a | PyCPointerType_paramfunc(CDataObject *self) |
|---|
| 877 | n/a | { |
|---|
| 878 | n/a | PyCArgObject *parg; |
|---|
| 879 | n/a | |
|---|
| 880 | n/a | parg = PyCArgObject_new(); |
|---|
| 881 | n/a | if (parg == NULL) |
|---|
| 882 | n/a | return NULL; |
|---|
| 883 | n/a | |
|---|
| 884 | n/a | parg->tag = 'P'; |
|---|
| 885 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 886 | n/a | Py_INCREF(self); |
|---|
| 887 | n/a | parg->obj = (PyObject *)self; |
|---|
| 888 | n/a | parg->value.p = *(void **)self->b_ptr; |
|---|
| 889 | n/a | return parg; |
|---|
| 890 | n/a | } |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | static PyObject * |
|---|
| 893 | n/a | PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 894 | n/a | { |
|---|
| 895 | n/a | PyTypeObject *result; |
|---|
| 896 | n/a | StgDictObject *stgdict; |
|---|
| 897 | n/a | PyObject *proto; |
|---|
| 898 | n/a | PyObject *typedict; |
|---|
| 899 | n/a | |
|---|
| 900 | n/a | typedict = PyTuple_GetItem(args, 2); |
|---|
| 901 | n/a | if (!typedict) |
|---|
| 902 | n/a | return NULL; |
|---|
| 903 | n/a | /* |
|---|
| 904 | n/a | stgdict items size, align, length contain info about pointers itself, |
|---|
| 905 | n/a | stgdict->proto has info about the pointed to type! |
|---|
| 906 | n/a | */ |
|---|
| 907 | n/a | stgdict = (StgDictObject *)PyObject_CallObject( |
|---|
| 908 | n/a | (PyObject *)&PyCStgDict_Type, NULL); |
|---|
| 909 | n/a | if (!stgdict) |
|---|
| 910 | n/a | return NULL; |
|---|
| 911 | n/a | stgdict->size = sizeof(void *); |
|---|
| 912 | n/a | stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; |
|---|
| 913 | n/a | stgdict->length = 1; |
|---|
| 914 | n/a | stgdict->ffi_type_pointer = ffi_type_pointer; |
|---|
| 915 | n/a | stgdict->paramfunc = PyCPointerType_paramfunc; |
|---|
| 916 | n/a | stgdict->flags |= TYPEFLAG_ISPOINTER; |
|---|
| 917 | n/a | |
|---|
| 918 | n/a | proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */ |
|---|
| 919 | n/a | if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) { |
|---|
| 920 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 921 | n/a | return NULL; |
|---|
| 922 | n/a | } |
|---|
| 923 | n/a | |
|---|
| 924 | n/a | if (proto) { |
|---|
| 925 | n/a | StgDictObject *itemdict = PyType_stgdict(proto); |
|---|
| 926 | n/a | const char *current_format; |
|---|
| 927 | n/a | assert(itemdict); |
|---|
| 928 | n/a | /* If itemdict->format is NULL, then this is a pointer to an |
|---|
| 929 | n/a | incomplete type. We create a generic format string |
|---|
| 930 | n/a | 'pointer to bytes' in this case. XXX Better would be to |
|---|
| 931 | n/a | fix the format string later... |
|---|
| 932 | n/a | */ |
|---|
| 933 | n/a | current_format = itemdict->format ? itemdict->format : "B"; |
|---|
| 934 | n/a | if (itemdict->shape != NULL) { |
|---|
| 935 | n/a | /* pointer to an array: the shape needs to be prefixed */ |
|---|
| 936 | n/a | stgdict->format = _ctypes_alloc_format_string_with_shape( |
|---|
| 937 | n/a | itemdict->ndim, itemdict->shape, "&", current_format); |
|---|
| 938 | n/a | } else { |
|---|
| 939 | n/a | stgdict->format = _ctypes_alloc_format_string("&", current_format); |
|---|
| 940 | n/a | } |
|---|
| 941 | n/a | if (stgdict->format == NULL) { |
|---|
| 942 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 943 | n/a | return NULL; |
|---|
| 944 | n/a | } |
|---|
| 945 | n/a | } |
|---|
| 946 | n/a | |
|---|
| 947 | n/a | /* create the new instance (which is a class, |
|---|
| 948 | n/a | since we are a metatype!) */ |
|---|
| 949 | n/a | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
|---|
| 950 | n/a | if (result == NULL) { |
|---|
| 951 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 952 | n/a | return NULL; |
|---|
| 953 | n/a | } |
|---|
| 954 | n/a | |
|---|
| 955 | n/a | /* replace the class dict by our updated spam dict */ |
|---|
| 956 | n/a | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
|---|
| 957 | n/a | Py_DECREF(result); |
|---|
| 958 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 959 | n/a | return NULL; |
|---|
| 960 | n/a | } |
|---|
| 961 | n/a | Py_SETREF(result->tp_dict, (PyObject *)stgdict); |
|---|
| 962 | n/a | |
|---|
| 963 | n/a | return (PyObject *)result; |
|---|
| 964 | n/a | } |
|---|
| 965 | n/a | |
|---|
| 966 | n/a | |
|---|
| 967 | n/a | static PyObject * |
|---|
| 968 | n/a | PyCPointerType_set_type(PyTypeObject *self, PyObject *type) |
|---|
| 969 | n/a | { |
|---|
| 970 | n/a | StgDictObject *dict; |
|---|
| 971 | n/a | |
|---|
| 972 | n/a | dict = PyType_stgdict((PyObject *)self); |
|---|
| 973 | n/a | assert(dict); |
|---|
| 974 | n/a | |
|---|
| 975 | n/a | if (-1 == PyCPointerType_SetProto(dict, type)) |
|---|
| 976 | n/a | return NULL; |
|---|
| 977 | n/a | |
|---|
| 978 | n/a | if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type)) |
|---|
| 979 | n/a | return NULL; |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | Py_RETURN_NONE; |
|---|
| 982 | n/a | } |
|---|
| 983 | n/a | |
|---|
| 984 | n/a | static PyObject *_byref(PyObject *); |
|---|
| 985 | n/a | |
|---|
| 986 | n/a | static PyObject * |
|---|
| 987 | n/a | PyCPointerType_from_param(PyObject *type, PyObject *value) |
|---|
| 988 | n/a | { |
|---|
| 989 | n/a | StgDictObject *typedict; |
|---|
| 990 | n/a | |
|---|
| 991 | n/a | if (value == Py_None) { |
|---|
| 992 | n/a | /* ConvParam will convert to a NULL pointer later */ |
|---|
| 993 | n/a | Py_INCREF(value); |
|---|
| 994 | n/a | return value; |
|---|
| 995 | n/a | } |
|---|
| 996 | n/a | |
|---|
| 997 | n/a | typedict = PyType_stgdict(type); |
|---|
| 998 | n/a | assert(typedict); /* Cannot be NULL for pointer types */ |
|---|
| 999 | n/a | |
|---|
| 1000 | n/a | /* If we expect POINTER(<type>), but receive a <type> instance, accept |
|---|
| 1001 | n/a | it by calling byref(<type>). |
|---|
| 1002 | n/a | */ |
|---|
| 1003 | n/a | switch (PyObject_IsInstance(value, typedict->proto)) { |
|---|
| 1004 | n/a | case 1: |
|---|
| 1005 | n/a | Py_INCREF(value); /* _byref steals a refcount */ |
|---|
| 1006 | n/a | return _byref(value); |
|---|
| 1007 | n/a | case -1: |
|---|
| 1008 | n/a | return NULL; |
|---|
| 1009 | n/a | default: |
|---|
| 1010 | n/a | break; |
|---|
| 1011 | n/a | } |
|---|
| 1012 | n/a | |
|---|
| 1013 | n/a | if (PointerObject_Check(value) || ArrayObject_Check(value)) { |
|---|
| 1014 | n/a | /* Array instances are also pointers when |
|---|
| 1015 | n/a | the item types are the same. |
|---|
| 1016 | n/a | */ |
|---|
| 1017 | n/a | StgDictObject *v = PyObject_stgdict(value); |
|---|
| 1018 | n/a | assert(v); /* Cannot be NULL for pointer or array objects */ |
|---|
| 1019 | n/a | if (PyObject_IsSubclass(v->proto, typedict->proto)) { |
|---|
| 1020 | n/a | Py_INCREF(value); |
|---|
| 1021 | n/a | return value; |
|---|
| 1022 | n/a | } |
|---|
| 1023 | n/a | } |
|---|
| 1024 | n/a | return CDataType_from_param(type, value); |
|---|
| 1025 | n/a | } |
|---|
| 1026 | n/a | |
|---|
| 1027 | n/a | static PyMethodDef PyCPointerType_methods[] = { |
|---|
| 1028 | n/a | { "from_address", CDataType_from_address, METH_O, from_address_doc }, |
|---|
| 1029 | n/a | { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, }, |
|---|
| 1030 | n/a | { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, }, |
|---|
| 1031 | n/a | { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc}, |
|---|
| 1032 | n/a | { "from_param", (PyCFunction)PyCPointerType_from_param, METH_O, from_param_doc}, |
|---|
| 1033 | n/a | { "set_type", (PyCFunction)PyCPointerType_set_type, METH_O }, |
|---|
| 1034 | n/a | { NULL, NULL }, |
|---|
| 1035 | n/a | }; |
|---|
| 1036 | n/a | |
|---|
| 1037 | n/a | PyTypeObject PyCPointerType_Type = { |
|---|
| 1038 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 1039 | n/a | "_ctypes.PyCPointerType", /* tp_name */ |
|---|
| 1040 | n/a | 0, /* tp_basicsize */ |
|---|
| 1041 | n/a | 0, /* tp_itemsize */ |
|---|
| 1042 | n/a | 0, /* tp_dealloc */ |
|---|
| 1043 | n/a | 0, /* tp_print */ |
|---|
| 1044 | n/a | 0, /* tp_getattr */ |
|---|
| 1045 | n/a | 0, /* tp_setattr */ |
|---|
| 1046 | n/a | 0, /* tp_reserved */ |
|---|
| 1047 | n/a | 0, /* tp_repr */ |
|---|
| 1048 | n/a | 0, /* tp_as_number */ |
|---|
| 1049 | n/a | &CDataType_as_sequence, /* tp_as_sequence */ |
|---|
| 1050 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1051 | n/a | 0, /* tp_hash */ |
|---|
| 1052 | n/a | 0, /* tp_call */ |
|---|
| 1053 | n/a | 0, /* tp_str */ |
|---|
| 1054 | n/a | 0, /* tp_getattro */ |
|---|
| 1055 | n/a | 0, /* tp_setattro */ |
|---|
| 1056 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1057 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 1058 | n/a | "metatype for the Pointer Objects", /* tp_doc */ |
|---|
| 1059 | n/a | (traverseproc)CDataType_traverse, /* tp_traverse */ |
|---|
| 1060 | n/a | (inquiry)CDataType_clear, /* tp_clear */ |
|---|
| 1061 | n/a | 0, /* tp_richcompare */ |
|---|
| 1062 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1063 | n/a | 0, /* tp_iter */ |
|---|
| 1064 | n/a | 0, /* tp_iternext */ |
|---|
| 1065 | n/a | PyCPointerType_methods, /* tp_methods */ |
|---|
| 1066 | n/a | 0, /* tp_members */ |
|---|
| 1067 | n/a | 0, /* tp_getset */ |
|---|
| 1068 | n/a | 0, /* tp_base */ |
|---|
| 1069 | n/a | 0, /* tp_dict */ |
|---|
| 1070 | n/a | 0, /* tp_descr_get */ |
|---|
| 1071 | n/a | 0, /* tp_descr_set */ |
|---|
| 1072 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1073 | n/a | 0, /* tp_init */ |
|---|
| 1074 | n/a | 0, /* tp_alloc */ |
|---|
| 1075 | n/a | PyCPointerType_new, /* tp_new */ |
|---|
| 1076 | n/a | 0, /* tp_free */ |
|---|
| 1077 | n/a | }; |
|---|
| 1078 | n/a | |
|---|
| 1079 | n/a | |
|---|
| 1080 | n/a | /******************************************************************/ |
|---|
| 1081 | n/a | /* |
|---|
| 1082 | n/a | PyCArrayType_Type |
|---|
| 1083 | n/a | */ |
|---|
| 1084 | n/a | /* |
|---|
| 1085 | n/a | PyCArrayType_new ensures that the new Array subclass created has a _length_ |
|---|
| 1086 | n/a | attribute, and a _type_ attribute. |
|---|
| 1087 | n/a | */ |
|---|
| 1088 | n/a | |
|---|
| 1089 | n/a | static int |
|---|
| 1090 | n/a | CharArray_set_raw(CDataObject *self, PyObject *value) |
|---|
| 1091 | n/a | { |
|---|
| 1092 | n/a | char *ptr; |
|---|
| 1093 | n/a | Py_ssize_t size; |
|---|
| 1094 | n/a | Py_buffer view; |
|---|
| 1095 | n/a | |
|---|
| 1096 | n/a | if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
|---|
| 1097 | n/a | return -1; |
|---|
| 1098 | n/a | size = view.len; |
|---|
| 1099 | n/a | ptr = view.buf; |
|---|
| 1100 | n/a | if (size > self->b_size) { |
|---|
| 1101 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1102 | n/a | "byte string too long"); |
|---|
| 1103 | n/a | goto fail; |
|---|
| 1104 | n/a | } |
|---|
| 1105 | n/a | |
|---|
| 1106 | n/a | memcpy(self->b_ptr, ptr, size); |
|---|
| 1107 | n/a | |
|---|
| 1108 | n/a | PyBuffer_Release(&view); |
|---|
| 1109 | n/a | return 0; |
|---|
| 1110 | n/a | fail: |
|---|
| 1111 | n/a | PyBuffer_Release(&view); |
|---|
| 1112 | n/a | return -1; |
|---|
| 1113 | n/a | } |
|---|
| 1114 | n/a | |
|---|
| 1115 | n/a | static PyObject * |
|---|
| 1116 | n/a | CharArray_get_raw(CDataObject *self) |
|---|
| 1117 | n/a | { |
|---|
| 1118 | n/a | return PyBytes_FromStringAndSize(self->b_ptr, self->b_size); |
|---|
| 1119 | n/a | } |
|---|
| 1120 | n/a | |
|---|
| 1121 | n/a | static PyObject * |
|---|
| 1122 | n/a | CharArray_get_value(CDataObject *self) |
|---|
| 1123 | n/a | { |
|---|
| 1124 | n/a | Py_ssize_t i; |
|---|
| 1125 | n/a | char *ptr = self->b_ptr; |
|---|
| 1126 | n/a | for (i = 0; i < self->b_size; ++i) |
|---|
| 1127 | n/a | if (*ptr++ == '\0') |
|---|
| 1128 | n/a | break; |
|---|
| 1129 | n/a | return PyBytes_FromStringAndSize(self->b_ptr, i); |
|---|
| 1130 | n/a | } |
|---|
| 1131 | n/a | |
|---|
| 1132 | n/a | static int |
|---|
| 1133 | n/a | CharArray_set_value(CDataObject *self, PyObject *value) |
|---|
| 1134 | n/a | { |
|---|
| 1135 | n/a | char *ptr; |
|---|
| 1136 | n/a | Py_ssize_t size; |
|---|
| 1137 | n/a | |
|---|
| 1138 | n/a | if (value == NULL) { |
|---|
| 1139 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1140 | n/a | "can't delete attribute"); |
|---|
| 1141 | n/a | return -1; |
|---|
| 1142 | n/a | } |
|---|
| 1143 | n/a | |
|---|
| 1144 | n/a | if (!PyBytes_Check(value)) { |
|---|
| 1145 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1146 | n/a | "bytes expected instead of %s instance", |
|---|
| 1147 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 1148 | n/a | return -1; |
|---|
| 1149 | n/a | } else |
|---|
| 1150 | n/a | Py_INCREF(value); |
|---|
| 1151 | n/a | size = PyBytes_GET_SIZE(value); |
|---|
| 1152 | n/a | if (size > self->b_size) { |
|---|
| 1153 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1154 | n/a | "byte string too long"); |
|---|
| 1155 | n/a | Py_DECREF(value); |
|---|
| 1156 | n/a | return -1; |
|---|
| 1157 | n/a | } |
|---|
| 1158 | n/a | |
|---|
| 1159 | n/a | ptr = PyBytes_AS_STRING(value); |
|---|
| 1160 | n/a | memcpy(self->b_ptr, ptr, size); |
|---|
| 1161 | n/a | if (size < self->b_size) |
|---|
| 1162 | n/a | self->b_ptr[size] = '\0'; |
|---|
| 1163 | n/a | Py_DECREF(value); |
|---|
| 1164 | n/a | |
|---|
| 1165 | n/a | return 0; |
|---|
| 1166 | n/a | } |
|---|
| 1167 | n/a | |
|---|
| 1168 | n/a | static PyGetSetDef CharArray_getsets[] = { |
|---|
| 1169 | n/a | { "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw, |
|---|
| 1170 | n/a | "value", NULL }, |
|---|
| 1171 | n/a | { "value", (getter)CharArray_get_value, (setter)CharArray_set_value, |
|---|
| 1172 | n/a | "string value"}, |
|---|
| 1173 | n/a | { NULL, NULL } |
|---|
| 1174 | n/a | }; |
|---|
| 1175 | n/a | |
|---|
| 1176 | n/a | #ifdef CTYPES_UNICODE |
|---|
| 1177 | n/a | static PyObject * |
|---|
| 1178 | n/a | WCharArray_get_value(CDataObject *self) |
|---|
| 1179 | n/a | { |
|---|
| 1180 | n/a | Py_ssize_t i; |
|---|
| 1181 | n/a | wchar_t *ptr = (wchar_t *)self->b_ptr; |
|---|
| 1182 | n/a | for (i = 0; i < self->b_size/(Py_ssize_t)sizeof(wchar_t); ++i) |
|---|
| 1183 | n/a | if (*ptr++ == (wchar_t)0) |
|---|
| 1184 | n/a | break; |
|---|
| 1185 | n/a | return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i); |
|---|
| 1186 | n/a | } |
|---|
| 1187 | n/a | |
|---|
| 1188 | n/a | static int |
|---|
| 1189 | n/a | WCharArray_set_value(CDataObject *self, PyObject *value) |
|---|
| 1190 | n/a | { |
|---|
| 1191 | n/a | Py_ssize_t result = 0; |
|---|
| 1192 | n/a | Py_UNICODE *wstr; |
|---|
| 1193 | n/a | Py_ssize_t len; |
|---|
| 1194 | n/a | |
|---|
| 1195 | n/a | if (value == NULL) { |
|---|
| 1196 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1197 | n/a | "can't delete attribute"); |
|---|
| 1198 | n/a | return -1; |
|---|
| 1199 | n/a | } |
|---|
| 1200 | n/a | if (!PyUnicode_Check(value)) { |
|---|
| 1201 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 1202 | n/a | "unicode string expected instead of %s instance", |
|---|
| 1203 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 1204 | n/a | return -1; |
|---|
| 1205 | n/a | } else |
|---|
| 1206 | n/a | Py_INCREF(value); |
|---|
| 1207 | n/a | |
|---|
| 1208 | n/a | wstr = PyUnicode_AsUnicodeAndSize(value, &len); |
|---|
| 1209 | n/a | if (wstr == NULL) |
|---|
| 1210 | n/a | return -1; |
|---|
| 1211 | n/a | if ((size_t)len > self->b_size/sizeof(wchar_t)) { |
|---|
| 1212 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1213 | n/a | "string too long"); |
|---|
| 1214 | n/a | result = -1; |
|---|
| 1215 | n/a | goto done; |
|---|
| 1216 | n/a | } |
|---|
| 1217 | n/a | result = PyUnicode_AsWideChar(value, |
|---|
| 1218 | n/a | (wchar_t *)self->b_ptr, |
|---|
| 1219 | n/a | self->b_size/sizeof(wchar_t)); |
|---|
| 1220 | n/a | if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t)) |
|---|
| 1221 | n/a | ((wchar_t *)self->b_ptr)[result] = (wchar_t)0; |
|---|
| 1222 | n/a | done: |
|---|
| 1223 | n/a | Py_DECREF(value); |
|---|
| 1224 | n/a | |
|---|
| 1225 | n/a | return result >= 0 ? 0 : -1; |
|---|
| 1226 | n/a | } |
|---|
| 1227 | n/a | |
|---|
| 1228 | n/a | static PyGetSetDef WCharArray_getsets[] = { |
|---|
| 1229 | n/a | { "value", (getter)WCharArray_get_value, (setter)WCharArray_set_value, |
|---|
| 1230 | n/a | "string value"}, |
|---|
| 1231 | n/a | { NULL, NULL } |
|---|
| 1232 | n/a | }; |
|---|
| 1233 | n/a | #endif |
|---|
| 1234 | n/a | |
|---|
| 1235 | n/a | /* |
|---|
| 1236 | n/a | The next three functions copied from Python's typeobject.c. |
|---|
| 1237 | n/a | |
|---|
| 1238 | n/a | They are used to attach methods, members, or getsets to a type *after* it |
|---|
| 1239 | n/a | has been created: Arrays of characters have additional getsets to treat them |
|---|
| 1240 | n/a | as strings. |
|---|
| 1241 | n/a | */ |
|---|
| 1242 | n/a | /* |
|---|
| 1243 | n/a | static int |
|---|
| 1244 | n/a | add_methods(PyTypeObject *type, PyMethodDef *meth) |
|---|
| 1245 | n/a | { |
|---|
| 1246 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 1247 | n/a | for (; meth->ml_name != NULL; meth++) { |
|---|
| 1248 | n/a | PyObject *descr; |
|---|
| 1249 | n/a | descr = PyDescr_NewMethod(type, meth); |
|---|
| 1250 | n/a | if (descr == NULL) |
|---|
| 1251 | n/a | return -1; |
|---|
| 1252 | n/a | if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0) { |
|---|
| 1253 | n/a | Py_DECREF(descr); |
|---|
| 1254 | n/a | return -1; |
|---|
| 1255 | n/a | } |
|---|
| 1256 | n/a | Py_DECREF(descr); |
|---|
| 1257 | n/a | } |
|---|
| 1258 | n/a | return 0; |
|---|
| 1259 | n/a | } |
|---|
| 1260 | n/a | |
|---|
| 1261 | n/a | static int |
|---|
| 1262 | n/a | add_members(PyTypeObject *type, PyMemberDef *memb) |
|---|
| 1263 | n/a | { |
|---|
| 1264 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 1265 | n/a | for (; memb->name != NULL; memb++) { |
|---|
| 1266 | n/a | PyObject *descr; |
|---|
| 1267 | n/a | descr = PyDescr_NewMember(type, memb); |
|---|
| 1268 | n/a | if (descr == NULL) |
|---|
| 1269 | n/a | return -1; |
|---|
| 1270 | n/a | if (PyDict_SetItemString(dict, memb->name, descr) < 0) { |
|---|
| 1271 | n/a | Py_DECREF(descr); |
|---|
| 1272 | n/a | return -1; |
|---|
| 1273 | n/a | } |
|---|
| 1274 | n/a | Py_DECREF(descr); |
|---|
| 1275 | n/a | } |
|---|
| 1276 | n/a | return 0; |
|---|
| 1277 | n/a | } |
|---|
| 1278 | n/a | */ |
|---|
| 1279 | n/a | |
|---|
| 1280 | n/a | static int |
|---|
| 1281 | n/a | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
|---|
| 1282 | n/a | { |
|---|
| 1283 | n/a | PyObject *dict = type->tp_dict; |
|---|
| 1284 | n/a | for (; gsp->name != NULL; gsp++) { |
|---|
| 1285 | n/a | PyObject *descr; |
|---|
| 1286 | n/a | descr = PyDescr_NewGetSet(type, gsp); |
|---|
| 1287 | n/a | if (descr == NULL) |
|---|
| 1288 | n/a | return -1; |
|---|
| 1289 | n/a | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) { |
|---|
| 1290 | n/a | Py_DECREF(descr); |
|---|
| 1291 | n/a | return -1; |
|---|
| 1292 | n/a | } |
|---|
| 1293 | n/a | Py_DECREF(descr); |
|---|
| 1294 | n/a | } |
|---|
| 1295 | n/a | return 0; |
|---|
| 1296 | n/a | } |
|---|
| 1297 | n/a | |
|---|
| 1298 | n/a | static PyCArgObject * |
|---|
| 1299 | n/a | PyCArrayType_paramfunc(CDataObject *self) |
|---|
| 1300 | n/a | { |
|---|
| 1301 | n/a | PyCArgObject *p = PyCArgObject_new(); |
|---|
| 1302 | n/a | if (p == NULL) |
|---|
| 1303 | n/a | return NULL; |
|---|
| 1304 | n/a | p->tag = 'P'; |
|---|
| 1305 | n/a | p->pffi_type = &ffi_type_pointer; |
|---|
| 1306 | n/a | p->value.p = (char *)self->b_ptr; |
|---|
| 1307 | n/a | Py_INCREF(self); |
|---|
| 1308 | n/a | p->obj = (PyObject *)self; |
|---|
| 1309 | n/a | return p; |
|---|
| 1310 | n/a | } |
|---|
| 1311 | n/a | |
|---|
| 1312 | n/a | static PyObject * |
|---|
| 1313 | n/a | PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1314 | n/a | { |
|---|
| 1315 | n/a | PyTypeObject *result; |
|---|
| 1316 | n/a | StgDictObject *stgdict; |
|---|
| 1317 | n/a | StgDictObject *itemdict; |
|---|
| 1318 | n/a | PyObject *length_attr, *type_attr; |
|---|
| 1319 | n/a | long length; |
|---|
| 1320 | n/a | int overflow; |
|---|
| 1321 | n/a | Py_ssize_t itemsize, itemalign; |
|---|
| 1322 | n/a | |
|---|
| 1323 | n/a | /* create the new instance (which is a class, |
|---|
| 1324 | n/a | since we are a metatype!) */ |
|---|
| 1325 | n/a | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
|---|
| 1326 | n/a | if (result == NULL) |
|---|
| 1327 | n/a | return NULL; |
|---|
| 1328 | n/a | |
|---|
| 1329 | n/a | /* Initialize these variables to NULL so that we can simplify error |
|---|
| 1330 | n/a | handling by using Py_XDECREF. */ |
|---|
| 1331 | n/a | stgdict = NULL; |
|---|
| 1332 | n/a | type_attr = NULL; |
|---|
| 1333 | n/a | |
|---|
| 1334 | n/a | length_attr = PyObject_GetAttrString((PyObject *)result, "_length_"); |
|---|
| 1335 | n/a | if (!length_attr || !PyLong_Check(length_attr)) { |
|---|
| 1336 | n/a | PyErr_SetString(PyExc_AttributeError, |
|---|
| 1337 | n/a | "class must define a '_length_' attribute, " |
|---|
| 1338 | n/a | "which must be a positive integer"); |
|---|
| 1339 | n/a | Py_XDECREF(length_attr); |
|---|
| 1340 | n/a | goto error; |
|---|
| 1341 | n/a | } |
|---|
| 1342 | n/a | length = PyLong_AsLongAndOverflow(length_attr, &overflow); |
|---|
| 1343 | n/a | if (overflow) { |
|---|
| 1344 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1345 | n/a | "The '_length_' attribute is too large"); |
|---|
| 1346 | n/a | Py_DECREF(length_attr); |
|---|
| 1347 | n/a | goto error; |
|---|
| 1348 | n/a | } |
|---|
| 1349 | n/a | Py_DECREF(length_attr); |
|---|
| 1350 | n/a | |
|---|
| 1351 | n/a | type_attr = PyObject_GetAttrString((PyObject *)result, "_type_"); |
|---|
| 1352 | n/a | if (!type_attr) { |
|---|
| 1353 | n/a | PyErr_SetString(PyExc_AttributeError, |
|---|
| 1354 | n/a | "class must define a '_type_' attribute"); |
|---|
| 1355 | n/a | goto error; |
|---|
| 1356 | n/a | } |
|---|
| 1357 | n/a | |
|---|
| 1358 | n/a | stgdict = (StgDictObject *)PyObject_CallObject( |
|---|
| 1359 | n/a | (PyObject *)&PyCStgDict_Type, NULL); |
|---|
| 1360 | n/a | if (!stgdict) |
|---|
| 1361 | n/a | goto error; |
|---|
| 1362 | n/a | |
|---|
| 1363 | n/a | itemdict = PyType_stgdict(type_attr); |
|---|
| 1364 | n/a | if (!itemdict) { |
|---|
| 1365 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1366 | n/a | "_type_ must have storage info"); |
|---|
| 1367 | n/a | goto error; |
|---|
| 1368 | n/a | } |
|---|
| 1369 | n/a | |
|---|
| 1370 | n/a | assert(itemdict->format); |
|---|
| 1371 | n/a | stgdict->format = _ctypes_alloc_format_string(NULL, itemdict->format); |
|---|
| 1372 | n/a | if (stgdict->format == NULL) |
|---|
| 1373 | n/a | goto error; |
|---|
| 1374 | n/a | stgdict->ndim = itemdict->ndim + 1; |
|---|
| 1375 | n/a | stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t) * stgdict->ndim); |
|---|
| 1376 | n/a | if (stgdict->shape == NULL) { |
|---|
| 1377 | n/a | PyErr_NoMemory(); |
|---|
| 1378 | n/a | goto error; |
|---|
| 1379 | n/a | } |
|---|
| 1380 | n/a | stgdict->shape[0] = length; |
|---|
| 1381 | n/a | if (stgdict->ndim > 1) { |
|---|
| 1382 | n/a | memmove(&stgdict->shape[1], itemdict->shape, |
|---|
| 1383 | n/a | sizeof(Py_ssize_t) * (stgdict->ndim - 1)); |
|---|
| 1384 | n/a | } |
|---|
| 1385 | n/a | |
|---|
| 1386 | n/a | itemsize = itemdict->size; |
|---|
| 1387 | n/a | if (length * itemsize < 0) { |
|---|
| 1388 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1389 | n/a | "array too large"); |
|---|
| 1390 | n/a | goto error; |
|---|
| 1391 | n/a | } |
|---|
| 1392 | n/a | |
|---|
| 1393 | n/a | itemalign = itemdict->align; |
|---|
| 1394 | n/a | |
|---|
| 1395 | n/a | if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER)) |
|---|
| 1396 | n/a | stgdict->flags |= TYPEFLAG_HASPOINTER; |
|---|
| 1397 | n/a | |
|---|
| 1398 | n/a | stgdict->size = itemsize * length; |
|---|
| 1399 | n/a | stgdict->align = itemalign; |
|---|
| 1400 | n/a | stgdict->length = length; |
|---|
| 1401 | n/a | stgdict->proto = type_attr; |
|---|
| 1402 | n/a | |
|---|
| 1403 | n/a | stgdict->paramfunc = &PyCArrayType_paramfunc; |
|---|
| 1404 | n/a | |
|---|
| 1405 | n/a | /* Arrays are passed as pointers to function calls. */ |
|---|
| 1406 | n/a | stgdict->ffi_type_pointer = ffi_type_pointer; |
|---|
| 1407 | n/a | |
|---|
| 1408 | n/a | /* replace the class dict by our updated spam dict */ |
|---|
| 1409 | n/a | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) |
|---|
| 1410 | n/a | goto error; |
|---|
| 1411 | n/a | Py_SETREF(result->tp_dict, (PyObject *)stgdict); /* steal the reference */ |
|---|
| 1412 | n/a | stgdict = NULL; |
|---|
| 1413 | n/a | |
|---|
| 1414 | n/a | /* Special case for character arrays. |
|---|
| 1415 | n/a | A permanent annoyance: char arrays are also strings! |
|---|
| 1416 | n/a | */ |
|---|
| 1417 | n/a | if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
|---|
| 1418 | n/a | if (-1 == add_getset(result, CharArray_getsets)) |
|---|
| 1419 | n/a | goto error; |
|---|
| 1420 | n/a | #ifdef CTYPES_UNICODE |
|---|
| 1421 | n/a | } else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
|---|
| 1422 | n/a | if (-1 == add_getset(result, WCharArray_getsets)) |
|---|
| 1423 | n/a | goto error; |
|---|
| 1424 | n/a | #endif |
|---|
| 1425 | n/a | } |
|---|
| 1426 | n/a | |
|---|
| 1427 | n/a | return (PyObject *)result; |
|---|
| 1428 | n/a | error: |
|---|
| 1429 | n/a | Py_XDECREF((PyObject*)stgdict); |
|---|
| 1430 | n/a | Py_XDECREF(type_attr); |
|---|
| 1431 | n/a | Py_DECREF(result); |
|---|
| 1432 | n/a | return NULL; |
|---|
| 1433 | n/a | } |
|---|
| 1434 | n/a | |
|---|
| 1435 | n/a | PyTypeObject PyCArrayType_Type = { |
|---|
| 1436 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 1437 | n/a | "_ctypes.PyCArrayType", /* tp_name */ |
|---|
| 1438 | n/a | 0, /* tp_basicsize */ |
|---|
| 1439 | n/a | 0, /* tp_itemsize */ |
|---|
| 1440 | n/a | 0, /* tp_dealloc */ |
|---|
| 1441 | n/a | 0, /* tp_print */ |
|---|
| 1442 | n/a | 0, /* tp_getattr */ |
|---|
| 1443 | n/a | 0, /* tp_setattr */ |
|---|
| 1444 | n/a | 0, /* tp_reserved */ |
|---|
| 1445 | n/a | 0, /* tp_repr */ |
|---|
| 1446 | n/a | 0, /* tp_as_number */ |
|---|
| 1447 | n/a | &CDataType_as_sequence, /* tp_as_sequence */ |
|---|
| 1448 | n/a | 0, /* tp_as_mapping */ |
|---|
| 1449 | n/a | 0, /* tp_hash */ |
|---|
| 1450 | n/a | 0, /* tp_call */ |
|---|
| 1451 | n/a | 0, /* tp_str */ |
|---|
| 1452 | n/a | 0, /* tp_getattro */ |
|---|
| 1453 | n/a | 0, /* tp_setattro */ |
|---|
| 1454 | n/a | 0, /* tp_as_buffer */ |
|---|
| 1455 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 1456 | n/a | "metatype for the Array Objects", /* tp_doc */ |
|---|
| 1457 | n/a | 0, /* tp_traverse */ |
|---|
| 1458 | n/a | 0, /* tp_clear */ |
|---|
| 1459 | n/a | 0, /* tp_richcompare */ |
|---|
| 1460 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 1461 | n/a | 0, /* tp_iter */ |
|---|
| 1462 | n/a | 0, /* tp_iternext */ |
|---|
| 1463 | n/a | CDataType_methods, /* tp_methods */ |
|---|
| 1464 | n/a | 0, /* tp_members */ |
|---|
| 1465 | n/a | 0, /* tp_getset */ |
|---|
| 1466 | n/a | 0, /* tp_base */ |
|---|
| 1467 | n/a | 0, /* tp_dict */ |
|---|
| 1468 | n/a | 0, /* tp_descr_get */ |
|---|
| 1469 | n/a | 0, /* tp_descr_set */ |
|---|
| 1470 | n/a | 0, /* tp_dictoffset */ |
|---|
| 1471 | n/a | 0, /* tp_init */ |
|---|
| 1472 | n/a | 0, /* tp_alloc */ |
|---|
| 1473 | n/a | PyCArrayType_new, /* tp_new */ |
|---|
| 1474 | n/a | 0, /* tp_free */ |
|---|
| 1475 | n/a | }; |
|---|
| 1476 | n/a | |
|---|
| 1477 | n/a | |
|---|
| 1478 | n/a | /******************************************************************/ |
|---|
| 1479 | n/a | /* |
|---|
| 1480 | n/a | PyCSimpleType_Type |
|---|
| 1481 | n/a | */ |
|---|
| 1482 | n/a | /* |
|---|
| 1483 | n/a | |
|---|
| 1484 | n/a | PyCSimpleType_new ensures that the new Simple_Type subclass created has a valid |
|---|
| 1485 | n/a | _type_ attribute. |
|---|
| 1486 | n/a | |
|---|
| 1487 | n/a | */ |
|---|
| 1488 | n/a | |
|---|
| 1489 | n/a | static const char SIMPLE_TYPE_CHARS[] = "cbBhHiIlLdfuzZqQPXOv?g"; |
|---|
| 1490 | n/a | |
|---|
| 1491 | n/a | static PyObject * |
|---|
| 1492 | n/a | c_wchar_p_from_param(PyObject *type, PyObject *value) |
|---|
| 1493 | n/a | { |
|---|
| 1494 | n/a | PyObject *as_parameter; |
|---|
| 1495 | n/a | int res; |
|---|
| 1496 | n/a | if (value == Py_None) { |
|---|
| 1497 | n/a | Py_RETURN_NONE; |
|---|
| 1498 | n/a | } |
|---|
| 1499 | n/a | if (PyUnicode_Check(value)) { |
|---|
| 1500 | n/a | PyCArgObject *parg; |
|---|
| 1501 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("Z"); |
|---|
| 1502 | n/a | |
|---|
| 1503 | n/a | parg = PyCArgObject_new(); |
|---|
| 1504 | n/a | if (parg == NULL) |
|---|
| 1505 | n/a | return NULL; |
|---|
| 1506 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1507 | n/a | parg->tag = 'Z'; |
|---|
| 1508 | n/a | parg->obj = fd->setfunc(&parg->value, value, 0); |
|---|
| 1509 | n/a | if (parg->obj == NULL) { |
|---|
| 1510 | n/a | Py_DECREF(parg); |
|---|
| 1511 | n/a | return NULL; |
|---|
| 1512 | n/a | } |
|---|
| 1513 | n/a | return (PyObject *)parg; |
|---|
| 1514 | n/a | } |
|---|
| 1515 | n/a | res = PyObject_IsInstance(value, type); |
|---|
| 1516 | n/a | if (res == -1) |
|---|
| 1517 | n/a | return NULL; |
|---|
| 1518 | n/a | if (res) { |
|---|
| 1519 | n/a | Py_INCREF(value); |
|---|
| 1520 | n/a | return value; |
|---|
| 1521 | n/a | } |
|---|
| 1522 | n/a | if (ArrayObject_Check(value) || PointerObject_Check(value)) { |
|---|
| 1523 | n/a | /* c_wchar array instance or pointer(c_wchar(...)) */ |
|---|
| 1524 | n/a | StgDictObject *dt = PyObject_stgdict(value); |
|---|
| 1525 | n/a | StgDictObject *dict; |
|---|
| 1526 | n/a | assert(dt); /* Cannot be NULL for pointer or array objects */ |
|---|
| 1527 | n/a | dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; |
|---|
| 1528 | n/a | if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) { |
|---|
| 1529 | n/a | Py_INCREF(value); |
|---|
| 1530 | n/a | return value; |
|---|
| 1531 | n/a | } |
|---|
| 1532 | n/a | } |
|---|
| 1533 | n/a | if (PyCArg_CheckExact(value)) { |
|---|
| 1534 | n/a | /* byref(c_char(...)) */ |
|---|
| 1535 | n/a | PyCArgObject *a = (PyCArgObject *)value; |
|---|
| 1536 | n/a | StgDictObject *dict = PyObject_stgdict(a->obj); |
|---|
| 1537 | n/a | if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) { |
|---|
| 1538 | n/a | Py_INCREF(value); |
|---|
| 1539 | n/a | return value; |
|---|
| 1540 | n/a | } |
|---|
| 1541 | n/a | } |
|---|
| 1542 | n/a | |
|---|
| 1543 | n/a | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
|---|
| 1544 | n/a | if (as_parameter) { |
|---|
| 1545 | n/a | value = c_wchar_p_from_param(type, as_parameter); |
|---|
| 1546 | n/a | Py_DECREF(as_parameter); |
|---|
| 1547 | n/a | return value; |
|---|
| 1548 | n/a | } |
|---|
| 1549 | n/a | /* XXX better message */ |
|---|
| 1550 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1551 | n/a | "wrong type"); |
|---|
| 1552 | n/a | return NULL; |
|---|
| 1553 | n/a | } |
|---|
| 1554 | n/a | |
|---|
| 1555 | n/a | static PyObject * |
|---|
| 1556 | n/a | c_char_p_from_param(PyObject *type, PyObject *value) |
|---|
| 1557 | n/a | { |
|---|
| 1558 | n/a | PyObject *as_parameter; |
|---|
| 1559 | n/a | int res; |
|---|
| 1560 | n/a | if (value == Py_None) { |
|---|
| 1561 | n/a | Py_RETURN_NONE; |
|---|
| 1562 | n/a | } |
|---|
| 1563 | n/a | if (PyBytes_Check(value)) { |
|---|
| 1564 | n/a | PyCArgObject *parg; |
|---|
| 1565 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
|---|
| 1566 | n/a | |
|---|
| 1567 | n/a | parg = PyCArgObject_new(); |
|---|
| 1568 | n/a | if (parg == NULL) |
|---|
| 1569 | n/a | return NULL; |
|---|
| 1570 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1571 | n/a | parg->tag = 'z'; |
|---|
| 1572 | n/a | parg->obj = fd->setfunc(&parg->value, value, 0); |
|---|
| 1573 | n/a | if (parg->obj == NULL) { |
|---|
| 1574 | n/a | Py_DECREF(parg); |
|---|
| 1575 | n/a | return NULL; |
|---|
| 1576 | n/a | } |
|---|
| 1577 | n/a | return (PyObject *)parg; |
|---|
| 1578 | n/a | } |
|---|
| 1579 | n/a | res = PyObject_IsInstance(value, type); |
|---|
| 1580 | n/a | if (res == -1) |
|---|
| 1581 | n/a | return NULL; |
|---|
| 1582 | n/a | if (res) { |
|---|
| 1583 | n/a | Py_INCREF(value); |
|---|
| 1584 | n/a | return value; |
|---|
| 1585 | n/a | } |
|---|
| 1586 | n/a | if (ArrayObject_Check(value) || PointerObject_Check(value)) { |
|---|
| 1587 | n/a | /* c_char array instance or pointer(c_char(...)) */ |
|---|
| 1588 | n/a | StgDictObject *dt = PyObject_stgdict(value); |
|---|
| 1589 | n/a | StgDictObject *dict; |
|---|
| 1590 | n/a | assert(dt); /* Cannot be NULL for pointer or array objects */ |
|---|
| 1591 | n/a | dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL; |
|---|
| 1592 | n/a | if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) { |
|---|
| 1593 | n/a | Py_INCREF(value); |
|---|
| 1594 | n/a | return value; |
|---|
| 1595 | n/a | } |
|---|
| 1596 | n/a | } |
|---|
| 1597 | n/a | if (PyCArg_CheckExact(value)) { |
|---|
| 1598 | n/a | /* byref(c_char(...)) */ |
|---|
| 1599 | n/a | PyCArgObject *a = (PyCArgObject *)value; |
|---|
| 1600 | n/a | StgDictObject *dict = PyObject_stgdict(a->obj); |
|---|
| 1601 | n/a | if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) { |
|---|
| 1602 | n/a | Py_INCREF(value); |
|---|
| 1603 | n/a | return value; |
|---|
| 1604 | n/a | } |
|---|
| 1605 | n/a | } |
|---|
| 1606 | n/a | |
|---|
| 1607 | n/a | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
|---|
| 1608 | n/a | if (as_parameter) { |
|---|
| 1609 | n/a | value = c_char_p_from_param(type, as_parameter); |
|---|
| 1610 | n/a | Py_DECREF(as_parameter); |
|---|
| 1611 | n/a | return value; |
|---|
| 1612 | n/a | } |
|---|
| 1613 | n/a | /* XXX better message */ |
|---|
| 1614 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1615 | n/a | "wrong type"); |
|---|
| 1616 | n/a | return NULL; |
|---|
| 1617 | n/a | } |
|---|
| 1618 | n/a | |
|---|
| 1619 | n/a | static PyObject * |
|---|
| 1620 | n/a | c_void_p_from_param(PyObject *type, PyObject *value) |
|---|
| 1621 | n/a | { |
|---|
| 1622 | n/a | StgDictObject *stgd; |
|---|
| 1623 | n/a | PyObject *as_parameter; |
|---|
| 1624 | n/a | int res; |
|---|
| 1625 | n/a | |
|---|
| 1626 | n/a | /* None */ |
|---|
| 1627 | n/a | if (value == Py_None) { |
|---|
| 1628 | n/a | Py_RETURN_NONE; |
|---|
| 1629 | n/a | } |
|---|
| 1630 | n/a | /* Should probably allow buffer interface as well */ |
|---|
| 1631 | n/a | /* int, long */ |
|---|
| 1632 | n/a | if (PyLong_Check(value)) { |
|---|
| 1633 | n/a | PyCArgObject *parg; |
|---|
| 1634 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("P"); |
|---|
| 1635 | n/a | |
|---|
| 1636 | n/a | parg = PyCArgObject_new(); |
|---|
| 1637 | n/a | if (parg == NULL) |
|---|
| 1638 | n/a | return NULL; |
|---|
| 1639 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1640 | n/a | parg->tag = 'P'; |
|---|
| 1641 | n/a | parg->obj = fd->setfunc(&parg->value, value, 0); |
|---|
| 1642 | n/a | if (parg->obj == NULL) { |
|---|
| 1643 | n/a | Py_DECREF(parg); |
|---|
| 1644 | n/a | return NULL; |
|---|
| 1645 | n/a | } |
|---|
| 1646 | n/a | return (PyObject *)parg; |
|---|
| 1647 | n/a | } |
|---|
| 1648 | n/a | /* XXX struni: remove later */ |
|---|
| 1649 | n/a | /* bytes */ |
|---|
| 1650 | n/a | if (PyBytes_Check(value)) { |
|---|
| 1651 | n/a | PyCArgObject *parg; |
|---|
| 1652 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
|---|
| 1653 | n/a | |
|---|
| 1654 | n/a | parg = PyCArgObject_new(); |
|---|
| 1655 | n/a | if (parg == NULL) |
|---|
| 1656 | n/a | return NULL; |
|---|
| 1657 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1658 | n/a | parg->tag = 'z'; |
|---|
| 1659 | n/a | parg->obj = fd->setfunc(&parg->value, value, 0); |
|---|
| 1660 | n/a | if (parg->obj == NULL) { |
|---|
| 1661 | n/a | Py_DECREF(parg); |
|---|
| 1662 | n/a | return NULL; |
|---|
| 1663 | n/a | } |
|---|
| 1664 | n/a | return (PyObject *)parg; |
|---|
| 1665 | n/a | } |
|---|
| 1666 | n/a | /* unicode */ |
|---|
| 1667 | n/a | if (PyUnicode_Check(value)) { |
|---|
| 1668 | n/a | PyCArgObject *parg; |
|---|
| 1669 | n/a | struct fielddesc *fd = _ctypes_get_fielddesc("Z"); |
|---|
| 1670 | n/a | |
|---|
| 1671 | n/a | parg = PyCArgObject_new(); |
|---|
| 1672 | n/a | if (parg == NULL) |
|---|
| 1673 | n/a | return NULL; |
|---|
| 1674 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1675 | n/a | parg->tag = 'Z'; |
|---|
| 1676 | n/a | parg->obj = fd->setfunc(&parg->value, value, 0); |
|---|
| 1677 | n/a | if (parg->obj == NULL) { |
|---|
| 1678 | n/a | Py_DECREF(parg); |
|---|
| 1679 | n/a | return NULL; |
|---|
| 1680 | n/a | } |
|---|
| 1681 | n/a | return (PyObject *)parg; |
|---|
| 1682 | n/a | } |
|---|
| 1683 | n/a | /* c_void_p instance (or subclass) */ |
|---|
| 1684 | n/a | res = PyObject_IsInstance(value, type); |
|---|
| 1685 | n/a | if (res == -1) |
|---|
| 1686 | n/a | return NULL; |
|---|
| 1687 | n/a | if (res) { |
|---|
| 1688 | n/a | /* c_void_p instances */ |
|---|
| 1689 | n/a | Py_INCREF(value); |
|---|
| 1690 | n/a | return value; |
|---|
| 1691 | n/a | } |
|---|
| 1692 | n/a | /* ctypes array or pointer instance */ |
|---|
| 1693 | n/a | if (ArrayObject_Check(value) || PointerObject_Check(value)) { |
|---|
| 1694 | n/a | /* Any array or pointer is accepted */ |
|---|
| 1695 | n/a | Py_INCREF(value); |
|---|
| 1696 | n/a | return value; |
|---|
| 1697 | n/a | } |
|---|
| 1698 | n/a | /* byref(...) */ |
|---|
| 1699 | n/a | if (PyCArg_CheckExact(value)) { |
|---|
| 1700 | n/a | /* byref(c_xxx()) */ |
|---|
| 1701 | n/a | PyCArgObject *a = (PyCArgObject *)value; |
|---|
| 1702 | n/a | if (a->tag == 'P') { |
|---|
| 1703 | n/a | Py_INCREF(value); |
|---|
| 1704 | n/a | return value; |
|---|
| 1705 | n/a | } |
|---|
| 1706 | n/a | } |
|---|
| 1707 | n/a | /* function pointer */ |
|---|
| 1708 | n/a | if (PyCFuncPtrObject_Check(value)) { |
|---|
| 1709 | n/a | PyCArgObject *parg; |
|---|
| 1710 | n/a | PyCFuncPtrObject *func; |
|---|
| 1711 | n/a | func = (PyCFuncPtrObject *)value; |
|---|
| 1712 | n/a | parg = PyCArgObject_new(); |
|---|
| 1713 | n/a | if (parg == NULL) |
|---|
| 1714 | n/a | return NULL; |
|---|
| 1715 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1716 | n/a | parg->tag = 'P'; |
|---|
| 1717 | n/a | Py_INCREF(value); |
|---|
| 1718 | n/a | parg->value.p = *(void **)func->b_ptr; |
|---|
| 1719 | n/a | parg->obj = value; |
|---|
| 1720 | n/a | return (PyObject *)parg; |
|---|
| 1721 | n/a | } |
|---|
| 1722 | n/a | /* c_char_p, c_wchar_p */ |
|---|
| 1723 | n/a | stgd = PyObject_stgdict(value); |
|---|
| 1724 | n/a | if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd->proto)) { |
|---|
| 1725 | n/a | PyCArgObject *parg; |
|---|
| 1726 | n/a | |
|---|
| 1727 | n/a | switch (PyUnicode_AsUTF8(stgd->proto)[0]) { |
|---|
| 1728 | n/a | case 'z': /* c_char_p */ |
|---|
| 1729 | n/a | case 'Z': /* c_wchar_p */ |
|---|
| 1730 | n/a | parg = PyCArgObject_new(); |
|---|
| 1731 | n/a | if (parg == NULL) |
|---|
| 1732 | n/a | return NULL; |
|---|
| 1733 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 1734 | n/a | parg->tag = 'Z'; |
|---|
| 1735 | n/a | Py_INCREF(value); |
|---|
| 1736 | n/a | parg->obj = value; |
|---|
| 1737 | n/a | /* Remember: b_ptr points to where the pointer is stored! */ |
|---|
| 1738 | n/a | parg->value.p = *(void **)(((CDataObject *)value)->b_ptr); |
|---|
| 1739 | n/a | return (PyObject *)parg; |
|---|
| 1740 | n/a | } |
|---|
| 1741 | n/a | } |
|---|
| 1742 | n/a | |
|---|
| 1743 | n/a | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
|---|
| 1744 | n/a | if (as_parameter) { |
|---|
| 1745 | n/a | value = c_void_p_from_param(type, as_parameter); |
|---|
| 1746 | n/a | Py_DECREF(as_parameter); |
|---|
| 1747 | n/a | return value; |
|---|
| 1748 | n/a | } |
|---|
| 1749 | n/a | /* XXX better message */ |
|---|
| 1750 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1751 | n/a | "wrong type"); |
|---|
| 1752 | n/a | return NULL; |
|---|
| 1753 | n/a | } |
|---|
| 1754 | n/a | |
|---|
| 1755 | n/a | static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O }; |
|---|
| 1756 | n/a | static PyMethodDef c_char_p_method = { "from_param", c_char_p_from_param, METH_O }; |
|---|
| 1757 | n/a | static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH_O }; |
|---|
| 1758 | n/a | |
|---|
| 1759 | n/a | static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds, |
|---|
| 1760 | n/a | PyObject *proto, struct fielddesc *fmt) |
|---|
| 1761 | n/a | { |
|---|
| 1762 | n/a | PyTypeObject *result; |
|---|
| 1763 | n/a | StgDictObject *stgdict; |
|---|
| 1764 | n/a | PyObject *name = PyTuple_GET_ITEM(args, 0); |
|---|
| 1765 | n/a | PyObject *newname; |
|---|
| 1766 | n/a | PyObject *swapped_args; |
|---|
| 1767 | n/a | static PyObject *suffix; |
|---|
| 1768 | n/a | Py_ssize_t i; |
|---|
| 1769 | n/a | |
|---|
| 1770 | n/a | swapped_args = PyTuple_New(PyTuple_GET_SIZE(args)); |
|---|
| 1771 | n/a | if (!swapped_args) |
|---|
| 1772 | n/a | return NULL; |
|---|
| 1773 | n/a | |
|---|
| 1774 | n/a | if (suffix == NULL) |
|---|
| 1775 | n/a | #ifdef WORDS_BIGENDIAN |
|---|
| 1776 | n/a | suffix = PyUnicode_InternFromString("_le"); |
|---|
| 1777 | n/a | #else |
|---|
| 1778 | n/a | suffix = PyUnicode_InternFromString("_be"); |
|---|
| 1779 | n/a | #endif |
|---|
| 1780 | n/a | |
|---|
| 1781 | n/a | newname = PyUnicode_Concat(name, suffix); |
|---|
| 1782 | n/a | if (newname == NULL) { |
|---|
| 1783 | n/a | Py_DECREF(swapped_args); |
|---|
| 1784 | n/a | return NULL; |
|---|
| 1785 | n/a | } |
|---|
| 1786 | n/a | |
|---|
| 1787 | n/a | PyTuple_SET_ITEM(swapped_args, 0, newname); |
|---|
| 1788 | n/a | for (i=1; i<PyTuple_GET_SIZE(args); ++i) { |
|---|
| 1789 | n/a | PyObject *v = PyTuple_GET_ITEM(args, i); |
|---|
| 1790 | n/a | Py_INCREF(v); |
|---|
| 1791 | n/a | PyTuple_SET_ITEM(swapped_args, i, v); |
|---|
| 1792 | n/a | } |
|---|
| 1793 | n/a | |
|---|
| 1794 | n/a | /* create the new instance (which is a class, |
|---|
| 1795 | n/a | since we are a metatype!) */ |
|---|
| 1796 | n/a | result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds); |
|---|
| 1797 | n/a | Py_DECREF(swapped_args); |
|---|
| 1798 | n/a | if (result == NULL) |
|---|
| 1799 | n/a | return NULL; |
|---|
| 1800 | n/a | |
|---|
| 1801 | n/a | stgdict = (StgDictObject *)PyObject_CallObject( |
|---|
| 1802 | n/a | (PyObject *)&PyCStgDict_Type, NULL); |
|---|
| 1803 | n/a | if (!stgdict) { |
|---|
| 1804 | n/a | Py_DECREF(result); |
|---|
| 1805 | n/a | return NULL; |
|---|
| 1806 | n/a | } |
|---|
| 1807 | n/a | |
|---|
| 1808 | n/a | stgdict->ffi_type_pointer = *fmt->pffi_type; |
|---|
| 1809 | n/a | stgdict->align = fmt->pffi_type->alignment; |
|---|
| 1810 | n/a | stgdict->length = 0; |
|---|
| 1811 | n/a | stgdict->size = fmt->pffi_type->size; |
|---|
| 1812 | n/a | stgdict->setfunc = fmt->setfunc_swapped; |
|---|
| 1813 | n/a | stgdict->getfunc = fmt->getfunc_swapped; |
|---|
| 1814 | n/a | |
|---|
| 1815 | n/a | Py_INCREF(proto); |
|---|
| 1816 | n/a | stgdict->proto = proto; |
|---|
| 1817 | n/a | |
|---|
| 1818 | n/a | /* replace the class dict by our updated spam dict */ |
|---|
| 1819 | n/a | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
|---|
| 1820 | n/a | Py_DECREF(result); |
|---|
| 1821 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 1822 | n/a | return NULL; |
|---|
| 1823 | n/a | } |
|---|
| 1824 | n/a | Py_SETREF(result->tp_dict, (PyObject *)stgdict); |
|---|
| 1825 | n/a | |
|---|
| 1826 | n/a | return (PyObject *)result; |
|---|
| 1827 | n/a | } |
|---|
| 1828 | n/a | |
|---|
| 1829 | n/a | static PyCArgObject * |
|---|
| 1830 | n/a | PyCSimpleType_paramfunc(CDataObject *self) |
|---|
| 1831 | n/a | { |
|---|
| 1832 | n/a | StgDictObject *dict; |
|---|
| 1833 | n/a | const char *fmt; |
|---|
| 1834 | n/a | PyCArgObject *parg; |
|---|
| 1835 | n/a | struct fielddesc *fd; |
|---|
| 1836 | n/a | |
|---|
| 1837 | n/a | dict = PyObject_stgdict((PyObject *)self); |
|---|
| 1838 | n/a | assert(dict); /* Cannot be NULL for CDataObject instances */ |
|---|
| 1839 | n/a | fmt = PyUnicode_AsUTF8(dict->proto); |
|---|
| 1840 | n/a | assert(fmt); |
|---|
| 1841 | n/a | |
|---|
| 1842 | n/a | fd = _ctypes_get_fielddesc(fmt); |
|---|
| 1843 | n/a | assert(fd); |
|---|
| 1844 | n/a | |
|---|
| 1845 | n/a | parg = PyCArgObject_new(); |
|---|
| 1846 | n/a | if (parg == NULL) |
|---|
| 1847 | n/a | return NULL; |
|---|
| 1848 | n/a | |
|---|
| 1849 | n/a | parg->tag = fmt[0]; |
|---|
| 1850 | n/a | parg->pffi_type = fd->pffi_type; |
|---|
| 1851 | n/a | Py_INCREF(self); |
|---|
| 1852 | n/a | parg->obj = (PyObject *)self; |
|---|
| 1853 | n/a | memcpy(&parg->value, self->b_ptr, self->b_size); |
|---|
| 1854 | n/a | return parg; |
|---|
| 1855 | n/a | } |
|---|
| 1856 | n/a | |
|---|
| 1857 | n/a | static PyObject * |
|---|
| 1858 | n/a | PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 1859 | n/a | { |
|---|
| 1860 | n/a | PyTypeObject *result; |
|---|
| 1861 | n/a | StgDictObject *stgdict; |
|---|
| 1862 | n/a | PyObject *proto; |
|---|
| 1863 | n/a | const char *proto_str; |
|---|
| 1864 | n/a | Py_ssize_t proto_len; |
|---|
| 1865 | n/a | PyMethodDef *ml; |
|---|
| 1866 | n/a | struct fielddesc *fmt; |
|---|
| 1867 | n/a | |
|---|
| 1868 | n/a | /* create the new instance (which is a class, |
|---|
| 1869 | n/a | since we are a metatype!) */ |
|---|
| 1870 | n/a | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
|---|
| 1871 | n/a | if (result == NULL) |
|---|
| 1872 | n/a | return NULL; |
|---|
| 1873 | n/a | |
|---|
| 1874 | n/a | proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */ |
|---|
| 1875 | n/a | if (!proto) { |
|---|
| 1876 | n/a | PyErr_SetString(PyExc_AttributeError, |
|---|
| 1877 | n/a | "class must define a '_type_' attribute"); |
|---|
| 1878 | n/a | error: |
|---|
| 1879 | n/a | Py_XDECREF(proto); |
|---|
| 1880 | n/a | Py_XDECREF(result); |
|---|
| 1881 | n/a | return NULL; |
|---|
| 1882 | n/a | } |
|---|
| 1883 | n/a | if (PyUnicode_Check(proto)) { |
|---|
| 1884 | n/a | proto_str = PyUnicode_AsUTF8AndSize(proto, &proto_len); |
|---|
| 1885 | n/a | if (!proto_str) |
|---|
| 1886 | n/a | goto error; |
|---|
| 1887 | n/a | } else { |
|---|
| 1888 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1889 | n/a | "class must define a '_type_' string attribute"); |
|---|
| 1890 | n/a | goto error; |
|---|
| 1891 | n/a | } |
|---|
| 1892 | n/a | if (proto_len != 1) { |
|---|
| 1893 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 1894 | n/a | "class must define a '_type_' attribute " |
|---|
| 1895 | n/a | "which must be a string of length 1"); |
|---|
| 1896 | n/a | goto error; |
|---|
| 1897 | n/a | } |
|---|
| 1898 | n/a | if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) { |
|---|
| 1899 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 1900 | n/a | "class must define a '_type_' attribute which must be\n" |
|---|
| 1901 | n/a | "a single character string containing one of '%s'.", |
|---|
| 1902 | n/a | SIMPLE_TYPE_CHARS); |
|---|
| 1903 | n/a | goto error; |
|---|
| 1904 | n/a | } |
|---|
| 1905 | n/a | fmt = _ctypes_get_fielddesc(proto_str); |
|---|
| 1906 | n/a | if (fmt == NULL) { |
|---|
| 1907 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 1908 | n/a | "_type_ '%s' not supported", proto_str); |
|---|
| 1909 | n/a | goto error; |
|---|
| 1910 | n/a | } |
|---|
| 1911 | n/a | |
|---|
| 1912 | n/a | stgdict = (StgDictObject *)PyObject_CallObject( |
|---|
| 1913 | n/a | (PyObject *)&PyCStgDict_Type, NULL); |
|---|
| 1914 | n/a | if (!stgdict) |
|---|
| 1915 | n/a | goto error; |
|---|
| 1916 | n/a | |
|---|
| 1917 | n/a | stgdict->ffi_type_pointer = *fmt->pffi_type; |
|---|
| 1918 | n/a | stgdict->align = fmt->pffi_type->alignment; |
|---|
| 1919 | n/a | stgdict->length = 0; |
|---|
| 1920 | n/a | stgdict->size = fmt->pffi_type->size; |
|---|
| 1921 | n/a | stgdict->setfunc = fmt->setfunc; |
|---|
| 1922 | n/a | stgdict->getfunc = fmt->getfunc; |
|---|
| 1923 | n/a | #ifdef WORDS_BIGENDIAN |
|---|
| 1924 | n/a | stgdict->format = _ctypes_alloc_format_string(">", proto_str); |
|---|
| 1925 | n/a | #else |
|---|
| 1926 | n/a | stgdict->format = _ctypes_alloc_format_string("<", proto_str); |
|---|
| 1927 | n/a | #endif |
|---|
| 1928 | n/a | if (stgdict->format == NULL) { |
|---|
| 1929 | n/a | Py_DECREF(result); |
|---|
| 1930 | n/a | Py_DECREF(proto); |
|---|
| 1931 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 1932 | n/a | return NULL; |
|---|
| 1933 | n/a | } |
|---|
| 1934 | n/a | |
|---|
| 1935 | n/a | stgdict->paramfunc = PyCSimpleType_paramfunc; |
|---|
| 1936 | n/a | /* |
|---|
| 1937 | n/a | if (result->tp_base != &Simple_Type) { |
|---|
| 1938 | n/a | stgdict->setfunc = NULL; |
|---|
| 1939 | n/a | stgdict->getfunc = NULL; |
|---|
| 1940 | n/a | } |
|---|
| 1941 | n/a | */ |
|---|
| 1942 | n/a | |
|---|
| 1943 | n/a | /* This consumes the refcount on proto which we have */ |
|---|
| 1944 | n/a | stgdict->proto = proto; |
|---|
| 1945 | n/a | |
|---|
| 1946 | n/a | /* replace the class dict by our updated spam dict */ |
|---|
| 1947 | n/a | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
|---|
| 1948 | n/a | Py_DECREF(result); |
|---|
| 1949 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 1950 | n/a | return NULL; |
|---|
| 1951 | n/a | } |
|---|
| 1952 | n/a | Py_SETREF(result->tp_dict, (PyObject *)stgdict); |
|---|
| 1953 | n/a | |
|---|
| 1954 | n/a | /* Install from_param class methods in ctypes base classes. |
|---|
| 1955 | n/a | Overrides the PyCSimpleType_from_param generic method. |
|---|
| 1956 | n/a | */ |
|---|
| 1957 | n/a | if (result->tp_base == &Simple_Type) { |
|---|
| 1958 | n/a | switch (*proto_str) { |
|---|
| 1959 | n/a | case 'z': /* c_char_p */ |
|---|
| 1960 | n/a | ml = &c_char_p_method; |
|---|
| 1961 | n/a | stgdict->flags |= TYPEFLAG_ISPOINTER; |
|---|
| 1962 | n/a | break; |
|---|
| 1963 | n/a | case 'Z': /* c_wchar_p */ |
|---|
| 1964 | n/a | ml = &c_wchar_p_method; |
|---|
| 1965 | n/a | stgdict->flags |= TYPEFLAG_ISPOINTER; |
|---|
| 1966 | n/a | break; |
|---|
| 1967 | n/a | case 'P': /* c_void_p */ |
|---|
| 1968 | n/a | ml = &c_void_p_method; |
|---|
| 1969 | n/a | stgdict->flags |= TYPEFLAG_ISPOINTER; |
|---|
| 1970 | n/a | break; |
|---|
| 1971 | n/a | case 's': |
|---|
| 1972 | n/a | case 'X': |
|---|
| 1973 | n/a | case 'O': |
|---|
| 1974 | n/a | ml = NULL; |
|---|
| 1975 | n/a | stgdict->flags |= TYPEFLAG_ISPOINTER; |
|---|
| 1976 | n/a | break; |
|---|
| 1977 | n/a | default: |
|---|
| 1978 | n/a | ml = NULL; |
|---|
| 1979 | n/a | break; |
|---|
| 1980 | n/a | } |
|---|
| 1981 | n/a | |
|---|
| 1982 | n/a | if (ml) { |
|---|
| 1983 | n/a | PyObject *meth; |
|---|
| 1984 | n/a | int x; |
|---|
| 1985 | n/a | meth = PyDescr_NewClassMethod(result, ml); |
|---|
| 1986 | n/a | if (!meth) { |
|---|
| 1987 | n/a | Py_DECREF(result); |
|---|
| 1988 | n/a | return NULL; |
|---|
| 1989 | n/a | } |
|---|
| 1990 | n/a | x = PyDict_SetItemString(result->tp_dict, |
|---|
| 1991 | n/a | ml->ml_name, |
|---|
| 1992 | n/a | meth); |
|---|
| 1993 | n/a | Py_DECREF(meth); |
|---|
| 1994 | n/a | if (x == -1) { |
|---|
| 1995 | n/a | Py_DECREF(result); |
|---|
| 1996 | n/a | return NULL; |
|---|
| 1997 | n/a | } |
|---|
| 1998 | n/a | } |
|---|
| 1999 | n/a | } |
|---|
| 2000 | n/a | |
|---|
| 2001 | n/a | if (type == &PyCSimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) { |
|---|
| 2002 | n/a | PyObject *swapped = CreateSwappedType(type, args, kwds, |
|---|
| 2003 | n/a | proto, fmt); |
|---|
| 2004 | n/a | StgDictObject *sw_dict; |
|---|
| 2005 | n/a | if (swapped == NULL) { |
|---|
| 2006 | n/a | Py_DECREF(result); |
|---|
| 2007 | n/a | return NULL; |
|---|
| 2008 | n/a | } |
|---|
| 2009 | n/a | sw_dict = PyType_stgdict(swapped); |
|---|
| 2010 | n/a | #ifdef WORDS_BIGENDIAN |
|---|
| 2011 | n/a | PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped); |
|---|
| 2012 | n/a | PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result); |
|---|
| 2013 | n/a | PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result); |
|---|
| 2014 | n/a | PyObject_SetAttrString(swapped, "__ctype_le__", swapped); |
|---|
| 2015 | n/a | /* We are creating the type for the OTHER endian */ |
|---|
| 2016 | n/a | sw_dict->format = _ctypes_alloc_format_string("<", stgdict->format+1); |
|---|
| 2017 | n/a | #else |
|---|
| 2018 | n/a | PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped); |
|---|
| 2019 | n/a | PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result); |
|---|
| 2020 | n/a | PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result); |
|---|
| 2021 | n/a | PyObject_SetAttrString(swapped, "__ctype_be__", swapped); |
|---|
| 2022 | n/a | /* We are creating the type for the OTHER endian */ |
|---|
| 2023 | n/a | sw_dict->format = _ctypes_alloc_format_string(">", stgdict->format+1); |
|---|
| 2024 | n/a | #endif |
|---|
| 2025 | n/a | Py_DECREF(swapped); |
|---|
| 2026 | n/a | if (PyErr_Occurred()) { |
|---|
| 2027 | n/a | Py_DECREF(result); |
|---|
| 2028 | n/a | return NULL; |
|---|
| 2029 | n/a | } |
|---|
| 2030 | n/a | }; |
|---|
| 2031 | n/a | |
|---|
| 2032 | n/a | return (PyObject *)result; |
|---|
| 2033 | n/a | } |
|---|
| 2034 | n/a | |
|---|
| 2035 | n/a | /* |
|---|
| 2036 | n/a | * This is a *class method*. |
|---|
| 2037 | n/a | * Convert a parameter into something that ConvParam can handle. |
|---|
| 2038 | n/a | */ |
|---|
| 2039 | n/a | static PyObject * |
|---|
| 2040 | n/a | PyCSimpleType_from_param(PyObject *type, PyObject *value) |
|---|
| 2041 | n/a | { |
|---|
| 2042 | n/a | StgDictObject *dict; |
|---|
| 2043 | n/a | const char *fmt; |
|---|
| 2044 | n/a | PyCArgObject *parg; |
|---|
| 2045 | n/a | struct fielddesc *fd; |
|---|
| 2046 | n/a | PyObject *as_parameter; |
|---|
| 2047 | n/a | int res; |
|---|
| 2048 | n/a | |
|---|
| 2049 | n/a | /* If the value is already an instance of the requested type, |
|---|
| 2050 | n/a | we can use it as is */ |
|---|
| 2051 | n/a | res = PyObject_IsInstance(value, type); |
|---|
| 2052 | n/a | if (res == -1) |
|---|
| 2053 | n/a | return NULL; |
|---|
| 2054 | n/a | if (res) { |
|---|
| 2055 | n/a | Py_INCREF(value); |
|---|
| 2056 | n/a | return value; |
|---|
| 2057 | n/a | } |
|---|
| 2058 | n/a | |
|---|
| 2059 | n/a | dict = PyType_stgdict(type); |
|---|
| 2060 | n/a | assert(dict); |
|---|
| 2061 | n/a | |
|---|
| 2062 | n/a | /* I think we can rely on this being a one-character string */ |
|---|
| 2063 | n/a | fmt = PyUnicode_AsUTF8(dict->proto); |
|---|
| 2064 | n/a | assert(fmt); |
|---|
| 2065 | n/a | |
|---|
| 2066 | n/a | fd = _ctypes_get_fielddesc(fmt); |
|---|
| 2067 | n/a | assert(fd); |
|---|
| 2068 | n/a | |
|---|
| 2069 | n/a | parg = PyCArgObject_new(); |
|---|
| 2070 | n/a | if (parg == NULL) |
|---|
| 2071 | n/a | return NULL; |
|---|
| 2072 | n/a | |
|---|
| 2073 | n/a | parg->tag = fmt[0]; |
|---|
| 2074 | n/a | parg->pffi_type = fd->pffi_type; |
|---|
| 2075 | n/a | parg->obj = fd->setfunc(&parg->value, value, 0); |
|---|
| 2076 | n/a | if (parg->obj) |
|---|
| 2077 | n/a | return (PyObject *)parg; |
|---|
| 2078 | n/a | PyErr_Clear(); |
|---|
| 2079 | n/a | Py_DECREF(parg); |
|---|
| 2080 | n/a | |
|---|
| 2081 | n/a | as_parameter = PyObject_GetAttrString(value, "_as_parameter_"); |
|---|
| 2082 | n/a | if (as_parameter) { |
|---|
| 2083 | n/a | if (Py_EnterRecursiveCall("while processing _as_parameter_")) { |
|---|
| 2084 | n/a | Py_DECREF(as_parameter); |
|---|
| 2085 | n/a | return NULL; |
|---|
| 2086 | n/a | } |
|---|
| 2087 | n/a | value = PyCSimpleType_from_param(type, as_parameter); |
|---|
| 2088 | n/a | Py_LeaveRecursiveCall(); |
|---|
| 2089 | n/a | Py_DECREF(as_parameter); |
|---|
| 2090 | n/a | return value; |
|---|
| 2091 | n/a | } |
|---|
| 2092 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2093 | n/a | "wrong type"); |
|---|
| 2094 | n/a | return NULL; |
|---|
| 2095 | n/a | } |
|---|
| 2096 | n/a | |
|---|
| 2097 | n/a | static PyMethodDef PyCSimpleType_methods[] = { |
|---|
| 2098 | n/a | { "from_param", PyCSimpleType_from_param, METH_O, from_param_doc }, |
|---|
| 2099 | n/a | { "from_address", CDataType_from_address, METH_O, from_address_doc }, |
|---|
| 2100 | n/a | { "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, }, |
|---|
| 2101 | n/a | { "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, }, |
|---|
| 2102 | n/a | { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc}, |
|---|
| 2103 | n/a | { NULL, NULL }, |
|---|
| 2104 | n/a | }; |
|---|
| 2105 | n/a | |
|---|
| 2106 | n/a | PyTypeObject PyCSimpleType_Type = { |
|---|
| 2107 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 2108 | n/a | "_ctypes.PyCSimpleType", /* tp_name */ |
|---|
| 2109 | n/a | 0, /* tp_basicsize */ |
|---|
| 2110 | n/a | 0, /* tp_itemsize */ |
|---|
| 2111 | n/a | 0, /* tp_dealloc */ |
|---|
| 2112 | n/a | 0, /* tp_print */ |
|---|
| 2113 | n/a | 0, /* tp_getattr */ |
|---|
| 2114 | n/a | 0, /* tp_setattr */ |
|---|
| 2115 | n/a | 0, /* tp_reserved */ |
|---|
| 2116 | n/a | 0, /* tp_repr */ |
|---|
| 2117 | n/a | 0, /* tp_as_number */ |
|---|
| 2118 | n/a | &CDataType_as_sequence, /* tp_as_sequence */ |
|---|
| 2119 | n/a | 0, /* tp_as_mapping */ |
|---|
| 2120 | n/a | 0, /* tp_hash */ |
|---|
| 2121 | n/a | 0, /* tp_call */ |
|---|
| 2122 | n/a | 0, /* tp_str */ |
|---|
| 2123 | n/a | 0, /* tp_getattro */ |
|---|
| 2124 | n/a | 0, /* tp_setattro */ |
|---|
| 2125 | n/a | 0, /* tp_as_buffer */ |
|---|
| 2126 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 2127 | n/a | "metatype for the PyCSimpleType Objects", /* tp_doc */ |
|---|
| 2128 | n/a | 0, /* tp_traverse */ |
|---|
| 2129 | n/a | 0, /* tp_clear */ |
|---|
| 2130 | n/a | 0, /* tp_richcompare */ |
|---|
| 2131 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 2132 | n/a | 0, /* tp_iter */ |
|---|
| 2133 | n/a | 0, /* tp_iternext */ |
|---|
| 2134 | n/a | PyCSimpleType_methods, /* tp_methods */ |
|---|
| 2135 | n/a | 0, /* tp_members */ |
|---|
| 2136 | n/a | 0, /* tp_getset */ |
|---|
| 2137 | n/a | 0, /* tp_base */ |
|---|
| 2138 | n/a | 0, /* tp_dict */ |
|---|
| 2139 | n/a | 0, /* tp_descr_get */ |
|---|
| 2140 | n/a | 0, /* tp_descr_set */ |
|---|
| 2141 | n/a | 0, /* tp_dictoffset */ |
|---|
| 2142 | n/a | 0, /* tp_init */ |
|---|
| 2143 | n/a | 0, /* tp_alloc */ |
|---|
| 2144 | n/a | PyCSimpleType_new, /* tp_new */ |
|---|
| 2145 | n/a | 0, /* tp_free */ |
|---|
| 2146 | n/a | }; |
|---|
| 2147 | n/a | |
|---|
| 2148 | n/a | /******************************************************************/ |
|---|
| 2149 | n/a | /* |
|---|
| 2150 | n/a | PyCFuncPtrType_Type |
|---|
| 2151 | n/a | */ |
|---|
| 2152 | n/a | |
|---|
| 2153 | n/a | static PyObject * |
|---|
| 2154 | n/a | converters_from_argtypes(PyObject *ob) |
|---|
| 2155 | n/a | { |
|---|
| 2156 | n/a | PyObject *converters; |
|---|
| 2157 | n/a | Py_ssize_t i; |
|---|
| 2158 | n/a | Py_ssize_t nArgs; |
|---|
| 2159 | n/a | |
|---|
| 2160 | n/a | ob = PySequence_Tuple(ob); /* new reference */ |
|---|
| 2161 | n/a | if (!ob) { |
|---|
| 2162 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2163 | n/a | "_argtypes_ must be a sequence of types"); |
|---|
| 2164 | n/a | return NULL; |
|---|
| 2165 | n/a | } |
|---|
| 2166 | n/a | |
|---|
| 2167 | n/a | nArgs = PyTuple_GET_SIZE(ob); |
|---|
| 2168 | n/a | converters = PyTuple_New(nArgs); |
|---|
| 2169 | n/a | if (!converters) { |
|---|
| 2170 | n/a | Py_DECREF(ob); |
|---|
| 2171 | n/a | return NULL; |
|---|
| 2172 | n/a | } |
|---|
| 2173 | n/a | |
|---|
| 2174 | n/a | /* I have to check if this is correct. Using c_char, which has a size |
|---|
| 2175 | n/a | of 1, will be assumed to be pushed as only one byte! |
|---|
| 2176 | n/a | Aren't these promoted to integers by the C compiler and pushed as 4 bytes? |
|---|
| 2177 | n/a | */ |
|---|
| 2178 | n/a | |
|---|
| 2179 | n/a | for (i = 0; i < nArgs; ++i) { |
|---|
| 2180 | n/a | PyObject *tp = PyTuple_GET_ITEM(ob, i); |
|---|
| 2181 | n/a | PyObject *cnv = PyObject_GetAttrString(tp, "from_param"); |
|---|
| 2182 | n/a | if (!cnv) |
|---|
| 2183 | n/a | goto argtypes_error_1; |
|---|
| 2184 | n/a | PyTuple_SET_ITEM(converters, i, cnv); |
|---|
| 2185 | n/a | } |
|---|
| 2186 | n/a | Py_DECREF(ob); |
|---|
| 2187 | n/a | return converters; |
|---|
| 2188 | n/a | |
|---|
| 2189 | n/a | argtypes_error_1: |
|---|
| 2190 | n/a | Py_XDECREF(converters); |
|---|
| 2191 | n/a | Py_DECREF(ob); |
|---|
| 2192 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2193 | n/a | "item %zd in _argtypes_ has no from_param method", |
|---|
| 2194 | n/a | i+1); |
|---|
| 2195 | n/a | return NULL; |
|---|
| 2196 | n/a | } |
|---|
| 2197 | n/a | |
|---|
| 2198 | n/a | static int |
|---|
| 2199 | n/a | make_funcptrtype_dict(StgDictObject *stgdict) |
|---|
| 2200 | n/a | { |
|---|
| 2201 | n/a | PyObject *ob; |
|---|
| 2202 | n/a | PyObject *converters = NULL; |
|---|
| 2203 | n/a | |
|---|
| 2204 | n/a | stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; |
|---|
| 2205 | n/a | stgdict->length = 1; |
|---|
| 2206 | n/a | stgdict->size = sizeof(void *); |
|---|
| 2207 | n/a | stgdict->setfunc = NULL; |
|---|
| 2208 | n/a | stgdict->getfunc = NULL; |
|---|
| 2209 | n/a | stgdict->ffi_type_pointer = ffi_type_pointer; |
|---|
| 2210 | n/a | |
|---|
| 2211 | n/a | ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_"); |
|---|
| 2212 | n/a | if (!ob || !PyLong_Check(ob)) { |
|---|
| 2213 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2214 | n/a | "class must define _flags_ which must be an integer"); |
|---|
| 2215 | n/a | return -1; |
|---|
| 2216 | n/a | } |
|---|
| 2217 | n/a | stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER; |
|---|
| 2218 | n/a | |
|---|
| 2219 | n/a | /* _argtypes_ is optional... */ |
|---|
| 2220 | n/a | ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_"); |
|---|
| 2221 | n/a | if (ob) { |
|---|
| 2222 | n/a | converters = converters_from_argtypes(ob); |
|---|
| 2223 | n/a | if (!converters) |
|---|
| 2224 | n/a | goto error; |
|---|
| 2225 | n/a | Py_INCREF(ob); |
|---|
| 2226 | n/a | stgdict->argtypes = ob; |
|---|
| 2227 | n/a | stgdict->converters = converters; |
|---|
| 2228 | n/a | } |
|---|
| 2229 | n/a | |
|---|
| 2230 | n/a | ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_"); |
|---|
| 2231 | n/a | if (ob) { |
|---|
| 2232 | n/a | if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) { |
|---|
| 2233 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2234 | n/a | "_restype_ must be a type, a callable, or None"); |
|---|
| 2235 | n/a | return -1; |
|---|
| 2236 | n/a | } |
|---|
| 2237 | n/a | Py_INCREF(ob); |
|---|
| 2238 | n/a | stgdict->restype = ob; |
|---|
| 2239 | n/a | stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_"); |
|---|
| 2240 | n/a | if (stgdict->checker == NULL) |
|---|
| 2241 | n/a | PyErr_Clear(); |
|---|
| 2242 | n/a | } |
|---|
| 2243 | n/a | /* XXX later, maybe. |
|---|
| 2244 | n/a | ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_"); |
|---|
| 2245 | n/a | if (ob) { |
|---|
| 2246 | n/a | if (!PyCallable_Check(ob)) { |
|---|
| 2247 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2248 | n/a | "_errcheck_ must be callable"); |
|---|
| 2249 | n/a | return -1; |
|---|
| 2250 | n/a | } |
|---|
| 2251 | n/a | Py_INCREF(ob); |
|---|
| 2252 | n/a | stgdict->errcheck = ob; |
|---|
| 2253 | n/a | } |
|---|
| 2254 | n/a | */ |
|---|
| 2255 | n/a | return 0; |
|---|
| 2256 | n/a | |
|---|
| 2257 | n/a | error: |
|---|
| 2258 | n/a | Py_XDECREF(converters); |
|---|
| 2259 | n/a | return -1; |
|---|
| 2260 | n/a | |
|---|
| 2261 | n/a | } |
|---|
| 2262 | n/a | |
|---|
| 2263 | n/a | static PyCArgObject * |
|---|
| 2264 | n/a | PyCFuncPtrType_paramfunc(CDataObject *self) |
|---|
| 2265 | n/a | { |
|---|
| 2266 | n/a | PyCArgObject *parg; |
|---|
| 2267 | n/a | |
|---|
| 2268 | n/a | parg = PyCArgObject_new(); |
|---|
| 2269 | n/a | if (parg == NULL) |
|---|
| 2270 | n/a | return NULL; |
|---|
| 2271 | n/a | |
|---|
| 2272 | n/a | parg->tag = 'P'; |
|---|
| 2273 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 2274 | n/a | Py_INCREF(self); |
|---|
| 2275 | n/a | parg->obj = (PyObject *)self; |
|---|
| 2276 | n/a | parg->value.p = *(void **)self->b_ptr; |
|---|
| 2277 | n/a | return parg; |
|---|
| 2278 | n/a | } |
|---|
| 2279 | n/a | |
|---|
| 2280 | n/a | static PyObject * |
|---|
| 2281 | n/a | PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 2282 | n/a | { |
|---|
| 2283 | n/a | PyTypeObject *result; |
|---|
| 2284 | n/a | StgDictObject *stgdict; |
|---|
| 2285 | n/a | |
|---|
| 2286 | n/a | stgdict = (StgDictObject *)PyObject_CallObject( |
|---|
| 2287 | n/a | (PyObject *)&PyCStgDict_Type, NULL); |
|---|
| 2288 | n/a | if (!stgdict) |
|---|
| 2289 | n/a | return NULL; |
|---|
| 2290 | n/a | |
|---|
| 2291 | n/a | stgdict->paramfunc = PyCFuncPtrType_paramfunc; |
|---|
| 2292 | n/a | /* We do NOT expose the function signature in the format string. It |
|---|
| 2293 | n/a | is impossible, generally, because the only requirement for the |
|---|
| 2294 | n/a | argtypes items is that they have a .from_param method - we do not |
|---|
| 2295 | n/a | know the types of the arguments (although, in practice, most |
|---|
| 2296 | n/a | argtypes would be a ctypes type). |
|---|
| 2297 | n/a | */ |
|---|
| 2298 | n/a | stgdict->format = _ctypes_alloc_format_string(NULL, "X{}"); |
|---|
| 2299 | n/a | if (stgdict->format == NULL) { |
|---|
| 2300 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 2301 | n/a | return NULL; |
|---|
| 2302 | n/a | } |
|---|
| 2303 | n/a | stgdict->flags |= TYPEFLAG_ISPOINTER; |
|---|
| 2304 | n/a | |
|---|
| 2305 | n/a | /* create the new instance (which is a class, |
|---|
| 2306 | n/a | since we are a metatype!) */ |
|---|
| 2307 | n/a | result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds); |
|---|
| 2308 | n/a | if (result == NULL) { |
|---|
| 2309 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 2310 | n/a | return NULL; |
|---|
| 2311 | n/a | } |
|---|
| 2312 | n/a | |
|---|
| 2313 | n/a | /* replace the class dict by our updated storage dict */ |
|---|
| 2314 | n/a | if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) { |
|---|
| 2315 | n/a | Py_DECREF(result); |
|---|
| 2316 | n/a | Py_DECREF((PyObject *)stgdict); |
|---|
| 2317 | n/a | return NULL; |
|---|
| 2318 | n/a | } |
|---|
| 2319 | n/a | Py_SETREF(result->tp_dict, (PyObject *)stgdict); |
|---|
| 2320 | n/a | |
|---|
| 2321 | n/a | if (-1 == make_funcptrtype_dict(stgdict)) { |
|---|
| 2322 | n/a | Py_DECREF(result); |
|---|
| 2323 | n/a | return NULL; |
|---|
| 2324 | n/a | } |
|---|
| 2325 | n/a | |
|---|
| 2326 | n/a | return (PyObject *)result; |
|---|
| 2327 | n/a | } |
|---|
| 2328 | n/a | |
|---|
| 2329 | n/a | PyTypeObject PyCFuncPtrType_Type = { |
|---|
| 2330 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 2331 | n/a | "_ctypes.PyCFuncPtrType", /* tp_name */ |
|---|
| 2332 | n/a | 0, /* tp_basicsize */ |
|---|
| 2333 | n/a | 0, /* tp_itemsize */ |
|---|
| 2334 | n/a | 0, /* tp_dealloc */ |
|---|
| 2335 | n/a | 0, /* tp_print */ |
|---|
| 2336 | n/a | 0, /* tp_getattr */ |
|---|
| 2337 | n/a | 0, /* tp_setattr */ |
|---|
| 2338 | n/a | 0, /* tp_reserved */ |
|---|
| 2339 | n/a | 0, /* tp_repr */ |
|---|
| 2340 | n/a | 0, /* tp_as_number */ |
|---|
| 2341 | n/a | &CDataType_as_sequence, /* tp_as_sequence */ |
|---|
| 2342 | n/a | 0, /* tp_as_mapping */ |
|---|
| 2343 | n/a | 0, /* tp_hash */ |
|---|
| 2344 | n/a | 0, /* tp_call */ |
|---|
| 2345 | n/a | 0, /* tp_str */ |
|---|
| 2346 | n/a | 0, /* tp_getattro */ |
|---|
| 2347 | n/a | 0, /* tp_setattro */ |
|---|
| 2348 | n/a | 0, /* tp_as_buffer */ |
|---|
| 2349 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
|---|
| 2350 | n/a | "metatype for C function pointers", /* tp_doc */ |
|---|
| 2351 | n/a | (traverseproc)CDataType_traverse, /* tp_traverse */ |
|---|
| 2352 | n/a | (inquiry)CDataType_clear, /* tp_clear */ |
|---|
| 2353 | n/a | 0, /* tp_richcompare */ |
|---|
| 2354 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 2355 | n/a | 0, /* tp_iter */ |
|---|
| 2356 | n/a | 0, /* tp_iternext */ |
|---|
| 2357 | n/a | CDataType_methods, /* tp_methods */ |
|---|
| 2358 | n/a | 0, /* tp_members */ |
|---|
| 2359 | n/a | 0, /* tp_getset */ |
|---|
| 2360 | n/a | 0, /* tp_base */ |
|---|
| 2361 | n/a | 0, /* tp_dict */ |
|---|
| 2362 | n/a | 0, /* tp_descr_get */ |
|---|
| 2363 | n/a | 0, /* tp_descr_set */ |
|---|
| 2364 | n/a | 0, /* tp_dictoffset */ |
|---|
| 2365 | n/a | 0, /* tp_init */ |
|---|
| 2366 | n/a | 0, /* tp_alloc */ |
|---|
| 2367 | n/a | PyCFuncPtrType_new, /* tp_new */ |
|---|
| 2368 | n/a | 0, /* tp_free */ |
|---|
| 2369 | n/a | }; |
|---|
| 2370 | n/a | |
|---|
| 2371 | n/a | |
|---|
| 2372 | n/a | /***************************************************************** |
|---|
| 2373 | n/a | * Code to keep needed objects alive |
|---|
| 2374 | n/a | */ |
|---|
| 2375 | n/a | |
|---|
| 2376 | n/a | static CDataObject * |
|---|
| 2377 | n/a | PyCData_GetContainer(CDataObject *self) |
|---|
| 2378 | n/a | { |
|---|
| 2379 | n/a | while (self->b_base) |
|---|
| 2380 | n/a | self = self->b_base; |
|---|
| 2381 | n/a | if (self->b_objects == NULL) { |
|---|
| 2382 | n/a | if (self->b_length) { |
|---|
| 2383 | n/a | self->b_objects = PyDict_New(); |
|---|
| 2384 | n/a | if (self->b_objects == NULL) |
|---|
| 2385 | n/a | return NULL; |
|---|
| 2386 | n/a | } else { |
|---|
| 2387 | n/a | Py_INCREF(Py_None); |
|---|
| 2388 | n/a | self->b_objects = Py_None; |
|---|
| 2389 | n/a | } |
|---|
| 2390 | n/a | } |
|---|
| 2391 | n/a | return self; |
|---|
| 2392 | n/a | } |
|---|
| 2393 | n/a | |
|---|
| 2394 | n/a | static PyObject * |
|---|
| 2395 | n/a | GetKeepedObjects(CDataObject *target) |
|---|
| 2396 | n/a | { |
|---|
| 2397 | n/a | CDataObject *container; |
|---|
| 2398 | n/a | container = PyCData_GetContainer(target); |
|---|
| 2399 | n/a | if (container == NULL) |
|---|
| 2400 | n/a | return NULL; |
|---|
| 2401 | n/a | return container->b_objects; |
|---|
| 2402 | n/a | } |
|---|
| 2403 | n/a | |
|---|
| 2404 | n/a | static PyObject * |
|---|
| 2405 | n/a | unique_key(CDataObject *target, Py_ssize_t index) |
|---|
| 2406 | n/a | { |
|---|
| 2407 | n/a | char string[256]; |
|---|
| 2408 | n/a | char *cp = string; |
|---|
| 2409 | n/a | size_t bytes_left; |
|---|
| 2410 | n/a | |
|---|
| 2411 | n/a | Py_BUILD_ASSERT(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2); |
|---|
| 2412 | n/a | cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int)); |
|---|
| 2413 | n/a | while (target->b_base) { |
|---|
| 2414 | n/a | bytes_left = sizeof(string) - (cp - string) - 1; |
|---|
| 2415 | n/a | /* Hex format needs 2 characters per byte */ |
|---|
| 2416 | n/a | if (bytes_left < sizeof(Py_ssize_t) * 2) { |
|---|
| 2417 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 2418 | n/a | "ctypes object structure too deep"); |
|---|
| 2419 | n/a | return NULL; |
|---|
| 2420 | n/a | } |
|---|
| 2421 | n/a | cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, int)); |
|---|
| 2422 | n/a | target = target->b_base; |
|---|
| 2423 | n/a | } |
|---|
| 2424 | n/a | return PyUnicode_FromStringAndSize(string, cp-string); |
|---|
| 2425 | n/a | } |
|---|
| 2426 | n/a | |
|---|
| 2427 | n/a | /* |
|---|
| 2428 | n/a | * Keep a reference to 'keep' in the 'target', at index 'index'. |
|---|
| 2429 | n/a | * |
|---|
| 2430 | n/a | * If 'keep' is None, do nothing. |
|---|
| 2431 | n/a | * |
|---|
| 2432 | n/a | * Otherwise create a dictionary (if it does not yet exist) id the root |
|---|
| 2433 | n/a | * objects 'b_objects' item, which will store the 'keep' object under a unique |
|---|
| 2434 | n/a | * key. |
|---|
| 2435 | n/a | * |
|---|
| 2436 | n/a | * The unique_key helper travels the target's b_base pointer down to the root, |
|---|
| 2437 | n/a | * building a string containing hex-formatted indexes found during traversal, |
|---|
| 2438 | n/a | * separated by colons. |
|---|
| 2439 | n/a | * |
|---|
| 2440 | n/a | * The index tuple is used as a key into the root object's b_objects dict. |
|---|
| 2441 | n/a | * |
|---|
| 2442 | n/a | * Note: This function steals a refcount of the third argument, even if it |
|---|
| 2443 | n/a | * fails! |
|---|
| 2444 | n/a | */ |
|---|
| 2445 | n/a | static int |
|---|
| 2446 | n/a | KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep) |
|---|
| 2447 | n/a | { |
|---|
| 2448 | n/a | int result; |
|---|
| 2449 | n/a | CDataObject *ob; |
|---|
| 2450 | n/a | PyObject *key; |
|---|
| 2451 | n/a | |
|---|
| 2452 | n/a | /* Optimization: no need to store None */ |
|---|
| 2453 | n/a | if (keep == Py_None) { |
|---|
| 2454 | n/a | Py_DECREF(Py_None); |
|---|
| 2455 | n/a | return 0; |
|---|
| 2456 | n/a | } |
|---|
| 2457 | n/a | ob = PyCData_GetContainer(target); |
|---|
| 2458 | n/a | if (ob == NULL) { |
|---|
| 2459 | n/a | Py_DECREF(keep); |
|---|
| 2460 | n/a | return -1; |
|---|
| 2461 | n/a | } |
|---|
| 2462 | n/a | if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) { |
|---|
| 2463 | n/a | Py_XSETREF(ob->b_objects, keep); /* refcount consumed */ |
|---|
| 2464 | n/a | return 0; |
|---|
| 2465 | n/a | } |
|---|
| 2466 | n/a | key = unique_key(target, index); |
|---|
| 2467 | n/a | if (key == NULL) { |
|---|
| 2468 | n/a | Py_DECREF(keep); |
|---|
| 2469 | n/a | return -1; |
|---|
| 2470 | n/a | } |
|---|
| 2471 | n/a | result = PyDict_SetItem(ob->b_objects, key, keep); |
|---|
| 2472 | n/a | Py_DECREF(key); |
|---|
| 2473 | n/a | Py_DECREF(keep); |
|---|
| 2474 | n/a | return result; |
|---|
| 2475 | n/a | } |
|---|
| 2476 | n/a | |
|---|
| 2477 | n/a | /******************************************************************/ |
|---|
| 2478 | n/a | /* |
|---|
| 2479 | n/a | PyCData_Type |
|---|
| 2480 | n/a | */ |
|---|
| 2481 | n/a | static int |
|---|
| 2482 | n/a | PyCData_traverse(CDataObject *self, visitproc visit, void *arg) |
|---|
| 2483 | n/a | { |
|---|
| 2484 | n/a | Py_VISIT(self->b_objects); |
|---|
| 2485 | n/a | Py_VISIT((PyObject *)self->b_base); |
|---|
| 2486 | n/a | return 0; |
|---|
| 2487 | n/a | } |
|---|
| 2488 | n/a | |
|---|
| 2489 | n/a | static int |
|---|
| 2490 | n/a | PyCData_clear(CDataObject *self) |
|---|
| 2491 | n/a | { |
|---|
| 2492 | n/a | Py_CLEAR(self->b_objects); |
|---|
| 2493 | n/a | if ((self->b_needsfree) |
|---|
| 2494 | n/a | && _CDataObject_HasExternalBuffer(self)) |
|---|
| 2495 | n/a | PyMem_Free(self->b_ptr); |
|---|
| 2496 | n/a | self->b_ptr = NULL; |
|---|
| 2497 | n/a | Py_CLEAR(self->b_base); |
|---|
| 2498 | n/a | return 0; |
|---|
| 2499 | n/a | } |
|---|
| 2500 | n/a | |
|---|
| 2501 | n/a | static void |
|---|
| 2502 | n/a | PyCData_dealloc(PyObject *self) |
|---|
| 2503 | n/a | { |
|---|
| 2504 | n/a | PyCData_clear((CDataObject *)self); |
|---|
| 2505 | n/a | Py_TYPE(self)->tp_free(self); |
|---|
| 2506 | n/a | } |
|---|
| 2507 | n/a | |
|---|
| 2508 | n/a | static PyMemberDef PyCData_members[] = { |
|---|
| 2509 | n/a | { "_b_base_", T_OBJECT, |
|---|
| 2510 | n/a | offsetof(CDataObject, b_base), READONLY, |
|---|
| 2511 | n/a | "the base object" }, |
|---|
| 2512 | n/a | { "_b_needsfree_", T_INT, |
|---|
| 2513 | n/a | offsetof(CDataObject, b_needsfree), READONLY, |
|---|
| 2514 | n/a | "whether the object owns the memory or not" }, |
|---|
| 2515 | n/a | { "_objects", T_OBJECT, |
|---|
| 2516 | n/a | offsetof(CDataObject, b_objects), READONLY, |
|---|
| 2517 | n/a | "internal objects tree (NEVER CHANGE THIS OBJECT!)"}, |
|---|
| 2518 | n/a | { NULL }, |
|---|
| 2519 | n/a | }; |
|---|
| 2520 | n/a | |
|---|
| 2521 | n/a | static int PyCData_NewGetBuffer(PyObject *myself, Py_buffer *view, int flags) |
|---|
| 2522 | n/a | { |
|---|
| 2523 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 2524 | n/a | StgDictObject *dict = PyObject_stgdict(myself); |
|---|
| 2525 | n/a | Py_ssize_t i; |
|---|
| 2526 | n/a | |
|---|
| 2527 | n/a | if (view == NULL) return 0; |
|---|
| 2528 | n/a | |
|---|
| 2529 | n/a | view->buf = self->b_ptr; |
|---|
| 2530 | n/a | view->obj = myself; |
|---|
| 2531 | n/a | Py_INCREF(myself); |
|---|
| 2532 | n/a | view->len = self->b_size; |
|---|
| 2533 | n/a | view->readonly = 0; |
|---|
| 2534 | n/a | /* use default format character if not set */ |
|---|
| 2535 | n/a | view->format = dict->format ? dict->format : "B"; |
|---|
| 2536 | n/a | view->ndim = dict->ndim; |
|---|
| 2537 | n/a | view->shape = dict->shape; |
|---|
| 2538 | n/a | view->itemsize = self->b_size; |
|---|
| 2539 | n/a | if (view->itemsize) { |
|---|
| 2540 | n/a | for (i = 0; i < view->ndim; ++i) { |
|---|
| 2541 | n/a | view->itemsize /= dict->shape[i]; |
|---|
| 2542 | n/a | } |
|---|
| 2543 | n/a | } |
|---|
| 2544 | n/a | view->strides = NULL; |
|---|
| 2545 | n/a | view->suboffsets = NULL; |
|---|
| 2546 | n/a | view->internal = NULL; |
|---|
| 2547 | n/a | return 0; |
|---|
| 2548 | n/a | } |
|---|
| 2549 | n/a | |
|---|
| 2550 | n/a | static PyBufferProcs PyCData_as_buffer = { |
|---|
| 2551 | n/a | PyCData_NewGetBuffer, |
|---|
| 2552 | n/a | NULL, |
|---|
| 2553 | n/a | }; |
|---|
| 2554 | n/a | |
|---|
| 2555 | n/a | /* |
|---|
| 2556 | n/a | * CData objects are mutable, so they cannot be hashable! |
|---|
| 2557 | n/a | */ |
|---|
| 2558 | n/a | static Py_hash_t |
|---|
| 2559 | n/a | PyCData_nohash(PyObject *self) |
|---|
| 2560 | n/a | { |
|---|
| 2561 | n/a | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
|---|
| 2562 | n/a | return -1; |
|---|
| 2563 | n/a | } |
|---|
| 2564 | n/a | |
|---|
| 2565 | n/a | static PyObject * |
|---|
| 2566 | n/a | PyCData_reduce(PyObject *myself, PyObject *args) |
|---|
| 2567 | n/a | { |
|---|
| 2568 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 2569 | n/a | |
|---|
| 2570 | n/a | if (PyObject_stgdict(myself)->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTER)) { |
|---|
| 2571 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 2572 | n/a | "ctypes objects containing pointers cannot be pickled"); |
|---|
| 2573 | n/a | return NULL; |
|---|
| 2574 | n/a | } |
|---|
| 2575 | n/a | return Py_BuildValue("O(O(NN))", |
|---|
| 2576 | n/a | _unpickle, |
|---|
| 2577 | n/a | Py_TYPE(myself), |
|---|
| 2578 | n/a | PyObject_GetAttrString(myself, "__dict__"), |
|---|
| 2579 | n/a | PyBytes_FromStringAndSize(self->b_ptr, self->b_size)); |
|---|
| 2580 | n/a | } |
|---|
| 2581 | n/a | |
|---|
| 2582 | n/a | static PyObject * |
|---|
| 2583 | n/a | PyCData_setstate(PyObject *myself, PyObject *args) |
|---|
| 2584 | n/a | { |
|---|
| 2585 | n/a | void *data; |
|---|
| 2586 | n/a | Py_ssize_t len; |
|---|
| 2587 | n/a | int res; |
|---|
| 2588 | n/a | PyObject *dict, *mydict; |
|---|
| 2589 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 2590 | n/a | if (!PyArg_ParseTuple(args, "Os#", &dict, &data, &len)) |
|---|
| 2591 | n/a | return NULL; |
|---|
| 2592 | n/a | if (len > self->b_size) |
|---|
| 2593 | n/a | len = self->b_size; |
|---|
| 2594 | n/a | memmove(self->b_ptr, data, len); |
|---|
| 2595 | n/a | mydict = PyObject_GetAttrString(myself, "__dict__"); |
|---|
| 2596 | n/a | res = PyDict_Update(mydict, dict); |
|---|
| 2597 | n/a | Py_DECREF(mydict); |
|---|
| 2598 | n/a | if (res == -1) |
|---|
| 2599 | n/a | return NULL; |
|---|
| 2600 | n/a | Py_RETURN_NONE; |
|---|
| 2601 | n/a | } |
|---|
| 2602 | n/a | |
|---|
| 2603 | n/a | /* |
|---|
| 2604 | n/a | * default __ctypes_from_outparam__ method returns self. |
|---|
| 2605 | n/a | */ |
|---|
| 2606 | n/a | static PyObject * |
|---|
| 2607 | n/a | PyCData_from_outparam(PyObject *self, PyObject *args) |
|---|
| 2608 | n/a | { |
|---|
| 2609 | n/a | Py_INCREF(self); |
|---|
| 2610 | n/a | return self; |
|---|
| 2611 | n/a | } |
|---|
| 2612 | n/a | |
|---|
| 2613 | n/a | static PyMethodDef PyCData_methods[] = { |
|---|
| 2614 | n/a | { "__ctypes_from_outparam__", PyCData_from_outparam, METH_NOARGS, }, |
|---|
| 2615 | n/a | { "__reduce__", PyCData_reduce, METH_NOARGS, }, |
|---|
| 2616 | n/a | { "__setstate__", PyCData_setstate, METH_VARARGS, }, |
|---|
| 2617 | n/a | { NULL, NULL }, |
|---|
| 2618 | n/a | }; |
|---|
| 2619 | n/a | |
|---|
| 2620 | n/a | PyTypeObject PyCData_Type = { |
|---|
| 2621 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 2622 | n/a | "_ctypes._CData", |
|---|
| 2623 | n/a | sizeof(CDataObject), /* tp_basicsize */ |
|---|
| 2624 | n/a | 0, /* tp_itemsize */ |
|---|
| 2625 | n/a | PyCData_dealloc, /* tp_dealloc */ |
|---|
| 2626 | n/a | 0, /* tp_print */ |
|---|
| 2627 | n/a | 0, /* tp_getattr */ |
|---|
| 2628 | n/a | 0, /* tp_setattr */ |
|---|
| 2629 | n/a | 0, /* tp_reserved */ |
|---|
| 2630 | n/a | 0, /* tp_repr */ |
|---|
| 2631 | n/a | 0, /* tp_as_number */ |
|---|
| 2632 | n/a | 0, /* tp_as_sequence */ |
|---|
| 2633 | n/a | 0, /* tp_as_mapping */ |
|---|
| 2634 | n/a | PyCData_nohash, /* tp_hash */ |
|---|
| 2635 | n/a | 0, /* tp_call */ |
|---|
| 2636 | n/a | 0, /* tp_str */ |
|---|
| 2637 | n/a | 0, /* tp_getattro */ |
|---|
| 2638 | n/a | 0, /* tp_setattro */ |
|---|
| 2639 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 2640 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 2641 | n/a | "XXX to be provided", /* tp_doc */ |
|---|
| 2642 | n/a | (traverseproc)PyCData_traverse, /* tp_traverse */ |
|---|
| 2643 | n/a | (inquiry)PyCData_clear, /* tp_clear */ |
|---|
| 2644 | n/a | 0, /* tp_richcompare */ |
|---|
| 2645 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 2646 | n/a | 0, /* tp_iter */ |
|---|
| 2647 | n/a | 0, /* tp_iternext */ |
|---|
| 2648 | n/a | PyCData_methods, /* tp_methods */ |
|---|
| 2649 | n/a | PyCData_members, /* tp_members */ |
|---|
| 2650 | n/a | 0, /* tp_getset */ |
|---|
| 2651 | n/a | 0, /* tp_base */ |
|---|
| 2652 | n/a | 0, /* tp_dict */ |
|---|
| 2653 | n/a | 0, /* tp_descr_get */ |
|---|
| 2654 | n/a | 0, /* tp_descr_set */ |
|---|
| 2655 | n/a | 0, /* tp_dictoffset */ |
|---|
| 2656 | n/a | 0, /* tp_init */ |
|---|
| 2657 | n/a | 0, /* tp_alloc */ |
|---|
| 2658 | n/a | 0, /* tp_new */ |
|---|
| 2659 | n/a | 0, /* tp_free */ |
|---|
| 2660 | n/a | }; |
|---|
| 2661 | n/a | |
|---|
| 2662 | n/a | static int PyCData_MallocBuffer(CDataObject *obj, StgDictObject *dict) |
|---|
| 2663 | n/a | { |
|---|
| 2664 | n/a | if ((size_t)dict->size <= sizeof(obj->b_value)) { |
|---|
| 2665 | n/a | /* No need to call malloc, can use the default buffer */ |
|---|
| 2666 | n/a | obj->b_ptr = (char *)&obj->b_value; |
|---|
| 2667 | n/a | /* The b_needsfree flag does not mean that we actually did |
|---|
| 2668 | n/a | call PyMem_Malloc to allocate the memory block; instead it |
|---|
| 2669 | n/a | means we are the *owner* of the memory and are responsible |
|---|
| 2670 | n/a | for freeing resources associated with the memory. This is |
|---|
| 2671 | n/a | also the reason that b_needsfree is exposed to Python. |
|---|
| 2672 | n/a | */ |
|---|
| 2673 | n/a | obj->b_needsfree = 1; |
|---|
| 2674 | n/a | } else { |
|---|
| 2675 | n/a | /* In python 2.4, and ctypes 0.9.6, the malloc call took about |
|---|
| 2676 | n/a | 33% of the creation time for c_int(). |
|---|
| 2677 | n/a | */ |
|---|
| 2678 | n/a | obj->b_ptr = (char *)PyMem_Malloc(dict->size); |
|---|
| 2679 | n/a | if (obj->b_ptr == NULL) { |
|---|
| 2680 | n/a | PyErr_NoMemory(); |
|---|
| 2681 | n/a | return -1; |
|---|
| 2682 | n/a | } |
|---|
| 2683 | n/a | obj->b_needsfree = 1; |
|---|
| 2684 | n/a | memset(obj->b_ptr, 0, dict->size); |
|---|
| 2685 | n/a | } |
|---|
| 2686 | n/a | obj->b_size = dict->size; |
|---|
| 2687 | n/a | return 0; |
|---|
| 2688 | n/a | } |
|---|
| 2689 | n/a | |
|---|
| 2690 | n/a | PyObject * |
|---|
| 2691 | n/a | PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr) |
|---|
| 2692 | n/a | { |
|---|
| 2693 | n/a | CDataObject *cmem; |
|---|
| 2694 | n/a | StgDictObject *dict; |
|---|
| 2695 | n/a | |
|---|
| 2696 | n/a | assert(PyType_Check(type)); |
|---|
| 2697 | n/a | dict = PyType_stgdict(type); |
|---|
| 2698 | n/a | if (!dict) { |
|---|
| 2699 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2700 | n/a | "abstract class"); |
|---|
| 2701 | n/a | return NULL; |
|---|
| 2702 | n/a | } |
|---|
| 2703 | n/a | dict->flags |= DICTFLAG_FINAL; |
|---|
| 2704 | n/a | cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0); |
|---|
| 2705 | n/a | if (cmem == NULL) |
|---|
| 2706 | n/a | return NULL; |
|---|
| 2707 | n/a | assert(CDataObject_Check(cmem)); |
|---|
| 2708 | n/a | |
|---|
| 2709 | n/a | cmem->b_length = dict->length; |
|---|
| 2710 | n/a | cmem->b_size = dict->size; |
|---|
| 2711 | n/a | if (base) { /* use base's buffer */ |
|---|
| 2712 | n/a | assert(CDataObject_Check(base)); |
|---|
| 2713 | n/a | cmem->b_ptr = adr; |
|---|
| 2714 | n/a | cmem->b_needsfree = 0; |
|---|
| 2715 | n/a | Py_INCREF(base); |
|---|
| 2716 | n/a | cmem->b_base = (CDataObject *)base; |
|---|
| 2717 | n/a | cmem->b_index = index; |
|---|
| 2718 | n/a | } else { /* copy contents of adr */ |
|---|
| 2719 | n/a | if (-1 == PyCData_MallocBuffer(cmem, dict)) { |
|---|
| 2720 | n/a | Py_DECREF(cmem); |
|---|
| 2721 | n/a | return NULL; |
|---|
| 2722 | n/a | } |
|---|
| 2723 | n/a | memcpy(cmem->b_ptr, adr, dict->size); |
|---|
| 2724 | n/a | cmem->b_index = index; |
|---|
| 2725 | n/a | } |
|---|
| 2726 | n/a | return (PyObject *)cmem; |
|---|
| 2727 | n/a | } |
|---|
| 2728 | n/a | |
|---|
| 2729 | n/a | /* |
|---|
| 2730 | n/a | Box a memory block into a CData instance. |
|---|
| 2731 | n/a | */ |
|---|
| 2732 | n/a | PyObject * |
|---|
| 2733 | n/a | PyCData_AtAddress(PyObject *type, void *buf) |
|---|
| 2734 | n/a | { |
|---|
| 2735 | n/a | CDataObject *pd; |
|---|
| 2736 | n/a | StgDictObject *dict; |
|---|
| 2737 | n/a | |
|---|
| 2738 | n/a | assert(PyType_Check(type)); |
|---|
| 2739 | n/a | dict = PyType_stgdict(type); |
|---|
| 2740 | n/a | if (!dict) { |
|---|
| 2741 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2742 | n/a | "abstract class"); |
|---|
| 2743 | n/a | return NULL; |
|---|
| 2744 | n/a | } |
|---|
| 2745 | n/a | dict->flags |= DICTFLAG_FINAL; |
|---|
| 2746 | n/a | |
|---|
| 2747 | n/a | pd = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0); |
|---|
| 2748 | n/a | if (!pd) |
|---|
| 2749 | n/a | return NULL; |
|---|
| 2750 | n/a | assert(CDataObject_Check(pd)); |
|---|
| 2751 | n/a | pd->b_ptr = (char *)buf; |
|---|
| 2752 | n/a | pd->b_length = dict->length; |
|---|
| 2753 | n/a | pd->b_size = dict->size; |
|---|
| 2754 | n/a | return (PyObject *)pd; |
|---|
| 2755 | n/a | } |
|---|
| 2756 | n/a | |
|---|
| 2757 | n/a | /* |
|---|
| 2758 | n/a | This function returns TRUE for c_int, c_void_p, and these kind of |
|---|
| 2759 | n/a | classes. FALSE otherwise FALSE also for subclasses of c_int and |
|---|
| 2760 | n/a | such. |
|---|
| 2761 | n/a | */ |
|---|
| 2762 | n/a | int _ctypes_simple_instance(PyObject *obj) |
|---|
| 2763 | n/a | { |
|---|
| 2764 | n/a | PyTypeObject *type = (PyTypeObject *)obj; |
|---|
| 2765 | n/a | |
|---|
| 2766 | n/a | if (PyCSimpleTypeObject_Check(type)) |
|---|
| 2767 | n/a | return type->tp_base != &Simple_Type; |
|---|
| 2768 | n/a | return 0; |
|---|
| 2769 | n/a | } |
|---|
| 2770 | n/a | |
|---|
| 2771 | n/a | PyObject * |
|---|
| 2772 | n/a | PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src, |
|---|
| 2773 | n/a | Py_ssize_t index, Py_ssize_t size, char *adr) |
|---|
| 2774 | n/a | { |
|---|
| 2775 | n/a | StgDictObject *dict; |
|---|
| 2776 | n/a | if (getfunc) |
|---|
| 2777 | n/a | return getfunc(adr, size); |
|---|
| 2778 | n/a | assert(type); |
|---|
| 2779 | n/a | dict = PyType_stgdict(type); |
|---|
| 2780 | n/a | if (dict && dict->getfunc && !_ctypes_simple_instance(type)) |
|---|
| 2781 | n/a | return dict->getfunc(adr, size); |
|---|
| 2782 | n/a | return PyCData_FromBaseObj(type, src, index, adr); |
|---|
| 2783 | n/a | } |
|---|
| 2784 | n/a | |
|---|
| 2785 | n/a | /* |
|---|
| 2786 | n/a | Helper function for PyCData_set below. |
|---|
| 2787 | n/a | */ |
|---|
| 2788 | n/a | static PyObject * |
|---|
| 2789 | n/a | _PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
|---|
| 2790 | n/a | Py_ssize_t size, char *ptr) |
|---|
| 2791 | n/a | { |
|---|
| 2792 | n/a | CDataObject *src; |
|---|
| 2793 | n/a | int err; |
|---|
| 2794 | n/a | |
|---|
| 2795 | n/a | if (setfunc) |
|---|
| 2796 | n/a | return setfunc(ptr, value, size); |
|---|
| 2797 | n/a | |
|---|
| 2798 | n/a | if (!CDataObject_Check(value)) { |
|---|
| 2799 | n/a | StgDictObject *dict = PyType_stgdict(type); |
|---|
| 2800 | n/a | if (dict && dict->setfunc) |
|---|
| 2801 | n/a | return dict->setfunc(ptr, value, size); |
|---|
| 2802 | n/a | /* |
|---|
| 2803 | n/a | If value is a tuple, we try to call the type with the tuple |
|---|
| 2804 | n/a | and use the result! |
|---|
| 2805 | n/a | */ |
|---|
| 2806 | n/a | assert(PyType_Check(type)); |
|---|
| 2807 | n/a | if (PyTuple_Check(value)) { |
|---|
| 2808 | n/a | PyObject *ob; |
|---|
| 2809 | n/a | PyObject *result; |
|---|
| 2810 | n/a | ob = PyObject_CallObject(type, value); |
|---|
| 2811 | n/a | if (ob == NULL) { |
|---|
| 2812 | n/a | _ctypes_extend_error(PyExc_RuntimeError, "(%s) ", |
|---|
| 2813 | n/a | ((PyTypeObject *)type)->tp_name); |
|---|
| 2814 | n/a | return NULL; |
|---|
| 2815 | n/a | } |
|---|
| 2816 | n/a | result = _PyCData_set(dst, type, setfunc, ob, |
|---|
| 2817 | n/a | size, ptr); |
|---|
| 2818 | n/a | Py_DECREF(ob); |
|---|
| 2819 | n/a | return result; |
|---|
| 2820 | n/a | } else if (value == Py_None && PyCPointerTypeObject_Check(type)) { |
|---|
| 2821 | n/a | *(void **)ptr = NULL; |
|---|
| 2822 | n/a | Py_RETURN_NONE; |
|---|
| 2823 | n/a | } else { |
|---|
| 2824 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2825 | n/a | "expected %s instance, got %s", |
|---|
| 2826 | n/a | ((PyTypeObject *)type)->tp_name, |
|---|
| 2827 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 2828 | n/a | return NULL; |
|---|
| 2829 | n/a | } |
|---|
| 2830 | n/a | } |
|---|
| 2831 | n/a | src = (CDataObject *)value; |
|---|
| 2832 | n/a | |
|---|
| 2833 | n/a | err = PyObject_IsInstance(value, type); |
|---|
| 2834 | n/a | if (err == -1) |
|---|
| 2835 | n/a | return NULL; |
|---|
| 2836 | n/a | if (err) { |
|---|
| 2837 | n/a | memcpy(ptr, |
|---|
| 2838 | n/a | src->b_ptr, |
|---|
| 2839 | n/a | size); |
|---|
| 2840 | n/a | |
|---|
| 2841 | n/a | if (PyCPointerTypeObject_Check(type)) { |
|---|
| 2842 | n/a | /* XXX */ |
|---|
| 2843 | n/a | } |
|---|
| 2844 | n/a | |
|---|
| 2845 | n/a | value = GetKeepedObjects(src); |
|---|
| 2846 | n/a | if (value == NULL) |
|---|
| 2847 | n/a | return NULL; |
|---|
| 2848 | n/a | |
|---|
| 2849 | n/a | Py_INCREF(value); |
|---|
| 2850 | n/a | return value; |
|---|
| 2851 | n/a | } |
|---|
| 2852 | n/a | |
|---|
| 2853 | n/a | if (PyCPointerTypeObject_Check(type) |
|---|
| 2854 | n/a | && ArrayObject_Check(value)) { |
|---|
| 2855 | n/a | StgDictObject *p1, *p2; |
|---|
| 2856 | n/a | PyObject *keep; |
|---|
| 2857 | n/a | p1 = PyObject_stgdict(value); |
|---|
| 2858 | n/a | assert(p1); /* Cannot be NULL for array instances */ |
|---|
| 2859 | n/a | p2 = PyType_stgdict(type); |
|---|
| 2860 | n/a | assert(p2); /* Cannot be NULL for pointer types */ |
|---|
| 2861 | n/a | |
|---|
| 2862 | n/a | if (p1->proto != p2->proto) { |
|---|
| 2863 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2864 | n/a | "incompatible types, %s instance instead of %s instance", |
|---|
| 2865 | n/a | Py_TYPE(value)->tp_name, |
|---|
| 2866 | n/a | ((PyTypeObject *)type)->tp_name); |
|---|
| 2867 | n/a | return NULL; |
|---|
| 2868 | n/a | } |
|---|
| 2869 | n/a | *(void **)ptr = src->b_ptr; |
|---|
| 2870 | n/a | |
|---|
| 2871 | n/a | keep = GetKeepedObjects(src); |
|---|
| 2872 | n/a | if (keep == NULL) |
|---|
| 2873 | n/a | return NULL; |
|---|
| 2874 | n/a | |
|---|
| 2875 | n/a | /* |
|---|
| 2876 | n/a | We are assigning an array object to a field which represents |
|---|
| 2877 | n/a | a pointer. This has the same effect as converting an array |
|---|
| 2878 | n/a | into a pointer. So, again, we have to keep the whole object |
|---|
| 2879 | n/a | pointed to (which is the array in this case) alive, and not |
|---|
| 2880 | n/a | only it's object list. So we create a tuple, containing |
|---|
| 2881 | n/a | b_objects list PLUS the array itself, and return that! |
|---|
| 2882 | n/a | */ |
|---|
| 2883 | n/a | return PyTuple_Pack(2, keep, value); |
|---|
| 2884 | n/a | } |
|---|
| 2885 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 2886 | n/a | "incompatible types, %s instance instead of %s instance", |
|---|
| 2887 | n/a | Py_TYPE(value)->tp_name, |
|---|
| 2888 | n/a | ((PyTypeObject *)type)->tp_name); |
|---|
| 2889 | n/a | return NULL; |
|---|
| 2890 | n/a | } |
|---|
| 2891 | n/a | |
|---|
| 2892 | n/a | /* |
|---|
| 2893 | n/a | * Set a slice in object 'dst', which has the type 'type', |
|---|
| 2894 | n/a | * to the value 'value'. |
|---|
| 2895 | n/a | */ |
|---|
| 2896 | n/a | int |
|---|
| 2897 | n/a | PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
|---|
| 2898 | n/a | Py_ssize_t index, Py_ssize_t size, char *ptr) |
|---|
| 2899 | n/a | { |
|---|
| 2900 | n/a | CDataObject *mem = (CDataObject *)dst; |
|---|
| 2901 | n/a | PyObject *result; |
|---|
| 2902 | n/a | |
|---|
| 2903 | n/a | if (!CDataObject_Check(dst)) { |
|---|
| 2904 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2905 | n/a | "not a ctype instance"); |
|---|
| 2906 | n/a | return -1; |
|---|
| 2907 | n/a | } |
|---|
| 2908 | n/a | |
|---|
| 2909 | n/a | result = _PyCData_set(mem, type, setfunc, value, |
|---|
| 2910 | n/a | size, ptr); |
|---|
| 2911 | n/a | if (result == NULL) |
|---|
| 2912 | n/a | return -1; |
|---|
| 2913 | n/a | |
|---|
| 2914 | n/a | /* KeepRef steals a refcount from it's last argument */ |
|---|
| 2915 | n/a | /* If KeepRef fails, we are stumped. The dst memory block has already |
|---|
| 2916 | n/a | been changed */ |
|---|
| 2917 | n/a | return KeepRef(mem, index, result); |
|---|
| 2918 | n/a | } |
|---|
| 2919 | n/a | |
|---|
| 2920 | n/a | |
|---|
| 2921 | n/a | /******************************************************************/ |
|---|
| 2922 | n/a | static PyObject * |
|---|
| 2923 | n/a | GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 2924 | n/a | { |
|---|
| 2925 | n/a | CDataObject *obj; |
|---|
| 2926 | n/a | StgDictObject *dict; |
|---|
| 2927 | n/a | |
|---|
| 2928 | n/a | dict = PyType_stgdict((PyObject *)type); |
|---|
| 2929 | n/a | if (!dict) { |
|---|
| 2930 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2931 | n/a | "abstract class"); |
|---|
| 2932 | n/a | return NULL; |
|---|
| 2933 | n/a | } |
|---|
| 2934 | n/a | dict->flags |= DICTFLAG_FINAL; |
|---|
| 2935 | n/a | |
|---|
| 2936 | n/a | obj = (CDataObject *)type->tp_alloc(type, 0); |
|---|
| 2937 | n/a | if (!obj) |
|---|
| 2938 | n/a | return NULL; |
|---|
| 2939 | n/a | |
|---|
| 2940 | n/a | obj->b_base = NULL; |
|---|
| 2941 | n/a | obj->b_index = 0; |
|---|
| 2942 | n/a | obj->b_objects = NULL; |
|---|
| 2943 | n/a | obj->b_length = dict->length; |
|---|
| 2944 | n/a | |
|---|
| 2945 | n/a | if (-1 == PyCData_MallocBuffer(obj, dict)) { |
|---|
| 2946 | n/a | Py_DECREF(obj); |
|---|
| 2947 | n/a | return NULL; |
|---|
| 2948 | n/a | } |
|---|
| 2949 | n/a | return (PyObject *)obj; |
|---|
| 2950 | n/a | } |
|---|
| 2951 | n/a | /*****************************************************************/ |
|---|
| 2952 | n/a | /* |
|---|
| 2953 | n/a | PyCFuncPtr_Type |
|---|
| 2954 | n/a | */ |
|---|
| 2955 | n/a | |
|---|
| 2956 | n/a | static int |
|---|
| 2957 | n/a | PyCFuncPtr_set_errcheck(PyCFuncPtrObject *self, PyObject *ob) |
|---|
| 2958 | n/a | { |
|---|
| 2959 | n/a | if (ob && !PyCallable_Check(ob)) { |
|---|
| 2960 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2961 | n/a | "the errcheck attribute must be callable"); |
|---|
| 2962 | n/a | return -1; |
|---|
| 2963 | n/a | } |
|---|
| 2964 | n/a | Py_XINCREF(ob); |
|---|
| 2965 | n/a | Py_XSETREF(self->errcheck, ob); |
|---|
| 2966 | n/a | return 0; |
|---|
| 2967 | n/a | } |
|---|
| 2968 | n/a | |
|---|
| 2969 | n/a | static PyObject * |
|---|
| 2970 | n/a | PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self) |
|---|
| 2971 | n/a | { |
|---|
| 2972 | n/a | if (self->errcheck) { |
|---|
| 2973 | n/a | Py_INCREF(self->errcheck); |
|---|
| 2974 | n/a | return self->errcheck; |
|---|
| 2975 | n/a | } |
|---|
| 2976 | n/a | Py_RETURN_NONE; |
|---|
| 2977 | n/a | } |
|---|
| 2978 | n/a | |
|---|
| 2979 | n/a | static int |
|---|
| 2980 | n/a | PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob) |
|---|
| 2981 | n/a | { |
|---|
| 2982 | n/a | if (ob == NULL) { |
|---|
| 2983 | n/a | Py_CLEAR(self->restype); |
|---|
| 2984 | n/a | Py_CLEAR(self->checker); |
|---|
| 2985 | n/a | return 0; |
|---|
| 2986 | n/a | } |
|---|
| 2987 | n/a | if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) { |
|---|
| 2988 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 2989 | n/a | "restype must be a type, a callable, or None"); |
|---|
| 2990 | n/a | return -1; |
|---|
| 2991 | n/a | } |
|---|
| 2992 | n/a | Py_INCREF(ob); |
|---|
| 2993 | n/a | Py_XSETREF(self->restype, ob); |
|---|
| 2994 | n/a | Py_XSETREF(self->checker, PyObject_GetAttrString(ob, "_check_retval_")); |
|---|
| 2995 | n/a | if (self->checker == NULL) |
|---|
| 2996 | n/a | PyErr_Clear(); |
|---|
| 2997 | n/a | return 0; |
|---|
| 2998 | n/a | } |
|---|
| 2999 | n/a | |
|---|
| 3000 | n/a | static PyObject * |
|---|
| 3001 | n/a | PyCFuncPtr_get_restype(PyCFuncPtrObject *self) |
|---|
| 3002 | n/a | { |
|---|
| 3003 | n/a | StgDictObject *dict; |
|---|
| 3004 | n/a | if (self->restype) { |
|---|
| 3005 | n/a | Py_INCREF(self->restype); |
|---|
| 3006 | n/a | return self->restype; |
|---|
| 3007 | n/a | } |
|---|
| 3008 | n/a | dict = PyObject_stgdict((PyObject *)self); |
|---|
| 3009 | n/a | assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */ |
|---|
| 3010 | n/a | if (dict->restype) { |
|---|
| 3011 | n/a | Py_INCREF(dict->restype); |
|---|
| 3012 | n/a | return dict->restype; |
|---|
| 3013 | n/a | } else { |
|---|
| 3014 | n/a | Py_RETURN_NONE; |
|---|
| 3015 | n/a | } |
|---|
| 3016 | n/a | } |
|---|
| 3017 | n/a | |
|---|
| 3018 | n/a | static int |
|---|
| 3019 | n/a | PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob) |
|---|
| 3020 | n/a | { |
|---|
| 3021 | n/a | PyObject *converters; |
|---|
| 3022 | n/a | |
|---|
| 3023 | n/a | if (ob == NULL || ob == Py_None) { |
|---|
| 3024 | n/a | Py_CLEAR(self->converters); |
|---|
| 3025 | n/a | Py_CLEAR(self->argtypes); |
|---|
| 3026 | n/a | } else { |
|---|
| 3027 | n/a | converters = converters_from_argtypes(ob); |
|---|
| 3028 | n/a | if (!converters) |
|---|
| 3029 | n/a | return -1; |
|---|
| 3030 | n/a | Py_XSETREF(self->converters, converters); |
|---|
| 3031 | n/a | Py_INCREF(ob); |
|---|
| 3032 | n/a | Py_XSETREF(self->argtypes, ob); |
|---|
| 3033 | n/a | } |
|---|
| 3034 | n/a | return 0; |
|---|
| 3035 | n/a | } |
|---|
| 3036 | n/a | |
|---|
| 3037 | n/a | static PyObject * |
|---|
| 3038 | n/a | PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self) |
|---|
| 3039 | n/a | { |
|---|
| 3040 | n/a | StgDictObject *dict; |
|---|
| 3041 | n/a | if (self->argtypes) { |
|---|
| 3042 | n/a | Py_INCREF(self->argtypes); |
|---|
| 3043 | n/a | return self->argtypes; |
|---|
| 3044 | n/a | } |
|---|
| 3045 | n/a | dict = PyObject_stgdict((PyObject *)self); |
|---|
| 3046 | n/a | assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */ |
|---|
| 3047 | n/a | if (dict->argtypes) { |
|---|
| 3048 | n/a | Py_INCREF(dict->argtypes); |
|---|
| 3049 | n/a | return dict->argtypes; |
|---|
| 3050 | n/a | } else { |
|---|
| 3051 | n/a | Py_RETURN_NONE; |
|---|
| 3052 | n/a | } |
|---|
| 3053 | n/a | } |
|---|
| 3054 | n/a | |
|---|
| 3055 | n/a | static PyGetSetDef PyCFuncPtr_getsets[] = { |
|---|
| 3056 | n/a | { "errcheck", (getter)PyCFuncPtr_get_errcheck, (setter)PyCFuncPtr_set_errcheck, |
|---|
| 3057 | n/a | "a function to check for errors", NULL }, |
|---|
| 3058 | n/a | { "restype", (getter)PyCFuncPtr_get_restype, (setter)PyCFuncPtr_set_restype, |
|---|
| 3059 | n/a | "specify the result type", NULL }, |
|---|
| 3060 | n/a | { "argtypes", (getter)PyCFuncPtr_get_argtypes, |
|---|
| 3061 | n/a | (setter)PyCFuncPtr_set_argtypes, |
|---|
| 3062 | n/a | "specify the argument types", NULL }, |
|---|
| 3063 | n/a | { NULL, NULL } |
|---|
| 3064 | n/a | }; |
|---|
| 3065 | n/a | |
|---|
| 3066 | n/a | #ifdef MS_WIN32 |
|---|
| 3067 | n/a | static PPROC FindAddress(void *handle, const char *name, PyObject *type) |
|---|
| 3068 | n/a | { |
|---|
| 3069 | n/a | #ifdef MS_WIN64 |
|---|
| 3070 | n/a | /* win64 has no stdcall calling conv, so it should |
|---|
| 3071 | n/a | also not have the name mangling of it. |
|---|
| 3072 | n/a | */ |
|---|
| 3073 | n/a | return (PPROC)GetProcAddress(handle, name); |
|---|
| 3074 | n/a | #else |
|---|
| 3075 | n/a | PPROC address; |
|---|
| 3076 | n/a | char *mangled_name; |
|---|
| 3077 | n/a | int i; |
|---|
| 3078 | n/a | StgDictObject *dict; |
|---|
| 3079 | n/a | |
|---|
| 3080 | n/a | address = (PPROC)GetProcAddress(handle, name); |
|---|
| 3081 | n/a | if (address) |
|---|
| 3082 | n/a | return address; |
|---|
| 3083 | n/a | if (((size_t)name & ~0xFFFF) == 0) { |
|---|
| 3084 | n/a | return NULL; |
|---|
| 3085 | n/a | } |
|---|
| 3086 | n/a | |
|---|
| 3087 | n/a | dict = PyType_stgdict((PyObject *)type); |
|---|
| 3088 | n/a | /* It should not happen that dict is NULL, but better be safe */ |
|---|
| 3089 | n/a | if (dict==NULL || dict->flags & FUNCFLAG_CDECL) |
|---|
| 3090 | n/a | return address; |
|---|
| 3091 | n/a | |
|---|
| 3092 | n/a | /* for stdcall, try mangled names: |
|---|
| 3093 | n/a | funcname -> _funcname@<n> |
|---|
| 3094 | n/a | where n is 0, 4, 8, 12, ..., 128 |
|---|
| 3095 | n/a | */ |
|---|
| 3096 | n/a | mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */ |
|---|
| 3097 | n/a | if (!mangled_name) |
|---|
| 3098 | n/a | return NULL; |
|---|
| 3099 | n/a | for (i = 0; i < 32; ++i) { |
|---|
| 3100 | n/a | sprintf(mangled_name, "_%s@%d", name, i*4); |
|---|
| 3101 | n/a | address = (PPROC)GetProcAddress(handle, mangled_name); |
|---|
| 3102 | n/a | if (address) |
|---|
| 3103 | n/a | return address; |
|---|
| 3104 | n/a | } |
|---|
| 3105 | n/a | return NULL; |
|---|
| 3106 | n/a | #endif |
|---|
| 3107 | n/a | } |
|---|
| 3108 | n/a | #endif |
|---|
| 3109 | n/a | |
|---|
| 3110 | n/a | /* Return 1 if usable, 0 else and exception set. */ |
|---|
| 3111 | n/a | static int |
|---|
| 3112 | n/a | _check_outarg_type(PyObject *arg, Py_ssize_t index) |
|---|
| 3113 | n/a | { |
|---|
| 3114 | n/a | StgDictObject *dict; |
|---|
| 3115 | n/a | |
|---|
| 3116 | n/a | if (PyCPointerTypeObject_Check(arg)) |
|---|
| 3117 | n/a | return 1; |
|---|
| 3118 | n/a | |
|---|
| 3119 | n/a | if (PyCArrayTypeObject_Check(arg)) |
|---|
| 3120 | n/a | return 1; |
|---|
| 3121 | n/a | |
|---|
| 3122 | n/a | dict = PyType_stgdict(arg); |
|---|
| 3123 | n/a | if (dict |
|---|
| 3124 | n/a | /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ |
|---|
| 3125 | n/a | && PyUnicode_Check(dict->proto) |
|---|
| 3126 | n/a | /* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter type */ |
|---|
| 3127 | n/a | && (strchr("PzZ", PyUnicode_AsUTF8(dict->proto)[0]))) { |
|---|
| 3128 | n/a | return 1; |
|---|
| 3129 | n/a | } |
|---|
| 3130 | n/a | |
|---|
| 3131 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3132 | n/a | "'out' parameter %d must be a pointer type, not %s", |
|---|
| 3133 | n/a | Py_SAFE_DOWNCAST(index, Py_ssize_t, int), |
|---|
| 3134 | n/a | PyType_Check(arg) ? |
|---|
| 3135 | n/a | ((PyTypeObject *)arg)->tp_name : |
|---|
| 3136 | n/a | Py_TYPE(arg)->tp_name); |
|---|
| 3137 | n/a | return 0; |
|---|
| 3138 | n/a | } |
|---|
| 3139 | n/a | |
|---|
| 3140 | n/a | /* Returns 1 on success, 0 on error */ |
|---|
| 3141 | n/a | static int |
|---|
| 3142 | n/a | _validate_paramflags(PyTypeObject *type, PyObject *paramflags) |
|---|
| 3143 | n/a | { |
|---|
| 3144 | n/a | Py_ssize_t i, len; |
|---|
| 3145 | n/a | StgDictObject *dict; |
|---|
| 3146 | n/a | PyObject *argtypes; |
|---|
| 3147 | n/a | |
|---|
| 3148 | n/a | dict = PyType_stgdict((PyObject *)type); |
|---|
| 3149 | n/a | assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */ |
|---|
| 3150 | n/a | argtypes = dict->argtypes; |
|---|
| 3151 | n/a | |
|---|
| 3152 | n/a | if (paramflags == NULL || dict->argtypes == NULL) |
|---|
| 3153 | n/a | return 1; |
|---|
| 3154 | n/a | |
|---|
| 3155 | n/a | if (!PyTuple_Check(paramflags)) { |
|---|
| 3156 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3157 | n/a | "paramflags must be a tuple or None"); |
|---|
| 3158 | n/a | return 0; |
|---|
| 3159 | n/a | } |
|---|
| 3160 | n/a | |
|---|
| 3161 | n/a | len = PyTuple_GET_SIZE(paramflags); |
|---|
| 3162 | n/a | if (len != PyTuple_GET_SIZE(dict->argtypes)) { |
|---|
| 3163 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 3164 | n/a | "paramflags must have the same length as argtypes"); |
|---|
| 3165 | n/a | return 0; |
|---|
| 3166 | n/a | } |
|---|
| 3167 | n/a | |
|---|
| 3168 | n/a | for (i = 0; i < len; ++i) { |
|---|
| 3169 | n/a | PyObject *item = PyTuple_GET_ITEM(paramflags, i); |
|---|
| 3170 | n/a | int flag; |
|---|
| 3171 | n/a | char *name; |
|---|
| 3172 | n/a | PyObject *defval; |
|---|
| 3173 | n/a | PyObject *typ; |
|---|
| 3174 | n/a | if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) { |
|---|
| 3175 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3176 | n/a | "paramflags must be a sequence of (int [,string [,value]]) tuples"); |
|---|
| 3177 | n/a | return 0; |
|---|
| 3178 | n/a | } |
|---|
| 3179 | n/a | typ = PyTuple_GET_ITEM(argtypes, i); |
|---|
| 3180 | n/a | switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { |
|---|
| 3181 | n/a | case 0: |
|---|
| 3182 | n/a | case PARAMFLAG_FIN: |
|---|
| 3183 | n/a | case PARAMFLAG_FIN | PARAMFLAG_FLCID: |
|---|
| 3184 | n/a | case PARAMFLAG_FIN | PARAMFLAG_FOUT: |
|---|
| 3185 | n/a | break; |
|---|
| 3186 | n/a | case PARAMFLAG_FOUT: |
|---|
| 3187 | n/a | if (!_check_outarg_type(typ, i+1)) |
|---|
| 3188 | n/a | return 0; |
|---|
| 3189 | n/a | break; |
|---|
| 3190 | n/a | default: |
|---|
| 3191 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3192 | n/a | "paramflag value %d not supported", |
|---|
| 3193 | n/a | flag); |
|---|
| 3194 | n/a | return 0; |
|---|
| 3195 | n/a | } |
|---|
| 3196 | n/a | } |
|---|
| 3197 | n/a | return 1; |
|---|
| 3198 | n/a | } |
|---|
| 3199 | n/a | |
|---|
| 3200 | n/a | static int |
|---|
| 3201 | n/a | _get_name(PyObject *obj, const char **pname) |
|---|
| 3202 | n/a | { |
|---|
| 3203 | n/a | #ifdef MS_WIN32 |
|---|
| 3204 | n/a | if (PyLong_Check(obj)) { |
|---|
| 3205 | n/a | /* We have to use MAKEINTRESOURCEA for Windows CE. |
|---|
| 3206 | n/a | Works on Windows as well, of course. |
|---|
| 3207 | n/a | */ |
|---|
| 3208 | n/a | *pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF); |
|---|
| 3209 | n/a | return 1; |
|---|
| 3210 | n/a | } |
|---|
| 3211 | n/a | #endif |
|---|
| 3212 | n/a | if (PyBytes_Check(obj)) { |
|---|
| 3213 | n/a | *pname = PyBytes_AS_STRING(obj); |
|---|
| 3214 | n/a | return *pname ? 1 : 0; |
|---|
| 3215 | n/a | } |
|---|
| 3216 | n/a | if (PyUnicode_Check(obj)) { |
|---|
| 3217 | n/a | *pname = PyUnicode_AsUTF8(obj); |
|---|
| 3218 | n/a | return *pname ? 1 : 0; |
|---|
| 3219 | n/a | } |
|---|
| 3220 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3221 | n/a | "function name must be string, bytes object or integer"); |
|---|
| 3222 | n/a | return 0; |
|---|
| 3223 | n/a | } |
|---|
| 3224 | n/a | |
|---|
| 3225 | n/a | |
|---|
| 3226 | n/a | static PyObject * |
|---|
| 3227 | n/a | PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3228 | n/a | { |
|---|
| 3229 | n/a | const char *name; |
|---|
| 3230 | n/a | int (* address)(void); |
|---|
| 3231 | n/a | PyObject *ftuple; |
|---|
| 3232 | n/a | PyObject *dll; |
|---|
| 3233 | n/a | PyObject *obj; |
|---|
| 3234 | n/a | PyCFuncPtrObject *self; |
|---|
| 3235 | n/a | void *handle; |
|---|
| 3236 | n/a | PyObject *paramflags = NULL; |
|---|
| 3237 | n/a | |
|---|
| 3238 | n/a | if (!PyArg_ParseTuple(args, "O|O", &ftuple, ¶mflags)) |
|---|
| 3239 | n/a | return NULL; |
|---|
| 3240 | n/a | if (paramflags == Py_None) |
|---|
| 3241 | n/a | paramflags = NULL; |
|---|
| 3242 | n/a | |
|---|
| 3243 | n/a | ftuple = PySequence_Tuple(ftuple); |
|---|
| 3244 | n/a | if (!ftuple) |
|---|
| 3245 | n/a | /* Here ftuple is a borrowed reference */ |
|---|
| 3246 | n/a | return NULL; |
|---|
| 3247 | n/a | |
|---|
| 3248 | n/a | if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) { |
|---|
| 3249 | n/a | Py_DECREF(ftuple); |
|---|
| 3250 | n/a | return NULL; |
|---|
| 3251 | n/a | } |
|---|
| 3252 | n/a | |
|---|
| 3253 | n/a | obj = PyObject_GetAttrString(dll, "_handle"); |
|---|
| 3254 | n/a | if (!obj) { |
|---|
| 3255 | n/a | Py_DECREF(ftuple); |
|---|
| 3256 | n/a | return NULL; |
|---|
| 3257 | n/a | } |
|---|
| 3258 | n/a | if (!PyLong_Check(obj)) { |
|---|
| 3259 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3260 | n/a | "the _handle attribute of the second argument must be an integer"); |
|---|
| 3261 | n/a | Py_DECREF(ftuple); |
|---|
| 3262 | n/a | Py_DECREF(obj); |
|---|
| 3263 | n/a | return NULL; |
|---|
| 3264 | n/a | } |
|---|
| 3265 | n/a | handle = (void *)PyLong_AsVoidPtr(obj); |
|---|
| 3266 | n/a | Py_DECREF(obj); |
|---|
| 3267 | n/a | if (PyErr_Occurred()) { |
|---|
| 3268 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 3269 | n/a | "could not convert the _handle attribute to a pointer"); |
|---|
| 3270 | n/a | Py_DECREF(ftuple); |
|---|
| 3271 | n/a | return NULL; |
|---|
| 3272 | n/a | } |
|---|
| 3273 | n/a | |
|---|
| 3274 | n/a | #ifdef MS_WIN32 |
|---|
| 3275 | n/a | address = FindAddress(handle, name, (PyObject *)type); |
|---|
| 3276 | n/a | if (!address) { |
|---|
| 3277 | n/a | if (!IS_INTRESOURCE(name)) |
|---|
| 3278 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 3279 | n/a | "function '%s' not found", |
|---|
| 3280 | n/a | name); |
|---|
| 3281 | n/a | else |
|---|
| 3282 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 3283 | n/a | "function ordinal %d not found", |
|---|
| 3284 | n/a | (WORD)(size_t)name); |
|---|
| 3285 | n/a | Py_DECREF(ftuple); |
|---|
| 3286 | n/a | return NULL; |
|---|
| 3287 | n/a | } |
|---|
| 3288 | n/a | #else |
|---|
| 3289 | n/a | address = (PPROC)ctypes_dlsym(handle, name); |
|---|
| 3290 | n/a | if (!address) { |
|---|
| 3291 | n/a | #ifdef __CYGWIN__ |
|---|
| 3292 | n/a | /* dlerror() isn't very helpful on cygwin */ |
|---|
| 3293 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 3294 | n/a | "function '%s' not found", |
|---|
| 3295 | n/a | name); |
|---|
| 3296 | n/a | #else |
|---|
| 3297 | n/a | PyErr_SetString(PyExc_AttributeError, ctypes_dlerror()); |
|---|
| 3298 | n/a | #endif |
|---|
| 3299 | n/a | Py_DECREF(ftuple); |
|---|
| 3300 | n/a | return NULL; |
|---|
| 3301 | n/a | } |
|---|
| 3302 | n/a | #endif |
|---|
| 3303 | n/a | Py_INCREF(dll); /* for KeepRef */ |
|---|
| 3304 | n/a | Py_DECREF(ftuple); |
|---|
| 3305 | n/a | if (!_validate_paramflags(type, paramflags)) |
|---|
| 3306 | n/a | return NULL; |
|---|
| 3307 | n/a | |
|---|
| 3308 | n/a | self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds); |
|---|
| 3309 | n/a | if (!self) |
|---|
| 3310 | n/a | return NULL; |
|---|
| 3311 | n/a | |
|---|
| 3312 | n/a | Py_XINCREF(paramflags); |
|---|
| 3313 | n/a | self->paramflags = paramflags; |
|---|
| 3314 | n/a | |
|---|
| 3315 | n/a | *(void **)self->b_ptr = address; |
|---|
| 3316 | n/a | |
|---|
| 3317 | n/a | if (-1 == KeepRef((CDataObject *)self, 0, dll)) { |
|---|
| 3318 | n/a | Py_DECREF((PyObject *)self); |
|---|
| 3319 | n/a | return NULL; |
|---|
| 3320 | n/a | } |
|---|
| 3321 | n/a | |
|---|
| 3322 | n/a | Py_INCREF(self); |
|---|
| 3323 | n/a | self->callable = (PyObject *)self; |
|---|
| 3324 | n/a | return (PyObject *)self; |
|---|
| 3325 | n/a | } |
|---|
| 3326 | n/a | |
|---|
| 3327 | n/a | #ifdef MS_WIN32 |
|---|
| 3328 | n/a | static PyObject * |
|---|
| 3329 | n/a | PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3330 | n/a | { |
|---|
| 3331 | n/a | PyCFuncPtrObject *self; |
|---|
| 3332 | n/a | int index; |
|---|
| 3333 | n/a | char *name = NULL; |
|---|
| 3334 | n/a | PyObject *paramflags = NULL; |
|---|
| 3335 | n/a | GUID *iid = NULL; |
|---|
| 3336 | n/a | Py_ssize_t iid_len = 0; |
|---|
| 3337 | n/a | |
|---|
| 3338 | n/a | if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, ¶mflags, &iid, &iid_len)) |
|---|
| 3339 | n/a | return NULL; |
|---|
| 3340 | n/a | if (paramflags == Py_None) |
|---|
| 3341 | n/a | paramflags = NULL; |
|---|
| 3342 | n/a | |
|---|
| 3343 | n/a | if (!_validate_paramflags(type, paramflags)) |
|---|
| 3344 | n/a | return NULL; |
|---|
| 3345 | n/a | |
|---|
| 3346 | n/a | self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds); |
|---|
| 3347 | n/a | self->index = index + 0x1000; |
|---|
| 3348 | n/a | Py_XINCREF(paramflags); |
|---|
| 3349 | n/a | self->paramflags = paramflags; |
|---|
| 3350 | n/a | if (iid_len == sizeof(GUID)) |
|---|
| 3351 | n/a | self->iid = iid; |
|---|
| 3352 | n/a | return (PyObject *)self; |
|---|
| 3353 | n/a | } |
|---|
| 3354 | n/a | #endif |
|---|
| 3355 | n/a | |
|---|
| 3356 | n/a | /* |
|---|
| 3357 | n/a | PyCFuncPtr_new accepts different argument lists in addition to the standard |
|---|
| 3358 | n/a | _basespec_ keyword arg: |
|---|
| 3359 | n/a | |
|---|
| 3360 | n/a | one argument form |
|---|
| 3361 | n/a | "i" - function address |
|---|
| 3362 | n/a | "O" - must be a callable, creates a C callable function |
|---|
| 3363 | n/a | |
|---|
| 3364 | n/a | two or more argument forms (the third argument is a paramflags tuple) |
|---|
| 3365 | n/a | "(sO)|..." - (function name, dll object (with an integer handle)), paramflags |
|---|
| 3366 | n/a | "(iO)|..." - (function ordinal, dll object (with an integer handle)), paramflags |
|---|
| 3367 | n/a | "is|..." - vtable index, method name, creates callable calling COM vtbl |
|---|
| 3368 | n/a | */ |
|---|
| 3369 | n/a | static PyObject * |
|---|
| 3370 | n/a | PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 3371 | n/a | { |
|---|
| 3372 | n/a | PyCFuncPtrObject *self; |
|---|
| 3373 | n/a | PyObject *callable; |
|---|
| 3374 | n/a | StgDictObject *dict; |
|---|
| 3375 | n/a | CThunkObject *thunk; |
|---|
| 3376 | n/a | |
|---|
| 3377 | n/a | if (PyTuple_GET_SIZE(args) == 0) |
|---|
| 3378 | n/a | return GenericPyCData_new(type, args, kwds); |
|---|
| 3379 | n/a | |
|---|
| 3380 | n/a | if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0))) |
|---|
| 3381 | n/a | return PyCFuncPtr_FromDll(type, args, kwds); |
|---|
| 3382 | n/a | |
|---|
| 3383 | n/a | #ifdef MS_WIN32 |
|---|
| 3384 | n/a | if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0))) |
|---|
| 3385 | n/a | return PyCFuncPtr_FromVtblIndex(type, args, kwds); |
|---|
| 3386 | n/a | #endif |
|---|
| 3387 | n/a | |
|---|
| 3388 | n/a | if (1 == PyTuple_GET_SIZE(args) |
|---|
| 3389 | n/a | && (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) { |
|---|
| 3390 | n/a | CDataObject *ob; |
|---|
| 3391 | n/a | void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0)); |
|---|
| 3392 | n/a | if (ptr == NULL && PyErr_Occurred()) |
|---|
| 3393 | n/a | return NULL; |
|---|
| 3394 | n/a | ob = (CDataObject *)GenericPyCData_new(type, args, kwds); |
|---|
| 3395 | n/a | if (ob == NULL) |
|---|
| 3396 | n/a | return NULL; |
|---|
| 3397 | n/a | *(void **)ob->b_ptr = ptr; |
|---|
| 3398 | n/a | return (PyObject *)ob; |
|---|
| 3399 | n/a | } |
|---|
| 3400 | n/a | |
|---|
| 3401 | n/a | if (!PyArg_ParseTuple(args, "O", &callable)) |
|---|
| 3402 | n/a | return NULL; |
|---|
| 3403 | n/a | if (!PyCallable_Check(callable)) { |
|---|
| 3404 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3405 | n/a | "argument must be callable or integer function address"); |
|---|
| 3406 | n/a | return NULL; |
|---|
| 3407 | n/a | } |
|---|
| 3408 | n/a | |
|---|
| 3409 | n/a | /* XXX XXX This would allow passing additional options. For COM |
|---|
| 3410 | n/a | method *implementations*, we would probably want different |
|---|
| 3411 | n/a | behaviour than in 'normal' callback functions: return a HRESULT if |
|---|
| 3412 | n/a | an exception occurs in the callback, and print the traceback not |
|---|
| 3413 | n/a | only on the console, but also to OutputDebugString() or something |
|---|
| 3414 | n/a | like that. |
|---|
| 3415 | n/a | */ |
|---|
| 3416 | n/a | /* |
|---|
| 3417 | n/a | if (kwds && PyDict_GetItemString(kwds, "options")) { |
|---|
| 3418 | n/a | ... |
|---|
| 3419 | n/a | } |
|---|
| 3420 | n/a | */ |
|---|
| 3421 | n/a | |
|---|
| 3422 | n/a | dict = PyType_stgdict((PyObject *)type); |
|---|
| 3423 | n/a | /* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */ |
|---|
| 3424 | n/a | if (!dict || !dict->argtypes) { |
|---|
| 3425 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3426 | n/a | "cannot construct instance of this class:" |
|---|
| 3427 | n/a | " no argtypes"); |
|---|
| 3428 | n/a | return NULL; |
|---|
| 3429 | n/a | } |
|---|
| 3430 | n/a | |
|---|
| 3431 | n/a | thunk = _ctypes_alloc_callback(callable, |
|---|
| 3432 | n/a | dict->argtypes, |
|---|
| 3433 | n/a | dict->restype, |
|---|
| 3434 | n/a | dict->flags); |
|---|
| 3435 | n/a | if (!thunk) |
|---|
| 3436 | n/a | return NULL; |
|---|
| 3437 | n/a | |
|---|
| 3438 | n/a | self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds); |
|---|
| 3439 | n/a | if (self == NULL) { |
|---|
| 3440 | n/a | Py_DECREF(thunk); |
|---|
| 3441 | n/a | return NULL; |
|---|
| 3442 | n/a | } |
|---|
| 3443 | n/a | |
|---|
| 3444 | n/a | Py_INCREF(callable); |
|---|
| 3445 | n/a | self->callable = callable; |
|---|
| 3446 | n/a | |
|---|
| 3447 | n/a | self->thunk = thunk; |
|---|
| 3448 | n/a | *(void **)self->b_ptr = (void *)thunk->pcl_exec; |
|---|
| 3449 | n/a | |
|---|
| 3450 | n/a | Py_INCREF((PyObject *)thunk); /* for KeepRef */ |
|---|
| 3451 | n/a | if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) { |
|---|
| 3452 | n/a | Py_DECREF((PyObject *)self); |
|---|
| 3453 | n/a | return NULL; |
|---|
| 3454 | n/a | } |
|---|
| 3455 | n/a | return (PyObject *)self; |
|---|
| 3456 | n/a | } |
|---|
| 3457 | n/a | |
|---|
| 3458 | n/a | |
|---|
| 3459 | n/a | /* |
|---|
| 3460 | n/a | _byref consumes a refcount to its argument |
|---|
| 3461 | n/a | */ |
|---|
| 3462 | n/a | static PyObject * |
|---|
| 3463 | n/a | _byref(PyObject *obj) |
|---|
| 3464 | n/a | { |
|---|
| 3465 | n/a | PyCArgObject *parg; |
|---|
| 3466 | n/a | if (!CDataObject_Check(obj)) { |
|---|
| 3467 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3468 | n/a | "expected CData instance"); |
|---|
| 3469 | n/a | return NULL; |
|---|
| 3470 | n/a | } |
|---|
| 3471 | n/a | |
|---|
| 3472 | n/a | parg = PyCArgObject_new(); |
|---|
| 3473 | n/a | if (parg == NULL) { |
|---|
| 3474 | n/a | Py_DECREF(obj); |
|---|
| 3475 | n/a | return NULL; |
|---|
| 3476 | n/a | } |
|---|
| 3477 | n/a | |
|---|
| 3478 | n/a | parg->tag = 'P'; |
|---|
| 3479 | n/a | parg->pffi_type = &ffi_type_pointer; |
|---|
| 3480 | n/a | parg->obj = obj; |
|---|
| 3481 | n/a | parg->value.p = ((CDataObject *)obj)->b_ptr; |
|---|
| 3482 | n/a | return (PyObject *)parg; |
|---|
| 3483 | n/a | } |
|---|
| 3484 | n/a | |
|---|
| 3485 | n/a | static PyObject * |
|---|
| 3486 | n/a | _get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds) |
|---|
| 3487 | n/a | { |
|---|
| 3488 | n/a | PyObject *v; |
|---|
| 3489 | n/a | |
|---|
| 3490 | n/a | if (*pindex < PyTuple_GET_SIZE(inargs)) { |
|---|
| 3491 | n/a | v = PyTuple_GET_ITEM(inargs, *pindex); |
|---|
| 3492 | n/a | ++*pindex; |
|---|
| 3493 | n/a | Py_INCREF(v); |
|---|
| 3494 | n/a | return v; |
|---|
| 3495 | n/a | } |
|---|
| 3496 | n/a | if (kwds && name && (v = PyDict_GetItem(kwds, name))) { |
|---|
| 3497 | n/a | ++*pindex; |
|---|
| 3498 | n/a | Py_INCREF(v); |
|---|
| 3499 | n/a | return v; |
|---|
| 3500 | n/a | } |
|---|
| 3501 | n/a | if (defval) { |
|---|
| 3502 | n/a | Py_INCREF(defval); |
|---|
| 3503 | n/a | return defval; |
|---|
| 3504 | n/a | } |
|---|
| 3505 | n/a | /* we can't currently emit a better error message */ |
|---|
| 3506 | n/a | if (name) |
|---|
| 3507 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3508 | n/a | "required argument '%S' missing", name); |
|---|
| 3509 | n/a | else |
|---|
| 3510 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3511 | n/a | "not enough arguments"); |
|---|
| 3512 | n/a | return NULL; |
|---|
| 3513 | n/a | } |
|---|
| 3514 | n/a | |
|---|
| 3515 | n/a | /* |
|---|
| 3516 | n/a | This function implements higher level functionality plus the ability to call |
|---|
| 3517 | n/a | functions with keyword arguments by looking at parameter flags. parameter |
|---|
| 3518 | n/a | flags is a tuple of 1, 2 or 3-tuples. The first entry in each is an integer |
|---|
| 3519 | n/a | specifying the direction of the data transfer for this parameter - 'in', |
|---|
| 3520 | n/a | 'out' or 'inout' (zero means the same as 'in'). The second entry is the |
|---|
| 3521 | n/a | parameter name, and the third is the default value if the parameter is |
|---|
| 3522 | n/a | missing in the function call. |
|---|
| 3523 | n/a | |
|---|
| 3524 | n/a | This function builds and returns a new tuple 'callargs' which contains the |
|---|
| 3525 | n/a | parameters to use in the call. Items on this tuple are copied from the |
|---|
| 3526 | n/a | 'inargs' tuple for 'in' and 'in, out' parameters, and constructed from the |
|---|
| 3527 | n/a | 'argtypes' tuple for 'out' parameters. It also calculates numretvals which |
|---|
| 3528 | n/a | is the number of return values for the function, outmask/inoutmask are |
|---|
| 3529 | n/a | bitmasks containing indexes into the callargs tuple specifying which |
|---|
| 3530 | n/a | parameters have to be returned. _build_result builds the return value of the |
|---|
| 3531 | n/a | function. |
|---|
| 3532 | n/a | */ |
|---|
| 3533 | n/a | static PyObject * |
|---|
| 3534 | n/a | _build_callargs(PyCFuncPtrObject *self, PyObject *argtypes, |
|---|
| 3535 | n/a | PyObject *inargs, PyObject *kwds, |
|---|
| 3536 | n/a | int *poutmask, int *pinoutmask, unsigned int *pnumretvals) |
|---|
| 3537 | n/a | { |
|---|
| 3538 | n/a | PyObject *paramflags = self->paramflags; |
|---|
| 3539 | n/a | PyObject *callargs; |
|---|
| 3540 | n/a | StgDictObject *dict; |
|---|
| 3541 | n/a | Py_ssize_t i, len; |
|---|
| 3542 | n/a | int inargs_index = 0; |
|---|
| 3543 | n/a | /* It's a little bit difficult to determine how many arguments the |
|---|
| 3544 | n/a | function call requires/accepts. For simplicity, we count the consumed |
|---|
| 3545 | n/a | args and compare this to the number of supplied args. */ |
|---|
| 3546 | n/a | Py_ssize_t actual_args; |
|---|
| 3547 | n/a | |
|---|
| 3548 | n/a | *poutmask = 0; |
|---|
| 3549 | n/a | *pinoutmask = 0; |
|---|
| 3550 | n/a | *pnumretvals = 0; |
|---|
| 3551 | n/a | |
|---|
| 3552 | n/a | /* Trivial cases, where we either return inargs itself, or a slice of it. */ |
|---|
| 3553 | n/a | if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) { |
|---|
| 3554 | n/a | #ifdef MS_WIN32 |
|---|
| 3555 | n/a | if (self->index) |
|---|
| 3556 | n/a | return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs)); |
|---|
| 3557 | n/a | #endif |
|---|
| 3558 | n/a | Py_INCREF(inargs); |
|---|
| 3559 | n/a | return inargs; |
|---|
| 3560 | n/a | } |
|---|
| 3561 | n/a | |
|---|
| 3562 | n/a | len = PyTuple_GET_SIZE(argtypes); |
|---|
| 3563 | n/a | callargs = PyTuple_New(len); /* the argument tuple we build */ |
|---|
| 3564 | n/a | if (callargs == NULL) |
|---|
| 3565 | n/a | return NULL; |
|---|
| 3566 | n/a | |
|---|
| 3567 | n/a | #ifdef MS_WIN32 |
|---|
| 3568 | n/a | /* For a COM method, skip the first arg */ |
|---|
| 3569 | n/a | if (self->index) { |
|---|
| 3570 | n/a | inargs_index = 1; |
|---|
| 3571 | n/a | } |
|---|
| 3572 | n/a | #endif |
|---|
| 3573 | n/a | for (i = 0; i < len; ++i) { |
|---|
| 3574 | n/a | PyObject *item = PyTuple_GET_ITEM(paramflags, i); |
|---|
| 3575 | n/a | PyObject *ob; |
|---|
| 3576 | n/a | int flag; |
|---|
| 3577 | n/a | PyObject *name = NULL; |
|---|
| 3578 | n/a | PyObject *defval = NULL; |
|---|
| 3579 | n/a | |
|---|
| 3580 | n/a | /* This way seems to be ~2 us faster than the PyArg_ParseTuple |
|---|
| 3581 | n/a | calls below. */ |
|---|
| 3582 | n/a | /* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */ |
|---|
| 3583 | n/a | Py_ssize_t tsize = PyTuple_GET_SIZE(item); |
|---|
| 3584 | n/a | flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0)); |
|---|
| 3585 | n/a | name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL; |
|---|
| 3586 | n/a | defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL; |
|---|
| 3587 | n/a | |
|---|
| 3588 | n/a | switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { |
|---|
| 3589 | n/a | case PARAMFLAG_FIN | PARAMFLAG_FLCID: |
|---|
| 3590 | n/a | /* ['in', 'lcid'] parameter. Always taken from defval, |
|---|
| 3591 | n/a | if given, else the integer 0. */ |
|---|
| 3592 | n/a | if (defval == NULL) { |
|---|
| 3593 | n/a | defval = PyLong_FromLong(0); |
|---|
| 3594 | n/a | if (defval == NULL) |
|---|
| 3595 | n/a | goto error; |
|---|
| 3596 | n/a | } else |
|---|
| 3597 | n/a | Py_INCREF(defval); |
|---|
| 3598 | n/a | PyTuple_SET_ITEM(callargs, i, defval); |
|---|
| 3599 | n/a | break; |
|---|
| 3600 | n/a | case (PARAMFLAG_FIN | PARAMFLAG_FOUT): |
|---|
| 3601 | n/a | *pinoutmask |= (1 << i); /* mark as inout arg */ |
|---|
| 3602 | n/a | (*pnumretvals)++; |
|---|
| 3603 | n/a | /* fall through to PARAMFLAG_FIN... */ |
|---|
| 3604 | n/a | case 0: |
|---|
| 3605 | n/a | case PARAMFLAG_FIN: |
|---|
| 3606 | n/a | /* 'in' parameter. Copy it from inargs. */ |
|---|
| 3607 | n/a | ob =_get_arg(&inargs_index, name, defval, inargs, kwds); |
|---|
| 3608 | n/a | if (ob == NULL) |
|---|
| 3609 | n/a | goto error; |
|---|
| 3610 | n/a | PyTuple_SET_ITEM(callargs, i, ob); |
|---|
| 3611 | n/a | break; |
|---|
| 3612 | n/a | case PARAMFLAG_FOUT: |
|---|
| 3613 | n/a | /* XXX Refactor this code into a separate function. */ |
|---|
| 3614 | n/a | /* 'out' parameter. |
|---|
| 3615 | n/a | argtypes[i] must be a POINTER to a c type. |
|---|
| 3616 | n/a | |
|---|
| 3617 | n/a | Cannot by supplied in inargs, but a defval will be used |
|---|
| 3618 | n/a | if available. XXX Should we support getting it from kwds? |
|---|
| 3619 | n/a | */ |
|---|
| 3620 | n/a | if (defval) { |
|---|
| 3621 | n/a | /* XXX Using mutable objects as defval will |
|---|
| 3622 | n/a | make the function non-threadsafe, unless we |
|---|
| 3623 | n/a | copy the object in each invocation */ |
|---|
| 3624 | n/a | Py_INCREF(defval); |
|---|
| 3625 | n/a | PyTuple_SET_ITEM(callargs, i, defval); |
|---|
| 3626 | n/a | *poutmask |= (1 << i); /* mark as out arg */ |
|---|
| 3627 | n/a | (*pnumretvals)++; |
|---|
| 3628 | n/a | break; |
|---|
| 3629 | n/a | } |
|---|
| 3630 | n/a | ob = PyTuple_GET_ITEM(argtypes, i); |
|---|
| 3631 | n/a | dict = PyType_stgdict(ob); |
|---|
| 3632 | n/a | if (dict == NULL) { |
|---|
| 3633 | n/a | /* Cannot happen: _validate_paramflags() |
|---|
| 3634 | n/a | would not accept such an object */ |
|---|
| 3635 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 3636 | n/a | "NULL stgdict unexpected"); |
|---|
| 3637 | n/a | goto error; |
|---|
| 3638 | n/a | } |
|---|
| 3639 | n/a | if (PyUnicode_Check(dict->proto)) { |
|---|
| 3640 | n/a | PyErr_Format( |
|---|
| 3641 | n/a | PyExc_TypeError, |
|---|
| 3642 | n/a | "%s 'out' parameter must be passed as default value", |
|---|
| 3643 | n/a | ((PyTypeObject *)ob)->tp_name); |
|---|
| 3644 | n/a | goto error; |
|---|
| 3645 | n/a | } |
|---|
| 3646 | n/a | if (PyCArrayTypeObject_Check(ob)) |
|---|
| 3647 | n/a | ob = _PyObject_CallNoArg(ob); |
|---|
| 3648 | n/a | else |
|---|
| 3649 | n/a | /* Create an instance of the pointed-to type */ |
|---|
| 3650 | n/a | ob = _PyObject_CallNoArg(dict->proto); |
|---|
| 3651 | n/a | /* |
|---|
| 3652 | n/a | XXX Is the following correct any longer? |
|---|
| 3653 | n/a | We must not pass a byref() to the array then but |
|---|
| 3654 | n/a | the array instance itself. Then, we cannot retrive |
|---|
| 3655 | n/a | the result from the PyCArgObject. |
|---|
| 3656 | n/a | */ |
|---|
| 3657 | n/a | if (ob == NULL) |
|---|
| 3658 | n/a | goto error; |
|---|
| 3659 | n/a | /* The .from_param call that will ocurr later will pass this |
|---|
| 3660 | n/a | as a byref parameter. */ |
|---|
| 3661 | n/a | PyTuple_SET_ITEM(callargs, i, ob); |
|---|
| 3662 | n/a | *poutmask |= (1 << i); /* mark as out arg */ |
|---|
| 3663 | n/a | (*pnumretvals)++; |
|---|
| 3664 | n/a | break; |
|---|
| 3665 | n/a | default: |
|---|
| 3666 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 3667 | n/a | "paramflag %d not yet implemented", flag); |
|---|
| 3668 | n/a | goto error; |
|---|
| 3669 | n/a | break; |
|---|
| 3670 | n/a | } |
|---|
| 3671 | n/a | } |
|---|
| 3672 | n/a | |
|---|
| 3673 | n/a | /* We have counted the arguments we have consumed in 'inargs_index'. This |
|---|
| 3674 | n/a | must be the same as len(inargs) + len(kwds), otherwise we have |
|---|
| 3675 | n/a | either too much or not enough arguments. */ |
|---|
| 3676 | n/a | |
|---|
| 3677 | n/a | actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_GET_SIZE(kwds) : 0); |
|---|
| 3678 | n/a | if (actual_args != inargs_index) { |
|---|
| 3679 | n/a | /* When we have default values or named parameters, this error |
|---|
| 3680 | n/a | message is misleading. See unittests/test_paramflags.py |
|---|
| 3681 | n/a | */ |
|---|
| 3682 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3683 | n/a | "call takes exactly %d arguments (%zd given)", |
|---|
| 3684 | n/a | inargs_index, actual_args); |
|---|
| 3685 | n/a | goto error; |
|---|
| 3686 | n/a | } |
|---|
| 3687 | n/a | |
|---|
| 3688 | n/a | /* outmask is a bitmask containing indexes into callargs. Items at |
|---|
| 3689 | n/a | these indexes contain values to return. |
|---|
| 3690 | n/a | */ |
|---|
| 3691 | n/a | return callargs; |
|---|
| 3692 | n/a | error: |
|---|
| 3693 | n/a | Py_DECREF(callargs); |
|---|
| 3694 | n/a | return NULL; |
|---|
| 3695 | n/a | } |
|---|
| 3696 | n/a | |
|---|
| 3697 | n/a | /* See also: |
|---|
| 3698 | n/a | http://msdn.microsoft.com/library/en-us/com/html/769127a1-1a14-4ed4-9d38-7cf3e571b661.asp |
|---|
| 3699 | n/a | */ |
|---|
| 3700 | n/a | /* |
|---|
| 3701 | n/a | Build return value of a function. |
|---|
| 3702 | n/a | |
|---|
| 3703 | n/a | Consumes the refcount on result and callargs. |
|---|
| 3704 | n/a | */ |
|---|
| 3705 | n/a | static PyObject * |
|---|
| 3706 | n/a | _build_result(PyObject *result, PyObject *callargs, |
|---|
| 3707 | n/a | int outmask, int inoutmask, unsigned int numretvals) |
|---|
| 3708 | n/a | { |
|---|
| 3709 | n/a | unsigned int i, index; |
|---|
| 3710 | n/a | int bit; |
|---|
| 3711 | n/a | PyObject *tup = NULL; |
|---|
| 3712 | n/a | |
|---|
| 3713 | n/a | if (callargs == NULL) |
|---|
| 3714 | n/a | return result; |
|---|
| 3715 | n/a | if (result == NULL || numretvals == 0) { |
|---|
| 3716 | n/a | Py_DECREF(callargs); |
|---|
| 3717 | n/a | return result; |
|---|
| 3718 | n/a | } |
|---|
| 3719 | n/a | Py_DECREF(result); |
|---|
| 3720 | n/a | |
|---|
| 3721 | n/a | /* tup will not be allocated if numretvals == 1 */ |
|---|
| 3722 | n/a | /* allocate tuple to hold the result */ |
|---|
| 3723 | n/a | if (numretvals > 1) { |
|---|
| 3724 | n/a | tup = PyTuple_New(numretvals); |
|---|
| 3725 | n/a | if (tup == NULL) { |
|---|
| 3726 | n/a | Py_DECREF(callargs); |
|---|
| 3727 | n/a | return NULL; |
|---|
| 3728 | n/a | } |
|---|
| 3729 | n/a | } |
|---|
| 3730 | n/a | |
|---|
| 3731 | n/a | index = 0; |
|---|
| 3732 | n/a | for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) { |
|---|
| 3733 | n/a | PyObject *v; |
|---|
| 3734 | n/a | if (bit & inoutmask) { |
|---|
| 3735 | n/a | v = PyTuple_GET_ITEM(callargs, i); |
|---|
| 3736 | n/a | Py_INCREF(v); |
|---|
| 3737 | n/a | if (numretvals == 1) { |
|---|
| 3738 | n/a | Py_DECREF(callargs); |
|---|
| 3739 | n/a | return v; |
|---|
| 3740 | n/a | } |
|---|
| 3741 | n/a | PyTuple_SET_ITEM(tup, index, v); |
|---|
| 3742 | n/a | index++; |
|---|
| 3743 | n/a | } else if (bit & outmask) { |
|---|
| 3744 | n/a | _Py_IDENTIFIER(__ctypes_from_outparam__); |
|---|
| 3745 | n/a | |
|---|
| 3746 | n/a | v = PyTuple_GET_ITEM(callargs, i); |
|---|
| 3747 | n/a | v = _PyObject_CallMethodId(v, &PyId___ctypes_from_outparam__, NULL); |
|---|
| 3748 | n/a | if (v == NULL || numretvals == 1) { |
|---|
| 3749 | n/a | Py_DECREF(callargs); |
|---|
| 3750 | n/a | return v; |
|---|
| 3751 | n/a | } |
|---|
| 3752 | n/a | PyTuple_SET_ITEM(tup, index, v); |
|---|
| 3753 | n/a | index++; |
|---|
| 3754 | n/a | } |
|---|
| 3755 | n/a | if (index == numretvals) |
|---|
| 3756 | n/a | break; |
|---|
| 3757 | n/a | } |
|---|
| 3758 | n/a | |
|---|
| 3759 | n/a | Py_DECREF(callargs); |
|---|
| 3760 | n/a | return tup; |
|---|
| 3761 | n/a | } |
|---|
| 3762 | n/a | |
|---|
| 3763 | n/a | static PyObject * |
|---|
| 3764 | n/a | PyCFuncPtr_call(PyCFuncPtrObject *self, PyObject *inargs, PyObject *kwds) |
|---|
| 3765 | n/a | { |
|---|
| 3766 | n/a | PyObject *restype; |
|---|
| 3767 | n/a | PyObject *converters; |
|---|
| 3768 | n/a | PyObject *checker; |
|---|
| 3769 | n/a | PyObject *argtypes; |
|---|
| 3770 | n/a | StgDictObject *dict = PyObject_stgdict((PyObject *)self); |
|---|
| 3771 | n/a | PyObject *result; |
|---|
| 3772 | n/a | PyObject *callargs; |
|---|
| 3773 | n/a | PyObject *errcheck; |
|---|
| 3774 | n/a | #ifdef MS_WIN32 |
|---|
| 3775 | n/a | IUnknown *piunk = NULL; |
|---|
| 3776 | n/a | #endif |
|---|
| 3777 | n/a | void *pProc = NULL; |
|---|
| 3778 | n/a | |
|---|
| 3779 | n/a | int inoutmask; |
|---|
| 3780 | n/a | int outmask; |
|---|
| 3781 | n/a | unsigned int numretvals; |
|---|
| 3782 | n/a | |
|---|
| 3783 | n/a | assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */ |
|---|
| 3784 | n/a | restype = self->restype ? self->restype : dict->restype; |
|---|
| 3785 | n/a | converters = self->converters ? self->converters : dict->converters; |
|---|
| 3786 | n/a | checker = self->checker ? self->checker : dict->checker; |
|---|
| 3787 | n/a | argtypes = self->argtypes ? self->argtypes : dict->argtypes; |
|---|
| 3788 | n/a | /* later, we probably want to have an errcheck field in stgdict */ |
|---|
| 3789 | n/a | errcheck = self->errcheck /* ? self->errcheck : dict->errcheck */; |
|---|
| 3790 | n/a | |
|---|
| 3791 | n/a | |
|---|
| 3792 | n/a | pProc = *(void **)self->b_ptr; |
|---|
| 3793 | n/a | #ifdef MS_WIN32 |
|---|
| 3794 | n/a | if (self->index) { |
|---|
| 3795 | n/a | /* It's a COM method */ |
|---|
| 3796 | n/a | CDataObject *this; |
|---|
| 3797 | n/a | this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */ |
|---|
| 3798 | n/a | if (!this) { |
|---|
| 3799 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 3800 | n/a | "native com method call without 'this' parameter"); |
|---|
| 3801 | n/a | return NULL; |
|---|
| 3802 | n/a | } |
|---|
| 3803 | n/a | if (!CDataObject_Check(this)) { |
|---|
| 3804 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 3805 | n/a | "Expected a COM this pointer as first argument"); |
|---|
| 3806 | n/a | return NULL; |
|---|
| 3807 | n/a | } |
|---|
| 3808 | n/a | /* there should be more checks? No, in Python */ |
|---|
| 3809 | n/a | /* First arg is a pointer to an interface instance */ |
|---|
| 3810 | n/a | if (!this->b_ptr || *(void **)this->b_ptr == NULL) { |
|---|
| 3811 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 3812 | n/a | "NULL COM pointer access"); |
|---|
| 3813 | n/a | return NULL; |
|---|
| 3814 | n/a | } |
|---|
| 3815 | n/a | piunk = *(IUnknown **)this->b_ptr; |
|---|
| 3816 | n/a | if (NULL == piunk->lpVtbl) { |
|---|
| 3817 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 3818 | n/a | "COM method call without VTable"); |
|---|
| 3819 | n/a | return NULL; |
|---|
| 3820 | n/a | } |
|---|
| 3821 | n/a | pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000]; |
|---|
| 3822 | n/a | } |
|---|
| 3823 | n/a | #endif |
|---|
| 3824 | n/a | callargs = _build_callargs(self, argtypes, |
|---|
| 3825 | n/a | inargs, kwds, |
|---|
| 3826 | n/a | &outmask, &inoutmask, &numretvals); |
|---|
| 3827 | n/a | if (callargs == NULL) |
|---|
| 3828 | n/a | return NULL; |
|---|
| 3829 | n/a | |
|---|
| 3830 | n/a | if (converters) { |
|---|
| 3831 | n/a | int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters), |
|---|
| 3832 | n/a | Py_ssize_t, int); |
|---|
| 3833 | n/a | int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs), |
|---|
| 3834 | n/a | Py_ssize_t, int); |
|---|
| 3835 | n/a | |
|---|
| 3836 | n/a | if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) { |
|---|
| 3837 | n/a | /* For cdecl functions, we allow more actual arguments |
|---|
| 3838 | n/a | than the length of the argtypes tuple. |
|---|
| 3839 | n/a | */ |
|---|
| 3840 | n/a | if (required > actual) { |
|---|
| 3841 | n/a | Py_DECREF(callargs); |
|---|
| 3842 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3843 | n/a | "this function takes at least %d argument%s (%d given)", |
|---|
| 3844 | n/a | required, |
|---|
| 3845 | n/a | required == 1 ? "" : "s", |
|---|
| 3846 | n/a | actual); |
|---|
| 3847 | n/a | return NULL; |
|---|
| 3848 | n/a | } |
|---|
| 3849 | n/a | } else if (required != actual) { |
|---|
| 3850 | n/a | Py_DECREF(callargs); |
|---|
| 3851 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 3852 | n/a | "this function takes %d argument%s (%d given)", |
|---|
| 3853 | n/a | required, |
|---|
| 3854 | n/a | required == 1 ? "" : "s", |
|---|
| 3855 | n/a | actual); |
|---|
| 3856 | n/a | return NULL; |
|---|
| 3857 | n/a | } |
|---|
| 3858 | n/a | } |
|---|
| 3859 | n/a | |
|---|
| 3860 | n/a | result = _ctypes_callproc(pProc, |
|---|
| 3861 | n/a | callargs, |
|---|
| 3862 | n/a | #ifdef MS_WIN32 |
|---|
| 3863 | n/a | piunk, |
|---|
| 3864 | n/a | self->iid, |
|---|
| 3865 | n/a | #endif |
|---|
| 3866 | n/a | dict->flags, |
|---|
| 3867 | n/a | converters, |
|---|
| 3868 | n/a | restype, |
|---|
| 3869 | n/a | checker); |
|---|
| 3870 | n/a | /* The 'errcheck' protocol */ |
|---|
| 3871 | n/a | if (result != NULL && errcheck) { |
|---|
| 3872 | n/a | PyObject *v = PyObject_CallFunctionObjArgs(errcheck, |
|---|
| 3873 | n/a | result, |
|---|
| 3874 | n/a | self, |
|---|
| 3875 | n/a | callargs, |
|---|
| 3876 | n/a | NULL); |
|---|
| 3877 | n/a | /* If the errcheck function failed, return NULL. |
|---|
| 3878 | n/a | If the errcheck function returned callargs unchanged, |
|---|
| 3879 | n/a | continue normal processing. |
|---|
| 3880 | n/a | If the errcheck function returned something else, |
|---|
| 3881 | n/a | use that as result. |
|---|
| 3882 | n/a | */ |
|---|
| 3883 | n/a | if (v == NULL || v != callargs) { |
|---|
| 3884 | n/a | Py_DECREF(result); |
|---|
| 3885 | n/a | Py_DECREF(callargs); |
|---|
| 3886 | n/a | return v; |
|---|
| 3887 | n/a | } |
|---|
| 3888 | n/a | Py_DECREF(v); |
|---|
| 3889 | n/a | } |
|---|
| 3890 | n/a | |
|---|
| 3891 | n/a | return _build_result(result, callargs, |
|---|
| 3892 | n/a | outmask, inoutmask, numretvals); |
|---|
| 3893 | n/a | } |
|---|
| 3894 | n/a | |
|---|
| 3895 | n/a | static int |
|---|
| 3896 | n/a | PyCFuncPtr_traverse(PyCFuncPtrObject *self, visitproc visit, void *arg) |
|---|
| 3897 | n/a | { |
|---|
| 3898 | n/a | Py_VISIT(self->callable); |
|---|
| 3899 | n/a | Py_VISIT(self->restype); |
|---|
| 3900 | n/a | Py_VISIT(self->checker); |
|---|
| 3901 | n/a | Py_VISIT(self->errcheck); |
|---|
| 3902 | n/a | Py_VISIT(self->argtypes); |
|---|
| 3903 | n/a | Py_VISIT(self->converters); |
|---|
| 3904 | n/a | Py_VISIT(self->paramflags); |
|---|
| 3905 | n/a | Py_VISIT(self->thunk); |
|---|
| 3906 | n/a | return PyCData_traverse((CDataObject *)self, visit, arg); |
|---|
| 3907 | n/a | } |
|---|
| 3908 | n/a | |
|---|
| 3909 | n/a | static int |
|---|
| 3910 | n/a | PyCFuncPtr_clear(PyCFuncPtrObject *self) |
|---|
| 3911 | n/a | { |
|---|
| 3912 | n/a | Py_CLEAR(self->callable); |
|---|
| 3913 | n/a | Py_CLEAR(self->restype); |
|---|
| 3914 | n/a | Py_CLEAR(self->checker); |
|---|
| 3915 | n/a | Py_CLEAR(self->errcheck); |
|---|
| 3916 | n/a | Py_CLEAR(self->argtypes); |
|---|
| 3917 | n/a | Py_CLEAR(self->converters); |
|---|
| 3918 | n/a | Py_CLEAR(self->paramflags); |
|---|
| 3919 | n/a | Py_CLEAR(self->thunk); |
|---|
| 3920 | n/a | return PyCData_clear((CDataObject *)self); |
|---|
| 3921 | n/a | } |
|---|
| 3922 | n/a | |
|---|
| 3923 | n/a | static void |
|---|
| 3924 | n/a | PyCFuncPtr_dealloc(PyCFuncPtrObject *self) |
|---|
| 3925 | n/a | { |
|---|
| 3926 | n/a | PyCFuncPtr_clear(self); |
|---|
| 3927 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 3928 | n/a | } |
|---|
| 3929 | n/a | |
|---|
| 3930 | n/a | static PyObject * |
|---|
| 3931 | n/a | PyCFuncPtr_repr(PyCFuncPtrObject *self) |
|---|
| 3932 | n/a | { |
|---|
| 3933 | n/a | #ifdef MS_WIN32 |
|---|
| 3934 | n/a | if (self->index) |
|---|
| 3935 | n/a | return PyUnicode_FromFormat("<COM method offset %d: %s at %p>", |
|---|
| 3936 | n/a | self->index - 0x1000, |
|---|
| 3937 | n/a | Py_TYPE(self)->tp_name, |
|---|
| 3938 | n/a | self); |
|---|
| 3939 | n/a | #endif |
|---|
| 3940 | n/a | return PyUnicode_FromFormat("<%s object at %p>", |
|---|
| 3941 | n/a | Py_TYPE(self)->tp_name, |
|---|
| 3942 | n/a | self); |
|---|
| 3943 | n/a | } |
|---|
| 3944 | n/a | |
|---|
| 3945 | n/a | static int |
|---|
| 3946 | n/a | PyCFuncPtr_bool(PyCFuncPtrObject *self) |
|---|
| 3947 | n/a | { |
|---|
| 3948 | n/a | return ((*(void **)self->b_ptr != NULL) |
|---|
| 3949 | n/a | #ifdef MS_WIN32 |
|---|
| 3950 | n/a | || (self->index != 0) |
|---|
| 3951 | n/a | #endif |
|---|
| 3952 | n/a | ); |
|---|
| 3953 | n/a | } |
|---|
| 3954 | n/a | |
|---|
| 3955 | n/a | static PyNumberMethods PyCFuncPtr_as_number = { |
|---|
| 3956 | n/a | 0, /* nb_add */ |
|---|
| 3957 | n/a | 0, /* nb_subtract */ |
|---|
| 3958 | n/a | 0, /* nb_multiply */ |
|---|
| 3959 | n/a | 0, /* nb_remainder */ |
|---|
| 3960 | n/a | 0, /* nb_divmod */ |
|---|
| 3961 | n/a | 0, /* nb_power */ |
|---|
| 3962 | n/a | 0, /* nb_negative */ |
|---|
| 3963 | n/a | 0, /* nb_positive */ |
|---|
| 3964 | n/a | 0, /* nb_absolute */ |
|---|
| 3965 | n/a | (inquiry)PyCFuncPtr_bool, /* nb_bool */ |
|---|
| 3966 | n/a | }; |
|---|
| 3967 | n/a | |
|---|
| 3968 | n/a | PyTypeObject PyCFuncPtr_Type = { |
|---|
| 3969 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 3970 | n/a | "_ctypes.PyCFuncPtr", |
|---|
| 3971 | n/a | sizeof(PyCFuncPtrObject), /* tp_basicsize */ |
|---|
| 3972 | n/a | 0, /* tp_itemsize */ |
|---|
| 3973 | n/a | (destructor)PyCFuncPtr_dealloc, /* tp_dealloc */ |
|---|
| 3974 | n/a | 0, /* tp_print */ |
|---|
| 3975 | n/a | 0, /* tp_getattr */ |
|---|
| 3976 | n/a | 0, /* tp_setattr */ |
|---|
| 3977 | n/a | 0, /* tp_reserved */ |
|---|
| 3978 | n/a | (reprfunc)PyCFuncPtr_repr, /* tp_repr */ |
|---|
| 3979 | n/a | &PyCFuncPtr_as_number, /* tp_as_number */ |
|---|
| 3980 | n/a | 0, /* tp_as_sequence */ |
|---|
| 3981 | n/a | 0, /* tp_as_mapping */ |
|---|
| 3982 | n/a | 0, /* tp_hash */ |
|---|
| 3983 | n/a | (ternaryfunc)PyCFuncPtr_call, /* tp_call */ |
|---|
| 3984 | n/a | 0, /* tp_str */ |
|---|
| 3985 | n/a | 0, /* tp_getattro */ |
|---|
| 3986 | n/a | 0, /* tp_setattro */ |
|---|
| 3987 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 3988 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 3989 | n/a | "Function Pointer", /* tp_doc */ |
|---|
| 3990 | n/a | (traverseproc)PyCFuncPtr_traverse, /* tp_traverse */ |
|---|
| 3991 | n/a | (inquiry)PyCFuncPtr_clear, /* tp_clear */ |
|---|
| 3992 | n/a | 0, /* tp_richcompare */ |
|---|
| 3993 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 3994 | n/a | 0, /* tp_iter */ |
|---|
| 3995 | n/a | 0, /* tp_iternext */ |
|---|
| 3996 | n/a | 0, /* tp_methods */ |
|---|
| 3997 | n/a | 0, /* tp_members */ |
|---|
| 3998 | n/a | PyCFuncPtr_getsets, /* tp_getset */ |
|---|
| 3999 | n/a | 0, /* tp_base */ |
|---|
| 4000 | n/a | 0, /* tp_dict */ |
|---|
| 4001 | n/a | 0, /* tp_descr_get */ |
|---|
| 4002 | n/a | 0, /* tp_descr_set */ |
|---|
| 4003 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4004 | n/a | 0, /* tp_init */ |
|---|
| 4005 | n/a | 0, /* tp_alloc */ |
|---|
| 4006 | n/a | PyCFuncPtr_new, /* tp_new */ |
|---|
| 4007 | n/a | 0, /* tp_free */ |
|---|
| 4008 | n/a | }; |
|---|
| 4009 | n/a | |
|---|
| 4010 | n/a | /*****************************************************************/ |
|---|
| 4011 | n/a | /* |
|---|
| 4012 | n/a | Struct_Type |
|---|
| 4013 | n/a | */ |
|---|
| 4014 | n/a | /* |
|---|
| 4015 | n/a | This function is called to initialize a Structure or Union with positional |
|---|
| 4016 | n/a | arguments. It calls itself recursively for all Structure or Union base |
|---|
| 4017 | n/a | classes, then retrieves the _fields_ member to associate the argument |
|---|
| 4018 | n/a | position with the correct field name. |
|---|
| 4019 | n/a | |
|---|
| 4020 | n/a | Returns -1 on error, or the index of next argument on success. |
|---|
| 4021 | n/a | */ |
|---|
| 4022 | n/a | static Py_ssize_t |
|---|
| 4023 | n/a | _init_pos_args(PyObject *self, PyTypeObject *type, |
|---|
| 4024 | n/a | PyObject *args, PyObject *kwds, |
|---|
| 4025 | n/a | Py_ssize_t index) |
|---|
| 4026 | n/a | { |
|---|
| 4027 | n/a | StgDictObject *dict; |
|---|
| 4028 | n/a | PyObject *fields; |
|---|
| 4029 | n/a | Py_ssize_t i; |
|---|
| 4030 | n/a | |
|---|
| 4031 | n/a | if (PyType_stgdict((PyObject *)type->tp_base)) { |
|---|
| 4032 | n/a | index = _init_pos_args(self, type->tp_base, |
|---|
| 4033 | n/a | args, kwds, |
|---|
| 4034 | n/a | index); |
|---|
| 4035 | n/a | if (index == -1) |
|---|
| 4036 | n/a | return -1; |
|---|
| 4037 | n/a | } |
|---|
| 4038 | n/a | |
|---|
| 4039 | n/a | dict = PyType_stgdict((PyObject *)type); |
|---|
| 4040 | n/a | fields = PyDict_GetItemString((PyObject *)dict, "_fields_"); |
|---|
| 4041 | n/a | if (fields == NULL) |
|---|
| 4042 | n/a | return index; |
|---|
| 4043 | n/a | |
|---|
| 4044 | n/a | for (i = 0; |
|---|
| 4045 | n/a | i < dict->length && (i+index) < PyTuple_GET_SIZE(args); |
|---|
| 4046 | n/a | ++i) { |
|---|
| 4047 | n/a | PyObject *pair = PySequence_GetItem(fields, i); |
|---|
| 4048 | n/a | PyObject *name, *val; |
|---|
| 4049 | n/a | int res; |
|---|
| 4050 | n/a | if (!pair) |
|---|
| 4051 | n/a | return -1; |
|---|
| 4052 | n/a | name = PySequence_GetItem(pair, 0); |
|---|
| 4053 | n/a | if (!name) { |
|---|
| 4054 | n/a | Py_DECREF(pair); |
|---|
| 4055 | n/a | return -1; |
|---|
| 4056 | n/a | } |
|---|
| 4057 | n/a | val = PyTuple_GET_ITEM(args, i + index); |
|---|
| 4058 | n/a | if (kwds && PyDict_GetItem(kwds, name)) { |
|---|
| 4059 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4060 | n/a | "duplicate values for field %R", |
|---|
| 4061 | n/a | name); |
|---|
| 4062 | n/a | Py_DECREF(pair); |
|---|
| 4063 | n/a | Py_DECREF(name); |
|---|
| 4064 | n/a | return -1; |
|---|
| 4065 | n/a | } |
|---|
| 4066 | n/a | |
|---|
| 4067 | n/a | res = PyObject_SetAttr(self, name, val); |
|---|
| 4068 | n/a | Py_DECREF(pair); |
|---|
| 4069 | n/a | Py_DECREF(name); |
|---|
| 4070 | n/a | if (res == -1) |
|---|
| 4071 | n/a | return -1; |
|---|
| 4072 | n/a | } |
|---|
| 4073 | n/a | return index + dict->length; |
|---|
| 4074 | n/a | } |
|---|
| 4075 | n/a | |
|---|
| 4076 | n/a | static int |
|---|
| 4077 | n/a | Struct_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 4078 | n/a | { |
|---|
| 4079 | n/a | /* Optimization possible: Store the attribute names _fields_[x][0] |
|---|
| 4080 | n/a | * in C accessible fields somewhere ? |
|---|
| 4081 | n/a | */ |
|---|
| 4082 | n/a | if (!PyTuple_Check(args)) { |
|---|
| 4083 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4084 | n/a | "args not a tuple?"); |
|---|
| 4085 | n/a | return -1; |
|---|
| 4086 | n/a | } |
|---|
| 4087 | n/a | if (PyTuple_GET_SIZE(args)) { |
|---|
| 4088 | n/a | Py_ssize_t res = _init_pos_args(self, Py_TYPE(self), |
|---|
| 4089 | n/a | args, kwds, 0); |
|---|
| 4090 | n/a | if (res == -1) |
|---|
| 4091 | n/a | return -1; |
|---|
| 4092 | n/a | if (res < PyTuple_GET_SIZE(args)) { |
|---|
| 4093 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4094 | n/a | "too many initializers"); |
|---|
| 4095 | n/a | return -1; |
|---|
| 4096 | n/a | } |
|---|
| 4097 | n/a | } |
|---|
| 4098 | n/a | |
|---|
| 4099 | n/a | if (kwds) { |
|---|
| 4100 | n/a | PyObject *key, *value; |
|---|
| 4101 | n/a | Py_ssize_t pos = 0; |
|---|
| 4102 | n/a | while(PyDict_Next(kwds, &pos, &key, &value)) { |
|---|
| 4103 | n/a | if (-1 == PyObject_SetAttr(self, key, value)) |
|---|
| 4104 | n/a | return -1; |
|---|
| 4105 | n/a | } |
|---|
| 4106 | n/a | } |
|---|
| 4107 | n/a | return 0; |
|---|
| 4108 | n/a | } |
|---|
| 4109 | n/a | |
|---|
| 4110 | n/a | static PyTypeObject Struct_Type = { |
|---|
| 4111 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4112 | n/a | "_ctypes.Structure", |
|---|
| 4113 | n/a | sizeof(CDataObject), /* tp_basicsize */ |
|---|
| 4114 | n/a | 0, /* tp_itemsize */ |
|---|
| 4115 | n/a | 0, /* tp_dealloc */ |
|---|
| 4116 | n/a | 0, /* tp_print */ |
|---|
| 4117 | n/a | 0, /* tp_getattr */ |
|---|
| 4118 | n/a | 0, /* tp_setattr */ |
|---|
| 4119 | n/a | 0, /* tp_reserved */ |
|---|
| 4120 | n/a | 0, /* tp_repr */ |
|---|
| 4121 | n/a | 0, /* tp_as_number */ |
|---|
| 4122 | n/a | 0, /* tp_as_sequence */ |
|---|
| 4123 | n/a | 0, /* tp_as_mapping */ |
|---|
| 4124 | n/a | 0, /* tp_hash */ |
|---|
| 4125 | n/a | 0, /* tp_call */ |
|---|
| 4126 | n/a | 0, /* tp_str */ |
|---|
| 4127 | n/a | 0, /* tp_getattro */ |
|---|
| 4128 | n/a | 0, /* tp_setattro */ |
|---|
| 4129 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 4130 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 4131 | n/a | "Structure base class", /* tp_doc */ |
|---|
| 4132 | n/a | (traverseproc)PyCData_traverse, /* tp_traverse */ |
|---|
| 4133 | n/a | (inquiry)PyCData_clear, /* tp_clear */ |
|---|
| 4134 | n/a | 0, /* tp_richcompare */ |
|---|
| 4135 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4136 | n/a | 0, /* tp_iter */ |
|---|
| 4137 | n/a | 0, /* tp_iternext */ |
|---|
| 4138 | n/a | 0, /* tp_methods */ |
|---|
| 4139 | n/a | 0, /* tp_members */ |
|---|
| 4140 | n/a | 0, /* tp_getset */ |
|---|
| 4141 | n/a | 0, /* tp_base */ |
|---|
| 4142 | n/a | 0, /* tp_dict */ |
|---|
| 4143 | n/a | 0, /* tp_descr_get */ |
|---|
| 4144 | n/a | 0, /* tp_descr_set */ |
|---|
| 4145 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4146 | n/a | Struct_init, /* tp_init */ |
|---|
| 4147 | n/a | 0, /* tp_alloc */ |
|---|
| 4148 | n/a | GenericPyCData_new, /* tp_new */ |
|---|
| 4149 | n/a | 0, /* tp_free */ |
|---|
| 4150 | n/a | }; |
|---|
| 4151 | n/a | |
|---|
| 4152 | n/a | static PyTypeObject Union_Type = { |
|---|
| 4153 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4154 | n/a | "_ctypes.Union", |
|---|
| 4155 | n/a | sizeof(CDataObject), /* tp_basicsize */ |
|---|
| 4156 | n/a | 0, /* tp_itemsize */ |
|---|
| 4157 | n/a | 0, /* tp_dealloc */ |
|---|
| 4158 | n/a | 0, /* tp_print */ |
|---|
| 4159 | n/a | 0, /* tp_getattr */ |
|---|
| 4160 | n/a | 0, /* tp_setattr */ |
|---|
| 4161 | n/a | 0, /* tp_reserved */ |
|---|
| 4162 | n/a | 0, /* tp_repr */ |
|---|
| 4163 | n/a | 0, /* tp_as_number */ |
|---|
| 4164 | n/a | 0, /* tp_as_sequence */ |
|---|
| 4165 | n/a | 0, /* tp_as_mapping */ |
|---|
| 4166 | n/a | 0, /* tp_hash */ |
|---|
| 4167 | n/a | 0, /* tp_call */ |
|---|
| 4168 | n/a | 0, /* tp_str */ |
|---|
| 4169 | n/a | 0, /* tp_getattro */ |
|---|
| 4170 | n/a | 0, /* tp_setattro */ |
|---|
| 4171 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 4172 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 4173 | n/a | "Union base class", /* tp_doc */ |
|---|
| 4174 | n/a | (traverseproc)PyCData_traverse, /* tp_traverse */ |
|---|
| 4175 | n/a | (inquiry)PyCData_clear, /* tp_clear */ |
|---|
| 4176 | n/a | 0, /* tp_richcompare */ |
|---|
| 4177 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4178 | n/a | 0, /* tp_iter */ |
|---|
| 4179 | n/a | 0, /* tp_iternext */ |
|---|
| 4180 | n/a | 0, /* tp_methods */ |
|---|
| 4181 | n/a | 0, /* tp_members */ |
|---|
| 4182 | n/a | 0, /* tp_getset */ |
|---|
| 4183 | n/a | 0, /* tp_base */ |
|---|
| 4184 | n/a | 0, /* tp_dict */ |
|---|
| 4185 | n/a | 0, /* tp_descr_get */ |
|---|
| 4186 | n/a | 0, /* tp_descr_set */ |
|---|
| 4187 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4188 | n/a | Struct_init, /* tp_init */ |
|---|
| 4189 | n/a | 0, /* tp_alloc */ |
|---|
| 4190 | n/a | GenericPyCData_new, /* tp_new */ |
|---|
| 4191 | n/a | 0, /* tp_free */ |
|---|
| 4192 | n/a | }; |
|---|
| 4193 | n/a | |
|---|
| 4194 | n/a | |
|---|
| 4195 | n/a | /******************************************************************/ |
|---|
| 4196 | n/a | /* |
|---|
| 4197 | n/a | PyCArray_Type |
|---|
| 4198 | n/a | */ |
|---|
| 4199 | n/a | static int |
|---|
| 4200 | n/a | Array_init(CDataObject *self, PyObject *args, PyObject *kw) |
|---|
| 4201 | n/a | { |
|---|
| 4202 | n/a | Py_ssize_t i; |
|---|
| 4203 | n/a | Py_ssize_t n; |
|---|
| 4204 | n/a | |
|---|
| 4205 | n/a | if (!PyTuple_Check(args)) { |
|---|
| 4206 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4207 | n/a | "args not a tuple?"); |
|---|
| 4208 | n/a | return -1; |
|---|
| 4209 | n/a | } |
|---|
| 4210 | n/a | n = PyTuple_GET_SIZE(args); |
|---|
| 4211 | n/a | for (i = 0; i < n; ++i) { |
|---|
| 4212 | n/a | PyObject *v; |
|---|
| 4213 | n/a | v = PyTuple_GET_ITEM(args, i); |
|---|
| 4214 | n/a | if (-1 == PySequence_SetItem((PyObject *)self, i, v)) |
|---|
| 4215 | n/a | return -1; |
|---|
| 4216 | n/a | } |
|---|
| 4217 | n/a | return 0; |
|---|
| 4218 | n/a | } |
|---|
| 4219 | n/a | |
|---|
| 4220 | n/a | static PyObject * |
|---|
| 4221 | n/a | Array_item(PyObject *myself, Py_ssize_t index) |
|---|
| 4222 | n/a | { |
|---|
| 4223 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4224 | n/a | Py_ssize_t offset, size; |
|---|
| 4225 | n/a | StgDictObject *stgdict; |
|---|
| 4226 | n/a | |
|---|
| 4227 | n/a | |
|---|
| 4228 | n/a | if (index < 0 || index >= self->b_length) { |
|---|
| 4229 | n/a | PyErr_SetString(PyExc_IndexError, |
|---|
| 4230 | n/a | "invalid index"); |
|---|
| 4231 | n/a | return NULL; |
|---|
| 4232 | n/a | } |
|---|
| 4233 | n/a | |
|---|
| 4234 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4235 | n/a | assert(stgdict); /* Cannot be NULL for array instances */ |
|---|
| 4236 | n/a | /* Would it be clearer if we got the item size from |
|---|
| 4237 | n/a | stgdict->proto's stgdict? |
|---|
| 4238 | n/a | */ |
|---|
| 4239 | n/a | size = stgdict->size / stgdict->length; |
|---|
| 4240 | n/a | offset = index * size; |
|---|
| 4241 | n/a | |
|---|
| 4242 | n/a | return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self, |
|---|
| 4243 | n/a | index, size, self->b_ptr + offset); |
|---|
| 4244 | n/a | } |
|---|
| 4245 | n/a | |
|---|
| 4246 | n/a | static PyObject * |
|---|
| 4247 | n/a | Array_subscript(PyObject *myself, PyObject *item) |
|---|
| 4248 | n/a | { |
|---|
| 4249 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4250 | n/a | |
|---|
| 4251 | n/a | if (PyIndex_Check(item)) { |
|---|
| 4252 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|---|
| 4253 | n/a | |
|---|
| 4254 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 4255 | n/a | return NULL; |
|---|
| 4256 | n/a | if (i < 0) |
|---|
| 4257 | n/a | i += self->b_length; |
|---|
| 4258 | n/a | return Array_item(myself, i); |
|---|
| 4259 | n/a | } |
|---|
| 4260 | n/a | else if (PySlice_Check(item)) { |
|---|
| 4261 | n/a | StgDictObject *stgdict, *itemdict; |
|---|
| 4262 | n/a | PyObject *proto; |
|---|
| 4263 | n/a | PyObject *np; |
|---|
| 4264 | n/a | Py_ssize_t start, stop, step, slicelen, cur, i; |
|---|
| 4265 | n/a | |
|---|
| 4266 | n/a | if (PySlice_GetIndicesEx(item, |
|---|
| 4267 | n/a | self->b_length, &start, &stop, |
|---|
| 4268 | n/a | &step, &slicelen) < 0) { |
|---|
| 4269 | n/a | return NULL; |
|---|
| 4270 | n/a | } |
|---|
| 4271 | n/a | |
|---|
| 4272 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4273 | n/a | assert(stgdict); /* Cannot be NULL for array object instances */ |
|---|
| 4274 | n/a | proto = stgdict->proto; |
|---|
| 4275 | n/a | itemdict = PyType_stgdict(proto); |
|---|
| 4276 | n/a | assert(itemdict); /* proto is the item type of the array, a |
|---|
| 4277 | n/a | ctypes type, so this cannot be NULL */ |
|---|
| 4278 | n/a | |
|---|
| 4279 | n/a | if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
|---|
| 4280 | n/a | char *ptr = (char *)self->b_ptr; |
|---|
| 4281 | n/a | char *dest; |
|---|
| 4282 | n/a | |
|---|
| 4283 | n/a | if (slicelen <= 0) |
|---|
| 4284 | n/a | return PyBytes_FromStringAndSize("", 0); |
|---|
| 4285 | n/a | if (step == 1) { |
|---|
| 4286 | n/a | return PyBytes_FromStringAndSize(ptr + start, |
|---|
| 4287 | n/a | slicelen); |
|---|
| 4288 | n/a | } |
|---|
| 4289 | n/a | dest = (char *)PyMem_Malloc(slicelen); |
|---|
| 4290 | n/a | |
|---|
| 4291 | n/a | if (dest == NULL) |
|---|
| 4292 | n/a | return PyErr_NoMemory(); |
|---|
| 4293 | n/a | |
|---|
| 4294 | n/a | for (cur = start, i = 0; i < slicelen; |
|---|
| 4295 | n/a | cur += step, i++) { |
|---|
| 4296 | n/a | dest[i] = ptr[cur]; |
|---|
| 4297 | n/a | } |
|---|
| 4298 | n/a | |
|---|
| 4299 | n/a | np = PyBytes_FromStringAndSize(dest, slicelen); |
|---|
| 4300 | n/a | PyMem_Free(dest); |
|---|
| 4301 | n/a | return np; |
|---|
| 4302 | n/a | } |
|---|
| 4303 | n/a | #ifdef CTYPES_UNICODE |
|---|
| 4304 | n/a | if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
|---|
| 4305 | n/a | wchar_t *ptr = (wchar_t *)self->b_ptr; |
|---|
| 4306 | n/a | wchar_t *dest; |
|---|
| 4307 | n/a | |
|---|
| 4308 | n/a | if (slicelen <= 0) |
|---|
| 4309 | n/a | return PyUnicode_New(0, 0); |
|---|
| 4310 | n/a | if (step == 1) { |
|---|
| 4311 | n/a | return PyUnicode_FromWideChar(ptr + start, |
|---|
| 4312 | n/a | slicelen); |
|---|
| 4313 | n/a | } |
|---|
| 4314 | n/a | |
|---|
| 4315 | n/a | dest = PyMem_New(wchar_t, slicelen); |
|---|
| 4316 | n/a | if (dest == NULL) { |
|---|
| 4317 | n/a | PyErr_NoMemory(); |
|---|
| 4318 | n/a | return NULL; |
|---|
| 4319 | n/a | } |
|---|
| 4320 | n/a | |
|---|
| 4321 | n/a | for (cur = start, i = 0; i < slicelen; |
|---|
| 4322 | n/a | cur += step, i++) { |
|---|
| 4323 | n/a | dest[i] = ptr[cur]; |
|---|
| 4324 | n/a | } |
|---|
| 4325 | n/a | |
|---|
| 4326 | n/a | np = PyUnicode_FromWideChar(dest, slicelen); |
|---|
| 4327 | n/a | PyMem_Free(dest); |
|---|
| 4328 | n/a | return np; |
|---|
| 4329 | n/a | } |
|---|
| 4330 | n/a | #endif |
|---|
| 4331 | n/a | |
|---|
| 4332 | n/a | np = PyList_New(slicelen); |
|---|
| 4333 | n/a | if (np == NULL) |
|---|
| 4334 | n/a | return NULL; |
|---|
| 4335 | n/a | |
|---|
| 4336 | n/a | for (cur = start, i = 0; i < slicelen; |
|---|
| 4337 | n/a | cur += step, i++) { |
|---|
| 4338 | n/a | PyObject *v = Array_item(myself, cur); |
|---|
| 4339 | n/a | if (v == NULL) { |
|---|
| 4340 | n/a | Py_DECREF(np); |
|---|
| 4341 | n/a | return NULL; |
|---|
| 4342 | n/a | } |
|---|
| 4343 | n/a | PyList_SET_ITEM(np, i, v); |
|---|
| 4344 | n/a | } |
|---|
| 4345 | n/a | return np; |
|---|
| 4346 | n/a | } |
|---|
| 4347 | n/a | else { |
|---|
| 4348 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4349 | n/a | "indices must be integers"); |
|---|
| 4350 | n/a | return NULL; |
|---|
| 4351 | n/a | } |
|---|
| 4352 | n/a | |
|---|
| 4353 | n/a | } |
|---|
| 4354 | n/a | |
|---|
| 4355 | n/a | static int |
|---|
| 4356 | n/a | Array_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value) |
|---|
| 4357 | n/a | { |
|---|
| 4358 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4359 | n/a | Py_ssize_t size, offset; |
|---|
| 4360 | n/a | StgDictObject *stgdict; |
|---|
| 4361 | n/a | char *ptr; |
|---|
| 4362 | n/a | |
|---|
| 4363 | n/a | if (value == NULL) { |
|---|
| 4364 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4365 | n/a | "Array does not support item deletion"); |
|---|
| 4366 | n/a | return -1; |
|---|
| 4367 | n/a | } |
|---|
| 4368 | n/a | |
|---|
| 4369 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4370 | n/a | assert(stgdict); /* Cannot be NULL for array object instances */ |
|---|
| 4371 | n/a | if (index < 0 || index >= stgdict->length) { |
|---|
| 4372 | n/a | PyErr_SetString(PyExc_IndexError, |
|---|
| 4373 | n/a | "invalid index"); |
|---|
| 4374 | n/a | return -1; |
|---|
| 4375 | n/a | } |
|---|
| 4376 | n/a | size = stgdict->size / stgdict->length; |
|---|
| 4377 | n/a | offset = index * size; |
|---|
| 4378 | n/a | ptr = self->b_ptr + offset; |
|---|
| 4379 | n/a | |
|---|
| 4380 | n/a | return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value, |
|---|
| 4381 | n/a | index, size, ptr); |
|---|
| 4382 | n/a | } |
|---|
| 4383 | n/a | |
|---|
| 4384 | n/a | static int |
|---|
| 4385 | n/a | Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value) |
|---|
| 4386 | n/a | { |
|---|
| 4387 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4388 | n/a | |
|---|
| 4389 | n/a | if (value == NULL) { |
|---|
| 4390 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4391 | n/a | "Array does not support item deletion"); |
|---|
| 4392 | n/a | return -1; |
|---|
| 4393 | n/a | } |
|---|
| 4394 | n/a | |
|---|
| 4395 | n/a | if (PyIndex_Check(item)) { |
|---|
| 4396 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|---|
| 4397 | n/a | |
|---|
| 4398 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 4399 | n/a | return -1; |
|---|
| 4400 | n/a | if (i < 0) |
|---|
| 4401 | n/a | i += self->b_length; |
|---|
| 4402 | n/a | return Array_ass_item(myself, i, value); |
|---|
| 4403 | n/a | } |
|---|
| 4404 | n/a | else if (PySlice_Check(item)) { |
|---|
| 4405 | n/a | Py_ssize_t start, stop, step, slicelen, otherlen, i, cur; |
|---|
| 4406 | n/a | |
|---|
| 4407 | n/a | if (PySlice_GetIndicesEx(item, |
|---|
| 4408 | n/a | self->b_length, &start, &stop, |
|---|
| 4409 | n/a | &step, &slicelen) < 0) { |
|---|
| 4410 | n/a | return -1; |
|---|
| 4411 | n/a | } |
|---|
| 4412 | n/a | if ((step < 0 && start < stop) || |
|---|
| 4413 | n/a | (step > 0 && start > stop)) |
|---|
| 4414 | n/a | stop = start; |
|---|
| 4415 | n/a | |
|---|
| 4416 | n/a | otherlen = PySequence_Length(value); |
|---|
| 4417 | n/a | if (otherlen != slicelen) { |
|---|
| 4418 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4419 | n/a | "Can only assign sequence of same size"); |
|---|
| 4420 | n/a | return -1; |
|---|
| 4421 | n/a | } |
|---|
| 4422 | n/a | for (cur = start, i = 0; i < otherlen; cur += step, i++) { |
|---|
| 4423 | n/a | PyObject *item = PySequence_GetItem(value, i); |
|---|
| 4424 | n/a | int result; |
|---|
| 4425 | n/a | if (item == NULL) |
|---|
| 4426 | n/a | return -1; |
|---|
| 4427 | n/a | result = Array_ass_item(myself, cur, item); |
|---|
| 4428 | n/a | Py_DECREF(item); |
|---|
| 4429 | n/a | if (result == -1) |
|---|
| 4430 | n/a | return -1; |
|---|
| 4431 | n/a | } |
|---|
| 4432 | n/a | return 0; |
|---|
| 4433 | n/a | } |
|---|
| 4434 | n/a | else { |
|---|
| 4435 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4436 | n/a | "indices must be integer"); |
|---|
| 4437 | n/a | return -1; |
|---|
| 4438 | n/a | } |
|---|
| 4439 | n/a | } |
|---|
| 4440 | n/a | |
|---|
| 4441 | n/a | static Py_ssize_t |
|---|
| 4442 | n/a | Array_length(PyObject *myself) |
|---|
| 4443 | n/a | { |
|---|
| 4444 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4445 | n/a | return self->b_length; |
|---|
| 4446 | n/a | } |
|---|
| 4447 | n/a | |
|---|
| 4448 | n/a | static PySequenceMethods Array_as_sequence = { |
|---|
| 4449 | n/a | Array_length, /* sq_length; */ |
|---|
| 4450 | n/a | 0, /* sq_concat; */ |
|---|
| 4451 | n/a | 0, /* sq_repeat; */ |
|---|
| 4452 | n/a | Array_item, /* sq_item; */ |
|---|
| 4453 | n/a | 0, /* sq_slice; */ |
|---|
| 4454 | n/a | Array_ass_item, /* sq_ass_item; */ |
|---|
| 4455 | n/a | 0, /* sq_ass_slice; */ |
|---|
| 4456 | n/a | 0, /* sq_contains; */ |
|---|
| 4457 | n/a | |
|---|
| 4458 | n/a | 0, /* sq_inplace_concat; */ |
|---|
| 4459 | n/a | 0, /* sq_inplace_repeat; */ |
|---|
| 4460 | n/a | }; |
|---|
| 4461 | n/a | |
|---|
| 4462 | n/a | static PyMappingMethods Array_as_mapping = { |
|---|
| 4463 | n/a | Array_length, |
|---|
| 4464 | n/a | Array_subscript, |
|---|
| 4465 | n/a | Array_ass_subscript, |
|---|
| 4466 | n/a | }; |
|---|
| 4467 | n/a | |
|---|
| 4468 | n/a | PyTypeObject PyCArray_Type = { |
|---|
| 4469 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4470 | n/a | "_ctypes.Array", |
|---|
| 4471 | n/a | sizeof(CDataObject), /* tp_basicsize */ |
|---|
| 4472 | n/a | 0, /* tp_itemsize */ |
|---|
| 4473 | n/a | 0, /* tp_dealloc */ |
|---|
| 4474 | n/a | 0, /* tp_print */ |
|---|
| 4475 | n/a | 0, /* tp_getattr */ |
|---|
| 4476 | n/a | 0, /* tp_setattr */ |
|---|
| 4477 | n/a | 0, /* tp_reserved */ |
|---|
| 4478 | n/a | 0, /* tp_repr */ |
|---|
| 4479 | n/a | 0, /* tp_as_number */ |
|---|
| 4480 | n/a | &Array_as_sequence, /* tp_as_sequence */ |
|---|
| 4481 | n/a | &Array_as_mapping, /* tp_as_mapping */ |
|---|
| 4482 | n/a | 0, /* tp_hash */ |
|---|
| 4483 | n/a | 0, /* tp_call */ |
|---|
| 4484 | n/a | 0, /* tp_str */ |
|---|
| 4485 | n/a | 0, /* tp_getattro */ |
|---|
| 4486 | n/a | 0, /* tp_setattro */ |
|---|
| 4487 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 4488 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 4489 | n/a | "XXX to be provided", /* tp_doc */ |
|---|
| 4490 | n/a | (traverseproc)PyCData_traverse, /* tp_traverse */ |
|---|
| 4491 | n/a | (inquiry)PyCData_clear, /* tp_clear */ |
|---|
| 4492 | n/a | 0, /* tp_richcompare */ |
|---|
| 4493 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4494 | n/a | 0, /* tp_iter */ |
|---|
| 4495 | n/a | 0, /* tp_iternext */ |
|---|
| 4496 | n/a | 0, /* tp_methods */ |
|---|
| 4497 | n/a | 0, /* tp_members */ |
|---|
| 4498 | n/a | 0, /* tp_getset */ |
|---|
| 4499 | n/a | 0, /* tp_base */ |
|---|
| 4500 | n/a | 0, /* tp_dict */ |
|---|
| 4501 | n/a | 0, /* tp_descr_get */ |
|---|
| 4502 | n/a | 0, /* tp_descr_set */ |
|---|
| 4503 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4504 | n/a | (initproc)Array_init, /* tp_init */ |
|---|
| 4505 | n/a | 0, /* tp_alloc */ |
|---|
| 4506 | n/a | GenericPyCData_new, /* tp_new */ |
|---|
| 4507 | n/a | 0, /* tp_free */ |
|---|
| 4508 | n/a | }; |
|---|
| 4509 | n/a | |
|---|
| 4510 | n/a | PyObject * |
|---|
| 4511 | n/a | PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length) |
|---|
| 4512 | n/a | { |
|---|
| 4513 | n/a | static PyObject *cache; |
|---|
| 4514 | n/a | PyObject *key; |
|---|
| 4515 | n/a | PyObject *result; |
|---|
| 4516 | n/a | char name[256]; |
|---|
| 4517 | n/a | PyObject *len; |
|---|
| 4518 | n/a | |
|---|
| 4519 | n/a | if (cache == NULL) { |
|---|
| 4520 | n/a | cache = PyDict_New(); |
|---|
| 4521 | n/a | if (cache == NULL) |
|---|
| 4522 | n/a | return NULL; |
|---|
| 4523 | n/a | } |
|---|
| 4524 | n/a | len = PyLong_FromSsize_t(length); |
|---|
| 4525 | n/a | if (len == NULL) |
|---|
| 4526 | n/a | return NULL; |
|---|
| 4527 | n/a | key = PyTuple_Pack(2, itemtype, len); |
|---|
| 4528 | n/a | Py_DECREF(len); |
|---|
| 4529 | n/a | if (!key) |
|---|
| 4530 | n/a | return NULL; |
|---|
| 4531 | n/a | result = PyDict_GetItemProxy(cache, key); |
|---|
| 4532 | n/a | if (result) { |
|---|
| 4533 | n/a | Py_INCREF(result); |
|---|
| 4534 | n/a | Py_DECREF(key); |
|---|
| 4535 | n/a | return result; |
|---|
| 4536 | n/a | } |
|---|
| 4537 | n/a | |
|---|
| 4538 | n/a | if (!PyType_Check(itemtype)) { |
|---|
| 4539 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4540 | n/a | "Expected a type object"); |
|---|
| 4541 | n/a | Py_DECREF(key); |
|---|
| 4542 | n/a | return NULL; |
|---|
| 4543 | n/a | } |
|---|
| 4544 | n/a | #ifdef MS_WIN64 |
|---|
| 4545 | n/a | sprintf(name, "%.200s_Array_%Id", |
|---|
| 4546 | n/a | ((PyTypeObject *)itemtype)->tp_name, length); |
|---|
| 4547 | n/a | #else |
|---|
| 4548 | n/a | sprintf(name, "%.200s_Array_%ld", |
|---|
| 4549 | n/a | ((PyTypeObject *)itemtype)->tp_name, (long)length); |
|---|
| 4550 | n/a | #endif |
|---|
| 4551 | n/a | |
|---|
| 4552 | n/a | result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type, |
|---|
| 4553 | n/a | "s(O){s:n,s:O}", |
|---|
| 4554 | n/a | name, |
|---|
| 4555 | n/a | &PyCArray_Type, |
|---|
| 4556 | n/a | "_length_", |
|---|
| 4557 | n/a | length, |
|---|
| 4558 | n/a | "_type_", |
|---|
| 4559 | n/a | itemtype |
|---|
| 4560 | n/a | ); |
|---|
| 4561 | n/a | if (result == NULL) { |
|---|
| 4562 | n/a | Py_DECREF(key); |
|---|
| 4563 | n/a | return NULL; |
|---|
| 4564 | n/a | } |
|---|
| 4565 | n/a | if (-1 == PyDict_SetItemProxy(cache, key, result)) { |
|---|
| 4566 | n/a | Py_DECREF(key); |
|---|
| 4567 | n/a | Py_DECREF(result); |
|---|
| 4568 | n/a | return NULL; |
|---|
| 4569 | n/a | } |
|---|
| 4570 | n/a | Py_DECREF(key); |
|---|
| 4571 | n/a | return result; |
|---|
| 4572 | n/a | } |
|---|
| 4573 | n/a | |
|---|
| 4574 | n/a | |
|---|
| 4575 | n/a | /******************************************************************/ |
|---|
| 4576 | n/a | /* |
|---|
| 4577 | n/a | Simple_Type |
|---|
| 4578 | n/a | */ |
|---|
| 4579 | n/a | |
|---|
| 4580 | n/a | static int |
|---|
| 4581 | n/a | Simple_set_value(CDataObject *self, PyObject *value) |
|---|
| 4582 | n/a | { |
|---|
| 4583 | n/a | PyObject *result; |
|---|
| 4584 | n/a | StgDictObject *dict = PyObject_stgdict((PyObject *)self); |
|---|
| 4585 | n/a | |
|---|
| 4586 | n/a | if (value == NULL) { |
|---|
| 4587 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4588 | n/a | "can't delete attribute"); |
|---|
| 4589 | n/a | return -1; |
|---|
| 4590 | n/a | } |
|---|
| 4591 | n/a | assert(dict); /* Cannot be NULL for CDataObject instances */ |
|---|
| 4592 | n/a | assert(dict->setfunc); |
|---|
| 4593 | n/a | result = dict->setfunc(self->b_ptr, value, dict->size); |
|---|
| 4594 | n/a | if (!result) |
|---|
| 4595 | n/a | return -1; |
|---|
| 4596 | n/a | |
|---|
| 4597 | n/a | /* consumes the refcount the setfunc returns */ |
|---|
| 4598 | n/a | return KeepRef(self, 0, result); |
|---|
| 4599 | n/a | } |
|---|
| 4600 | n/a | |
|---|
| 4601 | n/a | static int |
|---|
| 4602 | n/a | Simple_init(CDataObject *self, PyObject *args, PyObject *kw) |
|---|
| 4603 | n/a | { |
|---|
| 4604 | n/a | PyObject *value = NULL; |
|---|
| 4605 | n/a | if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value)) |
|---|
| 4606 | n/a | return -1; |
|---|
| 4607 | n/a | if (value) |
|---|
| 4608 | n/a | return Simple_set_value(self, value); |
|---|
| 4609 | n/a | return 0; |
|---|
| 4610 | n/a | } |
|---|
| 4611 | n/a | |
|---|
| 4612 | n/a | static PyObject * |
|---|
| 4613 | n/a | Simple_get_value(CDataObject *self) |
|---|
| 4614 | n/a | { |
|---|
| 4615 | n/a | StgDictObject *dict; |
|---|
| 4616 | n/a | dict = PyObject_stgdict((PyObject *)self); |
|---|
| 4617 | n/a | assert(dict); /* Cannot be NULL for CDataObject instances */ |
|---|
| 4618 | n/a | assert(dict->getfunc); |
|---|
| 4619 | n/a | return dict->getfunc(self->b_ptr, self->b_size); |
|---|
| 4620 | n/a | } |
|---|
| 4621 | n/a | |
|---|
| 4622 | n/a | static PyGetSetDef Simple_getsets[] = { |
|---|
| 4623 | n/a | { "value", (getter)Simple_get_value, (setter)Simple_set_value, |
|---|
| 4624 | n/a | "current value", NULL }, |
|---|
| 4625 | n/a | { NULL, NULL } |
|---|
| 4626 | n/a | }; |
|---|
| 4627 | n/a | |
|---|
| 4628 | n/a | static PyObject * |
|---|
| 4629 | n/a | Simple_from_outparm(PyObject *self, PyObject *args) |
|---|
| 4630 | n/a | { |
|---|
| 4631 | n/a | if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) { |
|---|
| 4632 | n/a | Py_INCREF(self); |
|---|
| 4633 | n/a | return self; |
|---|
| 4634 | n/a | } |
|---|
| 4635 | n/a | /* call stgdict->getfunc */ |
|---|
| 4636 | n/a | return Simple_get_value((CDataObject *)self); |
|---|
| 4637 | n/a | } |
|---|
| 4638 | n/a | |
|---|
| 4639 | n/a | static PyMethodDef Simple_methods[] = { |
|---|
| 4640 | n/a | { "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, }, |
|---|
| 4641 | n/a | { NULL, NULL }, |
|---|
| 4642 | n/a | }; |
|---|
| 4643 | n/a | |
|---|
| 4644 | n/a | static int Simple_bool(CDataObject *self) |
|---|
| 4645 | n/a | { |
|---|
| 4646 | n/a | return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size); |
|---|
| 4647 | n/a | } |
|---|
| 4648 | n/a | |
|---|
| 4649 | n/a | static PyNumberMethods Simple_as_number = { |
|---|
| 4650 | n/a | 0, /* nb_add */ |
|---|
| 4651 | n/a | 0, /* nb_subtract */ |
|---|
| 4652 | n/a | 0, /* nb_multiply */ |
|---|
| 4653 | n/a | 0, /* nb_remainder */ |
|---|
| 4654 | n/a | 0, /* nb_divmod */ |
|---|
| 4655 | n/a | 0, /* nb_power */ |
|---|
| 4656 | n/a | 0, /* nb_negative */ |
|---|
| 4657 | n/a | 0, /* nb_positive */ |
|---|
| 4658 | n/a | 0, /* nb_absolute */ |
|---|
| 4659 | n/a | (inquiry)Simple_bool, /* nb_bool */ |
|---|
| 4660 | n/a | }; |
|---|
| 4661 | n/a | |
|---|
| 4662 | n/a | /* "%s(%s)" % (self.__class__.__name__, self.value) */ |
|---|
| 4663 | n/a | static PyObject * |
|---|
| 4664 | n/a | Simple_repr(CDataObject *self) |
|---|
| 4665 | n/a | { |
|---|
| 4666 | n/a | PyObject *val, *result; |
|---|
| 4667 | n/a | |
|---|
| 4668 | n/a | if (Py_TYPE(self)->tp_base != &Simple_Type) { |
|---|
| 4669 | n/a | return PyUnicode_FromFormat("<%s object at %p>", |
|---|
| 4670 | n/a | Py_TYPE(self)->tp_name, self); |
|---|
| 4671 | n/a | } |
|---|
| 4672 | n/a | |
|---|
| 4673 | n/a | val = Simple_get_value(self); |
|---|
| 4674 | n/a | if (val == NULL) |
|---|
| 4675 | n/a | return NULL; |
|---|
| 4676 | n/a | |
|---|
| 4677 | n/a | result = PyUnicode_FromFormat("%s(%R)", |
|---|
| 4678 | n/a | Py_TYPE(self)->tp_name, val); |
|---|
| 4679 | n/a | Py_DECREF(val); |
|---|
| 4680 | n/a | return result; |
|---|
| 4681 | n/a | } |
|---|
| 4682 | n/a | |
|---|
| 4683 | n/a | static PyTypeObject Simple_Type = { |
|---|
| 4684 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4685 | n/a | "_ctypes._SimpleCData", |
|---|
| 4686 | n/a | sizeof(CDataObject), /* tp_basicsize */ |
|---|
| 4687 | n/a | 0, /* tp_itemsize */ |
|---|
| 4688 | n/a | 0, /* tp_dealloc */ |
|---|
| 4689 | n/a | 0, /* tp_print */ |
|---|
| 4690 | n/a | 0, /* tp_getattr */ |
|---|
| 4691 | n/a | 0, /* tp_setattr */ |
|---|
| 4692 | n/a | 0, /* tp_reserved */ |
|---|
| 4693 | n/a | (reprfunc)&Simple_repr, /* tp_repr */ |
|---|
| 4694 | n/a | &Simple_as_number, /* tp_as_number */ |
|---|
| 4695 | n/a | 0, /* tp_as_sequence */ |
|---|
| 4696 | n/a | 0, /* tp_as_mapping */ |
|---|
| 4697 | n/a | 0, /* tp_hash */ |
|---|
| 4698 | n/a | 0, /* tp_call */ |
|---|
| 4699 | n/a | 0, /* tp_str */ |
|---|
| 4700 | n/a | 0, /* tp_getattro */ |
|---|
| 4701 | n/a | 0, /* tp_setattro */ |
|---|
| 4702 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 4703 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 4704 | n/a | "XXX to be provided", /* tp_doc */ |
|---|
| 4705 | n/a | (traverseproc)PyCData_traverse, /* tp_traverse */ |
|---|
| 4706 | n/a | (inquiry)PyCData_clear, /* tp_clear */ |
|---|
| 4707 | n/a | 0, /* tp_richcompare */ |
|---|
| 4708 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4709 | n/a | 0, /* tp_iter */ |
|---|
| 4710 | n/a | 0, /* tp_iternext */ |
|---|
| 4711 | n/a | Simple_methods, /* tp_methods */ |
|---|
| 4712 | n/a | 0, /* tp_members */ |
|---|
| 4713 | n/a | Simple_getsets, /* tp_getset */ |
|---|
| 4714 | n/a | 0, /* tp_base */ |
|---|
| 4715 | n/a | 0, /* tp_dict */ |
|---|
| 4716 | n/a | 0, /* tp_descr_get */ |
|---|
| 4717 | n/a | 0, /* tp_descr_set */ |
|---|
| 4718 | n/a | 0, /* tp_dictoffset */ |
|---|
| 4719 | n/a | (initproc)Simple_init, /* tp_init */ |
|---|
| 4720 | n/a | 0, /* tp_alloc */ |
|---|
| 4721 | n/a | GenericPyCData_new, /* tp_new */ |
|---|
| 4722 | n/a | 0, /* tp_free */ |
|---|
| 4723 | n/a | }; |
|---|
| 4724 | n/a | |
|---|
| 4725 | n/a | /******************************************************************/ |
|---|
| 4726 | n/a | /* |
|---|
| 4727 | n/a | PyCPointer_Type |
|---|
| 4728 | n/a | */ |
|---|
| 4729 | n/a | static PyObject * |
|---|
| 4730 | n/a | Pointer_item(PyObject *myself, Py_ssize_t index) |
|---|
| 4731 | n/a | { |
|---|
| 4732 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4733 | n/a | Py_ssize_t size; |
|---|
| 4734 | n/a | Py_ssize_t offset; |
|---|
| 4735 | n/a | StgDictObject *stgdict, *itemdict; |
|---|
| 4736 | n/a | PyObject *proto; |
|---|
| 4737 | n/a | |
|---|
| 4738 | n/a | if (*(void **)self->b_ptr == NULL) { |
|---|
| 4739 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4740 | n/a | "NULL pointer access"); |
|---|
| 4741 | n/a | return NULL; |
|---|
| 4742 | n/a | } |
|---|
| 4743 | n/a | |
|---|
| 4744 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4745 | n/a | assert(stgdict); /* Cannot be NULL for pointer object instances */ |
|---|
| 4746 | n/a | |
|---|
| 4747 | n/a | proto = stgdict->proto; |
|---|
| 4748 | n/a | assert(proto); |
|---|
| 4749 | n/a | itemdict = PyType_stgdict(proto); |
|---|
| 4750 | n/a | assert(itemdict); /* proto is the item type of the pointer, a ctypes |
|---|
| 4751 | n/a | type, so this cannot be NULL */ |
|---|
| 4752 | n/a | |
|---|
| 4753 | n/a | size = itemdict->size; |
|---|
| 4754 | n/a | offset = index * itemdict->size; |
|---|
| 4755 | n/a | |
|---|
| 4756 | n/a | return PyCData_get(proto, stgdict->getfunc, (PyObject *)self, |
|---|
| 4757 | n/a | index, size, (*(char **)self->b_ptr) + offset); |
|---|
| 4758 | n/a | } |
|---|
| 4759 | n/a | |
|---|
| 4760 | n/a | static int |
|---|
| 4761 | n/a | Pointer_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value) |
|---|
| 4762 | n/a | { |
|---|
| 4763 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4764 | n/a | Py_ssize_t size; |
|---|
| 4765 | n/a | Py_ssize_t offset; |
|---|
| 4766 | n/a | StgDictObject *stgdict, *itemdict; |
|---|
| 4767 | n/a | PyObject *proto; |
|---|
| 4768 | n/a | |
|---|
| 4769 | n/a | if (value == NULL) { |
|---|
| 4770 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4771 | n/a | "Pointer does not support item deletion"); |
|---|
| 4772 | n/a | return -1; |
|---|
| 4773 | n/a | } |
|---|
| 4774 | n/a | |
|---|
| 4775 | n/a | if (*(void **)self->b_ptr == NULL) { |
|---|
| 4776 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4777 | n/a | "NULL pointer access"); |
|---|
| 4778 | n/a | return -1; |
|---|
| 4779 | n/a | } |
|---|
| 4780 | n/a | |
|---|
| 4781 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4782 | n/a | assert(stgdict); /* Cannot be NULL fr pointer instances */ |
|---|
| 4783 | n/a | |
|---|
| 4784 | n/a | proto = stgdict->proto; |
|---|
| 4785 | n/a | assert(proto); |
|---|
| 4786 | n/a | |
|---|
| 4787 | n/a | itemdict = PyType_stgdict(proto); |
|---|
| 4788 | n/a | assert(itemdict); /* Cannot be NULL because the itemtype of a pointer |
|---|
| 4789 | n/a | is always a ctypes type */ |
|---|
| 4790 | n/a | |
|---|
| 4791 | n/a | size = itemdict->size; |
|---|
| 4792 | n/a | offset = index * itemdict->size; |
|---|
| 4793 | n/a | |
|---|
| 4794 | n/a | return PyCData_set((PyObject *)self, proto, stgdict->setfunc, value, |
|---|
| 4795 | n/a | index, size, (*(char **)self->b_ptr) + offset); |
|---|
| 4796 | n/a | } |
|---|
| 4797 | n/a | |
|---|
| 4798 | n/a | static PyObject * |
|---|
| 4799 | n/a | Pointer_get_contents(CDataObject *self, void *closure) |
|---|
| 4800 | n/a | { |
|---|
| 4801 | n/a | StgDictObject *stgdict; |
|---|
| 4802 | n/a | |
|---|
| 4803 | n/a | if (*(void **)self->b_ptr == NULL) { |
|---|
| 4804 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4805 | n/a | "NULL pointer access"); |
|---|
| 4806 | n/a | return NULL; |
|---|
| 4807 | n/a | } |
|---|
| 4808 | n/a | |
|---|
| 4809 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4810 | n/a | assert(stgdict); /* Cannot be NULL fr pointer instances */ |
|---|
| 4811 | n/a | return PyCData_FromBaseObj(stgdict->proto, |
|---|
| 4812 | n/a | (PyObject *)self, 0, |
|---|
| 4813 | n/a | *(void **)self->b_ptr); |
|---|
| 4814 | n/a | } |
|---|
| 4815 | n/a | |
|---|
| 4816 | n/a | static int |
|---|
| 4817 | n/a | Pointer_set_contents(CDataObject *self, PyObject *value, void *closure) |
|---|
| 4818 | n/a | { |
|---|
| 4819 | n/a | StgDictObject *stgdict; |
|---|
| 4820 | n/a | CDataObject *dst; |
|---|
| 4821 | n/a | PyObject *keep; |
|---|
| 4822 | n/a | |
|---|
| 4823 | n/a | if (value == NULL) { |
|---|
| 4824 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4825 | n/a | "Pointer does not support item deletion"); |
|---|
| 4826 | n/a | return -1; |
|---|
| 4827 | n/a | } |
|---|
| 4828 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4829 | n/a | assert(stgdict); /* Cannot be NULL fr pointer instances */ |
|---|
| 4830 | n/a | assert(stgdict->proto); |
|---|
| 4831 | n/a | if (!CDataObject_Check(value)) { |
|---|
| 4832 | n/a | int res = PyObject_IsInstance(value, stgdict->proto); |
|---|
| 4833 | n/a | if (res == -1) |
|---|
| 4834 | n/a | return -1; |
|---|
| 4835 | n/a | if (!res) { |
|---|
| 4836 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4837 | n/a | "expected %s instead of %s", |
|---|
| 4838 | n/a | ((PyTypeObject *)(stgdict->proto))->tp_name, |
|---|
| 4839 | n/a | Py_TYPE(value)->tp_name); |
|---|
| 4840 | n/a | return -1; |
|---|
| 4841 | n/a | } |
|---|
| 4842 | n/a | } |
|---|
| 4843 | n/a | |
|---|
| 4844 | n/a | dst = (CDataObject *)value; |
|---|
| 4845 | n/a | *(void **)self->b_ptr = dst->b_ptr; |
|---|
| 4846 | n/a | |
|---|
| 4847 | n/a | /* |
|---|
| 4848 | n/a | A Pointer instance must keep the value it points to alive. So, a |
|---|
| 4849 | n/a | pointer instance has b_length set to 2 instead of 1, and we set |
|---|
| 4850 | n/a | 'value' itself as the second item of the b_objects list, additionally. |
|---|
| 4851 | n/a | */ |
|---|
| 4852 | n/a | Py_INCREF(value); |
|---|
| 4853 | n/a | if (-1 == KeepRef(self, 1, value)) |
|---|
| 4854 | n/a | return -1; |
|---|
| 4855 | n/a | |
|---|
| 4856 | n/a | keep = GetKeepedObjects(dst); |
|---|
| 4857 | n/a | if (keep == NULL) |
|---|
| 4858 | n/a | return -1; |
|---|
| 4859 | n/a | |
|---|
| 4860 | n/a | Py_INCREF(keep); |
|---|
| 4861 | n/a | return KeepRef(self, 0, keep); |
|---|
| 4862 | n/a | } |
|---|
| 4863 | n/a | |
|---|
| 4864 | n/a | static PyGetSetDef Pointer_getsets[] = { |
|---|
| 4865 | n/a | { "contents", (getter)Pointer_get_contents, |
|---|
| 4866 | n/a | (setter)Pointer_set_contents, |
|---|
| 4867 | n/a | "the object this pointer points to (read-write)", NULL }, |
|---|
| 4868 | n/a | { NULL, NULL } |
|---|
| 4869 | n/a | }; |
|---|
| 4870 | n/a | |
|---|
| 4871 | n/a | static int |
|---|
| 4872 | n/a | Pointer_init(CDataObject *self, PyObject *args, PyObject *kw) |
|---|
| 4873 | n/a | { |
|---|
| 4874 | n/a | PyObject *value = NULL; |
|---|
| 4875 | n/a | |
|---|
| 4876 | n/a | if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value)) |
|---|
| 4877 | n/a | return -1; |
|---|
| 4878 | n/a | if (value == NULL) |
|---|
| 4879 | n/a | return 0; |
|---|
| 4880 | n/a | return Pointer_set_contents(self, value, NULL); |
|---|
| 4881 | n/a | } |
|---|
| 4882 | n/a | |
|---|
| 4883 | n/a | static PyObject * |
|---|
| 4884 | n/a | Pointer_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
|---|
| 4885 | n/a | { |
|---|
| 4886 | n/a | StgDictObject *dict = PyType_stgdict((PyObject *)type); |
|---|
| 4887 | n/a | if (!dict || !dict->proto) { |
|---|
| 4888 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4889 | n/a | "Cannot create instance: has no _type_"); |
|---|
| 4890 | n/a | return NULL; |
|---|
| 4891 | n/a | } |
|---|
| 4892 | n/a | return GenericPyCData_new(type, args, kw); |
|---|
| 4893 | n/a | } |
|---|
| 4894 | n/a | |
|---|
| 4895 | n/a | static PyObject * |
|---|
| 4896 | n/a | Pointer_subscript(PyObject *myself, PyObject *item) |
|---|
| 4897 | n/a | { |
|---|
| 4898 | n/a | CDataObject *self = (CDataObject *)myself; |
|---|
| 4899 | n/a | if (PyIndex_Check(item)) { |
|---|
| 4900 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
|---|
| 4901 | n/a | if (i == -1 && PyErr_Occurred()) |
|---|
| 4902 | n/a | return NULL; |
|---|
| 4903 | n/a | return Pointer_item(myself, i); |
|---|
| 4904 | n/a | } |
|---|
| 4905 | n/a | else if (PySlice_Check(item)) { |
|---|
| 4906 | n/a | PySliceObject *slice = (PySliceObject *)item; |
|---|
| 4907 | n/a | Py_ssize_t start, stop, step; |
|---|
| 4908 | n/a | PyObject *np; |
|---|
| 4909 | n/a | StgDictObject *stgdict, *itemdict; |
|---|
| 4910 | n/a | PyObject *proto; |
|---|
| 4911 | n/a | Py_ssize_t i, len, cur; |
|---|
| 4912 | n/a | |
|---|
| 4913 | n/a | /* Since pointers have no length, and we want to apply |
|---|
| 4914 | n/a | different semantics to negative indices than normal |
|---|
| 4915 | n/a | slicing, we have to dissect the slice object ourselves.*/ |
|---|
| 4916 | n/a | if (slice->step == Py_None) { |
|---|
| 4917 | n/a | step = 1; |
|---|
| 4918 | n/a | } |
|---|
| 4919 | n/a | else { |
|---|
| 4920 | n/a | step = PyNumber_AsSsize_t(slice->step, |
|---|
| 4921 | n/a | PyExc_ValueError); |
|---|
| 4922 | n/a | if (step == -1 && PyErr_Occurred()) |
|---|
| 4923 | n/a | return NULL; |
|---|
| 4924 | n/a | if (step == 0) { |
|---|
| 4925 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4926 | n/a | "slice step cannot be zero"); |
|---|
| 4927 | n/a | return NULL; |
|---|
| 4928 | n/a | } |
|---|
| 4929 | n/a | } |
|---|
| 4930 | n/a | if (slice->start == Py_None) { |
|---|
| 4931 | n/a | if (step < 0) { |
|---|
| 4932 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4933 | n/a | "slice start is required " |
|---|
| 4934 | n/a | "for step < 0"); |
|---|
| 4935 | n/a | return NULL; |
|---|
| 4936 | n/a | } |
|---|
| 4937 | n/a | start = 0; |
|---|
| 4938 | n/a | } |
|---|
| 4939 | n/a | else { |
|---|
| 4940 | n/a | start = PyNumber_AsSsize_t(slice->start, |
|---|
| 4941 | n/a | PyExc_ValueError); |
|---|
| 4942 | n/a | if (start == -1 && PyErr_Occurred()) |
|---|
| 4943 | n/a | return NULL; |
|---|
| 4944 | n/a | } |
|---|
| 4945 | n/a | if (slice->stop == Py_None) { |
|---|
| 4946 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4947 | n/a | "slice stop is required"); |
|---|
| 4948 | n/a | return NULL; |
|---|
| 4949 | n/a | } |
|---|
| 4950 | n/a | stop = PyNumber_AsSsize_t(slice->stop, |
|---|
| 4951 | n/a | PyExc_ValueError); |
|---|
| 4952 | n/a | if (stop == -1 && PyErr_Occurred()) |
|---|
| 4953 | n/a | return NULL; |
|---|
| 4954 | n/a | if ((step > 0 && start > stop) || |
|---|
| 4955 | n/a | (step < 0 && start < stop)) |
|---|
| 4956 | n/a | len = 0; |
|---|
| 4957 | n/a | else if (step > 0) |
|---|
| 4958 | n/a | len = (stop - start - 1) / step + 1; |
|---|
| 4959 | n/a | else |
|---|
| 4960 | n/a | len = (stop - start + 1) / step + 1; |
|---|
| 4961 | n/a | |
|---|
| 4962 | n/a | stgdict = PyObject_stgdict((PyObject *)self); |
|---|
| 4963 | n/a | assert(stgdict); /* Cannot be NULL for pointer instances */ |
|---|
| 4964 | n/a | proto = stgdict->proto; |
|---|
| 4965 | n/a | assert(proto); |
|---|
| 4966 | n/a | itemdict = PyType_stgdict(proto); |
|---|
| 4967 | n/a | assert(itemdict); |
|---|
| 4968 | n/a | if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
|---|
| 4969 | n/a | char *ptr = *(char **)self->b_ptr; |
|---|
| 4970 | n/a | char *dest; |
|---|
| 4971 | n/a | |
|---|
| 4972 | n/a | if (len <= 0) |
|---|
| 4973 | n/a | return PyBytes_FromStringAndSize("", 0); |
|---|
| 4974 | n/a | if (step == 1) { |
|---|
| 4975 | n/a | return PyBytes_FromStringAndSize(ptr + start, |
|---|
| 4976 | n/a | len); |
|---|
| 4977 | n/a | } |
|---|
| 4978 | n/a | dest = (char *)PyMem_Malloc(len); |
|---|
| 4979 | n/a | if (dest == NULL) |
|---|
| 4980 | n/a | return PyErr_NoMemory(); |
|---|
| 4981 | n/a | for (cur = start, i = 0; i < len; cur += step, i++) { |
|---|
| 4982 | n/a | dest[i] = ptr[cur]; |
|---|
| 4983 | n/a | } |
|---|
| 4984 | n/a | np = PyBytes_FromStringAndSize(dest, len); |
|---|
| 4985 | n/a | PyMem_Free(dest); |
|---|
| 4986 | n/a | return np; |
|---|
| 4987 | n/a | } |
|---|
| 4988 | n/a | #ifdef CTYPES_UNICODE |
|---|
| 4989 | n/a | if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
|---|
| 4990 | n/a | wchar_t *ptr = *(wchar_t **)self->b_ptr; |
|---|
| 4991 | n/a | wchar_t *dest; |
|---|
| 4992 | n/a | |
|---|
| 4993 | n/a | if (len <= 0) |
|---|
| 4994 | n/a | return PyUnicode_New(0, 0); |
|---|
| 4995 | n/a | if (step == 1) { |
|---|
| 4996 | n/a | return PyUnicode_FromWideChar(ptr + start, |
|---|
| 4997 | n/a | len); |
|---|
| 4998 | n/a | } |
|---|
| 4999 | n/a | dest = PyMem_New(wchar_t, len); |
|---|
| 5000 | n/a | if (dest == NULL) |
|---|
| 5001 | n/a | return PyErr_NoMemory(); |
|---|
| 5002 | n/a | for (cur = start, i = 0; i < len; cur += step, i++) { |
|---|
| 5003 | n/a | dest[i] = ptr[cur]; |
|---|
| 5004 | n/a | } |
|---|
| 5005 | n/a | np = PyUnicode_FromWideChar(dest, len); |
|---|
| 5006 | n/a | PyMem_Free(dest); |
|---|
| 5007 | n/a | return np; |
|---|
| 5008 | n/a | } |
|---|
| 5009 | n/a | #endif |
|---|
| 5010 | n/a | |
|---|
| 5011 | n/a | np = PyList_New(len); |
|---|
| 5012 | n/a | if (np == NULL) |
|---|
| 5013 | n/a | return NULL; |
|---|
| 5014 | n/a | |
|---|
| 5015 | n/a | for (cur = start, i = 0; i < len; cur += step, i++) { |
|---|
| 5016 | n/a | PyObject *v = Pointer_item(myself, cur); |
|---|
| 5017 | n/a | PyList_SET_ITEM(np, i, v); |
|---|
| 5018 | n/a | } |
|---|
| 5019 | n/a | return np; |
|---|
| 5020 | n/a | } |
|---|
| 5021 | n/a | else { |
|---|
| 5022 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 5023 | n/a | "Pointer indices must be integer"); |
|---|
| 5024 | n/a | return NULL; |
|---|
| 5025 | n/a | } |
|---|
| 5026 | n/a | } |
|---|
| 5027 | n/a | |
|---|
| 5028 | n/a | static PySequenceMethods Pointer_as_sequence = { |
|---|
| 5029 | n/a | 0, /* inquiry sq_length; */ |
|---|
| 5030 | n/a | 0, /* binaryfunc sq_concat; */ |
|---|
| 5031 | n/a | 0, /* intargfunc sq_repeat; */ |
|---|
| 5032 | n/a | Pointer_item, /* intargfunc sq_item; */ |
|---|
| 5033 | n/a | 0, /* intintargfunc sq_slice; */ |
|---|
| 5034 | n/a | Pointer_ass_item, /* intobjargproc sq_ass_item; */ |
|---|
| 5035 | n/a | 0, /* intintobjargproc sq_ass_slice; */ |
|---|
| 5036 | n/a | 0, /* objobjproc sq_contains; */ |
|---|
| 5037 | n/a | /* Added in release 2.0 */ |
|---|
| 5038 | n/a | 0, /* binaryfunc sq_inplace_concat; */ |
|---|
| 5039 | n/a | 0, /* intargfunc sq_inplace_repeat; */ |
|---|
| 5040 | n/a | }; |
|---|
| 5041 | n/a | |
|---|
| 5042 | n/a | static PyMappingMethods Pointer_as_mapping = { |
|---|
| 5043 | n/a | 0, |
|---|
| 5044 | n/a | Pointer_subscript, |
|---|
| 5045 | n/a | }; |
|---|
| 5046 | n/a | |
|---|
| 5047 | n/a | static int |
|---|
| 5048 | n/a | Pointer_bool(CDataObject *self) |
|---|
| 5049 | n/a | { |
|---|
| 5050 | n/a | return (*(void **)self->b_ptr != NULL); |
|---|
| 5051 | n/a | } |
|---|
| 5052 | n/a | |
|---|
| 5053 | n/a | static PyNumberMethods Pointer_as_number = { |
|---|
| 5054 | n/a | 0, /* nb_add */ |
|---|
| 5055 | n/a | 0, /* nb_subtract */ |
|---|
| 5056 | n/a | 0, /* nb_multiply */ |
|---|
| 5057 | n/a | 0, /* nb_remainder */ |
|---|
| 5058 | n/a | 0, /* nb_divmod */ |
|---|
| 5059 | n/a | 0, /* nb_power */ |
|---|
| 5060 | n/a | 0, /* nb_negative */ |
|---|
| 5061 | n/a | 0, /* nb_positive */ |
|---|
| 5062 | n/a | 0, /* nb_absolute */ |
|---|
| 5063 | n/a | (inquiry)Pointer_bool, /* nb_bool */ |
|---|
| 5064 | n/a | }; |
|---|
| 5065 | n/a | |
|---|
| 5066 | n/a | PyTypeObject PyCPointer_Type = { |
|---|
| 5067 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 5068 | n/a | "_ctypes._Pointer", |
|---|
| 5069 | n/a | sizeof(CDataObject), /* tp_basicsize */ |
|---|
| 5070 | n/a | 0, /* tp_itemsize */ |
|---|
| 5071 | n/a | 0, /* tp_dealloc */ |
|---|
| 5072 | n/a | 0, /* tp_print */ |
|---|
| 5073 | n/a | 0, /* tp_getattr */ |
|---|
| 5074 | n/a | 0, /* tp_setattr */ |
|---|
| 5075 | n/a | 0, /* tp_reserved */ |
|---|
| 5076 | n/a | 0, /* tp_repr */ |
|---|
| 5077 | n/a | &Pointer_as_number, /* tp_as_number */ |
|---|
| 5078 | n/a | &Pointer_as_sequence, /* tp_as_sequence */ |
|---|
| 5079 | n/a | &Pointer_as_mapping, /* tp_as_mapping */ |
|---|
| 5080 | n/a | 0, /* tp_hash */ |
|---|
| 5081 | n/a | 0, /* tp_call */ |
|---|
| 5082 | n/a | 0, /* tp_str */ |
|---|
| 5083 | n/a | 0, /* tp_getattro */ |
|---|
| 5084 | n/a | 0, /* tp_setattro */ |
|---|
| 5085 | n/a | &PyCData_as_buffer, /* tp_as_buffer */ |
|---|
| 5086 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 5087 | n/a | "XXX to be provided", /* tp_doc */ |
|---|
| 5088 | n/a | (traverseproc)PyCData_traverse, /* tp_traverse */ |
|---|
| 5089 | n/a | (inquiry)PyCData_clear, /* tp_clear */ |
|---|
| 5090 | n/a | 0, /* tp_richcompare */ |
|---|
| 5091 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 5092 | n/a | 0, /* tp_iter */ |
|---|
| 5093 | n/a | 0, /* tp_iternext */ |
|---|
| 5094 | n/a | 0, /* tp_methods */ |
|---|
| 5095 | n/a | 0, /* tp_members */ |
|---|
| 5096 | n/a | Pointer_getsets, /* tp_getset */ |
|---|
| 5097 | n/a | 0, /* tp_base */ |
|---|
| 5098 | n/a | 0, /* tp_dict */ |
|---|
| 5099 | n/a | 0, /* tp_descr_get */ |
|---|
| 5100 | n/a | 0, /* tp_descr_set */ |
|---|
| 5101 | n/a | 0, /* tp_dictoffset */ |
|---|
| 5102 | n/a | (initproc)Pointer_init, /* tp_init */ |
|---|
| 5103 | n/a | 0, /* tp_alloc */ |
|---|
| 5104 | n/a | Pointer_new, /* tp_new */ |
|---|
| 5105 | n/a | 0, /* tp_free */ |
|---|
| 5106 | n/a | }; |
|---|
| 5107 | n/a | |
|---|
| 5108 | n/a | |
|---|
| 5109 | n/a | /******************************************************************/ |
|---|
| 5110 | n/a | /* |
|---|
| 5111 | n/a | * Module initialization. |
|---|
| 5112 | n/a | */ |
|---|
| 5113 | n/a | |
|---|
| 5114 | n/a | static const char module_docs[] = |
|---|
| 5115 | n/a | "Create and manipulate C compatible data types in Python."; |
|---|
| 5116 | n/a | |
|---|
| 5117 | n/a | #ifdef MS_WIN32 |
|---|
| 5118 | n/a | |
|---|
| 5119 | n/a | static const char comerror_doc[] = "Raised when a COM method call failed."; |
|---|
| 5120 | n/a | |
|---|
| 5121 | n/a | int |
|---|
| 5122 | n/a | comerror_init(PyObject *self, PyObject *args, PyObject *kwds) |
|---|
| 5123 | n/a | { |
|---|
| 5124 | n/a | PyObject *hresult, *text, *details; |
|---|
| 5125 | n/a | PyObject *a; |
|---|
| 5126 | n/a | int status; |
|---|
| 5127 | n/a | |
|---|
| 5128 | n/a | if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
|---|
| 5129 | n/a | return -1; |
|---|
| 5130 | n/a | |
|---|
| 5131 | n/a | if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details)) |
|---|
| 5132 | n/a | return -1; |
|---|
| 5133 | n/a | |
|---|
| 5134 | n/a | a = PySequence_GetSlice(args, 1, PySequence_Size(args)); |
|---|
| 5135 | n/a | if (!a) |
|---|
| 5136 | n/a | return -1; |
|---|
| 5137 | n/a | status = PyObject_SetAttrString(self, "args", a); |
|---|
| 5138 | n/a | Py_DECREF(a); |
|---|
| 5139 | n/a | if (status < 0) |
|---|
| 5140 | n/a | return -1; |
|---|
| 5141 | n/a | |
|---|
| 5142 | n/a | if (PyObject_SetAttrString(self, "hresult", hresult) < 0) |
|---|
| 5143 | n/a | return -1; |
|---|
| 5144 | n/a | |
|---|
| 5145 | n/a | if (PyObject_SetAttrString(self, "text", text) < 0) |
|---|
| 5146 | n/a | return -1; |
|---|
| 5147 | n/a | |
|---|
| 5148 | n/a | if (PyObject_SetAttrString(self, "details", details) < 0) |
|---|
| 5149 | n/a | return -1; |
|---|
| 5150 | n/a | |
|---|
| 5151 | n/a | Py_INCREF(args); |
|---|
| 5152 | n/a | Py_SETREF(((PyBaseExceptionObject *)self)->args, args); |
|---|
| 5153 | n/a | |
|---|
| 5154 | n/a | return 0; |
|---|
| 5155 | n/a | } |
|---|
| 5156 | n/a | |
|---|
| 5157 | n/a | static PyTypeObject PyComError_Type = { |
|---|
| 5158 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 5159 | n/a | "_ctypes.COMError", /* tp_name */ |
|---|
| 5160 | n/a | sizeof(PyBaseExceptionObject), /* tp_basicsize */ |
|---|
| 5161 | n/a | 0, /* tp_itemsize */ |
|---|
| 5162 | n/a | 0, /* tp_dealloc */ |
|---|
| 5163 | n/a | 0, /* tp_print */ |
|---|
| 5164 | n/a | 0, /* tp_getattr */ |
|---|
| 5165 | n/a | 0, /* tp_setattr */ |
|---|
| 5166 | n/a | 0, /* tp_reserved */ |
|---|
| 5167 | n/a | 0, /* tp_repr */ |
|---|
| 5168 | n/a | 0, /* tp_as_number */ |
|---|
| 5169 | n/a | 0, /* tp_as_sequence */ |
|---|
| 5170 | n/a | 0, /* tp_as_mapping */ |
|---|
| 5171 | n/a | 0, /* tp_hash */ |
|---|
| 5172 | n/a | 0, /* tp_call */ |
|---|
| 5173 | n/a | 0, /* tp_str */ |
|---|
| 5174 | n/a | 0, /* tp_getattro */ |
|---|
| 5175 | n/a | 0, /* tp_setattro */ |
|---|
| 5176 | n/a | 0, /* tp_as_buffer */ |
|---|
| 5177 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
|---|
| 5178 | n/a | PyDoc_STR(comerror_doc), /* tp_doc */ |
|---|
| 5179 | n/a | 0, /* tp_traverse */ |
|---|
| 5180 | n/a | 0, /* tp_clear */ |
|---|
| 5181 | n/a | 0, /* tp_richcompare */ |
|---|
| 5182 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 5183 | n/a | 0, /* tp_iter */ |
|---|
| 5184 | n/a | 0, /* tp_iternext */ |
|---|
| 5185 | n/a | 0, /* tp_methods */ |
|---|
| 5186 | n/a | 0, /* tp_members */ |
|---|
| 5187 | n/a | 0, /* tp_getset */ |
|---|
| 5188 | n/a | 0, /* tp_base */ |
|---|
| 5189 | n/a | 0, /* tp_dict */ |
|---|
| 5190 | n/a | 0, /* tp_descr_get */ |
|---|
| 5191 | n/a | 0, /* tp_descr_set */ |
|---|
| 5192 | n/a | 0, /* tp_dictoffset */ |
|---|
| 5193 | n/a | (initproc)comerror_init, /* tp_init */ |
|---|
| 5194 | n/a | 0, /* tp_alloc */ |
|---|
| 5195 | n/a | 0, /* tp_new */ |
|---|
| 5196 | n/a | }; |
|---|
| 5197 | n/a | |
|---|
| 5198 | n/a | |
|---|
| 5199 | n/a | static int |
|---|
| 5200 | n/a | create_comerror(void) |
|---|
| 5201 | n/a | { |
|---|
| 5202 | n/a | PyComError_Type.tp_base = (PyTypeObject*)PyExc_Exception; |
|---|
| 5203 | n/a | if (PyType_Ready(&PyComError_Type) < 0) |
|---|
| 5204 | n/a | return -1; |
|---|
| 5205 | n/a | Py_INCREF(&PyComError_Type); |
|---|
| 5206 | n/a | ComError = (PyObject*)&PyComError_Type; |
|---|
| 5207 | n/a | return 0; |
|---|
| 5208 | n/a | } |
|---|
| 5209 | n/a | |
|---|
| 5210 | n/a | #endif |
|---|
| 5211 | n/a | |
|---|
| 5212 | n/a | static PyObject * |
|---|
| 5213 | n/a | string_at(const char *ptr, int size) |
|---|
| 5214 | n/a | { |
|---|
| 5215 | n/a | if (size == -1) |
|---|
| 5216 | n/a | return PyBytes_FromStringAndSize(ptr, strlen(ptr)); |
|---|
| 5217 | n/a | return PyBytes_FromStringAndSize(ptr, size); |
|---|
| 5218 | n/a | } |
|---|
| 5219 | n/a | |
|---|
| 5220 | n/a | static int |
|---|
| 5221 | n/a | cast_check_pointertype(PyObject *arg) |
|---|
| 5222 | n/a | { |
|---|
| 5223 | n/a | StgDictObject *dict; |
|---|
| 5224 | n/a | |
|---|
| 5225 | n/a | if (PyCPointerTypeObject_Check(arg)) |
|---|
| 5226 | n/a | return 1; |
|---|
| 5227 | n/a | if (PyCFuncPtrTypeObject_Check(arg)) |
|---|
| 5228 | n/a | return 1; |
|---|
| 5229 | n/a | dict = PyType_stgdict(arg); |
|---|
| 5230 | n/a | if (dict) { |
|---|
| 5231 | n/a | if (PyUnicode_Check(dict->proto) |
|---|
| 5232 | n/a | && (strchr("sPzUZXO", PyUnicode_AsUTF8(dict->proto)[0]))) { |
|---|
| 5233 | n/a | /* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ |
|---|
| 5234 | n/a | return 1; |
|---|
| 5235 | n/a | } |
|---|
| 5236 | n/a | } |
|---|
| 5237 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 5238 | n/a | "cast() argument 2 must be a pointer type, not %s", |
|---|
| 5239 | n/a | PyType_Check(arg) |
|---|
| 5240 | n/a | ? ((PyTypeObject *)arg)->tp_name |
|---|
| 5241 | n/a | : Py_TYPE(arg)->tp_name); |
|---|
| 5242 | n/a | return 0; |
|---|
| 5243 | n/a | } |
|---|
| 5244 | n/a | |
|---|
| 5245 | n/a | static PyObject * |
|---|
| 5246 | n/a | cast(void *ptr, PyObject *src, PyObject *ctype) |
|---|
| 5247 | n/a | { |
|---|
| 5248 | n/a | CDataObject *result; |
|---|
| 5249 | n/a | if (0 == cast_check_pointertype(ctype)) |
|---|
| 5250 | n/a | return NULL; |
|---|
| 5251 | n/a | result = (CDataObject *)_PyObject_CallNoArg(ctype); |
|---|
| 5252 | n/a | if (result == NULL) |
|---|
| 5253 | n/a | return NULL; |
|---|
| 5254 | n/a | |
|---|
| 5255 | n/a | /* |
|---|
| 5256 | n/a | The casted objects '_objects' member: |
|---|
| 5257 | n/a | |
|---|
| 5258 | n/a | It must certainly contain the source objects one. |
|---|
| 5259 | n/a | It must contain the source object itself. |
|---|
| 5260 | n/a | */ |
|---|
| 5261 | n/a | if (CDataObject_Check(src)) { |
|---|
| 5262 | n/a | CDataObject *obj = (CDataObject *)src; |
|---|
| 5263 | n/a | CDataObject *container; |
|---|
| 5264 | n/a | |
|---|
| 5265 | n/a | /* PyCData_GetContainer will initialize src.b_objects, we need |
|---|
| 5266 | n/a | this so it can be shared */ |
|---|
| 5267 | n/a | container = PyCData_GetContainer(obj); |
|---|
| 5268 | n/a | if (container == NULL) |
|---|
| 5269 | n/a | goto failed; |
|---|
| 5270 | n/a | |
|---|
| 5271 | n/a | /* But we need a dictionary! */ |
|---|
| 5272 | n/a | if (obj->b_objects == Py_None) { |
|---|
| 5273 | n/a | Py_DECREF(Py_None); |
|---|
| 5274 | n/a | obj->b_objects = PyDict_New(); |
|---|
| 5275 | n/a | if (obj->b_objects == NULL) |
|---|
| 5276 | n/a | goto failed; |
|---|
| 5277 | n/a | } |
|---|
| 5278 | n/a | Py_XINCREF(obj->b_objects); |
|---|
| 5279 | n/a | result->b_objects = obj->b_objects; |
|---|
| 5280 | n/a | if (result->b_objects && PyDict_CheckExact(result->b_objects)) { |
|---|
| 5281 | n/a | PyObject *index; |
|---|
| 5282 | n/a | int rc; |
|---|
| 5283 | n/a | index = PyLong_FromVoidPtr((void *)src); |
|---|
| 5284 | n/a | if (index == NULL) |
|---|
| 5285 | n/a | goto failed; |
|---|
| 5286 | n/a | rc = PyDict_SetItem(result->b_objects, index, src); |
|---|
| 5287 | n/a | Py_DECREF(index); |
|---|
| 5288 | n/a | if (rc == -1) |
|---|
| 5289 | n/a | goto failed; |
|---|
| 5290 | n/a | } |
|---|
| 5291 | n/a | } |
|---|
| 5292 | n/a | /* Should we assert that result is a pointer type? */ |
|---|
| 5293 | n/a | memcpy(result->b_ptr, &ptr, sizeof(void *)); |
|---|
| 5294 | n/a | return (PyObject *)result; |
|---|
| 5295 | n/a | |
|---|
| 5296 | n/a | failed: |
|---|
| 5297 | n/a | Py_DECREF(result); |
|---|
| 5298 | n/a | return NULL; |
|---|
| 5299 | n/a | } |
|---|
| 5300 | n/a | |
|---|
| 5301 | n/a | #ifdef CTYPES_UNICODE |
|---|
| 5302 | n/a | static PyObject * |
|---|
| 5303 | n/a | wstring_at(const wchar_t *ptr, int size) |
|---|
| 5304 | n/a | { |
|---|
| 5305 | n/a | Py_ssize_t ssize = size; |
|---|
| 5306 | n/a | if (ssize == -1) |
|---|
| 5307 | n/a | ssize = wcslen(ptr); |
|---|
| 5308 | n/a | return PyUnicode_FromWideChar(ptr, ssize); |
|---|
| 5309 | n/a | } |
|---|
| 5310 | n/a | #endif |
|---|
| 5311 | n/a | |
|---|
| 5312 | n/a | |
|---|
| 5313 | n/a | static struct PyModuleDef _ctypesmodule = { |
|---|
| 5314 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 5315 | n/a | "_ctypes", |
|---|
| 5316 | n/a | module_docs, |
|---|
| 5317 | n/a | -1, |
|---|
| 5318 | n/a | _ctypes_module_methods, |
|---|
| 5319 | n/a | NULL, |
|---|
| 5320 | n/a | NULL, |
|---|
| 5321 | n/a | NULL, |
|---|
| 5322 | n/a | NULL |
|---|
| 5323 | n/a | }; |
|---|
| 5324 | n/a | |
|---|
| 5325 | n/a | PyMODINIT_FUNC |
|---|
| 5326 | n/a | PyInit__ctypes(void) |
|---|
| 5327 | n/a | { |
|---|
| 5328 | n/a | PyObject *m; |
|---|
| 5329 | n/a | |
|---|
| 5330 | n/a | /* Note: |
|---|
| 5331 | n/a | ob_type is the metatype (the 'type'), defaults to PyType_Type, |
|---|
| 5332 | n/a | tp_base is the base type, defaults to 'object' aka PyBaseObject_Type. |
|---|
| 5333 | n/a | */ |
|---|
| 5334 | n/a | #ifdef WITH_THREAD |
|---|
| 5335 | n/a | PyEval_InitThreads(); |
|---|
| 5336 | n/a | #endif |
|---|
| 5337 | n/a | m = PyModule_Create(&_ctypesmodule); |
|---|
| 5338 | n/a | if (!m) |
|---|
| 5339 | n/a | return NULL; |
|---|
| 5340 | n/a | |
|---|
| 5341 | n/a | _ctypes_ptrtype_cache = PyDict_New(); |
|---|
| 5342 | n/a | if (_ctypes_ptrtype_cache == NULL) |
|---|
| 5343 | n/a | return NULL; |
|---|
| 5344 | n/a | |
|---|
| 5345 | n/a | PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cache); |
|---|
| 5346 | n/a | |
|---|
| 5347 | n/a | _unpickle = PyObject_GetAttrString(m, "_unpickle"); |
|---|
| 5348 | n/a | if (_unpickle == NULL) |
|---|
| 5349 | n/a | return NULL; |
|---|
| 5350 | n/a | |
|---|
| 5351 | n/a | if (PyType_Ready(&PyCArg_Type) < 0) |
|---|
| 5352 | n/a | return NULL; |
|---|
| 5353 | n/a | |
|---|
| 5354 | n/a | if (PyType_Ready(&PyCThunk_Type) < 0) |
|---|
| 5355 | n/a | return NULL; |
|---|
| 5356 | n/a | |
|---|
| 5357 | n/a | /* StgDict is derived from PyDict_Type */ |
|---|
| 5358 | n/a | PyCStgDict_Type.tp_base = &PyDict_Type; |
|---|
| 5359 | n/a | if (PyType_Ready(&PyCStgDict_Type) < 0) |
|---|
| 5360 | n/a | return NULL; |
|---|
| 5361 | n/a | |
|---|
| 5362 | n/a | /************************************************* |
|---|
| 5363 | n/a | * |
|---|
| 5364 | n/a | * Metaclasses |
|---|
| 5365 | n/a | */ |
|---|
| 5366 | n/a | |
|---|
| 5367 | n/a | PyCStructType_Type.tp_base = &PyType_Type; |
|---|
| 5368 | n/a | if (PyType_Ready(&PyCStructType_Type) < 0) |
|---|
| 5369 | n/a | return NULL; |
|---|
| 5370 | n/a | |
|---|
| 5371 | n/a | UnionType_Type.tp_base = &PyType_Type; |
|---|
| 5372 | n/a | if (PyType_Ready(&UnionType_Type) < 0) |
|---|
| 5373 | n/a | return NULL; |
|---|
| 5374 | n/a | |
|---|
| 5375 | n/a | PyCPointerType_Type.tp_base = &PyType_Type; |
|---|
| 5376 | n/a | if (PyType_Ready(&PyCPointerType_Type) < 0) |
|---|
| 5377 | n/a | return NULL; |
|---|
| 5378 | n/a | |
|---|
| 5379 | n/a | PyCArrayType_Type.tp_base = &PyType_Type; |
|---|
| 5380 | n/a | if (PyType_Ready(&PyCArrayType_Type) < 0) |
|---|
| 5381 | n/a | return NULL; |
|---|
| 5382 | n/a | |
|---|
| 5383 | n/a | PyCSimpleType_Type.tp_base = &PyType_Type; |
|---|
| 5384 | n/a | if (PyType_Ready(&PyCSimpleType_Type) < 0) |
|---|
| 5385 | n/a | return NULL; |
|---|
| 5386 | n/a | |
|---|
| 5387 | n/a | PyCFuncPtrType_Type.tp_base = &PyType_Type; |
|---|
| 5388 | n/a | if (PyType_Ready(&PyCFuncPtrType_Type) < 0) |
|---|
| 5389 | n/a | return NULL; |
|---|
| 5390 | n/a | |
|---|
| 5391 | n/a | /************************************************* |
|---|
| 5392 | n/a | * |
|---|
| 5393 | n/a | * Classes using a custom metaclass |
|---|
| 5394 | n/a | */ |
|---|
| 5395 | n/a | |
|---|
| 5396 | n/a | if (PyType_Ready(&PyCData_Type) < 0) |
|---|
| 5397 | n/a | return NULL; |
|---|
| 5398 | n/a | |
|---|
| 5399 | n/a | Py_TYPE(&Struct_Type) = &PyCStructType_Type; |
|---|
| 5400 | n/a | Struct_Type.tp_base = &PyCData_Type; |
|---|
| 5401 | n/a | if (PyType_Ready(&Struct_Type) < 0) |
|---|
| 5402 | n/a | return NULL; |
|---|
| 5403 | n/a | Py_INCREF(&Struct_Type); |
|---|
| 5404 | n/a | PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type); |
|---|
| 5405 | n/a | |
|---|
| 5406 | n/a | Py_TYPE(&Union_Type) = &UnionType_Type; |
|---|
| 5407 | n/a | Union_Type.tp_base = &PyCData_Type; |
|---|
| 5408 | n/a | if (PyType_Ready(&Union_Type) < 0) |
|---|
| 5409 | n/a | return NULL; |
|---|
| 5410 | n/a | Py_INCREF(&Union_Type); |
|---|
| 5411 | n/a | PyModule_AddObject(m, "Union", (PyObject *)&Union_Type); |
|---|
| 5412 | n/a | |
|---|
| 5413 | n/a | Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type; |
|---|
| 5414 | n/a | PyCPointer_Type.tp_base = &PyCData_Type; |
|---|
| 5415 | n/a | if (PyType_Ready(&PyCPointer_Type) < 0) |
|---|
| 5416 | n/a | return NULL; |
|---|
| 5417 | n/a | Py_INCREF(&PyCPointer_Type); |
|---|
| 5418 | n/a | PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type); |
|---|
| 5419 | n/a | |
|---|
| 5420 | n/a | Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type; |
|---|
| 5421 | n/a | PyCArray_Type.tp_base = &PyCData_Type; |
|---|
| 5422 | n/a | if (PyType_Ready(&PyCArray_Type) < 0) |
|---|
| 5423 | n/a | return NULL; |
|---|
| 5424 | n/a | Py_INCREF(&PyCArray_Type); |
|---|
| 5425 | n/a | PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type); |
|---|
| 5426 | n/a | |
|---|
| 5427 | n/a | Py_TYPE(&Simple_Type) = &PyCSimpleType_Type; |
|---|
| 5428 | n/a | Simple_Type.tp_base = &PyCData_Type; |
|---|
| 5429 | n/a | if (PyType_Ready(&Simple_Type) < 0) |
|---|
| 5430 | n/a | return NULL; |
|---|
| 5431 | n/a | Py_INCREF(&Simple_Type); |
|---|
| 5432 | n/a | PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type); |
|---|
| 5433 | n/a | |
|---|
| 5434 | n/a | Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type; |
|---|
| 5435 | n/a | PyCFuncPtr_Type.tp_base = &PyCData_Type; |
|---|
| 5436 | n/a | if (PyType_Ready(&PyCFuncPtr_Type) < 0) |
|---|
| 5437 | n/a | return NULL; |
|---|
| 5438 | n/a | Py_INCREF(&PyCFuncPtr_Type); |
|---|
| 5439 | n/a | PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type); |
|---|
| 5440 | n/a | |
|---|
| 5441 | n/a | /************************************************* |
|---|
| 5442 | n/a | * |
|---|
| 5443 | n/a | * Simple classes |
|---|
| 5444 | n/a | */ |
|---|
| 5445 | n/a | |
|---|
| 5446 | n/a | /* PyCField_Type is derived from PyBaseObject_Type */ |
|---|
| 5447 | n/a | if (PyType_Ready(&PyCField_Type) < 0) |
|---|
| 5448 | n/a | return NULL; |
|---|
| 5449 | n/a | |
|---|
| 5450 | n/a | /************************************************* |
|---|
| 5451 | n/a | * |
|---|
| 5452 | n/a | * Other stuff |
|---|
| 5453 | n/a | */ |
|---|
| 5454 | n/a | |
|---|
| 5455 | n/a | DictRemover_Type.tp_new = PyType_GenericNew; |
|---|
| 5456 | n/a | if (PyType_Ready(&DictRemover_Type) < 0) |
|---|
| 5457 | n/a | return NULL; |
|---|
| 5458 | n/a | |
|---|
| 5459 | n/a | #ifdef MS_WIN32 |
|---|
| 5460 | n/a | if (create_comerror() < 0) |
|---|
| 5461 | n/a | return NULL; |
|---|
| 5462 | n/a | PyModule_AddObject(m, "COMError", ComError); |
|---|
| 5463 | n/a | |
|---|
| 5464 | n/a | PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT)); |
|---|
| 5465 | n/a | PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL)); |
|---|
| 5466 | n/a | #endif |
|---|
| 5467 | n/a | PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL)); |
|---|
| 5468 | n/a | PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO)); |
|---|
| 5469 | n/a | PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR)); |
|---|
| 5470 | n/a | PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI)); |
|---|
| 5471 | n/a | PyModule_AddStringConstant(m, "__version__", "1.1.0"); |
|---|
| 5472 | n/a | |
|---|
| 5473 | n/a | PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove)); |
|---|
| 5474 | n/a | PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset)); |
|---|
| 5475 | n/a | PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at)); |
|---|
| 5476 | n/a | PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast)); |
|---|
| 5477 | n/a | #ifdef CTYPES_UNICODE |
|---|
| 5478 | n/a | PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at)); |
|---|
| 5479 | n/a | #endif |
|---|
| 5480 | n/a | |
|---|
| 5481 | n/a | /* If RTLD_LOCAL is not defined (Windows!), set it to zero. */ |
|---|
| 5482 | n/a | #if !HAVE_DECL_RTLD_LOCAL |
|---|
| 5483 | n/a | #define RTLD_LOCAL 0 |
|---|
| 5484 | n/a | #endif |
|---|
| 5485 | n/a | |
|---|
| 5486 | n/a | /* If RTLD_GLOBAL is not defined (cygwin), set it to the same value as |
|---|
| 5487 | n/a | RTLD_LOCAL. |
|---|
| 5488 | n/a | */ |
|---|
| 5489 | n/a | #if !HAVE_DECL_RTLD_GLOBAL |
|---|
| 5490 | n/a | #define RTLD_GLOBAL RTLD_LOCAL |
|---|
| 5491 | n/a | #endif |
|---|
| 5492 | n/a | |
|---|
| 5493 | n/a | PyModule_AddObject(m, "RTLD_LOCAL", PyLong_FromLong(RTLD_LOCAL)); |
|---|
| 5494 | n/a | PyModule_AddObject(m, "RTLD_GLOBAL", PyLong_FromLong(RTLD_GLOBAL)); |
|---|
| 5495 | n/a | |
|---|
| 5496 | n/a | PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL); |
|---|
| 5497 | n/a | if (PyExc_ArgError) { |
|---|
| 5498 | n/a | Py_INCREF(PyExc_ArgError); |
|---|
| 5499 | n/a | PyModule_AddObject(m, "ArgumentError", PyExc_ArgError); |
|---|
| 5500 | n/a | } |
|---|
| 5501 | n/a | return m; |
|---|
| 5502 | n/a | } |
|---|
| 5503 | n/a | |
|---|
| 5504 | n/a | /* |
|---|
| 5505 | n/a | Local Variables: |
|---|
| 5506 | n/a | compile-command: "cd .. && python setup.py -q build -g && python setup.py -q build install --home ~" |
|---|
| 5507 | n/a | End: |
|---|
| 5508 | n/a | */ |
|---|