| 1 |
/* |
| 2 |
ToDo: |
| 3 |
|
| 4 |
Get rid of the checker (and also the converters) field in PyCFuncPtrObject and |
| 5 |
StgInfo, and replace them by slot functions in StgInfo. |
| 6 |
|
| 7 |
think about a buffer-like object (memory? bytes?) |
| 8 |
|
| 9 |
Should POINTER(c_char) and POINTER(c_wchar) have a .value property? |
| 10 |
What about c_char and c_wchar arrays then? |
| 11 |
|
| 12 |
Add from_mmap, from_file, from_string metaclass methods. |
| 13 |
|
| 14 |
Maybe we can get away with from_file (calls read) and with a from_buffer |
| 15 |
method? |
| 16 |
|
| 17 |
And what about the to_mmap, to_file, to_str(?) methods? They would clobber |
| 18 |
the namespace, probably. So, functions instead? And we already have memmove... |
| 19 |
*/ |
| 20 |
|
| 21 |
/* |
| 22 |
|
| 23 |
Name methods, members, getsets |
| 24 |
============================================================================== |
| 25 |
|
| 26 |
PyCStructType_Type __new__(), from_address(), __mul__(), from_param() |
| 27 |
UnionType_Type __new__(), from_address(), __mul__(), from_param() |
| 28 |
PyCPointerType_Type __new__(), from_address(), __mul__(), from_param(), set_type() |
| 29 |
PyCArrayType_Type __new__(), from_address(), __mul__(), from_param() |
| 30 |
PyCSimpleType_Type __new__(), from_address(), __mul__(), from_param() |
| 31 |
|
| 32 |
PyCData_Type |
| 33 |
Struct_Type __new__(), __init__() |
| 34 |
PyCPointer_Type __new__(), __init__(), _as_parameter_, contents |
| 35 |
PyCArray_Type __new__(), __init__(), _as_parameter_, __get/setitem__(), __len__() |
| 36 |
Simple_Type __new__(), __init__(), _as_parameter_ |
| 37 |
|
| 38 |
PyCField_Type |
| 39 |
|
| 40 |
============================================================================== |
| 41 |
|
| 42 |
class methods |
| 43 |
------------- |
| 44 |
|
| 45 |
It has some similarity to the byref() construct compared to pointer() |
| 46 |
from_address(addr) |
| 47 |
- construct an instance from a given memory block (sharing this memory block) |
| 48 |
|
| 49 |
from_param(obj) |
| 50 |
- typecheck and convert a Python object into a C function call parameter |
| 51 |
The result may be an instance of the type, or an integer or tuple |
| 52 |
(typecode, value[, obj]) |
| 53 |
|
| 54 |
instance methods/properties |
| 55 |
--------------------------- |
| 56 |
|
| 57 |
_as_parameter_ |
| 58 |
- convert self into a C function call parameter |
| 59 |
This is either an integer, or a 3-tuple (typecode, value, obj) |
| 60 |
|
| 61 |
functions |
| 62 |
--------- |
| 63 |
|
| 64 |
sizeof(cdata) |
| 65 |
- return the number of bytes the buffer contains |
| 66 |
|
| 67 |
sizeof(ctype) |
| 68 |
- return the number of bytes the buffer of an instance would contain |
| 69 |
|
| 70 |
byref(cdata) |
| 71 |
|
| 72 |
addressof(cdata) |
| 73 |
|
| 74 |
pointer(cdata) |
| 75 |
|
| 76 |
POINTER(ctype) |
| 77 |
|
| 78 |
bytes(cdata) |
| 79 |
- return the buffer contents as a sequence of bytes (which is currently a string) |
| 80 |
|
| 81 |
*/ |
| 82 |
|
| 83 |
/* |
| 84 |
* PyCStructType_Type |
| 85 |
* UnionType_Type |
| 86 |
* PyCPointerType_Type |
| 87 |
* PyCArrayType_Type |
| 88 |
* PyCSimpleType_Type |
| 89 |
* |
| 90 |
* PyCData_Type |
| 91 |
* Struct_Type |
| 92 |
* Union_Type |
| 93 |
* PyCArray_Type |
| 94 |
* Simple_Type |
| 95 |
* PyCPointer_Type |
| 96 |
* PyCField_Type |
| 97 |
* |
| 98 |
*/ |
| 99 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 100 |
# define Py_BUILD_CORE_MODULE 1 |
| 101 |
#endif |
| 102 |
|
| 103 |
#include "Python.h" |
| 104 |
// windows.h must be included before pycore internal headers |
| 105 |
#ifdef MS_WIN32 |
| 106 |
# include <windows.h> |
| 107 |
#endif |
| 108 |
|
| 109 |
#include "pycore_call.h" // _PyObject_CallNoArgs() |
| 110 |
#include "pycore_ceval.h" // _Py_EnterRecursiveCall() |
| 111 |
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString() |
| 112 |
#include "pycore_pyatomic_ft_wrappers.h" |
| 113 |
#include "pycore_object.h" |
| 114 |
#ifdef MS_WIN32 |
| 115 |
# include "pycore_modsupport.h" // _PyArg_NoKeywords() |
| 116 |
#endif |
| 117 |
|
| 118 |
|
| 119 |
#include <ffi.h> |
| 120 |
#ifdef MS_WIN32 |
| 121 |
#include <malloc.h> |
| 122 |
#ifndef IS_INTRESOURCE |
| 123 |
#define IS_INTRESOURCE(x) (((size_t)(x) >> 16) == 0) |
| 124 |
#endif |
| 125 |
#else |
| 126 |
#include <dlfcn.h> |
| 127 |
#endif |
| 128 |
#include "ctypes.h" |
| 129 |
|
| 130 |
#include "pycore_long.h" // _PyLong_GetZero() |
| 131 |
|
| 132 |
/*[clinic input] |
| 133 |
module _ctypes |
| 134 |
class _ctypes.CFuncPtr "PyCFuncPtrObject *" "&PyCFuncPtr_Type" |
| 135 |
[clinic start generated code]*/ |
| 136 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=58e8c99474bc631e]*/ |
| 137 |
|
| 138 |
#define clinic_state() (get_module_state_by_class(cls)) |
| 139 |
#define clinic_state_sub() (get_module_state_by_class(cls->tp_base)) |
| 140 |
#include "clinic/_ctypes.c.h" |
| 141 |
#undef clinic_state |
| 142 |
#undef clinic_state_sub |
| 143 |
|
| 144 |
/****************************************************************/ |
| 145 |
|
| 146 |
typedef struct { |
| 147 |
PyObject_HEAD |
| 148 |
PyObject *key; |
| 149 |
PyObject *dict; |
| 150 |
} DictRemoverObject; |
| 151 |
|
| 152 |
#define _DictRemoverObject_CAST(op) ((DictRemoverObject *)(op)) |
| 153 |
|
| 154 |
static int |
| 155 |
_DictRemover_traverse(PyObject *myself, visitproc visit, void *arg) |
| 156 |
{ |
| 157 |
DictRemoverObject *self = _DictRemoverObject_CAST(myself); |
| 158 |
Py_VISIT(Py_TYPE(self)); |
| 159 |
Py_VISIT(self->key); |
| 160 |
Py_VISIT(self->dict); |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
static int |
| 165 |
_DictRemover_clear(PyObject *myself) |
| 166 |
{ |
| 167 |
DictRemoverObject *self = _DictRemoverObject_CAST(myself); |
| 168 |
Py_CLEAR(self->key); |
| 169 |
Py_CLEAR(self->dict); |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
static void |
| 174 |
_DictRemover_dealloc(PyObject *myself) |
| 175 |
{ |
| 176 |
PyTypeObject *tp = Py_TYPE(myself); |
| 177 |
PyObject_GC_UnTrack(myself); |
| 178 |
(void)_DictRemover_clear(myself); |
| 179 |
tp->tp_free(myself); |
| 180 |
Py_DECREF(tp); |
| 181 |
} |
| 182 |
|
| 183 |
static PyObject * |
| 184 |
_DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw) |
| 185 |
{ |
| 186 |
DictRemoverObject *self = _DictRemoverObject_CAST(myself); |
| 187 |
if (self->key && self->dict) { |
| 188 |
if (-1 == PyDict_DelItem(self->dict, self->key)) { |
| 189 |
PyErr_FormatUnraisable("Exception ignored while " |
| 190 |
"calling _ctypes.DictRemover"); |
| 191 |
} |
| 192 |
Py_CLEAR(self->key); |
| 193 |
Py_CLEAR(self->dict); |
| 194 |
} |
| 195 |
Py_RETURN_NONE; |
| 196 |
} |
| 197 |
|
| 198 |
PyDoc_STRVAR(dictremover_doc, "deletes a key from a dictionary"); |
| 199 |
|
| 200 |
static PyType_Slot dictremover_slots[] = { |
| 201 |
{Py_tp_dealloc, _DictRemover_dealloc}, |
| 202 |
{Py_tp_traverse, _DictRemover_traverse}, |
| 203 |
{Py_tp_clear, _DictRemover_clear}, |
| 204 |
{Py_tp_call, _DictRemover_call}, |
| 205 |
{Py_tp_doc, (void *)dictremover_doc}, |
| 206 |
{0, NULL}, |
| 207 |
}; |
| 208 |
|
| 209 |
static PyType_Spec dictremover_spec = { |
| 210 |
.name = "_ctypes.DictRemover", |
| 211 |
.basicsize = sizeof(DictRemoverObject), |
| 212 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 213 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 214 |
.slots = dictremover_slots, |
| 215 |
}; |
| 216 |
|
| 217 |
int |
| 218 |
PyDict_SetItemProxy(ctypes_state *st, PyObject *dict, PyObject *key, PyObject *item) |
| 219 |
{ |
| 220 |
PyObject *obj; |
| 221 |
DictRemoverObject *remover; |
| 222 |
PyObject *proxy; |
| 223 |
int result; |
| 224 |
|
| 225 |
obj = _PyObject_CallNoArgs((PyObject *)st->DictRemover_Type); |
| 226 |
if (obj == NULL) |
| 227 |
return -1; |
| 228 |
|
| 229 |
remover = (DictRemoverObject *)obj; |
| 230 |
assert(remover->key == NULL); |
| 231 |
assert(remover->dict == NULL); |
| 232 |
remover->key = Py_NewRef(key); |
| 233 |
remover->dict = Py_NewRef(dict); |
| 234 |
|
| 235 |
proxy = PyWeakref_NewProxy(item, obj); |
| 236 |
Py_DECREF(obj); |
| 237 |
if (proxy == NULL) |
| 238 |
return -1; |
| 239 |
|
| 240 |
result = PyDict_SetItem(dict, key, proxy); |
| 241 |
Py_DECREF(proxy); |
| 242 |
return result; |
| 243 |
} |
| 244 |
|
| 245 |
static int |
| 246 |
_PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult) |
| 247 |
{ |
| 248 |
int rc = PyDict_GetItemRef(dict, key, presult); |
| 249 |
PyObject *item = *presult; |
| 250 |
if (item && PyWeakref_CheckProxy(item)) { |
| 251 |
rc = PyWeakref_GetRef(item, presult); |
| 252 |
Py_DECREF(item); |
| 253 |
} |
| 254 |
return rc; |
| 255 |
} |
| 256 |
|
| 257 |
/******************************************************************/ |
| 258 |
|
| 259 |
/* |
| 260 |
Allocate a memory block for a pep3118 format string, filled with |
| 261 |
a suitable PEP 3118 type code corresponding to the given ctypes |
| 262 |
type. Returns NULL on failure, with the error indicator set. |
| 263 |
|
| 264 |
This produces type codes in the standard size mode (cf. struct module), |
| 265 |
since the endianness may need to be swapped to a non-native one |
| 266 |
later on. |
| 267 |
*/ |
| 268 |
static char * |
| 269 |
_ctypes_alloc_format_string_for_type(char code, int big_endian) |
| 270 |
{ |
| 271 |
char *result; |
| 272 |
char pep_code = '\0'; |
| 273 |
|
| 274 |
switch (code) { |
| 275 |
#if SIZEOF_INT == 2 |
| 276 |
case 'i': pep_code = 'h'; break; |
| 277 |
case 'I': pep_code = 'H'; break; |
| 278 |
#elif SIZEOF_INT == 4 |
| 279 |
case 'i': pep_code = 'i'; break; |
| 280 |
case 'I': pep_code = 'I'; break; |
| 281 |
#elif SIZEOF_INT == 8 |
| 282 |
case 'i': pep_code = 'q'; break; |
| 283 |
case 'I': pep_code = 'Q'; break; |
| 284 |
#else |
| 285 |
# error SIZEOF_INT has an unexpected value |
| 286 |
#endif /* SIZEOF_INT */ |
| 287 |
#if SIZEOF_LONG == 4 |
| 288 |
case 'l': pep_code = 'l'; break; |
| 289 |
case 'L': pep_code = 'L'; break; |
| 290 |
#elif SIZEOF_LONG == 8 |
| 291 |
case 'l': pep_code = 'q'; break; |
| 292 |
case 'L': pep_code = 'Q'; break; |
| 293 |
#else |
| 294 |
# error SIZEOF_LONG has an unexpected value |
| 295 |
#endif /* SIZEOF_LONG */ |
| 296 |
#if SIZEOF__BOOL == 1 |
| 297 |
case '?': pep_code = '?'; break; |
| 298 |
#elif SIZEOF__BOOL == 2 |
| 299 |
case '?': pep_code = 'H'; break; |
| 300 |
#elif SIZEOF__BOOL == 4 |
| 301 |
case '?': pep_code = 'L'; break; |
| 302 |
#elif SIZEOF__BOOL == 8 |
| 303 |
case '?': pep_code = 'Q'; break; |
| 304 |
#else |
| 305 |
# error SIZEOF__BOOL has an unexpected value |
| 306 |
#endif /* SIZEOF__BOOL */ |
| 307 |
default: |
| 308 |
/* The standard-size code is the same as the ctypes one */ |
| 309 |
pep_code = code; |
| 310 |
break; |
| 311 |
} |
| 312 |
|
| 313 |
result = PyMem_Malloc(3); |
| 314 |
if (result == NULL) { |
| 315 |
PyErr_NoMemory(); |
| 316 |
return NULL; |
| 317 |
} |
| 318 |
|
| 319 |
result[0] = big_endian ? '>' : '<'; |
| 320 |
result[1] = pep_code; |
| 321 |
result[2] = '\0'; |
| 322 |
return result; |
| 323 |
} |
| 324 |
|
| 325 |
/* |
| 326 |
Allocate a memory block for a pep3118 format string, copy prefix (if |
| 327 |
non-null) and suffix into it. Returns NULL on failure, with the error |
| 328 |
indicator set. If called with a suffix of NULL the error indicator must |
| 329 |
already be set. |
| 330 |
*/ |
| 331 |
static char * |
| 332 |
_ctypes_alloc_format_string(const char *prefix, const char *suffix) |
| 333 |
{ |
| 334 |
size_t len; |
| 335 |
char *result; |
| 336 |
|
| 337 |
if (suffix == NULL) { |
| 338 |
assert(PyErr_Occurred()); |
| 339 |
return NULL; |
| 340 |
} |
| 341 |
len = strlen(suffix); |
| 342 |
if (prefix) |
| 343 |
len += strlen(prefix); |
| 344 |
result = PyMem_Malloc(len + 1); |
| 345 |
if (result == NULL) { |
| 346 |
PyErr_NoMemory(); |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
if (prefix) |
| 350 |
strcpy(result, prefix); |
| 351 |
else |
| 352 |
result[0] = '\0'; |
| 353 |
strcat(result, suffix); |
| 354 |
return result; |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
Allocate a memory block for a pep3118 format string, adding |
| 359 |
the given prefix (if non-null), an additional shape prefix, and a suffix. |
| 360 |
Returns NULL on failure, with the error indicator set. If called with |
| 361 |
a suffix of NULL the error indicator must already be set. |
| 362 |
*/ |
| 363 |
static char * |
| 364 |
_ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape, |
| 365 |
const char *prefix, const char *suffix) |
| 366 |
{ |
| 367 |
char *new_prefix; |
| 368 |
char *result; |
| 369 |
char buf[32]; |
| 370 |
Py_ssize_t prefix_len; |
| 371 |
int k; |
| 372 |
|
| 373 |
prefix_len = 32 * ndim + 3; |
| 374 |
if (prefix) |
| 375 |
prefix_len += strlen(prefix); |
| 376 |
new_prefix = PyMem_Malloc(prefix_len); |
| 377 |
if (new_prefix == NULL) { |
| 378 |
PyErr_NoMemory(); |
| 379 |
return NULL; |
| 380 |
} |
| 381 |
new_prefix[0] = '\0'; |
| 382 |
if (prefix) |
| 383 |
strcpy(new_prefix, prefix); |
| 384 |
if (ndim > 0) { |
| 385 |
/* Add the prefix "(shape[0],shape[1],...,shape[ndim-1])" */ |
| 386 |
strcat(new_prefix, "("); |
| 387 |
for (k = 0; k < ndim; ++k) { |
| 388 |
if (k < ndim-1) { |
| 389 |
sprintf(buf, "%zd,", shape[k]); |
| 390 |
} else { |
| 391 |
sprintf(buf, "%zd)", shape[k]); |
| 392 |
} |
| 393 |
strcat(new_prefix, buf); |
| 394 |
} |
| 395 |
} |
| 396 |
result = _ctypes_alloc_format_string(new_prefix, suffix); |
| 397 |
PyMem_Free(new_prefix); |
| 398 |
return result; |
| 399 |
} |
| 400 |
|
| 401 |
/* StructParamObject and StructParam_Type are used in _ctypes_callproc() |
| 402 |
for argument.keep to call PyMem_Free(ptr) on Py_DECREF(argument). |
| 403 |
|
| 404 |
StructUnionType_paramfunc() creates such object when a ctypes Structure is |
| 405 |
passed by copy to a C function. */ |
| 406 |
typedef struct { |
| 407 |
PyObject_HEAD |
| 408 |
void *ptr; |
| 409 |
PyObject *keep; // If set, a reference to the original CDataObject. |
| 410 |
} StructParamObject; |
| 411 |
|
| 412 |
#define _StructParamObject_CAST(op) ((StructParamObject *)(op)) |
| 413 |
|
| 414 |
static int |
| 415 |
StructParam_traverse(PyObject *myself, visitproc visit, void *arg) |
| 416 |
{ |
| 417 |
StructParamObject *self = _StructParamObject_CAST(myself); |
| 418 |
Py_VISIT(Py_TYPE(self)); |
| 419 |
Py_VISIT(self->keep); |
| 420 |
return 0; |
| 421 |
} |
| 422 |
|
| 423 |
static int |
| 424 |
StructParam_clear(PyObject *myself) |
| 425 |
{ |
| 426 |
StructParamObject *self = _StructParamObject_CAST(myself); |
| 427 |
Py_CLEAR(self->keep); |
| 428 |
return 0; |
| 429 |
} |
| 430 |
|
| 431 |
static void |
| 432 |
StructParam_dealloc(PyObject *myself) |
| 433 |
{ |
| 434 |
StructParamObject *self = _StructParamObject_CAST(myself); |
| 435 |
PyTypeObject *tp = Py_TYPE(self); |
| 436 |
PyObject_GC_UnTrack(myself); |
| 437 |
(void)StructParam_clear(myself); |
| 438 |
PyMem_Free(self->ptr); |
| 439 |
tp->tp_free(myself); |
| 440 |
Py_DECREF(tp); |
| 441 |
} |
| 442 |
|
| 443 |
static PyType_Slot structparam_slots[] = { |
| 444 |
{Py_tp_traverse, StructParam_traverse}, |
| 445 |
{Py_tp_clear, StructParam_clear}, |
| 446 |
{Py_tp_dealloc, StructParam_dealloc}, |
| 447 |
{0, NULL}, |
| 448 |
}; |
| 449 |
|
| 450 |
static PyType_Spec structparam_spec = { |
| 451 |
.name = "_ctypes.StructParam_Type", |
| 452 |
.basicsize = sizeof(StructParamObject), |
| 453 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | |
| 454 |
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_DISALLOW_INSTANTIATION), |
| 455 |
.slots = structparam_slots, |
| 456 |
}; |
| 457 |
|
| 458 |
/* |
| 459 |
CType_Type - a base metaclass. Its instances (classes) have a StgInfo. |
| 460 |
*/ |
| 461 |
|
| 462 |
/*[clinic input] |
| 463 |
class _ctypes.CType_Type "PyObject *" "clinic_state()->CType_Type" |
| 464 |
[clinic start generated code]*/ |
| 465 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=8389fc5b74a84f2a]*/ |
| 466 |
|
| 467 |
static int |
| 468 |
CType_Type_traverse(PyObject *self, visitproc visit, void *arg) |
| 469 |
{ |
| 470 |
StgInfo *info = _PyStgInfo_FromType_NoState(self); |
| 471 |
if (!info) { |
| 472 |
PyErr_FormatUnraisable("Exception ignored while " |
| 473 |
"calling ctypes traverse function %R", self); |
| 474 |
} |
| 475 |
if (info) { |
| 476 |
Py_VISIT(info->proto); |
| 477 |
Py_VISIT(info->argtypes); |
| 478 |
Py_VISIT(info->converters); |
| 479 |
Py_VISIT(info->restype); |
| 480 |
Py_VISIT(info->checker); |
| 481 |
Py_VISIT(info->pointer_type); |
| 482 |
Py_VISIT(info->module); |
| 483 |
} |
| 484 |
Py_VISIT(Py_TYPE(self)); |
| 485 |
return PyType_Type.tp_traverse(self, visit, arg); |
| 486 |
} |
| 487 |
|
| 488 |
void |
| 489 |
ctype_clear_stginfo(StgInfo *info) |
| 490 |
{ |
| 491 |
assert(info); |
| 492 |
Py_CLEAR(info->proto); |
| 493 |
Py_CLEAR(info->argtypes); |
| 494 |
Py_CLEAR(info->converters); |
| 495 |
Py_CLEAR(info->restype); |
| 496 |
Py_CLEAR(info->checker); |
| 497 |
Py_CLEAR(info->pointer_type); |
| 498 |
Py_CLEAR(info->module); // decref the module last |
| 499 |
} |
| 500 |
|
| 501 |
void |
| 502 |
ctype_free_stginfo_members(StgInfo *info) |
| 503 |
{ |
| 504 |
assert(info); |
| 505 |
|
| 506 |
PyMem_Free(info->ffi_type_pointer.elements); |
| 507 |
info->ffi_type_pointer.elements = NULL; |
| 508 |
PyMem_Free(info->format); |
| 509 |
info->format = NULL; |
| 510 |
PyMem_Free(info->shape); |
| 511 |
info->shape = NULL; |
| 512 |
ctype_clear_stginfo(info); |
| 513 |
} |
| 514 |
|
| 515 |
static int |
| 516 |
CType_Type_clear(PyObject *self) |
| 517 |
{ |
| 518 |
StgInfo *info = _PyStgInfo_FromType_NoState(self); |
| 519 |
if (!info) { |
| 520 |
PyErr_FormatUnraisable("Exception ignored while " |
| 521 |
"clearing ctypes %R", self); |
| 522 |
} |
| 523 |
if (info) { |
| 524 |
ctype_clear_stginfo(info); |
| 525 |
} |
| 526 |
return PyType_Type.tp_clear(self); |
| 527 |
} |
| 528 |
|
| 529 |
static void |
| 530 |
CType_Type_dealloc(PyObject *self) |
| 531 |
{ |
| 532 |
StgInfo *info = _PyStgInfo_FromType_NoState(self); |
| 533 |
if (!info) { |
| 534 |
PyErr_FormatUnraisable("Exception ignored while " |
| 535 |
"deallocating ctypes %R", self); |
| 536 |
} |
| 537 |
if (info) { |
| 538 |
ctype_free_stginfo_members(info); |
| 539 |
} |
| 540 |
|
| 541 |
PyTypeObject *tp = Py_TYPE(self); |
| 542 |
PyType_Type.tp_dealloc(self); |
| 543 |
Py_DECREF(tp); |
| 544 |
} |
| 545 |
|
| 546 |
/*[clinic input] |
| 547 |
_ctypes.CType_Type.__sizeof__ |
| 548 |
|
| 549 |
cls: defining_class |
| 550 |
/ |
| 551 |
Return memory consumption of the type object. |
| 552 |
[clinic start generated code]*/ |
| 553 |
|
| 554 |
static PyObject * |
| 555 |
_ctypes_CType_Type___sizeof___impl(PyObject *self, PyTypeObject *cls) |
| 556 |
/*[clinic end generated code: output=c68c235be84d03f3 input=d064433b6110d1ce]*/ |
| 557 |
{ |
| 558 |
Py_ssize_t size = Py_TYPE(self)->tp_basicsize; |
| 559 |
size += Py_TYPE(self)->tp_itemsize * Py_SIZE(self); |
| 560 |
|
| 561 |
ctypes_state *st = get_module_state_by_class(cls); |
| 562 |
StgInfo *info; |
| 563 |
if (PyStgInfo_FromType(st, self, &info) < 0) { |
| 564 |
return NULL; |
| 565 |
} |
| 566 |
if (info) { |
| 567 |
if (info->format) { |
| 568 |
size += strlen(info->format) + 1; |
| 569 |
} |
| 570 |
if (info->ffi_type_pointer.elements) { |
| 571 |
size += (info->length + 1) * sizeof(ffi_type *); |
| 572 |
} |
| 573 |
size += info->ndim * sizeof(Py_ssize_t); |
| 574 |
} |
| 575 |
|
| 576 |
return PyLong_FromSsize_t(size); |
| 577 |
} |
| 578 |
|
| 579 |
static PyObject * |
| 580 |
ctype_get_pointer_type(PyObject *self, void *Py_UNUSED(ignored)) |
| 581 |
{ |
| 582 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 583 |
StgInfo *info; |
| 584 |
if (PyStgInfo_FromType(st, self, &info) < 0) { |
| 585 |
return NULL; |
| 586 |
} |
| 587 |
if (!info) { |
| 588 |
PyErr_Format(PyExc_TypeError, "%R must have storage info", self); |
| 589 |
return NULL; |
| 590 |
} |
| 591 |
|
| 592 |
if (info->pointer_type) { |
| 593 |
return Py_NewRef(info->pointer_type); |
| 594 |
} |
| 595 |
|
| 596 |
PyErr_Format(PyExc_AttributeError, |
| 597 |
"%R has no attribute '__pointer_type__'", |
| 598 |
self); |
| 599 |
return NULL; |
| 600 |
} |
| 601 |
|
| 602 |
static int |
| 603 |
ctype_set_pointer_type(PyObject *self, PyObject *tp, void *Py_UNUSED(ignored)) |
| 604 |
{ |
| 605 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 606 |
StgInfo *info; |
| 607 |
if (PyStgInfo_FromType(st, self, &info) < 0) { |
| 608 |
return -1; |
| 609 |
} |
| 610 |
if (!info) { |
| 611 |
PyErr_Format(PyExc_TypeError, "%R must have storage info", self); |
| 612 |
return -1; |
| 613 |
} |
| 614 |
|
| 615 |
Py_XSETREF(info->pointer_type, Py_XNewRef(tp)); |
| 616 |
return 0; |
| 617 |
} |
| 618 |
|
| 619 |
static PyObject * |
| 620 |
CType_Type_repeat(PyObject *self, Py_ssize_t length); |
| 621 |
|
| 622 |
|
| 623 |
static PyMethodDef ctype_methods[] = { |
| 624 |
_CTYPES_CTYPE_TYPE___SIZEOF___METHODDEF |
| 625 |
{0}, |
| 626 |
}; |
| 627 |
|
| 628 |
static PyGetSetDef ctype_getsets[] = { |
| 629 |
{ "__pointer_type__", ctype_get_pointer_type, ctype_set_pointer_type, |
| 630 |
"pointer type", NULL }, |
| 631 |
{ NULL, NULL } |
| 632 |
}; |
| 633 |
|
| 634 |
static PyType_Slot ctype_type_slots[] = { |
| 635 |
{Py_tp_token, Py_TP_USE_SPEC}, |
| 636 |
{Py_tp_traverse, CType_Type_traverse}, |
| 637 |
{Py_tp_clear, CType_Type_clear}, |
| 638 |
{Py_tp_dealloc, CType_Type_dealloc}, |
| 639 |
{Py_tp_methods, ctype_methods}, |
| 640 |
{Py_tp_getset, ctype_getsets}, |
| 641 |
// Sequence protocol. |
| 642 |
{Py_sq_repeat, CType_Type_repeat}, |
| 643 |
{0, NULL}, |
| 644 |
}; |
| 645 |
|
| 646 |
PyType_Spec pyctype_type_spec = { |
| 647 |
.name = "_ctypes.CType_Type", |
| 648 |
.basicsize = -(Py_ssize_t)sizeof(StgInfo), |
| 649 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | |
| 650 |
Py_TPFLAGS_HAVE_GC | |
| 651 |
Py_TPFLAGS_BASETYPE ), |
| 652 |
.slots = ctype_type_slots, |
| 653 |
}; |
| 654 |
|
| 655 |
/* |
| 656 |
PyCStructType_Type - a meta type/class. Creating a new class using this one as |
| 657 |
__metaclass__ will call the constructor StructUnionType_new. |
| 658 |
It initializes the C accessible fields somehow. |
| 659 |
*/ |
| 660 |
|
| 661 |
static PyCArgObject * |
| 662 |
StructUnionType_paramfunc(ctypes_state *st, CDataObject *self) |
| 663 |
{ |
| 664 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 665 |
PyCArgObject *parg; |
| 666 |
PyObject *obj; |
| 667 |
void *ptr; |
| 668 |
|
| 669 |
if ((size_t)self->b_size > sizeof(void*)) { |
| 670 |
ptr = PyMem_Malloc(self->b_size); |
| 671 |
if (ptr == NULL) { |
| 672 |
return NULL; |
| 673 |
} |
| 674 |
memcpy(ptr, self->b_ptr, self->b_size); |
| 675 |
|
| 676 |
/* Create a Python object which calls PyMem_Free(ptr) in |
| 677 |
its deallocator. The object will be destroyed |
| 678 |
at _ctypes_callproc() cleanup. */ |
| 679 |
PyTypeObject *tp = st->StructParam_Type; |
| 680 |
obj = tp->tp_alloc(tp, 0); |
| 681 |
if (obj == NULL) { |
| 682 |
PyMem_Free(ptr); |
| 683 |
return NULL; |
| 684 |
} |
| 685 |
|
| 686 |
StructParamObject *struct_param = (StructParamObject *)obj; |
| 687 |
struct_param->ptr = ptr; |
| 688 |
struct_param->keep = Py_NewRef(self); |
| 689 |
} else { |
| 690 |
ptr = self->b_ptr; |
| 691 |
obj = Py_NewRef(self); |
| 692 |
} |
| 693 |
|
| 694 |
parg = PyCArgObject_new(st); |
| 695 |
if (parg == NULL) { |
| 696 |
Py_DECREF(obj); |
| 697 |
return NULL; |
| 698 |
} |
| 699 |
|
| 700 |
StgInfo *stginfo; |
| 701 |
if (PyStgInfo_FromObject(st, (PyObject *)self, &stginfo) < 0) { |
| 702 |
Py_DECREF(obj); |
| 703 |
return NULL; |
| 704 |
} |
| 705 |
assert(stginfo); /* Cannot be NULL for structure/union instances */ |
| 706 |
|
| 707 |
parg->tag = 'V'; |
| 708 |
parg->pffi_type = &stginfo->ffi_type_pointer; |
| 709 |
parg->value.p = ptr; |
| 710 |
parg->size = self->b_size; |
| 711 |
parg->obj = obj; |
| 712 |
return parg; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
static int |
| 717 |
StructUnionType_init(PyObject *self, PyObject *args, PyObject *kwds, int isStruct) |
| 718 |
{ |
| 719 |
PyObject *fields; |
| 720 |
|
| 721 |
PyObject *attrdict = PyType_GetDict((PyTypeObject *)self); |
| 722 |
if (!attrdict) { |
| 723 |
return -1; |
| 724 |
} |
| 725 |
|
| 726 |
/* keep this for bw compatibility */ |
| 727 |
int r = PyDict_Contains(attrdict, &_Py_ID(_abstract_)); |
| 728 |
if (r > 0) { |
| 729 |
Py_DECREF(attrdict); |
| 730 |
return 0; |
| 731 |
} |
| 732 |
if (r < 0) { |
| 733 |
Py_DECREF(attrdict); |
| 734 |
return -1; |
| 735 |
} |
| 736 |
|
| 737 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 738 |
StgInfo *info = PyStgInfo_Init(st, (PyTypeObject *)self); |
| 739 |
if (!info) { |
| 740 |
Py_DECREF(attrdict); |
| 741 |
return -1; |
| 742 |
} |
| 743 |
|
| 744 |
info->format = _ctypes_alloc_format_string(NULL, "B"); |
| 745 |
if (info->format == NULL) { |
| 746 |
Py_DECREF(attrdict); |
| 747 |
return -1; |
| 748 |
} |
| 749 |
|
| 750 |
info->paramfunc = StructUnionType_paramfunc; |
| 751 |
|
| 752 |
if (PyDict_GetItemRef(attrdict, &_Py_ID(_fields_), &fields) < 0) { |
| 753 |
Py_DECREF(attrdict); |
| 754 |
return -1; |
| 755 |
} |
| 756 |
Py_CLEAR(attrdict); |
| 757 |
if (fields) { |
| 758 |
if (PyObject_SetAttr(self, &_Py_ID(_fields_), fields) < 0) { |
| 759 |
Py_DECREF(fields); |
| 760 |
return -1; |
| 761 |
} |
| 762 |
Py_DECREF(fields); |
| 763 |
return 0; |
| 764 |
} |
| 765 |
else { |
| 766 |
StgInfo *baseinfo; |
| 767 |
if (PyStgInfo_FromType(st, (PyObject *)((PyTypeObject *)self)->tp_base, |
| 768 |
&baseinfo) < 0) { |
| 769 |
return -1; |
| 770 |
} |
| 771 |
if (baseinfo == NULL) { |
| 772 |
return 0; |
| 773 |
} |
| 774 |
int ret = 0; |
| 775 |
STGINFO_LOCK(baseinfo); |
| 776 |
/* copy base info */ |
| 777 |
ret = PyCStgInfo_clone(info, baseinfo); |
| 778 |
if (ret >= 0) { |
| 779 |
stginfo_set_dict_final_lock_held(baseinfo); /* set the 'final' flag in the baseclass info */ |
| 780 |
} |
| 781 |
STGINFO_UNLOCK(); |
| 782 |
return ret; |
| 783 |
} |
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
static int |
| 788 |
PyCStructType_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 789 |
{ |
| 790 |
return StructUnionType_init(self, args, kwds, 1); |
| 791 |
} |
| 792 |
|
| 793 |
static int |
| 794 |
UnionType_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 795 |
{ |
| 796 |
return StructUnionType_init(self, args, kwds, 0); |
| 797 |
} |
| 798 |
|
| 799 |
/*[clinic input] |
| 800 |
class _ctypes.CDataType "PyObject *" "clinic_state()->CType_Type" |
| 801 |
[clinic start generated code]*/ |
| 802 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=466a505a93d73156]*/ |
| 803 |
|
| 804 |
|
| 805 |
/*[clinic input] |
| 806 |
_ctypes.CDataType.from_address as CDataType_from_address |
| 807 |
|
| 808 |
type: self |
| 809 |
cls: defining_class |
| 810 |
value: object |
| 811 |
/ |
| 812 |
|
| 813 |
C.from_address(integer) -> C instance |
| 814 |
|
| 815 |
Access a C instance at the specified address. |
| 816 |
[clinic start generated code]*/ |
| 817 |
|
| 818 |
static PyObject * |
| 819 |
CDataType_from_address_impl(PyObject *type, PyTypeObject *cls, |
| 820 |
PyObject *value) |
| 821 |
/*[clinic end generated code: output=5be4a7c0d9aa6c74 input=827a22cefe380c01]*/ |
| 822 |
{ |
| 823 |
void *buf; |
| 824 |
if (!PyLong_Check(value)) { |
| 825 |
PyErr_SetString(PyExc_TypeError, |
| 826 |
"integer expected"); |
| 827 |
return NULL; |
| 828 |
} |
| 829 |
buf = (void *)PyLong_AsVoidPtr(value); |
| 830 |
if (PyErr_Occurred()) |
| 831 |
return NULL; |
| 832 |
ctypes_state *st = get_module_state_by_class(cls); |
| 833 |
return PyCData_AtAddress(st, type, buf); |
| 834 |
} |
| 835 |
|
| 836 |
static int |
| 837 |
KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep); |
| 838 |
|
| 839 |
/*[clinic input] |
| 840 |
_ctypes.CDataType.from_buffer as CDataType_from_buffer |
| 841 |
|
| 842 |
type: self |
| 843 |
cls: defining_class |
| 844 |
obj: object |
| 845 |
offset: Py_ssize_t = 0 |
| 846 |
/ |
| 847 |
|
| 848 |
C.from_buffer(object, offset=0) -> C instance |
| 849 |
|
| 850 |
Create a C instance from a writeable buffer. |
| 851 |
[clinic start generated code]*/ |
| 852 |
|
| 853 |
static PyObject * |
| 854 |
CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj, |
| 855 |
Py_ssize_t offset) |
| 856 |
/*[clinic end generated code: output=57604e99635abd31 input=0f36cedd105ca28d]*/ |
| 857 |
{ |
| 858 |
PyObject *mv; |
| 859 |
PyObject *result; |
| 860 |
Py_buffer *buffer; |
| 861 |
|
| 862 |
ctypes_state *st = get_module_state_by_class(cls); |
| 863 |
StgInfo *info; |
| 864 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 865 |
return NULL; |
| 866 |
} |
| 867 |
if (!info) { |
| 868 |
PyErr_SetString(PyExc_TypeError, "abstract class"); |
| 869 |
return NULL; |
| 870 |
} |
| 871 |
|
| 872 |
mv = PyMemoryView_FromObject(obj); |
| 873 |
if (mv == NULL) |
| 874 |
return NULL; |
| 875 |
|
| 876 |
buffer = PyMemoryView_GET_BUFFER(mv); |
| 877 |
|
| 878 |
if (buffer->readonly) { |
| 879 |
PyErr_SetString(PyExc_TypeError, |
| 880 |
"underlying buffer is not writable"); |
| 881 |
Py_DECREF(mv); |
| 882 |
return NULL; |
| 883 |
} |
| 884 |
|
| 885 |
if (!PyBuffer_IsContiguous(buffer, 'C')) { |
| 886 |
PyErr_SetString(PyExc_TypeError, |
| 887 |
"underlying buffer is not C contiguous"); |
| 888 |
Py_DECREF(mv); |
| 889 |
return NULL; |
| 890 |
} |
| 891 |
|
| 892 |
if (offset < 0) { |
| 893 |
PyErr_SetString(PyExc_ValueError, |
| 894 |
"offset cannot be negative"); |
| 895 |
Py_DECREF(mv); |
| 896 |
return NULL; |
| 897 |
} |
| 898 |
|
| 899 |
if (info->size > buffer->len - offset) { |
| 900 |
PyErr_Format(PyExc_ValueError, |
| 901 |
"Buffer size too small " |
| 902 |
"(%zd instead of at least %zd bytes)", |
| 903 |
buffer->len, info->size + offset); |
| 904 |
Py_DECREF(mv); |
| 905 |
return NULL; |
| 906 |
} |
| 907 |
|
| 908 |
if (PySys_Audit("ctypes.cdata/buffer", "nnn", |
| 909 |
(Py_ssize_t)buffer->buf, buffer->len, offset) < 0) { |
| 910 |
Py_DECREF(mv); |
| 911 |
return NULL; |
| 912 |
} |
| 913 |
|
| 914 |
result = PyCData_AtAddress(st, type, (char *)buffer->buf + offset); |
| 915 |
if (result == NULL) { |
| 916 |
Py_DECREF(mv); |
| 917 |
return NULL; |
| 918 |
} |
| 919 |
|
| 920 |
if (-1 == KeepRef((CDataObject *)result, -1, mv)) { |
| 921 |
Py_DECREF(result); |
| 922 |
return NULL; |
| 923 |
} |
| 924 |
|
| 925 |
return result; |
| 926 |
} |
| 927 |
|
| 928 |
static inline PyObject * |
| 929 |
generic_pycdata_new(ctypes_state *st, |
| 930 |
PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 931 |
|
| 932 |
static PyObject * |
| 933 |
GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds); |
| 934 |
|
| 935 |
/*[clinic input] |
| 936 |
_ctypes.CDataType.from_buffer_copy as CDataType_from_buffer_copy |
| 937 |
|
| 938 |
type: self |
| 939 |
cls: defining_class |
| 940 |
buffer: Py_buffer |
| 941 |
offset: Py_ssize_t = 0 |
| 942 |
/ |
| 943 |
|
| 944 |
C.from_buffer_copy(object, offset=0) -> C instance |
| 945 |
|
| 946 |
Create a C instance from a readable buffer. |
| 947 |
[clinic start generated code]*/ |
| 948 |
|
| 949 |
static PyObject * |
| 950 |
CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls, |
| 951 |
Py_buffer *buffer, Py_ssize_t offset) |
| 952 |
/*[clinic end generated code: output=c8fc62b03e5cc6fa input=2a81e11b765a6253]*/ |
| 953 |
{ |
| 954 |
PyObject *result; |
| 955 |
|
| 956 |
ctypes_state *st = get_module_state_by_class(cls); |
| 957 |
StgInfo *info; |
| 958 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 959 |
return NULL; |
| 960 |
} |
| 961 |
if (!info) { |
| 962 |
PyErr_SetString(PyExc_TypeError, "abstract class"); |
| 963 |
return NULL; |
| 964 |
} |
| 965 |
|
| 966 |
if (offset < 0) { |
| 967 |
PyErr_SetString(PyExc_ValueError, |
| 968 |
"offset cannot be negative"); |
| 969 |
return NULL; |
| 970 |
} |
| 971 |
|
| 972 |
if (info->size > buffer->len - offset) { |
| 973 |
PyErr_Format(PyExc_ValueError, |
| 974 |
"Buffer size too small (%zd instead of at least %zd bytes)", |
| 975 |
buffer->len, info->size + offset); |
| 976 |
return NULL; |
| 977 |
} |
| 978 |
|
| 979 |
if (PySys_Audit("ctypes.cdata/buffer", "nnn", |
| 980 |
(Py_ssize_t)buffer->buf, buffer->len, offset) < 0) { |
| 981 |
return NULL; |
| 982 |
} |
| 983 |
|
| 984 |
result = generic_pycdata_new(st, (PyTypeObject *)type, NULL, NULL); |
| 985 |
if (result != NULL) { |
| 986 |
assert(_PyObject_IsUniquelyReferenced(result)); |
| 987 |
memcpy(((CDataObject *) result)->b_ptr, (char *)buffer->buf + offset, info->size); |
| 988 |
} |
| 989 |
return result; |
| 990 |
} |
| 991 |
|
| 992 |
/*[clinic input] |
| 993 |
_ctypes.CDataType.in_dll as CDataType_in_dll |
| 994 |
|
| 995 |
type: self |
| 996 |
cls: defining_class |
| 997 |
dll: object |
| 998 |
name: str |
| 999 |
/ |
| 1000 |
|
| 1001 |
C.in_dll(dll, name) -> C instance |
| 1002 |
|
| 1003 |
Access a C instance in a dll. |
| 1004 |
[clinic start generated code]*/ |
| 1005 |
|
| 1006 |
static PyObject * |
| 1007 |
CDataType_in_dll_impl(PyObject *type, PyTypeObject *cls, PyObject *dll, |
| 1008 |
const char *name) |
| 1009 |
/*[clinic end generated code: output=d0e5c43b66bfa21f input=f85bf281477042b4]*/ |
| 1010 |
{ |
| 1011 |
PyObject *obj; |
| 1012 |
void *handle; |
| 1013 |
void *address; |
| 1014 |
|
| 1015 |
if (PySys_Audit("ctypes.dlsym", "Os", dll, name) < 0) { |
| 1016 |
return NULL; |
| 1017 |
} |
| 1018 |
|
| 1019 |
obj = PyObject_GetAttrString(dll, "_handle"); |
| 1020 |
if (!obj) |
| 1021 |
return NULL; |
| 1022 |
if (!PyLong_Check(obj)) { |
| 1023 |
PyErr_SetString(PyExc_TypeError, |
| 1024 |
"the _handle attribute of the second argument must be an integer"); |
| 1025 |
Py_DECREF(obj); |
| 1026 |
return NULL; |
| 1027 |
} |
| 1028 |
handle = (void *)PyLong_AsVoidPtr(obj); |
| 1029 |
Py_DECREF(obj); |
| 1030 |
if (PyErr_Occurred()) { |
| 1031 |
PyErr_SetString(PyExc_ValueError, |
| 1032 |
"could not convert the _handle attribute to a pointer"); |
| 1033 |
return NULL; |
| 1034 |
} |
| 1035 |
|
| 1036 |
#undef USE_DLERROR |
| 1037 |
#ifdef MS_WIN32 |
| 1038 |
Py_BEGIN_ALLOW_THREADS |
| 1039 |
address = (void *)GetProcAddress(handle, name); |
| 1040 |
Py_END_ALLOW_THREADS |
| 1041 |
#else |
| 1042 |
#ifdef __CYGWIN__ |
| 1043 |
// dlerror() isn't very helpful on cygwin |
| 1044 |
#else |
| 1045 |
#define USE_DLERROR |
| 1046 |
/* dlerror() always returns the latest error. |
| 1047 |
* |
| 1048 |
* Clear the previous value before calling dlsym(), |
| 1049 |
* to ensure we can tell if our call resulted in an error. |
| 1050 |
*/ |
| 1051 |
(void)dlerror(); |
| 1052 |
#endif |
| 1053 |
address = (void *)dlsym(handle, name); |
| 1054 |
#endif |
| 1055 |
|
| 1056 |
if (address) { |
| 1057 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 1058 |
return PyCData_AtAddress(st, type, address); |
| 1059 |
} |
| 1060 |
|
| 1061 |
#ifdef USE_DLERROR |
| 1062 |
const char *dlerr = dlerror(); |
| 1063 |
if (dlerr) { |
| 1064 |
_PyErr_SetLocaleString(PyExc_ValueError, dlerr); |
| 1065 |
return NULL; |
| 1066 |
} |
| 1067 |
#endif |
| 1068 |
#undef USE_DLERROR |
| 1069 |
PyErr_Format(PyExc_ValueError, "symbol '%s' not found", name); |
| 1070 |
return NULL; |
| 1071 |
} |
| 1072 |
|
| 1073 |
/*[clinic input] |
| 1074 |
_ctypes.CDataType.from_param as CDataType_from_param |
| 1075 |
|
| 1076 |
type: self |
| 1077 |
cls: defining_class |
| 1078 |
value: object |
| 1079 |
/ |
| 1080 |
|
| 1081 |
Convert a Python object into a function call parameter. |
| 1082 |
[clinic start generated code]*/ |
| 1083 |
|
| 1084 |
static PyObject * |
| 1085 |
CDataType_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value) |
| 1086 |
/*[clinic end generated code: output=8da9e34263309f9e input=275a52c4899ddff0]*/ |
| 1087 |
{ |
| 1088 |
PyObject *as_parameter; |
| 1089 |
int res = PyObject_IsInstance(value, type); |
| 1090 |
if (res == -1) |
| 1091 |
return NULL; |
| 1092 |
if (res) { |
| 1093 |
return Py_NewRef(value); |
| 1094 |
} |
| 1095 |
ctypes_state *st = get_module_state_by_class(cls); |
| 1096 |
if (PyCArg_CheckExact(st, value)) { |
| 1097 |
PyCArgObject *p = (PyCArgObject *)value; |
| 1098 |
PyObject *ob = p->obj; |
| 1099 |
const char *ob_name; |
| 1100 |
StgInfo *info; |
| 1101 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 1102 |
return NULL; |
| 1103 |
} |
| 1104 |
/* If we got a PyCArgObject, we must check if the object packed in it |
| 1105 |
is an instance of the type's info->proto */ |
| 1106 |
if(info && ob) { |
| 1107 |
res = PyObject_IsInstance(ob, info->proto); |
| 1108 |
if (res == -1) |
| 1109 |
return NULL; |
| 1110 |
if (res) { |
| 1111 |
return Py_NewRef(value); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???"; |
| 1115 |
PyErr_Format(PyExc_TypeError, |
| 1116 |
"expected %s instance instead of pointer to %s", |
| 1117 |
((PyTypeObject *)type)->tp_name, ob_name); |
| 1118 |
return NULL; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) { |
| 1122 |
return NULL; |
| 1123 |
} |
| 1124 |
if (as_parameter) { |
| 1125 |
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) { |
| 1126 |
Py_DECREF(as_parameter); |
| 1127 |
return NULL; |
| 1128 |
} |
| 1129 |
value = CDataType_from_param_impl(type, cls, as_parameter); |
| 1130 |
Py_DECREF(as_parameter); |
| 1131 |
_Py_LeaveRecursiveCall(); |
| 1132 |
return value; |
| 1133 |
} |
| 1134 |
PyErr_Format(PyExc_TypeError, |
| 1135 |
"expected %s instance instead of %s", |
| 1136 |
((PyTypeObject *)type)->tp_name, |
| 1137 |
Py_TYPE(value)->tp_name); |
| 1138 |
return NULL; |
| 1139 |
} |
| 1140 |
|
| 1141 |
static PyMethodDef CDataType_methods[] = { |
| 1142 |
CDATATYPE_FROM_PARAM_METHODDEF |
| 1143 |
CDATATYPE_FROM_ADDRESS_METHODDEF |
| 1144 |
CDATATYPE_FROM_BUFFER_METHODDEF |
| 1145 |
CDATATYPE_FROM_BUFFER_COPY_METHODDEF |
| 1146 |
CDATATYPE_IN_DLL_METHODDEF |
| 1147 |
{ NULL, NULL }, |
| 1148 |
}; |
| 1149 |
|
| 1150 |
static PyObject * |
| 1151 |
CType_Type_repeat(PyObject *self, Py_ssize_t length) |
| 1152 |
{ |
| 1153 |
if (length < 0) |
| 1154 |
return PyErr_Format(PyExc_ValueError, |
| 1155 |
"Array length must be >= 0, not %zd", |
| 1156 |
length); |
| 1157 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 1158 |
return PyCArrayType_from_ctype(st, self, length); |
| 1159 |
} |
| 1160 |
|
| 1161 |
static int |
| 1162 |
_structunion_setattro(PyObject *self, PyObject *key, PyObject *value, int is_struct) |
| 1163 |
{ |
| 1164 |
/* XXX Should we disallow deleting _fields_? */ |
| 1165 |
if (PyUnicode_Check(key) |
| 1166 |
&& _PyUnicode_EqualToASCIIString(key, "_fields_")) |
| 1167 |
{ |
| 1168 |
if (PyCStructUnionType_update_stginfo(self, value, is_struct) < 0) { |
| 1169 |
return -1; |
| 1170 |
} |
| 1171 |
} |
| 1172 |
|
| 1173 |
return PyType_Type.tp_setattro(self, key, value); |
| 1174 |
} |
| 1175 |
|
| 1176 |
static int |
| 1177 |
PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value) |
| 1178 |
{ |
| 1179 |
return _structunion_setattro(self, key, value, 1); |
| 1180 |
} |
| 1181 |
|
| 1182 |
static int |
| 1183 |
UnionType_setattro(PyObject *self, PyObject *key, PyObject *value) |
| 1184 |
{ |
| 1185 |
return _structunion_setattro(self, key, value, 0); |
| 1186 |
} |
| 1187 |
|
| 1188 |
static PyType_Slot pycstruct_type_slots[] = { |
| 1189 |
{Py_tp_setattro, PyCStructType_setattro}, |
| 1190 |
{Py_tp_doc, PyDoc_STR("metatype for the CData Objects")}, |
| 1191 |
{Py_tp_methods, CDataType_methods}, |
| 1192 |
{Py_tp_init, PyCStructType_init}, |
| 1193 |
{0, NULL}, |
| 1194 |
}; |
| 1195 |
|
| 1196 |
static PyType_Spec pycstruct_type_spec = { |
| 1197 |
.name = "_ctypes.PyCStructType", |
| 1198 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1199 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 1200 |
.slots = pycstruct_type_slots, |
| 1201 |
}; |
| 1202 |
|
| 1203 |
static PyType_Slot union_type_slots[] = { |
| 1204 |
{Py_tp_setattro, UnionType_setattro}, |
| 1205 |
{Py_tp_doc, PyDoc_STR("metatype for the Union Objects")}, |
| 1206 |
{Py_tp_methods, CDataType_methods}, |
| 1207 |
{Py_tp_init, UnionType_init}, |
| 1208 |
{0, NULL}, |
| 1209 |
}; |
| 1210 |
|
| 1211 |
static PyType_Spec union_type_spec = { |
| 1212 |
.name = "_ctypes.UnionType", |
| 1213 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1214 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 1215 |
.slots = union_type_slots, |
| 1216 |
}; |
| 1217 |
|
| 1218 |
/******************************************************************/ |
| 1219 |
|
| 1220 |
/* |
| 1221 |
|
| 1222 |
The PyCPointerType_Type metaclass must ensure that the subclass of Pointer can be |
| 1223 |
created. It must check for a _type_ attribute in the class. Since are no |
| 1224 |
runtime created properties, a CField is probably *not* needed ? |
| 1225 |
|
| 1226 |
class IntPointer(Pointer): |
| 1227 |
_type_ = "i" |
| 1228 |
|
| 1229 |
The PyCPointer_Type provides the functionality: a contents method/property, a |
| 1230 |
size property/method, and the sequence protocol. |
| 1231 |
|
| 1232 |
*/ |
| 1233 |
|
| 1234 |
/*[clinic input] |
| 1235 |
class _ctypes.PyCPointerType "PyObject *" "clinic_state()->PyCPointerType_Type" |
| 1236 |
[clinic start generated code]*/ |
| 1237 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=c45e96c1f7645ab7]*/ |
| 1238 |
|
| 1239 |
|
| 1240 |
static int |
| 1241 |
PyCPointerType_SetProto(ctypes_state *st, PyObject *self, StgInfo *stginfo, PyObject *proto) |
| 1242 |
{ |
| 1243 |
if (!proto || !PyType_Check(proto)) { |
| 1244 |
PyErr_SetString(PyExc_TypeError, |
| 1245 |
"_type_ must be a type"); |
| 1246 |
return -1; |
| 1247 |
} |
| 1248 |
StgInfo *info; |
| 1249 |
if (PyStgInfo_FromType(st, proto, &info) < 0) { |
| 1250 |
return -1; |
| 1251 |
} |
| 1252 |
if (!info) { |
| 1253 |
PyErr_Format(PyExc_TypeError, "%R must have storage info", proto); |
| 1254 |
return -1; |
| 1255 |
} |
| 1256 |
Py_XSETREF(stginfo->proto, Py_NewRef(proto)); |
| 1257 |
if (info->pointer_type == NULL) { |
| 1258 |
Py_XSETREF(info->pointer_type, Py_NewRef(self)); |
| 1259 |
} |
| 1260 |
return 0; |
| 1261 |
} |
| 1262 |
|
| 1263 |
static PyCArgObject * |
| 1264 |
PyCPointerType_paramfunc(ctypes_state *st, CDataObject *self) |
| 1265 |
{ |
| 1266 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 1267 |
PyCArgObject *parg; |
| 1268 |
|
| 1269 |
parg = PyCArgObject_new(st); |
| 1270 |
if (parg == NULL) |
| 1271 |
return NULL; |
| 1272 |
|
| 1273 |
parg->tag = 'P'; |
| 1274 |
parg->pffi_type = &ffi_type_pointer; |
| 1275 |
parg->obj = Py_NewRef(self); |
| 1276 |
parg->value.p = *(void **)self->b_ptr; |
| 1277 |
return parg; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static int |
| 1281 |
PyCPointerType_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1282 |
{ |
| 1283 |
PyObject *proto; |
| 1284 |
PyObject *typedict; |
| 1285 |
|
| 1286 |
typedict = PyTuple_GetItem(args, 2); |
| 1287 |
if (!typedict) { |
| 1288 |
return -1; |
| 1289 |
} |
| 1290 |
|
| 1291 |
/* |
| 1292 |
stginfo items size, align, length contain info about pointers itself, |
| 1293 |
stginfo->proto has info about the pointed to type! |
| 1294 |
*/ |
| 1295 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 1296 |
StgInfo *stginfo = PyStgInfo_Init(st, (PyTypeObject *)self); |
| 1297 |
if (!stginfo) { |
| 1298 |
return -1; |
| 1299 |
} |
| 1300 |
stginfo->size = sizeof(void *); |
| 1301 |
stginfo->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; |
| 1302 |
stginfo->length = 1; |
| 1303 |
stginfo->ffi_type_pointer = ffi_type_pointer; |
| 1304 |
stginfo->paramfunc = PyCPointerType_paramfunc; |
| 1305 |
stginfo->flags |= TYPEFLAG_ISPOINTER; |
| 1306 |
|
| 1307 |
if (PyDict_GetItemRef(typedict, &_Py_ID(_type_), &proto) < 0) { |
| 1308 |
return -1; |
| 1309 |
} |
| 1310 |
if (proto) { |
| 1311 |
const char *current_format; |
| 1312 |
if (PyCPointerType_SetProto(st, self, stginfo, proto) < 0) { |
| 1313 |
Py_DECREF(proto); |
| 1314 |
return -1; |
| 1315 |
} |
| 1316 |
StgInfo *iteminfo; |
| 1317 |
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) { |
| 1318 |
Py_DECREF(proto); |
| 1319 |
return -1; |
| 1320 |
} |
| 1321 |
/* PyCPointerType_SetProto has verified proto has a stginfo. */ |
| 1322 |
assert(iteminfo); |
| 1323 |
/* If iteminfo->format is NULL, then this is a pointer to an |
| 1324 |
incomplete type. We create a generic format string |
| 1325 |
'pointer to bytes' in this case. XXX Better would be to |
| 1326 |
fix the format string later... |
| 1327 |
*/ |
| 1328 |
current_format = iteminfo->format ? iteminfo->format : "B"; |
| 1329 |
if (iteminfo->shape != NULL) { |
| 1330 |
/* pointer to an array: the shape needs to be prefixed */ |
| 1331 |
stginfo->format = _ctypes_alloc_format_string_with_shape( |
| 1332 |
iteminfo->ndim, iteminfo->shape, "&", current_format); |
| 1333 |
} else { |
| 1334 |
stginfo->format = _ctypes_alloc_format_string("&", current_format); |
| 1335 |
} |
| 1336 |
Py_DECREF(proto); |
| 1337 |
if (stginfo->format == NULL) { |
| 1338 |
return -1; |
| 1339 |
} |
| 1340 |
} |
| 1341 |
|
| 1342 |
return 0; |
| 1343 |
} |
| 1344 |
|
| 1345 |
/*[clinic input] |
| 1346 |
_ctypes.PyCPointerType.set_type as PyCPointerType_set_type |
| 1347 |
|
| 1348 |
self: self(type="PyTypeObject *") |
| 1349 |
cls: defining_class |
| 1350 |
type: object |
| 1351 |
/ |
| 1352 |
[clinic start generated code]*/ |
| 1353 |
|
| 1354 |
static PyObject * |
| 1355 |
PyCPointerType_set_type_impl(PyTypeObject *self, PyTypeObject *cls, |
| 1356 |
PyObject *type) |
| 1357 |
/*[clinic end generated code: output=51459d8f429a70ac input=67e1e8df921f123e]*/ |
| 1358 |
{ |
| 1359 |
ctypes_state *st = get_module_state_by_class(cls); |
| 1360 |
StgInfo *info; |
| 1361 |
if (PyStgInfo_FromType(st, (PyObject *)self, &info) < 0) { |
| 1362 |
return NULL; |
| 1363 |
} |
| 1364 |
if (!info) { |
| 1365 |
PyErr_SetString(PyExc_TypeError, |
| 1366 |
"abstract class"); |
| 1367 |
return NULL; |
| 1368 |
} |
| 1369 |
|
| 1370 |
if (PyCPointerType_SetProto(st, (PyObject *)self, info, type) < 0) { |
| 1371 |
return NULL; |
| 1372 |
} |
| 1373 |
if (PyObject_SetAttr((PyObject *)self, &_Py_ID(_type_), type) < 0) { |
| 1374 |
return NULL; |
| 1375 |
} |
| 1376 |
Py_RETURN_NONE; |
| 1377 |
} |
| 1378 |
|
| 1379 |
static PyObject *_byref(ctypes_state *, PyObject *); |
| 1380 |
|
| 1381 |
/*[clinic input] |
| 1382 |
_ctypes.PyCPointerType.from_param as PyCPointerType_from_param |
| 1383 |
|
| 1384 |
type: self |
| 1385 |
cls: defining_class |
| 1386 |
value: object |
| 1387 |
/ |
| 1388 |
|
| 1389 |
Convert a Python object into a function call parameter. |
| 1390 |
[clinic start generated code]*/ |
| 1391 |
|
| 1392 |
static PyObject * |
| 1393 |
PyCPointerType_from_param_impl(PyObject *type, PyTypeObject *cls, |
| 1394 |
PyObject *value) |
| 1395 |
/*[clinic end generated code: output=a4b32d929aabaf64 input=6c231276e3997884]*/ |
| 1396 |
{ |
| 1397 |
if (value == Py_None) { |
| 1398 |
/* ConvParam will convert to a NULL pointer later */ |
| 1399 |
return Py_NewRef(value); |
| 1400 |
} |
| 1401 |
|
| 1402 |
ctypes_state *st = get_module_state_by_class(cls); |
| 1403 |
StgInfo *typeinfo; |
| 1404 |
if (PyStgInfo_FromType(st, type, &typeinfo) < 0) { |
| 1405 |
return NULL; |
| 1406 |
} |
| 1407 |
if (!typeinfo) { |
| 1408 |
PyErr_SetString(PyExc_TypeError, |
| 1409 |
"abstract class"); |
| 1410 |
return NULL; |
| 1411 |
} |
| 1412 |
|
| 1413 |
/* If we expect POINTER(<type>), but receive a <type> instance, accept |
| 1414 |
it by calling byref(<type>). |
| 1415 |
*/ |
| 1416 |
if (typeinfo->proto == NULL) { |
| 1417 |
PyErr_SetString( |
| 1418 |
PyExc_TypeError, |
| 1419 |
"cannot convert argument: POINTER _type_ type is not set" |
| 1420 |
); |
| 1421 |
return NULL; |
| 1422 |
} |
| 1423 |
switch (PyObject_IsInstance(value, typeinfo->proto)) { |
| 1424 |
case 1: |
| 1425 |
Py_INCREF(value); /* _byref steals a refcount */ |
| 1426 |
return _byref(st, value); |
| 1427 |
case -1: |
| 1428 |
return NULL; |
| 1429 |
default: |
| 1430 |
break; |
| 1431 |
} |
| 1432 |
|
| 1433 |
if (PointerObject_Check(st, value) || ArrayObject_Check(st, value)) { |
| 1434 |
/* Array instances are also pointers when |
| 1435 |
the item types are the same. |
| 1436 |
*/ |
| 1437 |
StgInfo *v; |
| 1438 |
if (PyStgInfo_FromObject(st, value, &v) < 0) { |
| 1439 |
return NULL; |
| 1440 |
} |
| 1441 |
assert(v); /* Cannot be NULL for pointer or array objects */ |
| 1442 |
int ret = PyObject_IsSubclass(v->proto, typeinfo->proto); |
| 1443 |
if (ret < 0) { |
| 1444 |
return NULL; |
| 1445 |
} |
| 1446 |
if (ret) { |
| 1447 |
return Py_NewRef(value); |
| 1448 |
} |
| 1449 |
} |
| 1450 |
return CDataType_from_param_impl(type, cls, value); |
| 1451 |
} |
| 1452 |
|
| 1453 |
static PyMethodDef PyCPointerType_methods[] = { |
| 1454 |
CDATATYPE_FROM_ADDRESS_METHODDEF |
| 1455 |
CDATATYPE_FROM_BUFFER_METHODDEF |
| 1456 |
CDATATYPE_FROM_BUFFER_COPY_METHODDEF |
| 1457 |
CDATATYPE_IN_DLL_METHODDEF |
| 1458 |
PYCPOINTERTYPE_FROM_PARAM_METHODDEF |
| 1459 |
PYCPOINTERTYPE_SET_TYPE_METHODDEF |
| 1460 |
{ NULL, NULL }, |
| 1461 |
}; |
| 1462 |
|
| 1463 |
static PyType_Slot pycpointer_type_slots[] = { |
| 1464 |
{Py_tp_doc, PyDoc_STR("metatype for the Pointer Objects")}, |
| 1465 |
{Py_tp_methods, PyCPointerType_methods}, |
| 1466 |
{Py_tp_init, PyCPointerType_init}, |
| 1467 |
{0, NULL}, |
| 1468 |
}; |
| 1469 |
|
| 1470 |
static PyType_Spec pycpointer_type_spec = { |
| 1471 |
.name = "_ctypes.PyCPointerType", |
| 1472 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1473 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 1474 |
.slots = pycpointer_type_slots, |
| 1475 |
}; |
| 1476 |
|
| 1477 |
|
| 1478 |
/******************************************************************/ |
| 1479 |
/* |
| 1480 |
PyCArrayType_Type |
| 1481 |
*/ |
| 1482 |
/* |
| 1483 |
PyCArrayType_init ensures that the new Array subclass created has a _length_ |
| 1484 |
attribute, and a _type_ attribute. |
| 1485 |
*/ |
| 1486 |
/*[clinic input] |
| 1487 |
class _ctypes.PyCArrayType_Type "CDataObject *" "clinic_state()->PyCArrayType_Type" |
| 1488 |
[clinic start generated code]*/ |
| 1489 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=6340cbaead1bf3f3]*/ |
| 1490 |
|
| 1491 |
/*[clinic input] |
| 1492 |
@critical_section |
| 1493 |
@setter |
| 1494 |
_ctypes.PyCArrayType_Type.raw |
| 1495 |
[clinic start generated code]*/ |
| 1496 |
|
| 1497 |
static int |
| 1498 |
_ctypes_PyCArrayType_Type_raw_set_impl(CDataObject *self, PyObject *value) |
| 1499 |
/*[clinic end generated code: output=cf9b2a9fd92e9ecb input=a3717561efc45efd]*/ |
| 1500 |
{ |
| 1501 |
char *ptr; |
| 1502 |
Py_ssize_t size; |
| 1503 |
Py_buffer view; |
| 1504 |
|
| 1505 |
if (value == NULL) { |
| 1506 |
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute"); |
| 1507 |
return -1; |
| 1508 |
} |
| 1509 |
if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0) |
| 1510 |
return -1; |
| 1511 |
size = view.len; |
| 1512 |
ptr = view.buf; |
| 1513 |
if (size > self->b_size) { |
| 1514 |
PyErr_SetString(PyExc_ValueError, |
| 1515 |
"byte string too long"); |
| 1516 |
goto fail; |
| 1517 |
} |
| 1518 |
|
| 1519 |
memcpy(self->b_ptr, ptr, size); |
| 1520 |
|
| 1521 |
PyBuffer_Release(&view); |
| 1522 |
return 0; |
| 1523 |
fail: |
| 1524 |
PyBuffer_Release(&view); |
| 1525 |
return -1; |
| 1526 |
} |
| 1527 |
|
| 1528 |
/*[clinic input] |
| 1529 |
@critical_section |
| 1530 |
@getter |
| 1531 |
_ctypes.PyCArrayType_Type.raw |
| 1532 |
[clinic start generated code]*/ |
| 1533 |
|
| 1534 |
static PyObject * |
| 1535 |
_ctypes_PyCArrayType_Type_raw_get_impl(CDataObject *self) |
| 1536 |
/*[clinic end generated code: output=3a90be6f43764e31 input=4c49bbb715235ba7]*/ |
| 1537 |
{ |
| 1538 |
return PyBytes_FromStringAndSize(self->b_ptr, self->b_size); |
| 1539 |
} |
| 1540 |
|
| 1541 |
/*[clinic input] |
| 1542 |
@critical_section |
| 1543 |
@getter |
| 1544 |
_ctypes.PyCArrayType_Type.value |
| 1545 |
[clinic start generated code]*/ |
| 1546 |
|
| 1547 |
static PyObject * |
| 1548 |
_ctypes_PyCArrayType_Type_value_get_impl(CDataObject *self) |
| 1549 |
/*[clinic end generated code: output=fb0636f4d8875483 input=2432a2aeb1ed78d1]*/ |
| 1550 |
{ |
| 1551 |
Py_ssize_t i; |
| 1552 |
PyObject *res; |
| 1553 |
char *ptr = self->b_ptr; |
| 1554 |
for (i = 0; i < self->b_size; ++i) |
| 1555 |
if (*ptr++ == '\0') |
| 1556 |
break; |
| 1557 |
res = PyBytes_FromStringAndSize(self->b_ptr, i); |
| 1558 |
return res; |
| 1559 |
} |
| 1560 |
|
| 1561 |
/*[clinic input] |
| 1562 |
@critical_section |
| 1563 |
@setter |
| 1564 |
_ctypes.PyCArrayType_Type.value |
| 1565 |
[clinic start generated code]*/ |
| 1566 |
|
| 1567 |
static int |
| 1568 |
_ctypes_PyCArrayType_Type_value_set_impl(CDataObject *self, PyObject *value) |
| 1569 |
/*[clinic end generated code: output=39ad655636a28dd5 input=e2e6385fc6ab1a29]*/ |
| 1570 |
{ |
| 1571 |
const char *ptr; |
| 1572 |
Py_ssize_t size; |
| 1573 |
|
| 1574 |
if (value == NULL) { |
| 1575 |
PyErr_SetString(PyExc_TypeError, |
| 1576 |
"can't delete attribute"); |
| 1577 |
return -1; |
| 1578 |
} |
| 1579 |
|
| 1580 |
if (!PyBytes_Check(value)) { |
| 1581 |
PyErr_Format(PyExc_TypeError, |
| 1582 |
"bytes expected instead of %s instance", |
| 1583 |
Py_TYPE(value)->tp_name); |
| 1584 |
return -1; |
| 1585 |
} else |
| 1586 |
Py_INCREF(value); |
| 1587 |
size = PyBytes_GET_SIZE(value); |
| 1588 |
if (size > self->b_size) { |
| 1589 |
PyErr_SetString(PyExc_ValueError, |
| 1590 |
"byte string too long"); |
| 1591 |
Py_DECREF(value); |
| 1592 |
return -1; |
| 1593 |
} |
| 1594 |
|
| 1595 |
ptr = PyBytes_AS_STRING(value); |
| 1596 |
memcpy(self->b_ptr, ptr, size); |
| 1597 |
if (size < self->b_size) |
| 1598 |
self->b_ptr[size] = '\0'; |
| 1599 |
Py_DECREF(value); |
| 1600 |
|
| 1601 |
return 0; |
| 1602 |
} |
| 1603 |
|
| 1604 |
static PyGetSetDef CharArray_getsets[] = { |
| 1605 |
_CTYPES_PYCARRAYTYPE_TYPE_RAW_GETSETDEF |
| 1606 |
_CTYPES_PYCARRAYTYPE_TYPE_VALUE_GETSETDEF |
| 1607 |
{ NULL, NULL } |
| 1608 |
}; |
| 1609 |
|
| 1610 |
static PyObject * |
| 1611 |
WCharArray_get_value_lock_held(PyObject *op) |
| 1612 |
{ |
| 1613 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); |
| 1614 |
Py_ssize_t i; |
| 1615 |
PyObject *res; |
| 1616 |
CDataObject *self = _CDataObject_CAST(op); |
| 1617 |
wchar_t *ptr = (wchar_t *)self->b_ptr; |
| 1618 |
for (i = 0; i < self->b_size/(Py_ssize_t)sizeof(wchar_t); ++i) |
| 1619 |
if (*ptr++ == (wchar_t)0) |
| 1620 |
break; |
| 1621 |
res = PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i); |
| 1622 |
return res; |
| 1623 |
} |
| 1624 |
|
| 1625 |
static PyObject * |
| 1626 |
WCharArray_get_value(PyObject *op, void *Py_UNUSED(ignored)) |
| 1627 |
{ |
| 1628 |
PyObject *res; |
| 1629 |
Py_BEGIN_CRITICAL_SECTION(op); |
| 1630 |
res = WCharArray_get_value_lock_held(op); |
| 1631 |
Py_END_CRITICAL_SECTION(); |
| 1632 |
return res; |
| 1633 |
} |
| 1634 |
|
| 1635 |
static int |
| 1636 |
WCharArray_set_value_lock_held(PyObject *op, PyObject *value) |
| 1637 |
{ |
| 1638 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); |
| 1639 |
CDataObject *self = _CDataObject_CAST(op); |
| 1640 |
|
| 1641 |
if (value == NULL) { |
| 1642 |
PyErr_SetString(PyExc_TypeError, |
| 1643 |
"can't delete attribute"); |
| 1644 |
return -1; |
| 1645 |
} |
| 1646 |
if (!PyUnicode_Check(value)) { |
| 1647 |
PyErr_Format(PyExc_TypeError, |
| 1648 |
"unicode string expected instead of %s instance", |
| 1649 |
Py_TYPE(value)->tp_name); |
| 1650 |
return -1; |
| 1651 |
} |
| 1652 |
|
| 1653 |
Py_ssize_t size = self->b_size / sizeof(wchar_t); |
| 1654 |
Py_ssize_t len = PyUnicode_AsWideChar(value, NULL, 0); |
| 1655 |
if (len < 0) { |
| 1656 |
return -1; |
| 1657 |
} |
| 1658 |
// PyUnicode_AsWideChar() returns number of wchars including trailing null byte, |
| 1659 |
// when it is called with NULL. |
| 1660 |
assert(len > 0); |
| 1661 |
if (len - 1 > size) { |
| 1662 |
PyErr_SetString(PyExc_ValueError, "string too long"); |
| 1663 |
return -1; |
| 1664 |
} |
| 1665 |
Py_ssize_t rc; |
| 1666 |
rc = PyUnicode_AsWideChar(value, (wchar_t *)self->b_ptr, size); |
| 1667 |
return rc < 0 ? -1 : 0; |
| 1668 |
} |
| 1669 |
|
| 1670 |
static int |
| 1671 |
WCharArray_set_value(PyObject *op, PyObject *value, void *Py_UNUSED(ignored)) |
| 1672 |
{ |
| 1673 |
int rc; |
| 1674 |
Py_BEGIN_CRITICAL_SECTION(op); |
| 1675 |
rc = WCharArray_set_value_lock_held(op, value); |
| 1676 |
Py_END_CRITICAL_SECTION(); |
| 1677 |
return rc; |
| 1678 |
} |
| 1679 |
|
| 1680 |
static PyGetSetDef WCharArray_getsets[] = { |
| 1681 |
{ "value", WCharArray_get_value, WCharArray_set_value, "string value" }, |
| 1682 |
{ NULL, NULL } |
| 1683 |
}; |
| 1684 |
|
| 1685 |
/* |
| 1686 |
The next function is copied from Python's typeobject.c. |
| 1687 |
|
| 1688 |
It is used to attach getsets to a type *after* it |
| 1689 |
has been created: Arrays of characters have additional getsets to treat them |
| 1690 |
as strings. |
| 1691 |
*/ |
| 1692 |
|
| 1693 |
static int |
| 1694 |
add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
| 1695 |
{ |
| 1696 |
PyObject *dict = type->tp_dict; |
| 1697 |
for (; gsp->name != NULL; gsp++) { |
| 1698 |
PyObject *descr; |
| 1699 |
descr = PyDescr_NewGetSet(type, gsp); |
| 1700 |
if (descr == NULL) |
| 1701 |
return -1; |
| 1702 |
if (PyDict_SetItemString(dict, gsp->name, descr) < 0) { |
| 1703 |
Py_DECREF(descr); |
| 1704 |
return -1; |
| 1705 |
} |
| 1706 |
Py_DECREF(descr); |
| 1707 |
} |
| 1708 |
return 0; |
| 1709 |
} |
| 1710 |
|
| 1711 |
static PyCArgObject * |
| 1712 |
PyCArrayType_paramfunc(ctypes_state *st, CDataObject *self) |
| 1713 |
{ |
| 1714 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 1715 |
PyCArgObject *p = PyCArgObject_new(st); |
| 1716 |
if (p == NULL) |
| 1717 |
return NULL; |
| 1718 |
p->tag = 'P'; |
| 1719 |
p->pffi_type = &ffi_type_pointer; |
| 1720 |
p->value.p = (char *)self->b_ptr; |
| 1721 |
p->obj = Py_NewRef(self); |
| 1722 |
return p; |
| 1723 |
} |
| 1724 |
|
| 1725 |
static int |
| 1726 |
PyCArrayType_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1727 |
{ |
| 1728 |
PyObject *length_attr, *type_attr; |
| 1729 |
Py_ssize_t length; |
| 1730 |
Py_ssize_t itemsize, itemalign; |
| 1731 |
|
| 1732 |
/* Initialize these variables to NULL so that we can simplify error |
| 1733 |
handling by using Py_XDECREF. */ |
| 1734 |
type_attr = NULL; |
| 1735 |
|
| 1736 |
if (PyObject_GetOptionalAttr(self, &_Py_ID(_length_), &length_attr) < 0) { |
| 1737 |
goto error; |
| 1738 |
} |
| 1739 |
if (!length_attr) { |
| 1740 |
PyErr_SetString(PyExc_AttributeError, |
| 1741 |
"class must define a '_length_' attribute"); |
| 1742 |
goto error; |
| 1743 |
} |
| 1744 |
|
| 1745 |
if (!PyLong_Check(length_attr)) { |
| 1746 |
Py_DECREF(length_attr); |
| 1747 |
PyErr_SetString(PyExc_TypeError, |
| 1748 |
"The '_length_' attribute must be an integer"); |
| 1749 |
goto error; |
| 1750 |
} |
| 1751 |
|
| 1752 |
if (_PyLong_IsNegative((PyLongObject *)length_attr)) { |
| 1753 |
Py_DECREF(length_attr); |
| 1754 |
PyErr_SetString(PyExc_ValueError, |
| 1755 |
"The '_length_' attribute must not be negative"); |
| 1756 |
goto error; |
| 1757 |
} |
| 1758 |
|
| 1759 |
length = PyLong_AsSsize_t(length_attr); |
| 1760 |
Py_DECREF(length_attr); |
| 1761 |
if (length == -1 && PyErr_Occurred()) { |
| 1762 |
if (PyErr_ExceptionMatches(PyExc_OverflowError)) { |
| 1763 |
PyErr_SetString(PyExc_OverflowError, |
| 1764 |
"The '_length_' attribute is too large"); |
| 1765 |
} |
| 1766 |
goto error; |
| 1767 |
} |
| 1768 |
|
| 1769 |
if (PyObject_GetOptionalAttr(self, &_Py_ID(_type_), &type_attr) < 0) { |
| 1770 |
goto error; |
| 1771 |
} |
| 1772 |
if (!type_attr) { |
| 1773 |
PyErr_SetString(PyExc_AttributeError, |
| 1774 |
"class must define a '_type_' attribute"); |
| 1775 |
goto error; |
| 1776 |
} |
| 1777 |
|
| 1778 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 1779 |
StgInfo *stginfo = PyStgInfo_Init(st, (PyTypeObject*)self); |
| 1780 |
if (!stginfo) { |
| 1781 |
goto error; |
| 1782 |
} |
| 1783 |
|
| 1784 |
StgInfo *iteminfo; |
| 1785 |
if (PyStgInfo_FromType(st, type_attr, &iteminfo) < 0) { |
| 1786 |
goto error; |
| 1787 |
} |
| 1788 |
if (!iteminfo) { |
| 1789 |
PyErr_SetString(PyExc_TypeError, |
| 1790 |
"_type_ must have storage info"); |
| 1791 |
goto error; |
| 1792 |
} |
| 1793 |
|
| 1794 |
assert(iteminfo->format); |
| 1795 |
stginfo->format = _ctypes_alloc_format_string(NULL, iteminfo->format); |
| 1796 |
if (stginfo->format == NULL) |
| 1797 |
goto error; |
| 1798 |
stginfo->ndim = iteminfo->ndim + 1; |
| 1799 |
stginfo->shape = PyMem_Malloc(sizeof(Py_ssize_t) * stginfo->ndim); |
| 1800 |
if (stginfo->shape == NULL) { |
| 1801 |
PyErr_NoMemory(); |
| 1802 |
goto error; |
| 1803 |
} |
| 1804 |
stginfo->shape[0] = length; |
| 1805 |
if (stginfo->ndim > 1) { |
| 1806 |
memmove(&stginfo->shape[1], iteminfo->shape, |
| 1807 |
sizeof(Py_ssize_t) * (stginfo->ndim - 1)); |
| 1808 |
} |
| 1809 |
|
| 1810 |
itemsize = iteminfo->size; |
| 1811 |
if (itemsize != 0 && length > PY_SSIZE_T_MAX / itemsize) { |
| 1812 |
PyErr_SetString(PyExc_OverflowError, |
| 1813 |
"array too large"); |
| 1814 |
goto error; |
| 1815 |
} |
| 1816 |
|
| 1817 |
itemalign = iteminfo->align; |
| 1818 |
|
| 1819 |
if (iteminfo->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER)) |
| 1820 |
stginfo->flags |= TYPEFLAG_HASPOINTER; |
| 1821 |
|
| 1822 |
stginfo->size = itemsize * length; |
| 1823 |
stginfo->align = itemalign; |
| 1824 |
stginfo->length = length; |
| 1825 |
stginfo->proto = type_attr; |
| 1826 |
type_attr = NULL; |
| 1827 |
|
| 1828 |
stginfo->paramfunc = &PyCArrayType_paramfunc; |
| 1829 |
|
| 1830 |
/* Arrays are passed as pointers to function calls. */ |
| 1831 |
stginfo->ffi_type_pointer = ffi_type_pointer; |
| 1832 |
|
| 1833 |
/* Special case for character arrays. |
| 1834 |
A permanent annoyance: char arrays are also strings! |
| 1835 |
*/ |
| 1836 |
if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
| 1837 |
if (-1 == add_getset((PyTypeObject*)self, CharArray_getsets)) |
| 1838 |
goto error; |
| 1839 |
} |
| 1840 |
else if (iteminfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
| 1841 |
if (-1 == add_getset((PyTypeObject*)self, WCharArray_getsets)) |
| 1842 |
goto error; |
| 1843 |
} |
| 1844 |
|
| 1845 |
return 0; |
| 1846 |
error: |
| 1847 |
Py_XDECREF(type_attr); |
| 1848 |
return -1; |
| 1849 |
} |
| 1850 |
|
| 1851 |
static PyType_Slot pycarray_type_slots[] = { |
| 1852 |
{Py_tp_doc, PyDoc_STR("metatype for the Array Objects")}, |
| 1853 |
{Py_tp_methods, CDataType_methods}, |
| 1854 |
{Py_tp_init, PyCArrayType_init}, |
| 1855 |
{0, NULL}, |
| 1856 |
}; |
| 1857 |
|
| 1858 |
static PyType_Spec pycarray_type_spec = { |
| 1859 |
.name = "_ctypes.PyCArrayType", |
| 1860 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 1861 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 1862 |
.slots = pycarray_type_slots, |
| 1863 |
}; |
| 1864 |
|
| 1865 |
/******************************************************************/ |
| 1866 |
/* |
| 1867 |
PyCSimpleType_Type |
| 1868 |
*/ |
| 1869 |
/* |
| 1870 |
|
| 1871 |
PyCSimpleType_init ensures that the new Simple_Type subclass created has a valid |
| 1872 |
_type_ attribute. |
| 1873 |
|
| 1874 |
*/ |
| 1875 |
|
| 1876 |
/*[clinic input] |
| 1877 |
class _ctypes.PyCSimpleType "PyObject *" "clinic_state()->PyCSimpleType_Type" |
| 1878 |
[clinic start generated code]*/ |
| 1879 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d5a45772668e7f49]*/ |
| 1880 |
|
| 1881 |
/*[clinic input] |
| 1882 |
class _ctypes.c_wchar_p "PyObject *" "clinic_state_sub()->PyCSimpleType_Type" |
| 1883 |
[clinic start generated code]*/ |
| 1884 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=468de7283d622d47]*/ |
| 1885 |
|
| 1886 |
/*[clinic input] |
| 1887 |
class _ctypes.c_char_p "PyObject *" "clinic_state_sub()->PyCSimpleType_Type" |
| 1888 |
[clinic start generated code]*/ |
| 1889 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e750865616e7dcea]*/ |
| 1890 |
|
| 1891 |
/*[clinic input] |
| 1892 |
class _ctypes.c_void_p "PyObject *" "clinic_state_sub()->PyCSimpleType_Type" |
| 1893 |
[clinic start generated code]*/ |
| 1894 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=dd4d9646c56f43a9]*/ |
| 1895 |
|
| 1896 |
|
| 1897 |
/*[clinic input] |
| 1898 |
_ctypes.c_wchar_p.from_param as c_wchar_p_from_param |
| 1899 |
|
| 1900 |
type: self |
| 1901 |
cls: defining_class |
| 1902 |
value: object |
| 1903 |
/ |
| 1904 |
[clinic start generated code]*/ |
| 1905 |
|
| 1906 |
static PyObject * |
| 1907 |
c_wchar_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value) |
| 1908 |
/*[clinic end generated code: output=e453949a2f725a4c input=d322c7237a319607]*/ |
| 1909 |
{ |
| 1910 |
PyObject *as_parameter; |
| 1911 |
int res; |
| 1912 |
if (value == Py_None) { |
| 1913 |
Py_RETURN_NONE; |
| 1914 |
} |
| 1915 |
ctypes_state *st = get_module_state_by_class(cls->tp_base); |
| 1916 |
if (PyUnicode_Check(value)) { |
| 1917 |
PyCArgObject *parg; |
| 1918 |
struct fielddesc *fd = _ctypes_get_fielddesc("Z"); |
| 1919 |
|
| 1920 |
parg = PyCArgObject_new(st); |
| 1921 |
if (parg == NULL) |
| 1922 |
return NULL; |
| 1923 |
parg->pffi_type = &ffi_type_pointer; |
| 1924 |
parg->tag = 'Z'; |
| 1925 |
parg->obj = fd->setfunc(&parg->value, value, 0); |
| 1926 |
if (parg->obj == NULL) { |
| 1927 |
Py_DECREF(parg); |
| 1928 |
return NULL; |
| 1929 |
} |
| 1930 |
return (PyObject *)parg; |
| 1931 |
} |
| 1932 |
res = PyObject_IsInstance(value, type); |
| 1933 |
if (res == -1) |
| 1934 |
return NULL; |
| 1935 |
if (res) { |
| 1936 |
return Py_NewRef(value); |
| 1937 |
} |
| 1938 |
if (ArrayObject_Check(st, value) || PointerObject_Check(st, value)) { |
| 1939 |
/* c_wchar array instance or pointer(c_wchar(...)) */ |
| 1940 |
StgInfo *it; |
| 1941 |
if (PyStgInfo_FromObject(st, value, &it) < 0) { |
| 1942 |
return NULL; |
| 1943 |
} |
| 1944 |
assert(it); /* Cannot be NULL for pointer or array objects */ |
| 1945 |
StgInfo *info = NULL; |
| 1946 |
if (it && it->proto) { |
| 1947 |
if (PyStgInfo_FromType(st, it->proto, &info) < 0) { |
| 1948 |
return NULL; |
| 1949 |
} |
| 1950 |
} |
| 1951 |
if (info && (info->setfunc == _ctypes_get_fielddesc("u")->setfunc)) { |
| 1952 |
return Py_NewRef(value); |
| 1953 |
} |
| 1954 |
} |
| 1955 |
if (PyCArg_CheckExact(st, value)) { |
| 1956 |
/* byref(c_char(...)) */ |
| 1957 |
PyCArgObject *a = (PyCArgObject *)value; |
| 1958 |
StgInfo *info; |
| 1959 |
if (PyStgInfo_FromObject(st, a->obj, &info) < 0) { |
| 1960 |
return NULL; |
| 1961 |
} |
| 1962 |
if (info && (info->setfunc == _ctypes_get_fielddesc("u")->setfunc)) { |
| 1963 |
return Py_NewRef(value); |
| 1964 |
} |
| 1965 |
} |
| 1966 |
|
| 1967 |
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) { |
| 1968 |
return NULL; |
| 1969 |
} |
| 1970 |
if (as_parameter) { |
| 1971 |
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) { |
| 1972 |
Py_DECREF(as_parameter); |
| 1973 |
return NULL; |
| 1974 |
} |
| 1975 |
value = c_wchar_p_from_param_impl(type, cls, as_parameter); |
| 1976 |
Py_DECREF(as_parameter); |
| 1977 |
_Py_LeaveRecursiveCall(); |
| 1978 |
return value; |
| 1979 |
} |
| 1980 |
PyErr_Format(PyExc_TypeError, |
| 1981 |
"'%.200s' object cannot be interpreted " |
| 1982 |
"as ctypes.c_wchar_p", Py_TYPE(value)->tp_name); |
| 1983 |
return NULL; |
| 1984 |
} |
| 1985 |
|
| 1986 |
/*[clinic input] |
| 1987 |
_ctypes.c_char_p.from_param as c_char_p_from_param |
| 1988 |
|
| 1989 |
type: self |
| 1990 |
cls: defining_class |
| 1991 |
value: object |
| 1992 |
/ |
| 1993 |
[clinic start generated code]*/ |
| 1994 |
|
| 1995 |
static PyObject * |
| 1996 |
c_char_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value) |
| 1997 |
/*[clinic end generated code: output=219652ab7c174aa1 input=6cf0d1b6bb4ede11]*/ |
| 1998 |
{ |
| 1999 |
PyObject *as_parameter; |
| 2000 |
int res; |
| 2001 |
if (value == Py_None) { |
| 2002 |
Py_RETURN_NONE; |
| 2003 |
} |
| 2004 |
ctypes_state *st = get_module_state_by_class(cls->tp_base); |
| 2005 |
if (PyBytes_Check(value)) { |
| 2006 |
PyCArgObject *parg; |
| 2007 |
struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
| 2008 |
|
| 2009 |
parg = PyCArgObject_new(st); |
| 2010 |
if (parg == NULL) |
| 2011 |
return NULL; |
| 2012 |
parg->pffi_type = &ffi_type_pointer; |
| 2013 |
parg->tag = 'z'; |
| 2014 |
parg->obj = fd->setfunc(&parg->value, value, 0); |
| 2015 |
if (parg->obj == NULL) { |
| 2016 |
Py_DECREF(parg); |
| 2017 |
return NULL; |
| 2018 |
} |
| 2019 |
return (PyObject *)parg; |
| 2020 |
} |
| 2021 |
res = PyObject_IsInstance(value, type); |
| 2022 |
if (res == -1) |
| 2023 |
return NULL; |
| 2024 |
if (res) { |
| 2025 |
return Py_NewRef(value); |
| 2026 |
} |
| 2027 |
if (ArrayObject_Check(st, value) || PointerObject_Check(st, value)) { |
| 2028 |
/* c_char array instance or pointer(c_char(...)) */ |
| 2029 |
StgInfo *it; |
| 2030 |
if (PyStgInfo_FromObject(st, value, &it) < 0) { |
| 2031 |
return NULL; |
| 2032 |
} |
| 2033 |
assert(it); /* Cannot be NULL for pointer or array objects */ |
| 2034 |
StgInfo *info = NULL; |
| 2035 |
if (it && it->proto) { |
| 2036 |
if (PyStgInfo_FromType(st, it->proto, &info) < 0) { |
| 2037 |
return NULL; |
| 2038 |
} |
| 2039 |
} |
| 2040 |
if (info && (info->setfunc == _ctypes_get_fielddesc("c")->setfunc)) { |
| 2041 |
return Py_NewRef(value); |
| 2042 |
} |
| 2043 |
} |
| 2044 |
if (PyCArg_CheckExact(st, value)) { |
| 2045 |
/* byref(c_char(...)) */ |
| 2046 |
PyCArgObject *a = (PyCArgObject *)value; |
| 2047 |
StgInfo *info; |
| 2048 |
if (PyStgInfo_FromObject(st, a->obj, &info) < 0) { |
| 2049 |
return NULL; |
| 2050 |
} |
| 2051 |
if (info && (info->setfunc == _ctypes_get_fielddesc("c")->setfunc)) { |
| 2052 |
return Py_NewRef(value); |
| 2053 |
} |
| 2054 |
} |
| 2055 |
|
| 2056 |
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) { |
| 2057 |
return NULL; |
| 2058 |
} |
| 2059 |
if (as_parameter) { |
| 2060 |
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) { |
| 2061 |
Py_DECREF(as_parameter); |
| 2062 |
return NULL; |
| 2063 |
} |
| 2064 |
value = c_char_p_from_param_impl(type, cls, as_parameter); |
| 2065 |
Py_DECREF(as_parameter); |
| 2066 |
_Py_LeaveRecursiveCall(); |
| 2067 |
return value; |
| 2068 |
} |
| 2069 |
PyErr_Format(PyExc_TypeError, |
| 2070 |
"'%.200s' object cannot be interpreted " |
| 2071 |
"as ctypes.c_char_p", Py_TYPE(value)->tp_name); |
| 2072 |
return NULL; |
| 2073 |
} |
| 2074 |
|
| 2075 |
/*[clinic input] |
| 2076 |
_ctypes.c_void_p.from_param as c_void_p_from_param |
| 2077 |
|
| 2078 |
type: self |
| 2079 |
cls: defining_class |
| 2080 |
value: object |
| 2081 |
/ |
| 2082 |
[clinic start generated code]*/ |
| 2083 |
|
| 2084 |
static PyObject * |
| 2085 |
c_void_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value) |
| 2086 |
/*[clinic end generated code: output=984d0075b6038cc7 input=0e8b343fc19c77d4]*/ |
| 2087 |
{ |
| 2088 |
PyObject *as_parameter; |
| 2089 |
int res; |
| 2090 |
|
| 2091 |
/* None */ |
| 2092 |
if (value == Py_None) { |
| 2093 |
Py_RETURN_NONE; |
| 2094 |
} |
| 2095 |
ctypes_state *st = get_module_state_by_class(cls->tp_base); |
| 2096 |
|
| 2097 |
/* Should probably allow buffer interface as well */ |
| 2098 |
/* int, long */ |
| 2099 |
if (PyLong_Check(value)) { |
| 2100 |
PyCArgObject *parg; |
| 2101 |
struct fielddesc *fd = _ctypes_get_fielddesc("P"); |
| 2102 |
|
| 2103 |
parg = PyCArgObject_new(st); |
| 2104 |
if (parg == NULL) |
| 2105 |
return NULL; |
| 2106 |
parg->pffi_type = &ffi_type_pointer; |
| 2107 |
parg->tag = 'P'; |
| 2108 |
parg->obj = fd->setfunc(&parg->value, value, sizeof(void*)); |
| 2109 |
if (parg->obj == NULL) { |
| 2110 |
Py_DECREF(parg); |
| 2111 |
return NULL; |
| 2112 |
} |
| 2113 |
return (PyObject *)parg; |
| 2114 |
} |
| 2115 |
/* XXX struni: remove later */ |
| 2116 |
/* bytes */ |
| 2117 |
if (PyBytes_Check(value)) { |
| 2118 |
PyCArgObject *parg; |
| 2119 |
struct fielddesc *fd = _ctypes_get_fielddesc("z"); |
| 2120 |
|
| 2121 |
parg = PyCArgObject_new(st); |
| 2122 |
if (parg == NULL) |
| 2123 |
return NULL; |
| 2124 |
parg->pffi_type = &ffi_type_pointer; |
| 2125 |
parg->tag = 'z'; |
| 2126 |
parg->obj = fd->setfunc(&parg->value, value, 0); |
| 2127 |
if (parg->obj == NULL) { |
| 2128 |
Py_DECREF(parg); |
| 2129 |
return NULL; |
| 2130 |
} |
| 2131 |
return (PyObject *)parg; |
| 2132 |
} |
| 2133 |
/* unicode */ |
| 2134 |
if (PyUnicode_Check(value)) { |
| 2135 |
PyCArgObject *parg; |
| 2136 |
struct fielddesc *fd = _ctypes_get_fielddesc("Z"); |
| 2137 |
|
| 2138 |
parg = PyCArgObject_new(st); |
| 2139 |
if (parg == NULL) |
| 2140 |
return NULL; |
| 2141 |
parg->pffi_type = &ffi_type_pointer; |
| 2142 |
parg->tag = 'Z'; |
| 2143 |
parg->obj = fd->setfunc(&parg->value, value, 0); |
| 2144 |
if (parg->obj == NULL) { |
| 2145 |
Py_DECREF(parg); |
| 2146 |
return NULL; |
| 2147 |
} |
| 2148 |
return (PyObject *)parg; |
| 2149 |
} |
| 2150 |
/* c_void_p instance (or subclass) */ |
| 2151 |
res = PyObject_IsInstance(value, type); |
| 2152 |
if (res == -1) |
| 2153 |
return NULL; |
| 2154 |
if (res) { |
| 2155 |
/* c_void_p instances */ |
| 2156 |
return Py_NewRef(value); |
| 2157 |
} |
| 2158 |
/* ctypes array or pointer instance */ |
| 2159 |
if (ArrayObject_Check(st, value) || PointerObject_Check(st, value)) { |
| 2160 |
/* Any array or pointer is accepted */ |
| 2161 |
return Py_NewRef(value); |
| 2162 |
} |
| 2163 |
/* byref(...) */ |
| 2164 |
if (PyCArg_CheckExact(st, value)) { |
| 2165 |
/* byref(c_xxx()) */ |
| 2166 |
PyCArgObject *a = (PyCArgObject *)value; |
| 2167 |
if (a->tag == 'P') { |
| 2168 |
return Py_NewRef(value); |
| 2169 |
} |
| 2170 |
} |
| 2171 |
/* function pointer */ |
| 2172 |
if (PyCFuncPtrObject_Check(st, value)) { |
| 2173 |
PyCArgObject *parg; |
| 2174 |
PyCFuncPtrObject *func; |
| 2175 |
func = (PyCFuncPtrObject *)value; |
| 2176 |
parg = PyCArgObject_new(st); |
| 2177 |
if (parg == NULL) |
| 2178 |
return NULL; |
| 2179 |
parg->pffi_type = &ffi_type_pointer; |
| 2180 |
parg->tag = 'P'; |
| 2181 |
Py_INCREF(value); |
| 2182 |
// Function pointers don't change their contents, no need to lock |
| 2183 |
parg->value.p = *(void **)func->b_ptr; |
| 2184 |
parg->obj = value; |
| 2185 |
return (PyObject *)parg; |
| 2186 |
} |
| 2187 |
/* c_char_p, c_wchar_p */ |
| 2188 |
StgInfo *stgi; |
| 2189 |
if (PyStgInfo_FromObject(st, value, &stgi) < 0) { |
| 2190 |
return NULL; |
| 2191 |
} |
| 2192 |
if (stgi |
| 2193 |
&& CDataObject_Check(st, value) |
| 2194 |
&& stgi->proto |
| 2195 |
&& PyUnicode_Check(stgi->proto)) |
| 2196 |
{ |
| 2197 |
PyCArgObject *parg; |
| 2198 |
|
| 2199 |
switch (PyUnicode_AsUTF8(stgi->proto)[0]) { |
| 2200 |
case 'z': /* c_char_p */ |
| 2201 |
case 'Z': /* c_wchar_p */ |
| 2202 |
parg = PyCArgObject_new(st); |
| 2203 |
if (parg == NULL) |
| 2204 |
return NULL; |
| 2205 |
parg->pffi_type = &ffi_type_pointer; |
| 2206 |
parg->tag = 'Z'; |
| 2207 |
parg->obj = Py_NewRef(value); |
| 2208 |
/* Remember: b_ptr points to where the pointer is stored! */ |
| 2209 |
Py_BEGIN_CRITICAL_SECTION(value); |
| 2210 |
parg->value.p = *(void **)_CDataObject_CAST(value)->b_ptr; |
| 2211 |
Py_END_CRITICAL_SECTION(); |
| 2212 |
return (PyObject *)parg; |
| 2213 |
} |
| 2214 |
} |
| 2215 |
|
| 2216 |
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) { |
| 2217 |
return NULL; |
| 2218 |
} |
| 2219 |
if (as_parameter) { |
| 2220 |
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) { |
| 2221 |
Py_DECREF(as_parameter); |
| 2222 |
return NULL; |
| 2223 |
} |
| 2224 |
value = c_void_p_from_param_impl(type, cls, as_parameter); |
| 2225 |
Py_DECREF(as_parameter); |
| 2226 |
_Py_LeaveRecursiveCall(); |
| 2227 |
return value; |
| 2228 |
} |
| 2229 |
PyErr_Format(PyExc_TypeError, |
| 2230 |
"'%.200s' object cannot be interpreted " |
| 2231 |
"as ctypes.c_void_p", Py_TYPE(value)->tp_name); |
| 2232 |
return NULL; |
| 2233 |
} |
| 2234 |
|
| 2235 |
static int |
| 2236 |
set_stginfo_ffi_type_pointer(StgInfo *stginfo, struct fielddesc *fmt) |
| 2237 |
{ |
| 2238 |
#if defined(_Py_FFI_SUPPORT_C_COMPLEX) |
| 2239 |
if (!fmt->pffi_type->elements) { |
| 2240 |
stginfo->ffi_type_pointer = *fmt->pffi_type; |
| 2241 |
} |
| 2242 |
else { |
| 2243 |
/* From primitive types - only complex types have the elements |
| 2244 |
struct field as non-NULL (two element array). */ |
| 2245 |
assert(fmt->pffi_type->type == FFI_TYPE_COMPLEX); |
| 2246 |
const size_t els_size = 2 * sizeof(ffi_type *); |
| 2247 |
stginfo->ffi_type_pointer.size = fmt->pffi_type->size; |
| 2248 |
stginfo->ffi_type_pointer.alignment = fmt->pffi_type->alignment; |
| 2249 |
stginfo->ffi_type_pointer.type = fmt->pffi_type->type; |
| 2250 |
stginfo->ffi_type_pointer.elements = PyMem_Malloc(els_size); |
| 2251 |
if (!stginfo->ffi_type_pointer.elements) { |
| 2252 |
PyErr_NoMemory(); |
| 2253 |
return -1; |
| 2254 |
} |
| 2255 |
memcpy(stginfo->ffi_type_pointer.elements, |
| 2256 |
fmt->pffi_type->elements, els_size); |
| 2257 |
} |
| 2258 |
#else |
| 2259 |
assert(!fmt->pffi_type->elements); |
| 2260 |
stginfo->ffi_type_pointer = *fmt->pffi_type; |
| 2261 |
#endif |
| 2262 |
return 0; |
| 2263 |
} |
| 2264 |
|
| 2265 |
static PyMethodDef c_void_p_methods[] = {C_VOID_P_FROM_PARAM_METHODDEF {0}}; |
| 2266 |
static PyMethodDef c_char_p_methods[] = {C_CHAR_P_FROM_PARAM_METHODDEF {0}}; |
| 2267 |
static PyMethodDef c_wchar_p_methods[] = {C_WCHAR_P_FROM_PARAM_METHODDEF {0}}; |
| 2268 |
|
| 2269 |
static PyObject *CreateSwappedType(ctypes_state *st, PyTypeObject *type, |
| 2270 |
PyObject *args, PyObject *kwds, |
| 2271 |
PyObject *proto, struct fielddesc *fmt) |
| 2272 |
{ |
| 2273 |
PyTypeObject *result; |
| 2274 |
PyObject *name = PyTuple_GET_ITEM(args, 0); |
| 2275 |
PyObject *newname; |
| 2276 |
PyObject *swapped_args; |
| 2277 |
Py_ssize_t i; |
| 2278 |
|
| 2279 |
swapped_args = PyTuple_New(PyTuple_GET_SIZE(args)); |
| 2280 |
if (!swapped_args) |
| 2281 |
return NULL; |
| 2282 |
|
| 2283 |
assert(st->swapped_suffix != NULL); |
| 2284 |
newname = PyUnicode_Concat(name, st->swapped_suffix); |
| 2285 |
if (newname == NULL) { |
| 2286 |
Py_DECREF(swapped_args); |
| 2287 |
return NULL; |
| 2288 |
} |
| 2289 |
|
| 2290 |
PyTuple_SET_ITEM(swapped_args, 0, newname); |
| 2291 |
for (i=1; i<PyTuple_GET_SIZE(args); ++i) { |
| 2292 |
PyObject *v = PyTuple_GET_ITEM(args, i); |
| 2293 |
Py_INCREF(v); |
| 2294 |
PyTuple_SET_ITEM(swapped_args, i, v); |
| 2295 |
} |
| 2296 |
|
| 2297 |
/* create the new instance (which is a class, |
| 2298 |
since we are a metatype!) */ |
| 2299 |
result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds); |
| 2300 |
Py_DECREF(swapped_args); |
| 2301 |
if (result == NULL) |
| 2302 |
return NULL; |
| 2303 |
|
| 2304 |
StgInfo *stginfo = PyStgInfo_Init(st, result); |
| 2305 |
if (!stginfo) { |
| 2306 |
Py_DECREF(result); |
| 2307 |
return NULL; |
| 2308 |
} |
| 2309 |
if (set_stginfo_ffi_type_pointer(stginfo, fmt)) { |
| 2310 |
Py_DECREF(result); |
| 2311 |
return NULL; |
| 2312 |
} |
| 2313 |
stginfo->align = fmt->pffi_type->alignment; |
| 2314 |
stginfo->length = 0; |
| 2315 |
stginfo->size = fmt->pffi_type->size; |
| 2316 |
stginfo->setfunc = fmt->setfunc_swapped; |
| 2317 |
stginfo->getfunc = fmt->getfunc_swapped; |
| 2318 |
|
| 2319 |
stginfo->proto = Py_NewRef(proto); |
| 2320 |
|
| 2321 |
return (PyObject *)result; |
| 2322 |
} |
| 2323 |
|
| 2324 |
static PyCArgObject * |
| 2325 |
PyCSimpleType_paramfunc(ctypes_state *st, CDataObject *self) |
| 2326 |
{ |
| 2327 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 2328 |
const char *fmt; |
| 2329 |
PyCArgObject *parg; |
| 2330 |
struct fielddesc *fd; |
| 2331 |
|
| 2332 |
StgInfo *info; |
| 2333 |
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { |
| 2334 |
return NULL; |
| 2335 |
} |
| 2336 |
assert(info); /* Cannot be NULL for CDataObject instances */ |
| 2337 |
fmt = PyUnicode_AsUTF8(info->proto); |
| 2338 |
assert(fmt); |
| 2339 |
|
| 2340 |
fd = _ctypes_get_fielddesc(fmt); |
| 2341 |
assert(fd); |
| 2342 |
|
| 2343 |
parg = PyCArgObject_new(st); |
| 2344 |
if (parg == NULL) |
| 2345 |
return NULL; |
| 2346 |
|
| 2347 |
parg->tag = fmt[0]; |
| 2348 |
parg->pffi_type = fd->pffi_type; |
| 2349 |
parg->obj = Py_NewRef(self); |
| 2350 |
memcpy(&parg->value, self->b_ptr, self->b_size); |
| 2351 |
return parg; |
| 2352 |
} |
| 2353 |
|
| 2354 |
|
| 2355 |
static int |
| 2356 |
PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2357 |
{ |
| 2358 |
PyObject *proto; |
| 2359 |
const char *proto_str; |
| 2360 |
Py_ssize_t proto_len; |
| 2361 |
PyMethodDef *ml; |
| 2362 |
struct fielddesc *fmt; |
| 2363 |
|
| 2364 |
if (PyType_Type.tp_init(self, args, kwds) < 0) { |
| 2365 |
return -1; |
| 2366 |
} |
| 2367 |
if (PyObject_GetOptionalAttr(self, &_Py_ID(_type_), &proto) < 0) { |
| 2368 |
return -1; |
| 2369 |
} |
| 2370 |
if (!proto) { |
| 2371 |
PyErr_SetString(PyExc_AttributeError, |
| 2372 |
"class must define a '_type_' attribute"); |
| 2373 |
error: |
| 2374 |
Py_XDECREF(proto); |
| 2375 |
return -1; |
| 2376 |
} |
| 2377 |
if (PyUnicode_Check(proto)) { |
| 2378 |
proto_str = PyUnicode_AsUTF8AndSize(proto, &proto_len); |
| 2379 |
if (!proto_str) |
| 2380 |
goto error; |
| 2381 |
} else { |
| 2382 |
PyErr_SetString(PyExc_TypeError, |
| 2383 |
"class must define a '_type_' string attribute"); |
| 2384 |
goto error; |
| 2385 |
} |
| 2386 |
if (proto_len != 1) { |
| 2387 |
PyErr_SetString(PyExc_ValueError, |
| 2388 |
"class must define a '_type_' attribute " |
| 2389 |
"which must be a string of length 1"); |
| 2390 |
goto error; |
| 2391 |
} |
| 2392 |
fmt = _ctypes_get_fielddesc(proto_str); |
| 2393 |
if (!fmt) { |
| 2394 |
PyErr_Format(PyExc_AttributeError, |
| 2395 |
"class must define a '_type_' attribute which must be\n" |
| 2396 |
"a single character string containing one of the\n" |
| 2397 |
"supported types: '%s'.", |
| 2398 |
_ctypes_get_simple_type_chars()); |
| 2399 |
goto error; |
| 2400 |
} |
| 2401 |
|
| 2402 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 2403 |
StgInfo *stginfo = PyStgInfo_Init(st, (PyTypeObject *)self); |
| 2404 |
if (!stginfo) { |
| 2405 |
goto error; |
| 2406 |
} |
| 2407 |
if (set_stginfo_ffi_type_pointer(stginfo, fmt)) { |
| 2408 |
goto error; |
| 2409 |
} |
| 2410 |
stginfo->align = fmt->pffi_type->alignment; |
| 2411 |
stginfo->length = 0; |
| 2412 |
stginfo->size = fmt->pffi_type->size; |
| 2413 |
stginfo->setfunc = fmt->setfunc; |
| 2414 |
stginfo->getfunc = fmt->getfunc; |
| 2415 |
#ifdef WORDS_BIGENDIAN |
| 2416 |
stginfo->format = _ctypes_alloc_format_string_for_type(proto_str[0], 1); |
| 2417 |
#else |
| 2418 |
stginfo->format = _ctypes_alloc_format_string_for_type(proto_str[0], 0); |
| 2419 |
#endif |
| 2420 |
if (stginfo->format == NULL) { |
| 2421 |
Py_DECREF(proto); |
| 2422 |
return -1; |
| 2423 |
} |
| 2424 |
|
| 2425 |
stginfo->paramfunc = PyCSimpleType_paramfunc; |
| 2426 |
/* |
| 2427 |
if (self->tp_base != st->Simple_Type) { |
| 2428 |
stginfo->setfunc = NULL; |
| 2429 |
stginfo->getfunc = NULL; |
| 2430 |
} |
| 2431 |
*/ |
| 2432 |
|
| 2433 |
/* This consumes the refcount on proto which we have */ |
| 2434 |
stginfo->proto = proto; |
| 2435 |
|
| 2436 |
/* Install from_param class methods in ctypes base classes. |
| 2437 |
Overrides the PyCSimpleType_from_param generic method. |
| 2438 |
*/ |
| 2439 |
if (((PyTypeObject *)self)->tp_base == st->Simple_Type) { |
| 2440 |
switch (*proto_str) { |
| 2441 |
case 'z': /* c_char_p */ |
| 2442 |
ml = c_char_p_methods; |
| 2443 |
stginfo->flags |= TYPEFLAG_ISPOINTER; |
| 2444 |
break; |
| 2445 |
case 'Z': /* c_wchar_p */ |
| 2446 |
ml = c_wchar_p_methods; |
| 2447 |
stginfo->flags |= TYPEFLAG_ISPOINTER; |
| 2448 |
break; |
| 2449 |
case 'P': /* c_void_p */ |
| 2450 |
ml = c_void_p_methods; |
| 2451 |
stginfo->flags |= TYPEFLAG_ISPOINTER; |
| 2452 |
break; |
| 2453 |
case 's': |
| 2454 |
case 'X': |
| 2455 |
case 'O': |
| 2456 |
ml = NULL; |
| 2457 |
stginfo->flags |= TYPEFLAG_ISPOINTER; |
| 2458 |
break; |
| 2459 |
default: |
| 2460 |
ml = NULL; |
| 2461 |
break; |
| 2462 |
} |
| 2463 |
|
| 2464 |
if (ml) { |
| 2465 |
PyObject *meth; |
| 2466 |
int x; |
| 2467 |
meth = PyDescr_NewClassMethod((PyTypeObject*)self, ml); |
| 2468 |
if (!meth) { |
| 2469 |
return -1; |
| 2470 |
} |
| 2471 |
PyObject *name = PyUnicode_FromString(ml->ml_name); |
| 2472 |
if (name == NULL) { |
| 2473 |
Py_DECREF(meth); |
| 2474 |
return -1; |
| 2475 |
} |
| 2476 |
PyUnicode_InternInPlace(&name); |
| 2477 |
x = PyDict_SetItem(((PyTypeObject*)self)->tp_dict, name, meth); |
| 2478 |
Py_DECREF(name); |
| 2479 |
Py_DECREF(meth); |
| 2480 |
if (x == -1) { |
| 2481 |
return -1; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
} |
| 2485 |
|
| 2486 |
PyTypeObject *type = Py_TYPE(self); |
| 2487 |
if (type == st->PyCSimpleType_Type |
| 2488 |
&& fmt->setfunc_swapped |
| 2489 |
&& fmt->getfunc_swapped) |
| 2490 |
{ |
| 2491 |
PyObject *swapped = CreateSwappedType(st, type, args, kwds, |
| 2492 |
proto, fmt); |
| 2493 |
if (swapped == NULL) { |
| 2494 |
return -1; |
| 2495 |
} |
| 2496 |
StgInfo *sw_info; |
| 2497 |
if (PyStgInfo_FromType(st, swapped, &sw_info) < 0) { |
| 2498 |
Py_DECREF(swapped); |
| 2499 |
return -1; |
| 2500 |
} |
| 2501 |
assert(sw_info); |
| 2502 |
#ifdef WORDS_BIGENDIAN |
| 2503 |
PyObject_SetAttrString(self, "__ctype_le__", swapped); |
| 2504 |
PyObject_SetAttrString(self, "__ctype_be__", self); |
| 2505 |
PyObject_SetAttrString(swapped, "__ctype_be__", self); |
| 2506 |
PyObject_SetAttrString(swapped, "__ctype_le__", swapped); |
| 2507 |
/* We are creating the type for the OTHER endian */ |
| 2508 |
sw_info->format = _ctypes_alloc_format_string("<", stginfo->format+1); |
| 2509 |
#else |
| 2510 |
PyObject_SetAttrString(self, "__ctype_be__", swapped); |
| 2511 |
PyObject_SetAttrString(self, "__ctype_le__", self); |
| 2512 |
PyObject_SetAttrString(swapped, "__ctype_le__", self); |
| 2513 |
PyObject_SetAttrString(swapped, "__ctype_be__", swapped); |
| 2514 |
/* We are creating the type for the OTHER endian */ |
| 2515 |
sw_info->format = _ctypes_alloc_format_string(">", stginfo->format+1); |
| 2516 |
#endif |
| 2517 |
Py_DECREF(swapped); |
| 2518 |
if (PyErr_Occurred()) { |
| 2519 |
return -1; |
| 2520 |
} |
| 2521 |
}; |
| 2522 |
|
| 2523 |
return 0; |
| 2524 |
} |
| 2525 |
|
| 2526 |
/* |
| 2527 |
* This is a *class method*. |
| 2528 |
* Convert a parameter into something that ConvParam can handle. |
| 2529 |
*/ |
| 2530 |
|
| 2531 |
/*[clinic input] |
| 2532 |
_ctypes.PyCSimpleType.from_param as PyCSimpleType_from_param |
| 2533 |
|
| 2534 |
type: self |
| 2535 |
cls: defining_class |
| 2536 |
value: object |
| 2537 |
/ |
| 2538 |
|
| 2539 |
Convert a Python object into a function call parameter. |
| 2540 |
[clinic start generated code]*/ |
| 2541 |
|
| 2542 |
static PyObject * |
| 2543 |
PyCSimpleType_from_param_impl(PyObject *type, PyTypeObject *cls, |
| 2544 |
PyObject *value) |
| 2545 |
/*[clinic end generated code: output=8a8453d9663e3a2e input=61cc48ce3a87a570]*/ |
| 2546 |
{ |
| 2547 |
const char *fmt; |
| 2548 |
PyCArgObject *parg; |
| 2549 |
struct fielddesc *fd; |
| 2550 |
PyObject *as_parameter; |
| 2551 |
int res; |
| 2552 |
|
| 2553 |
/* If the value is already an instance of the requested type, |
| 2554 |
we can use it as is */ |
| 2555 |
res = PyObject_IsInstance(value, type); |
| 2556 |
if (res == -1) |
| 2557 |
return NULL; |
| 2558 |
if (res) { |
| 2559 |
return Py_NewRef(value); |
| 2560 |
} |
| 2561 |
|
| 2562 |
ctypes_state *st = get_module_state_by_class(cls); |
| 2563 |
StgInfo *info; |
| 2564 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 2565 |
return NULL; |
| 2566 |
} |
| 2567 |
if (!info) { |
| 2568 |
PyErr_SetString(PyExc_TypeError, |
| 2569 |
"abstract class"); |
| 2570 |
return NULL; |
| 2571 |
} |
| 2572 |
|
| 2573 |
/* I think we can rely on this being a one-character string */ |
| 2574 |
fmt = PyUnicode_AsUTF8(info->proto); |
| 2575 |
assert(fmt); |
| 2576 |
|
| 2577 |
fd = _ctypes_get_fielddesc(fmt); |
| 2578 |
assert(fd); |
| 2579 |
|
| 2580 |
parg = PyCArgObject_new(st); |
| 2581 |
if (parg == NULL) |
| 2582 |
return NULL; |
| 2583 |
|
| 2584 |
parg->tag = fmt[0]; |
| 2585 |
parg->pffi_type = fd->pffi_type; |
| 2586 |
parg->obj = fd->setfunc(&parg->value, value, info->size); |
| 2587 |
if (parg->obj) |
| 2588 |
return (PyObject *)parg; |
| 2589 |
PyObject *exc = PyErr_GetRaisedException(); |
| 2590 |
Py_DECREF(parg); |
| 2591 |
|
| 2592 |
if (PyObject_GetOptionalAttr(value, &_Py_ID(_as_parameter_), &as_parameter) < 0) { |
| 2593 |
Py_XDECREF(exc); |
| 2594 |
return NULL; |
| 2595 |
} |
| 2596 |
if (as_parameter) { |
| 2597 |
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) { |
| 2598 |
Py_DECREF(as_parameter); |
| 2599 |
Py_XDECREF(exc); |
| 2600 |
return NULL; |
| 2601 |
} |
| 2602 |
value = PyCSimpleType_from_param_impl(type, cls, as_parameter); |
| 2603 |
Py_DECREF(as_parameter); |
| 2604 |
Py_XDECREF(exc); |
| 2605 |
_Py_LeaveRecursiveCall(); |
| 2606 |
return value; |
| 2607 |
} |
| 2608 |
if (exc) { |
| 2609 |
PyErr_SetRaisedException(exc); |
| 2610 |
} |
| 2611 |
else { |
| 2612 |
PyErr_SetString(PyExc_TypeError, "wrong type"); |
| 2613 |
} |
| 2614 |
return NULL; |
| 2615 |
} |
| 2616 |
|
| 2617 |
static PyMethodDef PyCSimpleType_methods[] = { |
| 2618 |
PYCSIMPLETYPE_FROM_PARAM_METHODDEF |
| 2619 |
CDATATYPE_FROM_ADDRESS_METHODDEF |
| 2620 |
CDATATYPE_FROM_BUFFER_METHODDEF |
| 2621 |
CDATATYPE_FROM_BUFFER_COPY_METHODDEF |
| 2622 |
CDATATYPE_IN_DLL_METHODDEF |
| 2623 |
{ NULL, NULL }, |
| 2624 |
}; |
| 2625 |
|
| 2626 |
static PyType_Slot pycsimple_type_slots[] = { |
| 2627 |
{Py_tp_doc, PyDoc_STR("metatype for the PyCSimpleType Objects")}, |
| 2628 |
{Py_tp_methods, PyCSimpleType_methods}, |
| 2629 |
{Py_tp_init, PyCSimpleType_init}, |
| 2630 |
{0, NULL}, |
| 2631 |
}; |
| 2632 |
|
| 2633 |
static PyType_Spec pycsimple_type_spec = { |
| 2634 |
.name = "_ctypes.PyCSimpleType", |
| 2635 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 2636 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 2637 |
.slots = pycsimple_type_slots, |
| 2638 |
}; |
| 2639 |
|
| 2640 |
/******************************************************************/ |
| 2641 |
/* |
| 2642 |
PyCFuncPtrType_Type |
| 2643 |
*/ |
| 2644 |
|
| 2645 |
static PyObject * |
| 2646 |
converters_from_argtypes(ctypes_state *st, PyObject *ob) |
| 2647 |
{ |
| 2648 |
PyObject *converters; |
| 2649 |
Py_ssize_t i; |
| 2650 |
|
| 2651 |
ob = PySequence_Tuple(ob); /* new reference */ |
| 2652 |
if (!ob) { |
| 2653 |
PyErr_SetString(PyExc_TypeError, |
| 2654 |
"_argtypes_ must be a sequence of types"); |
| 2655 |
return NULL; |
| 2656 |
} |
| 2657 |
|
| 2658 |
Py_ssize_t nArgs = PyTuple_GET_SIZE(ob); |
| 2659 |
if (nArgs > CTYPES_MAX_ARGCOUNT) { |
| 2660 |
Py_DECREF(ob); |
| 2661 |
PyErr_Format(st->PyExc_ArgError, |
| 2662 |
"_argtypes_ has too many arguments (%zi), maximum is %i", |
| 2663 |
nArgs, CTYPES_MAX_ARGCOUNT); |
| 2664 |
return NULL; |
| 2665 |
} |
| 2666 |
|
| 2667 |
converters = PyTuple_New(nArgs); |
| 2668 |
if (!converters) { |
| 2669 |
Py_DECREF(ob); |
| 2670 |
return NULL; |
| 2671 |
} |
| 2672 |
|
| 2673 |
/* I have to check if this is correct. Using c_char, which has a size |
| 2674 |
of 1, will be assumed to be pushed as only one byte! |
| 2675 |
Aren't these promoted to integers by the C compiler and pushed as 4 bytes? |
| 2676 |
*/ |
| 2677 |
|
| 2678 |
for (i = 0; i < nArgs; ++i) { |
| 2679 |
PyObject *cnv; |
| 2680 |
PyObject *tp = PyTuple_GET_ITEM(ob, i); |
| 2681 |
/* |
| 2682 |
* The following checks, relating to bpo-16575 and bpo-16576, have been |
| 2683 |
* disabled. The reason is that, although there is a definite problem with |
| 2684 |
* how libffi handles unions (https://github.com/libffi/libffi/issues/33), |
| 2685 |
* there are numerous libraries which pass structures containing unions |
| 2686 |
* by values - especially on Windows but examples also exist on Linux |
| 2687 |
* (https://bugs.python.org/msg359834). |
| 2688 |
* |
| 2689 |
* It may not be possible to get proper support for unions and bitfields |
| 2690 |
* until support is forthcoming in libffi, but for now, adding the checks |
| 2691 |
* has caused problems in otherwise-working software, which suggests it |
| 2692 |
* is better to disable the checks. |
| 2693 |
* |
| 2694 |
* Although specific examples reported relate specifically to unions and |
| 2695 |
* not bitfields, the bitfields check is also being disabled as a |
| 2696 |
* precaution. |
| 2697 |
|
| 2698 |
StgInfo *stginfo; |
| 2699 |
if (PyStgInfo_FromType(st, tp, &stginfo) < 0) { |
| 2700 |
return -1; |
| 2701 |
} |
| 2702 |
|
| 2703 |
// TYPEFLAG_HASUNION and TYPEFLAG_HASBITFIELD used to be set |
| 2704 |
// if there were any unions/bitfields; |
| 2705 |
// if the check is re-enabled we either need to loop here or |
| 2706 |
// restore the flag |
| 2707 |
if (stginfo != NULL) { |
| 2708 |
if (stginfo->flags & TYPEFLAG_HASUNION) { |
| 2709 |
Py_DECREF(converters); |
| 2710 |
Py_DECREF(ob); |
| 2711 |
if (!PyErr_Occurred()) { |
| 2712 |
PyErr_Format(PyExc_TypeError, |
| 2713 |
"item %zd in _argtypes_ passes a union by " |
| 2714 |
"value, which is unsupported.", |
| 2715 |
i + 1); |
| 2716 |
} |
| 2717 |
return NULL; |
| 2718 |
} |
| 2719 |
if (stginfo->flags & TYPEFLAG_HASBITFIELD) { |
| 2720 |
Py_DECREF(converters); |
| 2721 |
Py_DECREF(ob); |
| 2722 |
if (!PyErr_Occurred()) { |
| 2723 |
PyErr_Format(PyExc_TypeError, |
| 2724 |
"item %zd in _argtypes_ passes a struct/" |
| 2725 |
"union with a bitfield by value, which is " |
| 2726 |
"unsupported.", |
| 2727 |
i + 1); |
| 2728 |
} |
| 2729 |
return NULL; |
| 2730 |
} |
| 2731 |
} |
| 2732 |
*/ |
| 2733 |
|
| 2734 |
if (PyObject_GetOptionalAttr(tp, &_Py_ID(from_param), &cnv) <= 0) { |
| 2735 |
Py_DECREF(converters); |
| 2736 |
Py_DECREF(ob); |
| 2737 |
if (!PyErr_Occurred()) { |
| 2738 |
PyErr_Format(PyExc_TypeError, |
| 2739 |
"item %zd in _argtypes_ has no from_param method", |
| 2740 |
i+1); |
| 2741 |
} |
| 2742 |
return NULL; |
| 2743 |
} |
| 2744 |
PyTuple_SET_ITEM(converters, i, cnv); |
| 2745 |
} |
| 2746 |
Py_DECREF(ob); |
| 2747 |
return converters; |
| 2748 |
} |
| 2749 |
|
| 2750 |
static int |
| 2751 |
make_funcptrtype_dict(ctypes_state *st, PyObject *attrdict, StgInfo *stginfo) |
| 2752 |
{ |
| 2753 |
PyObject *ob; |
| 2754 |
PyObject *converters = NULL; |
| 2755 |
|
| 2756 |
stginfo->align = _ctypes_get_fielddesc("P")->pffi_type->alignment; |
| 2757 |
stginfo->length = 1; |
| 2758 |
stginfo->size = sizeof(void *); |
| 2759 |
stginfo->setfunc = NULL; |
| 2760 |
stginfo->getfunc = NULL; |
| 2761 |
stginfo->ffi_type_pointer = ffi_type_pointer; |
| 2762 |
|
| 2763 |
if (PyDict_GetItemRef(attrdict, &_Py_ID(_flags_), &ob) < 0) { |
| 2764 |
return -1; |
| 2765 |
} |
| 2766 |
if (!ob || !PyLong_Check(ob)) { |
| 2767 |
PyErr_SetString(PyExc_TypeError, |
| 2768 |
"class must define _flags_ which must be an integer"); |
| 2769 |
Py_XDECREF(ob); |
| 2770 |
return -1; |
| 2771 |
} |
| 2772 |
stginfo->flags = PyLong_AsUnsignedLongMask(ob) | TYPEFLAG_ISPOINTER; |
| 2773 |
Py_DECREF(ob); |
| 2774 |
|
| 2775 |
/* _argtypes_ is optional... */ |
| 2776 |
if (PyDict_GetItemRef(attrdict, &_Py_ID(_argtypes_), &ob) < 0) { |
| 2777 |
return -1; |
| 2778 |
} |
| 2779 |
if (ob) { |
| 2780 |
converters = converters_from_argtypes(st, ob); |
| 2781 |
if (!converters) { |
| 2782 |
Py_DECREF(ob); |
| 2783 |
return -1; |
| 2784 |
} |
| 2785 |
stginfo->argtypes = ob; |
| 2786 |
stginfo->converters = converters; |
| 2787 |
} |
| 2788 |
|
| 2789 |
if (PyDict_GetItemRef(attrdict, &_Py_ID(_restype_), &ob) < 0) { |
| 2790 |
return -1; |
| 2791 |
} |
| 2792 |
if (ob) { |
| 2793 |
StgInfo *info; |
| 2794 |
if (PyStgInfo_FromType(st, ob, &info) < 0) { |
| 2795 |
Py_DECREF(ob); |
| 2796 |
return -1; |
| 2797 |
} |
| 2798 |
if (ob != Py_None && !info && !PyCallable_Check(ob)) { |
| 2799 |
PyErr_SetString(PyExc_TypeError, |
| 2800 |
"_restype_ must be a type, a callable, or None"); |
| 2801 |
Py_DECREF(ob); |
| 2802 |
return -1; |
| 2803 |
} |
| 2804 |
stginfo->restype = ob; |
| 2805 |
if (PyObject_GetOptionalAttr(ob, &_Py_ID(_check_retval_), |
| 2806 |
&stginfo->checker) < 0) |
| 2807 |
{ |
| 2808 |
return -1; |
| 2809 |
} |
| 2810 |
} |
| 2811 |
/* XXX later, maybe. |
| 2812 |
if (PyDict_GetItemRef(attrdict, &_Py _ID(_errcheck_), &ob) < 0) { |
| 2813 |
return -1; |
| 2814 |
} |
| 2815 |
if (ob) { |
| 2816 |
if (!PyCallable_Check(ob)) { |
| 2817 |
PyErr_SetString(PyExc_TypeError, |
| 2818 |
"_errcheck_ must be callable"); |
| 2819 |
Py_DECREF(ob); |
| 2820 |
return -1; |
| 2821 |
} |
| 2822 |
stginfo->errcheck = ob; |
| 2823 |
} |
| 2824 |
*/ |
| 2825 |
return 0; |
| 2826 |
} |
| 2827 |
|
| 2828 |
static PyCArgObject * |
| 2829 |
PyCFuncPtrType_paramfunc(ctypes_state *st, CDataObject *self) |
| 2830 |
{ |
| 2831 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 2832 |
PyCArgObject *parg; |
| 2833 |
|
| 2834 |
parg = PyCArgObject_new(st); |
| 2835 |
if (parg == NULL) |
| 2836 |
return NULL; |
| 2837 |
|
| 2838 |
parg->tag = 'P'; |
| 2839 |
parg->pffi_type = &ffi_type_pointer; |
| 2840 |
parg->obj = Py_NewRef(self); |
| 2841 |
parg->value.p = *(void **)self->b_ptr; |
| 2842 |
return parg; |
| 2843 |
} |
| 2844 |
|
| 2845 |
static int |
| 2846 |
PyCFuncPtrType_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2847 |
{ |
| 2848 |
PyObject *attrdict = PyType_GetDict((PyTypeObject *)self); |
| 2849 |
if (!attrdict) { |
| 2850 |
return -1; |
| 2851 |
} |
| 2852 |
|
| 2853 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(self)); |
| 2854 |
StgInfo *stginfo = PyStgInfo_Init(st, (PyTypeObject *)self); |
| 2855 |
if (!stginfo) { |
| 2856 |
Py_DECREF(attrdict); |
| 2857 |
return -1; |
| 2858 |
} |
| 2859 |
|
| 2860 |
stginfo->paramfunc = PyCFuncPtrType_paramfunc; |
| 2861 |
|
| 2862 |
/* We do NOT expose the function signature in the format string. It |
| 2863 |
is impossible, generally, because the only requirement for the |
| 2864 |
argtypes items is that they have a .from_param method - we do not |
| 2865 |
know the types of the arguments (although, in practice, most |
| 2866 |
argtypes would be a ctypes type). |
| 2867 |
*/ |
| 2868 |
stginfo->format = _ctypes_alloc_format_string(NULL, "X{}"); |
| 2869 |
if (stginfo->format == NULL) { |
| 2870 |
Py_DECREF(attrdict); |
| 2871 |
return -1; |
| 2872 |
} |
| 2873 |
stginfo->flags |= TYPEFLAG_ISPOINTER; |
| 2874 |
|
| 2875 |
if (make_funcptrtype_dict(st, attrdict, stginfo) < 0) { |
| 2876 |
Py_DECREF(attrdict); |
| 2877 |
return -1; |
| 2878 |
} |
| 2879 |
|
| 2880 |
Py_DECREF(attrdict); |
| 2881 |
return 0; |
| 2882 |
} |
| 2883 |
|
| 2884 |
static PyType_Slot pycfuncptr_type_slots[] = { |
| 2885 |
{Py_tp_doc, PyDoc_STR("metatype for C function pointers")}, |
| 2886 |
{Py_tp_methods, CDataType_methods}, |
| 2887 |
{Py_tp_init, PyCFuncPtrType_init}, |
| 2888 |
{0, NULL}, |
| 2889 |
}; |
| 2890 |
|
| 2891 |
static PyType_Spec pycfuncptr_type_spec = { |
| 2892 |
.name = "_ctypes.PyCFuncPtrType", |
| 2893 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 2894 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 2895 |
.slots = pycfuncptr_type_slots, |
| 2896 |
}; |
| 2897 |
|
| 2898 |
|
| 2899 |
/***************************************************************** |
| 2900 |
* Code to keep needed objects alive |
| 2901 |
*/ |
| 2902 |
|
| 2903 |
static CDataObject * |
| 2904 |
PyCData_GetContainer(CDataObject *self) |
| 2905 |
{ |
| 2906 |
while (self->b_base) { |
| 2907 |
self = self->b_base; |
| 2908 |
} |
| 2909 |
CDataObject *res = self; |
| 2910 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 2911 |
// avoid using return directly in this block because critical section |
| 2912 |
// needs to be released before returning |
| 2913 |
if (self->b_objects == NULL) { |
| 2914 |
if (self->b_length) { |
| 2915 |
self->b_objects = PyDict_New(); |
| 2916 |
if (self->b_objects == NULL) { |
| 2917 |
res = NULL; |
| 2918 |
goto exit; |
| 2919 |
} |
| 2920 |
} else { |
| 2921 |
self->b_objects = Py_NewRef(Py_None); |
| 2922 |
} |
| 2923 |
} |
| 2924 |
exit:; |
| 2925 |
Py_END_CRITICAL_SECTION(); |
| 2926 |
return res; |
| 2927 |
} |
| 2928 |
|
| 2929 |
static PyObject * |
| 2930 |
GetKeepedObjects(CDataObject *target) |
| 2931 |
{ |
| 2932 |
CDataObject *container; |
| 2933 |
container = PyCData_GetContainer(target); |
| 2934 |
if (container == NULL) |
| 2935 |
return NULL; |
| 2936 |
return container->b_objects; |
| 2937 |
} |
| 2938 |
|
| 2939 |
static PyObject * |
| 2940 |
unique_key(CDataObject *target, Py_ssize_t index) |
| 2941 |
{ |
| 2942 |
char string[256]; |
| 2943 |
char *cp = string; |
| 2944 |
size_t bytes_left; |
| 2945 |
|
| 2946 |
Py_BUILD_ASSERT(sizeof(string) - 1 > sizeof(Py_ssize_t) * 2); |
| 2947 |
cp += sprintf(cp, "%x", Py_SAFE_DOWNCAST(index, Py_ssize_t, int)); |
| 2948 |
while (target->b_base) { |
| 2949 |
bytes_left = sizeof(string) - (cp - string) - 1; |
| 2950 |
/* Hex format needs 2 characters per byte */ |
| 2951 |
if (bytes_left < sizeof(Py_ssize_t) * 2) { |
| 2952 |
PyErr_SetString(PyExc_ValueError, |
| 2953 |
"ctypes object structure too deep"); |
| 2954 |
return NULL; |
| 2955 |
} |
| 2956 |
cp += sprintf(cp, ":%x", Py_SAFE_DOWNCAST(target->b_index, Py_ssize_t, int)); |
| 2957 |
target = target->b_base; |
| 2958 |
} |
| 2959 |
return PyUnicode_FromStringAndSize(string, cp-string); |
| 2960 |
} |
| 2961 |
|
| 2962 |
static int |
| 2963 |
KeepRef_lock_held(CDataObject *target, Py_ssize_t index, PyObject *keep) |
| 2964 |
{ |
| 2965 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(target); |
| 2966 |
int result; |
| 2967 |
CDataObject *ob; |
| 2968 |
PyObject *key; |
| 2969 |
|
| 2970 |
/* Optimization: no need to store None */ |
| 2971 |
if (keep == Py_None) { |
| 2972 |
Py_DECREF(Py_None); |
| 2973 |
return 0; |
| 2974 |
} |
| 2975 |
ob = PyCData_GetContainer(target); |
| 2976 |
if (ob == NULL) { |
| 2977 |
Py_DECREF(keep); |
| 2978 |
return -1; |
| 2979 |
} |
| 2980 |
if (ob->b_objects == NULL || !PyDict_CheckExact(ob->b_objects)) { |
| 2981 |
Py_XSETREF(ob->b_objects, keep); /* refcount consumed */ |
| 2982 |
return 0; |
| 2983 |
} |
| 2984 |
key = unique_key(target, index); |
| 2985 |
if (key == NULL) { |
| 2986 |
Py_DECREF(keep); |
| 2987 |
return -1; |
| 2988 |
} |
| 2989 |
result = PyDict_SetItem(ob->b_objects, key, keep); |
| 2990 |
Py_DECREF(key); |
| 2991 |
Py_DECREF(keep); |
| 2992 |
return result; |
| 2993 |
} |
| 2994 |
|
| 2995 |
/* |
| 2996 |
* Keep a reference to 'keep' in the 'target', at index 'index'. |
| 2997 |
* |
| 2998 |
* If 'keep' is None, do nothing. |
| 2999 |
* |
| 3000 |
* Otherwise create a dictionary (if it does not yet exist) id the root |
| 3001 |
* objects 'b_objects' item, which will store the 'keep' object under a unique |
| 3002 |
* key. |
| 3003 |
* |
| 3004 |
* The unique_key helper travels the target's b_base pointer down to the root, |
| 3005 |
* building a string containing hex-formatted indexes found during traversal, |
| 3006 |
* separated by colons. |
| 3007 |
* |
| 3008 |
* The index tuple is used as a key into the root object's b_objects dict. |
| 3009 |
* |
| 3010 |
* Note: This function steals a refcount of the third argument, even if it |
| 3011 |
* fails! |
| 3012 |
*/ |
| 3013 |
static int |
| 3014 |
KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep) |
| 3015 |
{ |
| 3016 |
int res; |
| 3017 |
Py_BEGIN_CRITICAL_SECTION(target); |
| 3018 |
res = KeepRef_lock_held(target, index, keep); |
| 3019 |
Py_END_CRITICAL_SECTION(); |
| 3020 |
return res; |
| 3021 |
} |
| 3022 |
|
| 3023 |
/******************************************************************/ |
| 3024 |
/* |
| 3025 |
PyCData_Type |
| 3026 |
*/ |
| 3027 |
|
| 3028 |
/*[clinic input] |
| 3029 |
class _ctypes.PyCData "PyObject *" "clinic_state()->PyCData_Type" |
| 3030 |
[clinic start generated code]*/ |
| 3031 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ac13df38dee3c22c]*/ |
| 3032 |
|
| 3033 |
|
| 3034 |
static int |
| 3035 |
PyCData_traverse(PyObject *op, visitproc visit, void *arg) |
| 3036 |
{ |
| 3037 |
CDataObject *self = _CDataObject_CAST(op); |
| 3038 |
Py_VISIT(self->b_objects); |
| 3039 |
Py_VISIT((PyObject *)self->b_base); |
| 3040 |
PyTypeObject *type = Py_TYPE(self); |
| 3041 |
Py_VISIT(type); |
| 3042 |
return 0; |
| 3043 |
} |
| 3044 |
|
| 3045 |
static int |
| 3046 |
PyCData_clear(PyObject *op) |
| 3047 |
{ |
| 3048 |
CDataObject *self = _CDataObject_CAST(op); |
| 3049 |
Py_CLEAR(self->b_objects); |
| 3050 |
if ((self->b_needsfree) |
| 3051 |
&& _CDataObject_HasExternalBuffer(self)) |
| 3052 |
PyMem_Free(self->b_ptr); |
| 3053 |
self->b_ptr = NULL; |
| 3054 |
Py_CLEAR(self->b_base); |
| 3055 |
return 0; |
| 3056 |
} |
| 3057 |
|
| 3058 |
static void |
| 3059 |
PyCData_dealloc(PyObject *self) |
| 3060 |
{ |
| 3061 |
PyTypeObject *type = Py_TYPE(self); |
| 3062 |
PyObject_GC_UnTrack(self); |
| 3063 |
(void)PyCData_clear(self); |
| 3064 |
type->tp_free(self); |
| 3065 |
Py_DECREF(type); |
| 3066 |
} |
| 3067 |
|
| 3068 |
static PyMemberDef PyCData_members[] = { |
| 3069 |
{ "_b_base_", _Py_T_OBJECT, |
| 3070 |
offsetof(CDataObject, b_base), Py_READONLY, |
| 3071 |
"the base object" }, |
| 3072 |
{ "_b_needsfree_", Py_T_INT, |
| 3073 |
offsetof(CDataObject, b_needsfree), Py_READONLY, |
| 3074 |
"whether the object owns the memory or not" }, |
| 3075 |
{ "_objects", _Py_T_OBJECT, |
| 3076 |
offsetof(CDataObject, b_objects), Py_READONLY, |
| 3077 |
"internal objects tree (NEVER CHANGE THIS OBJECT!)"}, |
| 3078 |
{ NULL }, |
| 3079 |
}; |
| 3080 |
|
| 3081 |
/* Find the innermost type of an array type, returning a borrowed reference */ |
| 3082 |
static PyObject * |
| 3083 |
PyCData_item_type(ctypes_state *st, PyObject *type) |
| 3084 |
{ |
| 3085 |
if (PyCArrayTypeObject_Check(st, type)) { |
| 3086 |
PyObject *elem_type; |
| 3087 |
|
| 3088 |
/* asserts used here as these are all guaranteed by construction */ |
| 3089 |
StgInfo *stg_info; |
| 3090 |
if (PyStgInfo_FromType(st, type, &stg_info) < 0) { |
| 3091 |
return NULL; |
| 3092 |
} |
| 3093 |
assert(stg_info); |
| 3094 |
elem_type = stg_info->proto; |
| 3095 |
assert(elem_type); |
| 3096 |
return PyCData_item_type(st, elem_type); |
| 3097 |
} |
| 3098 |
else { |
| 3099 |
return type; |
| 3100 |
} |
| 3101 |
} |
| 3102 |
|
| 3103 |
static int |
| 3104 |
PyCData_NewGetBuffer(PyObject *myself, Py_buffer *view, int flags) |
| 3105 |
{ |
| 3106 |
CDataObject *self = _CDataObject_CAST(myself); |
| 3107 |
|
| 3108 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(myself))); |
| 3109 |
StgInfo *info; |
| 3110 |
if (PyStgInfo_FromObject(st, myself, &info) < 0) { |
| 3111 |
return -1; |
| 3112 |
} |
| 3113 |
assert(info); |
| 3114 |
|
| 3115 |
PyObject *item_type = PyCData_item_type(st, (PyObject*)Py_TYPE(myself)); |
| 3116 |
if (item_type == NULL) { |
| 3117 |
return 0; |
| 3118 |
} |
| 3119 |
|
| 3120 |
if (view == NULL) return 0; |
| 3121 |
|
| 3122 |
StgInfo *item_info; |
| 3123 |
if (PyStgInfo_FromType(st, item_type, &item_info) < 0) { |
| 3124 |
return -1; |
| 3125 |
} |
| 3126 |
assert(item_info); |
| 3127 |
|
| 3128 |
view->buf = self->b_ptr; |
| 3129 |
view->obj = Py_NewRef(myself); |
| 3130 |
view->len = self->b_size; |
| 3131 |
view->readonly = 0; |
| 3132 |
/* use default format character if not set */ |
| 3133 |
view->format = info->format ? info->format : "B"; |
| 3134 |
view->ndim = info->ndim; |
| 3135 |
view->shape = info->shape; |
| 3136 |
view->itemsize = item_info->size; |
| 3137 |
view->strides = NULL; |
| 3138 |
view->suboffsets = NULL; |
| 3139 |
view->internal = NULL; |
| 3140 |
return 0; |
| 3141 |
} |
| 3142 |
|
| 3143 |
/* |
| 3144 |
* CData objects are mutable, so they cannot be hashable! |
| 3145 |
*/ |
| 3146 |
static Py_hash_t |
| 3147 |
PyCData_nohash(PyObject *self) |
| 3148 |
{ |
| 3149 |
PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 3150 |
return -1; |
| 3151 |
} |
| 3152 |
|
| 3153 |
/*[clinic input] |
| 3154 |
@critical_section |
| 3155 |
_ctypes.PyCData.__reduce__ |
| 3156 |
|
| 3157 |
myself: self |
| 3158 |
cls: defining_class |
| 3159 |
/ |
| 3160 |
[clinic start generated code]*/ |
| 3161 |
|
| 3162 |
static PyObject * |
| 3163 |
_ctypes_PyCData___reduce___impl(PyObject *myself, PyTypeObject *cls) |
| 3164 |
/*[clinic end generated code: output=eaad97e111599294 input=6a464e1a1e2bbdbd]*/ |
| 3165 |
{ |
| 3166 |
CDataObject *self = _CDataObject_CAST(myself); |
| 3167 |
|
| 3168 |
ctypes_state *st = get_module_state_by_class(cls); |
| 3169 |
StgInfo *info; |
| 3170 |
if (PyStgInfo_FromObject(st, myself, &info) < 0) { |
| 3171 |
return NULL; |
| 3172 |
} |
| 3173 |
assert(info); |
| 3174 |
|
| 3175 |
if (info->flags & (TYPEFLAG_ISPOINTER|TYPEFLAG_HASPOINTER)) { |
| 3176 |
PyErr_SetString(PyExc_ValueError, |
| 3177 |
"ctypes objects containing pointers cannot be pickled"); |
| 3178 |
return NULL; |
| 3179 |
} |
| 3180 |
PyObject *dict = PyObject_GetAttrString(myself, "__dict__"); |
| 3181 |
if (dict == NULL) { |
| 3182 |
return NULL; |
| 3183 |
} |
| 3184 |
PyObject *bytes; |
| 3185 |
bytes = PyBytes_FromStringAndSize(self->b_ptr, self->b_size); |
| 3186 |
return Py_BuildValue("O(O(NN))", st->_unpickle, Py_TYPE(myself), dict, |
| 3187 |
bytes); |
| 3188 |
} |
| 3189 |
|
| 3190 |
/*[clinic input] |
| 3191 |
@critical_section |
| 3192 |
_ctypes.PyCData.__setstate__ |
| 3193 |
|
| 3194 |
myself: self |
| 3195 |
dict: object(subclass_of="&PyDict_Type") |
| 3196 |
data: str(accept={str, robuffer}, zeroes=True) |
| 3197 |
/ |
| 3198 |
[clinic start generated code]*/ |
| 3199 |
|
| 3200 |
static PyObject * |
| 3201 |
_ctypes_PyCData___setstate___impl(PyObject *myself, PyObject *dict, |
| 3202 |
const char *data, Py_ssize_t data_length) |
| 3203 |
/*[clinic end generated code: output=8bd4c0a5b4f254bd input=124f5070258254c6]*/ |
| 3204 |
{ |
| 3205 |
CDataObject *self = _CDataObject_CAST(myself); |
| 3206 |
|
| 3207 |
if (data_length > self->b_size) { |
| 3208 |
data_length = self->b_size; |
| 3209 |
} |
| 3210 |
memmove(self->b_ptr, data, data_length); |
| 3211 |
PyObject *mydict = PyObject_GetAttrString(myself, "__dict__"); |
| 3212 |
if (mydict == NULL) { |
| 3213 |
return NULL; |
| 3214 |
} |
| 3215 |
if (!PyDict_Check(mydict)) { |
| 3216 |
PyErr_Format(PyExc_TypeError, |
| 3217 |
"%.200s.__dict__ must be a dictionary, not %.200s", |
| 3218 |
Py_TYPE(myself)->tp_name, Py_TYPE(mydict)->tp_name); |
| 3219 |
Py_DECREF(mydict); |
| 3220 |
return NULL; |
| 3221 |
} |
| 3222 |
int res = PyDict_Update(mydict, dict); |
| 3223 |
Py_DECREF(mydict); |
| 3224 |
if (res == -1) |
| 3225 |
return NULL; |
| 3226 |
Py_RETURN_NONE; |
| 3227 |
} |
| 3228 |
|
| 3229 |
/*[clinic input] |
| 3230 |
_ctypes.PyCData.__ctypes_from_outparam__ |
| 3231 |
|
| 3232 |
default __ctypes_from_outparam__ method returns self. |
| 3233 |
[clinic start generated code]*/ |
| 3234 |
|
| 3235 |
static PyObject * |
| 3236 |
_ctypes_PyCData___ctypes_from_outparam___impl(PyObject *self) |
| 3237 |
/*[clinic end generated code: output=a7facc849097b549 input=910c5fec33e268c9]*/ |
| 3238 |
{ |
| 3239 |
return Py_NewRef(self); |
| 3240 |
} |
| 3241 |
|
| 3242 |
static PyMethodDef PyCData_methods[] = { |
| 3243 |
_CTYPES_PYCDATA___CTYPES_FROM_OUTPARAM___METHODDEF |
| 3244 |
_CTYPES_PYCDATA___SETSTATE___METHODDEF |
| 3245 |
_CTYPES_PYCDATA___REDUCE___METHODDEF |
| 3246 |
{ NULL, NULL }, |
| 3247 |
}; |
| 3248 |
|
| 3249 |
static PyType_Slot pycdata_slots[] = { |
| 3250 |
{Py_tp_dealloc, PyCData_dealloc}, |
| 3251 |
{Py_tp_hash, PyCData_nohash}, |
| 3252 |
{Py_tp_doc, PyDoc_STR("XXX to be provided")}, |
| 3253 |
{Py_tp_traverse, PyCData_traverse}, |
| 3254 |
{Py_tp_clear, PyCData_clear}, |
| 3255 |
{Py_tp_methods, PyCData_methods}, |
| 3256 |
{Py_tp_members, PyCData_members}, |
| 3257 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 3258 |
{0, NULL}, |
| 3259 |
}; |
| 3260 |
|
| 3261 |
static PyType_Spec pycdata_spec = { |
| 3262 |
.name = "_ctypes._CData", |
| 3263 |
.basicsize = sizeof(CDataObject), |
| 3264 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 3265 |
Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION), |
| 3266 |
.slots = pycdata_slots, |
| 3267 |
}; |
| 3268 |
|
| 3269 |
static int |
| 3270 |
PyCData_MallocBuffer(CDataObject *obj, StgInfo *info) |
| 3271 |
{ |
| 3272 |
/* We don't have to lock in this function, because it's only |
| 3273 |
* used in constructors and therefore does not have concurrent |
| 3274 |
* access. |
| 3275 |
*/ |
| 3276 |
assert (Py_REFCNT(obj) == 1); |
| 3277 |
assert(stginfo_get_dict_final(info) == 1); |
| 3278 |
|
| 3279 |
if ((size_t)info->size <= sizeof(obj->b_value)) { |
| 3280 |
/* No need to call malloc, can use the default buffer */ |
| 3281 |
obj->b_ptr = (char *)&obj->b_value; |
| 3282 |
/* The b_needsfree flag does not mean that we actually did |
| 3283 |
call PyMem_Malloc to allocate the memory block; instead it |
| 3284 |
means we are the *owner* of the memory and are responsible |
| 3285 |
for freeing resources associated with the memory. This is |
| 3286 |
also the reason that b_needsfree is exposed to Python. |
| 3287 |
*/ |
| 3288 |
obj->b_needsfree = 1; |
| 3289 |
} else { |
| 3290 |
/* In python 2.4, and ctypes 0.9.6, the malloc call took about |
| 3291 |
33% of the creation time for c_int(). |
| 3292 |
*/ |
| 3293 |
obj->b_ptr = (char *)PyMem_Malloc(info->size); |
| 3294 |
if (obj->b_ptr == NULL) { |
| 3295 |
PyErr_NoMemory(); |
| 3296 |
return -1; |
| 3297 |
} |
| 3298 |
obj->b_needsfree = 1; |
| 3299 |
memset(obj->b_ptr, 0, info->size); |
| 3300 |
} |
| 3301 |
obj->b_size = info->size; |
| 3302 |
return 0; |
| 3303 |
} |
| 3304 |
|
| 3305 |
PyObject * |
| 3306 |
PyCData_FromBaseObj(ctypes_state *st, |
| 3307 |
PyObject *type, PyObject *base, Py_ssize_t index, char *adr) |
| 3308 |
{ |
| 3309 |
CDataObject *cmem; |
| 3310 |
|
| 3311 |
assert(PyType_Check(type)); |
| 3312 |
|
| 3313 |
StgInfo *info; |
| 3314 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 3315 |
return NULL; |
| 3316 |
} |
| 3317 |
if (!info) { |
| 3318 |
PyErr_SetString(PyExc_TypeError, |
| 3319 |
"abstract class"); |
| 3320 |
return NULL; |
| 3321 |
} |
| 3322 |
|
| 3323 |
stginfo_set_dict_final(info); |
| 3324 |
cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0); |
| 3325 |
if (cmem == NULL) { |
| 3326 |
return NULL; |
| 3327 |
} |
| 3328 |
assert(CDataObject_Check(st, cmem)); |
| 3329 |
cmem->b_length = info->length; |
| 3330 |
cmem->b_size = info->size; |
| 3331 |
if (base) { /* use base's buffer */ |
| 3332 |
assert(CDataObject_Check(st, base)); |
| 3333 |
cmem->b_ptr = adr; |
| 3334 |
cmem->b_needsfree = 0; |
| 3335 |
cmem->b_base = (CDataObject *)Py_NewRef(base); |
| 3336 |
cmem->b_index = index; |
| 3337 |
} else { /* copy contents of adr */ |
| 3338 |
if (-1 == PyCData_MallocBuffer(cmem, info)) { |
| 3339 |
Py_DECREF(cmem); |
| 3340 |
return NULL; |
| 3341 |
} |
| 3342 |
memcpy(cmem->b_ptr, adr, info->size); |
| 3343 |
cmem->b_index = index; |
| 3344 |
} |
| 3345 |
return (PyObject *)cmem; |
| 3346 |
} |
| 3347 |
|
| 3348 |
/* |
| 3349 |
Box a memory block into a CData instance. |
| 3350 |
*/ |
| 3351 |
PyObject * |
| 3352 |
PyCData_AtAddress(ctypes_state *st, PyObject *type, void *buf) |
| 3353 |
{ |
| 3354 |
CDataObject *pd; |
| 3355 |
|
| 3356 |
if (PySys_Audit("ctypes.cdata", "n", (Py_ssize_t)buf) < 0) { |
| 3357 |
return NULL; |
| 3358 |
} |
| 3359 |
|
| 3360 |
assert(PyType_Check(type)); |
| 3361 |
|
| 3362 |
StgInfo *info; |
| 3363 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 3364 |
return NULL; |
| 3365 |
} |
| 3366 |
if (!info) { |
| 3367 |
PyErr_SetString(PyExc_TypeError, |
| 3368 |
"abstract class"); |
| 3369 |
return NULL; |
| 3370 |
} |
| 3371 |
|
| 3372 |
stginfo_set_dict_final(info); |
| 3373 |
|
| 3374 |
pd = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0); |
| 3375 |
if (!pd) { |
| 3376 |
return NULL; |
| 3377 |
} |
| 3378 |
assert(CDataObject_Check(st, pd)); |
| 3379 |
pd->b_ptr = (char *)buf; |
| 3380 |
pd->b_length = info->length; |
| 3381 |
pd->b_size = info->size; |
| 3382 |
return (PyObject *)pd; |
| 3383 |
} |
| 3384 |
|
| 3385 |
/* |
| 3386 |
This function returns TRUE for c_int, c_void_p, and these kind of |
| 3387 |
classes. FALSE otherwise FALSE also for subclasses of c_int and |
| 3388 |
such. |
| 3389 |
*/ |
| 3390 |
int _ctypes_simple_instance(ctypes_state *st, PyObject *obj) |
| 3391 |
{ |
| 3392 |
PyTypeObject *type = (PyTypeObject *)obj; |
| 3393 |
|
| 3394 |
if (PyCSimpleTypeObject_Check(st, type)) { |
| 3395 |
return type->tp_base != st->Simple_Type; |
| 3396 |
} |
| 3397 |
return 0; |
| 3398 |
} |
| 3399 |
|
| 3400 |
PyObject * |
| 3401 |
PyCData_get(ctypes_state *st, PyObject *type, GETFUNC getfunc, PyObject *src, |
| 3402 |
Py_ssize_t index, Py_ssize_t size, char *adr) |
| 3403 |
{ |
| 3404 |
if (getfunc) { |
| 3405 |
PyObject *res; |
| 3406 |
Py_BEGIN_CRITICAL_SECTION(src); |
| 3407 |
res = getfunc(adr, size); |
| 3408 |
Py_END_CRITICAL_SECTION(); |
| 3409 |
return res; |
| 3410 |
} |
| 3411 |
assert(type); |
| 3412 |
StgInfo *info; |
| 3413 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 3414 |
return NULL; |
| 3415 |
} |
| 3416 |
if (info && info->getfunc && !_ctypes_simple_instance(st, type)) { |
| 3417 |
PyObject *res; |
| 3418 |
Py_BEGIN_CRITICAL_SECTION(src); |
| 3419 |
res = info->getfunc(adr, size); |
| 3420 |
Py_END_CRITICAL_SECTION(); |
| 3421 |
return res; |
| 3422 |
} |
| 3423 |
return PyCData_FromBaseObj(st, type, src, index, adr); |
| 3424 |
} |
| 3425 |
|
| 3426 |
/* |
| 3427 |
Helper function for PyCData_set below. |
| 3428 |
*/ |
| 3429 |
static PyObject * |
| 3430 |
_PyCData_set(ctypes_state *st, |
| 3431 |
CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
| 3432 |
Py_ssize_t size, char *ptr) |
| 3433 |
{ |
| 3434 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dst); |
| 3435 |
CDataObject *src; |
| 3436 |
int err; |
| 3437 |
|
| 3438 |
if (setfunc) { |
| 3439 |
PyObject *res; |
| 3440 |
res = setfunc(ptr, value, size); |
| 3441 |
return res; |
| 3442 |
} |
| 3443 |
if (!CDataObject_Check(st, value)) { |
| 3444 |
StgInfo *info; |
| 3445 |
if (PyStgInfo_FromType(st, type, &info) < 0) { |
| 3446 |
return NULL; |
| 3447 |
} |
| 3448 |
if (info && info->setfunc) { |
| 3449 |
PyObject *res; |
| 3450 |
res = info->setfunc(ptr, value, size); |
| 3451 |
return res; |
| 3452 |
} |
| 3453 |
/* |
| 3454 |
If value is a tuple, we try to call the type with the tuple |
| 3455 |
and use the result! |
| 3456 |
*/ |
| 3457 |
assert(PyType_Check(type)); |
| 3458 |
if (PyTuple_Check(value)) { |
| 3459 |
PyObject *ob; |
| 3460 |
PyObject *result; |
| 3461 |
ob = PyObject_CallObject(type, value); |
| 3462 |
if (ob == NULL) { |
| 3463 |
_ctypes_extend_error(PyExc_RuntimeError, "(%s) ", |
| 3464 |
((PyTypeObject *)type)->tp_name); |
| 3465 |
return NULL; |
| 3466 |
} |
| 3467 |
result = _PyCData_set(st, dst, type, setfunc, ob, |
| 3468 |
size, ptr); |
| 3469 |
Py_DECREF(ob); |
| 3470 |
return result; |
| 3471 |
} else if (value == Py_None && PyCPointerTypeObject_Check(st, type)) { |
| 3472 |
*(void **)ptr = NULL; |
| 3473 |
Py_RETURN_NONE; |
| 3474 |
} else { |
| 3475 |
PyErr_Format(PyExc_TypeError, |
| 3476 |
"expected %s instance, got %s", |
| 3477 |
((PyTypeObject *)type)->tp_name, |
| 3478 |
Py_TYPE(value)->tp_name); |
| 3479 |
return NULL; |
| 3480 |
} |
| 3481 |
} |
| 3482 |
src = (CDataObject *)value; |
| 3483 |
|
| 3484 |
err = PyObject_IsInstance(value, type); |
| 3485 |
if (err == -1) |
| 3486 |
return NULL; |
| 3487 |
if (err) { |
| 3488 |
Py_BEGIN_CRITICAL_SECTION(src); |
| 3489 |
memcpy(ptr, src->b_ptr, size); |
| 3490 |
value = GetKeepedObjects(src); |
| 3491 |
Py_END_CRITICAL_SECTION(); |
| 3492 |
if (value == NULL) |
| 3493 |
return NULL; |
| 3494 |
|
| 3495 |
return Py_NewRef(value); |
| 3496 |
} |
| 3497 |
|
| 3498 |
if (PyCPointerTypeObject_Check(st, type) |
| 3499 |
&& ArrayObject_Check(st, value)) { |
| 3500 |
PyObject *keep; |
| 3501 |
|
| 3502 |
StgInfo *p1, *p2; |
| 3503 |
if (PyStgInfo_FromObject(st, value, &p1) < 0) { |
| 3504 |
return NULL; |
| 3505 |
} |
| 3506 |
assert(p1); /* Cannot be NULL for array instances */ |
| 3507 |
if (PyStgInfo_FromType(st, type, &p2) < 0) { |
| 3508 |
return NULL; |
| 3509 |
} |
| 3510 |
assert(p2); /* Cannot be NULL for pointer types */ |
| 3511 |
|
| 3512 |
if (p1->proto != p2->proto) { |
| 3513 |
PyErr_Format(PyExc_TypeError, |
| 3514 |
"incompatible types, %s instance instead of %s instance", |
| 3515 |
Py_TYPE(value)->tp_name, |
| 3516 |
((PyTypeObject *)type)->tp_name); |
| 3517 |
return NULL; |
| 3518 |
} |
| 3519 |
Py_BEGIN_CRITICAL_SECTION(src); |
| 3520 |
*(void **)ptr = src->b_ptr; |
| 3521 |
|
| 3522 |
keep = GetKeepedObjects(src); |
| 3523 |
Py_END_CRITICAL_SECTION(); |
| 3524 |
if (keep == NULL) |
| 3525 |
return NULL; |
| 3526 |
|
| 3527 |
/* |
| 3528 |
We are assigning an array object to a field which represents |
| 3529 |
a pointer. This has the same effect as converting an array |
| 3530 |
into a pointer. So, again, we have to keep the whole object |
| 3531 |
pointed to (which is the array in this case) alive, and not |
| 3532 |
only it's object list. So we create a tuple, containing |
| 3533 |
b_objects list PLUS the array itself, and return that! |
| 3534 |
*/ |
| 3535 |
return PyTuple_Pack(2, keep, value); |
| 3536 |
} |
| 3537 |
PyErr_Format(PyExc_TypeError, |
| 3538 |
"incompatible types, %s instance instead of %s instance", |
| 3539 |
Py_TYPE(value)->tp_name, |
| 3540 |
((PyTypeObject *)type)->tp_name); |
| 3541 |
return NULL; |
| 3542 |
} |
| 3543 |
|
| 3544 |
/* |
| 3545 |
* Set a slice in object 'dst', which has the type 'type', |
| 3546 |
* to the value 'value'. |
| 3547 |
*/ |
| 3548 |
int |
| 3549 |
PyCData_set(ctypes_state *st, |
| 3550 |
PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
| 3551 |
Py_ssize_t index, Py_ssize_t size, char *ptr) |
| 3552 |
{ |
| 3553 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dst); |
| 3554 |
|
| 3555 |
CDataObject *mem = (CDataObject *)dst; |
| 3556 |
PyObject *result; |
| 3557 |
|
| 3558 |
if (!CDataObject_Check(st, dst)) { |
| 3559 |
PyErr_SetString(PyExc_TypeError, |
| 3560 |
"not a ctype instance"); |
| 3561 |
return -1; |
| 3562 |
} |
| 3563 |
|
| 3564 |
result = _PyCData_set(st, mem, type, setfunc, value, |
| 3565 |
size, ptr); |
| 3566 |
if (result == NULL) |
| 3567 |
return -1; |
| 3568 |
|
| 3569 |
/* KeepRef steals a refcount from it's last argument */ |
| 3570 |
/* If KeepRef fails, we are stumped. The dst memory block has already |
| 3571 |
been changed */ |
| 3572 |
return KeepRef(mem, index, result); |
| 3573 |
} |
| 3574 |
|
| 3575 |
|
| 3576 |
/******************************************************************/ |
| 3577 |
static PyObject * |
| 3578 |
GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3579 |
{ |
| 3580 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 3581 |
return generic_pycdata_new(st, type, args, kwds); |
| 3582 |
} |
| 3583 |
|
| 3584 |
static inline PyObject * |
| 3585 |
generic_pycdata_new(ctypes_state *st, |
| 3586 |
PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3587 |
{ |
| 3588 |
CDataObject *obj; |
| 3589 |
|
| 3590 |
StgInfo *info; |
| 3591 |
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { |
| 3592 |
return NULL; |
| 3593 |
} |
| 3594 |
if (!info) { |
| 3595 |
PyErr_SetString(PyExc_TypeError, |
| 3596 |
"abstract class"); |
| 3597 |
return NULL; |
| 3598 |
} |
| 3599 |
|
| 3600 |
stginfo_set_dict_final(info); |
| 3601 |
|
| 3602 |
obj = (CDataObject *)type->tp_alloc(type, 0); |
| 3603 |
if (!obj) |
| 3604 |
return NULL; |
| 3605 |
|
| 3606 |
obj->b_base = NULL; |
| 3607 |
obj->b_index = 0; |
| 3608 |
obj->b_objects = NULL; |
| 3609 |
obj->b_length = info->length; |
| 3610 |
|
| 3611 |
if (-1 == PyCData_MallocBuffer(obj, info)) { |
| 3612 |
Py_DECREF(obj); |
| 3613 |
return NULL; |
| 3614 |
} |
| 3615 |
return (PyObject *)obj; |
| 3616 |
} |
| 3617 |
/*****************************************************************/ |
| 3618 |
/* |
| 3619 |
PyCFuncPtr_Type |
| 3620 |
*/ |
| 3621 |
|
| 3622 |
static inline void |
| 3623 |
atomic_xsetref(PyObject **field, PyObject *value) |
| 3624 |
{ |
| 3625 |
#ifdef Py_GIL_DISABLED |
| 3626 |
PyObject *old = *field; |
| 3627 |
_Py_atomic_store_ptr(field, value); |
| 3628 |
Py_XDECREF(old); |
| 3629 |
#else |
| 3630 |
Py_XSETREF(*field, value); |
| 3631 |
#endif |
| 3632 |
} |
| 3633 |
/* |
| 3634 |
This function atomically loads the reference from *field, and |
| 3635 |
tries to get a new reference to it. If the incref fails, |
| 3636 |
it acquires critical section of obj and returns a new reference to the *field. |
| 3637 |
In the general case, this avoids contention on acquiring the critical section. |
| 3638 |
*/ |
| 3639 |
static inline PyObject * |
| 3640 |
atomic_xgetref(PyObject *obj, PyObject **field) |
| 3641 |
{ |
| 3642 |
#ifdef Py_GIL_DISABLED |
| 3643 |
PyObject *value = _Py_atomic_load_ptr(field); |
| 3644 |
if (value == NULL) { |
| 3645 |
return NULL; |
| 3646 |
} |
| 3647 |
if (_Py_TryIncrefCompare(field, value)) { |
| 3648 |
return value; |
| 3649 |
} |
| 3650 |
Py_BEGIN_CRITICAL_SECTION(obj); |
| 3651 |
value = Py_XNewRef(*field); |
| 3652 |
Py_END_CRITICAL_SECTION(); |
| 3653 |
return value; |
| 3654 |
#else |
| 3655 |
return Py_XNewRef(*field); |
| 3656 |
#endif |
| 3657 |
} |
| 3658 |
|
| 3659 |
static int |
| 3660 |
_validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags, PyObject *argtypes); |
| 3661 |
|
| 3662 |
|
| 3663 |
|
| 3664 |
/*[clinic input] |
| 3665 |
@critical_section |
| 3666 |
@setter |
| 3667 |
_ctypes.CFuncPtr.errcheck |
| 3668 |
[clinic start generated code]*/ |
| 3669 |
|
| 3670 |
static int |
| 3671 |
_ctypes_CFuncPtr_errcheck_set_impl(PyCFuncPtrObject *self, PyObject *value) |
| 3672 |
/*[clinic end generated code: output=6580cf1ffdf3b9fb input=84930bb16c490b33]*/ |
| 3673 |
{ |
| 3674 |
if (value && !PyCallable_Check(value)) { |
| 3675 |
PyErr_SetString(PyExc_TypeError, |
| 3676 |
"the errcheck attribute must be callable"); |
| 3677 |
return -1; |
| 3678 |
} |
| 3679 |
Py_XINCREF(value); |
| 3680 |
atomic_xsetref(&self->errcheck, value); |
| 3681 |
return 0; |
| 3682 |
} |
| 3683 |
|
| 3684 |
/*[clinic input] |
| 3685 |
@critical_section |
| 3686 |
@getter |
| 3687 |
_ctypes.CFuncPtr.errcheck |
| 3688 |
|
| 3689 |
a function to check for errors |
| 3690 |
[clinic start generated code]*/ |
| 3691 |
|
| 3692 |
static PyObject * |
| 3693 |
_ctypes_CFuncPtr_errcheck_get_impl(PyCFuncPtrObject *self) |
| 3694 |
/*[clinic end generated code: output=dfa6fb5c6f90fd14 input=4672135fef37819f]*/ |
| 3695 |
{ |
| 3696 |
if (self->errcheck) { |
| 3697 |
return Py_NewRef(self->errcheck); |
| 3698 |
} |
| 3699 |
Py_RETURN_NONE; |
| 3700 |
} |
| 3701 |
|
| 3702 |
/*[clinic input] |
| 3703 |
@setter |
| 3704 |
@critical_section |
| 3705 |
_ctypes.CFuncPtr.restype |
| 3706 |
[clinic start generated code]*/ |
| 3707 |
|
| 3708 |
static int |
| 3709 |
_ctypes_CFuncPtr_restype_set_impl(PyCFuncPtrObject *self, PyObject *value) |
| 3710 |
/*[clinic end generated code: output=0be0a086abbabf18 input=683c3bef4562ccc6]*/ |
| 3711 |
{ |
| 3712 |
PyObject *checker; |
| 3713 |
if (value == NULL) { |
| 3714 |
atomic_xsetref(&self->restype, NULL); |
| 3715 |
atomic_xsetref(&self->checker, NULL); |
| 3716 |
return 0; |
| 3717 |
} |
| 3718 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 3719 |
StgInfo *info; |
| 3720 |
if (PyStgInfo_FromType(st, value, &info) < 0) { |
| 3721 |
return -1; |
| 3722 |
} |
| 3723 |
if (value != Py_None && !info && !PyCallable_Check(value)) { |
| 3724 |
PyErr_SetString(PyExc_TypeError, |
| 3725 |
"restype must be a type, a callable, or None"); |
| 3726 |
return -1; |
| 3727 |
} |
| 3728 |
if (PyObject_GetOptionalAttr(value, &_Py_ID(_check_retval_), &checker) < 0) { |
| 3729 |
return -1; |
| 3730 |
} |
| 3731 |
Py_INCREF(value); |
| 3732 |
atomic_xsetref(&self->checker, checker); |
| 3733 |
atomic_xsetref(&self->restype, value); |
| 3734 |
return 0; |
| 3735 |
} |
| 3736 |
|
| 3737 |
/*[clinic input] |
| 3738 |
@getter |
| 3739 |
@critical_section |
| 3740 |
_ctypes.CFuncPtr.restype |
| 3741 |
|
| 3742 |
specify the result type |
| 3743 |
[clinic start generated code]*/ |
| 3744 |
|
| 3745 |
static PyObject * |
| 3746 |
_ctypes_CFuncPtr_restype_get_impl(PyCFuncPtrObject *self) |
| 3747 |
/*[clinic end generated code: output=c8f44cd16f1dee5e input=5e3ed95116204fd2]*/ |
| 3748 |
{ |
| 3749 |
if (self->restype) { |
| 3750 |
return Py_NewRef(self->restype); |
| 3751 |
} |
| 3752 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 3753 |
StgInfo *info; |
| 3754 |
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { |
| 3755 |
return NULL; |
| 3756 |
} |
| 3757 |
assert(info); /* Cannot be NULL for PyCFuncPtrObject instances */ |
| 3758 |
if (info->restype) { |
| 3759 |
return Py_NewRef(info->restype); |
| 3760 |
} else { |
| 3761 |
Py_RETURN_NONE; |
| 3762 |
} |
| 3763 |
} |
| 3764 |
|
| 3765 |
/*[clinic input] |
| 3766 |
@setter |
| 3767 |
@critical_section |
| 3768 |
_ctypes.CFuncPtr.argtypes |
| 3769 |
[clinic start generated code]*/ |
| 3770 |
|
| 3771 |
static int |
| 3772 |
_ctypes_CFuncPtr_argtypes_set_impl(PyCFuncPtrObject *self, PyObject *value) |
| 3773 |
/*[clinic end generated code: output=596a36e2ae89d7d1 input=c4627573e980aa8b]*/ |
| 3774 |
{ |
| 3775 |
if (value == NULL || value == Py_None) { |
| 3776 |
atomic_xsetref(&self->argtypes, NULL); |
| 3777 |
atomic_xsetref(&self->converters, NULL); |
| 3778 |
} else { |
| 3779 |
PyTypeObject *type = Py_TYPE(self); |
| 3780 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 3781 |
|
| 3782 |
PyObject *converters = converters_from_argtypes(st, value); |
| 3783 |
if (!converters) |
| 3784 |
return -1; |
| 3785 |
|
| 3786 |
/* Verify paramflags again due to constraints with argtypes */ |
| 3787 |
if (!_validate_paramflags(st, type, self->paramflags, value)) { |
| 3788 |
Py_DECREF(converters); |
| 3789 |
return -1; |
| 3790 |
} |
| 3791 |
atomic_xsetref(&self->converters, converters); |
| 3792 |
Py_INCREF(value); |
| 3793 |
atomic_xsetref(&self->argtypes, value); |
| 3794 |
} |
| 3795 |
return 0; |
| 3796 |
} |
| 3797 |
|
| 3798 |
/*[clinic input] |
| 3799 |
@getter |
| 3800 |
@critical_section |
| 3801 |
_ctypes.CFuncPtr.argtypes |
| 3802 |
|
| 3803 |
specify the argument types |
| 3804 |
[clinic start generated code]*/ |
| 3805 |
|
| 3806 |
static PyObject * |
| 3807 |
_ctypes_CFuncPtr_argtypes_get_impl(PyCFuncPtrObject *self) |
| 3808 |
/*[clinic end generated code: output=c46b05a1b0f99172 input=37a8a545a56f8ae2]*/ |
| 3809 |
{ |
| 3810 |
if (self->argtypes) { |
| 3811 |
return Py_NewRef(self->argtypes); |
| 3812 |
} |
| 3813 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 3814 |
StgInfo *info; |
| 3815 |
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { |
| 3816 |
return NULL; |
| 3817 |
} |
| 3818 |
assert(info); /* Cannot be NULL for PyCFuncPtrObject instances */ |
| 3819 |
if (info->argtypes) { |
| 3820 |
return Py_NewRef(info->argtypes); |
| 3821 |
} else { |
| 3822 |
Py_RETURN_NONE; |
| 3823 |
} |
| 3824 |
} |
| 3825 |
|
| 3826 |
static PyGetSetDef PyCFuncPtr_getsets[] = { |
| 3827 |
_CTYPES_CFUNCPTR_ERRCHECK_GETSETDEF |
| 3828 |
_CTYPES_CFUNCPTR_RESTYPE_GETSETDEF |
| 3829 |
_CTYPES_CFUNCPTR_ARGTYPES_GETSETDEF |
| 3830 |
{ NULL, NULL } |
| 3831 |
}; |
| 3832 |
|
| 3833 |
#ifdef MS_WIN32 |
| 3834 |
static PPROC FindAddress(void *handle, const char *name, PyObject *type) |
| 3835 |
{ |
| 3836 |
PPROC address; |
| 3837 |
#ifdef MS_WIN64 |
| 3838 |
/* win64 has no stdcall calling conv, so it should |
| 3839 |
also not have the name mangling of it. |
| 3840 |
*/ |
| 3841 |
Py_BEGIN_ALLOW_THREADS |
| 3842 |
address = (PPROC)GetProcAddress(handle, name); |
| 3843 |
Py_END_ALLOW_THREADS |
| 3844 |
return address; |
| 3845 |
#else |
| 3846 |
char *mangled_name; |
| 3847 |
int i; |
| 3848 |
|
| 3849 |
Py_BEGIN_ALLOW_THREADS |
| 3850 |
address = (PPROC)GetProcAddress(handle, name); |
| 3851 |
Py_END_ALLOW_THREADS |
| 3852 |
if (address) |
| 3853 |
return address; |
| 3854 |
if (((size_t)name & ~0xFFFF) == 0) { |
| 3855 |
return NULL; |
| 3856 |
} |
| 3857 |
|
| 3858 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 3859 |
StgInfo *info; |
| 3860 |
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { |
| 3861 |
return NULL; |
| 3862 |
} |
| 3863 |
/* It should not happen that info is NULL, but better be safe */ |
| 3864 |
if (info==NULL || info->flags & FUNCFLAG_CDECL) |
| 3865 |
return address; |
| 3866 |
|
| 3867 |
/* for stdcall, try mangled names: |
| 3868 |
funcname -> _funcname@<n> |
| 3869 |
where n is 0, 4, 8, 12, ..., 128 |
| 3870 |
*/ |
| 3871 |
mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */ |
| 3872 |
if (!mangled_name) |
| 3873 |
return NULL; |
| 3874 |
for (i = 0; i < 32; ++i) { |
| 3875 |
sprintf(mangled_name, "_%s@%d", name, i*4); |
| 3876 |
Py_BEGIN_ALLOW_THREADS |
| 3877 |
address = (PPROC)GetProcAddress(handle, mangled_name); |
| 3878 |
Py_END_ALLOW_THREADS |
| 3879 |
if (address) |
| 3880 |
return address; |
| 3881 |
} |
| 3882 |
return NULL; |
| 3883 |
#endif |
| 3884 |
} |
| 3885 |
#endif |
| 3886 |
|
| 3887 |
/* Return 1 if usable, 0 else and exception set. */ |
| 3888 |
static int |
| 3889 |
_check_outarg_type(ctypes_state *st, PyObject *arg, Py_ssize_t index) |
| 3890 |
{ |
| 3891 |
if (PyCPointerTypeObject_Check(st, arg)) { |
| 3892 |
return 1; |
| 3893 |
} |
| 3894 |
if (PyCArrayTypeObject_Check(st, arg)) { |
| 3895 |
return 1; |
| 3896 |
} |
| 3897 |
StgInfo *info; |
| 3898 |
if (PyStgInfo_FromType(st, arg, &info) < 0) { |
| 3899 |
return -1; |
| 3900 |
} |
| 3901 |
if (info |
| 3902 |
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ |
| 3903 |
&& PyUnicode_Check(info->proto) |
| 3904 |
/* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter type */ |
| 3905 |
&& (strchr("PzZ", PyUnicode_AsUTF8(info->proto)[0]))) { |
| 3906 |
return 1; |
| 3907 |
} |
| 3908 |
|
| 3909 |
PyErr_Format(PyExc_TypeError, |
| 3910 |
"'out' parameter %d must be a pointer type, not %s", |
| 3911 |
Py_SAFE_DOWNCAST(index, Py_ssize_t, int), |
| 3912 |
PyType_Check(arg) ? |
| 3913 |
((PyTypeObject *)arg)->tp_name : |
| 3914 |
Py_TYPE(arg)->tp_name); |
| 3915 |
return 0; |
| 3916 |
} |
| 3917 |
|
| 3918 |
/* Returns 1 on success, 0 on error */ |
| 3919 |
static int |
| 3920 |
_validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags, PyObject *argtypes) |
| 3921 |
{ |
| 3922 |
Py_ssize_t i, len; |
| 3923 |
|
| 3924 |
StgInfo *info; |
| 3925 |
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { |
| 3926 |
return -1; |
| 3927 |
} |
| 3928 |
if (!info) { |
| 3929 |
PyErr_SetString(PyExc_TypeError, |
| 3930 |
"abstract class"); |
| 3931 |
return 0; |
| 3932 |
} |
| 3933 |
if (argtypes == NULL) { |
| 3934 |
argtypes = info->argtypes; |
| 3935 |
} |
| 3936 |
|
| 3937 |
if (paramflags == NULL || argtypes == NULL) { |
| 3938 |
return 1; |
| 3939 |
} |
| 3940 |
|
| 3941 |
if (!PyTuple_Check(paramflags)) { |
| 3942 |
PyErr_SetString(PyExc_TypeError, |
| 3943 |
"paramflags must be a tuple or None"); |
| 3944 |
return 0; |
| 3945 |
} |
| 3946 |
|
| 3947 |
len = PyTuple_GET_SIZE(paramflags); |
| 3948 |
if (len != PyTuple_GET_SIZE(argtypes)) { |
| 3949 |
PyErr_SetString(PyExc_ValueError, |
| 3950 |
"paramflags must have the same length as argtypes"); |
| 3951 |
return 0; |
| 3952 |
} |
| 3953 |
|
| 3954 |
for (i = 0; i < len; ++i) { |
| 3955 |
PyObject *item = PyTuple_GET_ITEM(paramflags, i); |
| 3956 |
int flag; |
| 3957 |
PyObject *name = Py_None; |
| 3958 |
PyObject *defval; |
| 3959 |
PyObject *typ; |
| 3960 |
if (!PyArg_ParseTuple(item, "i|OO", &flag, &name, &defval) || |
| 3961 |
!(name == Py_None || PyUnicode_Check(name))) |
| 3962 |
{ |
| 3963 |
PyErr_SetString(PyExc_TypeError, |
| 3964 |
"paramflags must be a sequence of (int [,string [,value]]) tuples"); |
| 3965 |
return 0; |
| 3966 |
} |
| 3967 |
typ = PyTuple_GET_ITEM(argtypes, i); |
| 3968 |
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { |
| 3969 |
case 0: |
| 3970 |
case PARAMFLAG_FIN: |
| 3971 |
case PARAMFLAG_FIN | PARAMFLAG_FLCID: |
| 3972 |
case PARAMFLAG_FIN | PARAMFLAG_FOUT: |
| 3973 |
break; |
| 3974 |
case PARAMFLAG_FOUT: |
| 3975 |
if (!_check_outarg_type(st, typ, i+1)) |
| 3976 |
return 0; |
| 3977 |
break; |
| 3978 |
default: |
| 3979 |
PyErr_Format(PyExc_TypeError, |
| 3980 |
"paramflag value %d not supported", |
| 3981 |
flag); |
| 3982 |
return 0; |
| 3983 |
} |
| 3984 |
} |
| 3985 |
return 1; |
| 3986 |
} |
| 3987 |
|
| 3988 |
static int |
| 3989 |
_get_name(PyObject *obj, void *arg) |
| 3990 |
{ |
| 3991 |
const char **pname = (const char **)arg; |
| 3992 |
#ifdef MS_WIN32 |
| 3993 |
if (PyLong_Check(obj)) { |
| 3994 |
/* We have to use MAKEINTRESOURCEA for Windows CE. |
| 3995 |
Works on Windows as well, of course. |
| 3996 |
*/ |
| 3997 |
*pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF); |
| 3998 |
return 1; |
| 3999 |
} |
| 4000 |
#endif |
| 4001 |
if (PyBytes_Check(obj)) { |
| 4002 |
*pname = PyBytes_AS_STRING(obj); |
| 4003 |
return *pname ? 1 : 0; |
| 4004 |
} |
| 4005 |
if (PyUnicode_Check(obj)) { |
| 4006 |
*pname = PyUnicode_AsUTF8(obj); |
| 4007 |
return *pname ? 1 : 0; |
| 4008 |
} |
| 4009 |
PyErr_SetString(PyExc_TypeError, |
| 4010 |
"function name must be string, bytes object or integer"); |
| 4011 |
return 0; |
| 4012 |
} |
| 4013 |
|
| 4014 |
|
| 4015 |
static PyObject * |
| 4016 |
PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4017 |
{ |
| 4018 |
const char *name; |
| 4019 |
int (* address)(void); |
| 4020 |
PyObject *ftuple; |
| 4021 |
PyObject *dll; |
| 4022 |
PyObject *obj; |
| 4023 |
PyCFuncPtrObject *self; |
| 4024 |
void *handle; |
| 4025 |
PyObject *paramflags = NULL; |
| 4026 |
|
| 4027 |
if (!PyArg_ParseTuple(args, "O|O", &ftuple, ¶mflags)) |
| 4028 |
return NULL; |
| 4029 |
if (paramflags == Py_None) |
| 4030 |
paramflags = NULL; |
| 4031 |
|
| 4032 |
ftuple = PySequence_Tuple(ftuple); |
| 4033 |
if (!ftuple) |
| 4034 |
/* Here ftuple is a borrowed reference */ |
| 4035 |
return NULL; |
| 4036 |
|
| 4037 |
if (!PyArg_ParseTuple(ftuple, "O&O;illegal func_spec argument", |
| 4038 |
_get_name, &name, &dll)) |
| 4039 |
{ |
| 4040 |
Py_DECREF(ftuple); |
| 4041 |
return NULL; |
| 4042 |
} |
| 4043 |
|
| 4044 |
#ifdef MS_WIN32 |
| 4045 |
if (PySys_Audit("ctypes.dlsym", |
| 4046 |
((uintptr_t)name & ~0xFFFF) ? "Os" : "On", |
| 4047 |
dll, name) < 0) { |
| 4048 |
Py_DECREF(ftuple); |
| 4049 |
return NULL; |
| 4050 |
} |
| 4051 |
#else |
| 4052 |
if (PySys_Audit("ctypes.dlsym", "Os", dll, name) < 0) { |
| 4053 |
Py_DECREF(ftuple); |
| 4054 |
return NULL; |
| 4055 |
} |
| 4056 |
#endif |
| 4057 |
|
| 4058 |
obj = PyObject_GetAttrString(dll, "_handle"); |
| 4059 |
if (!obj) { |
| 4060 |
Py_DECREF(ftuple); |
| 4061 |
return NULL; |
| 4062 |
} |
| 4063 |
if (!PyLong_Check(obj)) { |
| 4064 |
PyErr_SetString(PyExc_TypeError, |
| 4065 |
"the _handle attribute of the second argument must be an integer"); |
| 4066 |
Py_DECREF(ftuple); |
| 4067 |
Py_DECREF(obj); |
| 4068 |
return NULL; |
| 4069 |
} |
| 4070 |
handle = (void *)PyLong_AsVoidPtr(obj); |
| 4071 |
Py_DECREF(obj); |
| 4072 |
if (PyErr_Occurred()) { |
| 4073 |
PyErr_SetString(PyExc_ValueError, |
| 4074 |
"could not convert the _handle attribute to a pointer"); |
| 4075 |
Py_DECREF(ftuple); |
| 4076 |
return NULL; |
| 4077 |
} |
| 4078 |
|
| 4079 |
#undef USE_DLERROR |
| 4080 |
#ifdef MS_WIN32 |
| 4081 |
address = FindAddress(handle, name, (PyObject *)type); |
| 4082 |
if (!address) { |
| 4083 |
if (!IS_INTRESOURCE(name)) |
| 4084 |
PyErr_Format(PyExc_AttributeError, |
| 4085 |
"function '%s' not found", |
| 4086 |
name); |
| 4087 |
else |
| 4088 |
PyErr_Format(PyExc_AttributeError, |
| 4089 |
"function ordinal %d not found", |
| 4090 |
(WORD)(size_t)name); |
| 4091 |
Py_DECREF(ftuple); |
| 4092 |
return NULL; |
| 4093 |
} |
| 4094 |
#else |
| 4095 |
#ifdef __CYGWIN__ |
| 4096 |
//dlerror() isn't very helpful on cygwin */ |
| 4097 |
#else |
| 4098 |
#define USE_DLERROR |
| 4099 |
/* dlerror() always returns the latest error. |
| 4100 |
* |
| 4101 |
* Clear the previous value before calling dlsym(), |
| 4102 |
* to ensure we can tell if our call resulted in an error. |
| 4103 |
*/ |
| 4104 |
(void)dlerror(); |
| 4105 |
#endif |
| 4106 |
address = (PPROC)dlsym(handle, name); |
| 4107 |
|
| 4108 |
if (!address) { |
| 4109 |
#ifdef USE_DLERROR |
| 4110 |
const char *dlerr = dlerror(); |
| 4111 |
if (dlerr) { |
| 4112 |
_PyErr_SetLocaleString(PyExc_AttributeError, dlerr); |
| 4113 |
Py_DECREF(ftuple); |
| 4114 |
return NULL; |
| 4115 |
} |
| 4116 |
#endif |
| 4117 |
PyErr_Format(PyExc_AttributeError, "function '%s' not found", name); |
| 4118 |
Py_DECREF(ftuple); |
| 4119 |
return NULL; |
| 4120 |
} |
| 4121 |
#endif |
| 4122 |
#undef USE_DLERROR |
| 4123 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 4124 |
if (!_validate_paramflags(st, type, paramflags, NULL)) { |
| 4125 |
Py_DECREF(ftuple); |
| 4126 |
return NULL; |
| 4127 |
} |
| 4128 |
|
| 4129 |
self = (PyCFuncPtrObject *)generic_pycdata_new(st, type, args, kwds); |
| 4130 |
if (!self) { |
| 4131 |
Py_DECREF(ftuple); |
| 4132 |
return NULL; |
| 4133 |
} |
| 4134 |
|
| 4135 |
self->paramflags = Py_XNewRef(paramflags); |
| 4136 |
|
| 4137 |
// No other threads can have this object, no need to |
| 4138 |
// lock it. |
| 4139 |
*(void **)self->b_ptr = address; |
| 4140 |
Py_INCREF(dll); |
| 4141 |
Py_DECREF(ftuple); |
| 4142 |
if (-1 == KeepRef((CDataObject *)self, 0, dll)) { |
| 4143 |
Py_DECREF((PyObject *)self); |
| 4144 |
return NULL; |
| 4145 |
} |
| 4146 |
|
| 4147 |
self->callable = Py_NewRef(self); |
| 4148 |
return (PyObject *)self; |
| 4149 |
} |
| 4150 |
|
| 4151 |
#ifdef MS_WIN32 |
| 4152 |
static PyObject * |
| 4153 |
PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4154 |
{ |
| 4155 |
PyCFuncPtrObject *self; |
| 4156 |
int index; |
| 4157 |
char *name = NULL; |
| 4158 |
PyObject *paramflags = NULL; |
| 4159 |
GUID *iid = NULL; |
| 4160 |
Py_ssize_t iid_len = 0; |
| 4161 |
|
| 4162 |
if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, ¶mflags, &iid, &iid_len)) |
| 4163 |
return NULL; |
| 4164 |
if (paramflags == Py_None) |
| 4165 |
paramflags = NULL; |
| 4166 |
|
| 4167 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 4168 |
if (!_validate_paramflags(st, type, paramflags, NULL)) { |
| 4169 |
return NULL; |
| 4170 |
} |
| 4171 |
self = (PyCFuncPtrObject *)generic_pycdata_new(st, type, args, kwds); |
| 4172 |
self->index = index + 0x1000; |
| 4173 |
self->paramflags = Py_XNewRef(paramflags); |
| 4174 |
if (iid_len == sizeof(GUID)) |
| 4175 |
self->iid = iid; |
| 4176 |
return (PyObject *)self; |
| 4177 |
} |
| 4178 |
#endif |
| 4179 |
|
| 4180 |
/* |
| 4181 |
PyCFuncPtr_new accepts different argument lists in addition to the standard |
| 4182 |
_basespec_ keyword arg: |
| 4183 |
|
| 4184 |
one argument form |
| 4185 |
"i" - function address |
| 4186 |
"O" - must be a callable, creates a C callable function |
| 4187 |
|
| 4188 |
two or more argument forms (the third argument is a paramflags tuple) |
| 4189 |
"(sO)|..." - (function name, dll object (with an integer handle)), paramflags |
| 4190 |
"(iO)|..." - (function ordinal, dll object (with an integer handle)), paramflags |
| 4191 |
"is|..." - vtable index, method name, creates callable calling COM vtbl |
| 4192 |
*/ |
| 4193 |
static PyObject * |
| 4194 |
PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 4195 |
{ |
| 4196 |
PyCFuncPtrObject *self; |
| 4197 |
PyObject *callable; |
| 4198 |
CThunkObject *thunk; |
| 4199 |
|
| 4200 |
if (PyTuple_GET_SIZE(args) == 0) |
| 4201 |
return GenericPyCData_new(type, args, kwds); |
| 4202 |
|
| 4203 |
if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0))) |
| 4204 |
return PyCFuncPtr_FromDll(type, args, kwds); |
| 4205 |
|
| 4206 |
#ifdef MS_WIN32 |
| 4207 |
if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0))) |
| 4208 |
return PyCFuncPtr_FromVtblIndex(type, args, kwds); |
| 4209 |
#endif |
| 4210 |
|
| 4211 |
if (1 == PyTuple_GET_SIZE(args) |
| 4212 |
&& (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) { |
| 4213 |
CDataObject *ob; |
| 4214 |
void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0)); |
| 4215 |
if (ptr == NULL && PyErr_Occurred()) |
| 4216 |
return NULL; |
| 4217 |
ob = (CDataObject *)GenericPyCData_new(type, args, kwds); |
| 4218 |
if (ob == NULL) |
| 4219 |
return NULL; |
| 4220 |
*(void **)ob->b_ptr = ptr; |
| 4221 |
return (PyObject *)ob; |
| 4222 |
} |
| 4223 |
|
| 4224 |
if (!PyArg_ParseTuple(args, "O", &callable)) |
| 4225 |
return NULL; |
| 4226 |
if (!PyCallable_Check(callable)) { |
| 4227 |
PyErr_SetString(PyExc_TypeError, |
| 4228 |
"argument must be callable or integer function address"); |
| 4229 |
return NULL; |
| 4230 |
} |
| 4231 |
|
| 4232 |
/* XXX XXX This would allow passing additional options. For COM |
| 4233 |
method *implementations*, we would probably want different |
| 4234 |
behaviour than in 'normal' callback functions: return a HRESULT if |
| 4235 |
an exception occurs in the callback, and print the traceback not |
| 4236 |
only on the console, but also to OutputDebugString() or something |
| 4237 |
like that. |
| 4238 |
*/ |
| 4239 |
/* |
| 4240 |
if (kwds && _PyDict_GetItemIdWithError(kwds, &PyId_options)) { |
| 4241 |
... |
| 4242 |
} |
| 4243 |
else if (PyErr_Occurred()) { |
| 4244 |
return NULL; |
| 4245 |
} |
| 4246 |
*/ |
| 4247 |
|
| 4248 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 4249 |
StgInfo *info; |
| 4250 |
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { |
| 4251 |
return NULL; |
| 4252 |
} |
| 4253 |
/* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */ |
| 4254 |
if (!info || !info->argtypes) { |
| 4255 |
PyErr_SetString(PyExc_TypeError, |
| 4256 |
"cannot construct instance of this class:" |
| 4257 |
" no argtypes"); |
| 4258 |
return NULL; |
| 4259 |
} |
| 4260 |
|
| 4261 |
thunk = _ctypes_alloc_callback(st, |
| 4262 |
callable, |
| 4263 |
info->argtypes, |
| 4264 |
info->restype, |
| 4265 |
info->flags); |
| 4266 |
if (!thunk) |
| 4267 |
return NULL; |
| 4268 |
|
| 4269 |
self = (PyCFuncPtrObject *)generic_pycdata_new(st, type, args, kwds); |
| 4270 |
if (self == NULL) { |
| 4271 |
Py_DECREF(thunk); |
| 4272 |
return NULL; |
| 4273 |
} |
| 4274 |
|
| 4275 |
self->callable = Py_NewRef(callable); |
| 4276 |
|
| 4277 |
self->thunk = thunk; |
| 4278 |
*(void **)self->b_ptr = (void *)thunk->pcl_exec; |
| 4279 |
|
| 4280 |
Py_INCREF((PyObject *)thunk); /* for KeepRef */ |
| 4281 |
if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) { |
| 4282 |
Py_DECREF((PyObject *)self); |
| 4283 |
return NULL; |
| 4284 |
} |
| 4285 |
return (PyObject *)self; |
| 4286 |
} |
| 4287 |
|
| 4288 |
|
| 4289 |
/* |
| 4290 |
_byref consumes a refcount to its argument |
| 4291 |
*/ |
| 4292 |
static PyObject * |
| 4293 |
_byref(ctypes_state *st, PyObject *obj) |
| 4294 |
{ |
| 4295 |
PyCArgObject *parg; |
| 4296 |
|
| 4297 |
if (!CDataObject_Check(st, obj)) { |
| 4298 |
PyErr_SetString(PyExc_TypeError, |
| 4299 |
"expected CData instance"); |
| 4300 |
return NULL; |
| 4301 |
} |
| 4302 |
|
| 4303 |
parg = PyCArgObject_new(st); |
| 4304 |
if (parg == NULL) { |
| 4305 |
Py_DECREF(obj); |
| 4306 |
return NULL; |
| 4307 |
} |
| 4308 |
|
| 4309 |
parg->tag = 'P'; |
| 4310 |
parg->pffi_type = &ffi_type_pointer; |
| 4311 |
parg->obj = obj; |
| 4312 |
parg->value.p = ((CDataObject *)obj)->b_ptr; |
| 4313 |
return (PyObject *)parg; |
| 4314 |
} |
| 4315 |
|
| 4316 |
static PyObject * |
| 4317 |
_get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds) |
| 4318 |
{ |
| 4319 |
PyObject *v; |
| 4320 |
|
| 4321 |
if (*pindex < PyTuple_GET_SIZE(inargs)) { |
| 4322 |
v = PyTuple_GET_ITEM(inargs, *pindex); |
| 4323 |
++*pindex; |
| 4324 |
return Py_NewRef(v); |
| 4325 |
} |
| 4326 |
if (kwds && name) { |
| 4327 |
if (PyDict_GetItemRef(kwds, name, &v) < 0) { |
| 4328 |
return NULL; |
| 4329 |
} |
| 4330 |
if (v) { |
| 4331 |
++*pindex; |
| 4332 |
return v; |
| 4333 |
} |
| 4334 |
} |
| 4335 |
if (defval) { |
| 4336 |
return Py_NewRef(defval); |
| 4337 |
} |
| 4338 |
/* we can't currently emit a better error message */ |
| 4339 |
if (name) |
| 4340 |
PyErr_Format(PyExc_TypeError, |
| 4341 |
"required argument '%S' missing", name); |
| 4342 |
else |
| 4343 |
PyErr_Format(PyExc_TypeError, |
| 4344 |
"not enough arguments"); |
| 4345 |
return NULL; |
| 4346 |
} |
| 4347 |
|
| 4348 |
/* |
| 4349 |
This function implements higher level functionality plus the ability to call |
| 4350 |
functions with keyword arguments by looking at parameter flags. parameter |
| 4351 |
flags is a tuple of 1, 2 or 3-tuples. The first entry in each is an integer |
| 4352 |
specifying the direction of the data transfer for this parameter - 'in', |
| 4353 |
'out' or 'inout' (zero means the same as 'in'). The second entry is the |
| 4354 |
parameter name, and the third is the default value if the parameter is |
| 4355 |
missing in the function call. |
| 4356 |
|
| 4357 |
This function builds and returns a new tuple 'callargs' which contains the |
| 4358 |
parameters to use in the call. Items on this tuple are copied from the |
| 4359 |
'inargs' tuple for 'in' and 'in, out' parameters, and constructed from the |
| 4360 |
'argtypes' tuple for 'out' parameters. It also calculates numretvals which |
| 4361 |
is the number of return values for the function, outmask/inoutmask are |
| 4362 |
bitmasks containing indexes into the callargs tuple specifying which |
| 4363 |
parameters have to be returned. _build_result builds the return value of the |
| 4364 |
function. |
| 4365 |
*/ |
| 4366 |
static PyObject * |
| 4367 |
_build_callargs(ctypes_state *st, PyCFuncPtrObject *self, PyObject *argtypes, |
| 4368 |
PyObject *inargs, PyObject *kwds, |
| 4369 |
int *poutmask, int *pinoutmask, unsigned int *pnumretvals) |
| 4370 |
{ |
| 4371 |
PyObject *paramflags = self->paramflags; |
| 4372 |
PyObject *callargs; |
| 4373 |
Py_ssize_t i, len; |
| 4374 |
int inargs_index = 0; |
| 4375 |
/* It's a little bit difficult to determine how many arguments the |
| 4376 |
function call requires/accepts. For simplicity, we count the consumed |
| 4377 |
args and compare this to the number of supplied args. */ |
| 4378 |
Py_ssize_t actual_args; |
| 4379 |
|
| 4380 |
*poutmask = 0; |
| 4381 |
*pinoutmask = 0; |
| 4382 |
*pnumretvals = 0; |
| 4383 |
|
| 4384 |
/* Trivial cases, where we either return inargs itself, or a slice of it. */ |
| 4385 |
if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) { |
| 4386 |
#ifdef MS_WIN32 |
| 4387 |
if (self->index) |
| 4388 |
return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs)); |
| 4389 |
#endif |
| 4390 |
return Py_NewRef(inargs); |
| 4391 |
} |
| 4392 |
|
| 4393 |
len = PyTuple_GET_SIZE(argtypes); |
| 4394 |
callargs = PyTuple_New(len); /* the argument tuple we build */ |
| 4395 |
if (callargs == NULL) |
| 4396 |
return NULL; |
| 4397 |
|
| 4398 |
#ifdef MS_WIN32 |
| 4399 |
/* For a COM method, skip the first arg */ |
| 4400 |
if (self->index) { |
| 4401 |
inargs_index = 1; |
| 4402 |
} |
| 4403 |
#endif |
| 4404 |
for (i = 0; i < len; ++i) { |
| 4405 |
PyObject *item = PyTuple_GET_ITEM(paramflags, i); |
| 4406 |
PyObject *ob; |
| 4407 |
unsigned int flag; |
| 4408 |
PyObject *name = NULL; |
| 4409 |
PyObject *defval = NULL; |
| 4410 |
|
| 4411 |
/* This way seems to be ~2 us faster than the PyArg_ParseTuple |
| 4412 |
calls below. */ |
| 4413 |
/* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */ |
| 4414 |
Py_ssize_t tsize = PyTuple_GET_SIZE(item); |
| 4415 |
flag = PyLong_AsUnsignedLongMask(PyTuple_GET_ITEM(item, 0)); |
| 4416 |
name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL; |
| 4417 |
defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL; |
| 4418 |
|
| 4419 |
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) { |
| 4420 |
case PARAMFLAG_FIN | PARAMFLAG_FLCID: |
| 4421 |
/* ['in', 'lcid'] parameter. Always taken from defval, |
| 4422 |
if given, else the integer 0. */ |
| 4423 |
if (defval == NULL) { |
| 4424 |
defval = _PyLong_GetZero(); |
| 4425 |
} |
| 4426 |
Py_INCREF(defval); |
| 4427 |
PyTuple_SET_ITEM(callargs, i, defval); |
| 4428 |
break; |
| 4429 |
case (PARAMFLAG_FIN | PARAMFLAG_FOUT): |
| 4430 |
*pinoutmask |= (1 << i); /* mark as inout arg */ |
| 4431 |
(*pnumretvals)++; |
| 4432 |
_Py_FALLTHROUGH; |
| 4433 |
case 0: |
| 4434 |
case PARAMFLAG_FIN: |
| 4435 |
/* 'in' parameter. Copy it from inargs. */ |
| 4436 |
ob =_get_arg(&inargs_index, name, defval, inargs, kwds); |
| 4437 |
if (ob == NULL) |
| 4438 |
goto error; |
| 4439 |
PyTuple_SET_ITEM(callargs, i, ob); |
| 4440 |
break; |
| 4441 |
case PARAMFLAG_FOUT: |
| 4442 |
/* XXX Refactor this code into a separate function. */ |
| 4443 |
/* 'out' parameter. |
| 4444 |
argtypes[i] must be a POINTER to a c type. |
| 4445 |
|
| 4446 |
Cannot by supplied in inargs, but a defval will be used |
| 4447 |
if available. XXX Should we support getting it from kwds? |
| 4448 |
*/ |
| 4449 |
if (defval) { |
| 4450 |
/* XXX Using mutable objects as defval will |
| 4451 |
make the function non-threadsafe, unless we |
| 4452 |
copy the object in each invocation */ |
| 4453 |
Py_INCREF(defval); |
| 4454 |
PyTuple_SET_ITEM(callargs, i, defval); |
| 4455 |
*poutmask |= (1 << i); /* mark as out arg */ |
| 4456 |
(*pnumretvals)++; |
| 4457 |
break; |
| 4458 |
} |
| 4459 |
ob = PyTuple_GET_ITEM(argtypes, i); |
| 4460 |
StgInfo *info; |
| 4461 |
if (PyStgInfo_FromType(st, ob, &info) < 0) { |
| 4462 |
goto error; |
| 4463 |
} |
| 4464 |
if (info == NULL) { |
| 4465 |
/* Cannot happen: _validate_paramflags() |
| 4466 |
would not accept such an object */ |
| 4467 |
PyErr_Format(PyExc_RuntimeError, |
| 4468 |
"NULL stginfo unexpected"); |
| 4469 |
goto error; |
| 4470 |
} |
| 4471 |
if (PyUnicode_Check(info->proto)) { |
| 4472 |
PyErr_Format( |
| 4473 |
PyExc_TypeError, |
| 4474 |
"%s 'out' parameter must be passed as default value", |
| 4475 |
((PyTypeObject *)ob)->tp_name); |
| 4476 |
goto error; |
| 4477 |
} |
| 4478 |
if (PyCArrayTypeObject_Check(st, ob)) { |
| 4479 |
ob = _PyObject_CallNoArgs(ob); |
| 4480 |
} |
| 4481 |
else { |
| 4482 |
/* Create an instance of the pointed-to type */ |
| 4483 |
ob = _PyObject_CallNoArgs(info->proto); |
| 4484 |
} |
| 4485 |
/* |
| 4486 |
XXX Is the following correct any longer? |
| 4487 |
We must not pass a byref() to the array then but |
| 4488 |
the array instance itself. Then, we cannot retrieve |
| 4489 |
the result from the PyCArgObject. |
| 4490 |
*/ |
| 4491 |
if (ob == NULL) |
| 4492 |
goto error; |
| 4493 |
/* The .from_param call that will occur later will pass this |
| 4494 |
as a byref parameter. */ |
| 4495 |
PyTuple_SET_ITEM(callargs, i, ob); |
| 4496 |
*poutmask |= (1 << i); /* mark as out arg */ |
| 4497 |
(*pnumretvals)++; |
| 4498 |
break; |
| 4499 |
default: |
| 4500 |
PyErr_Format(PyExc_ValueError, |
| 4501 |
"paramflag %u not yet implemented", flag); |
| 4502 |
goto error; |
| 4503 |
break; |
| 4504 |
} |
| 4505 |
} |
| 4506 |
|
| 4507 |
/* We have counted the arguments we have consumed in 'inargs_index'. This |
| 4508 |
must be the same as len(inargs) + len(kwds), otherwise we have |
| 4509 |
either too much or not enough arguments. */ |
| 4510 |
|
| 4511 |
actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_GET_SIZE(kwds) : 0); |
| 4512 |
if (actual_args != inargs_index) { |
| 4513 |
/* When we have default values or named parameters, this error |
| 4514 |
message is misleading. See unittests/test_paramflags.py |
| 4515 |
*/ |
| 4516 |
PyErr_Format(PyExc_TypeError, |
| 4517 |
"call takes exactly %d arguments (%zd given)", |
| 4518 |
inargs_index, actual_args); |
| 4519 |
goto error; |
| 4520 |
} |
| 4521 |
|
| 4522 |
/* outmask is a bitmask containing indexes into callargs. Items at |
| 4523 |
these indexes contain values to return. |
| 4524 |
*/ |
| 4525 |
return callargs; |
| 4526 |
error: |
| 4527 |
Py_DECREF(callargs); |
| 4528 |
return NULL; |
| 4529 |
} |
| 4530 |
|
| 4531 |
/* See also: |
| 4532 |
http://msdn.microsoft.com/library/en-us/com/html/769127a1-1a14-4ed4-9d38-7cf3e571b661.asp |
| 4533 |
*/ |
| 4534 |
/* |
| 4535 |
Build return value of a function. |
| 4536 |
|
| 4537 |
Consumes the refcount on result and callargs. |
| 4538 |
*/ |
| 4539 |
static PyObject * |
| 4540 |
_build_result(PyObject *result, PyObject *callargs, |
| 4541 |
int outmask, int inoutmask, unsigned int numretvals) |
| 4542 |
{ |
| 4543 |
unsigned int i, index; |
| 4544 |
int bit; |
| 4545 |
PyObject *tup = NULL; |
| 4546 |
|
| 4547 |
if (callargs == NULL) |
| 4548 |
return result; |
| 4549 |
if (result == NULL || numretvals == 0) { |
| 4550 |
Py_DECREF(callargs); |
| 4551 |
return result; |
| 4552 |
} |
| 4553 |
Py_DECREF(result); |
| 4554 |
|
| 4555 |
/* tup will not be allocated if numretvals == 1 */ |
| 4556 |
/* allocate tuple to hold the result */ |
| 4557 |
if (numretvals > 1) { |
| 4558 |
tup = PyTuple_New(numretvals); |
| 4559 |
if (tup == NULL) { |
| 4560 |
Py_DECREF(callargs); |
| 4561 |
return NULL; |
| 4562 |
} |
| 4563 |
} |
| 4564 |
|
| 4565 |
index = 0; |
| 4566 |
for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) { |
| 4567 |
PyObject *v; |
| 4568 |
if (bit & inoutmask) { |
| 4569 |
v = PyTuple_GET_ITEM(callargs, i); |
| 4570 |
Py_INCREF(v); |
| 4571 |
if (numretvals == 1) { |
| 4572 |
Py_DECREF(callargs); |
| 4573 |
return v; |
| 4574 |
} |
| 4575 |
PyTuple_SET_ITEM(tup, index, v); |
| 4576 |
index++; |
| 4577 |
} else if (bit & outmask) { |
| 4578 |
|
| 4579 |
v = PyTuple_GET_ITEM(callargs, i); |
| 4580 |
v = PyObject_CallMethodNoArgs(v, &_Py_ID(__ctypes_from_outparam__)); |
| 4581 |
if (v == NULL || numretvals == 1) { |
| 4582 |
Py_XDECREF(tup); |
| 4583 |
Py_DECREF(callargs); |
| 4584 |
return v; |
| 4585 |
} |
| 4586 |
PyTuple_SET_ITEM(tup, index, v); |
| 4587 |
index++; |
| 4588 |
} |
| 4589 |
if (index == numretvals) |
| 4590 |
break; |
| 4591 |
} |
| 4592 |
|
| 4593 |
Py_DECREF(callargs); |
| 4594 |
return tup; |
| 4595 |
} |
| 4596 |
|
| 4597 |
static PyObject * |
| 4598 |
PyCFuncPtr_call(PyObject *op, PyObject *inargs, PyObject *kwds) |
| 4599 |
{ |
| 4600 |
PyObject *result = NULL; |
| 4601 |
PyObject *callargs = NULL; |
| 4602 |
PyObject *ret = NULL; |
| 4603 |
#ifdef MS_WIN32 |
| 4604 |
IUnknown *piunk = NULL; |
| 4605 |
#endif |
| 4606 |
void *pProc = NULL; |
| 4607 |
PyCFuncPtrObject *self = _PyCFuncPtrObject_CAST(op); |
| 4608 |
|
| 4609 |
int inoutmask; |
| 4610 |
int outmask; |
| 4611 |
unsigned int numretvals; |
| 4612 |
|
| 4613 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 4614 |
StgInfo *info; |
| 4615 |
if (PyStgInfo_FromObject(st, op, &info) < 0) { |
| 4616 |
return NULL; |
| 4617 |
} |
| 4618 |
assert(info); /* Cannot be NULL for PyCFuncPtrObject instances */ |
| 4619 |
|
| 4620 |
PyObject *restype = atomic_xgetref(op, &self->restype); |
| 4621 |
if (restype == NULL) { |
| 4622 |
restype = Py_XNewRef(info->restype); |
| 4623 |
} |
| 4624 |
PyObject *converters = atomic_xgetref(op, &self->converters); |
| 4625 |
if (converters == NULL) { |
| 4626 |
converters = Py_XNewRef(info->converters); |
| 4627 |
} |
| 4628 |
PyObject *checker = atomic_xgetref(op, &self->checker); |
| 4629 |
if (checker == NULL) { |
| 4630 |
checker = Py_XNewRef(info->checker); |
| 4631 |
} |
| 4632 |
PyObject *argtypes = atomic_xgetref(op, &self->argtypes); |
| 4633 |
if (argtypes == NULL) { |
| 4634 |
argtypes = Py_XNewRef(info->argtypes); |
| 4635 |
} |
| 4636 |
/* later, we probably want to have an errcheck field in stginfo */ |
| 4637 |
PyObject *errcheck = atomic_xgetref(op, &self->errcheck); |
| 4638 |
|
| 4639 |
pProc = *(void **)self->b_ptr; |
| 4640 |
#ifdef MS_WIN32 |
| 4641 |
if (self->index) { |
| 4642 |
/* It's a COM method */ |
| 4643 |
CDataObject *this; |
| 4644 |
this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */ |
| 4645 |
if (!this) { |
| 4646 |
PyErr_SetString(PyExc_ValueError, |
| 4647 |
"native com method call without 'this' parameter"); |
| 4648 |
goto finally; |
| 4649 |
} |
| 4650 |
if (!CDataObject_Check(st, this)) { |
| 4651 |
PyErr_SetString(PyExc_TypeError, |
| 4652 |
"Expected a COM this pointer as first argument"); |
| 4653 |
goto finally; |
| 4654 |
} |
| 4655 |
/* there should be more checks? No, in Python */ |
| 4656 |
/* First arg is a pointer to an interface instance */ |
| 4657 |
if (!this->b_ptr || *(void **)this->b_ptr == NULL) { |
| 4658 |
PyErr_SetString(PyExc_ValueError, |
| 4659 |
"NULL COM pointer access"); |
| 4660 |
goto finally; |
| 4661 |
} |
| 4662 |
piunk = *(IUnknown **)this->b_ptr; |
| 4663 |
if (NULL == piunk->lpVtbl) { |
| 4664 |
PyErr_SetString(PyExc_ValueError, |
| 4665 |
"COM method call without VTable"); |
| 4666 |
goto finally; |
| 4667 |
} |
| 4668 |
pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000]; |
| 4669 |
} |
| 4670 |
#endif |
| 4671 |
callargs = _build_callargs(st, self, argtypes, |
| 4672 |
inargs, kwds, |
| 4673 |
&outmask, &inoutmask, &numretvals); |
| 4674 |
if (callargs == NULL) { |
| 4675 |
goto finally; |
| 4676 |
} |
| 4677 |
|
| 4678 |
if (converters) { |
| 4679 |
int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters), |
| 4680 |
Py_ssize_t, int); |
| 4681 |
int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs), |
| 4682 |
Py_ssize_t, int); |
| 4683 |
|
| 4684 |
if ((info->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) { |
| 4685 |
/* For cdecl functions, we allow more actual arguments |
| 4686 |
than the length of the argtypes tuple. |
| 4687 |
*/ |
| 4688 |
if (required > actual) { |
| 4689 |
Py_DECREF(callargs); |
| 4690 |
PyErr_Format(PyExc_TypeError, |
| 4691 |
"this function takes at least %d argument%s (%d given)", |
| 4692 |
required, |
| 4693 |
required == 1 ? "" : "s", |
| 4694 |
actual); |
| 4695 |
goto finally; |
| 4696 |
} |
| 4697 |
} else if (required != actual) { |
| 4698 |
Py_DECREF(callargs); |
| 4699 |
PyErr_Format(PyExc_TypeError, |
| 4700 |
"this function takes %d argument%s (%d given)", |
| 4701 |
required, |
| 4702 |
required == 1 ? "" : "s", |
| 4703 |
actual); |
| 4704 |
goto finally; |
| 4705 |
} |
| 4706 |
} |
| 4707 |
|
| 4708 |
result = _ctypes_callproc(st, |
| 4709 |
pProc, |
| 4710 |
callargs, |
| 4711 |
#ifdef MS_WIN32 |
| 4712 |
piunk, |
| 4713 |
self->iid, |
| 4714 |
#endif |
| 4715 |
info->flags, |
| 4716 |
converters, |
| 4717 |
restype, |
| 4718 |
checker); |
| 4719 |
/* The 'errcheck' protocol */ |
| 4720 |
if (result != NULL && errcheck) { |
| 4721 |
PyObject *v = PyObject_CallFunctionObjArgs(errcheck, |
| 4722 |
result, |
| 4723 |
self, |
| 4724 |
callargs, |
| 4725 |
NULL); |
| 4726 |
/* If the errcheck function failed, return NULL. |
| 4727 |
If the errcheck function returned callargs unchanged, |
| 4728 |
continue normal processing. |
| 4729 |
If the errcheck function returned something else, |
| 4730 |
use that as result. |
| 4731 |
*/ |
| 4732 |
if (v == NULL || v != callargs) { |
| 4733 |
Py_DECREF(result); |
| 4734 |
Py_DECREF(callargs); |
| 4735 |
ret = v; |
| 4736 |
goto finally; |
| 4737 |
} |
| 4738 |
Py_DECREF(v); |
| 4739 |
} |
| 4740 |
ret = _build_result(result, callargs, outmask, inoutmask, numretvals); |
| 4741 |
finally: |
| 4742 |
Py_XDECREF(restype); |
| 4743 |
Py_XDECREF(converters); |
| 4744 |
Py_XDECREF(checker); |
| 4745 |
Py_XDECREF(argtypes); |
| 4746 |
Py_XDECREF(errcheck); |
| 4747 |
return ret; |
| 4748 |
} |
| 4749 |
|
| 4750 |
static int |
| 4751 |
PyCFuncPtr_traverse(PyObject *op, visitproc visit, void *arg) |
| 4752 |
{ |
| 4753 |
PyCFuncPtrObject *self = _PyCFuncPtrObject_CAST(op); |
| 4754 |
Py_VISIT(self->callable); |
| 4755 |
Py_VISIT(self->restype); |
| 4756 |
Py_VISIT(self->checker); |
| 4757 |
Py_VISIT(self->errcheck); |
| 4758 |
Py_VISIT(self->argtypes); |
| 4759 |
Py_VISIT(self->converters); |
| 4760 |
Py_VISIT(self->paramflags); |
| 4761 |
Py_VISIT(self->thunk); |
| 4762 |
return PyCData_traverse(op, visit, arg); |
| 4763 |
} |
| 4764 |
|
| 4765 |
static int |
| 4766 |
PyCFuncPtr_clear(PyObject *op) |
| 4767 |
{ |
| 4768 |
PyCFuncPtrObject *self = _PyCFuncPtrObject_CAST(op); |
| 4769 |
Py_CLEAR(self->callable); |
| 4770 |
Py_CLEAR(self->restype); |
| 4771 |
Py_CLEAR(self->checker); |
| 4772 |
Py_CLEAR(self->errcheck); |
| 4773 |
Py_CLEAR(self->argtypes); |
| 4774 |
Py_CLEAR(self->converters); |
| 4775 |
Py_CLEAR(self->paramflags); |
| 4776 |
Py_CLEAR(self->thunk); |
| 4777 |
return PyCData_clear(op); |
| 4778 |
} |
| 4779 |
|
| 4780 |
static void |
| 4781 |
PyCFuncPtr_dealloc(PyObject *self) |
| 4782 |
{ |
| 4783 |
PyObject_GC_UnTrack(self); |
| 4784 |
(void)PyCFuncPtr_clear(self); |
| 4785 |
PyTypeObject *type = Py_TYPE(self); |
| 4786 |
type->tp_free(self); |
| 4787 |
Py_DECREF(type); |
| 4788 |
} |
| 4789 |
|
| 4790 |
static PyObject * |
| 4791 |
PyCFuncPtr_repr(PyObject *op) |
| 4792 |
{ |
| 4793 |
PyCFuncPtrObject *self = _PyCFuncPtrObject_CAST(op); |
| 4794 |
#ifdef MS_WIN32 |
| 4795 |
if (self->index) |
| 4796 |
return PyUnicode_FromFormat("<COM method offset %d: %s at %p>", |
| 4797 |
self->index - 0x1000, |
| 4798 |
Py_TYPE(self)->tp_name, |
| 4799 |
self); |
| 4800 |
#endif |
| 4801 |
return PyUnicode_FromFormat("<%s object at %p>", |
| 4802 |
Py_TYPE(self)->tp_name, |
| 4803 |
self); |
| 4804 |
} |
| 4805 |
|
| 4806 |
static int |
| 4807 |
PyCFuncPtr_bool(PyObject *op) |
| 4808 |
{ |
| 4809 |
PyCFuncPtrObject *self = _PyCFuncPtrObject_CAST(op); |
| 4810 |
return ((*(void **)self->b_ptr != NULL) |
| 4811 |
#ifdef MS_WIN32 |
| 4812 |
|| (self->index != 0) |
| 4813 |
#endif |
| 4814 |
); |
| 4815 |
} |
| 4816 |
|
| 4817 |
static PyType_Slot pycfuncptr_slots[] = { |
| 4818 |
{Py_tp_dealloc, PyCFuncPtr_dealloc}, |
| 4819 |
{Py_tp_repr, PyCFuncPtr_repr}, |
| 4820 |
{Py_tp_call, PyCFuncPtr_call}, |
| 4821 |
{Py_tp_doc, PyDoc_STR("Function Pointer")}, |
| 4822 |
{Py_tp_traverse, PyCFuncPtr_traverse}, |
| 4823 |
{Py_tp_clear, PyCFuncPtr_clear}, |
| 4824 |
{Py_tp_getset, PyCFuncPtr_getsets}, |
| 4825 |
{Py_tp_new, PyCFuncPtr_new}, |
| 4826 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 4827 |
{Py_nb_bool, PyCFuncPtr_bool}, |
| 4828 |
{0, NULL}, |
| 4829 |
}; |
| 4830 |
|
| 4831 |
static PyType_Spec pycfuncptr_spec = { |
| 4832 |
.name = "_ctypes.CFuncPtr", |
| 4833 |
.basicsize = sizeof(PyCFuncPtrObject), |
| 4834 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC | |
| 4835 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 4836 |
.slots = pycfuncptr_slots, |
| 4837 |
}; |
| 4838 |
|
| 4839 |
/*****************************************************************/ |
| 4840 |
/* |
| 4841 |
Struct_Type |
| 4842 |
*/ |
| 4843 |
/* |
| 4844 |
This function is called to initialize a Structure or Union with positional |
| 4845 |
arguments. It calls itself recursively for all Structure or Union base |
| 4846 |
classes, then retrieves the _fields_ member to associate the argument |
| 4847 |
position with the correct field name. |
| 4848 |
|
| 4849 |
Returns -1 on error, or the index of next argument on success. |
| 4850 |
*/ |
| 4851 |
static Py_ssize_t |
| 4852 |
_init_pos_args(PyObject *self, PyTypeObject *type, |
| 4853 |
PyObject *args, PyObject *kwds, |
| 4854 |
Py_ssize_t index) |
| 4855 |
{ |
| 4856 |
PyObject *fields; |
| 4857 |
Py_ssize_t i; |
| 4858 |
|
| 4859 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 4860 |
StgInfo *baseinfo; |
| 4861 |
if (PyStgInfo_FromType(st, (PyObject *)type->tp_base, &baseinfo) < 0) { |
| 4862 |
return -1; |
| 4863 |
} |
| 4864 |
if (baseinfo) { |
| 4865 |
index = _init_pos_args(self, type->tp_base, |
| 4866 |
args, kwds, |
| 4867 |
index); |
| 4868 |
if (index == -1) |
| 4869 |
return -1; |
| 4870 |
} |
| 4871 |
|
| 4872 |
StgInfo *info; |
| 4873 |
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { |
| 4874 |
return -1; |
| 4875 |
} |
| 4876 |
assert(info); |
| 4877 |
|
| 4878 |
PyObject *attrdict = PyType_GetDict(type); |
| 4879 |
assert(attrdict); |
| 4880 |
|
| 4881 |
fields = PyDict_GetItemWithError((PyObject *)attrdict, &_Py_ID(_fields_)); |
| 4882 |
Py_CLEAR(attrdict); |
| 4883 |
if (fields == NULL) { |
| 4884 |
if (PyErr_Occurred()) { |
| 4885 |
return -1; |
| 4886 |
} |
| 4887 |
return index; |
| 4888 |
} |
| 4889 |
|
| 4890 |
for (i = index; |
| 4891 |
i < info->length && i < PyTuple_GET_SIZE(args); |
| 4892 |
++i) { |
| 4893 |
PyObject *pair = PySequence_GetItem(fields, i - index); |
| 4894 |
PyObject *name, *val; |
| 4895 |
int res; |
| 4896 |
if (!pair) |
| 4897 |
return -1; |
| 4898 |
name = PySequence_GetItem(pair, 0); |
| 4899 |
if (!name) { |
| 4900 |
Py_DECREF(pair); |
| 4901 |
return -1; |
| 4902 |
} |
| 4903 |
val = PyTuple_GET_ITEM(args, i); |
| 4904 |
if (kwds) { |
| 4905 |
res = PyDict_Contains(kwds, name); |
| 4906 |
if (res != 0) { |
| 4907 |
if (res > 0) { |
| 4908 |
PyErr_Format(PyExc_TypeError, |
| 4909 |
"duplicate values for field %R", |
| 4910 |
name); |
| 4911 |
} |
| 4912 |
Py_DECREF(pair); |
| 4913 |
Py_DECREF(name); |
| 4914 |
return -1; |
| 4915 |
} |
| 4916 |
} |
| 4917 |
|
| 4918 |
res = PyObject_SetAttr(self, name, val); |
| 4919 |
Py_DECREF(pair); |
| 4920 |
Py_DECREF(name); |
| 4921 |
if (res == -1) |
| 4922 |
return -1; |
| 4923 |
} |
| 4924 |
return info->length; |
| 4925 |
} |
| 4926 |
|
| 4927 |
static int |
| 4928 |
Struct_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 4929 |
{ |
| 4930 |
/* Optimization possible: Store the attribute names _fields_[x][0] |
| 4931 |
* in C accessible fields somewhere ? |
| 4932 |
*/ |
| 4933 |
if (!PyTuple_Check(args)) { |
| 4934 |
PyErr_SetString(PyExc_TypeError, |
| 4935 |
"args not a tuple?"); |
| 4936 |
return -1; |
| 4937 |
} |
| 4938 |
if (PyTuple_GET_SIZE(args)) { |
| 4939 |
Py_ssize_t res = _init_pos_args(self, Py_TYPE(self), |
| 4940 |
args, kwds, 0); |
| 4941 |
if (res == -1) |
| 4942 |
return -1; |
| 4943 |
if (res < PyTuple_GET_SIZE(args)) { |
| 4944 |
PyErr_SetString(PyExc_TypeError, |
| 4945 |
"too many initializers"); |
| 4946 |
return -1; |
| 4947 |
} |
| 4948 |
} |
| 4949 |
|
| 4950 |
if (kwds) { |
| 4951 |
PyObject *key, *value; |
| 4952 |
Py_ssize_t pos = 0; |
| 4953 |
while(PyDict_Next(kwds, &pos, &key, &value)) { |
| 4954 |
if (-1 == PyObject_SetAttr(self, key, value)) |
| 4955 |
return -1; |
| 4956 |
} |
| 4957 |
} |
| 4958 |
return 0; |
| 4959 |
} |
| 4960 |
|
| 4961 |
static PyType_Slot pycstruct_slots[] = { |
| 4962 |
{Py_tp_doc, PyDoc_STR("Structure base class")}, |
| 4963 |
{Py_tp_init, Struct_init}, |
| 4964 |
{Py_tp_new, GenericPyCData_new}, |
| 4965 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 4966 |
{0, NULL}, |
| 4967 |
}; |
| 4968 |
|
| 4969 |
static PyType_Spec pycstruct_spec = { |
| 4970 |
.name = "_ctypes.Structure", |
| 4971 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 4972 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 4973 |
.slots = pycstruct_slots, |
| 4974 |
}; |
| 4975 |
|
| 4976 |
static PyType_Slot pycunion_slots[] = { |
| 4977 |
{Py_tp_doc, PyDoc_STR("Union base class")}, |
| 4978 |
{Py_tp_init, Struct_init}, |
| 4979 |
{Py_tp_new, GenericPyCData_new}, |
| 4980 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 4981 |
{0, NULL}, |
| 4982 |
}; |
| 4983 |
|
| 4984 |
static PyType_Spec pycunion_spec = { |
| 4985 |
.name = "_ctypes.Union", |
| 4986 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 4987 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 4988 |
.slots = pycunion_slots, |
| 4989 |
}; |
| 4990 |
|
| 4991 |
|
| 4992 |
/******************************************************************/ |
| 4993 |
/* |
| 4994 |
PyCArray_Type |
| 4995 |
*/ |
| 4996 |
static int |
| 4997 |
Array_init(PyObject *self, PyObject *args, PyObject *kw) |
| 4998 |
{ |
| 4999 |
Py_ssize_t i; |
| 5000 |
Py_ssize_t n; |
| 5001 |
|
| 5002 |
if (!PyTuple_Check(args)) { |
| 5003 |
PyErr_SetString(PyExc_TypeError, |
| 5004 |
"args not a tuple?"); |
| 5005 |
return -1; |
| 5006 |
} |
| 5007 |
n = PyTuple_GET_SIZE(args); |
| 5008 |
for (i = 0; i < n; ++i) { |
| 5009 |
PyObject *v; |
| 5010 |
v = PyTuple_GET_ITEM(args, i); |
| 5011 |
if (-1 == PySequence_SetItem(self, i, v)) |
| 5012 |
return -1; |
| 5013 |
} |
| 5014 |
return 0; |
| 5015 |
} |
| 5016 |
|
| 5017 |
static PyObject * |
| 5018 |
Array_item_lock_held(PyObject *myself, Py_ssize_t index) |
| 5019 |
{ |
| 5020 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself); |
| 5021 |
|
| 5022 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5023 |
Py_ssize_t offset, size; |
| 5024 |
|
| 5025 |
if (index < 0 || index >= self->b_length) { |
| 5026 |
PyErr_SetString(PyExc_IndexError, |
| 5027 |
"invalid index"); |
| 5028 |
return NULL; |
| 5029 |
} |
| 5030 |
|
| 5031 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5032 |
StgInfo *stginfo; |
| 5033 |
if (PyStgInfo_FromObject(st, myself, &stginfo) < 0) { |
| 5034 |
return NULL; |
| 5035 |
} |
| 5036 |
|
| 5037 |
/* Would it be clearer if we got the item size from |
| 5038 |
stginfo->proto's stginfo? |
| 5039 |
*/ |
| 5040 |
size = stginfo->size / stginfo->length; |
| 5041 |
offset = index * size; |
| 5042 |
|
| 5043 |
return PyCData_get(st, stginfo->proto, stginfo->getfunc, myself, |
| 5044 |
index, size, self->b_ptr + offset); |
| 5045 |
} |
| 5046 |
|
| 5047 |
static PyObject * |
| 5048 |
Array_item(PyObject *myself, Py_ssize_t index) |
| 5049 |
{ |
| 5050 |
PyObject *result; |
| 5051 |
Py_BEGIN_CRITICAL_SECTION(myself); |
| 5052 |
result = Array_item_lock_held(myself, index); |
| 5053 |
Py_END_CRITICAL_SECTION(); |
| 5054 |
return result; |
| 5055 |
} |
| 5056 |
|
| 5057 |
static PyObject * |
| 5058 |
Array_subscript_lock_held(PyObject *myself, PyObject *item) |
| 5059 |
{ |
| 5060 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself); |
| 5061 |
|
| 5062 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5063 |
|
| 5064 |
if (PyIndex_Check(item)) { |
| 5065 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 5066 |
|
| 5067 |
if (i == -1 && PyErr_Occurred()) |
| 5068 |
return NULL; |
| 5069 |
if (i < 0) |
| 5070 |
i += self->b_length; |
| 5071 |
return Array_item_lock_held(myself, i); |
| 5072 |
} |
| 5073 |
else if (PySlice_Check(item)) { |
| 5074 |
PyObject *proto; |
| 5075 |
PyObject *np; |
| 5076 |
Py_ssize_t start, stop, step, slicelen, i; |
| 5077 |
size_t cur; |
| 5078 |
|
| 5079 |
if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
| 5080 |
return NULL; |
| 5081 |
} |
| 5082 |
slicelen = PySlice_AdjustIndices(self->b_length, &start, &stop, step); |
| 5083 |
|
| 5084 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5085 |
StgInfo *stginfo; |
| 5086 |
if (PyStgInfo_FromObject(st, myself, &stginfo) < 0) { |
| 5087 |
return NULL; |
| 5088 |
} |
| 5089 |
assert(stginfo); /* Cannot be NULL for array object instances */ |
| 5090 |
proto = stginfo->proto; |
| 5091 |
StgInfo *iteminfo; |
| 5092 |
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) { |
| 5093 |
return NULL; |
| 5094 |
} |
| 5095 |
assert(iteminfo); /* proto is the item type of the array, a |
| 5096 |
ctypes type, so this cannot be NULL */ |
| 5097 |
|
| 5098 |
if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
| 5099 |
char *ptr = (char *)self->b_ptr; |
| 5100 |
char *dest; |
| 5101 |
|
| 5102 |
if (slicelen <= 0) |
| 5103 |
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
| 5104 |
if (step == 1) { |
| 5105 |
PyObject *res; |
| 5106 |
res = PyBytes_FromStringAndSize(ptr + start, |
| 5107 |
slicelen); |
| 5108 |
return res; |
| 5109 |
} |
| 5110 |
dest = (char *)PyMem_Malloc(slicelen); |
| 5111 |
|
| 5112 |
if (dest == NULL) |
| 5113 |
return PyErr_NoMemory(); |
| 5114 |
|
| 5115 |
for (cur = start, i = 0; i < slicelen; |
| 5116 |
cur += step, i++) { |
| 5117 |
dest[i] = ptr[cur]; |
| 5118 |
} |
| 5119 |
|
| 5120 |
np = PyBytes_FromStringAndSize(dest, slicelen); |
| 5121 |
PyMem_Free(dest); |
| 5122 |
return np; |
| 5123 |
} |
| 5124 |
if (iteminfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
| 5125 |
wchar_t *ptr = (wchar_t *)self->b_ptr; |
| 5126 |
wchar_t *dest; |
| 5127 |
|
| 5128 |
if (slicelen <= 0) |
| 5129 |
return Py_GetConstant(Py_CONSTANT_EMPTY_STR); |
| 5130 |
if (step == 1) { |
| 5131 |
PyObject *res; |
| 5132 |
res = PyUnicode_FromWideChar(ptr + start, |
| 5133 |
slicelen); |
| 5134 |
return res; |
| 5135 |
} |
| 5136 |
|
| 5137 |
dest = PyMem_New(wchar_t, slicelen); |
| 5138 |
if (dest == NULL) { |
| 5139 |
PyErr_NoMemory(); |
| 5140 |
return NULL; |
| 5141 |
} |
| 5142 |
|
| 5143 |
for (cur = start, i = 0; i < slicelen; |
| 5144 |
cur += step, i++) { |
| 5145 |
dest[i] = ptr[cur]; |
| 5146 |
} |
| 5147 |
|
| 5148 |
np = PyUnicode_FromWideChar(dest, slicelen); |
| 5149 |
PyMem_Free(dest); |
| 5150 |
return np; |
| 5151 |
} |
| 5152 |
|
| 5153 |
np = PyList_New(slicelen); |
| 5154 |
if (np == NULL) |
| 5155 |
return NULL; |
| 5156 |
|
| 5157 |
for (cur = start, i = 0; i < slicelen; |
| 5158 |
cur += step, i++) { |
| 5159 |
PyObject *v = Array_item_lock_held(myself, cur); |
| 5160 |
if (v == NULL) { |
| 5161 |
Py_DECREF(np); |
| 5162 |
return NULL; |
| 5163 |
} |
| 5164 |
PyList_SET_ITEM(np, i, v); |
| 5165 |
} |
| 5166 |
return np; |
| 5167 |
} |
| 5168 |
else { |
| 5169 |
PyErr_SetString(PyExc_TypeError, |
| 5170 |
"indices must be integers"); |
| 5171 |
return NULL; |
| 5172 |
} |
| 5173 |
} |
| 5174 |
|
| 5175 |
|
| 5176 |
static PyObject * |
| 5177 |
Array_subscript(PyObject *myself, PyObject *item) |
| 5178 |
{ |
| 5179 |
PyObject *result; |
| 5180 |
Py_BEGIN_CRITICAL_SECTION(myself); |
| 5181 |
result = Array_subscript_lock_held(myself, item); |
| 5182 |
Py_END_CRITICAL_SECTION(); |
| 5183 |
return result; |
| 5184 |
} |
| 5185 |
|
| 5186 |
static int |
| 5187 |
Array_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value) |
| 5188 |
{ |
| 5189 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself); |
| 5190 |
|
| 5191 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5192 |
Py_ssize_t size, offset; |
| 5193 |
char *ptr; |
| 5194 |
|
| 5195 |
if (value == NULL) { |
| 5196 |
PyErr_SetString(PyExc_TypeError, |
| 5197 |
"Array does not support item deletion"); |
| 5198 |
return -1; |
| 5199 |
} |
| 5200 |
|
| 5201 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5202 |
StgInfo *stginfo; |
| 5203 |
if (PyStgInfo_FromObject(st, myself, &stginfo) < 0) { |
| 5204 |
return -1; |
| 5205 |
} |
| 5206 |
assert(stginfo); /* Cannot be NULL for array object instances */ |
| 5207 |
|
| 5208 |
if (index < 0 || index >= stginfo->length) { |
| 5209 |
PyErr_SetString(PyExc_IndexError, |
| 5210 |
"invalid index"); |
| 5211 |
return -1; |
| 5212 |
} |
| 5213 |
size = stginfo->size / stginfo->length; |
| 5214 |
offset = index * size; |
| 5215 |
ptr = self->b_ptr + offset; |
| 5216 |
|
| 5217 |
return PyCData_set(st, myself, stginfo->proto, stginfo->setfunc, value, |
| 5218 |
index, size, ptr); |
| 5219 |
} |
| 5220 |
|
| 5221 |
static int |
| 5222 |
Array_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value) |
| 5223 |
{ |
| 5224 |
int result; |
| 5225 |
Py_BEGIN_CRITICAL_SECTION(myself); |
| 5226 |
result = Array_ass_item_lock_held(myself, index, value); |
| 5227 |
Py_END_CRITICAL_SECTION(); |
| 5228 |
return result; |
| 5229 |
} |
| 5230 |
|
| 5231 |
|
| 5232 |
static int |
| 5233 |
Array_ass_subscript_lock_held(PyObject *myself, PyObject *item, PyObject *value) |
| 5234 |
{ |
| 5235 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5236 |
|
| 5237 |
if (value == NULL) { |
| 5238 |
PyErr_SetString(PyExc_TypeError, |
| 5239 |
"Array does not support item deletion"); |
| 5240 |
return -1; |
| 5241 |
} |
| 5242 |
|
| 5243 |
if (PyIndex_Check(item)) { |
| 5244 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 5245 |
|
| 5246 |
if (i == -1 && PyErr_Occurred()) |
| 5247 |
return -1; |
| 5248 |
if (i < 0) |
| 5249 |
i += self->b_length; |
| 5250 |
return Array_ass_item_lock_held(myself, i, value); |
| 5251 |
} |
| 5252 |
else if (PySlice_Check(item)) { |
| 5253 |
Py_ssize_t start, stop, step, slicelen, otherlen, i; |
| 5254 |
size_t cur; |
| 5255 |
|
| 5256 |
if (PySlice_Unpack(item, &start, &stop, &step) < 0) { |
| 5257 |
return -1; |
| 5258 |
} |
| 5259 |
slicelen = PySlice_AdjustIndices(self->b_length, &start, &stop, step); |
| 5260 |
if ((step < 0 && start < stop) || |
| 5261 |
(step > 0 && start > stop)) |
| 5262 |
stop = start; |
| 5263 |
|
| 5264 |
otherlen = PySequence_Length(value); |
| 5265 |
if (otherlen != slicelen) { |
| 5266 |
PyErr_SetString(PyExc_ValueError, |
| 5267 |
"Can only assign sequence of same size"); |
| 5268 |
return -1; |
| 5269 |
} |
| 5270 |
for (cur = start, i = 0; i < otherlen; cur += step, i++) { |
| 5271 |
PyObject *item = PySequence_GetItem(value, i); |
| 5272 |
int result; |
| 5273 |
if (item == NULL) |
| 5274 |
return -1; |
| 5275 |
result = Array_ass_item_lock_held(myself, cur, item); |
| 5276 |
Py_DECREF(item); |
| 5277 |
if (result == -1) |
| 5278 |
return -1; |
| 5279 |
} |
| 5280 |
return 0; |
| 5281 |
} |
| 5282 |
else { |
| 5283 |
PyErr_SetString(PyExc_TypeError, |
| 5284 |
"indices must be integer"); |
| 5285 |
return -1; |
| 5286 |
} |
| 5287 |
} |
| 5288 |
|
| 5289 |
static int |
| 5290 |
Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value) |
| 5291 |
{ |
| 5292 |
int result; |
| 5293 |
Py_BEGIN_CRITICAL_SECTION(myself); |
| 5294 |
result = Array_ass_subscript_lock_held(myself, item, value); |
| 5295 |
Py_END_CRITICAL_SECTION(); |
| 5296 |
return result; |
| 5297 |
} |
| 5298 |
|
| 5299 |
|
| 5300 |
static Py_ssize_t |
| 5301 |
Array_length(PyObject *myself) |
| 5302 |
{ |
| 5303 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5304 |
return self->b_length; |
| 5305 |
} |
| 5306 |
|
| 5307 |
static PyMethodDef Array_methods[] = { |
| 5308 |
{"__class_getitem__", Py_GenericAlias, |
| 5309 |
METH_O|METH_CLASS, PyDoc_STR("Arrays are generic over the type of their elements")}, |
| 5310 |
{ NULL, NULL } |
| 5311 |
}; |
| 5312 |
|
| 5313 |
PyDoc_STRVAR(array_doc, |
| 5314 |
"Abstract base class for arrays.\n" |
| 5315 |
"\n" |
| 5316 |
"The recommended way to create concrete array types is by multiplying any\n" |
| 5317 |
"ctypes data type with a non-negative integer. Alternatively, you can subclass\n" |
| 5318 |
"this type and define _length_ and _type_ class variables. Array elements can\n" |
| 5319 |
"be read and written using standard subscript and slice accesses for slice\n" |
| 5320 |
"reads, the resulting object is not itself an Array." |
| 5321 |
); |
| 5322 |
|
| 5323 |
static PyType_Slot pycarray_slots[] = { |
| 5324 |
{Py_tp_doc, (char*)array_doc}, |
| 5325 |
{Py_tp_methods, Array_methods}, |
| 5326 |
{Py_tp_init, Array_init}, |
| 5327 |
{Py_tp_new, GenericPyCData_new}, |
| 5328 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 5329 |
{Py_sq_length, Array_length}, |
| 5330 |
{Py_sq_item, Array_item}, |
| 5331 |
{Py_sq_ass_item, Array_ass_item}, |
| 5332 |
{Py_mp_length, Array_length}, |
| 5333 |
{Py_mp_subscript, Array_subscript}, |
| 5334 |
{Py_mp_ass_subscript, Array_ass_subscript}, |
| 5335 |
{0, NULL}, |
| 5336 |
}; |
| 5337 |
|
| 5338 |
static PyType_Spec pycarray_spec = { |
| 5339 |
.name = "_ctypes.Array", |
| 5340 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5341 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 5342 |
.slots = pycarray_slots, |
| 5343 |
}; |
| 5344 |
|
| 5345 |
PyObject * |
| 5346 |
PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length) |
| 5347 |
{ |
| 5348 |
PyObject *key; |
| 5349 |
char name[256]; |
| 5350 |
PyObject *len; |
| 5351 |
|
| 5352 |
assert(st->array_cache != NULL); |
| 5353 |
len = PyLong_FromSsize_t(length); |
| 5354 |
if (len == NULL) |
| 5355 |
return NULL; |
| 5356 |
key = PyTuple_Pack(2, itemtype, len); |
| 5357 |
Py_DECREF(len); |
| 5358 |
if (!key) |
| 5359 |
return NULL; |
| 5360 |
|
| 5361 |
PyObject *result; |
| 5362 |
if (_PyDict_GetItemProxy(st->array_cache, key, &result) != 0) { |
| 5363 |
// found or error |
| 5364 |
Py_DECREF(key); |
| 5365 |
return result; |
| 5366 |
} |
| 5367 |
// not found |
| 5368 |
if (!PyType_Check(itemtype)) { |
| 5369 |
PyErr_SetString(PyExc_TypeError, |
| 5370 |
"Expected a type object"); |
| 5371 |
Py_DECREF(key); |
| 5372 |
return NULL; |
| 5373 |
} |
| 5374 |
#ifdef MS_WIN64 |
| 5375 |
sprintf(name, "%.200s_Array_%Id", |
| 5376 |
((PyTypeObject *)itemtype)->tp_name, length); |
| 5377 |
#else |
| 5378 |
sprintf(name, "%.200s_Array_%ld", |
| 5379 |
((PyTypeObject *)itemtype)->tp_name, (long)length); |
| 5380 |
#endif |
| 5381 |
result = PyObject_CallFunction((PyObject *)st->PyCArrayType_Type, |
| 5382 |
"s(O){s:n,s:O}", |
| 5383 |
name, |
| 5384 |
st->PyCArray_Type, |
| 5385 |
"_length_", |
| 5386 |
length, |
| 5387 |
"_type_", |
| 5388 |
itemtype |
| 5389 |
); |
| 5390 |
if (result == NULL) { |
| 5391 |
Py_DECREF(key); |
| 5392 |
return NULL; |
| 5393 |
} |
| 5394 |
if (PyDict_SetItemProxy(st, st->array_cache, key, result) < 0) { |
| 5395 |
Py_DECREF(key); |
| 5396 |
Py_DECREF(result); |
| 5397 |
return NULL; |
| 5398 |
} |
| 5399 |
Py_DECREF(key); |
| 5400 |
return result; |
| 5401 |
} |
| 5402 |
|
| 5403 |
|
| 5404 |
/******************************************************************/ |
| 5405 |
/* |
| 5406 |
Simple_Type |
| 5407 |
*/ |
| 5408 |
|
| 5409 |
/*[clinic input] |
| 5410 |
class _ctypes.Simple "CDataObject *" "clinic_state()->Simple_Type" |
| 5411 |
[clinic start generated code]*/ |
| 5412 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e0493451fecf8cd4]*/ |
| 5413 |
|
| 5414 |
/*[clinic input] |
| 5415 |
@critical_section |
| 5416 |
@setter |
| 5417 |
_ctypes.Simple.value |
| 5418 |
[clinic start generated code]*/ |
| 5419 |
|
| 5420 |
static int |
| 5421 |
_ctypes_Simple_value_set_impl(CDataObject *self, PyObject *value) |
| 5422 |
/*[clinic end generated code: output=f267186118939863 input=977af9dc9e71e857]*/ |
| 5423 |
{ |
| 5424 |
PyObject *result; |
| 5425 |
|
| 5426 |
if (value == NULL) { |
| 5427 |
PyErr_SetString(PyExc_TypeError, |
| 5428 |
"can't delete attribute"); |
| 5429 |
return -1; |
| 5430 |
} |
| 5431 |
|
| 5432 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5433 |
StgInfo *info; |
| 5434 |
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { |
| 5435 |
return -1; |
| 5436 |
} |
| 5437 |
assert(info); /* Cannot be NULL for CDataObject instances */ |
| 5438 |
assert(info->setfunc); |
| 5439 |
|
| 5440 |
result = info->setfunc(self->b_ptr, value, info->size); |
| 5441 |
if (!result) |
| 5442 |
return -1; |
| 5443 |
|
| 5444 |
/* consumes the refcount the setfunc returns */ |
| 5445 |
return KeepRef(self, 0, result); |
| 5446 |
} |
| 5447 |
|
| 5448 |
|
| 5449 |
static int |
| 5450 |
Simple_init(PyObject *self, PyObject *args, PyObject *kw) |
| 5451 |
{ |
| 5452 |
PyObject *value = NULL; |
| 5453 |
if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value)) |
| 5454 |
return -1; |
| 5455 |
if (value) |
| 5456 |
return _ctypes_Simple_value_set(self, value, NULL); |
| 5457 |
return 0; |
| 5458 |
} |
| 5459 |
|
| 5460 |
|
| 5461 |
/*[clinic input] |
| 5462 |
@critical_section |
| 5463 |
@getter |
| 5464 |
_ctypes.Simple.value |
| 5465 |
[clinic start generated code]*/ |
| 5466 |
|
| 5467 |
static PyObject * |
| 5468 |
_ctypes_Simple_value_get_impl(CDataObject *self) |
| 5469 |
/*[clinic end generated code: output=ce5a26570830a243 input=3ed3f735cec89282]*/ |
| 5470 |
{ |
| 5471 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5472 |
StgInfo *info; |
| 5473 |
if (PyStgInfo_FromObject(st, (PyObject *)self, &info) < 0) { |
| 5474 |
return NULL; |
| 5475 |
} |
| 5476 |
assert(info); /* Cannot be NULL for CDataObject instances */ |
| 5477 |
assert(info->getfunc); |
| 5478 |
PyObject *res; |
| 5479 |
res = info->getfunc(self->b_ptr, self->b_size); |
| 5480 |
return res; |
| 5481 |
} |
| 5482 |
|
| 5483 |
static PyGetSetDef Simple_getsets[] = { |
| 5484 |
_CTYPES_SIMPLE_VALUE_GETSETDEF |
| 5485 |
{ NULL, NULL } |
| 5486 |
}; |
| 5487 |
|
| 5488 |
/*[clinic input] |
| 5489 |
_ctypes.Simple.__ctypes_from_outparam__ as Simple_from_outparm |
| 5490 |
|
| 5491 |
self: self |
| 5492 |
cls: defining_class |
| 5493 |
/ |
| 5494 |
[clinic start generated code]*/ |
| 5495 |
|
| 5496 |
static PyObject * |
| 5497 |
Simple_from_outparm_impl(PyObject *self, PyTypeObject *cls) |
| 5498 |
/*[clinic end generated code: output=6c61d90da8aa9b4f input=0f362803fb4629d5]*/ |
| 5499 |
{ |
| 5500 |
ctypes_state *st = get_module_state_by_class(cls); |
| 5501 |
if (_ctypes_simple_instance(st, (PyObject *)Py_TYPE(self))) { |
| 5502 |
return Py_NewRef(self); |
| 5503 |
} |
| 5504 |
/* call stginfo->getfunc */ |
| 5505 |
return _ctypes_Simple_value_get(self, NULL); |
| 5506 |
} |
| 5507 |
|
| 5508 |
static PyMethodDef Simple_methods[] = { |
| 5509 |
SIMPLE_FROM_OUTPARM_METHODDEF |
| 5510 |
{ NULL, NULL }, |
| 5511 |
}; |
| 5512 |
|
| 5513 |
static int |
| 5514 |
Simple_bool(PyObject *op) |
| 5515 |
{ |
| 5516 |
int cmp; |
| 5517 |
CDataObject *self = _CDataObject_CAST(op); |
| 5518 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5519 |
cmp = memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size); |
| 5520 |
Py_END_CRITICAL_SECTION(); |
| 5521 |
return cmp; |
| 5522 |
} |
| 5523 |
|
| 5524 |
/* "%s(%s)" % (self.__class__.__name__, self.value) */ |
| 5525 |
static PyObject * |
| 5526 |
Simple_repr(PyObject *self) |
| 5527 |
{ |
| 5528 |
PyObject *val, *result; |
| 5529 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5530 |
|
| 5531 |
if (Py_TYPE(self)->tp_base != st->Simple_Type) { |
| 5532 |
return PyUnicode_FromFormat("<%s object at %p>", |
| 5533 |
Py_TYPE(self)->tp_name, self); |
| 5534 |
} |
| 5535 |
|
| 5536 |
val = _ctypes_Simple_value_get(self, NULL); |
| 5537 |
if (val == NULL) |
| 5538 |
return NULL; |
| 5539 |
|
| 5540 |
result = PyUnicode_FromFormat("%s(%R)", |
| 5541 |
Py_TYPE(self)->tp_name, val); |
| 5542 |
Py_DECREF(val); |
| 5543 |
return result; |
| 5544 |
} |
| 5545 |
|
| 5546 |
static PyType_Slot pycsimple_slots[] = { |
| 5547 |
{Py_tp_repr, &Simple_repr}, |
| 5548 |
{Py_tp_doc, PyDoc_STR("XXX to be provided")}, |
| 5549 |
{Py_tp_methods, Simple_methods}, |
| 5550 |
{Py_tp_getset, Simple_getsets}, |
| 5551 |
{Py_tp_init, Simple_init}, |
| 5552 |
{Py_tp_new, GenericPyCData_new}, |
| 5553 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 5554 |
{Py_nb_bool, Simple_bool}, |
| 5555 |
{0, NULL}, |
| 5556 |
}; |
| 5557 |
|
| 5558 |
static PyType_Spec pycsimple_spec = { |
| 5559 |
.name = "_ctypes._SimpleCData", |
| 5560 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 5561 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 5562 |
.slots = pycsimple_slots, |
| 5563 |
}; |
| 5564 |
|
| 5565 |
|
| 5566 |
/******************************************************************/ |
| 5567 |
/* |
| 5568 |
PyCPointer_Type |
| 5569 |
*/ |
| 5570 |
static PyObject * |
| 5571 |
Pointer_item_lock_held(PyObject *myself, Py_ssize_t index) |
| 5572 |
{ |
| 5573 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5574 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 5575 |
Py_ssize_t size; |
| 5576 |
Py_ssize_t offset; |
| 5577 |
PyObject *proto; |
| 5578 |
void *deref = *(void **)self->b_ptr; |
| 5579 |
|
| 5580 |
if (deref == NULL) { |
| 5581 |
PyErr_SetString(PyExc_ValueError, |
| 5582 |
"NULL pointer access"); |
| 5583 |
return NULL; |
| 5584 |
} |
| 5585 |
|
| 5586 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(myself))); |
| 5587 |
StgInfo *stginfo; |
| 5588 |
if (PyStgInfo_FromObject(st, myself, &stginfo) < 0) { |
| 5589 |
return NULL; |
| 5590 |
} |
| 5591 |
assert(stginfo); /* Cannot be NULL for pointer object instances */ |
| 5592 |
|
| 5593 |
proto = stginfo->proto; |
| 5594 |
assert(proto); |
| 5595 |
|
| 5596 |
StgInfo *iteminfo; |
| 5597 |
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) { |
| 5598 |
return NULL; |
| 5599 |
} |
| 5600 |
assert(iteminfo); /* proto is the item type of the pointer, a ctypes |
| 5601 |
type, so this cannot be NULL */ |
| 5602 |
|
| 5603 |
size = iteminfo->size; |
| 5604 |
offset = index * iteminfo->size; |
| 5605 |
|
| 5606 |
return PyCData_get(st, proto, stginfo->getfunc, myself, |
| 5607 |
index, size, (char *)((char *)deref + offset)); |
| 5608 |
} |
| 5609 |
|
| 5610 |
static PyObject * |
| 5611 |
Pointer_item(PyObject *self, Py_ssize_t index) |
| 5612 |
{ |
| 5613 |
PyObject *res; |
| 5614 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5615 |
res = Pointer_item_lock_held(self, index); |
| 5616 |
Py_END_CRITICAL_SECTION(); |
| 5617 |
return res; |
| 5618 |
} |
| 5619 |
|
| 5620 |
static int |
| 5621 |
Pointer_ass_item_lock_held(PyObject *myself, Py_ssize_t index, PyObject *value) |
| 5622 |
{ |
| 5623 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5624 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 5625 |
Py_ssize_t size; |
| 5626 |
Py_ssize_t offset; |
| 5627 |
PyObject *proto; |
| 5628 |
|
| 5629 |
if (value == NULL) { |
| 5630 |
PyErr_SetString(PyExc_TypeError, |
| 5631 |
"Pointer does not support item deletion"); |
| 5632 |
return -1; |
| 5633 |
} |
| 5634 |
|
| 5635 |
void *deref = *(void **)self->b_ptr; |
| 5636 |
if (deref == NULL) { |
| 5637 |
PyErr_SetString(PyExc_ValueError, |
| 5638 |
"NULL pointer access"); |
| 5639 |
return -1; |
| 5640 |
} |
| 5641 |
|
| 5642 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(myself))); |
| 5643 |
StgInfo *stginfo; |
| 5644 |
if (PyStgInfo_FromObject(st, myself, &stginfo) < 0) { |
| 5645 |
return -1; |
| 5646 |
} |
| 5647 |
assert(stginfo); /* Cannot be NULL for pointer instances */ |
| 5648 |
|
| 5649 |
proto = stginfo->proto; |
| 5650 |
assert(proto); |
| 5651 |
|
| 5652 |
StgInfo *iteminfo; |
| 5653 |
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) { |
| 5654 |
return -1; |
| 5655 |
} |
| 5656 |
assert(iteminfo); /* Cannot be NULL because the itemtype of a pointer |
| 5657 |
is always a ctypes type */ |
| 5658 |
|
| 5659 |
size = iteminfo->size; |
| 5660 |
offset = index * iteminfo->size; |
| 5661 |
|
| 5662 |
return PyCData_set(st, myself, proto, stginfo->setfunc, value, |
| 5663 |
index, size, ((char *)deref + offset)); |
| 5664 |
} |
| 5665 |
|
| 5666 |
static int |
| 5667 |
Pointer_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) |
| 5668 |
{ |
| 5669 |
int res; |
| 5670 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5671 |
res = Pointer_ass_item_lock_held(self, index, value); |
| 5672 |
Py_END_CRITICAL_SECTION(); |
| 5673 |
return res; |
| 5674 |
} |
| 5675 |
|
| 5676 |
static PyObject * |
| 5677 |
Pointer_get_contents_lock_held(PyObject *self, void *closure) |
| 5678 |
{ |
| 5679 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(self); |
| 5680 |
void *deref = *(void **)_CDataObject_CAST(self)->b_ptr; |
| 5681 |
if (deref == NULL) { |
| 5682 |
PyErr_SetString(PyExc_ValueError, |
| 5683 |
"NULL pointer access"); |
| 5684 |
return NULL; |
| 5685 |
} |
| 5686 |
|
| 5687 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5688 |
StgInfo *stginfo; |
| 5689 |
if (PyStgInfo_FromObject(st, self, &stginfo) < 0) { |
| 5690 |
return NULL; |
| 5691 |
} |
| 5692 |
assert(stginfo); /* Cannot be NULL for pointer instances */ |
| 5693 |
|
| 5694 |
return PyCData_FromBaseObj(st, stginfo->proto, self, 0, deref); |
| 5695 |
} |
| 5696 |
|
| 5697 |
static PyObject * |
| 5698 |
Pointer_get_contents(PyObject *self, void *closure) |
| 5699 |
{ |
| 5700 |
PyObject *res; |
| 5701 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5702 |
res = Pointer_get_contents_lock_held(self, closure); |
| 5703 |
Py_END_CRITICAL_SECTION(); |
| 5704 |
return res; |
| 5705 |
} |
| 5706 |
|
| 5707 |
static int |
| 5708 |
Pointer_set_contents_lock_held(PyObject *op, PyObject *value, void *closure) |
| 5709 |
{ |
| 5710 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(op); |
| 5711 |
CDataObject *dst; |
| 5712 |
PyObject *keep; |
| 5713 |
CDataObject *self = _CDataObject_CAST(op); |
| 5714 |
|
| 5715 |
if (value == NULL) { |
| 5716 |
PyErr_SetString(PyExc_TypeError, |
| 5717 |
"Pointer does not support item deletion"); |
| 5718 |
return -1; |
| 5719 |
} |
| 5720 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); |
| 5721 |
StgInfo *stginfo; |
| 5722 |
if (PyStgInfo_FromObject(st, op, &stginfo) < 0) { |
| 5723 |
return -1; |
| 5724 |
} |
| 5725 |
assert(stginfo); /* Cannot be NULL for pointer instances */ |
| 5726 |
assert(stginfo->proto); |
| 5727 |
if (!CDataObject_Check(st, value)) { |
| 5728 |
int res = PyObject_IsInstance(value, stginfo->proto); |
| 5729 |
if (res == -1) |
| 5730 |
return -1; |
| 5731 |
if (!res) { |
| 5732 |
PyErr_Format(PyExc_TypeError, |
| 5733 |
"expected %s instead of %s", |
| 5734 |
((PyTypeObject *)(stginfo->proto))->tp_name, |
| 5735 |
Py_TYPE(value)->tp_name); |
| 5736 |
return -1; |
| 5737 |
} |
| 5738 |
} |
| 5739 |
|
| 5740 |
dst = (CDataObject *)value; |
| 5741 |
*((void **)self->b_ptr) = dst->b_ptr; |
| 5742 |
|
| 5743 |
/* |
| 5744 |
A Pointer instance must keep the value it points to alive. So, a |
| 5745 |
pointer instance has b_length set to 2 instead of 1, and we set |
| 5746 |
'value' itself as the second item of the b_objects list, additionally. |
| 5747 |
*/ |
| 5748 |
Py_INCREF(value); |
| 5749 |
if (-1 == KeepRef(self, 1, value)) |
| 5750 |
return -1; |
| 5751 |
|
| 5752 |
keep = GetKeepedObjects(dst); |
| 5753 |
if (keep == NULL) |
| 5754 |
return -1; |
| 5755 |
|
| 5756 |
Py_INCREF(keep); |
| 5757 |
return KeepRef(self, 0, keep); |
| 5758 |
} |
| 5759 |
|
| 5760 |
static int |
| 5761 |
Pointer_set_contents(PyObject *op, PyObject *value, void *closure) |
| 5762 |
{ |
| 5763 |
int res; |
| 5764 |
Py_BEGIN_CRITICAL_SECTION2(op, value); |
| 5765 |
res = Pointer_set_contents_lock_held(op, value, closure); |
| 5766 |
Py_END_CRITICAL_SECTION2(); |
| 5767 |
return res; |
| 5768 |
} |
| 5769 |
|
| 5770 |
static PyGetSetDef Pointer_getsets[] = { |
| 5771 |
{ "contents", Pointer_get_contents, Pointer_set_contents, |
| 5772 |
"the object this pointer points to (read-write)", NULL }, |
| 5773 |
{ NULL, NULL } |
| 5774 |
}; |
| 5775 |
|
| 5776 |
static int |
| 5777 |
Pointer_init(PyObject *self, PyObject *args, PyObject *kw) |
| 5778 |
{ |
| 5779 |
PyObject *value = NULL; |
| 5780 |
if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value)) |
| 5781 |
return -1; |
| 5782 |
if (value == NULL) |
| 5783 |
return 0; |
| 5784 |
return Pointer_set_contents(self, value, NULL); |
| 5785 |
} |
| 5786 |
|
| 5787 |
static PyObject * |
| 5788 |
Pointer_new(PyTypeObject *type, PyObject *args, PyObject *kw) |
| 5789 |
{ |
| 5790 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); |
| 5791 |
StgInfo *info; |
| 5792 |
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { |
| 5793 |
return NULL; |
| 5794 |
} |
| 5795 |
if (!info || !info->proto) { |
| 5796 |
PyErr_SetString(PyExc_TypeError, |
| 5797 |
"Cannot create instance: has no _type_"); |
| 5798 |
return NULL; |
| 5799 |
} |
| 5800 |
return generic_pycdata_new(st, type, args, kw); |
| 5801 |
} |
| 5802 |
|
| 5803 |
static int |
| 5804 |
copy_pointer_to_list_lock_held(PyObject *myself, PyObject *np, Py_ssize_t len, |
| 5805 |
Py_ssize_t start, Py_ssize_t step) |
| 5806 |
{ |
| 5807 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(myself); |
| 5808 |
Py_ssize_t i; |
| 5809 |
size_t cur; |
| 5810 |
for (cur = start, i = 0; i < len; cur += step, i++) { |
| 5811 |
PyObject *v = Pointer_item_lock_held(myself, cur); |
| 5812 |
if (!v) { |
| 5813 |
return -1; |
| 5814 |
} |
| 5815 |
PyList_SET_ITEM(np, i, v); |
| 5816 |
} |
| 5817 |
|
| 5818 |
return 0; |
| 5819 |
} |
| 5820 |
|
| 5821 |
static PyObject * |
| 5822 |
Pointer_subscript(PyObject *myself, PyObject *item) |
| 5823 |
{ |
| 5824 |
CDataObject *self = _CDataObject_CAST(myself); |
| 5825 |
if (PyIndex_Check(item)) { |
| 5826 |
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError); |
| 5827 |
if (i == -1 && PyErr_Occurred()) |
| 5828 |
return NULL; |
| 5829 |
return Pointer_item(myself, i); |
| 5830 |
} |
| 5831 |
else if (PySlice_Check(item)) { |
| 5832 |
PySliceObject *slice = (PySliceObject *)item; |
| 5833 |
Py_ssize_t start, stop, step; |
| 5834 |
PyObject *np; |
| 5835 |
PyObject *proto; |
| 5836 |
Py_ssize_t i, len; |
| 5837 |
size_t cur; |
| 5838 |
|
| 5839 |
/* Since pointers have no length, and we want to apply |
| 5840 |
different semantics to negative indices than normal |
| 5841 |
slicing, we have to dissect the slice object ourselves.*/ |
| 5842 |
if (slice->step == Py_None) { |
| 5843 |
step = 1; |
| 5844 |
} |
| 5845 |
else { |
| 5846 |
step = PyNumber_AsSsize_t(slice->step, |
| 5847 |
PyExc_ValueError); |
| 5848 |
if (step == -1 && PyErr_Occurred()) |
| 5849 |
return NULL; |
| 5850 |
if (step == 0) { |
| 5851 |
PyErr_SetString(PyExc_ValueError, |
| 5852 |
"slice step cannot be zero"); |
| 5853 |
return NULL; |
| 5854 |
} |
| 5855 |
} |
| 5856 |
if (slice->start == Py_None) { |
| 5857 |
if (step < 0) { |
| 5858 |
PyErr_SetString(PyExc_ValueError, |
| 5859 |
"slice start is required " |
| 5860 |
"for step < 0"); |
| 5861 |
return NULL; |
| 5862 |
} |
| 5863 |
start = 0; |
| 5864 |
} |
| 5865 |
else { |
| 5866 |
start = PyNumber_AsSsize_t(slice->start, |
| 5867 |
PyExc_ValueError); |
| 5868 |
if (start == -1 && PyErr_Occurred()) |
| 5869 |
return NULL; |
| 5870 |
} |
| 5871 |
if (slice->stop == Py_None) { |
| 5872 |
PyErr_SetString(PyExc_ValueError, |
| 5873 |
"slice stop is required"); |
| 5874 |
return NULL; |
| 5875 |
} |
| 5876 |
stop = PyNumber_AsSsize_t(slice->stop, |
| 5877 |
PyExc_ValueError); |
| 5878 |
if (stop == -1 && PyErr_Occurred()) |
| 5879 |
return NULL; |
| 5880 |
if ((step > 0 && start > stop) || |
| 5881 |
(step < 0 && start < stop)) |
| 5882 |
len = 0; |
| 5883 |
else if (step > 0) |
| 5884 |
len = (stop - start - 1) / step + 1; |
| 5885 |
else |
| 5886 |
len = (stop - start + 1) / step + 1; |
| 5887 |
|
| 5888 |
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(myself))); |
| 5889 |
StgInfo *stginfo; |
| 5890 |
if (PyStgInfo_FromObject(st, myself, &stginfo) < 0) { |
| 5891 |
return NULL; |
| 5892 |
} |
| 5893 |
assert(stginfo); /* Cannot be NULL for pointer instances */ |
| 5894 |
proto = stginfo->proto; |
| 5895 |
assert(proto); |
| 5896 |
StgInfo *iteminfo; |
| 5897 |
if (PyStgInfo_FromType(st, proto, &iteminfo) < 0) { |
| 5898 |
return NULL; |
| 5899 |
} |
| 5900 |
assert(iteminfo); |
| 5901 |
if (iteminfo->getfunc == _ctypes_get_fielddesc("c")->getfunc) { |
| 5902 |
char *dest; |
| 5903 |
|
| 5904 |
if (len <= 0) |
| 5905 |
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES); |
| 5906 |
if (step == 1) { |
| 5907 |
PyObject *res; |
| 5908 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5909 |
char *ptr = *(void **)self->b_ptr; |
| 5910 |
res = PyBytes_FromStringAndSize(ptr + start, |
| 5911 |
len); |
| 5912 |
Py_END_CRITICAL_SECTION(); |
| 5913 |
return res; |
| 5914 |
} |
| 5915 |
dest = (char *)PyMem_Malloc(len); |
| 5916 |
if (dest == NULL) |
| 5917 |
return PyErr_NoMemory(); |
| 5918 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5919 |
char *ptr = *(void **)self->b_ptr; |
| 5920 |
for (cur = start, i = 0; i < len; cur += step, i++) { |
| 5921 |
dest[i] = ptr[cur]; |
| 5922 |
} |
| 5923 |
Py_END_CRITICAL_SECTION(); |
| 5924 |
np = PyBytes_FromStringAndSize(dest, len); |
| 5925 |
PyMem_Free(dest); |
| 5926 |
return np; |
| 5927 |
} |
| 5928 |
if (iteminfo->getfunc == _ctypes_get_fielddesc("u")->getfunc) { |
| 5929 |
wchar_t *dest; |
| 5930 |
|
| 5931 |
if (len <= 0) |
| 5932 |
return Py_GetConstant(Py_CONSTANT_EMPTY_STR); |
| 5933 |
if (step == 1) { |
| 5934 |
PyObject *res; |
| 5935 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5936 |
wchar_t *ptr = *(wchar_t **)self->b_ptr; |
| 5937 |
res = PyUnicode_FromWideChar(ptr + start, |
| 5938 |
len); |
| 5939 |
Py_END_CRITICAL_SECTION(); |
| 5940 |
return res; |
| 5941 |
} |
| 5942 |
dest = PyMem_New(wchar_t, len); |
| 5943 |
if (dest == NULL) |
| 5944 |
return PyErr_NoMemory(); |
| 5945 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5946 |
wchar_t *ptr = *(wchar_t **)self->b_ptr; |
| 5947 |
for (cur = start, i = 0; i < len; cur += step, i++) { |
| 5948 |
dest[i] = ptr[cur]; |
| 5949 |
} |
| 5950 |
Py_END_CRITICAL_SECTION(); |
| 5951 |
np = PyUnicode_FromWideChar(dest, len); |
| 5952 |
PyMem_Free(dest); |
| 5953 |
return np; |
| 5954 |
} |
| 5955 |
|
| 5956 |
np = PyList_New(len); |
| 5957 |
if (np == NULL) |
| 5958 |
return NULL; |
| 5959 |
|
| 5960 |
int res; |
| 5961 |
Py_BEGIN_CRITICAL_SECTION(myself); |
| 5962 |
res = copy_pointer_to_list_lock_held(myself, np, len, start, step); |
| 5963 |
Py_END_CRITICAL_SECTION(); |
| 5964 |
if (res < 0) { |
| 5965 |
Py_DECREF(np); |
| 5966 |
return NULL; |
| 5967 |
} |
| 5968 |
|
| 5969 |
return np; |
| 5970 |
} |
| 5971 |
else { |
| 5972 |
PyErr_SetString(PyExc_TypeError, |
| 5973 |
"Pointer indices must be integer"); |
| 5974 |
return NULL; |
| 5975 |
} |
| 5976 |
} |
| 5977 |
|
| 5978 |
static int |
| 5979 |
Pointer_bool(PyObject *self) |
| 5980 |
{ |
| 5981 |
int res; |
| 5982 |
Py_BEGIN_CRITICAL_SECTION(self); |
| 5983 |
res = *(void **)_CDataObject_CAST(self)->b_ptr != NULL; |
| 5984 |
Py_END_CRITICAL_SECTION(); |
| 5985 |
return res; |
| 5986 |
} |
| 5987 |
|
| 5988 |
static PyType_Slot pycpointer_slots[] = { |
| 5989 |
{Py_tp_doc, (void *)PyDoc_STR("XXX to be provided")}, |
| 5990 |
{Py_tp_getset, Pointer_getsets}, |
| 5991 |
{Py_tp_init, Pointer_init}, |
| 5992 |
{Py_tp_new, Pointer_new}, |
| 5993 |
{Py_bf_getbuffer, PyCData_NewGetBuffer}, |
| 5994 |
{Py_nb_bool, Pointer_bool}, |
| 5995 |
{Py_mp_subscript, Pointer_subscript}, |
| 5996 |
{Py_sq_item, Pointer_item}, |
| 5997 |
{Py_sq_ass_item, Pointer_ass_item}, |
| 5998 |
{0, NULL}, |
| 5999 |
}; |
| 6000 |
|
| 6001 |
static PyType_Spec pycpointer_spec = { |
| 6002 |
.name = "_ctypes._Pointer", |
| 6003 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 6004 |
Py_TPFLAGS_IMMUTABLETYPE), |
| 6005 |
.slots = pycpointer_slots, |
| 6006 |
}; |
| 6007 |
|
| 6008 |
/******************************************************************/ |
| 6009 |
/* |
| 6010 |
* Module initialization. |
| 6011 |
*/ |
| 6012 |
|
| 6013 |
PyDoc_STRVAR(_ctypes__doc__, |
| 6014 |
"Create and manipulate C compatible data types in Python."); |
| 6015 |
|
| 6016 |
#ifdef MS_WIN32 |
| 6017 |
|
| 6018 |
PyDoc_STRVAR(comerror_doc, "Raised when a COM method call failed."); |
| 6019 |
|
| 6020 |
int |
| 6021 |
comerror_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 6022 |
{ |
| 6023 |
PyObject *hresult, *text, *details; |
| 6024 |
PyObject *a; |
| 6025 |
int status; |
| 6026 |
|
| 6027 |
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds)) |
| 6028 |
return -1; |
| 6029 |
|
| 6030 |
if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details)) |
| 6031 |
return -1; |
| 6032 |
|
| 6033 |
a = PySequence_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 6034 |
if (!a) |
| 6035 |
return -1; |
| 6036 |
status = PyObject_SetAttrString(self, "args", a); |
| 6037 |
Py_DECREF(a); |
| 6038 |
if (status < 0) |
| 6039 |
return -1; |
| 6040 |
|
| 6041 |
if (PyObject_SetAttrString(self, "hresult", hresult) < 0) |
| 6042 |
return -1; |
| 6043 |
|
| 6044 |
if (PyObject_SetAttrString(self, "text", text) < 0) |
| 6045 |
return -1; |
| 6046 |
|
| 6047 |
if (PyObject_SetAttrString(self, "details", details) < 0) |
| 6048 |
return -1; |
| 6049 |
|
| 6050 |
Py_INCREF(args); |
| 6051 |
Py_SETREF(((PyBaseExceptionObject *)self)->args, args); |
| 6052 |
|
| 6053 |
return 0; |
| 6054 |
} |
| 6055 |
|
| 6056 |
static int |
| 6057 |
comerror_clear(PyObject *self) |
| 6058 |
{ |
| 6059 |
return ((PyTypeObject *)PyExc_BaseException)->tp_clear(self); |
| 6060 |
} |
| 6061 |
|
| 6062 |
static int |
| 6063 |
comerror_traverse(PyObject *self, visitproc visit, void *arg) |
| 6064 |
{ |
| 6065 |
Py_VISIT(Py_TYPE(self)); |
| 6066 |
return ((PyTypeObject *)PyExc_BaseException)->tp_traverse(self, visit, arg); |
| 6067 |
} |
| 6068 |
|
| 6069 |
static void |
| 6070 |
comerror_dealloc(PyObject *self) |
| 6071 |
{ |
| 6072 |
PyTypeObject *tp = Py_TYPE(self); |
| 6073 |
PyObject_GC_UnTrack(self); |
| 6074 |
(void)comerror_clear(self); |
| 6075 |
tp->tp_free(self); |
| 6076 |
Py_DECREF(tp); |
| 6077 |
} |
| 6078 |
|
| 6079 |
static PyType_Slot comerror_slots[] = { |
| 6080 |
{Py_tp_doc, (void *)PyDoc_STR(comerror_doc)}, |
| 6081 |
{Py_tp_init, comerror_init}, |
| 6082 |
{Py_tp_traverse, comerror_traverse}, |
| 6083 |
{Py_tp_dealloc, comerror_dealloc}, |
| 6084 |
{Py_tp_clear, comerror_clear}, |
| 6085 |
{0, NULL}, |
| 6086 |
}; |
| 6087 |
|
| 6088 |
static PyType_Spec comerror_spec = { |
| 6089 |
.name = "_ctypes.COMError", |
| 6090 |
.basicsize = sizeof(PyBaseExceptionObject), |
| 6091 |
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | |
| 6092 |
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE), |
| 6093 |
.slots = comerror_slots, |
| 6094 |
}; |
| 6095 |
|
| 6096 |
#endif // MS_WIN32 |
| 6097 |
|
| 6098 |
static PyObject * |
| 6099 |
string_at(const char *ptr, int size) |
| 6100 |
{ |
| 6101 |
if (PySys_Audit("ctypes.string_at", "ni", (Py_ssize_t)ptr, size) < 0) { |
| 6102 |
return NULL; |
| 6103 |
} |
| 6104 |
if (size == -1) |
| 6105 |
return PyBytes_FromStringAndSize(ptr, strlen(ptr)); |
| 6106 |
return PyBytes_FromStringAndSize(ptr, size); |
| 6107 |
} |
| 6108 |
|
| 6109 |
static int |
| 6110 |
cast_check_pointertype(ctypes_state *st, PyObject *arg) |
| 6111 |
{ |
| 6112 |
if (PyCPointerTypeObject_Check(st, arg)) { |
| 6113 |
return 1; |
| 6114 |
} |
| 6115 |
if (PyCFuncPtrTypeObject_Check(st, arg)) { |
| 6116 |
return 1; |
| 6117 |
} |
| 6118 |
StgInfo *info; |
| 6119 |
if (PyStgInfo_FromType(st, arg, &info) < 0) { |
| 6120 |
return 0; |
| 6121 |
} |
| 6122 |
if (info != NULL && info->proto != NULL) { |
| 6123 |
if (PyUnicode_Check(info->proto) |
| 6124 |
&& (strchr("sPzUZXO", PyUnicode_AsUTF8(info->proto)[0]))) { |
| 6125 |
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */ |
| 6126 |
return 1; |
| 6127 |
} |
| 6128 |
} |
| 6129 |
PyErr_Format(PyExc_TypeError, |
| 6130 |
"cast() argument 2 must be a pointer type, not %s", |
| 6131 |
PyType_Check(arg) |
| 6132 |
? ((PyTypeObject *)arg)->tp_name |
| 6133 |
: Py_TYPE(arg)->tp_name); |
| 6134 |
return 0; |
| 6135 |
} |
| 6136 |
|
| 6137 |
static PyObject * |
| 6138 |
cast_lock_held(void *ptr, PyObject *src, PyObject *ctype) |
| 6139 |
{ |
| 6140 |
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(src); |
| 6141 |
PyObject *mod = PyType_GetModuleByDef(Py_TYPE(ctype), &_ctypesmodule); |
| 6142 |
if (!mod) { |
| 6143 |
PyErr_SetString(PyExc_TypeError, |
| 6144 |
"cast() argument 2 must be a pointer type"); |
| 6145 |
return NULL; |
| 6146 |
} |
| 6147 |
ctypes_state *st = get_module_state(mod); |
| 6148 |
|
| 6149 |
CDataObject *result; |
| 6150 |
if (cast_check_pointertype(st, ctype) == 0) { |
| 6151 |
return NULL; |
| 6152 |
} |
| 6153 |
result = (CDataObject *)_PyObject_CallNoArgs(ctype); |
| 6154 |
if (result == NULL) |
| 6155 |
return NULL; |
| 6156 |
|
| 6157 |
/* |
| 6158 |
The casted objects '_objects' member: |
| 6159 |
|
| 6160 |
It must certainly contain the source objects one. |
| 6161 |
It must contain the source object itself. |
| 6162 |
*/ |
| 6163 |
if (CDataObject_Check(st, src)) { |
| 6164 |
CDataObject *obj = (CDataObject *)src; |
| 6165 |
CDataObject *container; |
| 6166 |
|
| 6167 |
/* PyCData_GetContainer will initialize src.b_objects, we need |
| 6168 |
this so it can be shared */ |
| 6169 |
container = PyCData_GetContainer(obj); |
| 6170 |
if (container == NULL) |
| 6171 |
goto failed; |
| 6172 |
|
| 6173 |
/* But we need a dictionary! */ |
| 6174 |
if (obj->b_objects == Py_None) { |
| 6175 |
Py_DECREF(Py_None); |
| 6176 |
obj->b_objects = PyDict_New(); |
| 6177 |
if (obj->b_objects == NULL) |
| 6178 |
goto failed; |
| 6179 |
} |
| 6180 |
result->b_objects = Py_XNewRef(obj->b_objects); |
| 6181 |
if (result->b_objects && PyDict_CheckExact(result->b_objects)) { |
| 6182 |
PyObject *index; |
| 6183 |
int rc; |
| 6184 |
index = PyLong_FromVoidPtr((void *)src); |
| 6185 |
if (index == NULL) |
| 6186 |
goto failed; |
| 6187 |
rc = PyDict_SetItem(result->b_objects, index, src); |
| 6188 |
Py_DECREF(index); |
| 6189 |
if (rc == -1) |
| 6190 |
goto failed; |
| 6191 |
} |
| 6192 |
} |
| 6193 |
/* Should we assert that result is a pointer type? */ |
| 6194 |
memcpy(result->b_ptr, &ptr, sizeof(void *)); |
| 6195 |
return (PyObject *)result; |
| 6196 |
|
| 6197 |
failed: |
| 6198 |
Py_DECREF(result); |
| 6199 |
return NULL; |
| 6200 |
} |
| 6201 |
|
| 6202 |
static PyObject * |
| 6203 |
cast(void *ptr, PyObject *src, PyObject *ctype) |
| 6204 |
{ |
| 6205 |
PyObject *res; |
| 6206 |
Py_BEGIN_CRITICAL_SECTION(src); |
| 6207 |
res = cast_lock_held(ptr, src, ctype); |
| 6208 |
Py_END_CRITICAL_SECTION(); |
| 6209 |
return res; |
| 6210 |
} |
| 6211 |
|
| 6212 |
static PyObject * |
| 6213 |
wstring_at(const wchar_t *ptr, int size) |
| 6214 |
{ |
| 6215 |
Py_ssize_t ssize = size; |
| 6216 |
if (PySys_Audit("ctypes.wstring_at", "nn", (Py_ssize_t)ptr, ssize) < 0) { |
| 6217 |
return NULL; |
| 6218 |
} |
| 6219 |
if (ssize == -1) |
| 6220 |
ssize = wcslen(ptr); |
| 6221 |
return PyUnicode_FromWideChar(ptr, ssize); |
| 6222 |
} |
| 6223 |
|
| 6224 |
static PyObject * |
| 6225 |
memoryview_at(void *ptr, Py_ssize_t size, int readonly) |
| 6226 |
{ |
| 6227 |
if (PySys_Audit("ctypes.memoryview_at", "nni", |
| 6228 |
(Py_ssize_t)ptr, size, readonly) < 0) { |
| 6229 |
return NULL; |
| 6230 |
} |
| 6231 |
if (size < 0) { |
| 6232 |
PyErr_Format(PyExc_ValueError, |
| 6233 |
"memoryview_at: size is negative (or overflowed): %zd", |
| 6234 |
size); |
| 6235 |
return NULL; |
| 6236 |
} |
| 6237 |
return PyMemoryView_FromMemory(ptr, size, |
| 6238 |
readonly ? PyBUF_READ : PyBUF_WRITE); |
| 6239 |
} |
| 6240 |
|
| 6241 |
static int |
| 6242 |
_ctypes_add_types(PyObject *mod) |
| 6243 |
{ |
| 6244 |
#define TYPE_READY(TYPE) \ |
| 6245 |
if (PyType_Ready(TYPE) < 0) { \ |
| 6246 |
return -1; \ |
| 6247 |
} |
| 6248 |
#define CREATE_TYPE(TP, SPEC, META, BASE) do { \ |
| 6249 |
PyObject *type = PyType_FromMetaclass(META, mod, SPEC, \ |
| 6250 |
(PyObject *)BASE); \ |
| 6251 |
if (type == NULL) { \ |
| 6252 |
return -1; \ |
| 6253 |
} \ |
| 6254 |
TP = (PyTypeObject *)type; \ |
| 6255 |
} while (0) |
| 6256 |
|
| 6257 |
#define MOD_ADD_TYPE(TP, SPEC, META, BASE) do { \ |
| 6258 |
CREATE_TYPE(TP, SPEC, META, BASE); \ |
| 6259 |
if (PyModule_AddType(mod, (PyTypeObject *)(TP)) < 0) { \ |
| 6260 |
return -1; \ |
| 6261 |
} \ |
| 6262 |
} while (0) |
| 6263 |
|
| 6264 |
ctypes_state *st = get_module_state(mod); |
| 6265 |
|
| 6266 |
/* Note: |
| 6267 |
ob_type is the metatype (the 'type'), defaults to PyType_Type, |
| 6268 |
tp_base is the base type, defaults to 'object' aka PyBaseObject_Type. |
| 6269 |
*/ |
| 6270 |
CREATE_TYPE(st->PyCArg_Type, &carg_spec, NULL, NULL); |
| 6271 |
CREATE_TYPE(st->PyCThunk_Type, &cthunk_spec, NULL, NULL); |
| 6272 |
CREATE_TYPE(st->PyCData_Type, &pycdata_spec, NULL, NULL); |
| 6273 |
|
| 6274 |
// Common Metaclass |
| 6275 |
CREATE_TYPE(st->PyCType_Type, &pyctype_type_spec, |
| 6276 |
NULL, &PyType_Type); |
| 6277 |
|
| 6278 |
/************************************************* |
| 6279 |
* |
| 6280 |
* Metaclasses |
| 6281 |
*/ |
| 6282 |
CREATE_TYPE(st->PyCStructType_Type, &pycstruct_type_spec, |
| 6283 |
NULL, st->PyCType_Type); |
| 6284 |
CREATE_TYPE(st->UnionType_Type, &union_type_spec, |
| 6285 |
NULL, st->PyCType_Type); |
| 6286 |
CREATE_TYPE(st->PyCPointerType_Type, &pycpointer_type_spec, |
| 6287 |
NULL, st->PyCType_Type); |
| 6288 |
CREATE_TYPE(st->PyCArrayType_Type, &pycarray_type_spec, |
| 6289 |
NULL, st->PyCType_Type); |
| 6290 |
CREATE_TYPE(st->PyCSimpleType_Type, &pycsimple_type_spec, |
| 6291 |
NULL, st->PyCType_Type); |
| 6292 |
CREATE_TYPE(st->PyCFuncPtrType_Type, &pycfuncptr_type_spec, |
| 6293 |
NULL, st->PyCType_Type); |
| 6294 |
|
| 6295 |
/************************************************* |
| 6296 |
* |
| 6297 |
* Classes using a custom metaclass |
| 6298 |
*/ |
| 6299 |
|
| 6300 |
MOD_ADD_TYPE(st->Struct_Type, &pycstruct_spec, |
| 6301 |
st->PyCStructType_Type, st->PyCData_Type); |
| 6302 |
MOD_ADD_TYPE(st->Union_Type, &pycunion_spec, |
| 6303 |
st->UnionType_Type, st->PyCData_Type); |
| 6304 |
MOD_ADD_TYPE(st->PyCPointer_Type, &pycpointer_spec, |
| 6305 |
st->PyCPointerType_Type, st->PyCData_Type); |
| 6306 |
MOD_ADD_TYPE(st->PyCArray_Type, &pycarray_spec, |
| 6307 |
st->PyCArrayType_Type, st->PyCData_Type); |
| 6308 |
MOD_ADD_TYPE(st->Simple_Type, &pycsimple_spec, |
| 6309 |
st->PyCSimpleType_Type, st->PyCData_Type); |
| 6310 |
MOD_ADD_TYPE(st->PyCFuncPtr_Type, &pycfuncptr_spec, |
| 6311 |
st->PyCFuncPtrType_Type, st->PyCData_Type); |
| 6312 |
|
| 6313 |
/************************************************* |
| 6314 |
* |
| 6315 |
* Simple classes |
| 6316 |
*/ |
| 6317 |
|
| 6318 |
MOD_ADD_TYPE(st->PyCField_Type, &cfield_spec, NULL, NULL); |
| 6319 |
|
| 6320 |
/************************************************* |
| 6321 |
* |
| 6322 |
* Other stuff |
| 6323 |
*/ |
| 6324 |
|
| 6325 |
CREATE_TYPE(st->DictRemover_Type, &dictremover_spec, NULL, NULL); |
| 6326 |
CREATE_TYPE(st->StructParam_Type, &structparam_spec, NULL, NULL); |
| 6327 |
|
| 6328 |
#ifdef MS_WIN32 |
| 6329 |
CREATE_TYPE(st->PyComError_Type, &comerror_spec, NULL, PyExc_Exception); |
| 6330 |
#endif |
| 6331 |
|
| 6332 |
#undef TYPE_READY |
| 6333 |
#undef MOD_ADD_TYPE |
| 6334 |
#undef CREATE_TYPE |
| 6335 |
return 0; |
| 6336 |
} |
| 6337 |
|
| 6338 |
|
| 6339 |
static int |
| 6340 |
_ctypes_add_objects(PyObject *mod) |
| 6341 |
{ |
| 6342 |
#define MOD_ADD(name, expr) \ |
| 6343 |
do { \ |
| 6344 |
if (PyModule_Add(mod, name, (expr)) < 0) { \ |
| 6345 |
return -1; \ |
| 6346 |
} \ |
| 6347 |
} while (0) |
| 6348 |
|
| 6349 |
ctypes_state *st = get_module_state(mod); |
| 6350 |
|
| 6351 |
#ifdef MS_WIN32 |
| 6352 |
MOD_ADD("COMError", Py_NewRef(st->PyComError_Type)); |
| 6353 |
MOD_ADD("FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT)); |
| 6354 |
MOD_ADD("FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL)); |
| 6355 |
#endif |
| 6356 |
MOD_ADD("FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL)); |
| 6357 |
MOD_ADD("FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO)); |
| 6358 |
MOD_ADD("FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR)); |
| 6359 |
MOD_ADD("FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI)); |
| 6360 |
MOD_ADD("__version__", PyUnicode_FromString("1.1.0")); |
| 6361 |
|
| 6362 |
MOD_ADD("_memmove_addr", PyLong_FromVoidPtr(memmove)); |
| 6363 |
MOD_ADD("_memset_addr", PyLong_FromVoidPtr(memset)); |
| 6364 |
MOD_ADD("_string_at_addr", PyLong_FromVoidPtr(string_at)); |
| 6365 |
MOD_ADD("_cast_addr", PyLong_FromVoidPtr(cast)); |
| 6366 |
MOD_ADD("_wstring_at_addr", PyLong_FromVoidPtr(wstring_at)); |
| 6367 |
MOD_ADD("_memoryview_at_addr", PyLong_FromVoidPtr(memoryview_at)); |
| 6368 |
|
| 6369 |
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */ |
| 6370 |
#if !HAVE_DECL_RTLD_LOCAL |
| 6371 |
# define RTLD_LOCAL 0 |
| 6372 |
#endif |
| 6373 |
|
| 6374 |
/* If RTLD_GLOBAL is not defined (cygwin), set it to the same value as |
| 6375 |
RTLD_LOCAL. */ |
| 6376 |
#if !HAVE_DECL_RTLD_GLOBAL |
| 6377 |
# define RTLD_GLOBAL RTLD_LOCAL |
| 6378 |
#endif |
| 6379 |
MOD_ADD("RTLD_LOCAL", PyLong_FromLong(RTLD_LOCAL)); |
| 6380 |
MOD_ADD("RTLD_GLOBAL", PyLong_FromLong(RTLD_GLOBAL)); |
| 6381 |
MOD_ADD("CTYPES_MAX_ARGCOUNT", PyLong_FromLong(CTYPES_MAX_ARGCOUNT)); |
| 6382 |
MOD_ADD("ArgumentError", Py_NewRef(st->PyExc_ArgError)); |
| 6383 |
MOD_ADD("SIZEOF_TIME_T", PyLong_FromSsize_t(SIZEOF_TIME_T)); |
| 6384 |
return 0; |
| 6385 |
#undef MOD_ADD |
| 6386 |
} |
| 6387 |
|
| 6388 |
|
| 6389 |
static int |
| 6390 |
_ctypes_mod_exec(PyObject *mod) |
| 6391 |
{ |
| 6392 |
// See https://github.com/python/cpython/issues/128485 |
| 6393 |
// This allocates some memory and then frees it to ensure that the |
| 6394 |
// the dlmalloc allocator initializes itself to avoid data races |
| 6395 |
// in free-threading. |
| 6396 |
void *codeloc = NULL; |
| 6397 |
void *ptr = Py_ffi_closure_alloc(sizeof(void *), &codeloc); |
| 6398 |
if (ptr == NULL) { |
| 6399 |
PyErr_NoMemory(); |
| 6400 |
return -1; |
| 6401 |
} |
| 6402 |
Py_ffi_closure_free(ptr); |
| 6403 |
|
| 6404 |
_ctypes_init_fielddesc(); |
| 6405 |
|
| 6406 |
ctypes_state *st = get_module_state(mod); |
| 6407 |
st->_unpickle = PyObject_GetAttrString(mod, "_unpickle"); |
| 6408 |
if (st->_unpickle == NULL) { |
| 6409 |
return -1; |
| 6410 |
} |
| 6411 |
|
| 6412 |
st->PyExc_ArgError = PyErr_NewException("ctypes.ArgumentError", NULL, NULL); |
| 6413 |
if (!st->PyExc_ArgError) { |
| 6414 |
return -1; |
| 6415 |
} |
| 6416 |
|
| 6417 |
st->array_cache = PyDict_New(); |
| 6418 |
if (st->array_cache == NULL) { |
| 6419 |
return -1; |
| 6420 |
} |
| 6421 |
|
| 6422 |
#ifdef WORDS_BIGENDIAN |
| 6423 |
st->swapped_suffix = PyUnicode_InternFromString("_le"); |
| 6424 |
#else |
| 6425 |
st->swapped_suffix = PyUnicode_InternFromString("_be"); |
| 6426 |
#endif |
| 6427 |
if (st->swapped_suffix == NULL) { |
| 6428 |
return -1; |
| 6429 |
} |
| 6430 |
|
| 6431 |
st->error_object_name = PyUnicode_InternFromString("ctypes.error_object"); |
| 6432 |
if (st->error_object_name == NULL) { |
| 6433 |
return -1; |
| 6434 |
} |
| 6435 |
|
| 6436 |
if (_ctypes_add_types(mod) < 0) { |
| 6437 |
return -1; |
| 6438 |
} |
| 6439 |
|
| 6440 |
if (_ctypes_add_objects(mod) < 0) { |
| 6441 |
return -1; |
| 6442 |
} |
| 6443 |
return 0; |
| 6444 |
} |
| 6445 |
|
| 6446 |
|
| 6447 |
static int |
| 6448 |
module_traverse(PyObject *module, visitproc visit, void *arg) { |
| 6449 |
ctypes_state *st = get_module_state(module); |
| 6450 |
Py_VISIT(st->_unpickle); |
| 6451 |
Py_VISIT(st->array_cache); |
| 6452 |
Py_VISIT(st->error_object_name); |
| 6453 |
Py_VISIT(st->PyExc_ArgError); |
| 6454 |
Py_VISIT(st->swapped_suffix); |
| 6455 |
|
| 6456 |
Py_VISIT(st->DictRemover_Type); |
| 6457 |
Py_VISIT(st->PyCArg_Type); |
| 6458 |
Py_VISIT(st->PyCField_Type); |
| 6459 |
Py_VISIT(st->PyCThunk_Type); |
| 6460 |
Py_VISIT(st->StructParam_Type); |
| 6461 |
Py_VISIT(st->PyCStructType_Type); |
| 6462 |
Py_VISIT(st->UnionType_Type); |
| 6463 |
Py_VISIT(st->PyCPointerType_Type); |
| 6464 |
Py_VISIT(st->PyCArrayType_Type); |
| 6465 |
Py_VISIT(st->PyCSimpleType_Type); |
| 6466 |
Py_VISIT(st->PyCFuncPtrType_Type); |
| 6467 |
Py_VISIT(st->PyCData_Type); |
| 6468 |
Py_VISIT(st->Struct_Type); |
| 6469 |
Py_VISIT(st->Union_Type); |
| 6470 |
Py_VISIT(st->PyCArray_Type); |
| 6471 |
Py_VISIT(st->Simple_Type); |
| 6472 |
Py_VISIT(st->PyCPointer_Type); |
| 6473 |
Py_VISIT(st->PyCFuncPtr_Type); |
| 6474 |
#ifdef MS_WIN32 |
| 6475 |
Py_VISIT(st->PyComError_Type); |
| 6476 |
#endif |
| 6477 |
Py_VISIT(st->PyCType_Type); |
| 6478 |
return 0; |
| 6479 |
} |
| 6480 |
|
| 6481 |
static int |
| 6482 |
module_clear(PyObject *module) { |
| 6483 |
ctypes_state *st = get_module_state(module); |
| 6484 |
Py_CLEAR(st->_unpickle); |
| 6485 |
Py_CLEAR(st->array_cache); |
| 6486 |
Py_CLEAR(st->error_object_name); |
| 6487 |
Py_CLEAR(st->PyExc_ArgError); |
| 6488 |
Py_CLEAR(st->swapped_suffix); |
| 6489 |
|
| 6490 |
Py_CLEAR(st->DictRemover_Type); |
| 6491 |
Py_CLEAR(st->PyCArg_Type); |
| 6492 |
Py_CLEAR(st->PyCField_Type); |
| 6493 |
Py_CLEAR(st->PyCThunk_Type); |
| 6494 |
Py_CLEAR(st->StructParam_Type); |
| 6495 |
Py_CLEAR(st->PyCStructType_Type); |
| 6496 |
Py_CLEAR(st->UnionType_Type); |
| 6497 |
Py_CLEAR(st->PyCPointerType_Type); |
| 6498 |
Py_CLEAR(st->PyCArrayType_Type); |
| 6499 |
Py_CLEAR(st->PyCSimpleType_Type); |
| 6500 |
Py_CLEAR(st->PyCFuncPtrType_Type); |
| 6501 |
Py_CLEAR(st->PyCData_Type); |
| 6502 |
Py_CLEAR(st->Struct_Type); |
| 6503 |
Py_CLEAR(st->Union_Type); |
| 6504 |
Py_CLEAR(st->PyCArray_Type); |
| 6505 |
Py_CLEAR(st->Simple_Type); |
| 6506 |
Py_CLEAR(st->PyCPointer_Type); |
| 6507 |
Py_CLEAR(st->PyCFuncPtr_Type); |
| 6508 |
#ifdef MS_WIN32 |
| 6509 |
Py_CLEAR(st->PyComError_Type); |
| 6510 |
#endif |
| 6511 |
Py_CLEAR(st->PyCType_Type); |
| 6512 |
return 0; |
| 6513 |
} |
| 6514 |
|
| 6515 |
static void |
| 6516 |
module_free(void *module) |
| 6517 |
{ |
| 6518 |
(void)module_clear((PyObject *)module); |
| 6519 |
} |
| 6520 |
|
| 6521 |
static PyModuleDef_Slot module_slots[] = { |
| 6522 |
{Py_mod_exec, _ctypes_mod_exec}, |
| 6523 |
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
| 6524 |
{Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
| 6525 |
{0, NULL} |
| 6526 |
}; |
| 6527 |
|
| 6528 |
struct PyModuleDef _ctypesmodule = { |
| 6529 |
PyModuleDef_HEAD_INIT, |
| 6530 |
.m_name = "_ctypes", |
| 6531 |
.m_doc = _ctypes__doc__, |
| 6532 |
.m_size = sizeof(ctypes_state), |
| 6533 |
.m_methods = _ctypes_module_methods, |
| 6534 |
.m_slots = module_slots, |
| 6535 |
.m_traverse = module_traverse, |
| 6536 |
.m_clear = module_clear, |
| 6537 |
.m_free = module_free, |
| 6538 |
}; |
| 6539 |
|
| 6540 |
PyMODINIT_FUNC |
| 6541 |
PyInit__ctypes(void) |
| 6542 |
{ |
| 6543 |
return PyModuleDef_Init(&_ctypesmodule); |
| 6544 |
} |