| 1 |
#define PY_SSIZE_T_CLEAN |
| 2 |
#include <Python.h> |
| 3 |
#include "structmember.h" |
| 4 |
#include "misc_thread_common.h" |
| 5 |
#define CFFI_VERSION "2.1.1" |
| 6 |
|
| 7 |
#ifdef MS_WIN32 |
| 8 |
#include <windows.h> |
| 9 |
#include "misc_win32.h" |
| 10 |
#else |
| 11 |
#include <stddef.h> |
| 12 |
#include <stdint.h> |
| 13 |
#include <dlfcn.h> |
| 14 |
#include <errno.h> |
| 15 |
#include <ffi.h> |
| 16 |
#include <sys/mman.h> |
| 17 |
#include "misc_thread_posix.h" |
| 18 |
#endif |
| 19 |
|
| 20 |
/* this block of #ifs should be kept exactly identical between |
| 21 |
c/_cffi_backend.c, cffi/vengine_cpy.py, cffi/vengine_gen.py */ |
| 22 |
#if defined(_MSC_VER) |
| 23 |
# include <malloc.h> /* for alloca() */ |
| 24 |
# if _MSC_VER < 1600 /* MSVC < 2010 */ |
| 25 |
typedef __int8 int8_t; |
| 26 |
typedef __int16 int16_t; |
| 27 |
typedef __int32 int32_t; |
| 28 |
typedef __int64 int64_t; |
| 29 |
typedef unsigned __int8 uint8_t; |
| 30 |
typedef unsigned __int16 uint16_t; |
| 31 |
typedef unsigned __int32 uint32_t; |
| 32 |
typedef unsigned __int64 uint64_t; |
| 33 |
typedef __int8 int_least8_t; |
| 34 |
typedef __int16 int_least16_t; |
| 35 |
typedef __int32 int_least32_t; |
| 36 |
typedef __int64 int_least64_t; |
| 37 |
typedef unsigned __int8 uint_least8_t; |
| 38 |
typedef unsigned __int16 uint_least16_t; |
| 39 |
typedef unsigned __int32 uint_least32_t; |
| 40 |
typedef unsigned __int64 uint_least64_t; |
| 41 |
typedef __int8 int_fast8_t; |
| 42 |
typedef __int16 int_fast16_t; |
| 43 |
typedef __int32 int_fast32_t; |
| 44 |
typedef __int64 int_fast64_t; |
| 45 |
typedef unsigned __int8 uint_fast8_t; |
| 46 |
typedef unsigned __int16 uint_fast16_t; |
| 47 |
typedef unsigned __int32 uint_fast32_t; |
| 48 |
typedef unsigned __int64 uint_fast64_t; |
| 49 |
typedef __int64 intmax_t; |
| 50 |
typedef unsigned __int64 uintmax_t; |
| 51 |
# else |
| 52 |
# include <stdint.h> |
| 53 |
# endif |
| 54 |
# if _MSC_VER < 1800 /* MSVC < 2013 */ |
| 55 |
typedef unsigned char _Bool; |
| 56 |
# endif |
| 57 |
# define _cffi_float_complex_t _Fcomplex /* include <complex.h> for it */ |
| 58 |
# define _cffi_double_complex_t _Dcomplex /* include <complex.h> for it */ |
| 59 |
#else |
| 60 |
# include <stdint.h> |
| 61 |
# if (defined (__SVR4) && defined (__sun)) || defined(_AIX) || defined(__hpux) |
| 62 |
# include <alloca.h> |
| 63 |
# endif |
| 64 |
# define _cffi_float_complex_t float _Complex |
| 65 |
# define _cffi_double_complex_t double _Complex |
| 66 |
#endif |
| 67 |
|
| 68 |
/* Convert from closure pointer to function pointer. */ |
| 69 |
#if defined(__hppa__) && !defined(__LP64__) |
| 70 |
#define CFFI_CLOSURE_TO_FNPTR(type, f) ((type)((unsigned int)(f) | 2)) |
| 71 |
#else |
| 72 |
#define CFFI_CLOSURE_TO_FNPTR(type, f) ((type)(f)) |
| 73 |
#endif |
| 74 |
|
| 75 |
|
| 76 |
/* Define the following macro ONLY if you trust libffi's version of |
| 77 |
* ffi_closure_alloc() more than the code in malloc_closure.h. |
| 78 |
* IMPORTANT: DO NOT ENABLE THIS ON LINUX, unless you understand exactly |
| 79 |
* why I recommend against it and decide that you trust it more than my |
| 80 |
* analysis below. |
| 81 |
* |
| 82 |
* There are two versions of this code: one inside libffi itself, and |
| 83 |
* one inside malloc_closure.h here. Both should be fine as long as the |
| 84 |
* Linux distribution does _not_ enable extra security features. If it |
| 85 |
* does, then the code in malloc_closure.h will cleanly crash because |
| 86 |
* there is no reasonable way to obtain a read-write-execute memory |
| 87 |
* page. On the other hand, the code in libffi will appear to |
| 88 |
* work---but will actually randomly crash after a fork() if the child |
| 89 |
* does not immediately call exec(). This second crash is of the kind |
| 90 |
* that can be turned into an attack vector by a motivated attacker. |
| 91 |
* So, _enabling_ extra security features _opens_ an attack vector. |
| 92 |
* That sounds like a horribly bad idea to me, and is the reason for why |
| 93 |
* I prefer CFFI crashing cleanly. |
| 94 |
* |
| 95 |
* Currently, we use libffi's ffi_closure_alloc() on NetBSD. It is |
| 96 |
* known that on the NetBSD kernel, a different strategy is used which |
| 97 |
* should not be open to the fork() bug. |
| 98 |
* |
| 99 |
* This is also used on macOS, provided we are executing on macOS 10.15 or |
| 100 |
* above. It's a mess because it needs runtime checks in that case. |
| 101 |
*/ |
| 102 |
#ifdef __NetBSD__ |
| 103 |
|
| 104 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC 1 |
| 105 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1 |
| 106 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 1 |
| 107 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1 |
| 108 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR 0 |
| 109 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0 |
| 110 |
|
| 111 |
#elif defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE) |
| 112 |
|
| 113 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) |
| 114 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1 |
| 115 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) |
| 116 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1 |
| 117 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR __builtin_available(macos 10.15, ios 13, watchos 6, tvos 13, *) |
| 118 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 1 |
| 119 |
|
| 120 |
#elif defined(__EMSCRIPTEN__) |
| 121 |
|
| 122 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC 1 |
| 123 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 1 |
| 124 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 1 |
| 125 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 1 |
| 126 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR 1 |
| 127 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 1 |
| 128 |
|
| 129 |
#else |
| 130 |
|
| 131 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC 0 |
| 132 |
# define CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE 0 |
| 133 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC 0 |
| 134 |
# define CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE 0 |
| 135 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR 0 |
| 136 |
# define CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE 0 |
| 137 |
|
| 138 |
#endif |
| 139 |
|
| 140 |
/* always includes this, even if it turns out not to be used on NetBSD |
| 141 |
because calls are behind "if (0)" */ |
| 142 |
#include "malloc_closure.h" |
| 143 |
|
| 144 |
|
| 145 |
#if PY_VERSION_HEX < 0x030900a4 |
| 146 |
# define Py_SET_REFCNT(obj, val) (Py_REFCNT(obj) = (val)) |
| 147 |
#endif |
| 148 |
|
| 149 |
#if PY_VERSION_HEX <= 0x030d00a1 |
| 150 |
static int PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result) |
| 151 |
{ |
| 152 |
PyObject *obj = PyDict_GetItemWithError(mp, key); |
| 153 |
Py_XINCREF(obj); |
| 154 |
*result = obj; |
| 155 |
if (obj == NULL) { |
| 156 |
if (PyErr_Occurred()) { |
| 157 |
return -1; |
| 158 |
} |
| 159 |
return 0; |
| 160 |
} |
| 161 |
return 1; |
| 162 |
} |
| 163 |
#endif |
| 164 |
|
| 165 |
#if PY_VERSION_HEX < 0x030d00a1 |
| 166 |
static int PyWeakref_GetRef(PyObject *ref, PyObject **pobj) |
| 167 |
{ |
| 168 |
PyObject *obj = PyWeakref_GetObject(ref); |
| 169 |
if (obj == NULL) { |
| 170 |
*pobj = NULL; |
| 171 |
return -1; |
| 172 |
} |
| 173 |
if (obj == Py_None) { |
| 174 |
*pobj = NULL; |
| 175 |
return 0; |
| 176 |
} |
| 177 |
Py_INCREF(obj); |
| 178 |
*pobj = obj; |
| 179 |
return 1; |
| 180 |
} |
| 181 |
#endif |
| 182 |
|
| 183 |
|
| 184 |
#ifdef Py_GIL_DISABLED |
| 185 |
# define LOCK_UNIQUE_CACHE() PyMutex_Lock(&unique_cache_lock) |
| 186 |
# define UNLOCK_UNIQUE_CACHE() PyMutex_Unlock(&unique_cache_lock) |
| 187 |
#else |
| 188 |
# define LOCK_UNIQUE_CACHE() ((void)0) |
| 189 |
# define UNLOCK_UNIQUE_CACHE() ((void)0) |
| 190 |
#endif |
| 191 |
|
| 192 |
/************************************************************/ |
| 193 |
|
| 194 |
/* base type flag: exactly one of the following: */ |
| 195 |
#define CT_PRIMITIVE_SIGNED 0x001 /* signed integer */ |
| 196 |
#define CT_PRIMITIVE_UNSIGNED 0x002 /* unsigned integer */ |
| 197 |
#define CT_PRIMITIVE_CHAR 0x004 /* char, wchar_t, charN_t */ |
| 198 |
#define CT_PRIMITIVE_FLOAT 0x008 /* float, double, long double */ |
| 199 |
#define CT_POINTER 0x010 /* pointer, excluding ptr-to-func */ |
| 200 |
#define CT_ARRAY 0x020 /* array */ |
| 201 |
#define CT_STRUCT 0x040 /* struct */ |
| 202 |
#define CT_UNION 0x080 /* union */ |
| 203 |
#define CT_FUNCTIONPTR 0x100 /* pointer to function */ |
| 204 |
#define CT_VOID 0x200 /* void */ |
| 205 |
#define CT_PRIMITIVE_COMPLEX 0x400 /* _cffi_float/double_complex_t */ |
| 206 |
|
| 207 |
/* other flags that may also be set in addition to the base flag: */ |
| 208 |
#define CT_IS_VOIDCHAR_PTR 0x00001000 |
| 209 |
#define CT_PRIMITIVE_FITS_LONG 0x00002000 |
| 210 |
#define CT_IS_OPAQUE 0x00004000 |
| 211 |
#define CT_IS_ENUM 0x00008000 |
| 212 |
#define CT_IS_PTR_TO_OWNED 0x00010000 /* only owned if CDataOwning_Type */ |
| 213 |
/* unused 0x00020000 */ |
| 214 |
#define CT_IS_LONGDOUBLE 0x00040000 |
| 215 |
#define CT_IS_BOOL 0x00080000 |
| 216 |
#define CT_IS_FILE 0x00100000 |
| 217 |
#define CT_IS_VOID_PTR 0x00200000 |
| 218 |
/* unused 0x00400000 */ |
| 219 |
/* unused 0x00800000 */ |
| 220 |
/* unused 0x01000000 */ |
| 221 |
/* unused 0x02000000 */ |
| 222 |
#define CT_IS_SIGNED_WCHAR 0x04000000 |
| 223 |
#define CT_PRIMITIVE_ANY (CT_PRIMITIVE_SIGNED | \ |
| 224 |
CT_PRIMITIVE_UNSIGNED | \ |
| 225 |
CT_PRIMITIVE_CHAR | \ |
| 226 |
CT_PRIMITIVE_FLOAT | \ |
| 227 |
CT_PRIMITIVE_COMPLEX) |
| 228 |
|
| 229 |
/* flags that are mutable at runtime, stored separately in ct_flags_mut to avoid |
| 230 |
races with ct_flags |
| 231 |
|
| 232 |
these are read and set without using atomic operations because CFFI_LOCK is held |
| 233 |
while mutating ct_flags_mut |
| 234 |
*/ |
| 235 |
#define CT_CUSTOM_FIELD_POS 0x00000001 |
| 236 |
#define CT_WITH_VAR_ARRAY 0x00000002 /* with open-ended array, anywhere */ |
| 237 |
#define CT_WITH_PACKED_CHANGE 0x00000004 |
| 238 |
|
| 239 |
typedef struct _ctypedescr { |
| 240 |
PyObject_VAR_HEAD |
| 241 |
|
| 242 |
struct _ctypedescr *ct_itemdescr; /* ptrs and arrays: the item type */ |
| 243 |
PyObject *ct_stuff; /* structs: dict of the fields |
| 244 |
arrays: ctypedescr of the ptr type |
| 245 |
function: tuple(abi, ctres, ctargs..) |
| 246 |
enum: pair {"name":x},{x:"name"} |
| 247 |
ptrs: lazily, ctypedescr of array */ |
| 248 |
void *ct_extra; /* structs: first field (not a ref!) |
| 249 |
function types: cif_description |
| 250 |
primitives: prebuilt "cif" object */ |
| 251 |
|
| 252 |
PyObject *ct_weakreflist; /* weakref support */ |
| 253 |
|
| 254 |
PyObject *ct_unique_key; /* key in unique_cache (a string, but not |
| 255 |
human-readable) */ |
| 256 |
|
| 257 |
Py_ssize_t ct_size; /* size of instances, or -1 if unknown */ |
| 258 |
Py_ssize_t ct_length; /* length of arrays, or -1 if unknown; |
| 259 |
or alignment of primitive and struct types; |
| 260 |
always -1 for pointers */ |
| 261 |
int ct_flags; /* Immutable CT_xxx flags */ |
| 262 |
int ct_flags_mut; /* Mutable CT_xxx flags */ |
| 263 |
uint8_t ct_under_construction; |
| 264 |
uint8_t ct_lazy_field_list; |
| 265 |
uint8_t ct_unrealized_struct_or_union; |
| 266 |
int ct_name_position; /* index in ct_name of where to put a var name */ |
| 267 |
char ct_name[1]; /* string, e.g. "int *" for pointers to ints */ |
| 268 |
} CTypeDescrObject; |
| 269 |
|
| 270 |
typedef struct { |
| 271 |
PyObject_HEAD |
| 272 |
CTypeDescrObject *c_type; |
| 273 |
char *c_data; |
| 274 |
PyObject *c_weakreflist; |
| 275 |
} CDataObject; |
| 276 |
|
| 277 |
typedef struct cfieldobject_s { |
| 278 |
PyObject_HEAD |
| 279 |
CTypeDescrObject *cf_type; |
| 280 |
Py_ssize_t cf_offset; |
| 281 |
short cf_bitshift; /* >= 0: bitshift; or BS_REGULAR or BS_EMPTY_ARRAY */ |
| 282 |
short cf_bitsize; |
| 283 |
unsigned char cf_flags; /* BF_... */ |
| 284 |
struct cfieldobject_s *cf_next; |
| 285 |
} CFieldObject; |
| 286 |
#define BS_REGULAR (-1) /* a regular field, not with bitshift */ |
| 287 |
#define BS_EMPTY_ARRAY (-2) /* a field declared 'type[0]' or 'type[]' */ |
| 288 |
#define BF_IGNORE_IN_CTOR 0x01 /* union field not in the first place */ |
| 289 |
|
| 290 |
static PyTypeObject CTypeDescr_Type; |
| 291 |
static PyTypeObject CField_Type; |
| 292 |
static PyTypeObject CData_Type; |
| 293 |
static PyTypeObject CDataOwning_Type; |
| 294 |
static PyTypeObject CDataOwningGC_Type; |
| 295 |
static PyTypeObject CDataFromBuf_Type; |
| 296 |
static PyTypeObject CDataGCP_Type; |
| 297 |
|
| 298 |
#define CTypeDescr_Check(ob) (Py_TYPE(ob) == &CTypeDescr_Type) |
| 299 |
#define CData_Check(ob) (Py_TYPE(ob) == &CData_Type || \ |
| 300 |
Py_TYPE(ob) == &CDataOwning_Type || \ |
| 301 |
Py_TYPE(ob) == &CDataOwningGC_Type || \ |
| 302 |
Py_TYPE(ob) == &CDataFromBuf_Type || \ |
| 303 |
Py_TYPE(ob) == &CDataGCP_Type) |
| 304 |
#define CDataOwn_Check(ob) (Py_TYPE(ob) == &CDataOwning_Type || \ |
| 305 |
Py_TYPE(ob) == &CDataOwningGC_Type) |
| 306 |
|
| 307 |
typedef union { |
| 308 |
unsigned char m_char; |
| 309 |
unsigned short m_short; |
| 310 |
unsigned int m_int; |
| 311 |
unsigned long m_long; |
| 312 |
unsigned long long m_longlong; |
| 313 |
float m_float; |
| 314 |
double m_double; |
| 315 |
long double m_longdouble; |
| 316 |
} union_alignment; |
| 317 |
|
| 318 |
typedef struct { |
| 319 |
CDataObject head; |
| 320 |
union_alignment alignment; |
| 321 |
} CDataObject_casted_primitive; |
| 322 |
|
| 323 |
typedef struct { |
| 324 |
CDataObject head; |
| 325 |
union_alignment alignment; |
| 326 |
} CDataObject_own_nolength; |
| 327 |
|
| 328 |
typedef struct { |
| 329 |
CDataObject head; |
| 330 |
Py_ssize_t length; |
| 331 |
union_alignment alignment; |
| 332 |
} CDataObject_own_length; |
| 333 |
|
| 334 |
typedef struct { |
| 335 |
CDataObject head; |
| 336 |
PyObject *structobj; /* for ffi.new_handle() or ffi.new("struct *") */ |
| 337 |
} CDataObject_own_structptr; |
| 338 |
|
| 339 |
typedef struct { |
| 340 |
CDataObject head; |
| 341 |
Py_ssize_t length; /* same as CDataObject_own_length up to here */ |
| 342 |
Py_buffer *bufferview; |
| 343 |
} CDataObject_frombuf; |
| 344 |
|
| 345 |
typedef struct { |
| 346 |
CDataObject head; |
| 347 |
Py_ssize_t length; /* same as CDataObject_own_length up to here */ |
| 348 |
PyObject *origobj; |
| 349 |
PyObject *destructor; |
| 350 |
} CDataObject_gcp; |
| 351 |
|
| 352 |
typedef struct { |
| 353 |
CDataObject head; |
| 354 |
ffi_closure *closure; |
| 355 |
} CDataObject_closure; |
| 356 |
|
| 357 |
typedef struct { |
| 358 |
ffi_cif cif; |
| 359 |
/* the following information is used when doing the call: |
| 360 |
- a buffer of size 'exchange_size' is malloced |
| 361 |
- the arguments are converted from Python objects to raw data |
| 362 |
- the i'th raw data is stored at 'buffer + exchange_offset_arg[1+i]' |
| 363 |
- the call is done |
| 364 |
- the result is read back from 'buffer + exchange_offset_arg[0]' */ |
| 365 |
Py_ssize_t exchange_size; |
| 366 |
Py_ssize_t exchange_offset_arg[1]; |
| 367 |
} cif_description_t; |
| 368 |
|
| 369 |
#define ADD_WRAPAROUND(x, y) ((Py_ssize_t)(((size_t)(x)) + ((size_t)(y)))) |
| 370 |
#define MUL_WRAPAROUND(x, y) ((Py_ssize_t)(((size_t)(x)) * ((size_t)(y)))) |
| 371 |
|
| 372 |
|
| 373 |
/* whenever running Python code, the errno is saved in this thread-local |
| 374 |
variable */ |
| 375 |
#ifndef MS_WIN32 |
| 376 |
# include "misc_thread_posix.h" |
| 377 |
#endif |
| 378 |
|
| 379 |
#include "minibuffer.h" |
| 380 |
#include "file_emulator.h" |
| 381 |
|
| 382 |
#ifdef PyUnicode_KIND /* Python >= 3.3 */ |
| 383 |
# include "wchar_helper_3.h" |
| 384 |
#else |
| 385 |
# include "wchar_helper.h" |
| 386 |
#endif |
| 387 |
|
| 388 |
#include "../cffi/_cffi_errors.h" |
| 389 |
|
| 390 |
typedef struct _cffi_allocator_s { |
| 391 |
PyObject *ca_alloc, *ca_free; |
| 392 |
int ca_dont_clear; |
| 393 |
} cffi_allocator_t; |
| 394 |
static const cffi_allocator_t default_allocator = { NULL, NULL, 0 }; |
| 395 |
static PyObject *FFIError; |
| 396 |
|
| 397 |
#ifdef Py_GIL_DISABLED |
| 398 |
static PyMutex unique_cache_lock; |
| 399 |
#endif |
| 400 |
static PyObject *unique_cache; |
| 401 |
|
| 402 |
/************************************************************/ |
| 403 |
|
| 404 |
static CTypeDescrObject * |
| 405 |
ctypedescr_new(int name_size) |
| 406 |
{ |
| 407 |
CTypeDescrObject *ct = PyObject_GC_NewVar(CTypeDescrObject, |
| 408 |
&CTypeDescr_Type, |
| 409 |
name_size); |
| 410 |
if (ct == NULL) |
| 411 |
return NULL; |
| 412 |
|
| 413 |
ct->ct_itemdescr = NULL; |
| 414 |
ct->ct_stuff = NULL; |
| 415 |
ct->ct_weakreflist = NULL; |
| 416 |
ct->ct_unique_key = NULL; |
| 417 |
ct->ct_lazy_field_list = 0; |
| 418 |
ct->ct_under_construction = 0; |
| 419 |
ct->ct_unrealized_struct_or_union = 0; |
| 420 |
ct->ct_flags_mut = 0; |
| 421 |
PyObject_GC_Track(ct); |
| 422 |
return ct; |
| 423 |
} |
| 424 |
|
| 425 |
static CTypeDescrObject * |
| 426 |
ctypedescr_new_on_top(CTypeDescrObject *ct_base, const char *extra_text, |
| 427 |
int extra_position) |
| 428 |
{ |
| 429 |
int base_name_len = strlen(ct_base->ct_name); |
| 430 |
int extra_name_len = strlen(extra_text); |
| 431 |
CTypeDescrObject *ct = ctypedescr_new(base_name_len + extra_name_len + 1); |
| 432 |
char *p; |
| 433 |
if (ct == NULL) |
| 434 |
return NULL; |
| 435 |
|
| 436 |
Py_INCREF(ct_base); |
| 437 |
ct->ct_itemdescr = ct_base; |
| 438 |
ct->ct_name_position = ct_base->ct_name_position + extra_position; |
| 439 |
|
| 440 |
p = ct->ct_name; |
| 441 |
memcpy(p, ct_base->ct_name, ct_base->ct_name_position); |
| 442 |
p += ct_base->ct_name_position; |
| 443 |
memcpy(p, extra_text, extra_name_len); |
| 444 |
p += extra_name_len; |
| 445 |
memcpy(p, ct_base->ct_name + ct_base->ct_name_position, |
| 446 |
base_name_len - ct_base->ct_name_position + 1); |
| 447 |
|
| 448 |
return ct; |
| 449 |
} |
| 450 |
|
| 451 |
static PyObject * |
| 452 |
ctypedescr_repr(CTypeDescrObject *ct) |
| 453 |
{ |
| 454 |
return PyUnicode_FromFormat("<ctype '%s'>", ct->ct_name); |
| 455 |
} |
| 456 |
|
| 457 |
static void remove_dead_unique_reference(PyObject *unique_key); |
| 458 |
|
| 459 |
static void |
| 460 |
ctypedescr_dealloc(CTypeDescrObject *ct) |
| 461 |
{ |
| 462 |
PyObject_GC_UnTrack(ct); |
| 463 |
PyObject_ClearWeakRefs((PyObject *) ct); |
| 464 |
|
| 465 |
if (ct->ct_unique_key != NULL) { |
| 466 |
/* delete the weak reference from unique_cache */ |
| 467 |
remove_dead_unique_reference(ct->ct_unique_key); |
| 468 |
Py_DECREF(ct->ct_unique_key); |
| 469 |
} |
| 470 |
Py_XDECREF(ct->ct_itemdescr); |
| 471 |
Py_XDECREF(ct->ct_stuff); |
| 472 |
if (ct->ct_flags & CT_FUNCTIONPTR) |
| 473 |
PyObject_Free(ct->ct_extra); |
| 474 |
Py_TYPE(ct)->tp_free((PyObject *)ct); |
| 475 |
} |
| 476 |
|
| 477 |
static int |
| 478 |
ctypedescr_traverse(CTypeDescrObject *ct, visitproc visit, void *arg) |
| 479 |
{ |
| 480 |
Py_VISIT(ct->ct_itemdescr); |
| 481 |
Py_VISIT(ct->ct_stuff); |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
static int |
| 486 |
ctypedescr_clear(CTypeDescrObject *ct) |
| 487 |
{ |
| 488 |
Py_CLEAR(ct->ct_itemdescr); |
| 489 |
Py_CLEAR(ct->ct_stuff); |
| 490 |
return 0; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
static PyObject *nosuchattr(const char *attr) |
| 495 |
{ |
| 496 |
PyErr_SetString(PyExc_AttributeError, attr); |
| 497 |
return NULL; |
| 498 |
} |
| 499 |
|
| 500 |
static PyObject *ctypeget_kind(CTypeDescrObject *ct, void *context) |
| 501 |
{ |
| 502 |
char *result; |
| 503 |
if (ct->ct_flags & CT_PRIMITIVE_ANY) { |
| 504 |
if (ct->ct_flags & CT_IS_ENUM) |
| 505 |
result = "enum"; |
| 506 |
else |
| 507 |
result = "primitive"; |
| 508 |
} |
| 509 |
else if (ct->ct_flags & CT_POINTER) { |
| 510 |
result = "pointer"; |
| 511 |
} |
| 512 |
else if (ct->ct_flags & CT_ARRAY) { |
| 513 |
result = "array"; |
| 514 |
} |
| 515 |
else if (ct->ct_flags & CT_VOID) { |
| 516 |
result = "void"; |
| 517 |
} |
| 518 |
else if (ct->ct_flags & CT_STRUCT) { |
| 519 |
result = "struct"; |
| 520 |
} |
| 521 |
else if (ct->ct_flags & CT_UNION) { |
| 522 |
result = "union"; |
| 523 |
} |
| 524 |
else if (ct->ct_flags & CT_FUNCTIONPTR) { |
| 525 |
result = "function"; |
| 526 |
} |
| 527 |
else |
| 528 |
result = "?"; |
| 529 |
|
| 530 |
return PyUnicode_FromString(result); |
| 531 |
} |
| 532 |
|
| 533 |
static PyObject *ctypeget_cname(CTypeDescrObject *ct, void *context) |
| 534 |
{ |
| 535 |
return PyUnicode_FromString(ct->ct_name); |
| 536 |
} |
| 537 |
|
| 538 |
static PyObject *ctypeget_item(CTypeDescrObject *ct, void *context) |
| 539 |
{ |
| 540 |
if (ct->ct_flags & (CT_POINTER | CT_ARRAY)) { |
| 541 |
Py_INCREF(ct->ct_itemdescr); |
| 542 |
return (PyObject *)ct->ct_itemdescr; |
| 543 |
} |
| 544 |
return nosuchattr("item"); |
| 545 |
} |
| 546 |
|
| 547 |
static PyObject *ctypeget_length(CTypeDescrObject *ct, void *context) |
| 548 |
{ |
| 549 |
if (ct->ct_flags & CT_ARRAY) { |
| 550 |
if (ct->ct_length >= 0) { |
| 551 |
return PyLong_FromSsize_t(ct->ct_length); |
| 552 |
} |
| 553 |
else { |
| 554 |
Py_INCREF(Py_None); |
| 555 |
return Py_None; |
| 556 |
} |
| 557 |
} |
| 558 |
return nosuchattr("length"); |
| 559 |
} |
| 560 |
|
| 561 |
static PyObject * |
| 562 |
get_field_name(CTypeDescrObject *ct, CFieldObject *cf); /* forward */ |
| 563 |
|
| 564 |
static int do_realize_lazy_struct(CTypeDescrObject *ct); |
| 565 |
/* forward, implemented in realize_c_type.c */ |
| 566 |
|
| 567 |
/* returns 0 if the struct ctype is opaque, 1 if it is not, or -1 if |
| 568 |
an exception occurs */ |
| 569 |
static inline int |
| 570 |
force_lazy_struct(CTypeDescrObject *ct) |
| 571 |
{ |
| 572 |
assert(ct->ct_flags & (CT_STRUCT | CT_UNION)); |
| 573 |
uint8_t lazy_fields = cffi_check_flag(ct->ct_lazy_field_list); |
| 574 |
if (lazy_fields) { |
| 575 |
// not realized yet |
| 576 |
return do_realize_lazy_struct(ct); |
| 577 |
} |
| 578 |
return ct->ct_stuff != NULL; |
| 579 |
} |
| 580 |
|
| 581 |
|
| 582 |
static PyObject *ctypeget_fields(CTypeDescrObject *ct, void *context) |
| 583 |
{ |
| 584 |
if (ct->ct_flags & (CT_STRUCT | CT_UNION)) { |
| 585 |
assert((ct->ct_flags & CT_IS_OPAQUE) == 0); |
| 586 |
if (!cffi_check_flag(ct->ct_unrealized_struct_or_union)) { |
| 587 |
CFieldObject *cf; |
| 588 |
PyObject *res; |
| 589 |
if (force_lazy_struct(ct) < 0) |
| 590 |
return NULL; |
| 591 |
res = PyList_New(0); |
| 592 |
if (res == NULL) |
| 593 |
return NULL; |
| 594 |
for (cf = (CFieldObject *)ct->ct_extra; |
| 595 |
cf != NULL; cf = cf->cf_next) { |
| 596 |
PyObject *o = PyTuple_Pack(2, get_field_name(ct, cf), |
| 597 |
(PyObject *)cf); |
| 598 |
int err = (o != NULL) ? PyList_Append(res, o) : -1; |
| 599 |
Py_XDECREF(o); |
| 600 |
if (err < 0) { |
| 601 |
Py_DECREF(res); |
| 602 |
return NULL; |
| 603 |
} |
| 604 |
} |
| 605 |
return res; |
| 606 |
} |
| 607 |
else { |
| 608 |
Py_INCREF(Py_None); |
| 609 |
return Py_None; |
| 610 |
} |
| 611 |
} |
| 612 |
return nosuchattr("fields"); |
| 613 |
} |
| 614 |
|
| 615 |
static PyObject *ctypeget_args(CTypeDescrObject *ct, void *context) |
| 616 |
{ |
| 617 |
if (ct->ct_flags & CT_FUNCTIONPTR) { |
| 618 |
PyObject *t = ct->ct_stuff; |
| 619 |
return PyTuple_GetSlice(t, 2, PyTuple_GET_SIZE(t)); |
| 620 |
} |
| 621 |
return nosuchattr("args"); |
| 622 |
} |
| 623 |
|
| 624 |
static PyObject *ctypeget_result(CTypeDescrObject *ct, void *context) |
| 625 |
{ |
| 626 |
if (ct->ct_flags & CT_FUNCTIONPTR) { |
| 627 |
PyObject *res = PyTuple_GetItem(ct->ct_stuff, 1); |
| 628 |
Py_XINCREF(res); |
| 629 |
return res; |
| 630 |
} |
| 631 |
return nosuchattr("result"); |
| 632 |
} |
| 633 |
|
| 634 |
static PyObject *ctypeget_ellipsis(CTypeDescrObject *ct, void *context) |
| 635 |
{ |
| 636 |
if (ct->ct_flags & CT_FUNCTIONPTR) { |
| 637 |
PyObject *res = ct->ct_extra ? Py_False : Py_True; |
| 638 |
Py_INCREF(res); |
| 639 |
return res; |
| 640 |
} |
| 641 |
return nosuchattr("ellipsis"); |
| 642 |
} |
| 643 |
|
| 644 |
static PyObject *ctypeget_abi(CTypeDescrObject *ct, void *context) |
| 645 |
{ |
| 646 |
if (ct->ct_flags & CT_FUNCTIONPTR) { |
| 647 |
PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0); |
| 648 |
Py_XINCREF(res); |
| 649 |
return res; |
| 650 |
} |
| 651 |
return nosuchattr("abi"); |
| 652 |
} |
| 653 |
|
| 654 |
static PyObject *ctypeget_elements(CTypeDescrObject *ct, void *context) |
| 655 |
{ |
| 656 |
if (ct->ct_flags & CT_IS_ENUM) { |
| 657 |
PyObject *res = PyTuple_GetItem(ct->ct_stuff, 1); |
| 658 |
if (res) res = PyDict_Copy(res); |
| 659 |
return res; |
| 660 |
} |
| 661 |
return nosuchattr("elements"); |
| 662 |
} |
| 663 |
|
| 664 |
static PyObject *ctypeget_relements(CTypeDescrObject *ct, void *context) |
| 665 |
{ |
| 666 |
if (ct->ct_flags & CT_IS_ENUM) { |
| 667 |
PyObject *res = PyTuple_GetItem(ct->ct_stuff, 0); |
| 668 |
if (res) res = PyDict_Copy(res); |
| 669 |
return res; |
| 670 |
} |
| 671 |
return nosuchattr("relements"); |
| 672 |
} |
| 673 |
|
| 674 |
static PyGetSetDef ctypedescr_getsets[] = { |
| 675 |
{"kind", (getter)ctypeget_kind, NULL, "kind"}, |
| 676 |
{"cname", (getter)ctypeget_cname, NULL, "C name"}, |
| 677 |
{"item", (getter)ctypeget_item, NULL, "pointer to, or array of"}, |
| 678 |
{"length", (getter)ctypeget_length, NULL, "array length or None"}, |
| 679 |
{"fields", (getter)ctypeget_fields, NULL, "struct or union fields"}, |
| 680 |
{"args", (getter)ctypeget_args, NULL, "function argument types"}, |
| 681 |
{"result", (getter)ctypeget_result, NULL, "function result type"}, |
| 682 |
{"ellipsis", (getter)ctypeget_ellipsis, NULL, "function has '...'"}, |
| 683 |
{"abi", (getter)ctypeget_abi, NULL, "function ABI"}, |
| 684 |
{"elements", (getter)ctypeget_elements, NULL, "enum elements"}, |
| 685 |
{"relements", (getter)ctypeget_relements, NULL, "enum elements, reverse"}, |
| 686 |
{NULL} /* sentinel */ |
| 687 |
}; |
| 688 |
|
| 689 |
static PyObject * |
| 690 |
ctypedescr_dir(PyObject *ct, PyObject *noarg) |
| 691 |
{ |
| 692 |
int err; |
| 693 |
struct PyGetSetDef *gsdef; |
| 694 |
PyObject *res = PyList_New(0); |
| 695 |
if (res == NULL) |
| 696 |
return NULL; |
| 697 |
|
| 698 |
for (gsdef = ctypedescr_getsets; gsdef->name; gsdef++) { |
| 699 |
PyObject *x = PyObject_GetAttrString(ct, gsdef->name); |
| 700 |
if (x == NULL) { |
| 701 |
PyErr_Clear(); |
| 702 |
} |
| 703 |
else { |
| 704 |
Py_DECREF(x); |
| 705 |
x = PyUnicode_FromString(gsdef->name); |
| 706 |
err = (x != NULL) ? PyList_Append(res, x) : -1; |
| 707 |
Py_XDECREF(x); |
| 708 |
if (err < 0) { |
| 709 |
Py_DECREF(res); |
| 710 |
return NULL; |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
return res; |
| 715 |
} |
| 716 |
|
| 717 |
static PyMethodDef ctypedescr_methods[] = { |
| 718 |
{"__dir__", ctypedescr_dir, METH_NOARGS}, |
| 719 |
{NULL, NULL} /* sentinel */ |
| 720 |
}; |
| 721 |
|
| 722 |
static PyTypeObject CTypeDescr_Type = { |
| 723 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 724 |
"_cffi_backend.CType", |
| 725 |
offsetof(CTypeDescrObject, ct_name), |
| 726 |
sizeof(char), |
| 727 |
(destructor)ctypedescr_dealloc, /* tp_dealloc */ |
| 728 |
0, /* tp_print */ |
| 729 |
0, /* tp_getattr */ |
| 730 |
0, /* tp_setattr */ |
| 731 |
0, /* tp_compare */ |
| 732 |
(reprfunc)ctypedescr_repr, /* tp_repr */ |
| 733 |
0, /* tp_as_number */ |
| 734 |
0, /* tp_as_sequence */ |
| 735 |
0, /* tp_as_mapping */ |
| 736 |
0, /* tp_hash */ |
| 737 |
0, /* tp_call */ |
| 738 |
0, /* tp_str */ |
| 739 |
PyObject_GenericGetAttr, /* tp_getattro */ |
| 740 |
0, /* tp_setattro */ |
| 741 |
0, /* tp_as_buffer */ |
| 742 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 743 |
0, /* tp_doc */ |
| 744 |
(traverseproc)ctypedescr_traverse, /* tp_traverse */ |
| 745 |
(inquiry)ctypedescr_clear, /* tp_clear */ |
| 746 |
0, /* tp_richcompare */ |
| 747 |
offsetof(CTypeDescrObject, ct_weakreflist), /* tp_weaklistoffset */ |
| 748 |
0, /* tp_iter */ |
| 749 |
0, /* tp_iternext */ |
| 750 |
ctypedescr_methods, /* tp_methods */ |
| 751 |
0, /* tp_members */ |
| 752 |
ctypedescr_getsets, /* tp_getset */ |
| 753 |
}; |
| 754 |
|
| 755 |
/************************************************************/ |
| 756 |
|
| 757 |
static PyObject * |
| 758 |
get_field_name(CTypeDescrObject *ct, CFieldObject *cf) |
| 759 |
{ |
| 760 |
Py_ssize_t i = 0; |
| 761 |
PyObject *d_key, *d_value; |
| 762 |
while (PyDict_Next(ct->ct_stuff, &i, &d_key, &d_value)) { |
| 763 |
if (d_value == (PyObject *)cf) |
| 764 |
return d_key; |
| 765 |
} |
| 766 |
Py_FatalError("_cffi_backend: get_field_name()"); |
| 767 |
return NULL; |
| 768 |
} |
| 769 |
|
| 770 |
static void |
| 771 |
cfield_dealloc(CFieldObject *cf) |
| 772 |
{ |
| 773 |
Py_DECREF(cf->cf_type); |
| 774 |
PyObject_Del(cf); |
| 775 |
} |
| 776 |
|
| 777 |
#undef OFF |
| 778 |
#define OFF(x) offsetof(CFieldObject, x) |
| 779 |
|
| 780 |
static PyMemberDef cfield_members[] = { |
| 781 |
{"type", T_OBJECT, OFF(cf_type), READONLY}, |
| 782 |
{"offset", T_PYSSIZET, OFF(cf_offset), READONLY}, |
| 783 |
{"bitshift", T_SHORT, OFF(cf_bitshift), READONLY}, |
| 784 |
{"bitsize", T_SHORT, OFF(cf_bitsize), READONLY}, |
| 785 |
{"flags", T_UBYTE, OFF(cf_flags), READONLY}, |
| 786 |
{NULL} /* Sentinel */ |
| 787 |
}; |
| 788 |
#undef OFF |
| 789 |
|
| 790 |
static PyTypeObject CField_Type = { |
| 791 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 792 |
"_cffi_backend.CField", |
| 793 |
sizeof(CFieldObject), |
| 794 |
0, |
| 795 |
(destructor)cfield_dealloc, /* tp_dealloc */ |
| 796 |
0, /* tp_print */ |
| 797 |
0, /* tp_getattr */ |
| 798 |
0, /* tp_setattr */ |
| 799 |
0, /* tp_compare */ |
| 800 |
0, /* tp_repr */ |
| 801 |
0, /* tp_as_number */ |
| 802 |
0, /* tp_as_sequence */ |
| 803 |
0, /* tp_as_mapping */ |
| 804 |
0, /* tp_hash */ |
| 805 |
0, /* tp_call */ |
| 806 |
0, /* tp_str */ |
| 807 |
PyObject_GenericGetAttr, /* tp_getattro */ |
| 808 |
0, /* tp_setattro */ |
| 809 |
0, /* tp_as_buffer */ |
| 810 |
Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 811 |
0, /* tp_doc */ |
| 812 |
0, /* tp_traverse */ |
| 813 |
0, /* tp_clear */ |
| 814 |
0, /* tp_richcompare */ |
| 815 |
0, /* tp_weaklistoffset */ |
| 816 |
0, /* tp_iter */ |
| 817 |
0, /* tp_iternext */ |
| 818 |
0, /* tp_methods */ |
| 819 |
cfield_members, /* tp_members */ |
| 820 |
}; |
| 821 |
|
| 822 |
/************************************************************/ |
| 823 |
|
| 824 |
static int |
| 825 |
CDataObject_Or_PyFloat_Check(PyObject *ob) |
| 826 |
{ |
| 827 |
return (PyFloat_Check(ob) || |
| 828 |
(CData_Check(ob) && |
| 829 |
(((CDataObject *)ob)->c_type->ct_flags & CT_PRIMITIVE_FLOAT))); |
| 830 |
} |
| 831 |
|
| 832 |
static PY_LONG_LONG |
| 833 |
_my_PyLong_AsLongLong(PyObject *ob) |
| 834 |
{ |
| 835 |
/* (possibly) convert and cast a Python object to a long long. |
| 836 |
Like PyLong_AsLongLong(), this version accepts a Python int too, and |
| 837 |
does conversions from other types of objects. The difference is that |
| 838 |
this version refuses floats. */ |
| 839 |
if (PyLong_Check(ob)) { |
| 840 |
return PyLong_AsLongLong(ob); |
| 841 |
} |
| 842 |
else { |
| 843 |
PyObject *io; |
| 844 |
PY_LONG_LONG res; |
| 845 |
PyNumberMethods *nb = ob->ob_type->tp_as_number; |
| 846 |
|
| 847 |
if (CDataObject_Or_PyFloat_Check(ob) || |
| 848 |
nb == NULL || nb->nb_int == NULL) { |
| 849 |
PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 850 |
return -1; |
| 851 |
} |
| 852 |
io = (*nb->nb_int) (ob); |
| 853 |
if (io == NULL) |
| 854 |
return -1; |
| 855 |
|
| 856 |
if (PyLong_Check(io)) { |
| 857 |
res = _my_PyLong_AsLongLong(io); |
| 858 |
} |
| 859 |
else { |
| 860 |
PyErr_SetString(PyExc_TypeError, "integer conversion failed"); |
| 861 |
res = -1; |
| 862 |
} |
| 863 |
Py_DECREF(io); |
| 864 |
return res; |
| 865 |
} |
| 866 |
} |
| 867 |
|
| 868 |
static unsigned PY_LONG_LONG |
| 869 |
_my_PyLong_AsUnsignedLongLong(PyObject *ob, int strict) |
| 870 |
{ |
| 871 |
/* (possibly) convert and cast a Python object to an unsigned long long. |
| 872 |
Like PyLong_AsLongLong(), this version accepts a Python int too, and |
| 873 |
does conversions from other types of objects. If 'strict', complains |
| 874 |
with OverflowError and refuses floats. If '!strict', rounds floats |
| 875 |
and masks the result. */ |
| 876 |
if (PyLong_Check(ob)) { |
| 877 |
if (strict) { |
| 878 |
if (_PyLong_Sign(ob) < 0) |
| 879 |
goto negative; |
| 880 |
return PyLong_AsUnsignedLongLong(ob); |
| 881 |
} |
| 882 |
else { |
| 883 |
return PyLong_AsUnsignedLongLongMask(ob); |
| 884 |
} |
| 885 |
} |
| 886 |
else { |
| 887 |
PyObject *io; |
| 888 |
unsigned PY_LONG_LONG res; |
| 889 |
PyNumberMethods *nb = ob->ob_type->tp_as_number; |
| 890 |
|
| 891 |
if ((strict && CDataObject_Or_PyFloat_Check(ob)) || |
| 892 |
nb == NULL || nb->nb_int == NULL) { |
| 893 |
PyErr_SetString(PyExc_TypeError, "an integer is required"); |
| 894 |
return (unsigned PY_LONG_LONG)-1; |
| 895 |
} |
| 896 |
io = (*nb->nb_int) (ob); |
| 897 |
if (io == NULL) |
| 898 |
return (unsigned PY_LONG_LONG)-1; |
| 899 |
|
| 900 |
if (PyLong_Check(io)) { |
| 901 |
res = _my_PyLong_AsUnsignedLongLong(io, strict); |
| 902 |
} |
| 903 |
else { |
| 904 |
PyErr_SetString(PyExc_TypeError, "integer conversion failed"); |
| 905 |
res = (unsigned PY_LONG_LONG)-1; |
| 906 |
} |
| 907 |
Py_DECREF(io); |
| 908 |
return res; |
| 909 |
} |
| 910 |
|
| 911 |
negative: |
| 912 |
PyErr_SetString(PyExc_OverflowError, |
| 913 |
"can't convert negative number to unsigned"); |
| 914 |
return (unsigned PY_LONG_LONG)-1; |
| 915 |
} |
| 916 |
|
| 917 |
#define _read_raw_data(type) \ |
| 918 |
do { \ |
| 919 |
if (size == sizeof(type)) { \ |
| 920 |
type r; \ |
| 921 |
memcpy(&r, target, sizeof(type)); \ |
| 922 |
return r; \ |
| 923 |
} \ |
| 924 |
} while(0) |
| 925 |
|
| 926 |
static PY_LONG_LONG |
| 927 |
read_raw_signed_data(char *target, int size) |
| 928 |
{ |
| 929 |
_read_raw_data(signed char); |
| 930 |
_read_raw_data(short); |
| 931 |
_read_raw_data(int); |
| 932 |
_read_raw_data(long); |
| 933 |
_read_raw_data(PY_LONG_LONG); |
| 934 |
Py_FatalError("read_raw_signed_data: bad integer size"); |
| 935 |
return 0; |
| 936 |
} |
| 937 |
|
| 938 |
static unsigned PY_LONG_LONG |
| 939 |
read_raw_unsigned_data(char *target, int size) |
| 940 |
{ |
| 941 |
_read_raw_data(unsigned char); |
| 942 |
_read_raw_data(unsigned short); |
| 943 |
_read_raw_data(unsigned int); |
| 944 |
_read_raw_data(unsigned long); |
| 945 |
_read_raw_data(unsigned PY_LONG_LONG); |
| 946 |
Py_FatalError("read_raw_unsigned_data: bad integer size"); |
| 947 |
return 0; |
| 948 |
} |
| 949 |
|
| 950 |
#ifdef __GNUC__ |
| 951 |
/* This is a workaround for what I think is a GCC bug on several |
| 952 |
platforms. See issue #378. */ |
| 953 |
__attribute__((noinline)) |
| 954 |
#endif |
| 955 |
void _cffi_memcpy(char *target, const void *src, size_t size) |
| 956 |
{ |
| 957 |
memcpy(target, src, size); |
| 958 |
} |
| 959 |
|
| 960 |
#define _write_raw_data(type) \ |
| 961 |
do { \ |
| 962 |
if (size == sizeof(type)) { \ |
| 963 |
type r = (type)source; \ |
| 964 |
_cffi_memcpy(target, &r, sizeof(type)); \ |
| 965 |
return; \ |
| 966 |
} \ |
| 967 |
} while(0) |
| 968 |
|
| 969 |
static void |
| 970 |
write_raw_integer_data(char *target, unsigned PY_LONG_LONG source, int size) |
| 971 |
{ |
| 972 |
_write_raw_data(unsigned char); |
| 973 |
_write_raw_data(unsigned short); |
| 974 |
_write_raw_data(unsigned int); |
| 975 |
_write_raw_data(unsigned long); |
| 976 |
_write_raw_data(unsigned PY_LONG_LONG); |
| 977 |
Py_FatalError("write_raw_integer_data: bad integer size"); |
| 978 |
} |
| 979 |
|
| 980 |
static double |
| 981 |
read_raw_float_data(char *target, int size) |
| 982 |
{ |
| 983 |
_read_raw_data(float); |
| 984 |
_read_raw_data(double); |
| 985 |
Py_FatalError("read_raw_float_data: bad float size"); |
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
static long double |
| 990 |
read_raw_longdouble_data(char *target) |
| 991 |
{ |
| 992 |
int size = sizeof(long double); |
| 993 |
_read_raw_data(long double); |
| 994 |
Py_FatalError("read_raw_longdouble_data: bad long double size"); |
| 995 |
return 0; |
| 996 |
} |
| 997 |
|
| 998 |
static Py_complex |
| 999 |
read_raw_complex_data(char *target, int size) |
| 1000 |
{ |
| 1001 |
Py_complex r = {0.0, 0.0}; |
| 1002 |
if (size == 2*sizeof(float)) { |
| 1003 |
float real_part, imag_part; |
| 1004 |
memcpy(&real_part, target + 0, sizeof(float)); |
| 1005 |
memcpy(&imag_part, target + sizeof(float), sizeof(float)); |
| 1006 |
r.real = real_part; |
| 1007 |
r.imag = imag_part; |
| 1008 |
return r; |
| 1009 |
} |
| 1010 |
if (size == 2*sizeof(double)) { |
| 1011 |
memcpy(&r, target, 2*sizeof(double)); |
| 1012 |
return r; |
| 1013 |
} |
| 1014 |
Py_FatalError("read_raw_complex_data: bad complex size"); |
| 1015 |
return r; |
| 1016 |
} |
| 1017 |
|
| 1018 |
static void |
| 1019 |
write_raw_float_data(char *target, double source, int size) |
| 1020 |
{ |
| 1021 |
_write_raw_data(float); |
| 1022 |
_write_raw_data(double); |
| 1023 |
Py_FatalError("write_raw_float_data: bad float size"); |
| 1024 |
} |
| 1025 |
|
| 1026 |
static void |
| 1027 |
write_raw_longdouble_data(char *target, long double source) |
| 1028 |
{ |
| 1029 |
int size = sizeof(long double); |
| 1030 |
_write_raw_data(long double); |
| 1031 |
} |
| 1032 |
|
| 1033 |
#define _write_raw_complex_data(type) \ |
| 1034 |
do { \ |
| 1035 |
if (size == 2*sizeof(type)) { \ |
| 1036 |
type r = (type)source.real; \ |
| 1037 |
type i = (type)source.imag; \ |
| 1038 |
_cffi_memcpy(target, &r, sizeof(type)); \ |
| 1039 |
_cffi_memcpy(target+sizeof(type), &i, sizeof(type)); \ |
| 1040 |
return; \ |
| 1041 |
} \ |
| 1042 |
} while(0) |
| 1043 |
|
| 1044 |
static void |
| 1045 |
write_raw_complex_data(char *target, Py_complex source, int size) |
| 1046 |
{ |
| 1047 |
_write_raw_complex_data(float); |
| 1048 |
_write_raw_complex_data(double); |
| 1049 |
Py_FatalError("write_raw_complex_data: bad complex size"); |
| 1050 |
} |
| 1051 |
|
| 1052 |
static PyObject * |
| 1053 |
new_simple_cdata(char *data, CTypeDescrObject *ct) |
| 1054 |
{ |
| 1055 |
CDataObject *cd = PyObject_New(CDataObject, &CData_Type); |
| 1056 |
if (cd == NULL) |
| 1057 |
return NULL; |
| 1058 |
Py_INCREF(ct); |
| 1059 |
cd->c_data = data; |
| 1060 |
cd->c_type = ct; |
| 1061 |
cd->c_weakreflist = NULL; |
| 1062 |
return (PyObject *)cd; |
| 1063 |
} |
| 1064 |
|
| 1065 |
static PyObject * |
| 1066 |
new_sized_cdata(char *data, CTypeDescrObject *ct, Py_ssize_t length) |
| 1067 |
{ |
| 1068 |
CDataObject_own_length *scd; |
| 1069 |
|
| 1070 |
scd = (CDataObject_own_length *)PyObject_Malloc( |
| 1071 |
offsetof(CDataObject_own_length, alignment)); |
| 1072 |
if (PyObject_Init((PyObject *)scd, &CData_Type) == NULL) |
| 1073 |
return NULL; |
| 1074 |
Py_INCREF(ct); |
| 1075 |
scd->head.c_type = ct; |
| 1076 |
scd->head.c_data = data; |
| 1077 |
scd->head.c_weakreflist = NULL; |
| 1078 |
scd->length = length; |
| 1079 |
return (PyObject *)scd; |
| 1080 |
} |
| 1081 |
|
| 1082 |
static CDataObject *_new_casted_primitive(CTypeDescrObject *ct); /*forward*/ |
| 1083 |
|
| 1084 |
static PyObject * |
| 1085 |
convert_to_object(char *data, CTypeDescrObject *ct) |
| 1086 |
{ |
| 1087 |
if (!(ct->ct_flags & CT_PRIMITIVE_ANY)) { |
| 1088 |
/* non-primitive types (check done just for performance) */ |
| 1089 |
if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) { |
| 1090 |
char *ptrdata = *(char **)data; |
| 1091 |
/*READ(data, sizeof(char *))*/ |
| 1092 |
return new_simple_cdata(ptrdata, ct); |
| 1093 |
} |
| 1094 |
else if (ct->ct_flags & CT_IS_OPAQUE) { |
| 1095 |
PyErr_Format(PyExc_TypeError, "cdata '%s' is opaque", |
| 1096 |
ct->ct_name); |
| 1097 |
return NULL; |
| 1098 |
} |
| 1099 |
else if (ct->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 1100 |
if (force_lazy_struct(ct) < 0) |
| 1101 |
return NULL; |
| 1102 |
return new_simple_cdata(data, ct); |
| 1103 |
} |
| 1104 |
else if (ct->ct_flags & CT_ARRAY) { |
| 1105 |
if (ct->ct_length < 0) { |
| 1106 |
/* we can't return a <cdata 'int[]'> here, because we don't |
| 1107 |
know the length to give it. As a compromise, returns |
| 1108 |
<cdata 'int *'> in this case. */ |
| 1109 |
ct = (CTypeDescrObject *)ct->ct_stuff; |
| 1110 |
} |
| 1111 |
return new_simple_cdata(data, ct); |
| 1112 |
} |
| 1113 |
} |
| 1114 |
else if (ct->ct_flags & CT_PRIMITIVE_SIGNED) { |
| 1115 |
PY_LONG_LONG value; |
| 1116 |
/*READ(data, ct->ct_size)*/ |
| 1117 |
value = read_raw_signed_data(data, ct->ct_size); |
| 1118 |
if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) |
| 1119 |
return PyLong_FromLong((long)value); |
| 1120 |
else |
| 1121 |
return PyLong_FromLongLong(value); |
| 1122 |
} |
| 1123 |
else if (ct->ct_flags & CT_PRIMITIVE_UNSIGNED) { |
| 1124 |
unsigned PY_LONG_LONG value; |
| 1125 |
/*READ(data, ct->ct_size)*/ |
| 1126 |
value = read_raw_unsigned_data(data, ct->ct_size); |
| 1127 |
|
| 1128 |
if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) { |
| 1129 |
if (ct->ct_flags & CT_IS_BOOL) { |
| 1130 |
PyObject *x; |
| 1131 |
switch ((int)value) { |
| 1132 |
case 0: x = Py_False; break; |
| 1133 |
case 1: x = Py_True; break; |
| 1134 |
default: |
| 1135 |
PyErr_Format(PyExc_ValueError, |
| 1136 |
"got a _Bool of value %d, expected 0 or 1", |
| 1137 |
(int)value); |
| 1138 |
return NULL; |
| 1139 |
} |
| 1140 |
Py_INCREF(x); |
| 1141 |
return x; |
| 1142 |
} |
| 1143 |
return PyLong_FromLong((long)value); |
| 1144 |
} |
| 1145 |
else |
| 1146 |
return PyLong_FromUnsignedLongLong(value); |
| 1147 |
} |
| 1148 |
else if (ct->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 1149 |
/*READ(data, ct->ct_size)*/ |
| 1150 |
if (!(ct->ct_flags & CT_IS_LONGDOUBLE)) { |
| 1151 |
double value = read_raw_float_data(data, ct->ct_size); |
| 1152 |
return PyFloat_FromDouble(value); |
| 1153 |
} |
| 1154 |
else { |
| 1155 |
long double value = read_raw_longdouble_data(data); |
| 1156 |
CDataObject *cd = _new_casted_primitive(ct); |
| 1157 |
if (cd != NULL) |
| 1158 |
write_raw_longdouble_data(cd->c_data, value); |
| 1159 |
return (PyObject *)cd; |
| 1160 |
} |
| 1161 |
} |
| 1162 |
else if (ct->ct_flags & CT_PRIMITIVE_CHAR) { |
| 1163 |
/*READ(data, ct->ct_size)*/ |
| 1164 |
switch (ct->ct_size) { |
| 1165 |
case sizeof(char): |
| 1166 |
return PyBytes_FromStringAndSize(data, 1); |
| 1167 |
case 2: |
| 1168 |
return _my_PyUnicode_FromChar16((cffi_char16_t *)data, 1); |
| 1169 |
case 4: |
| 1170 |
return _my_PyUnicode_FromChar32((cffi_char32_t *)data, 1); |
| 1171 |
} |
| 1172 |
} |
| 1173 |
else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) { |
| 1174 |
Py_complex value = read_raw_complex_data(data, ct->ct_size); |
| 1175 |
return PyComplex_FromCComplex(value); |
| 1176 |
} |
| 1177 |
|
| 1178 |
PyErr_Format(PyExc_SystemError, |
| 1179 |
"convert_to_object: '%s'", ct->ct_name); |
| 1180 |
return NULL; |
| 1181 |
} |
| 1182 |
|
| 1183 |
static PyObject * |
| 1184 |
convert_to_object_bitfield(char *data, CFieldObject *cf) |
| 1185 |
{ |
| 1186 |
CTypeDescrObject *ct = cf->cf_type; |
| 1187 |
/*READ(data, ct->ct_size)*/ |
| 1188 |
|
| 1189 |
if (ct->ct_flags & CT_PRIMITIVE_SIGNED) { |
| 1190 |
unsigned PY_LONG_LONG value, valuemask, shiftforsign; |
| 1191 |
PY_LONG_LONG result; |
| 1192 |
|
| 1193 |
value = (unsigned PY_LONG_LONG)read_raw_signed_data(data, ct->ct_size); |
| 1194 |
valuemask = (1ULL << cf->cf_bitsize) - 1ULL; |
| 1195 |
shiftforsign = 1ULL << (cf->cf_bitsize - 1); |
| 1196 |
value = ((value >> cf->cf_bitshift) + shiftforsign) & valuemask; |
| 1197 |
result = ((PY_LONG_LONG)value) - (PY_LONG_LONG)shiftforsign; |
| 1198 |
|
| 1199 |
if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) |
| 1200 |
return PyLong_FromLong((long)result); |
| 1201 |
else |
| 1202 |
return PyLong_FromLongLong(result); |
| 1203 |
} |
| 1204 |
else { |
| 1205 |
unsigned PY_LONG_LONG value, valuemask; |
| 1206 |
|
| 1207 |
value = read_raw_unsigned_data(data, ct->ct_size); |
| 1208 |
valuemask = (1ULL << cf->cf_bitsize) - 1ULL; |
| 1209 |
value = (value >> cf->cf_bitshift) & valuemask; |
| 1210 |
|
| 1211 |
if (ct->ct_flags & CT_PRIMITIVE_FITS_LONG) |
| 1212 |
return PyLong_FromLong((long)value); |
| 1213 |
else |
| 1214 |
return PyLong_FromUnsignedLongLong(value); |
| 1215 |
} |
| 1216 |
} |
| 1217 |
|
| 1218 |
static int _convert_overflow(PyObject *init, const char *ct_name) |
| 1219 |
{ |
| 1220 |
PyObject *s; |
| 1221 |
if (PyErr_Occurred()) /* already an exception pending */ |
| 1222 |
return -1; |
| 1223 |
s = PyObject_Str(init); |
| 1224 |
if (s == NULL) |
| 1225 |
return -1; |
| 1226 |
PyErr_Format(PyExc_OverflowError, "integer %s does not fit '%s'", |
| 1227 |
PyUnicode_AsUTF8(s), ct_name); |
| 1228 |
Py_DECREF(s); |
| 1229 |
return -1; |
| 1230 |
} |
| 1231 |
|
| 1232 |
static int _convert_to_char(PyObject *init) |
| 1233 |
{ |
| 1234 |
if (PyBytes_Check(init) && PyBytes_GET_SIZE(init) == 1) { |
| 1235 |
return (unsigned char)(PyBytes_AS_STRING(init)[0]); |
| 1236 |
} |
| 1237 |
if (CData_Check(init) && |
| 1238 |
(((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) && |
| 1239 |
(((CDataObject *)init)->c_type->ct_size == sizeof(char))) { |
| 1240 |
char *data = ((CDataObject *)init)->c_data; |
| 1241 |
/*READ(data, 1)*/ |
| 1242 |
return *(unsigned char *)data; |
| 1243 |
} |
| 1244 |
PyErr_Format(PyExc_TypeError, |
| 1245 |
"initializer for ctype 'char' must be a bytes" |
| 1246 |
" of length 1, not %.200s", Py_TYPE(init)->tp_name); |
| 1247 |
return -1; |
| 1248 |
} |
| 1249 |
|
| 1250 |
static cffi_char16_t _convert_to_char16_t(PyObject *init) |
| 1251 |
{ |
| 1252 |
char err_got[80]; |
| 1253 |
err_got[0] = 0; |
| 1254 |
|
| 1255 |
if (PyUnicode_Check(init)) { |
| 1256 |
cffi_char16_t ordinal; |
| 1257 |
if (_my_PyUnicode_AsSingleChar16(init, &ordinal, err_got) == 0) |
| 1258 |
return ordinal; |
| 1259 |
} |
| 1260 |
if (CData_Check(init) && |
| 1261 |
(((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) && |
| 1262 |
(((CDataObject *)init)->c_type->ct_size == 2)) { |
| 1263 |
char *data = ((CDataObject *)init)->c_data; |
| 1264 |
/*READ(data, 2)*/ |
| 1265 |
return *(cffi_char16_t *)data; |
| 1266 |
} |
| 1267 |
PyErr_Format(PyExc_TypeError, |
| 1268 |
"initializer for ctype 'char16_t' must be a unicode string " |
| 1269 |
"of length 1, not %.200s", |
| 1270 |
err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got); |
| 1271 |
return (cffi_char16_t)-1; |
| 1272 |
} |
| 1273 |
|
| 1274 |
static cffi_char32_t _convert_to_char32_t(PyObject *init) |
| 1275 |
{ |
| 1276 |
char err_got[80]; |
| 1277 |
err_got[0] = 0; |
| 1278 |
|
| 1279 |
if (PyUnicode_Check(init)) { |
| 1280 |
cffi_char32_t ordinal; |
| 1281 |
if (_my_PyUnicode_AsSingleChar32(init, &ordinal, err_got) == 0) |
| 1282 |
return ordinal; |
| 1283 |
} |
| 1284 |
if (CData_Check(init) && |
| 1285 |
(((CDataObject *)init)->c_type->ct_flags & CT_PRIMITIVE_CHAR) && |
| 1286 |
(((CDataObject *)init)->c_type->ct_size == 4)) { |
| 1287 |
char *data = ((CDataObject *)init)->c_data; |
| 1288 |
/*READ(data, 4)*/ |
| 1289 |
return *(cffi_char32_t *)data; |
| 1290 |
} |
| 1291 |
PyErr_Format(PyExc_TypeError, |
| 1292 |
"initializer for ctype 'char32_t' must be a unicode string " |
| 1293 |
"of length 1, not %.200s", |
| 1294 |
err_got[0] == 0 ? Py_TYPE(init)->tp_name : err_got); |
| 1295 |
return (cffi_char32_t)-1; |
| 1296 |
} |
| 1297 |
|
| 1298 |
static int _convert_error(PyObject *init, CTypeDescrObject *ct, |
| 1299 |
const char *expected) |
| 1300 |
{ |
| 1301 |
if (CData_Check(init)) { |
| 1302 |
CTypeDescrObject *ct2 = ((CDataObject *)init)->c_type; |
| 1303 |
if (strcmp(ct->ct_name, ct2->ct_name) != 0) |
| 1304 |
PyErr_Format(PyExc_TypeError, |
| 1305 |
"initializer for ctype '%s' must be a %s, " |
| 1306 |
"not cdata '%s'", |
| 1307 |
ct->ct_name, expected, ct2->ct_name); |
| 1308 |
else if (ct != ct2) { |
| 1309 |
/* in case we'd give the error message "initializer for |
| 1310 |
ctype 'A' must be a pointer to same type, not cdata |
| 1311 |
'B'", but with A=B, then give instead a different error |
| 1312 |
message to try to clear up the confusion */ |
| 1313 |
PyErr_Format(PyExc_TypeError, |
| 1314 |
"initializer for ctype '%s' appears indeed to be '%s'," |
| 1315 |
" but the types are different (check that you are not" |
| 1316 |
" e.g. mixing up different ffi instances)", |
| 1317 |
ct->ct_name, ct2->ct_name); |
| 1318 |
} |
| 1319 |
else |
| 1320 |
{ |
| 1321 |
PyErr_Format(PyExc_SystemError, |
| 1322 |
"initializer for ctype '%s' is correct, but we get " |
| 1323 |
"an internal mismatch--please report a bug", |
| 1324 |
ct->ct_name); |
| 1325 |
} |
| 1326 |
} |
| 1327 |
else |
| 1328 |
PyErr_Format(PyExc_TypeError, |
| 1329 |
"initializer for ctype '%s' must be a %s, " |
| 1330 |
"not %.200s", |
| 1331 |
ct->ct_name, expected, Py_TYPE(init)->tp_name); |
| 1332 |
return -1; |
| 1333 |
} |
| 1334 |
|
| 1335 |
static int /* forward */ |
| 1336 |
convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init); |
| 1337 |
static int /* forward */ |
| 1338 |
convert_from_object_bitfield(char *data, CFieldObject *cf, PyObject *init); |
| 1339 |
|
| 1340 |
static Py_ssize_t |
| 1341 |
get_new_array_length(CTypeDescrObject *ctitem, PyObject **pvalue) |
| 1342 |
{ |
| 1343 |
PyObject *value = *pvalue; |
| 1344 |
|
| 1345 |
if (PyList_Check(value) || PyTuple_Check(value)) { |
| 1346 |
return PySequence_Fast_GET_SIZE(value); |
| 1347 |
} |
| 1348 |
else if (PyBytes_Check(value)) { |
| 1349 |
/* from a string, we add the null terminator */ |
| 1350 |
return PyBytes_GET_SIZE(value) + 1; |
| 1351 |
} |
| 1352 |
else if (PyUnicode_Check(value)) { |
| 1353 |
/* from a unicode, we add the null terminator */ |
| 1354 |
int length; |
| 1355 |
if (ctitem->ct_size == 2) |
| 1356 |
length = _my_PyUnicode_SizeAsChar16(value); |
| 1357 |
else |
| 1358 |
length = _my_PyUnicode_SizeAsChar32(value); |
| 1359 |
return length + 1; |
| 1360 |
} |
| 1361 |
else { |
| 1362 |
Py_ssize_t explicitlength; |
| 1363 |
explicitlength = PyNumber_AsSsize_t(value, PyExc_OverflowError); |
| 1364 |
if (explicitlength < 0) { |
| 1365 |
if (PyErr_Occurred()) { |
| 1366 |
if (PyErr_ExceptionMatches(PyExc_TypeError)) |
| 1367 |
PyErr_Format(PyExc_TypeError, |
| 1368 |
"expected new array length or list/tuple/str, " |
| 1369 |
"not %.200s", Py_TYPE(value)->tp_name); |
| 1370 |
} |
| 1371 |
else |
| 1372 |
PyErr_SetString(PyExc_ValueError, "negative array length"); |
| 1373 |
return -1; |
| 1374 |
} |
| 1375 |
*pvalue = Py_None; |
| 1376 |
return explicitlength; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
|
| 1380 |
static int |
| 1381 |
convert_field_from_object(char *data, CFieldObject *cf, PyObject *value) |
| 1382 |
{ |
| 1383 |
data += cf->cf_offset; |
| 1384 |
if (cf->cf_bitshift >= 0) |
| 1385 |
return convert_from_object_bitfield(data, cf, value); |
| 1386 |
else |
| 1387 |
return convert_from_object(data, cf->cf_type, value); |
| 1388 |
} |
| 1389 |
|
| 1390 |
static int |
| 1391 |
add_varsize_length(Py_ssize_t offset, Py_ssize_t itemsize, |
| 1392 |
Py_ssize_t varsizelength, Py_ssize_t *optvarsize) |
| 1393 |
{ |
| 1394 |
/* update '*optvarsize' to account for an array of 'varsizelength' |
| 1395 |
elements, each of size 'itemsize', that starts at 'offset'. */ |
| 1396 |
Py_ssize_t size = ADD_WRAPAROUND(offset, |
| 1397 |
MUL_WRAPAROUND(itemsize, varsizelength)); |
| 1398 |
if (size < 0 || |
| 1399 |
((size - offset) / itemsize) != varsizelength) { |
| 1400 |
PyErr_SetString(PyExc_OverflowError, |
| 1401 |
"array size would overflow a Py_ssize_t"); |
| 1402 |
return -1; |
| 1403 |
} |
| 1404 |
if (size > *optvarsize) |
| 1405 |
*optvarsize = size; |
| 1406 |
return 0; |
| 1407 |
} |
| 1408 |
|
| 1409 |
static int |
| 1410 |
convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init, |
| 1411 |
Py_ssize_t *optvarsize); /* forward */ |
| 1412 |
|
| 1413 |
static int |
| 1414 |
convert_vfield_from_object(char *data, CFieldObject *cf, PyObject *value, |
| 1415 |
Py_ssize_t *optvarsize) |
| 1416 |
{ |
| 1417 |
/* a special case for var-sized C99 arrays */ |
| 1418 |
if ((cf->cf_type->ct_flags & CT_ARRAY) && cf->cf_type->ct_size < 0) { |
| 1419 |
Py_ssize_t varsizelength = get_new_array_length( |
| 1420 |
cf->cf_type->ct_itemdescr, &value); |
| 1421 |
if (varsizelength < 0) |
| 1422 |
return -1; |
| 1423 |
if (optvarsize != NULL) { |
| 1424 |
/* in this mode, the only purpose of this function is to compute |
| 1425 |
the real size of the structure from a var-sized C99 array */ |
| 1426 |
assert(data == NULL); |
| 1427 |
return add_varsize_length(cf->cf_offset, |
| 1428 |
cf->cf_type->ct_itemdescr->ct_size, |
| 1429 |
varsizelength, |
| 1430 |
optvarsize); |
| 1431 |
} |
| 1432 |
/* if 'value' was only an integer, get_new_array_length() returns |
| 1433 |
it and convert 'value' to be None. Detect if this was the case, |
| 1434 |
and if so, stop here, leaving the content uninitialized |
| 1435 |
(it should be zero-initialized from somewhere else). */ |
| 1436 |
if (value == Py_None) |
| 1437 |
return 0; |
| 1438 |
} |
| 1439 |
if (optvarsize == NULL) { |
| 1440 |
return convert_field_from_object(data, cf, value); |
| 1441 |
} |
| 1442 |
else if ((cf->cf_type->ct_flags_mut & CT_WITH_VAR_ARRAY) && !CData_Check(value)) { |
| 1443 |
Py_ssize_t subsize = cf->cf_type->ct_size; |
| 1444 |
if (convert_struct_from_object(NULL, cf->cf_type, value, &subsize) < 0) |
| 1445 |
return -1; |
| 1446 |
return add_varsize_length(cf->cf_offset, 1, subsize, optvarsize); |
| 1447 |
} |
| 1448 |
else |
| 1449 |
return 0; |
| 1450 |
} |
| 1451 |
|
| 1452 |
static int |
| 1453 |
must_be_array_of_zero_or_one(const char *data, Py_ssize_t n) |
| 1454 |
{ |
| 1455 |
Py_ssize_t i; |
| 1456 |
for (i = 0; i < n; i++) { |
| 1457 |
if (((unsigned char)data[i]) > 1) { |
| 1458 |
PyErr_SetString(PyExc_ValueError, |
| 1459 |
"an array of _Bool can only contain \\x00 or \\x01"); |
| 1460 |
return -1; |
| 1461 |
} |
| 1462 |
} |
| 1463 |
return 0; |
| 1464 |
} |
| 1465 |
|
| 1466 |
static Py_ssize_t |
| 1467 |
get_array_length(CDataObject *cd) |
| 1468 |
{ |
| 1469 |
if (cd->c_type->ct_length < 0) |
| 1470 |
return ((CDataObject_own_length *)cd)->length; |
| 1471 |
else |
| 1472 |
return cd->c_type->ct_length; |
| 1473 |
} |
| 1474 |
|
| 1475 |
static int |
| 1476 |
convert_array_from_object(char *data, CTypeDescrObject *ct, PyObject *init) |
| 1477 |
{ |
| 1478 |
/* used by convert_from_object(), and also to decode lists/tuples/unicodes |
| 1479 |
passed as function arguments. 'ct' is an CT_ARRAY in the first case |
| 1480 |
and a CT_POINTER in the second case. */ |
| 1481 |
const char *expected; |
| 1482 |
CTypeDescrObject *ctitem = ct->ct_itemdescr; |
| 1483 |
|
| 1484 |
if (PyList_Check(init) || PyTuple_Check(init)) { |
| 1485 |
PyObject **items; |
| 1486 |
Py_ssize_t i, n; |
| 1487 |
n = PySequence_Fast_GET_SIZE(init); |
| 1488 |
if (ct->ct_length >= 0 && n > ct->ct_length) { |
| 1489 |
PyErr_Format(PyExc_IndexError, |
| 1490 |
"too many initializers for '%s' (got %zd)", |
| 1491 |
ct->ct_name, n); |
| 1492 |
return -1; |
| 1493 |
} |
| 1494 |
items = PySequence_Fast_ITEMS(init); |
| 1495 |
for (i=0; i<n; i++) { |
| 1496 |
if (convert_from_object(data, ctitem, items[i]) < 0) |
| 1497 |
return -1; |
| 1498 |
data += ctitem->ct_size; |
| 1499 |
} |
| 1500 |
return 0; |
| 1501 |
} |
| 1502 |
else if ((ctitem->ct_flags & CT_PRIMITIVE_CHAR) || |
| 1503 |
((ctitem->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED)) |
| 1504 |
&& (ctitem->ct_size == sizeof(char)))) { |
| 1505 |
if (ctitem->ct_size == sizeof(char)) { |
| 1506 |
char *srcdata; |
| 1507 |
Py_ssize_t n; |
| 1508 |
if (!PyBytes_Check(init)) { |
| 1509 |
expected = "bytes or list or tuple"; |
| 1510 |
goto cannot_convert; |
| 1511 |
} |
| 1512 |
n = PyBytes_GET_SIZE(init); |
| 1513 |
if (ct->ct_length >= 0 && n > ct->ct_length) { |
| 1514 |
PyErr_Format(PyExc_IndexError, |
| 1515 |
"initializer bytes is too long for '%s' " |
| 1516 |
"(got %zd characters)", ct->ct_name, n); |
| 1517 |
return -1; |
| 1518 |
} |
| 1519 |
if (n != ct->ct_length) |
| 1520 |
n++; |
| 1521 |
srcdata = PyBytes_AS_STRING(init); |
| 1522 |
if (ctitem->ct_flags & CT_IS_BOOL) |
| 1523 |
if (must_be_array_of_zero_or_one(srcdata, n) < 0) |
| 1524 |
return -1; |
| 1525 |
memcpy(data, srcdata, n); |
| 1526 |
return 0; |
| 1527 |
} |
| 1528 |
else { |
| 1529 |
Py_ssize_t n; |
| 1530 |
if (!PyUnicode_Check(init)) { |
| 1531 |
expected = "unicode or list or tuple"; |
| 1532 |
goto cannot_convert; |
| 1533 |
} |
| 1534 |
|
| 1535 |
if (ctitem->ct_size == 4) |
| 1536 |
n = _my_PyUnicode_SizeAsChar32(init); |
| 1537 |
else |
| 1538 |
n = _my_PyUnicode_SizeAsChar16(init); |
| 1539 |
|
| 1540 |
if (ct->ct_length >= 0 && n > ct->ct_length) { |
| 1541 |
PyErr_Format(PyExc_IndexError, |
| 1542 |
"initializer unicode is too long for '%s' " |
| 1543 |
"(got %zd characters)", ct->ct_name, n); |
| 1544 |
return -1; |
| 1545 |
} |
| 1546 |
if (n != ct->ct_length) |
| 1547 |
n++; |
| 1548 |
if (ctitem->ct_size == 4) |
| 1549 |
return _my_PyUnicode_AsChar32(init, (cffi_char32_t *)data, n); |
| 1550 |
else |
| 1551 |
return _my_PyUnicode_AsChar16(init, (cffi_char16_t *)data, n); |
| 1552 |
} |
| 1553 |
} |
| 1554 |
else { |
| 1555 |
expected = "list or tuple"; |
| 1556 |
goto cannot_convert; |
| 1557 |
} |
| 1558 |
|
| 1559 |
cannot_convert: |
| 1560 |
if ((ct->ct_flags & CT_ARRAY) && CData_Check(init)) |
| 1561 |
{ |
| 1562 |
CDataObject *cd = (CDataObject *)init; |
| 1563 |
if (cd->c_type == ct) |
| 1564 |
{ |
| 1565 |
Py_ssize_t n = get_array_length(cd); |
| 1566 |
memcpy(data, cd->c_data, n * ctitem->ct_size); |
| 1567 |
return 0; |
| 1568 |
} |
| 1569 |
} |
| 1570 |
return _convert_error(init, ct, expected); |
| 1571 |
} |
| 1572 |
|
| 1573 |
static int |
| 1574 |
convert_struct_from_object(char *data, CTypeDescrObject *ct, PyObject *init, |
| 1575 |
Py_ssize_t *optvarsize) |
| 1576 |
{ |
| 1577 |
/* does not accept 'init' being already a CData */ |
| 1578 |
const char *expected; |
| 1579 |
|
| 1580 |
if (force_lazy_struct(ct) <= 0) { |
| 1581 |
if (!PyErr_Occurred()) |
| 1582 |
PyErr_Format(PyExc_TypeError, "'%s' is opaque", ct->ct_name); |
| 1583 |
return -1; |
| 1584 |
} |
| 1585 |
|
| 1586 |
if (PyList_Check(init) || PyTuple_Check(init)) { |
| 1587 |
PyObject **items = PySequence_Fast_ITEMS(init); |
| 1588 |
Py_ssize_t i, n = PySequence_Fast_GET_SIZE(init); |
| 1589 |
CFieldObject *cf = (CFieldObject *)ct->ct_extra; |
| 1590 |
|
| 1591 |
for (i=0; i<n; i++) { |
| 1592 |
while (cf != NULL && (cf->cf_flags & BF_IGNORE_IN_CTOR)) |
| 1593 |
cf = cf->cf_next; |
| 1594 |
if (cf == NULL) { |
| 1595 |
PyErr_Format(PyExc_ValueError, |
| 1596 |
"too many initializers for '%s' (got %zd)", |
| 1597 |
ct->ct_name, n); |
| 1598 |
return -1; |
| 1599 |
} |
| 1600 |
if (convert_vfield_from_object(data, cf, items[i], optvarsize) < 0) |
| 1601 |
return -1; |
| 1602 |
cf = cf->cf_next; |
| 1603 |
} |
| 1604 |
return 0; |
| 1605 |
} |
| 1606 |
if (PyDict_Check(init)) { |
| 1607 |
PyObject *d_key, *d_value; |
| 1608 |
Py_ssize_t i = 0; |
| 1609 |
CFieldObject *cf; |
| 1610 |
|
| 1611 |
while (PyDict_Next(init, &i, &d_key, &d_value)) { |
| 1612 |
cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, d_key); |
| 1613 |
if (cf == NULL) { |
| 1614 |
PyErr_SetObject(PyExc_KeyError, d_key); |
| 1615 |
return -1; |
| 1616 |
} |
| 1617 |
if (convert_vfield_from_object(data, cf, d_value, optvarsize) < 0) |
| 1618 |
return -1; |
| 1619 |
} |
| 1620 |
return 0; |
| 1621 |
} |
| 1622 |
expected = optvarsize == NULL ? "list or tuple or dict or struct-cdata" |
| 1623 |
: "list or tuple or dict"; |
| 1624 |
return _convert_error(init, ct, expected); |
| 1625 |
} |
| 1626 |
|
| 1627 |
static PyObject* try_extract_directfnptr(PyObject *x); /* forward */ |
| 1628 |
|
| 1629 |
#ifdef __GNUC__ |
| 1630 |
# if __GNUC__ >= 4 |
| 1631 |
/* Don't go inlining this huge function. Needed because occasionally |
| 1632 |
it gets inlined in places where is causes a warning: call to |
| 1633 |
__builtin___memcpy_chk will always overflow destination buffer |
| 1634 |
(which is places where the 'ct' should never represent such a large |
| 1635 |
primitive type anyway). */ |
| 1636 |
__attribute__((noinline)) |
| 1637 |
# endif |
| 1638 |
#endif |
| 1639 |
static int |
| 1640 |
convert_from_object(char *data, CTypeDescrObject *ct, PyObject *init) |
| 1641 |
{ |
| 1642 |
const char *expected; |
| 1643 |
char buf[sizeof(PY_LONG_LONG)]; |
| 1644 |
|
| 1645 |
/*if (ct->ct_size > 0)*/ |
| 1646 |
/*WRITE(data, ct->ct_size)*/ |
| 1647 |
|
| 1648 |
if (ct->ct_flags & CT_ARRAY) { |
| 1649 |
return convert_array_from_object(data, ct, init); |
| 1650 |
} |
| 1651 |
if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) { |
| 1652 |
char *ptrdata; |
| 1653 |
CTypeDescrObject *ctinit; |
| 1654 |
|
| 1655 |
if (!CData_Check(init)) { |
| 1656 |
PyObject *func_cdata = try_extract_directfnptr(init); |
| 1657 |
if (func_cdata != NULL && CData_Check(func_cdata)) { |
| 1658 |
init = func_cdata; |
| 1659 |
} |
| 1660 |
else { |
| 1661 |
if (PyErr_Occurred()) |
| 1662 |
return -1; |
| 1663 |
expected = "cdata pointer"; |
| 1664 |
goto cannot_convert; |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
ctinit = ((CDataObject *)init)->c_type; |
| 1669 |
if (!(ctinit->ct_flags & (CT_POINTER|CT_FUNCTIONPTR))) { |
| 1670 |
if (ctinit->ct_flags & CT_ARRAY) |
| 1671 |
ctinit = (CTypeDescrObject *)ctinit->ct_stuff; |
| 1672 |
else { |
| 1673 |
expected = "pointer or array"; |
| 1674 |
goto cannot_convert; |
| 1675 |
} |
| 1676 |
} |
| 1677 |
if (ctinit != ct) { |
| 1678 |
int combined_flags = ct->ct_flags | ctinit->ct_flags; |
| 1679 |
if (combined_flags & CT_IS_VOID_PTR) |
| 1680 |
; /* accept "void *" as either source or target */ |
| 1681 |
else if (combined_flags & CT_IS_VOIDCHAR_PTR) { |
| 1682 |
/* for backward compatibility, accept "char *" as either |
| 1683 |
source of target. This is not what C does, though, |
| 1684 |
so emit a warning that will eventually turn into an |
| 1685 |
error. The warning is turned off if both types are |
| 1686 |
pointers to single bytes. */ |
| 1687 |
char *msg = (ct->ct_flags & CT_IS_VOIDCHAR_PTR ? |
| 1688 |
"implicit cast to 'char *' from a different pointer type." |
| 1689 |
"(check that the types are as you expect; use an explicit " |
| 1690 |
"ffi.cast() if they are correct)" : |
| 1691 |
"implicit cast from 'char *' to a different pointer type: " |
| 1692 |
"(check that the types are as you expect; use an explicit " |
| 1693 |
"ffi.cast() if they are correct)"); |
| 1694 |
if ((ct->ct_flags & ctinit->ct_flags & CT_POINTER) && |
| 1695 |
ct->ct_itemdescr->ct_size == 1 && |
| 1696 |
ctinit->ct_itemdescr->ct_size == 1) { |
| 1697 |
/* no error */ |
| 1698 |
} |
| 1699 |
else { |
| 1700 |
PyErr_SetString(PyExc_TypeError, msg); |
| 1701 |
return -1; |
| 1702 |
} |
| 1703 |
} |
| 1704 |
else { |
| 1705 |
expected = "pointer to same type"; |
| 1706 |
goto cannot_convert; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
ptrdata = ((CDataObject *)init)->c_data; |
| 1710 |
|
| 1711 |
*(char **)data = ptrdata; |
| 1712 |
return 0; |
| 1713 |
} |
| 1714 |
if (ct->ct_flags & CT_PRIMITIVE_SIGNED) { |
| 1715 |
PY_LONG_LONG value = _my_PyLong_AsLongLong(init); |
| 1716 |
if (value == -1 && PyErr_Occurred()) |
| 1717 |
return -1; |
| 1718 |
write_raw_integer_data(buf, value, ct->ct_size); |
| 1719 |
if (value != read_raw_signed_data(buf, ct->ct_size)) |
| 1720 |
goto overflow; |
| 1721 |
write_raw_integer_data(data, value, ct->ct_size); |
| 1722 |
return 0; |
| 1723 |
} |
| 1724 |
if (ct->ct_flags & CT_PRIMITIVE_UNSIGNED) { |
| 1725 |
unsigned PY_LONG_LONG value = _my_PyLong_AsUnsignedLongLong(init, 1); |
| 1726 |
if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 1727 |
return -1; |
| 1728 |
if (ct->ct_flags & CT_IS_BOOL) { |
| 1729 |
if (value > 1ULL) /* value != 0 && value != 1 */ |
| 1730 |
goto overflow; |
| 1731 |
} |
| 1732 |
else { |
| 1733 |
write_raw_integer_data(buf, value, ct->ct_size); |
| 1734 |
if (value != read_raw_unsigned_data(buf, ct->ct_size)) |
| 1735 |
goto overflow; |
| 1736 |
} |
| 1737 |
write_raw_integer_data(data, value, ct->ct_size); |
| 1738 |
return 0; |
| 1739 |
} |
| 1740 |
if (ct->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 1741 |
double value; |
| 1742 |
if ((ct->ct_flags & CT_IS_LONGDOUBLE) && |
| 1743 |
CData_Check(init) && |
| 1744 |
(((CDataObject *)init)->c_type->ct_flags & CT_IS_LONGDOUBLE)) { |
| 1745 |
long double lvalue; |
| 1746 |
char *initdata = ((CDataObject *)init)->c_data; |
| 1747 |
/*READ(initdata, sizeof(long double))*/ |
| 1748 |
lvalue = read_raw_longdouble_data(initdata); |
| 1749 |
write_raw_longdouble_data(data, lvalue); |
| 1750 |
return 0; |
| 1751 |
} |
| 1752 |
value = PyFloat_AsDouble(init); |
| 1753 |
if (value == -1.0 && PyErr_Occurred()) |
| 1754 |
return -1; |
| 1755 |
if (!(ct->ct_flags & CT_IS_LONGDOUBLE)) |
| 1756 |
write_raw_float_data(data, value, ct->ct_size); |
| 1757 |
else |
| 1758 |
write_raw_longdouble_data(data, (long double)value); |
| 1759 |
return 0; |
| 1760 |
} |
| 1761 |
if (ct->ct_flags & CT_PRIMITIVE_CHAR) { |
| 1762 |
switch (ct->ct_size) { |
| 1763 |
case sizeof(char): { |
| 1764 |
int res = _convert_to_char(init); |
| 1765 |
if (res < 0) |
| 1766 |
return -1; |
| 1767 |
data[0] = res; |
| 1768 |
return 0; |
| 1769 |
} |
| 1770 |
case 2: { |
| 1771 |
cffi_char16_t res = _convert_to_char16_t(init); |
| 1772 |
if (res == (cffi_char16_t)-1 && PyErr_Occurred()) |
| 1773 |
return -1; |
| 1774 |
*(cffi_char16_t *)data = res; |
| 1775 |
return 0; |
| 1776 |
} |
| 1777 |
case 4: { |
| 1778 |
cffi_char32_t res = _convert_to_char32_t(init); |
| 1779 |
if (res == (cffi_char32_t)-1 && PyErr_Occurred()) |
| 1780 |
return -1; |
| 1781 |
*(cffi_char32_t *)data = res; |
| 1782 |
return 0; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
if (ct->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 1787 |
|
| 1788 |
if (CData_Check(init)) { |
| 1789 |
if (((CDataObject *)init)->c_type == ct && cffi_get_size(ct) >= 0) { |
| 1790 |
memcpy(data, ((CDataObject *)init)->c_data, cffi_get_size(ct)); |
| 1791 |
return 0; |
| 1792 |
} |
| 1793 |
} |
| 1794 |
return convert_struct_from_object(data, ct, init, NULL); |
| 1795 |
} |
| 1796 |
if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) { |
| 1797 |
Py_complex value = PyComplex_AsCComplex(init); |
| 1798 |
if (PyErr_Occurred()) |
| 1799 |
return -1; |
| 1800 |
write_raw_complex_data(data, value, ct->ct_size); |
| 1801 |
return 0; |
| 1802 |
} |
| 1803 |
PyErr_Format(PyExc_SystemError, |
| 1804 |
"convert_from_object: '%s'", ct->ct_name); |
| 1805 |
return -1; |
| 1806 |
|
| 1807 |
overflow: |
| 1808 |
return _convert_overflow(init, ct->ct_name); |
| 1809 |
|
| 1810 |
cannot_convert: |
| 1811 |
return _convert_error(init, ct, expected); |
| 1812 |
} |
| 1813 |
|
| 1814 |
static int |
| 1815 |
convert_from_object_bitfield(char *data, CFieldObject *cf, PyObject *init) |
| 1816 |
{ |
| 1817 |
CTypeDescrObject *ct = cf->cf_type; |
| 1818 |
PY_LONG_LONG fmin, fmax, value = PyLong_AsLongLong(init); |
| 1819 |
unsigned PY_LONG_LONG rawfielddata, rawvalue, rawmask; |
| 1820 |
if (value == -1 && PyErr_Occurred()) |
| 1821 |
return -1; |
| 1822 |
|
| 1823 |
if (ct->ct_flags & CT_PRIMITIVE_SIGNED) { |
| 1824 |
fmin = -(1LL << (cf->cf_bitsize-1)); |
| 1825 |
fmax = (1LL << (cf->cf_bitsize-1)) - 1LL; |
| 1826 |
if (fmax == 0) |
| 1827 |
fmax = 1; /* special case to let "int x:1" receive "1" */ |
| 1828 |
} |
| 1829 |
else { |
| 1830 |
fmin = 0LL; |
| 1831 |
fmax = (PY_LONG_LONG)((1ULL << cf->cf_bitsize) - 1ULL); |
| 1832 |
} |
| 1833 |
if (value < fmin || value > fmax) { |
| 1834 |
/* phew, PyErr_Format does not support "%lld" in Python 2.6 */ |
| 1835 |
PyObject *svalue = NULL, *sfmin = NULL, *sfmax = NULL; |
| 1836 |
PyObject *lfmin = NULL, *lfmax = NULL; |
| 1837 |
svalue = PyObject_Str(init); |
| 1838 |
if (svalue == NULL) goto skip; |
| 1839 |
lfmin = PyLong_FromLongLong(fmin); |
| 1840 |
if (lfmin == NULL) goto skip; |
| 1841 |
sfmin = PyObject_Str(lfmin); |
| 1842 |
if (sfmin == NULL) goto skip; |
| 1843 |
lfmax = PyLong_FromLongLong(fmax); |
| 1844 |
if (lfmax == NULL) goto skip; |
| 1845 |
sfmax = PyObject_Str(lfmax); |
| 1846 |
if (sfmax == NULL) goto skip; |
| 1847 |
PyErr_Format(PyExc_OverflowError, |
| 1848 |
"value %s outside the range allowed by the " |
| 1849 |
"bit field width: %s <= x <= %s", |
| 1850 |
PyUnicode_AsUTF8(svalue), |
| 1851 |
PyUnicode_AsUTF8(sfmin), |
| 1852 |
PyUnicode_AsUTF8(sfmax)); |
| 1853 |
skip: |
| 1854 |
Py_XDECREF(svalue); |
| 1855 |
Py_XDECREF(sfmin); |
| 1856 |
Py_XDECREF(sfmax); |
| 1857 |
Py_XDECREF(lfmin); |
| 1858 |
Py_XDECREF(lfmax); |
| 1859 |
return -1; |
| 1860 |
} |
| 1861 |
|
| 1862 |
rawmask = ((1ULL << cf->cf_bitsize) - 1ULL) << cf->cf_bitshift; |
| 1863 |
rawvalue = ((unsigned PY_LONG_LONG)value) << cf->cf_bitshift; |
| 1864 |
/*WRITE(data, ct->ct_size)*/ |
| 1865 |
rawfielddata = read_raw_unsigned_data(data, ct->ct_size); |
| 1866 |
rawfielddata = (rawfielddata & ~rawmask) | (rawvalue & rawmask); |
| 1867 |
write_raw_integer_data(data, rawfielddata, ct->ct_size); |
| 1868 |
return 0; |
| 1869 |
} |
| 1870 |
|
| 1871 |
static int |
| 1872 |
get_alignment(CTypeDescrObject *ct) |
| 1873 |
{ |
| 1874 |
int align; |
| 1875 |
retry: |
| 1876 |
if ((ct->ct_flags & (CT_PRIMITIVE_ANY|CT_STRUCT|CT_UNION)) && |
| 1877 |
!((ct->ct_flags & CT_IS_OPAQUE) || cffi_check_flag(ct->ct_unrealized_struct_or_union))) { |
| 1878 |
if (cffi_check_flag(ct->ct_lazy_field_list)) { |
| 1879 |
if (force_lazy_struct(ct) < 0) { |
| 1880 |
return -1; |
| 1881 |
} |
| 1882 |
} |
| 1883 |
align = ct->ct_length; |
| 1884 |
} |
| 1885 |
else if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) { |
| 1886 |
struct aligncheck_ptr { char x; char *y; }; |
| 1887 |
align = offsetof(struct aligncheck_ptr, y); |
| 1888 |
} |
| 1889 |
else if (ct->ct_flags & CT_ARRAY) { |
| 1890 |
ct = ct->ct_itemdescr; |
| 1891 |
goto retry; |
| 1892 |
} |
| 1893 |
else { |
| 1894 |
PyErr_Format(PyExc_ValueError, "ctype '%s' is of unknown alignment", |
| 1895 |
ct->ct_name); |
| 1896 |
return -1; |
| 1897 |
} |
| 1898 |
|
| 1899 |
if ((align < 1) || (align & (align-1))) { |
| 1900 |
PyErr_Format(PyExc_SystemError, |
| 1901 |
"found for ctype '%s' bogus alignment '%d'", |
| 1902 |
ct->ct_name, align); |
| 1903 |
return -1; |
| 1904 |
} |
| 1905 |
return align; |
| 1906 |
} |
| 1907 |
|
| 1908 |
static void cdata_dealloc(CDataObject *cd) |
| 1909 |
{ |
| 1910 |
PyObject_ClearWeakRefs((PyObject *) cd); |
| 1911 |
|
| 1912 |
Py_DECREF(cd->c_type); |
| 1913 |
#ifndef CFFI_MEM_LEAK /* never release anything, tests only */ |
| 1914 |
Py_TYPE(cd)->tp_free((PyObject *)cd); |
| 1915 |
#endif |
| 1916 |
} |
| 1917 |
|
| 1918 |
static void cdataowning_dealloc(CDataObject *cd) |
| 1919 |
{ |
| 1920 |
assert(!(cd->c_type->ct_flags & (CT_IS_VOID_PTR | CT_FUNCTIONPTR))); |
| 1921 |
|
| 1922 |
if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) { |
| 1923 |
/* for ffi.new("struct *") */ |
| 1924 |
Py_DECREF(((CDataObject_own_structptr *)cd)->structobj); |
| 1925 |
} |
| 1926 |
#if defined(CFFI_MEM_DEBUG) || defined(CFFI_MEM_LEAK) |
| 1927 |
if (cd->c_type->ct_flags & (CT_PRIMITIVE_ANY | CT_STRUCT | CT_UNION)) { |
| 1928 |
assert(cd->c_type->ct_size >= 0); |
| 1929 |
memset(cd->c_data, 0xDD, cd->c_type->ct_size); |
| 1930 |
} |
| 1931 |
else if (cd->c_type->ct_flags & CT_ARRAY) { |
| 1932 |
Py_ssize_t x = get_array_length(cd); |
| 1933 |
assert(x >= 0); |
| 1934 |
x *= cd->c_type->ct_itemdescr->ct_size; |
| 1935 |
assert(x >= 0); |
| 1936 |
memset(cd->c_data, 0xDD, x); |
| 1937 |
} |
| 1938 |
#endif |
| 1939 |
cdata_dealloc(cd); |
| 1940 |
} |
| 1941 |
|
| 1942 |
static void cdataowninggc_dealloc(CDataObject *cd) |
| 1943 |
{ |
| 1944 |
PyObject_GC_UnTrack(cd); |
| 1945 |
|
| 1946 |
if (cd->c_type->ct_flags & CT_IS_VOID_PTR) { /* a handle */ |
| 1947 |
PyObject *x = ((CDataObject_own_structptr *)cd)->structobj; |
| 1948 |
Py_DECREF(x); |
| 1949 |
} |
| 1950 |
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */ |
| 1951 |
ffi_closure *closure = ((CDataObject_closure *)cd)->closure; |
| 1952 |
PyObject *args = (PyObject *)(closure->user_data); |
| 1953 |
Py_XDECREF(args); |
| 1954 |
#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE |
| 1955 |
if (CFFI_CHECK_FFI_CLOSURE_ALLOC) { |
| 1956 |
ffi_closure_free(closure); |
| 1957 |
} else |
| 1958 |
#endif |
| 1959 |
cffi_closure_free(closure); |
| 1960 |
} |
| 1961 |
else { |
| 1962 |
Py_FatalError("cdata CDataOwningGC_Type with unexpected type flags"); |
| 1963 |
} |
| 1964 |
cdata_dealloc(cd); |
| 1965 |
} |
| 1966 |
|
| 1967 |
static void cdatafrombuf_dealloc(CDataObject *cd) |
| 1968 |
{ |
| 1969 |
Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview; |
| 1970 |
PyObject_GC_UnTrack(cd); |
| 1971 |
cdata_dealloc(cd); |
| 1972 |
|
| 1973 |
PyBuffer_Release(view); |
| 1974 |
PyObject_Free(view); |
| 1975 |
} |
| 1976 |
|
| 1977 |
static int cdataowninggc_traverse(CDataObject *cd, visitproc visit, void *arg) |
| 1978 |
{ |
| 1979 |
if (cd->c_type->ct_flags & CT_IS_VOID_PTR) { /* a handle */ |
| 1980 |
PyObject *x = ((CDataObject_own_structptr *)cd)->structobj; |
| 1981 |
Py_VISIT(x); |
| 1982 |
} |
| 1983 |
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */ |
| 1984 |
ffi_closure *closure = ((CDataObject_closure *)cd)->closure; |
| 1985 |
PyObject *args = (PyObject *)(closure->user_data); |
| 1986 |
Py_VISIT(args); |
| 1987 |
} |
| 1988 |
return 0; |
| 1989 |
} |
| 1990 |
|
| 1991 |
static int cdatafrombuf_traverse(CDataObject *cd, visitproc visit, void *arg) |
| 1992 |
{ |
| 1993 |
Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview; |
| 1994 |
Py_VISIT(view->obj); |
| 1995 |
return 0; |
| 1996 |
} |
| 1997 |
|
| 1998 |
static int cdataowninggc_clear(CDataObject *cd) |
| 1999 |
{ |
| 2000 |
if (cd->c_type->ct_flags & CT_IS_VOID_PTR) { /* a handle */ |
| 2001 |
CDataObject_own_structptr *cd1 = (CDataObject_own_structptr *)cd; |
| 2002 |
PyObject *x = cd1->structobj; |
| 2003 |
Py_INCREF(Py_None); |
| 2004 |
cd1->structobj = Py_None; |
| 2005 |
Py_DECREF(x); |
| 2006 |
} |
| 2007 |
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */ |
| 2008 |
ffi_closure *closure = ((CDataObject_closure *)cd)->closure; |
| 2009 |
PyObject *args = (PyObject *)(closure->user_data); |
| 2010 |
closure->user_data = NULL; |
| 2011 |
Py_XDECREF(args); |
| 2012 |
} |
| 2013 |
return 0; |
| 2014 |
} |
| 2015 |
|
| 2016 |
static int cdatafrombuf_clear(CDataObject *cd) |
| 2017 |
{ |
| 2018 |
Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview; |
| 2019 |
PyBuffer_Release(view); |
| 2020 |
return 0; |
| 2021 |
} |
| 2022 |
|
| 2023 |
/* forward */ |
| 2024 |
static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb, |
| 2025 |
char *objdescr, PyObject *obj, |
| 2026 |
char *extra_error_line); |
| 2027 |
|
| 2028 |
|
| 2029 |
static void gcp_finalize(PyObject *destructor, PyObject *origobj) |
| 2030 |
{ |
| 2031 |
/* NOTE: this decrements the reference count of the two arguments */ |
| 2032 |
|
| 2033 |
if (destructor != NULL) { |
| 2034 |
PyObject *result; |
| 2035 |
PyObject *error_type, *error_value, *error_traceback; |
| 2036 |
|
| 2037 |
/* Save the current exception */ |
| 2038 |
PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 2039 |
|
| 2040 |
result = PyObject_CallFunctionObjArgs(destructor, origobj, NULL); |
| 2041 |
if (result != NULL) { |
| 2042 |
Py_DECREF(result); |
| 2043 |
} |
| 2044 |
else { |
| 2045 |
PyObject *t, *v, *tb; |
| 2046 |
PyErr_Fetch(&t, &v, &tb); |
| 2047 |
/* Don't use error capture here, because it is very much |
| 2048 |
* like errors at __del__(), and these ones are not captured |
| 2049 |
* either */ |
| 2050 |
/* ecap = _cffi_start_error_capture(); */ |
| 2051 |
_my_PyErr_WriteUnraisable(t, v, tb, "From callback for ffi.gc ", |
| 2052 |
origobj, NULL); |
| 2053 |
/* _cffi_stop_error_capture(ecap); */ |
| 2054 |
} |
| 2055 |
Py_DECREF(destructor); |
| 2056 |
|
| 2057 |
/* Restore the saved exception */ |
| 2058 |
PyErr_Restore(error_type, error_value, error_traceback); |
| 2059 |
} |
| 2060 |
Py_XDECREF(origobj); |
| 2061 |
} |
| 2062 |
|
| 2063 |
static void cdatagcp_finalize(CDataObject_gcp *cd) |
| 2064 |
{ |
| 2065 |
PyObject *destructor = cd->destructor; |
| 2066 |
PyObject *origobj = cd->origobj; |
| 2067 |
cd->destructor = NULL; |
| 2068 |
cd->origobj = NULL; |
| 2069 |
gcp_finalize(destructor, origobj); |
| 2070 |
} |
| 2071 |
|
| 2072 |
static void cdatagcp_dealloc(CDataObject_gcp *cd) |
| 2073 |
{ |
| 2074 |
PyObject *destructor = cd->destructor; |
| 2075 |
PyObject *origobj = cd->origobj; |
| 2076 |
PyObject_GC_UnTrack(cd); |
| 2077 |
cdata_dealloc((CDataObject *)cd); |
| 2078 |
|
| 2079 |
gcp_finalize(destructor, origobj); |
| 2080 |
} |
| 2081 |
|
| 2082 |
static int cdatagcp_traverse(CDataObject_gcp *cd, visitproc visit, void *arg) |
| 2083 |
{ |
| 2084 |
Py_VISIT(cd->destructor); |
| 2085 |
Py_VISIT(cd->origobj); |
| 2086 |
return 0; |
| 2087 |
} |
| 2088 |
|
| 2089 |
static PyObject *cdata_float(CDataObject *cd); /*forward*/ |
| 2090 |
|
| 2091 |
static PyObject *convert_cdata_to_enum_string(CDataObject *cd, int both) |
| 2092 |
{ |
| 2093 |
PyObject *d_key, *d_value; |
| 2094 |
CTypeDescrObject *ct = cd->c_type; |
| 2095 |
|
| 2096 |
assert(ct->ct_flags & CT_IS_ENUM); |
| 2097 |
d_key = convert_to_object(cd->c_data, ct); |
| 2098 |
if (d_key == NULL) |
| 2099 |
return NULL; |
| 2100 |
|
| 2101 |
d_value = PyDict_GetItem(PyTuple_GET_ITEM(ct->ct_stuff, 1), d_key); |
| 2102 |
if (d_value != NULL) { |
| 2103 |
if (both) { |
| 2104 |
PyObject *o = PyObject_Str(d_key); |
| 2105 |
if (o == NULL) |
| 2106 |
d_value = NULL; |
| 2107 |
else { |
| 2108 |
d_value = PyUnicode_FromFormat("%s: %s", |
| 2109 |
PyUnicode_AsUTF8(o), |
| 2110 |
PyUnicode_AsUTF8(d_value)); |
| 2111 |
Py_DECREF(o); |
| 2112 |
} |
| 2113 |
} |
| 2114 |
else |
| 2115 |
Py_INCREF(d_value); |
| 2116 |
} |
| 2117 |
else |
| 2118 |
d_value = PyObject_Str(d_key); |
| 2119 |
Py_DECREF(d_key); |
| 2120 |
return d_value; |
| 2121 |
} |
| 2122 |
|
| 2123 |
static PyObject *cdata_repr(CDataObject *cd) |
| 2124 |
{ |
| 2125 |
char *extra; |
| 2126 |
PyObject *result, *s; |
| 2127 |
|
| 2128 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) { |
| 2129 |
if (cd->c_type->ct_flags & CT_IS_ENUM) { |
| 2130 |
s = convert_cdata_to_enum_string(cd, 1); |
| 2131 |
} |
| 2132 |
else if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE) { |
| 2133 |
long double lvalue; |
| 2134 |
char buffer[128]; /* big enough */ |
| 2135 |
/*READ(cd->c_data, sizeof(long double)*/ |
| 2136 |
lvalue = read_raw_longdouble_data(cd->c_data); |
| 2137 |
sprintf(buffer, "%LE", lvalue); |
| 2138 |
s = PyUnicode_FromString(buffer); |
| 2139 |
} |
| 2140 |
else { |
| 2141 |
PyObject *o = convert_to_object(cd->c_data, cd->c_type); |
| 2142 |
if (o == NULL) |
| 2143 |
return NULL; |
| 2144 |
s = PyObject_Repr(o); |
| 2145 |
Py_DECREF(o); |
| 2146 |
} |
| 2147 |
} |
| 2148 |
else if ((cd->c_type->ct_flags & CT_ARRAY) && cd->c_type->ct_length < 0) { |
| 2149 |
s = PyUnicode_FromFormat("sliced length %zd", get_array_length(cd)); |
| 2150 |
} |
| 2151 |
else { |
| 2152 |
if (cd->c_data != NULL) { |
| 2153 |
s = PyUnicode_FromFormat("%p", cd->c_data); |
| 2154 |
} |
| 2155 |
else |
| 2156 |
s = PyUnicode_FromString("NULL"); |
| 2157 |
} |
| 2158 |
if (s == NULL) |
| 2159 |
return NULL; |
| 2160 |
/* it's slightly confusing to get "<cdata 'struct foo' 0x...>" because the |
| 2161 |
struct foo is not owned. Trying to make it clearer, write in this |
| 2162 |
case "<cdata 'struct foo &' 0x...>". */ |
| 2163 |
if (cd->c_type->ct_flags & (CT_STRUCT|CT_UNION)) |
| 2164 |
extra = " &"; |
| 2165 |
else |
| 2166 |
extra = ""; |
| 2167 |
result = PyUnicode_FromFormat("<cdata '%s%s' %s>", |
| 2168 |
cd->c_type->ct_name, extra, |
| 2169 |
PyUnicode_AsUTF8(s)); |
| 2170 |
Py_DECREF(s); |
| 2171 |
return result; |
| 2172 |
} |
| 2173 |
|
| 2174 |
static PyObject *_cdata_repr2(CDataObject *cd, char *text, PyObject *x) |
| 2175 |
{ |
| 2176 |
PyObject *res, *s = PyObject_Repr(x); |
| 2177 |
if (s == NULL) |
| 2178 |
return NULL; |
| 2179 |
res = PyUnicode_FromFormat("<cdata '%s' %s %s>", |
| 2180 |
cd->c_type->ct_name, text, PyUnicode_AsUTF8(s)); |
| 2181 |
Py_DECREF(s); |
| 2182 |
return res; |
| 2183 |
} |
| 2184 |
|
| 2185 |
static Py_ssize_t _cdata_var_byte_size(CDataObject *cd) |
| 2186 |
{ |
| 2187 |
/* If 'cd' is a 'struct foo' or 'struct foo *' allocated with |
| 2188 |
ffi.new(), and if the struct foo contains a varsize array, |
| 2189 |
then return the real allocated size. Otherwise, return -1. */ |
| 2190 |
if (!CDataOwn_Check(cd)) |
| 2191 |
return -1; |
| 2192 |
|
| 2193 |
if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) { |
| 2194 |
cd = (CDataObject *)((CDataObject_own_structptr *)cd)->structobj; |
| 2195 |
} |
| 2196 |
if (cd->c_type->ct_flags_mut & CT_WITH_VAR_ARRAY) { |
| 2197 |
return ((CDataObject_own_length *)cd)->length; |
| 2198 |
} |
| 2199 |
return -1; |
| 2200 |
} |
| 2201 |
|
| 2202 |
static PyObject *_frombuf_repr(CDataObject *cd, const char *cd_type_name) |
| 2203 |
{ |
| 2204 |
Py_buffer *view = ((CDataObject_frombuf *)cd)->bufferview; |
| 2205 |
const char *obj_tp_name; |
| 2206 |
if (view->obj == NULL) { |
| 2207 |
return PyUnicode_FromFormat( |
| 2208 |
"<cdata '%s' buffer RELEASED>", |
| 2209 |
cd_type_name); |
| 2210 |
} |
| 2211 |
|
| 2212 |
obj_tp_name = Py_TYPE(view->obj)->tp_name; |
| 2213 |
if (cd->c_type->ct_flags & CT_ARRAY) |
| 2214 |
{ |
| 2215 |
Py_ssize_t buflen = get_array_length(cd); |
| 2216 |
return PyUnicode_FromFormat( |
| 2217 |
"<cdata '%s' buffer len %zd from '%.200s' object>", |
| 2218 |
cd_type_name, |
| 2219 |
buflen, |
| 2220 |
obj_tp_name); |
| 2221 |
} |
| 2222 |
else |
| 2223 |
{ |
| 2224 |
return PyUnicode_FromFormat( |
| 2225 |
"<cdata '%s' buffer from '%.200s' object>", |
| 2226 |
cd_type_name, |
| 2227 |
obj_tp_name); |
| 2228 |
} |
| 2229 |
} |
| 2230 |
|
| 2231 |
static Py_ssize_t cdataowning_size_bytes(CDataObject *cd) |
| 2232 |
{ |
| 2233 |
Py_ssize_t size = _cdata_var_byte_size(cd); |
| 2234 |
if (size < 0) { |
| 2235 |
if (cd->c_type->ct_flags & CT_POINTER) |
| 2236 |
size = cd->c_type->ct_itemdescr->ct_size; |
| 2237 |
else if (cd->c_type->ct_flags & CT_ARRAY) |
| 2238 |
size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size; |
| 2239 |
else |
| 2240 |
size = cffi_get_size(cd->c_type); |
| 2241 |
} |
| 2242 |
return size; |
| 2243 |
} |
| 2244 |
|
| 2245 |
static PyObject *cdataowning_repr(CDataObject *cd) |
| 2246 |
{ |
| 2247 |
Py_ssize_t size = cdataowning_size_bytes(cd); |
| 2248 |
return PyUnicode_FromFormat("<cdata '%s' owning %zd bytes>", |
| 2249 |
cd->c_type->ct_name, size); |
| 2250 |
} |
| 2251 |
|
| 2252 |
static PyObject *cdataowninggc_repr(CDataObject *cd) |
| 2253 |
{ |
| 2254 |
if (cd->c_type->ct_flags & CT_IS_VOID_PTR) { /* a handle */ |
| 2255 |
PyObject *x = ((CDataObject_own_structptr *)cd)->structobj; |
| 2256 |
return _cdata_repr2(cd, "handle to", x); |
| 2257 |
} |
| 2258 |
else if (cd->c_type->ct_flags & CT_FUNCTIONPTR) { /* a callback */ |
| 2259 |
ffi_closure *closure = ((CDataObject_closure *)cd)->closure; |
| 2260 |
PyObject *args = (PyObject *)closure->user_data; |
| 2261 |
if (args == NULL) |
| 2262 |
return cdata_repr(cd); |
| 2263 |
else |
| 2264 |
return _cdata_repr2(cd, "calling", PyTuple_GET_ITEM(args, 1)); |
| 2265 |
} |
| 2266 |
return cdataowning_repr(cd); /* but should be unreachable */ |
| 2267 |
} |
| 2268 |
|
| 2269 |
static PyObject *cdatafrombuf_repr(CDataObject *cd) |
| 2270 |
{ |
| 2271 |
return _frombuf_repr(cd, cd->c_type->ct_name); |
| 2272 |
} |
| 2273 |
|
| 2274 |
static int cdata_nonzero(CDataObject *cd) |
| 2275 |
{ |
| 2276 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_ANY) { |
| 2277 |
if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED | |
| 2278 |
CT_PRIMITIVE_UNSIGNED | |
| 2279 |
CT_PRIMITIVE_CHAR)) |
| 2280 |
return read_raw_unsigned_data(cd->c_data, cd->c_type->ct_size) != 0; |
| 2281 |
|
| 2282 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 2283 |
if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE) |
| 2284 |
return read_raw_longdouble_data(cd->c_data) != 0.0; |
| 2285 |
return read_raw_float_data(cd->c_data, cd->c_type->ct_size) != 0.0; |
| 2286 |
} |
| 2287 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) { |
| 2288 |
Py_complex value = read_raw_complex_data(cd->c_data, |
| 2289 |
cd->c_type->ct_size); |
| 2290 |
return value.real != 0.0 || value.imag != 0.0; |
| 2291 |
} |
| 2292 |
} |
| 2293 |
return cd->c_data != NULL; |
| 2294 |
} |
| 2295 |
|
| 2296 |
static PyObject *cdata_int(CDataObject *cd) |
| 2297 |
{ |
| 2298 |
if ((cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_FITS_LONG)) |
| 2299 |
== (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_FITS_LONG)) { |
| 2300 |
/* this case is to handle enums, but also serves as a slight |
| 2301 |
performance improvement for some other primitive types */ |
| 2302 |
long value; |
| 2303 |
/*READ(cd->c_data, cd->c_type->ct_size)*/ |
| 2304 |
value = (long)read_raw_signed_data(cd->c_data, cd->c_type->ct_size); |
| 2305 |
return PyLong_FromLong(value); |
| 2306 |
} |
| 2307 |
if (cd->c_type->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED)) { |
| 2308 |
PyObject *result = convert_to_object(cd->c_data, cd->c_type); |
| 2309 |
if (result != NULL && PyBool_Check(result)) |
| 2310 |
result = PyLong_FromLong(PyLong_AsLong(result)); |
| 2311 |
return result; |
| 2312 |
} |
| 2313 |
else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) { |
| 2314 |
/*READ(cd->c_data, cd->c_type->ct_size)*/ |
| 2315 |
switch (cd->c_type->ct_size) { |
| 2316 |
case sizeof(char): |
| 2317 |
return PyLong_FromLong((unsigned char)cd->c_data[0]); |
| 2318 |
case 2: |
| 2319 |
return PyLong_FromLong((long)*(cffi_char16_t *)cd->c_data); |
| 2320 |
case 4: |
| 2321 |
if (cd->c_type->ct_flags & CT_IS_SIGNED_WCHAR) |
| 2322 |
return PyLong_FromLong((long)*(int32_t *)cd->c_data); |
| 2323 |
else if (sizeof(long) > 4) |
| 2324 |
return PyLong_FromLong(*(uint32_t *)cd->c_data); |
| 2325 |
else |
| 2326 |
return PyLong_FromUnsignedLong(*(uint32_t *)cd->c_data); |
| 2327 |
} |
| 2328 |
} |
| 2329 |
else if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 2330 |
PyObject *o = cdata_float(cd); |
| 2331 |
PyObject *r = o ? PyNumber_Long(o) : NULL; |
| 2332 |
Py_XDECREF(o); |
| 2333 |
return r; |
| 2334 |
} |
| 2335 |
PyErr_Format(PyExc_TypeError, "int() not supported on cdata '%s'", |
| 2336 |
cd->c_type->ct_name); |
| 2337 |
return NULL; |
| 2338 |
} |
| 2339 |
|
| 2340 |
static PyObject *cdata_float(CDataObject *cd) |
| 2341 |
{ |
| 2342 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 2343 |
double value; |
| 2344 |
/*READ(cd->c_data, cd->c_type->ct_size)*/ |
| 2345 |
if (!(cd->c_type->ct_flags & CT_IS_LONGDOUBLE)) { |
| 2346 |
value = read_raw_float_data(cd->c_data, cd->c_type->ct_size); |
| 2347 |
} |
| 2348 |
else { |
| 2349 |
value = (double)read_raw_longdouble_data(cd->c_data); |
| 2350 |
} |
| 2351 |
return PyFloat_FromDouble(value); |
| 2352 |
} |
| 2353 |
PyErr_Format(PyExc_TypeError, "float() not supported on cdata '%s'", |
| 2354 |
cd->c_type->ct_name); |
| 2355 |
return NULL; |
| 2356 |
} |
| 2357 |
|
| 2358 |
static PyObject *cdata_richcompare(PyObject *v, PyObject *w, int op) |
| 2359 |
{ |
| 2360 |
int v_is_ptr, w_is_ptr; |
| 2361 |
PyObject *pyres; |
| 2362 |
|
| 2363 |
assert(CData_Check(v)); |
| 2364 |
|
| 2365 |
/* Comparisons involving a primitive cdata work differently than |
| 2366 |
* comparisons involving a struct/array/pointer. |
| 2367 |
* |
| 2368 |
* If v or w is a struct/array/pointer, then the other must be too |
| 2369 |
* (otherwise we return NotImplemented and leave the case to |
| 2370 |
* Python). If both are, then we compare the addresses. |
| 2371 |
* |
| 2372 |
* If v and/or w is a primitive cdata, then we convert the cdata(s) |
| 2373 |
* to regular Python objects and redo the comparison there. |
| 2374 |
*/ |
| 2375 |
|
| 2376 |
v_is_ptr = !(((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY); |
| 2377 |
w_is_ptr = CData_Check(w) && |
| 2378 |
!(((CDataObject *)w)->c_type->ct_flags & CT_PRIMITIVE_ANY); |
| 2379 |
|
| 2380 |
if (v_is_ptr && w_is_ptr) { |
| 2381 |
int res; |
| 2382 |
char *v_cdata = ((CDataObject *)v)->c_data; |
| 2383 |
char *w_cdata = ((CDataObject *)w)->c_data; |
| 2384 |
|
| 2385 |
switch (op) { |
| 2386 |
case Py_EQ: res = (v_cdata == w_cdata); break; |
| 2387 |
case Py_NE: res = (v_cdata != w_cdata); break; |
| 2388 |
case Py_LT: res = (v_cdata < w_cdata); break; |
| 2389 |
case Py_LE: res = (v_cdata <= w_cdata); break; |
| 2390 |
case Py_GT: res = (v_cdata > w_cdata); break; |
| 2391 |
case Py_GE: res = (v_cdata >= w_cdata); break; |
| 2392 |
default: res = -1; |
| 2393 |
} |
| 2394 |
pyres = res ? Py_True : Py_False; |
| 2395 |
} |
| 2396 |
else if (v_is_ptr || w_is_ptr) { |
| 2397 |
pyres = Py_NotImplemented; |
| 2398 |
} |
| 2399 |
else { |
| 2400 |
PyObject *aa[2]; |
| 2401 |
int i; |
| 2402 |
|
| 2403 |
aa[0] = v; Py_INCREF(v); |
| 2404 |
aa[1] = w; Py_INCREF(w); |
| 2405 |
pyres = NULL; |
| 2406 |
|
| 2407 |
for (i = 0; i < 2; i++) { |
| 2408 |
v = aa[i]; |
| 2409 |
if (!CData_Check(v)) |
| 2410 |
continue; |
| 2411 |
w = convert_to_object(((CDataObject *)v)->c_data, |
| 2412 |
((CDataObject *)v)->c_type); |
| 2413 |
if (w == NULL) |
| 2414 |
goto error; |
| 2415 |
if (CData_Check(w)) { |
| 2416 |
Py_DECREF(w); |
| 2417 |
PyErr_Format(PyExc_NotImplementedError, |
| 2418 |
"cannot use <cdata '%s'> in a comparison", |
| 2419 |
((CDataObject *)v)->c_type->ct_name); |
| 2420 |
goto error; |
| 2421 |
} |
| 2422 |
aa[i] = w; |
| 2423 |
Py_DECREF(v); |
| 2424 |
} |
| 2425 |
pyres = PyObject_RichCompare(aa[0], aa[1], op); |
| 2426 |
error: |
| 2427 |
Py_DECREF(aa[1]); |
| 2428 |
Py_DECREF(aa[0]); |
| 2429 |
return pyres; |
| 2430 |
} |
| 2431 |
|
| 2432 |
Py_INCREF(pyres); |
| 2433 |
return pyres; |
| 2434 |
} |
| 2435 |
|
| 2436 |
static Py_hash_t cdata_hash(PyObject *v) |
| 2437 |
{ |
| 2438 |
if (((CDataObject *)v)->c_type->ct_flags & CT_PRIMITIVE_ANY) { |
| 2439 |
PyObject *vv = convert_to_object(((CDataObject *)v)->c_data, |
| 2440 |
((CDataObject *)v)->c_type); |
| 2441 |
if (vv == NULL) |
| 2442 |
return -1; |
| 2443 |
if (!CData_Check(vv)) { |
| 2444 |
Py_hash_t hash = PyObject_Hash(vv); |
| 2445 |
Py_DECREF(vv); |
| 2446 |
return hash; |
| 2447 |
} |
| 2448 |
Py_DECREF(vv); |
| 2449 |
} |
| 2450 |
#if PY_VERSION_HEX < 0x030D0000 |
| 2451 |
return _Py_HashPointer(((CDataObject *)v)->c_data); |
| 2452 |
#else |
| 2453 |
return Py_HashPointer(((CDataObject *)v)->c_data); |
| 2454 |
#endif |
| 2455 |
} |
| 2456 |
|
| 2457 |
static Py_ssize_t |
| 2458 |
cdata_length(CDataObject *cd) |
| 2459 |
{ |
| 2460 |
if (cd->c_type->ct_flags & CT_ARRAY) { |
| 2461 |
return get_array_length(cd); |
| 2462 |
} |
| 2463 |
PyErr_Format(PyExc_TypeError, "cdata of type '%s' has no len()", |
| 2464 |
cd->c_type->ct_name); |
| 2465 |
return -1; |
| 2466 |
} |
| 2467 |
|
| 2468 |
static char * |
| 2469 |
_cdata_get_indexed_ptr(CDataObject *cd, PyObject *key) |
| 2470 |
{ |
| 2471 |
Py_ssize_t i = PyNumber_AsSsize_t(key, PyExc_IndexError); |
| 2472 |
if (i == -1 && PyErr_Occurred()) |
| 2473 |
return NULL; |
| 2474 |
|
| 2475 |
if (cd->c_type->ct_flags & CT_POINTER) { |
| 2476 |
if (CDataOwn_Check(cd)) { |
| 2477 |
if (i != 0) { |
| 2478 |
PyErr_Format(PyExc_IndexError, |
| 2479 |
"cdata '%s' can only be indexed by 0", |
| 2480 |
cd->c_type->ct_name); |
| 2481 |
return NULL; |
| 2482 |
} |
| 2483 |
} |
| 2484 |
else { |
| 2485 |
if (cd->c_data == NULL) { |
| 2486 |
PyErr_Format(PyExc_RuntimeError, |
| 2487 |
"cannot dereference null pointer from cdata '%s'", |
| 2488 |
cd->c_type->ct_name); |
| 2489 |
return NULL; |
| 2490 |
} |
| 2491 |
} |
| 2492 |
} |
| 2493 |
else if (cd->c_type->ct_flags & CT_ARRAY) { |
| 2494 |
if (i < 0) { |
| 2495 |
PyErr_SetString(PyExc_IndexError, |
| 2496 |
"negative index"); |
| 2497 |
return NULL; |
| 2498 |
} |
| 2499 |
if (i >= get_array_length(cd)) { |
| 2500 |
PyErr_Format(PyExc_IndexError, |
| 2501 |
"index too large for cdata '%s' (expected %zd < %zd)", |
| 2502 |
cd->c_type->ct_name, |
| 2503 |
i, get_array_length(cd)); |
| 2504 |
return NULL; |
| 2505 |
} |
| 2506 |
} |
| 2507 |
else { |
| 2508 |
PyErr_Format(PyExc_TypeError, "cdata of type '%s' cannot be indexed", |
| 2509 |
cd->c_type->ct_name); |
| 2510 |
return NULL; |
| 2511 |
} |
| 2512 |
return cd->c_data + i * cffi_get_size(cd->c_type->ct_itemdescr); |
| 2513 |
} |
| 2514 |
|
| 2515 |
static PyObject * |
| 2516 |
new_array_type(CTypeDescrObject *ctptr, Py_ssize_t length); /* forward */ |
| 2517 |
|
| 2518 |
static CTypeDescrObject * |
| 2519 |
_cdata_getslicearg(CDataObject *cd, PySliceObject *slice, Py_ssize_t bounds[]) |
| 2520 |
{ |
| 2521 |
Py_ssize_t start, stop; |
| 2522 |
CTypeDescrObject *ct; |
| 2523 |
|
| 2524 |
start = PyLong_AsSsize_t(slice->start); |
| 2525 |
if (start == -1 && PyErr_Occurred()) { |
| 2526 |
if (slice->start == Py_None) |
| 2527 |
PyErr_SetString(PyExc_IndexError, "slice start must be specified"); |
| 2528 |
return NULL; |
| 2529 |
} |
| 2530 |
stop = PyLong_AsSsize_t(slice->stop); |
| 2531 |
if (stop == -1 && PyErr_Occurred()) { |
| 2532 |
if (slice->stop == Py_None) |
| 2533 |
PyErr_SetString(PyExc_IndexError, "slice stop must be specified"); |
| 2534 |
return NULL; |
| 2535 |
} |
| 2536 |
if (slice->step != Py_None) { |
| 2537 |
PyErr_SetString(PyExc_IndexError, "slice with step not supported"); |
| 2538 |
return NULL; |
| 2539 |
} |
| 2540 |
if (start > stop) { |
| 2541 |
PyErr_SetString(PyExc_IndexError, "slice start > stop"); |
| 2542 |
return NULL; |
| 2543 |
} |
| 2544 |
|
| 2545 |
ct = cd->c_type; |
| 2546 |
if (ct->ct_flags & CT_ARRAY) { |
| 2547 |
if (start < 0) { |
| 2548 |
PyErr_SetString(PyExc_IndexError, |
| 2549 |
"negative index"); |
| 2550 |
return NULL; |
| 2551 |
} |
| 2552 |
if (stop > get_array_length(cd)) { |
| 2553 |
PyErr_Format(PyExc_IndexError, |
| 2554 |
"index too large (expected %zd <= %zd)", |
| 2555 |
stop, get_array_length(cd)); |
| 2556 |
return NULL; |
| 2557 |
} |
| 2558 |
ct = (CTypeDescrObject *)ct->ct_stuff; |
| 2559 |
} |
| 2560 |
else if (!(ct->ct_flags & CT_POINTER)) { |
| 2561 |
PyErr_Format(PyExc_TypeError, "cdata of type '%s' cannot be indexed", |
| 2562 |
ct->ct_name); |
| 2563 |
return NULL; |
| 2564 |
} |
| 2565 |
|
| 2566 |
bounds[0] = start; |
| 2567 |
bounds[1] = stop - start; |
| 2568 |
return ct; |
| 2569 |
} |
| 2570 |
|
| 2571 |
static PyObject * |
| 2572 |
cdata_slice(CDataObject *cd, PySliceObject *slice) |
| 2573 |
{ |
| 2574 |
char *cdata; |
| 2575 |
Py_ssize_t bounds[2]; |
| 2576 |
CTypeDescrObject *ct = _cdata_getslicearg(cd, slice, bounds); |
| 2577 |
if (ct == NULL) |
| 2578 |
return NULL; |
| 2579 |
CTypeDescrObject *array_type = NULL; |
| 2580 |
Py_BEGIN_CRITICAL_SECTION(ct); |
| 2581 |
array_type = (CTypeDescrObject *)ct->ct_stuff; |
| 2582 |
if (ct->ct_stuff == NULL) { |
| 2583 |
array_type = (CTypeDescrObject *)new_array_type(ct, -1); |
| 2584 |
ct->ct_stuff = (PyObject *)array_type; |
| 2585 |
} |
| 2586 |
Py_END_CRITICAL_SECTION(); |
| 2587 |
|
| 2588 |
if (array_type == NULL) { |
| 2589 |
return NULL; |
| 2590 |
} |
| 2591 |
|
| 2592 |
cdata = cd->c_data + array_type->ct_itemdescr->ct_size * bounds[0]; |
| 2593 |
return new_sized_cdata(cdata, array_type, bounds[1]); |
| 2594 |
} |
| 2595 |
|
| 2596 |
static int |
| 2597 |
cdata_ass_slice(CDataObject *cd, PySliceObject *slice, PyObject *v) |
| 2598 |
{ |
| 2599 |
Py_ssize_t bounds[2], i, length, itemsize; |
| 2600 |
PyObject *it, *item; |
| 2601 |
PyObject *(*iternext)(PyObject *); |
| 2602 |
char *cdata; |
| 2603 |
int err; |
| 2604 |
CTypeDescrObject *ct = _cdata_getslicearg(cd, slice, bounds); |
| 2605 |
if (ct == NULL) |
| 2606 |
return -1; |
| 2607 |
ct = ct->ct_itemdescr; |
| 2608 |
itemsize = ct->ct_size; |
| 2609 |
cdata = cd->c_data + itemsize * bounds[0]; |
| 2610 |
length = bounds[1]; |
| 2611 |
|
| 2612 |
if (v == NULL) { |
| 2613 |
PyErr_SetString(PyExc_TypeError, |
| 2614 |
"'del x[n]' not supported for cdata objects"); |
| 2615 |
return -1; |
| 2616 |
} |
| 2617 |
if (CData_Check(v)) { |
| 2618 |
CTypeDescrObject *ctv = ((CDataObject *)v)->c_type; |
| 2619 |
if ((ctv->ct_flags & CT_ARRAY) && (ctv->ct_itemdescr == ct) && |
| 2620 |
(get_array_length((CDataObject *)v) == length)) { |
| 2621 |
/* fast path: copying from exactly the correct type */ |
| 2622 |
memmove(cdata, ((CDataObject *)v)->c_data, itemsize * length); |
| 2623 |
return 0; |
| 2624 |
} |
| 2625 |
} |
| 2626 |
|
| 2627 |
/* A fast path for <char[]>[0:N] = b"somestring" or bytearray, which |
| 2628 |
also adds support for Python 3: otherwise, you get integers while |
| 2629 |
enumerating the string, and you can't set them to characters :-/ |
| 2630 |
*/ |
| 2631 |
if ((ct->ct_flags & CT_PRIMITIVE_CHAR) && itemsize == sizeof(char)) { |
| 2632 |
char *src; |
| 2633 |
Py_ssize_t srclen; |
| 2634 |
if (PyBytes_Check(v)) { |
| 2635 |
srclen = PyBytes_GET_SIZE(v); |
| 2636 |
src = PyBytes_AS_STRING(v); |
| 2637 |
} |
| 2638 |
else if (PyByteArray_Check(v)) { |
| 2639 |
srclen = PyByteArray_GET_SIZE(v); |
| 2640 |
src = PyByteArray_AS_STRING(v); |
| 2641 |
} |
| 2642 |
else |
| 2643 |
goto other_types; |
| 2644 |
|
| 2645 |
if (srclen != length) { |
| 2646 |
PyErr_Format(PyExc_ValueError, |
| 2647 |
"need a string of length %zd, got %zd", |
| 2648 |
length, srclen); |
| 2649 |
return -1; |
| 2650 |
} |
| 2651 |
memcpy(cdata, src, length); |
| 2652 |
return 0; |
| 2653 |
} |
| 2654 |
other_types: |
| 2655 |
|
| 2656 |
it = PyObject_GetIter(v); |
| 2657 |
if (it == NULL) |
| 2658 |
return -1; |
| 2659 |
iternext = *it->ob_type->tp_iternext; |
| 2660 |
|
| 2661 |
for (i = 0; i < length; i++) { |
| 2662 |
item = iternext(it); |
| 2663 |
if (item == NULL) { |
| 2664 |
if (!PyErr_Occurred()) |
| 2665 |
PyErr_Format(PyExc_ValueError, |
| 2666 |
"need %zd values to unpack, got %zd", |
| 2667 |
length, i); |
| 2668 |
goto error; |
| 2669 |
} |
| 2670 |
err = convert_from_object(cdata, ct, item); |
| 2671 |
Py_DECREF(item); |
| 2672 |
if (err < 0) |
| 2673 |
goto error; |
| 2674 |
|
| 2675 |
cdata += itemsize; |
| 2676 |
} |
| 2677 |
item = iternext(it); |
| 2678 |
if (item != NULL) { |
| 2679 |
Py_DECREF(item); |
| 2680 |
PyErr_Format(PyExc_ValueError, |
| 2681 |
"got more than %zd values to unpack", length); |
| 2682 |
} |
| 2683 |
error: |
| 2684 |
Py_DECREF(it); |
| 2685 |
return PyErr_Occurred() ? -1 : 0; |
| 2686 |
} |
| 2687 |
|
| 2688 |
static PyObject * |
| 2689 |
cdataowning_subscript(CDataObject *cd, PyObject *key) |
| 2690 |
{ |
| 2691 |
char *c; |
| 2692 |
if (PySlice_Check(key)) |
| 2693 |
return cdata_slice(cd, (PySliceObject *)key); |
| 2694 |
|
| 2695 |
c = _cdata_get_indexed_ptr(cd, key); |
| 2696 |
/* use 'mp_subscript' instead of 'sq_item' because we don't want |
| 2697 |
negative indexes to be corrected automatically */ |
| 2698 |
if (c == NULL && PyErr_Occurred()) |
| 2699 |
return NULL; |
| 2700 |
|
| 2701 |
if (cd->c_type->ct_flags & CT_IS_PTR_TO_OWNED) { |
| 2702 |
PyObject *res = ((CDataObject_own_structptr *)cd)->structobj; |
| 2703 |
Py_INCREF(res); |
| 2704 |
return res; |
| 2705 |
} |
| 2706 |
else { |
| 2707 |
return convert_to_object(c, cd->c_type->ct_itemdescr); |
| 2708 |
} |
| 2709 |
} |
| 2710 |
|
| 2711 |
static PyObject * |
| 2712 |
cdata_subscript(CDataObject *cd, PyObject *key) |
| 2713 |
{ |
| 2714 |
char *c; |
| 2715 |
if (PySlice_Check(key)) |
| 2716 |
return cdata_slice(cd, (PySliceObject *)key); |
| 2717 |
|
| 2718 |
c = _cdata_get_indexed_ptr(cd, key); |
| 2719 |
/* use 'mp_subscript' instead of 'sq_item' because we don't want |
| 2720 |
negative indexes to be corrected automatically */ |
| 2721 |
if (c == NULL && PyErr_Occurred()) |
| 2722 |
return NULL; |
| 2723 |
return convert_to_object(c, cd->c_type->ct_itemdescr); |
| 2724 |
} |
| 2725 |
|
| 2726 |
static int |
| 2727 |
cdata_ass_sub(CDataObject *cd, PyObject *key, PyObject *v) |
| 2728 |
{ |
| 2729 |
char *c; |
| 2730 |
CTypeDescrObject *ctitem; |
| 2731 |
if (PySlice_Check(key)) |
| 2732 |
return cdata_ass_slice(cd, (PySliceObject *)key, v); |
| 2733 |
|
| 2734 |
c = _cdata_get_indexed_ptr(cd, key); |
| 2735 |
ctitem = cd->c_type->ct_itemdescr; |
| 2736 |
/* use 'mp_ass_subscript' instead of 'sq_ass_item' because we don't want |
| 2737 |
negative indexes to be corrected automatically */ |
| 2738 |
if (c == NULL && PyErr_Occurred()) |
| 2739 |
return -1; |
| 2740 |
if (v == NULL) { |
| 2741 |
PyErr_SetString(PyExc_TypeError, |
| 2742 |
"'del x[n]' not supported for cdata objects"); |
| 2743 |
return -1; |
| 2744 |
} |
| 2745 |
return convert_from_object(c, ctitem, v); |
| 2746 |
} |
| 2747 |
|
| 2748 |
static PyObject * |
| 2749 |
_cdata_add_or_sub(PyObject *v, PyObject *w, int sign) |
| 2750 |
{ |
| 2751 |
Py_ssize_t i, itemsize; |
| 2752 |
CDataObject *cd; |
| 2753 |
CTypeDescrObject *ctptr; |
| 2754 |
|
| 2755 |
if (!CData_Check(v)) { |
| 2756 |
PyObject *swap; |
| 2757 |
assert(CData_Check(w)); |
| 2758 |
if (sign != 1) |
| 2759 |
goto not_implemented; |
| 2760 |
swap = v; |
| 2761 |
v = w; |
| 2762 |
w = swap; |
| 2763 |
} |
| 2764 |
|
| 2765 |
i = PyNumber_AsSsize_t(w, PyExc_OverflowError); |
| 2766 |
if (i == -1 && PyErr_Occurred()) |
| 2767 |
return NULL; |
| 2768 |
i *= sign; |
| 2769 |
|
| 2770 |
cd = (CDataObject *)v; |
| 2771 |
if (cd->c_type->ct_flags & CT_POINTER) |
| 2772 |
ctptr = cd->c_type; |
| 2773 |
else if (cd->c_type->ct_flags & CT_ARRAY) { |
| 2774 |
ctptr = (CTypeDescrObject *)cd->c_type->ct_stuff; |
| 2775 |
} |
| 2776 |
else { |
| 2777 |
PyErr_Format(PyExc_TypeError, "cannot add a cdata '%s' and a number", |
| 2778 |
cd->c_type->ct_name); |
| 2779 |
return NULL; |
| 2780 |
} |
| 2781 |
itemsize = cffi_get_size(ctptr->ct_itemdescr); |
| 2782 |
if (itemsize < 0) { |
| 2783 |
if (ctptr->ct_flags & CT_IS_VOID_PTR) { |
| 2784 |
itemsize = 1; |
| 2785 |
} |
| 2786 |
else { |
| 2787 |
PyErr_Format(PyExc_TypeError, |
| 2788 |
"ctype '%s' points to items of unknown size", |
| 2789 |
cd->c_type->ct_name); |
| 2790 |
return NULL; |
| 2791 |
} |
| 2792 |
} |
| 2793 |
return new_simple_cdata(cd->c_data + i * itemsize, ctptr); |
| 2794 |
|
| 2795 |
not_implemented: |
| 2796 |
Py_INCREF(Py_NotImplemented); |
| 2797 |
return Py_NotImplemented; |
| 2798 |
} |
| 2799 |
|
| 2800 |
static PyObject * |
| 2801 |
cdata_add(PyObject *v, PyObject *w) |
| 2802 |
{ |
| 2803 |
return _cdata_add_or_sub(v, w, +1); |
| 2804 |
} |
| 2805 |
|
| 2806 |
static PyObject * |
| 2807 |
cdata_sub(PyObject *v, PyObject *w) |
| 2808 |
{ |
| 2809 |
if (CData_Check(v) && CData_Check(w)) { |
| 2810 |
CDataObject *cdv = (CDataObject *)v; |
| 2811 |
CDataObject *cdw = (CDataObject *)w; |
| 2812 |
CTypeDescrObject *ct = cdw->c_type; |
| 2813 |
Py_ssize_t diff, itemsize; |
| 2814 |
|
| 2815 |
if (ct->ct_flags & CT_ARRAY) /* ptr_to_T - array_of_T: ok */ |
| 2816 |
ct = (CTypeDescrObject *)ct->ct_stuff; |
| 2817 |
|
| 2818 |
if (ct != cdv->c_type || !(ct->ct_flags & CT_POINTER) || |
| 2819 |
(ct->ct_itemdescr->ct_size <= 0 && |
| 2820 |
!(ct->ct_flags & CT_IS_VOID_PTR))) { |
| 2821 |
PyErr_Format(PyExc_TypeError, |
| 2822 |
"cannot subtract cdata '%s' and cdata '%s'", |
| 2823 |
cdv->c_type->ct_name, ct->ct_name); |
| 2824 |
return NULL; |
| 2825 |
} |
| 2826 |
itemsize = ct->ct_itemdescr->ct_size; |
| 2827 |
diff = cdv->c_data - cdw->c_data; |
| 2828 |
if (itemsize > 1) { |
| 2829 |
if (diff % itemsize) { |
| 2830 |
PyErr_SetString(PyExc_ValueError, |
| 2831 |
"pointer subtraction: the distance between the two " |
| 2832 |
"pointers is not a multiple of the item size"); |
| 2833 |
return NULL; |
| 2834 |
} |
| 2835 |
diff = diff / itemsize; |
| 2836 |
} |
| 2837 |
return PyLong_FromSsize_t(diff); |
| 2838 |
} |
| 2839 |
|
| 2840 |
return _cdata_add_or_sub(v, w, -1); |
| 2841 |
} |
| 2842 |
|
| 2843 |
static void |
| 2844 |
_cdata_attr_errmsg(char *errmsg, CDataObject *cd, PyObject *attr) |
| 2845 |
{ |
| 2846 |
const char *text; |
| 2847 |
if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2848 |
return; |
| 2849 |
PyErr_Clear(); |
| 2850 |
text = PyUnicode_AsUTF8(attr); |
| 2851 |
if (text == NULL) |
| 2852 |
return; |
| 2853 |
PyErr_Format(PyExc_AttributeError, errmsg, cd->c_type->ct_name, text); |
| 2854 |
} |
| 2855 |
|
| 2856 |
static PyObject * |
| 2857 |
cdata_getattro(CDataObject *cd, PyObject *attr) |
| 2858 |
{ |
| 2859 |
CFieldObject *cf; |
| 2860 |
CTypeDescrObject *ct = cd->c_type; |
| 2861 |
char *errmsg = "cdata '%s' has no attribute '%s'"; |
| 2862 |
PyObject *x; |
| 2863 |
|
| 2864 |
if (ct->ct_flags & CT_POINTER) |
| 2865 |
ct = ct->ct_itemdescr; |
| 2866 |
|
| 2867 |
if (ct->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 2868 |
switch (force_lazy_struct(ct)) { |
| 2869 |
case 1: |
| 2870 |
cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, attr); |
| 2871 |
if (cf != NULL) { |
| 2872 |
/* read the field 'cf' */ |
| 2873 |
char *data = cd->c_data + cf->cf_offset; |
| 2874 |
Py_ssize_t array_len, size; |
| 2875 |
|
| 2876 |
if (cf->cf_bitshift == BS_REGULAR) { |
| 2877 |
return convert_to_object(data, cf->cf_type); |
| 2878 |
} |
| 2879 |
else if (cf->cf_bitshift != BS_EMPTY_ARRAY) { |
| 2880 |
return convert_to_object_bitfield(data, cf); |
| 2881 |
} |
| 2882 |
|
| 2883 |
/* variable-length array: */ |
| 2884 |
/* if reading variable length array from variable length |
| 2885 |
struct, calculate array type from allocated length */ |
| 2886 |
size = _cdata_var_byte_size(cd) - cf->cf_offset; |
| 2887 |
if (size >= 0) { |
| 2888 |
array_len = size / cf->cf_type->ct_itemdescr->ct_size; |
| 2889 |
return new_sized_cdata(data, cf->cf_type, array_len); |
| 2890 |
} |
| 2891 |
return new_simple_cdata(data, |
| 2892 |
(CTypeDescrObject *)cf->cf_type->ct_stuff); |
| 2893 |
} |
| 2894 |
errmsg = "cdata '%s' has no field '%s'"; |
| 2895 |
break; |
| 2896 |
case -1: |
| 2897 |
return NULL; |
| 2898 |
default: |
| 2899 |
errmsg = "cdata '%s' points to an opaque type: cannot read fields"; |
| 2900 |
break; |
| 2901 |
} |
| 2902 |
} |
| 2903 |
x = PyObject_GenericGetAttr((PyObject *)cd, attr); |
| 2904 |
if (x == NULL) |
| 2905 |
_cdata_attr_errmsg(errmsg, cd, attr); |
| 2906 |
return x; |
| 2907 |
} |
| 2908 |
|
| 2909 |
static int |
| 2910 |
cdata_setattro(CDataObject *cd, PyObject *attr, PyObject *value) |
| 2911 |
{ |
| 2912 |
CFieldObject *cf; |
| 2913 |
CTypeDescrObject *ct = cd->c_type; |
| 2914 |
char *errmsg = "cdata '%s' has no attribute '%s'"; |
| 2915 |
int x; |
| 2916 |
|
| 2917 |
if (ct->ct_flags & CT_POINTER) |
| 2918 |
ct = ct->ct_itemdescr; |
| 2919 |
|
| 2920 |
if (ct->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 2921 |
switch (force_lazy_struct(ct)) { |
| 2922 |
case 1: |
| 2923 |
cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, attr); |
| 2924 |
if (cf != NULL) { |
| 2925 |
/* write the field 'cf' */ |
| 2926 |
if (value != NULL) { |
| 2927 |
return convert_field_from_object(cd->c_data, cf, value); |
| 2928 |
} |
| 2929 |
else { |
| 2930 |
PyErr_SetString(PyExc_AttributeError, |
| 2931 |
"cannot delete struct field"); |
| 2932 |
return -1; |
| 2933 |
} |
| 2934 |
} |
| 2935 |
errmsg = "cdata '%s' has no field '%s'"; |
| 2936 |
break; |
| 2937 |
case -1: |
| 2938 |
return -1; |
| 2939 |
default: |
| 2940 |
errmsg = "cdata '%s' points to an opaque type: cannot write fields"; |
| 2941 |
break; |
| 2942 |
} |
| 2943 |
} |
| 2944 |
x = PyObject_GenericSetAttr((PyObject *)cd, attr, value); |
| 2945 |
if (x < 0) |
| 2946 |
_cdata_attr_errmsg(errmsg, cd, attr); |
| 2947 |
return x; |
| 2948 |
} |
| 2949 |
|
| 2950 |
static PyObject * |
| 2951 |
convert_struct_to_owning_object(char *data, CTypeDescrObject *ct); /*forward*/ |
| 2952 |
|
| 2953 |
static cif_description_t * |
| 2954 |
fb_prepare_cif(PyObject *fargs, CTypeDescrObject *, Py_ssize_t, ffi_abi); |
| 2955 |
/*forward*/ |
| 2956 |
|
| 2957 |
static CTypeDescrObject *_get_ct_int(void); |
| 2958 |
/* forward, implemented in realize_c_type.c */ |
| 2959 |
|
| 2960 |
static Py_ssize_t |
| 2961 |
_prepare_pointer_call_argument(CTypeDescrObject *ctptr, PyObject *init, |
| 2962 |
char **output_data) |
| 2963 |
{ |
| 2964 |
/* 'ctptr' is here a pointer type 'ITEM *'. Accept as argument an |
| 2965 |
initializer for an array 'ITEM[]'. This includes the case of |
| 2966 |
passing a Python byte string to a 'char *' argument. |
| 2967 |
|
| 2968 |
This function returns -1 if an error occurred, |
| 2969 |
0 if conversion succeeded (into *output_data), |
| 2970 |
or N > 0 if conversion would require N bytes of storage. |
| 2971 |
*/ |
| 2972 |
Py_ssize_t length, datasize; |
| 2973 |
CTypeDescrObject *ctitem; |
| 2974 |
|
| 2975 |
if (CData_Check(init)) |
| 2976 |
goto convert_default; |
| 2977 |
|
| 2978 |
ctitem = ctptr->ct_itemdescr; |
| 2979 |
/* XXX some code duplication, how to avoid it? */ |
| 2980 |
if (PyBytes_Check(init)) { |
| 2981 |
/* from a string: just returning the string here is fine. |
| 2982 |
We assume that the C code won't modify the 'char *' data. */ |
| 2983 |
if ((ctptr->ct_flags & CT_IS_VOIDCHAR_PTR) || |
| 2984 |
((ctitem->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED)) |
| 2985 |
&& (ctitem->ct_size == sizeof(char)))) { |
| 2986 |
#if defined(CFFI_MEM_DEBUG) || defined(CFFI_MEM_LEAK) |
| 2987 |
length = PyBytes_GET_SIZE(init) + 1; |
| 2988 |
#else |
| 2989 |
*output_data = PyBytes_AS_STRING(init); |
| 2990 |
if (ctitem->ct_flags & CT_IS_BOOL) |
| 2991 |
if (must_be_array_of_zero_or_one(*output_data, |
| 2992 |
PyBytes_GET_SIZE(init)) < 0) |
| 2993 |
return -1; |
| 2994 |
return 0; |
| 2995 |
#endif |
| 2996 |
} |
| 2997 |
else |
| 2998 |
goto convert_default; |
| 2999 |
} |
| 3000 |
else if (PyList_Check(init) || PyTuple_Check(init)) { |
| 3001 |
length = PySequence_Fast_GET_SIZE(init); |
| 3002 |
} |
| 3003 |
else if (PyUnicode_Check(init)) { |
| 3004 |
/* from a unicode, we add the null terminator */ |
| 3005 |
if (ctitem->ct_size == 2) |
| 3006 |
length = _my_PyUnicode_SizeAsChar16(init); |
| 3007 |
else |
| 3008 |
length = _my_PyUnicode_SizeAsChar32(init); |
| 3009 |
length += 1; |
| 3010 |
} |
| 3011 |
else if ((ctitem->ct_flags & CT_IS_FILE) && PyFile_Check(init)) { |
| 3012 |
*output_data = (char *)PyFile_AsFile(init); |
| 3013 |
if (*output_data == NULL && PyErr_Occurred()) |
| 3014 |
return -1; |
| 3015 |
return 0; |
| 3016 |
} |
| 3017 |
else { |
| 3018 |
/* refuse to receive just an integer (and interpret it |
| 3019 |
as the array size) */ |
| 3020 |
goto convert_default; |
| 3021 |
} |
| 3022 |
|
| 3023 |
if (ctitem->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 3024 |
if (force_lazy_struct(ctitem) < 0) { |
| 3025 |
return -1; |
| 3026 |
} |
| 3027 |
} |
| 3028 |
if (ctitem->ct_size <= 0) |
| 3029 |
goto convert_default; |
| 3030 |
datasize = MUL_WRAPAROUND(length, ctitem->ct_size); |
| 3031 |
if ((datasize / ctitem->ct_size) != length) { |
| 3032 |
PyErr_SetString(PyExc_OverflowError, |
| 3033 |
"array size would overflow a Py_ssize_t"); |
| 3034 |
return -1; |
| 3035 |
} |
| 3036 |
if (datasize <= 0) |
| 3037 |
datasize = 1; |
| 3038 |
return datasize; |
| 3039 |
|
| 3040 |
convert_default: |
| 3041 |
return convert_from_object((char *)output_data, ctptr, init); |
| 3042 |
} |
| 3043 |
|
| 3044 |
static PyObject* |
| 3045 |
cdata_call(CDataObject *cd, PyObject *args, PyObject *kwds) |
| 3046 |
{ |
| 3047 |
char *buffer; |
| 3048 |
void** buffer_array; |
| 3049 |
cif_description_t *cif_descr; |
| 3050 |
Py_ssize_t i, nargs, nargs_declared; |
| 3051 |
PyObject *signature, *res = NULL, *fvarargs; |
| 3052 |
CTypeDescrObject *fresult; |
| 3053 |
char *resultdata; |
| 3054 |
char *errormsg; |
| 3055 |
struct freeme_s { |
| 3056 |
struct freeme_s *next; |
| 3057 |
union_alignment alignment; |
| 3058 |
} *freeme = NULL; |
| 3059 |
|
| 3060 |
if (!(cd->c_type->ct_flags & CT_FUNCTIONPTR)) { |
| 3061 |
PyErr_Format(PyExc_TypeError, "cdata '%s' is not callable", |
| 3062 |
cd->c_type->ct_name); |
| 3063 |
return NULL; |
| 3064 |
} |
| 3065 |
if (cd->c_data == NULL) { |
| 3066 |
PyErr_Format(PyExc_RuntimeError, |
| 3067 |
"cannot call null pointer pointer from cdata '%s'", |
| 3068 |
cd->c_type->ct_name); |
| 3069 |
return NULL; |
| 3070 |
} |
| 3071 |
if (kwds != NULL && PyDict_Size(kwds) != 0) { |
| 3072 |
PyErr_SetString(PyExc_TypeError, |
| 3073 |
"a cdata function cannot be called with keyword arguments"); |
| 3074 |
return NULL; |
| 3075 |
} |
| 3076 |
signature = cd->c_type->ct_stuff; |
| 3077 |
nargs = PyTuple_Size(args); |
| 3078 |
if (nargs < 0) |
| 3079 |
return NULL; |
| 3080 |
nargs_declared = PyTuple_GET_SIZE(signature) - 2; |
| 3081 |
fresult = (CTypeDescrObject *)PyTuple_GET_ITEM(signature, 1); |
| 3082 |
fvarargs = NULL; |
| 3083 |
buffer = NULL; |
| 3084 |
|
| 3085 |
cif_descr = (cif_description_t *)cd->c_type->ct_extra; |
| 3086 |
|
| 3087 |
if (cif_descr != NULL) { |
| 3088 |
/* regular case: this function does not take '...' arguments */ |
| 3089 |
if (nargs != nargs_declared) { |
| 3090 |
errormsg = "'%s' expects %zd arguments, got %zd"; |
| 3091 |
bad_number_of_arguments: |
| 3092 |
PyErr_Format(PyExc_TypeError, errormsg, |
| 3093 |
cd->c_type->ct_name, nargs_declared, nargs); |
| 3094 |
goto error; |
| 3095 |
} |
| 3096 |
} |
| 3097 |
else { |
| 3098 |
/* call of a variadic function */ |
| 3099 |
ffi_abi fabi; |
| 3100 |
if (nargs < nargs_declared) { |
| 3101 |
errormsg = "'%s' expects at least %zd arguments, got %zd"; |
| 3102 |
goto bad_number_of_arguments; |
| 3103 |
} |
| 3104 |
fvarargs = PyTuple_New(nargs); |
| 3105 |
if (fvarargs == NULL) |
| 3106 |
goto error; |
| 3107 |
for (i = 0; i < nargs_declared; i++) { |
| 3108 |
PyObject *o = PyTuple_GET_ITEM(signature, 2 + i); |
| 3109 |
Py_INCREF(o); |
| 3110 |
PyTuple_SET_ITEM(fvarargs, i, o); |
| 3111 |
} |
| 3112 |
for (i = nargs_declared; i < nargs; i++) { |
| 3113 |
PyObject *obj = PyTuple_GET_ITEM(args, i); |
| 3114 |
CTypeDescrObject *ct; |
| 3115 |
|
| 3116 |
if (CData_Check(obj)) { |
| 3117 |
ct = ((CDataObject *)obj)->c_type; |
| 3118 |
if (ct->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_UNSIGNED | |
| 3119 |
CT_PRIMITIVE_SIGNED)) { |
| 3120 |
if (ct->ct_size < (Py_ssize_t)sizeof(int)) { |
| 3121 |
ct = _get_ct_int(); |
| 3122 |
if (ct == NULL) |
| 3123 |
goto error; |
| 3124 |
} |
| 3125 |
} |
| 3126 |
else if (ct->ct_flags & CT_ARRAY) { |
| 3127 |
ct = (CTypeDescrObject *)ct->ct_stuff; |
| 3128 |
} |
| 3129 |
Py_INCREF(ct); |
| 3130 |
} |
| 3131 |
else { |
| 3132 |
PyErr_Format(PyExc_TypeError, |
| 3133 |
"argument %zd passed in the variadic part " |
| 3134 |
"needs to be a cdata object (got %.200s)", |
| 3135 |
i + 1, Py_TYPE(obj)->tp_name); |
| 3136 |
goto error; |
| 3137 |
} |
| 3138 |
PyTuple_SET_ITEM(fvarargs, i, (PyObject *)ct); |
| 3139 |
} |
| 3140 |
fabi = PyLong_AS_LONG(PyTuple_GET_ITEM(signature, 0)); |
| 3141 |
cif_descr = fb_prepare_cif(fvarargs, fresult, nargs_declared, fabi); |
| 3142 |
if (cif_descr == NULL) |
| 3143 |
goto error; |
| 3144 |
} |
| 3145 |
|
| 3146 |
buffer = PyObject_Malloc(cif_descr->exchange_size); |
| 3147 |
if (buffer == NULL) { |
| 3148 |
PyErr_NoMemory(); |
| 3149 |
goto error; |
| 3150 |
} |
| 3151 |
|
| 3152 |
buffer_array = (void **)buffer; |
| 3153 |
|
| 3154 |
for (i=0; i<nargs; i++) { |
| 3155 |
CTypeDescrObject *argtype; |
| 3156 |
char *data = buffer + cif_descr->exchange_offset_arg[1 + i]; |
| 3157 |
PyObject *obj = PyTuple_GET_ITEM(args, i); |
| 3158 |
|
| 3159 |
buffer_array[i] = data; |
| 3160 |
|
| 3161 |
if (i < nargs_declared) |
| 3162 |
argtype = (CTypeDescrObject *)PyTuple_GET_ITEM(signature, 2 + i); |
| 3163 |
else |
| 3164 |
argtype = (CTypeDescrObject *)PyTuple_GET_ITEM(fvarargs, i); |
| 3165 |
|
| 3166 |
if (argtype->ct_flags & CT_POINTER) { |
| 3167 |
char *tmpbuf; |
| 3168 |
Py_ssize_t datasize = _prepare_pointer_call_argument( |
| 3169 |
argtype, obj, (char **)data); |
| 3170 |
if (datasize == 0) |
| 3171 |
; /* successfully filled '*data' */ |
| 3172 |
else if (datasize < 0) |
| 3173 |
goto error; |
| 3174 |
else { |
| 3175 |
if (datasize <= 512) { |
| 3176 |
tmpbuf = alloca(datasize); |
| 3177 |
} |
| 3178 |
else { |
| 3179 |
struct freeme_s *fp = (struct freeme_s *)PyObject_Malloc( |
| 3180 |
offsetof(struct freeme_s, alignment) + |
| 3181 |
(size_t)datasize); |
| 3182 |
if (fp == NULL) { |
| 3183 |
PyErr_NoMemory(); |
| 3184 |
goto error; |
| 3185 |
} |
| 3186 |
fp->next = freeme; |
| 3187 |
freeme = fp; |
| 3188 |
tmpbuf = (char *)&fp->alignment; |
| 3189 |
} |
| 3190 |
memset(tmpbuf, 0, datasize); |
| 3191 |
*(char **)data = tmpbuf; |
| 3192 |
if (convert_array_from_object(tmpbuf, argtype, obj) < 0) |
| 3193 |
goto error; |
| 3194 |
} |
| 3195 |
} |
| 3196 |
else if (convert_from_object(data, argtype, obj) < 0) |
| 3197 |
goto error; |
| 3198 |
} |
| 3199 |
|
| 3200 |
resultdata = buffer + cif_descr->exchange_offset_arg[0]; |
| 3201 |
/*READ(cd->c_data, sizeof(void(*)(void)))*/ |
| 3202 |
|
| 3203 |
Py_BEGIN_ALLOW_THREADS |
| 3204 |
restore_errno(); |
| 3205 |
ffi_call(&cif_descr->cif, CFFI_CLOSURE_TO_FNPTR(void (*)(void), cd->c_data), |
| 3206 |
resultdata, buffer_array); |
| 3207 |
save_errno(); |
| 3208 |
Py_END_ALLOW_THREADS |
| 3209 |
|
| 3210 |
if (fresult->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_SIGNED | |
| 3211 |
CT_PRIMITIVE_UNSIGNED)) { |
| 3212 |
#ifdef WORDS_BIGENDIAN |
| 3213 |
/* For results of precisely these types, libffi has a strange |
| 3214 |
rule that they will be returned as a whole 'ffi_arg' if they |
| 3215 |
are smaller. The difference only matters on big-endian. */ |
| 3216 |
if (fresult->ct_size < sizeof(ffi_arg)) |
| 3217 |
resultdata += (sizeof(ffi_arg) - fresult->ct_size); |
| 3218 |
#endif |
| 3219 |
res = convert_to_object(resultdata, fresult); |
| 3220 |
} |
| 3221 |
else if (fresult->ct_flags & CT_VOID) { |
| 3222 |
res = Py_None; |
| 3223 |
Py_INCREF(res); |
| 3224 |
} |
| 3225 |
else if (fresult->ct_flags & CT_STRUCT) { |
| 3226 |
res = convert_struct_to_owning_object(resultdata, fresult); |
| 3227 |
} |
| 3228 |
else { |
| 3229 |
res = convert_to_object(resultdata, fresult); |
| 3230 |
} |
| 3231 |
/* fall-through */ |
| 3232 |
|
| 3233 |
error: |
| 3234 |
while (freeme != NULL) { |
| 3235 |
void *p = (void *)freeme; |
| 3236 |
freeme = freeme->next; |
| 3237 |
PyObject_Free(p); |
| 3238 |
} |
| 3239 |
if (buffer) |
| 3240 |
PyObject_Free(buffer); |
| 3241 |
if (fvarargs != NULL) { |
| 3242 |
Py_DECREF(fvarargs); |
| 3243 |
if (cif_descr != NULL) /* but only if fvarargs != NULL, if variadic */ |
| 3244 |
PyObject_Free(cif_descr); |
| 3245 |
} |
| 3246 |
return res; |
| 3247 |
} |
| 3248 |
|
| 3249 |
static PyObject *cdata_dir(PyObject *cd, PyObject *noarg) |
| 3250 |
{ |
| 3251 |
CTypeDescrObject *ct = ((CDataObject *)cd)->c_type; |
| 3252 |
|
| 3253 |
/* replace the type 'pointer-to-t' with just 't' */ |
| 3254 |
if (ct->ct_flags & CT_POINTER) { |
| 3255 |
ct = ct->ct_itemdescr; |
| 3256 |
} |
| 3257 |
if ((ct->ct_flags & (CT_STRUCT | CT_UNION)) && |
| 3258 |
!(cffi_check_flag(ct->ct_unrealized_struct_or_union))) { |
| 3259 |
assert((ct->ct_flags & CT_IS_OPAQUE) == 0); |
| 3260 |
/* for non-opaque structs or unions */ |
| 3261 |
if (force_lazy_struct(ct) < 0) |
| 3262 |
return NULL; |
| 3263 |
assert(ct->ct_stuff); |
| 3264 |
return PyDict_Keys(ct->ct_stuff); |
| 3265 |
} |
| 3266 |
else { |
| 3267 |
return PyList_New(0); /* empty list for the other cases */ |
| 3268 |
} |
| 3269 |
} |
| 3270 |
|
| 3271 |
static PyObject *cdata_complex(PyObject *cd_, PyObject *noarg) |
| 3272 |
{ |
| 3273 |
CDataObject *cd = (CDataObject *)cd_; |
| 3274 |
|
| 3275 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_COMPLEX) { |
| 3276 |
Py_complex value = read_raw_complex_data(cd->c_data, cd->c_type->ct_size); |
| 3277 |
PyObject *op = PyComplex_FromCComplex(value); |
| 3278 |
return op; |
| 3279 |
} |
| 3280 |
/* <cdata 'float'> or <cdata 'int'> cannot be directly converted by |
| 3281 |
calling complex(), just like <cdata 'int'> cannot be directly |
| 3282 |
converted by calling float() */ |
| 3283 |
|
| 3284 |
PyErr_Format(PyExc_TypeError, "complex() not supported on cdata '%s'", |
| 3285 |
cd->c_type->ct_name); |
| 3286 |
return NULL; |
| 3287 |
} |
| 3288 |
|
| 3289 |
static int explicit_release_case(PyObject *cd) |
| 3290 |
{ |
| 3291 |
CTypeDescrObject *ct = ((CDataObject *)cd)->c_type; |
| 3292 |
if (Py_TYPE(cd) == &CDataOwning_Type) { |
| 3293 |
if ((ct->ct_flags & (CT_POINTER | CT_ARRAY)) != 0) /* ffi.new() */ |
| 3294 |
return 0; |
| 3295 |
} |
| 3296 |
else if (Py_TYPE(cd) == &CDataFromBuf_Type) { |
| 3297 |
return 1; /* ffi.from_buffer() */ |
| 3298 |
} |
| 3299 |
else if (Py_TYPE(cd) == &CDataGCP_Type) { |
| 3300 |
return 2; /* ffi.gc() */ |
| 3301 |
} |
| 3302 |
PyErr_SetString(PyExc_ValueError, |
| 3303 |
"only 'cdata' object from ffi.new(), ffi.gc(), ffi.from_buffer() " |
| 3304 |
"or ffi.new_allocator()() can be used with the 'with' keyword or " |
| 3305 |
"ffi.release()"); |
| 3306 |
return -1; |
| 3307 |
} |
| 3308 |
|
| 3309 |
static PyObject *cdata_enter(PyObject *cd, PyObject *noarg) |
| 3310 |
{ |
| 3311 |
if (explicit_release_case(cd) < 0) /* only to check the ctype */ |
| 3312 |
return NULL; |
| 3313 |
Py_INCREF(cd); |
| 3314 |
return cd; |
| 3315 |
} |
| 3316 |
|
| 3317 |
static PyObject *cdata_exit(PyObject *cd, PyObject *args) |
| 3318 |
{ |
| 3319 |
/* 'args' ignored */ |
| 3320 |
CTypeDescrObject *ct; |
| 3321 |
Py_buffer *view; |
| 3322 |
switch (explicit_release_case(cd)) |
| 3323 |
{ |
| 3324 |
case 0: /* ffi.new() */ |
| 3325 |
/* no effect on CPython: raw memory is allocated with the |
| 3326 |
same malloc() as the object itself, so it can't be |
| 3327 |
released independently. If we use a custom allocator, |
| 3328 |
then it's implemented with ffi.gc(). */ |
| 3329 |
ct = ((CDataObject *)cd)->c_type; |
| 3330 |
if (ct->ct_flags & CT_IS_PTR_TO_OWNED) { |
| 3331 |
PyObject *x = ((CDataObject_own_structptr *)cd)->structobj; |
| 3332 |
if (Py_TYPE(x) == &CDataGCP_Type) { |
| 3333 |
/* this is a special case for |
| 3334 |
ffi.new_allocator()("struct-or-union *") */ |
| 3335 |
cdatagcp_finalize((CDataObject_gcp *)x); |
| 3336 |
} |
| 3337 |
} |
| 3338 |
break; |
| 3339 |
|
| 3340 |
case 1: /* ffi.from_buffer() */ |
| 3341 |
view = ((CDataObject_frombuf *)cd)->bufferview; |
| 3342 |
PyBuffer_Release(view); |
| 3343 |
break; |
| 3344 |
|
| 3345 |
case 2: /* ffi.gc() or ffi.new_allocator()("not-struct-nor-union") */ |
| 3346 |
/* call the destructor immediately */ |
| 3347 |
cdatagcp_finalize((CDataObject_gcp *)cd); |
| 3348 |
break; |
| 3349 |
|
| 3350 |
default: |
| 3351 |
return NULL; |
| 3352 |
} |
| 3353 |
Py_INCREF(Py_None); |
| 3354 |
return Py_None; |
| 3355 |
} |
| 3356 |
|
| 3357 |
static PyObject *cdata_iter(CDataObject *); |
| 3358 |
|
| 3359 |
static PyNumberMethods CData_as_number = { |
| 3360 |
(binaryfunc)cdata_add, /*nb_add*/ |
| 3361 |
(binaryfunc)cdata_sub, /*nb_subtract*/ |
| 3362 |
0, /*nb_multiply*/ |
| 3363 |
0, /*nb_remainder*/ |
| 3364 |
0, /*nb_divmod*/ |
| 3365 |
0, /*nb_power*/ |
| 3366 |
0, /*nb_negative*/ |
| 3367 |
0, /*nb_positive*/ |
| 3368 |
0, /*nb_absolute*/ |
| 3369 |
(inquiry)cdata_nonzero, /*nb_nonzero*/ |
| 3370 |
0, /*nb_invert*/ |
| 3371 |
0, /*nb_lshift*/ |
| 3372 |
0, /*nb_rshift*/ |
| 3373 |
0, /*nb_and*/ |
| 3374 |
0, /*nb_xor*/ |
| 3375 |
0, /*nb_or*/ |
| 3376 |
(unaryfunc)cdata_int, /*nb_int*/ |
| 3377 |
0, /*nb_reserved*/ |
| 3378 |
(unaryfunc)cdata_float, /*nb_float*/ |
| 3379 |
0, /*nb_oct*/ |
| 3380 |
0, /*nb_hex*/ |
| 3381 |
}; |
| 3382 |
|
| 3383 |
static PyMappingMethods CData_as_mapping = { |
| 3384 |
(lenfunc)cdata_length, /*mp_length*/ |
| 3385 |
(binaryfunc)cdata_subscript, /*mp_subscript*/ |
| 3386 |
(objobjargproc)cdata_ass_sub, /*mp_ass_subscript*/ |
| 3387 |
}; |
| 3388 |
|
| 3389 |
static PyMappingMethods CDataOwn_as_mapping = { |
| 3390 |
(lenfunc)cdata_length, /*mp_length*/ |
| 3391 |
(binaryfunc)cdataowning_subscript, /*mp_subscript*/ |
| 3392 |
(objobjargproc)cdata_ass_sub, /*mp_ass_subscript*/ |
| 3393 |
}; |
| 3394 |
|
| 3395 |
static PyMethodDef cdata_methods[] = { |
| 3396 |
{"__dir__", cdata_dir, METH_NOARGS}, |
| 3397 |
{"__complex__", cdata_complex, METH_NOARGS}, |
| 3398 |
{"__enter__", cdata_enter, METH_NOARGS}, |
| 3399 |
{"__exit__", cdata_exit, METH_VARARGS}, |
| 3400 |
{NULL, NULL} /* sentinel */ |
| 3401 |
}; |
| 3402 |
|
| 3403 |
static PyTypeObject CData_Type = { |
| 3404 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3405 |
"_cffi_backend._CDataBase", |
| 3406 |
sizeof(CDataObject), |
| 3407 |
0, |
| 3408 |
(destructor)cdata_dealloc, /* tp_dealloc */ |
| 3409 |
0, /* tp_print */ |
| 3410 |
0, /* tp_getattr */ |
| 3411 |
0, /* tp_setattr */ |
| 3412 |
0, /* tp_compare */ |
| 3413 |
(reprfunc)cdata_repr, /* tp_repr */ |
| 3414 |
&CData_as_number, /* tp_as_number */ |
| 3415 |
0, /* tp_as_sequence */ |
| 3416 |
&CData_as_mapping, /* tp_as_mapping */ |
| 3417 |
cdata_hash, /* tp_hash */ |
| 3418 |
(ternaryfunc)cdata_call, /* tp_call */ |
| 3419 |
0, /* tp_str */ |
| 3420 |
(getattrofunc)cdata_getattro, /* tp_getattro */ |
| 3421 |
(setattrofunc)cdata_setattro, /* tp_setattro */ |
| 3422 |
0, /* tp_as_buffer */ |
| 3423 |
Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3424 |
"The internal base type for CData objects. Use FFI.CData to access " |
| 3425 |
"it. Always check with isinstance(): subtypes are sometimes returned " |
| 3426 |
"on CPython, for performance reasons.", /* tp_doc */ |
| 3427 |
0, /* tp_traverse */ |
| 3428 |
0, /* tp_clear */ |
| 3429 |
cdata_richcompare, /* tp_richcompare */ |
| 3430 |
offsetof(CDataObject, c_weakreflist), /* tp_weaklistoffset */ |
| 3431 |
(getiterfunc)cdata_iter, /* tp_iter */ |
| 3432 |
0, /* tp_iternext */ |
| 3433 |
cdata_methods, /* tp_methods */ |
| 3434 |
0, /* tp_members */ |
| 3435 |
0, /* tp_getset */ |
| 3436 |
0, /* tp_base */ |
| 3437 |
0, /* tp_dict */ |
| 3438 |
0, /* tp_descr_get */ |
| 3439 |
0, /* tp_descr_set */ |
| 3440 |
0, /* tp_dictoffset */ |
| 3441 |
0, /* tp_init */ |
| 3442 |
0, /* tp_alloc */ |
| 3443 |
0, /* tp_new */ |
| 3444 |
PyObject_Del, /* tp_free */ |
| 3445 |
}; |
| 3446 |
|
| 3447 |
static PyTypeObject CDataOwning_Type = { |
| 3448 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3449 |
"_cffi_backend.__CDataOwn", |
| 3450 |
sizeof(CDataObject), |
| 3451 |
0, |
| 3452 |
(destructor)cdataowning_dealloc, /* tp_dealloc */ |
| 3453 |
0, /* tp_print */ |
| 3454 |
0, /* tp_getattr */ |
| 3455 |
0, /* tp_setattr */ |
| 3456 |
0, /* tp_compare */ |
| 3457 |
(reprfunc)cdataowning_repr, /* tp_repr */ |
| 3458 |
0, /* inherited */ /* tp_as_number */ |
| 3459 |
0, /* tp_as_sequence */ |
| 3460 |
&CDataOwn_as_mapping, /* tp_as_mapping */ |
| 3461 |
0, /* inherited */ /* tp_hash */ |
| 3462 |
0, /* inherited */ /* tp_call */ |
| 3463 |
0, /* tp_str */ |
| 3464 |
0, /* inherited */ /* tp_getattro */ |
| 3465 |
0, /* inherited */ /* tp_setattro */ |
| 3466 |
0, /* tp_as_buffer */ |
| 3467 |
Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3468 |
"This is an internal subtype of _CDataBase for performance only on " |
| 3469 |
"CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */ |
| 3470 |
0, /* tp_traverse */ |
| 3471 |
0, /* tp_clear */ |
| 3472 |
0, /* inherited */ /* tp_richcompare */ |
| 3473 |
0, /* inherited */ /* tp_weaklistoffset */ |
| 3474 |
0, /* inherited */ /* tp_iter */ |
| 3475 |
0, /* tp_iternext */ |
| 3476 |
0, /* inherited */ /* tp_methods */ |
| 3477 |
0, /* tp_members */ |
| 3478 |
0, /* tp_getset */ |
| 3479 |
&CData_Type, /* tp_base */ |
| 3480 |
0, /* tp_dict */ |
| 3481 |
0, /* tp_descr_get */ |
| 3482 |
0, /* tp_descr_set */ |
| 3483 |
0, /* tp_dictoffset */ |
| 3484 |
0, /* tp_init */ |
| 3485 |
0, /* tp_alloc */ |
| 3486 |
0, /* tp_new */ |
| 3487 |
free, /* tp_free */ |
| 3488 |
}; |
| 3489 |
|
| 3490 |
static PyTypeObject CDataOwningGC_Type = { |
| 3491 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3492 |
"_cffi_backend.__CDataOwnGC", |
| 3493 |
sizeof(CDataObject_own_structptr), |
| 3494 |
0, |
| 3495 |
(destructor)cdataowninggc_dealloc, /* tp_dealloc */ |
| 3496 |
0, /* tp_print */ |
| 3497 |
0, /* tp_getattr */ |
| 3498 |
0, /* tp_setattr */ |
| 3499 |
0, /* tp_compare */ |
| 3500 |
(reprfunc)cdataowninggc_repr, /* tp_repr */ |
| 3501 |
0, /* inherited */ /* tp_as_number */ |
| 3502 |
0, /* tp_as_sequence */ |
| 3503 |
0, /* inherited */ /* tp_as_mapping */ |
| 3504 |
0, /* inherited */ /* tp_hash */ |
| 3505 |
0, /* inherited */ /* tp_call */ |
| 3506 |
0, /* tp_str */ |
| 3507 |
0, /* inherited */ /* tp_getattro */ |
| 3508 |
0, /* inherited */ /* tp_setattro */ |
| 3509 |
0, /* tp_as_buffer */ |
| 3510 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3511 |
"This is an internal subtype of _CDataBase for performance only on " |
| 3512 |
"CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */ |
| 3513 |
(traverseproc)cdataowninggc_traverse, /* tp_traverse */ |
| 3514 |
(inquiry)cdataowninggc_clear, /* tp_clear */ |
| 3515 |
0, /* inherited */ /* tp_richcompare */ |
| 3516 |
0, /* inherited */ /* tp_weaklistoffset */ |
| 3517 |
0, /* inherited */ /* tp_iter */ |
| 3518 |
0, /* tp_iternext */ |
| 3519 |
0, /* inherited */ /* tp_methods */ |
| 3520 |
0, /* tp_members */ |
| 3521 |
0, /* tp_getset */ |
| 3522 |
&CDataOwning_Type, /* tp_base */ |
| 3523 |
0, /* tp_dict */ |
| 3524 |
0, /* tp_descr_get */ |
| 3525 |
0, /* tp_descr_set */ |
| 3526 |
0, /* tp_dictoffset */ |
| 3527 |
0, /* tp_init */ |
| 3528 |
0, /* tp_alloc */ |
| 3529 |
0, /* tp_new */ |
| 3530 |
PyObject_GC_Del, /* tp_free */ |
| 3531 |
}; |
| 3532 |
|
| 3533 |
static PyTypeObject CDataFromBuf_Type = { |
| 3534 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3535 |
"_cffi_backend.__CDataFromBuf", |
| 3536 |
sizeof(CDataObject_frombuf), |
| 3537 |
0, |
| 3538 |
(destructor)cdatafrombuf_dealloc, /* tp_dealloc */ |
| 3539 |
0, /* tp_print */ |
| 3540 |
0, /* tp_getattr */ |
| 3541 |
0, /* tp_setattr */ |
| 3542 |
0, /* tp_compare */ |
| 3543 |
(reprfunc)cdatafrombuf_repr, /* tp_repr */ |
| 3544 |
0, /* inherited */ /* tp_as_number */ |
| 3545 |
0, /* tp_as_sequence */ |
| 3546 |
0, /* inherited */ /* tp_as_mapping */ |
| 3547 |
0, /* inherited */ /* tp_hash */ |
| 3548 |
0, /* inherited */ /* tp_call */ |
| 3549 |
0, /* tp_str */ |
| 3550 |
0, /* inherited */ /* tp_getattro */ |
| 3551 |
0, /* inherited */ /* tp_setattro */ |
| 3552 |
0, /* tp_as_buffer */ |
| 3553 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
| 3554 |
"This is an internal subtype of _CDataBase for performance only on " |
| 3555 |
"CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */ |
| 3556 |
(traverseproc)cdatafrombuf_traverse, /* tp_traverse */ |
| 3557 |
(inquiry)cdatafrombuf_clear, /* tp_clear */ |
| 3558 |
0, /* inherited */ /* tp_richcompare */ |
| 3559 |
0, /* inherited */ /* tp_weaklistoffset */ |
| 3560 |
0, /* inherited */ /* tp_iter */ |
| 3561 |
0, /* tp_iternext */ |
| 3562 |
0, /* inherited */ /* tp_methods */ |
| 3563 |
0, /* tp_members */ |
| 3564 |
0, /* tp_getset */ |
| 3565 |
&CData_Type, /* tp_base */ |
| 3566 |
0, /* tp_dict */ |
| 3567 |
0, /* tp_descr_get */ |
| 3568 |
0, /* tp_descr_set */ |
| 3569 |
0, /* tp_dictoffset */ |
| 3570 |
0, /* tp_init */ |
| 3571 |
0, /* tp_alloc */ |
| 3572 |
0, /* tp_new */ |
| 3573 |
PyObject_GC_Del, /* tp_free */ |
| 3574 |
}; |
| 3575 |
|
| 3576 |
static PyTypeObject CDataGCP_Type = { |
| 3577 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3578 |
"_cffi_backend.__CDataGCP", |
| 3579 |
sizeof(CDataObject_gcp), |
| 3580 |
0, |
| 3581 |
(destructor)cdatagcp_dealloc, /* tp_dealloc */ |
| 3582 |
0, /* tp_print */ |
| 3583 |
0, /* tp_getattr */ |
| 3584 |
0, /* tp_setattr */ |
| 3585 |
0, /* tp_compare */ |
| 3586 |
0, /* inherited */ /* tp_repr */ |
| 3587 |
0, /* inherited */ /* tp_as_number */ |
| 3588 |
0, /* tp_as_sequence */ |
| 3589 |
0, /* inherited */ /* tp_as_mapping */ |
| 3590 |
0, /* inherited */ /* tp_hash */ |
| 3591 |
0, /* inherited */ /* tp_call */ |
| 3592 |
0, /* tp_str */ |
| 3593 |
0, /* inherited */ /* tp_getattro */ |
| 3594 |
0, /* inherited */ /* tp_setattro */ |
| 3595 |
0, /* tp_as_buffer */ |
| 3596 |
Py_TPFLAGS_DEFAULT /* tp_flags */ |
| 3597 |
#ifdef Py_TPFLAGS_HAVE_FINALIZE |
| 3598 |
| Py_TPFLAGS_HAVE_FINALIZE |
| 3599 |
#endif |
| 3600 |
| Py_TPFLAGS_HAVE_GC, |
| 3601 |
"This is an internal subtype of _CDataBase for performance only on " |
| 3602 |
"CPython. Check with isinstance(x, ffi.CData).", /* tp_doc */ |
| 3603 |
(traverseproc)cdatagcp_traverse, /* tp_traverse */ |
| 3604 |
0, /* tp_clear */ |
| 3605 |
0, /* inherited */ /* tp_richcompare */ |
| 3606 |
0, /* inherited */ /* tp_weaklistoffset */ |
| 3607 |
0, /* inherited */ /* tp_iter */ |
| 3608 |
0, /* tp_iternext */ |
| 3609 |
0, /* inherited */ /* tp_methods */ |
| 3610 |
0, /* tp_members */ |
| 3611 |
0, /* tp_getset */ |
| 3612 |
&CData_Type, /* tp_base */ |
| 3613 |
#ifdef Py_TPFLAGS_HAVE_FINALIZE /* CPython >= 3.4 */ |
| 3614 |
0, /* tp_dict */ |
| 3615 |
0, /* tp_descr_get */ |
| 3616 |
0, /* tp_descr_set */ |
| 3617 |
0, /* tp_dictoffset */ |
| 3618 |
0, /* tp_init */ |
| 3619 |
0, /* tp_alloc */ |
| 3620 |
0, /* tp_new */ |
| 3621 |
0, /* inherited */ /* tp_free */ |
| 3622 |
0, /* tp_is_gc */ |
| 3623 |
0, /* tp_bases */ |
| 3624 |
0, /* tp_mro */ |
| 3625 |
0, /* tp_cache */ |
| 3626 |
0, /* tp_subclasses */ |
| 3627 |
0, /* tp_weaklist */ |
| 3628 |
0, /* tp_del */ |
| 3629 |
0, /* version_tag */ |
| 3630 |
(destructor)cdatagcp_finalize, /* tp_finalize */ |
| 3631 |
#endif |
| 3632 |
}; |
| 3633 |
|
| 3634 |
/************************************************************/ |
| 3635 |
|
| 3636 |
typedef struct { |
| 3637 |
PyObject_HEAD |
| 3638 |
char *di_next, *di_stop; |
| 3639 |
CDataObject *di_object; |
| 3640 |
CTypeDescrObject *di_itemtype; |
| 3641 |
} CDataIterObject; |
| 3642 |
|
| 3643 |
static PyObject * |
| 3644 |
cdataiter_next(CDataIterObject *it) |
| 3645 |
{ |
| 3646 |
char *result = it->di_next; |
| 3647 |
if (result != it->di_stop) { |
| 3648 |
it->di_next = result + it->di_itemtype->ct_size; |
| 3649 |
return convert_to_object(result, it->di_itemtype); |
| 3650 |
} |
| 3651 |
return NULL; |
| 3652 |
} |
| 3653 |
|
| 3654 |
static void |
| 3655 |
cdataiter_dealloc(CDataIterObject *it) |
| 3656 |
{ |
| 3657 |
Py_DECREF(it->di_object); |
| 3658 |
PyObject_Del(it); |
| 3659 |
} |
| 3660 |
|
| 3661 |
static PyTypeObject CDataIter_Type = { |
| 3662 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 3663 |
"_cffi_backend.__CData_iterator", /* tp_name */ |
| 3664 |
sizeof(CDataIterObject), /* tp_basicsize */ |
| 3665 |
0, /* tp_itemsize */ |
| 3666 |
/* methods */ |
| 3667 |
(destructor)cdataiter_dealloc, /* tp_dealloc */ |
| 3668 |
0, /* tp_print */ |
| 3669 |
0, /* tp_getattr */ |
| 3670 |
0, /* tp_setattr */ |
| 3671 |
0, /* tp_compare */ |
| 3672 |
0, /* tp_repr */ |
| 3673 |
0, /* tp_as_number */ |
| 3674 |
0, /* tp_as_sequence */ |
| 3675 |
0, /* tp_as_mapping */ |
| 3676 |
0, /* tp_hash */ |
| 3677 |
0, /* tp_call */ |
| 3678 |
0, /* tp_str */ |
| 3679 |
PyObject_GenericGetAttr, /* tp_getattro */ |
| 3680 |
0, /* tp_setattro */ |
| 3681 |
0, /* tp_as_buffer */ |
| 3682 |
Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 3683 |
0, /* tp_doc */ |
| 3684 |
0, /* tp_traverse */ |
| 3685 |
0, /* tp_clear */ |
| 3686 |
0, /* tp_richcompare */ |
| 3687 |
0, /* tp_weaklistoffset */ |
| 3688 |
PyObject_SelfIter, /* tp_iter */ |
| 3689 |
(iternextfunc)cdataiter_next, /* tp_iternext */ |
| 3690 |
}; |
| 3691 |
|
| 3692 |
static PyObject * |
| 3693 |
cdata_iter(CDataObject *cd) |
| 3694 |
{ |
| 3695 |
CDataIterObject *it; |
| 3696 |
|
| 3697 |
if (!(cd->c_type->ct_flags & CT_ARRAY)) { |
| 3698 |
PyErr_Format(PyExc_TypeError, "cdata '%s' does not support iteration", |
| 3699 |
cd->c_type->ct_name); |
| 3700 |
return NULL; |
| 3701 |
} |
| 3702 |
|
| 3703 |
it = PyObject_New(CDataIterObject, &CDataIter_Type); |
| 3704 |
if (it == NULL) |
| 3705 |
return NULL; |
| 3706 |
|
| 3707 |
Py_INCREF(cd); |
| 3708 |
it->di_object = cd; |
| 3709 |
it->di_itemtype = cd->c_type->ct_itemdescr; |
| 3710 |
it->di_next = cd->c_data; |
| 3711 |
it->di_stop = cd->c_data + get_array_length(cd) * it->di_itemtype->ct_size; |
| 3712 |
return (PyObject *)it; |
| 3713 |
} |
| 3714 |
|
| 3715 |
/************************************************************/ |
| 3716 |
|
| 3717 |
static CDataObject *allocate_owning_object(Py_ssize_t size, |
| 3718 |
CTypeDescrObject *ct, |
| 3719 |
int dont_clear) |
| 3720 |
{ |
| 3721 |
/* note: objects with &CDataOwning_Type are always allocated with |
| 3722 |
either a plain malloc() or calloc(), and freed with free(). */ |
| 3723 |
CDataObject *cd; |
| 3724 |
if (dont_clear) |
| 3725 |
cd = malloc(size); |
| 3726 |
else |
| 3727 |
cd = calloc(size, 1); |
| 3728 |
if (PyObject_Init((PyObject *)cd, &CDataOwning_Type) == NULL) |
| 3729 |
return NULL; |
| 3730 |
|
| 3731 |
Py_INCREF(ct); |
| 3732 |
cd->c_type = ct; |
| 3733 |
cd->c_weakreflist = NULL; |
| 3734 |
return cd; |
| 3735 |
} |
| 3736 |
|
| 3737 |
static PyObject * |
| 3738 |
convert_struct_to_owning_object(char *data, CTypeDescrObject *ct) |
| 3739 |
{ |
| 3740 |
/* also accepts unions, for the API mode */ |
| 3741 |
CDataObject *cd; |
| 3742 |
Py_ssize_t dataoffset = offsetof(CDataObject_own_nolength, alignment); |
| 3743 |
Py_ssize_t datasize = ct->ct_size; |
| 3744 |
|
| 3745 |
if (datasize < 0) { |
| 3746 |
PyErr_SetString(PyExc_TypeError, |
| 3747 |
"return type is an opaque structure or union"); |
| 3748 |
return NULL; |
| 3749 |
} |
| 3750 |
if (ct->ct_flags_mut & CT_WITH_VAR_ARRAY) { |
| 3751 |
PyErr_SetString(PyExc_TypeError, |
| 3752 |
"return type is a struct/union with a varsize array member"); |
| 3753 |
return NULL; |
| 3754 |
} |
| 3755 |
cd = allocate_owning_object(dataoffset + datasize, ct, /*dont_clear=*/1); |
| 3756 |
if (cd == NULL) |
| 3757 |
return NULL; |
| 3758 |
cd->c_data = ((char *)cd) + dataoffset; |
| 3759 |
|
| 3760 |
memcpy(cd->c_data, data, datasize); |
| 3761 |
return (PyObject *)cd; |
| 3762 |
} |
| 3763 |
|
| 3764 |
static CDataObject *allocate_gcp_object(CDataObject *origobj, |
| 3765 |
CTypeDescrObject *ct, |
| 3766 |
PyObject *destructor) |
| 3767 |
{ |
| 3768 |
CDataObject_gcp *cd = PyObject_GC_New(CDataObject_gcp, &CDataGCP_Type); |
| 3769 |
if (cd == NULL) |
| 3770 |
return NULL; |
| 3771 |
|
| 3772 |
Py_XINCREF(destructor); |
| 3773 |
Py_INCREF(origobj); |
| 3774 |
Py_INCREF(ct); |
| 3775 |
cd->head.c_data = origobj->c_data; |
| 3776 |
cd->head.c_type = ct; |
| 3777 |
cd->head.c_weakreflist = NULL; |
| 3778 |
cd->origobj = (PyObject *)origobj; |
| 3779 |
cd->destructor = destructor; |
| 3780 |
|
| 3781 |
PyObject_GC_Track(cd); |
| 3782 |
return (CDataObject *)cd; |
| 3783 |
} |
| 3784 |
|
| 3785 |
static CDataObject *allocate_with_allocator(Py_ssize_t basesize, |
| 3786 |
Py_ssize_t datasize, |
| 3787 |
CTypeDescrObject *ct, |
| 3788 |
const cffi_allocator_t *allocator) |
| 3789 |
{ |
| 3790 |
CDataObject *cd; |
| 3791 |
|
| 3792 |
if (allocator->ca_alloc == NULL) { |
| 3793 |
cd = allocate_owning_object(basesize + datasize, ct, |
| 3794 |
allocator->ca_dont_clear); |
| 3795 |
if (cd == NULL) |
| 3796 |
return NULL; |
| 3797 |
cd->c_data = ((char *)cd) + basesize; |
| 3798 |
} |
| 3799 |
else { |
| 3800 |
PyObject *res = PyObject_CallFunction(allocator->ca_alloc, "n", datasize); |
| 3801 |
if (res == NULL) |
| 3802 |
return NULL; |
| 3803 |
|
| 3804 |
if (!CData_Check(res)) { |
| 3805 |
PyErr_Format(PyExc_TypeError, |
| 3806 |
"alloc() must return a cdata object (got %.200s)", |
| 3807 |
Py_TYPE(res)->tp_name); |
| 3808 |
Py_DECREF(res); |
| 3809 |
return NULL; |
| 3810 |
} |
| 3811 |
cd = (CDataObject *)res; |
| 3812 |
if (!(cd->c_type->ct_flags & (CT_POINTER|CT_ARRAY))) { |
| 3813 |
PyErr_Format(PyExc_TypeError, |
| 3814 |
"alloc() must return a cdata pointer, not '%s'", |
| 3815 |
cd->c_type->ct_name); |
| 3816 |
Py_DECREF(res); |
| 3817 |
return NULL; |
| 3818 |
} |
| 3819 |
if (!cd->c_data) { |
| 3820 |
PyErr_SetString(PyExc_MemoryError, "alloc() returned NULL"); |
| 3821 |
Py_DECREF(res); |
| 3822 |
return NULL; |
| 3823 |
} |
| 3824 |
|
| 3825 |
cd = allocate_gcp_object(cd, ct, allocator->ca_free); |
| 3826 |
Py_DECREF(res); |
| 3827 |
if (!allocator->ca_dont_clear) |
| 3828 |
memset(cd->c_data, 0, datasize); |
| 3829 |
} |
| 3830 |
return cd; |
| 3831 |
} |
| 3832 |
|
| 3833 |
static PyObject *direct_newp(CTypeDescrObject *ct, PyObject *init, |
| 3834 |
const cffi_allocator_t *allocator) |
| 3835 |
{ |
| 3836 |
CTypeDescrObject *ctitem; |
| 3837 |
CDataObject *cd; |
| 3838 |
Py_ssize_t dataoffset, datasize, explicitlength; |
| 3839 |
|
| 3840 |
explicitlength = -1; |
| 3841 |
if (ct->ct_flags & CT_POINTER) { |
| 3842 |
dataoffset = offsetof(CDataObject_own_nolength, alignment); |
| 3843 |
ctitem = ct->ct_itemdescr; |
| 3844 |
if (ctitem->ct_flags & (CT_STRUCT | CT_UNION)) { |
| 3845 |
if (force_lazy_struct(ctitem) < 0) |
| 3846 |
return NULL; |
| 3847 |
} |
| 3848 |
datasize = cffi_get_size(ctitem); |
| 3849 |
if (datasize < 0) { |
| 3850 |
PyErr_Format(PyExc_TypeError, |
| 3851 |
"cannot instantiate ctype '%s' of unknown size", |
| 3852 |
ctitem->ct_name); |
| 3853 |
return NULL; |
| 3854 |
} |
| 3855 |
if (ctitem->ct_flags & CT_PRIMITIVE_CHAR) |
| 3856 |
datasize *= 2; /* forcefully add another character: a null */ |
| 3857 |
|
| 3858 |
if (ctitem->ct_flags & (CT_STRUCT | CT_UNION)) { |
| 3859 |
if (ctitem->ct_flags_mut & CT_WITH_VAR_ARRAY) { |
| 3860 |
assert(ct->ct_flags & CT_IS_PTR_TO_OWNED); |
| 3861 |
dataoffset = offsetof(CDataObject_own_length, alignment); |
| 3862 |
|
| 3863 |
if (init != Py_None) { |
| 3864 |
Py_ssize_t optvarsize = datasize; |
| 3865 |
if (convert_struct_from_object(NULL, ctitem, init, |
| 3866 |
&optvarsize) < 0) |
| 3867 |
return NULL; |
| 3868 |
datasize = optvarsize; |
| 3869 |
} |
| 3870 |
} |
| 3871 |
} |
| 3872 |
} |
| 3873 |
else if (ct->ct_flags & CT_ARRAY) { |
| 3874 |
dataoffset = offsetof(CDataObject_own_nolength, alignment); |
| 3875 |
datasize = ct->ct_size; |
| 3876 |
if (datasize < 0) { |
| 3877 |
explicitlength = get_new_array_length(ct->ct_itemdescr, &init); |
| 3878 |
if (explicitlength < 0) |
| 3879 |
return NULL; |
| 3880 |
ctitem = ct->ct_itemdescr; |
| 3881 |
dataoffset = offsetof(CDataObject_own_length, alignment); |
| 3882 |
datasize = MUL_WRAPAROUND(explicitlength, ctitem->ct_size); |
| 3883 |
if (explicitlength > 0 && |
| 3884 |
(datasize / explicitlength) != ctitem->ct_size) { |
| 3885 |
PyErr_SetString(PyExc_OverflowError, |
| 3886 |
"array size would overflow a Py_ssize_t"); |
| 3887 |
return NULL; |
| 3888 |
} |
| 3889 |
} |
| 3890 |
} |
| 3891 |
else { |
| 3892 |
PyErr_Format(PyExc_TypeError, |
| 3893 |
"expected a pointer or array ctype, got '%s'", |
| 3894 |
ct->ct_name); |
| 3895 |
return NULL; |
| 3896 |
} |
| 3897 |
|
| 3898 |
if (ct->ct_flags & CT_IS_PTR_TO_OWNED) { |
| 3899 |
/* common case of ptr-to-struct (or ptr-to-union): for this case |
| 3900 |
we build two objects instead of one, with the memory-owning |
| 3901 |
one being really the struct (or union) and the returned one |
| 3902 |
having a strong reference to it */ |
| 3903 |
CDataObject *cds; |
| 3904 |
|
| 3905 |
cds = allocate_with_allocator(dataoffset, datasize, ct->ct_itemdescr, |
| 3906 |
allocator); |
| 3907 |
if (cds == NULL) |
| 3908 |
return NULL; |
| 3909 |
|
| 3910 |
cd = allocate_owning_object(sizeof(CDataObject_own_structptr), ct, |
| 3911 |
/*dont_clear=*/1); |
| 3912 |
if (cd == NULL) { |
| 3913 |
Py_DECREF(cds); |
| 3914 |
return NULL; |
| 3915 |
} |
| 3916 |
/* store the only reference to cds into cd */ |
| 3917 |
((CDataObject_own_structptr *)cd)->structobj = (PyObject *)cds; |
| 3918 |
/* store information about the allocated size of the struct */ |
| 3919 |
if (dataoffset == offsetof(CDataObject_own_length, alignment)) { |
| 3920 |
((CDataObject_own_length *)cds)->length = datasize; |
| 3921 |
} |
| 3922 |
assert(explicitlength < 0); |
| 3923 |
|
| 3924 |
cd->c_data = cds->c_data; |
| 3925 |
} |
| 3926 |
else { |
| 3927 |
cd = allocate_with_allocator(dataoffset, datasize, ct, allocator); |
| 3928 |
if (cd == NULL) |
| 3929 |
return NULL; |
| 3930 |
|
| 3931 |
if (explicitlength >= 0) |
| 3932 |
((CDataObject_own_length*)cd)->length = explicitlength; |
| 3933 |
} |
| 3934 |
|
| 3935 |
if (init != Py_None) { |
| 3936 |
if (convert_from_object(cd->c_data, |
| 3937 |
(ct->ct_flags & CT_POINTER) ? ct->ct_itemdescr : ct, init) < 0) { |
| 3938 |
Py_DECREF(cd); |
| 3939 |
return NULL; |
| 3940 |
} |
| 3941 |
} |
| 3942 |
return (PyObject *)cd; |
| 3943 |
} |
| 3944 |
|
| 3945 |
static PyObject *b_newp(PyObject *self, PyObject *args) |
| 3946 |
{ |
| 3947 |
CTypeDescrObject *ct; |
| 3948 |
PyObject *init = Py_None; |
| 3949 |
if (!PyArg_ParseTuple(args, "O!|O:newp", &CTypeDescr_Type, &ct, &init)) |
| 3950 |
return NULL; |
| 3951 |
return direct_newp(ct, init, &default_allocator); |
| 3952 |
} |
| 3953 |
|
| 3954 |
static int |
| 3955 |
_my_PyObject_AsBool(PyObject *ob) |
| 3956 |
{ |
| 3957 |
/* convert and cast a Python object to a boolean. Accept an integer |
| 3958 |
or a float object, up to a CData 'long double'. */ |
| 3959 |
PyObject *io; |
| 3960 |
PyNumberMethods *nb; |
| 3961 |
int res; |
| 3962 |
|
| 3963 |
if (PyLong_Check(ob)) { |
| 3964 |
return _PyLong_Sign(ob) != 0; |
| 3965 |
} |
| 3966 |
else if (PyFloat_Check(ob)) { |
| 3967 |
return PyFloat_AS_DOUBLE(ob) != 0.0; |
| 3968 |
} |
| 3969 |
else if (CData_Check(ob)) { |
| 3970 |
CDataObject *cd = (CDataObject *)ob; |
| 3971 |
if (cd->c_type->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 3972 |
/*READ(cd->c_data, cd->c_type->ct_size)*/ |
| 3973 |
if (cd->c_type->ct_flags & CT_IS_LONGDOUBLE) { |
| 3974 |
/* 'long double' objects: return the answer directly */ |
| 3975 |
return read_raw_longdouble_data(cd->c_data) != 0.0; |
| 3976 |
} |
| 3977 |
else { |
| 3978 |
/* 'float'/'double' objects: return the answer directly */ |
| 3979 |
return read_raw_float_data(cd->c_data, |
| 3980 |
cd->c_type->ct_size) != 0.0; |
| 3981 |
} |
| 3982 |
} |
| 3983 |
} |
| 3984 |
nb = ob->ob_type->tp_as_number; |
| 3985 |
if (nb == NULL || (nb->nb_float == NULL && nb->nb_int == NULL)) { |
| 3986 |
PyErr_SetString(PyExc_TypeError, "integer/float expected"); |
| 3987 |
return -1; |
| 3988 |
} |
| 3989 |
if (nb->nb_float && !CData_Check(ob)) |
| 3990 |
io = (*nb->nb_float) (ob); |
| 3991 |
else |
| 3992 |
io = (*nb->nb_int) (ob); |
| 3993 |
if (io == NULL) |
| 3994 |
return -1; |
| 3995 |
|
| 3996 |
if (PyLong_Check(io) || PyFloat_Check(io)) { |
| 3997 |
res = _my_PyObject_AsBool(io); |
| 3998 |
} |
| 3999 |
else { |
| 4000 |
PyErr_SetString(PyExc_TypeError, "integer/float conversion failed"); |
| 4001 |
res = -1; |
| 4002 |
} |
| 4003 |
Py_DECREF(io); |
| 4004 |
return res; |
| 4005 |
} |
| 4006 |
|
| 4007 |
static CDataObject *_new_casted_primitive(CTypeDescrObject *ct) |
| 4008 |
{ |
| 4009 |
int dataoffset = offsetof(CDataObject_casted_primitive, alignment); |
| 4010 |
CDataObject *cd = (CDataObject *)PyObject_Malloc(dataoffset + ct->ct_size); |
| 4011 |
if (PyObject_Init((PyObject *)cd, &CData_Type) == NULL) |
| 4012 |
return NULL; |
| 4013 |
Py_INCREF(ct); |
| 4014 |
cd->c_type = ct; |
| 4015 |
cd->c_data = ((char*)cd) + dataoffset; |
| 4016 |
cd->c_weakreflist = NULL; |
| 4017 |
return cd; |
| 4018 |
} |
| 4019 |
|
| 4020 |
static CDataObject *cast_to_integer_or_char(CTypeDescrObject *ct, PyObject *ob) |
| 4021 |
{ |
| 4022 |
unsigned PY_LONG_LONG value; |
| 4023 |
CDataObject *cd; |
| 4024 |
|
| 4025 |
if (CData_Check(ob) && |
| 4026 |
((CDataObject *)ob)->c_type->ct_flags & |
| 4027 |
(CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY)) { |
| 4028 |
value = (Py_intptr_t)((CDataObject *)ob)->c_data; |
| 4029 |
} |
| 4030 |
else if (PyUnicode_Check(ob)) { |
| 4031 |
char err_buf[80]; |
| 4032 |
cffi_char32_t ordinal; |
| 4033 |
if (_my_PyUnicode_AsSingleChar32(ob, &ordinal, err_buf) < 0) { |
| 4034 |
PyErr_Format(PyExc_TypeError, |
| 4035 |
"cannot cast %s to ctype '%s'", err_buf, ct->ct_name); |
| 4036 |
return NULL; |
| 4037 |
} |
| 4038 |
/* the types char16_t and char32_t are both unsigned. However, |
| 4039 |
wchar_t might be signed. In theory it does not matter, |
| 4040 |
because 'ordinal' comes from a regular Python unicode. */ |
| 4041 |
#ifdef HAVE_WCHAR_H |
| 4042 |
if (ct->ct_flags & CT_IS_SIGNED_WCHAR) |
| 4043 |
value = (wchar_t)ordinal; |
| 4044 |
else |
| 4045 |
#endif |
| 4046 |
value = ordinal; |
| 4047 |
} |
| 4048 |
else if (PyBytes_Check(ob)) { |
| 4049 |
int res = _convert_to_char(ob); |
| 4050 |
if (res < 0) |
| 4051 |
return NULL; |
| 4052 |
value = (unsigned char)res; |
| 4053 |
} |
| 4054 |
else if (ct->ct_flags & CT_IS_BOOL) { |
| 4055 |
int res = _my_PyObject_AsBool(ob); |
| 4056 |
if (res < 0) |
| 4057 |
return NULL; |
| 4058 |
value = res; |
| 4059 |
} |
| 4060 |
else { |
| 4061 |
if (PyCFunction_Check(ob)) { |
| 4062 |
PyObject *func_cdata = try_extract_directfnptr(ob); |
| 4063 |
if (func_cdata != NULL && CData_Check(func_cdata)) { |
| 4064 |
value = (Py_intptr_t)((CDataObject *)func_cdata)->c_data; |
| 4065 |
goto got_value; |
| 4066 |
} |
| 4067 |
if (PyErr_Occurred()) |
| 4068 |
return NULL; |
| 4069 |
} |
| 4070 |
value = _my_PyLong_AsUnsignedLongLong(ob, 0); |
| 4071 |
if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 4072 |
return NULL; |
| 4073 |
} |
| 4074 |
got_value: |
| 4075 |
if (ct->ct_flags & CT_IS_BOOL) |
| 4076 |
value = !!value; |
| 4077 |
cd = _new_casted_primitive(ct); |
| 4078 |
if (cd != NULL) |
| 4079 |
write_raw_integer_data(cd->c_data, value, ct->ct_size); |
| 4080 |
return cd; |
| 4081 |
} |
| 4082 |
|
| 4083 |
/* returns -1 if cannot cast, 0 if we don't get a value, 1 if we do */ |
| 4084 |
static int check_bytes_for_float_compatible(PyObject *io, double *out_value) |
| 4085 |
{ |
| 4086 |
if (PyBytes_Check(io)) { |
| 4087 |
if (PyBytes_GET_SIZE(io) != 1) |
| 4088 |
goto error; |
| 4089 |
*out_value = (unsigned char)PyBytes_AS_STRING(io)[0]; |
| 4090 |
return 1; |
| 4091 |
} |
| 4092 |
else if (PyUnicode_Check(io)) { |
| 4093 |
char ignored[80]; |
| 4094 |
cffi_char32_t ordinal; |
| 4095 |
if (_my_PyUnicode_AsSingleChar32(io, &ordinal, ignored) < 0) |
| 4096 |
goto error; |
| 4097 |
/* the signness of the 32-bit version of wide chars should not |
| 4098 |
* matter here, because 'ordinal' comes from a normal Python |
| 4099 |
* unicode string */ |
| 4100 |
*out_value = ordinal; |
| 4101 |
return 1; |
| 4102 |
} |
| 4103 |
*out_value = 0; /* silence a gcc warning if this function is inlined */ |
| 4104 |
return 0; |
| 4105 |
|
| 4106 |
error: |
| 4107 |
Py_DECREF(io); |
| 4108 |
*out_value = 0; /* silence a gcc warning if this function is inlined */ |
| 4109 |
return -1; |
| 4110 |
} |
| 4111 |
|
| 4112 |
static PyObject *do_cast(CTypeDescrObject *ct, PyObject *ob) |
| 4113 |
{ |
| 4114 |
CDataObject *cd; |
| 4115 |
|
| 4116 |
if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY) && |
| 4117 |
ct->ct_size >= 0) { |
| 4118 |
/* cast to a pointer, to a funcptr, or to an array. |
| 4119 |
Note that casting to an array is an extension to the C language, |
| 4120 |
which seems to be necessary in order to sanely get a |
| 4121 |
<cdata 'int[3]'> at some address. */ |
| 4122 |
unsigned PY_LONG_LONG value; |
| 4123 |
|
| 4124 |
if (CData_Check(ob)) { |
| 4125 |
CDataObject *cdsrc = (CDataObject *)ob; |
| 4126 |
if (cdsrc->c_type->ct_flags & |
| 4127 |
(CT_POINTER|CT_FUNCTIONPTR|CT_ARRAY)) { |
| 4128 |
return new_simple_cdata(cdsrc->c_data, ct); |
| 4129 |
} |
| 4130 |
} |
| 4131 |
if (PyCFunction_Check(ob)) { |
| 4132 |
PyObject *func_cdata = try_extract_directfnptr(ob); |
| 4133 |
if (func_cdata != NULL && CData_Check(func_cdata)) { |
| 4134 |
char *ptr = ((CDataObject *)func_cdata)->c_data; |
| 4135 |
return new_simple_cdata(ptr, ct); |
| 4136 |
} |
| 4137 |
if (PyErr_Occurred()) |
| 4138 |
return NULL; |
| 4139 |
} |
| 4140 |
if ((ct->ct_flags & CT_POINTER) && |
| 4141 |
(ct->ct_itemdescr->ct_flags & CT_IS_FILE) && |
| 4142 |
PyFile_Check(ob)) { |
| 4143 |
FILE *f = PyFile_AsFile(ob); |
| 4144 |
if (f == NULL && PyErr_Occurred()) |
| 4145 |
return NULL; |
| 4146 |
return new_simple_cdata((char *)f, ct); |
| 4147 |
} |
| 4148 |
value = _my_PyLong_AsUnsignedLongLong(ob, 0); |
| 4149 |
if (value == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
| 4150 |
return NULL; |
| 4151 |
return new_simple_cdata((char *)(Py_intptr_t)value, ct); |
| 4152 |
} |
| 4153 |
else if (ct->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED |
| 4154 |
|CT_PRIMITIVE_CHAR)) { |
| 4155 |
/* cast to an integer type or a char */ |
| 4156 |
return (PyObject *)cast_to_integer_or_char(ct, ob); |
| 4157 |
} |
| 4158 |
else if (ct->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 4159 |
/* cast to a float */ |
| 4160 |
double value; |
| 4161 |
PyObject *io; |
| 4162 |
int res; |
| 4163 |
|
| 4164 |
if (CData_Check(ob)) { |
| 4165 |
CDataObject *cdsrc = (CDataObject *)ob; |
| 4166 |
|
| 4167 |
if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY)) |
| 4168 |
goto cannot_cast; |
| 4169 |
io = convert_to_object(cdsrc->c_data, cdsrc->c_type); |
| 4170 |
if (io == NULL) |
| 4171 |
return NULL; |
| 4172 |
} |
| 4173 |
else { |
| 4174 |
io = ob; |
| 4175 |
Py_INCREF(io); |
| 4176 |
} |
| 4177 |
|
| 4178 |
res = check_bytes_for_float_compatible(io, &value); |
| 4179 |
if (res == -1) |
| 4180 |
goto cannot_cast; |
| 4181 |
if (res == 0) { |
| 4182 |
if ((ct->ct_flags & CT_IS_LONGDOUBLE) && |
| 4183 |
CData_Check(io) && |
| 4184 |
(((CDataObject *)io)->c_type->ct_flags & CT_IS_LONGDOUBLE)) { |
| 4185 |
long double lvalue; |
| 4186 |
char *data = ((CDataObject *)io)->c_data; |
| 4187 |
/*READ(data, sizeof(long double)*/ |
| 4188 |
lvalue = read_raw_longdouble_data(data); |
| 4189 |
Py_DECREF(io); |
| 4190 |
cd = _new_casted_primitive(ct); |
| 4191 |
if (cd != NULL) |
| 4192 |
write_raw_longdouble_data(cd->c_data, lvalue); |
| 4193 |
return (PyObject *)cd; |
| 4194 |
} |
| 4195 |
value = PyFloat_AsDouble(io); |
| 4196 |
} |
| 4197 |
Py_DECREF(io); |
| 4198 |
if (value == -1.0 && PyErr_Occurred()) |
| 4199 |
return NULL; |
| 4200 |
|
| 4201 |
cd = _new_casted_primitive(ct); |
| 4202 |
if (cd != NULL) { |
| 4203 |
if (!(ct->ct_flags & CT_IS_LONGDOUBLE)) |
| 4204 |
write_raw_float_data(cd->c_data, value, ct->ct_size); |
| 4205 |
else |
| 4206 |
write_raw_longdouble_data(cd->c_data, (long double)value); |
| 4207 |
} |
| 4208 |
return (PyObject *)cd; |
| 4209 |
} |
| 4210 |
else if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) { |
| 4211 |
/* cast to a complex */ |
| 4212 |
Py_complex value; |
| 4213 |
PyObject *io; |
| 4214 |
int res; |
| 4215 |
|
| 4216 |
if (CData_Check(ob)) { |
| 4217 |
CDataObject *cdsrc = (CDataObject *)ob; |
| 4218 |
|
| 4219 |
if (!(cdsrc->c_type->ct_flags & CT_PRIMITIVE_ANY)) |
| 4220 |
goto cannot_cast; |
| 4221 |
io = convert_to_object(cdsrc->c_data, cdsrc->c_type); |
| 4222 |
if (io == NULL) |
| 4223 |
return NULL; |
| 4224 |
} |
| 4225 |
else { |
| 4226 |
io = ob; |
| 4227 |
Py_INCREF(io); |
| 4228 |
} |
| 4229 |
|
| 4230 |
res = check_bytes_for_float_compatible(io, &value.real); |
| 4231 |
if (res == -1) |
| 4232 |
goto cannot_cast; |
| 4233 |
if (res == 1) { |
| 4234 |
// got it from string |
| 4235 |
value.imag = 0.0; |
| 4236 |
} else { |
| 4237 |
value = PyComplex_AsCComplex(io); |
| 4238 |
} |
| 4239 |
Py_DECREF(io); |
| 4240 |
if (PyErr_Occurred()) { |
| 4241 |
return NULL; |
| 4242 |
} |
| 4243 |
cd = _new_casted_primitive(ct); |
| 4244 |
if (cd != NULL) { |
| 4245 |
write_raw_complex_data(cd->c_data, value, ct->ct_size); |
| 4246 |
} |
| 4247 |
return (PyObject *)cd; |
| 4248 |
} |
| 4249 |
else { |
| 4250 |
PyErr_Format(PyExc_TypeError, "cannot cast to ctype '%s'", |
| 4251 |
ct->ct_name); |
| 4252 |
return NULL; |
| 4253 |
} |
| 4254 |
|
| 4255 |
cannot_cast: |
| 4256 |
if (CData_Check(ob)) |
| 4257 |
PyErr_Format(PyExc_TypeError, "cannot cast ctype '%s' to ctype '%s'", |
| 4258 |
((CDataObject *)ob)->c_type->ct_name, ct->ct_name); |
| 4259 |
else |
| 4260 |
PyErr_Format(PyExc_TypeError, |
| 4261 |
"cannot cast %.200s object to ctype '%s'", |
| 4262 |
Py_TYPE(ob)->tp_name, ct->ct_name); |
| 4263 |
return NULL; |
| 4264 |
} |
| 4265 |
|
| 4266 |
static PyObject *b_cast(PyObject *self, PyObject *args) |
| 4267 |
{ |
| 4268 |
CTypeDescrObject *ct; |
| 4269 |
PyObject *ob; |
| 4270 |
if (!PyArg_ParseTuple(args, "O!O:cast", &CTypeDescr_Type, &ct, &ob)) |
| 4271 |
return NULL; |
| 4272 |
|
| 4273 |
return do_cast(ct, ob); |
| 4274 |
} |
| 4275 |
|
| 4276 |
/************************************************************/ |
| 4277 |
|
| 4278 |
typedef struct { |
| 4279 |
PyObject_HEAD |
| 4280 |
void *dl_handle; |
| 4281 |
char *dl_name; |
| 4282 |
int dl_auto_close; |
| 4283 |
} DynLibObject; |
| 4284 |
|
| 4285 |
static void dl_dealloc(DynLibObject *dlobj) |
| 4286 |
{ |
| 4287 |
if (dlobj->dl_handle != NULL && dlobj->dl_auto_close) |
| 4288 |
dlclose(dlobj->dl_handle); |
| 4289 |
free(dlobj->dl_name); |
| 4290 |
PyObject_Del(dlobj); |
| 4291 |
} |
| 4292 |
|
| 4293 |
static PyObject *dl_repr(DynLibObject *dlobj) |
| 4294 |
{ |
| 4295 |
return PyUnicode_FromFormat("<clibrary '%s'>", dlobj->dl_name); |
| 4296 |
} |
| 4297 |
|
| 4298 |
static int dl_check_closed(DynLibObject *dlobj) |
| 4299 |
{ |
| 4300 |
if (dlobj->dl_handle == NULL) |
| 4301 |
{ |
| 4302 |
PyErr_Format(PyExc_ValueError, "library '%s' has already been closed", |
| 4303 |
dlobj->dl_name); |
| 4304 |
return -1; |
| 4305 |
} |
| 4306 |
return 0; |
| 4307 |
} |
| 4308 |
|
| 4309 |
static PyObject *dl_load_function(DynLibObject *dlobj, PyObject *args) |
| 4310 |
{ |
| 4311 |
CTypeDescrObject *ct; |
| 4312 |
char *funcname; |
| 4313 |
void *funcptr; |
| 4314 |
|
| 4315 |
if (!PyArg_ParseTuple(args, "O!s:load_function", |
| 4316 |
&CTypeDescr_Type, &ct, &funcname)) |
| 4317 |
return NULL; |
| 4318 |
|
| 4319 |
if (dl_check_closed(dlobj) < 0) |
| 4320 |
return NULL; |
| 4321 |
|
| 4322 |
if (!(ct->ct_flags & (CT_FUNCTIONPTR | CT_POINTER | CT_ARRAY))) { |
| 4323 |
PyErr_Format(PyExc_TypeError, |
| 4324 |
"function or pointer or array cdata expected, got '%s'", |
| 4325 |
ct->ct_name); |
| 4326 |
return NULL; |
| 4327 |
} |
| 4328 |
dlerror(); /* clear error condition */ |
| 4329 |
funcptr = dlsym(dlobj->dl_handle, funcname); |
| 4330 |
if (funcptr == NULL) { |
| 4331 |
const char *error = dlerror(); |
| 4332 |
PyErr_Format(PyExc_AttributeError, |
| 4333 |
"function/symbol '%s' not found in library '%s': %s", |
| 4334 |
funcname, dlobj->dl_name, error); |
| 4335 |
return NULL; |
| 4336 |
} |
| 4337 |
|
| 4338 |
if ((ct->ct_flags & CT_ARRAY) && ct->ct_length < 0) { |
| 4339 |
ct = (CTypeDescrObject *)ct->ct_stuff; |
| 4340 |
} |
| 4341 |
return new_simple_cdata(funcptr, ct); |
| 4342 |
} |
| 4343 |
|
| 4344 |
static PyObject *dl_read_variable(DynLibObject *dlobj, PyObject *args) |
| 4345 |
{ |
| 4346 |
CTypeDescrObject *ct; |
| 4347 |
char *varname; |
| 4348 |
char *data; |
| 4349 |
|
| 4350 |
if (!PyArg_ParseTuple(args, "O!s:read_variable", |
| 4351 |
&CTypeDescr_Type, &ct, &varname)) |
| 4352 |
return NULL; |
| 4353 |
|
| 4354 |
if (dl_check_closed(dlobj) < 0) |
| 4355 |
return NULL; |
| 4356 |
|
| 4357 |
dlerror(); /* clear error condition */ |
| 4358 |
data = dlsym(dlobj->dl_handle, varname); |
| 4359 |
if (data == NULL) { |
| 4360 |
const char *error = dlerror(); |
| 4361 |
if (error != NULL) { |
| 4362 |
PyErr_Format(PyExc_KeyError, |
| 4363 |
"variable '%s' not found in library '%s': %s", |
| 4364 |
varname, dlobj->dl_name, error); |
| 4365 |
return NULL; |
| 4366 |
} |
| 4367 |
} |
| 4368 |
return convert_to_object(data, ct); |
| 4369 |
} |
| 4370 |
|
| 4371 |
static PyObject *dl_write_variable(DynLibObject *dlobj, PyObject *args) |
| 4372 |
{ |
| 4373 |
CTypeDescrObject *ct; |
| 4374 |
PyObject *value; |
| 4375 |
char *varname; |
| 4376 |
char *data; |
| 4377 |
|
| 4378 |
if (!PyArg_ParseTuple(args, "O!sO:write_variable", |
| 4379 |
&CTypeDescr_Type, &ct, &varname, &value)) |
| 4380 |
return NULL; |
| 4381 |
|
| 4382 |
if (dl_check_closed(dlobj) < 0) |
| 4383 |
return NULL; |
| 4384 |
|
| 4385 |
dlerror(); /* clear error condition */ |
| 4386 |
data = dlsym(dlobj->dl_handle, varname); |
| 4387 |
if (data == NULL) { |
| 4388 |
const char *error = dlerror(); |
| 4389 |
PyErr_Format(PyExc_KeyError, |
| 4390 |
"variable '%s' not found in library '%s': %s", |
| 4391 |
varname, dlobj->dl_name, error); |
| 4392 |
return NULL; |
| 4393 |
} |
| 4394 |
if (convert_from_object(data, ct, value) < 0) |
| 4395 |
return NULL; |
| 4396 |
Py_INCREF(Py_None); |
| 4397 |
return Py_None; |
| 4398 |
} |
| 4399 |
|
| 4400 |
static PyObject *dl_close_lib(DynLibObject *dlobj, PyObject *no_args) |
| 4401 |
{ |
| 4402 |
if (dlobj->dl_handle != NULL) |
| 4403 |
{ |
| 4404 |
dlclose(dlobj->dl_handle); |
| 4405 |
dlobj->dl_handle = NULL; |
| 4406 |
} |
| 4407 |
Py_INCREF(Py_None); |
| 4408 |
return Py_None; |
| 4409 |
} |
| 4410 |
|
| 4411 |
static PyMethodDef dl_methods[] = { |
| 4412 |
{"load_function", (PyCFunction)dl_load_function, METH_VARARGS}, |
| 4413 |
{"read_variable", (PyCFunction)dl_read_variable, METH_VARARGS}, |
| 4414 |
{"write_variable", (PyCFunction)dl_write_variable, METH_VARARGS}, |
| 4415 |
{"close_lib", (PyCFunction)dl_close_lib, METH_NOARGS}, |
| 4416 |
{NULL, NULL} /* sentinel */ |
| 4417 |
}; |
| 4418 |
|
| 4419 |
static PyTypeObject dl_type = { |
| 4420 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 4421 |
"_cffi_backend.CLibrary", /* tp_name */ |
| 4422 |
sizeof(DynLibObject), /* tp_basicsize */ |
| 4423 |
0, /* tp_itemsize */ |
| 4424 |
/* methods */ |
| 4425 |
(destructor)dl_dealloc, /* tp_dealloc */ |
| 4426 |
0, /* tp_print */ |
| 4427 |
0, /* tp_getattr */ |
| 4428 |
0, /* tp_setattr */ |
| 4429 |
0, /* tp_compare */ |
| 4430 |
(reprfunc)dl_repr, /* tp_repr */ |
| 4431 |
0, /* tp_as_number */ |
| 4432 |
0, /* tp_as_sequence */ |
| 4433 |
0, /* tp_as_mapping */ |
| 4434 |
0, /* tp_hash */ |
| 4435 |
0, /* tp_call */ |
| 4436 |
0, /* tp_str */ |
| 4437 |
PyObject_GenericGetAttr, /* tp_getattro */ |
| 4438 |
0, /* tp_setattro */ |
| 4439 |
0, /* tp_as_buffer */ |
| 4440 |
Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 4441 |
0, /* tp_doc */ |
| 4442 |
0, /* tp_traverse */ |
| 4443 |
0, /* tp_clear */ |
| 4444 |
0, /* tp_richcompare */ |
| 4445 |
0, /* tp_weaklistoffset */ |
| 4446 |
0, /* tp_iter */ |
| 4447 |
0, /* tp_iternext */ |
| 4448 |
dl_methods, /* tp_methods */ |
| 4449 |
}; |
| 4450 |
|
| 4451 |
static void *b_do_dlopen(PyObject *args, const char **p_printable_filename, |
| 4452 |
PyObject **p_temp, int *auto_close) |
| 4453 |
{ |
| 4454 |
/* Logic to call the correct version of dlopen(). Returns NULL in case of error. |
| 4455 |
Otherwise, '*p_printable_filename' will point to a printable char version of |
| 4456 |
the filename (maybe utf-8-encoded). '*p_temp' will be set either to NULL or |
| 4457 |
to a temporary object that must be freed after looking at printable_filename. |
| 4458 |
*/ |
| 4459 |
void *handle; |
| 4460 |
char *filename_or_null; |
| 4461 |
int flags = 0; |
| 4462 |
*p_temp = NULL; |
| 4463 |
*auto_close = 1; |
| 4464 |
|
| 4465 |
if (PyTuple_GET_SIZE(args) == 0 || PyTuple_GET_ITEM(args, 0) == Py_None) { |
| 4466 |
PyObject *dummy; |
| 4467 |
if (!PyArg_ParseTuple(args, "|Oi:load_library", |
| 4468 |
&dummy, &flags)) |
| 4469 |
return NULL; |
| 4470 |
filename_or_null = NULL; |
| 4471 |
*p_printable_filename = "<None>"; |
| 4472 |
} |
| 4473 |
else if (CData_Check(PyTuple_GET_ITEM(args, 0))) |
| 4474 |
{ |
| 4475 |
CDataObject *cd; |
| 4476 |
if (!PyArg_ParseTuple(args, "O|i:load_library", &cd, &flags)) |
| 4477 |
return NULL; |
| 4478 |
/* 'flags' is accepted but ignored in this case */ |
| 4479 |
if ((cd->c_type->ct_flags & CT_IS_VOID_PTR) == 0) { |
| 4480 |
PyErr_Format(PyExc_TypeError, |
| 4481 |
"dlopen() takes a file name or 'void *' handle, not '%s'", |
| 4482 |
cd->c_type->ct_name); |
| 4483 |
return NULL; |
| 4484 |
} |
| 4485 |
handle = cd->c_data; |
| 4486 |
if (handle == NULL) { |
| 4487 |
PyErr_Format(PyExc_RuntimeError, "cannot call dlopen(NULL)"); |
| 4488 |
return NULL; |
| 4489 |
} |
| 4490 |
*p_temp = PyUnicode_FromFormat("%p", handle); |
| 4491 |
*p_printable_filename = PyUnicode_AsUTF8(*p_temp); |
| 4492 |
*auto_close = 0; |
| 4493 |
return handle; |
| 4494 |
} |
| 4495 |
else |
| 4496 |
{ |
| 4497 |
PyObject *s = PyTuple_GET_ITEM(args, 0); |
| 4498 |
#ifdef MS_WIN32 |
| 4499 |
PyObject *filename_unicode; |
| 4500 |
if (PyArg_ParseTuple(args, "U|i:load_library", &filename_unicode, &flags)) |
| 4501 |
{ |
| 4502 |
Py_ssize_t sz1; |
| 4503 |
wchar_t *w1; |
| 4504 |
*p_printable_filename = PyUnicode_AsUTF8(s); |
| 4505 |
if (*p_printable_filename == NULL) |
| 4506 |
return NULL; |
| 4507 |
|
| 4508 |
sz1 = PyUnicode_GetLength(filename_unicode) + 1; |
| 4509 |
sz1 *= 2; /* should not be needed, but you never know */ |
| 4510 |
w1 = alloca(sizeof(wchar_t) * sz1); |
| 4511 |
sz1 = PyUnicode_AsWideChar(filename_unicode, |
| 4512 |
w1, sz1 - 1); |
| 4513 |
if (sz1 < 0) |
| 4514 |
return NULL; |
| 4515 |
w1[sz1] = 0; |
| 4516 |
handle = dlopenWinW(w1, flags); |
| 4517 |
goto got_handle; |
| 4518 |
} |
| 4519 |
PyErr_Clear(); |
| 4520 |
#endif |
| 4521 |
if (!PyArg_ParseTuple(args, "et|i:load_library", |
| 4522 |
Py_FileSystemDefaultEncoding, &filename_or_null, &flags)) |
| 4523 |
return NULL; |
| 4524 |
*p_printable_filename = PyUnicode_AsUTF8(s); |
| 4525 |
if (*p_printable_filename == NULL) { |
| 4526 |
PyMem_Free(filename_or_null); |
| 4527 |
return NULL; |
| 4528 |
} |
| 4529 |
} |
| 4530 |
if ((flags & (RTLD_NOW | RTLD_LAZY)) == 0) |
| 4531 |
flags |= RTLD_NOW; |
| 4532 |
|
| 4533 |
#ifdef MS_WIN32 |
| 4534 |
if (filename_or_null == NULL) { |
| 4535 |
PyErr_SetString(PyExc_OSError, "dlopen(None) not supported on Windows"); |
| 4536 |
return NULL; |
| 4537 |
} |
| 4538 |
#endif |
| 4539 |
|
| 4540 |
handle = dlopen(filename_or_null, flags); |
| 4541 |
PyMem_Free(filename_or_null); |
| 4542 |
|
| 4543 |
#ifdef MS_WIN32 |
| 4544 |
got_handle: |
| 4545 |
#endif |
| 4546 |
if (handle == NULL) { |
| 4547 |
const char *error = dlerror(); |
| 4548 |
PyErr_Format(PyExc_OSError, "cannot load library '%s': %s", |
| 4549 |
*p_printable_filename, error); |
| 4550 |
return NULL; |
| 4551 |
} |
| 4552 |
return handle; |
| 4553 |
} |
| 4554 |
|
| 4555 |
static PyObject *b_load_library(PyObject *self, PyObject *args) |
| 4556 |
{ |
| 4557 |
const char *printable_filename; |
| 4558 |
PyObject *temp; |
| 4559 |
void *handle; |
| 4560 |
DynLibObject *dlobj = NULL; |
| 4561 |
int auto_close; |
| 4562 |
|
| 4563 |
handle = b_do_dlopen(args, &printable_filename, &temp, &auto_close); |
| 4564 |
if (handle == NULL) |
| 4565 |
goto error; |
| 4566 |
|
| 4567 |
dlobj = PyObject_New(DynLibObject, &dl_type); |
| 4568 |
if (dlobj == NULL) { |
| 4569 |
dlclose(handle); |
| 4570 |
goto error; |
| 4571 |
} |
| 4572 |
dlobj->dl_handle = handle; |
| 4573 |
dlobj->dl_name = strdup(printable_filename); |
| 4574 |
dlobj->dl_auto_close = auto_close; |
| 4575 |
|
| 4576 |
error: |
| 4577 |
Py_XDECREF(temp); |
| 4578 |
return (PyObject *)dlobj; |
| 4579 |
} |
| 4580 |
|
| 4581 |
/************************************************************/ |
| 4582 |
|
| 4583 |
static PyObject *get_or_insert_unique_type(CTypeDescrObject *x, |
| 4584 |
PyObject *key) |
| 4585 |
{ |
| 4586 |
PyObject *wr, *obj; |
| 4587 |
|
| 4588 |
if (PyDict_GetItemRef(unique_cache, key, &wr) < 0) { |
| 4589 |
return NULL; |
| 4590 |
} |
| 4591 |
|
| 4592 |
if (wr != NULL) { |
| 4593 |
if (PyWeakref_GetRef(wr, &obj) < 0) { |
| 4594 |
Py_DECREF(wr); |
| 4595 |
return NULL; |
| 4596 |
} |
| 4597 |
Py_DECREF(wr); |
| 4598 |
if (obj != NULL) { |
| 4599 |
return obj; |
| 4600 |
} |
| 4601 |
} |
| 4602 |
|
| 4603 |
/* Use a weakref so that the dictionary does not keep 'x' alive */ |
| 4604 |
wr = PyWeakref_NewRef((PyObject *)x, NULL); |
| 4605 |
if (wr == NULL) { |
| 4606 |
return NULL; |
| 4607 |
} |
| 4608 |
|
| 4609 |
if (PyDict_SetItem(unique_cache, key, wr) < 0) { |
| 4610 |
Py_DECREF(wr); |
| 4611 |
return NULL; |
| 4612 |
} |
| 4613 |
|
| 4614 |
assert(x->ct_unique_key == NULL); |
| 4615 |
Py_INCREF(key); |
| 4616 |
x->ct_unique_key = key; /* the key will be freed in ctypedescr_dealloc() */ |
| 4617 |
|
| 4618 |
Py_DECREF(wr); |
| 4619 |
Py_INCREF(x); |
| 4620 |
return (PyObject *)x; |
| 4621 |
} |
| 4622 |
|
| 4623 |
static PyObject *get_unique_type(CTypeDescrObject *x, |
| 4624 |
const void *unique_key[], long keylength) |
| 4625 |
{ |
| 4626 |
/* Replace the CTypeDescrObject 'x' with a standardized one. |
| 4627 |
This either just returns x, or x is decrefed and a new reference |
| 4628 |
to the already-existing equivalent is returned. |
| 4629 |
|
| 4630 |
In this function, 'x' always contains a reference that must be |
| 4631 |
either decrefed or returned. |
| 4632 |
|
| 4633 |
Keys: |
| 4634 |
void ["void"] |
| 4635 |
primitive [&static_struct] |
| 4636 |
pointer [ctype] |
| 4637 |
array [ctype, length] |
| 4638 |
funcptr [ctresult, ellipsis+abi, num_args, ctargs...] |
| 4639 |
*/ |
| 4640 |
PyObject *key, *y; |
| 4641 |
|
| 4642 |
key = PyBytes_FromStringAndSize((const char *)unique_key, keylength * sizeof(void *)); |
| 4643 |
if (key == NULL) { |
| 4644 |
Py_DECREF(x); |
| 4645 |
return NULL; |
| 4646 |
} |
| 4647 |
|
| 4648 |
LOCK_UNIQUE_CACHE(); |
| 4649 |
y = get_or_insert_unique_type(x, key); |
| 4650 |
UNLOCK_UNIQUE_CACHE(); |
| 4651 |
|
| 4652 |
Py_DECREF(key); |
| 4653 |
Py_DECREF(x); |
| 4654 |
return y; |
| 4655 |
} |
| 4656 |
|
| 4657 |
/* Delete a dead weakref from the unique cache */ |
| 4658 |
static void remove_dead_unique_reference(PyObject *unique_key) |
| 4659 |
{ |
| 4660 |
PyObject *wr; |
| 4661 |
PyObject *tmp = NULL; |
| 4662 |
int err = 0; |
| 4663 |
|
| 4664 |
LOCK_UNIQUE_CACHE(); |
| 4665 |
/* We need to check that it's not already been replaced by a live weakref |
| 4666 |
to a different object. */ |
| 4667 |
wr = PyDict_GetItemWithError(unique_cache, unique_key); |
| 4668 |
if (wr != NULL) { |
| 4669 |
err = PyWeakref_GetRef(wr, &tmp); |
| 4670 |
if (err == 0) { |
| 4671 |
/* The weakref is dead, delete it. */ |
| 4672 |
assert(tmp == NULL); |
| 4673 |
err = PyDict_DelItem(unique_cache, unique_key); |
| 4674 |
} |
| 4675 |
} |
| 4676 |
UNLOCK_UNIQUE_CACHE(); |
| 4677 |
|
| 4678 |
Py_XDECREF(tmp); |
| 4679 |
if (err < 0) { |
| 4680 |
PyErr_WriteUnraisable(NULL); |
| 4681 |
} |
| 4682 |
} |
| 4683 |
|
| 4684 |
/* according to the C standard, these types should be equivalent to the |
| 4685 |
_Complex types for the purposes of storage (not arguments in calls!) */ |
| 4686 |
typedef float cffi_float_complex_t[2]; |
| 4687 |
typedef double cffi_double_complex_t[2]; |
| 4688 |
|
| 4689 |
static PyObject *new_primitive_type(const char *name) |
| 4690 |
{ |
| 4691 |
#define ENUM_PRIMITIVE_TYPES \ |
| 4692 |
EPTYPE(c, char, CT_PRIMITIVE_CHAR) \ |
| 4693 |
EPTYPE(s, short, CT_PRIMITIVE_SIGNED ) \ |
| 4694 |
EPTYPE(i, int, CT_PRIMITIVE_SIGNED ) \ |
| 4695 |
EPTYPE(l, long, CT_PRIMITIVE_SIGNED ) \ |
| 4696 |
EPTYPE(ll, long long, CT_PRIMITIVE_SIGNED ) \ |
| 4697 |
EPTYPE(sc, signed char, CT_PRIMITIVE_SIGNED ) \ |
| 4698 |
EPTYPE(uc, unsigned char, CT_PRIMITIVE_UNSIGNED ) \ |
| 4699 |
EPTYPE(us, unsigned short, CT_PRIMITIVE_UNSIGNED ) \ |
| 4700 |
EPTYPE(ui, unsigned int, CT_PRIMITIVE_UNSIGNED ) \ |
| 4701 |
EPTYPE(ul, unsigned long, CT_PRIMITIVE_UNSIGNED ) \ |
| 4702 |
EPTYPE(ull, unsigned long long, CT_PRIMITIVE_UNSIGNED ) \ |
| 4703 |
EPTYPE(f, float, CT_PRIMITIVE_FLOAT ) \ |
| 4704 |
EPTYPE(d, double, CT_PRIMITIVE_FLOAT ) \ |
| 4705 |
EPTYPE(ld, long double, CT_PRIMITIVE_FLOAT | CT_IS_LONGDOUBLE ) \ |
| 4706 |
EPTYPE2(fc, "_cffi_float_complex_t", cffi_float_complex_t, CT_PRIMITIVE_COMPLEX)\ |
| 4707 |
EPTYPE2(dc, "_cffi_double_complex_t", cffi_double_complex_t, CT_PRIMITIVE_COMPLEX)\ |
| 4708 |
ENUM_PRIMITIVE_TYPES_WCHAR \ |
| 4709 |
EPTYPE2(c16, "char16_t", cffi_char16_t, CT_PRIMITIVE_CHAR ) \ |
| 4710 |
EPTYPE2(c32, "char32_t", cffi_char32_t, CT_PRIMITIVE_CHAR ) \ |
| 4711 |
EPTYPE(b, _Bool, CT_PRIMITIVE_UNSIGNED | CT_IS_BOOL ) \ |
| 4712 |
/* the following types are not primitive in the C sense */ \ |
| 4713 |
EPTYPE(i8, int8_t, CT_PRIMITIVE_SIGNED) \ |
| 4714 |
EPTYPE(u8, uint8_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4715 |
EPTYPE(i16, int16_t, CT_PRIMITIVE_SIGNED) \ |
| 4716 |
EPTYPE(u16, uint16_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4717 |
EPTYPE(i32, int32_t, CT_PRIMITIVE_SIGNED) \ |
| 4718 |
EPTYPE(u32, uint32_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4719 |
EPTYPE(i64, int64_t, CT_PRIMITIVE_SIGNED) \ |
| 4720 |
EPTYPE(u64, uint64_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4721 |
EPTYPE(il8, int_least8_t, CT_PRIMITIVE_SIGNED) \ |
| 4722 |
EPTYPE(ul8, uint_least8_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4723 |
EPTYPE(il16, int_least16_t, CT_PRIMITIVE_SIGNED) \ |
| 4724 |
EPTYPE(ul16, uint_least16_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4725 |
EPTYPE(il32, int_least32_t, CT_PRIMITIVE_SIGNED) \ |
| 4726 |
EPTYPE(ul32, uint_least32_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4727 |
EPTYPE(il64, int_least64_t, CT_PRIMITIVE_SIGNED) \ |
| 4728 |
EPTYPE(ul64, uint_least64_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4729 |
EPTYPE(if8, int_fast8_t, CT_PRIMITIVE_SIGNED) \ |
| 4730 |
EPTYPE(uf8, uint_fast8_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4731 |
EPTYPE(if16, int_fast16_t, CT_PRIMITIVE_SIGNED) \ |
| 4732 |
EPTYPE(uf16, uint_fast16_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4733 |
EPTYPE(if32, int_fast32_t, CT_PRIMITIVE_SIGNED) \ |
| 4734 |
EPTYPE(uf32, uint_fast32_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4735 |
EPTYPE(if64, int_fast64_t, CT_PRIMITIVE_SIGNED) \ |
| 4736 |
EPTYPE(uf64, uint_fast64_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4737 |
EPTYPE(ip, intptr_t, CT_PRIMITIVE_SIGNED) \ |
| 4738 |
EPTYPE(up, uintptr_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4739 |
EPTYPE(im, intmax_t, CT_PRIMITIVE_SIGNED) \ |
| 4740 |
EPTYPE(um, uintmax_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4741 |
EPTYPE(pd, ptrdiff_t, CT_PRIMITIVE_SIGNED) \ |
| 4742 |
EPTYPE(sz, size_t, CT_PRIMITIVE_UNSIGNED) \ |
| 4743 |
EPTYPE2(ssz, "ssize_t", Py_ssize_t, CT_PRIMITIVE_SIGNED) |
| 4744 |
|
| 4745 |
#ifdef HAVE_WCHAR_H |
| 4746 |
# define ENUM_PRIMITIVE_TYPES_WCHAR \ |
| 4747 |
EPTYPE(wc, wchar_t, CT_PRIMITIVE_CHAR | \ |
| 4748 |
(((wchar_t)-1) > 0 ? 0 : CT_IS_SIGNED_WCHAR)) |
| 4749 |
#else |
| 4750 |
# define ENUM_PRIMITIVE_TYPES_WCHAR /* nothing */ |
| 4751 |
#endif |
| 4752 |
|
| 4753 |
#define EPTYPE(code, typename, flags) EPTYPE2(code, #typename, typename, flags) |
| 4754 |
|
| 4755 |
#define EPTYPE2(code, export_name, typename, flags) \ |
| 4756 |
struct aligncheck_##code { char x; typename y; }; |
| 4757 |
ENUM_PRIMITIVE_TYPES |
| 4758 |
#undef EPTYPE2 |
| 4759 |
|
| 4760 |
CTypeDescrObject *td; |
| 4761 |
static const struct descr_s { const char *name; int size, align, flags; } |
| 4762 |
types[] = { |
| 4763 |
#define EPTYPE2(code, export_name, typename, flags) \ |
| 4764 |
{ export_name, \ |
| 4765 |
sizeof(typename), \ |
| 4766 |
offsetof(struct aligncheck_##code, y), \ |
| 4767 |
flags \ |
| 4768 |
}, |
| 4769 |
ENUM_PRIMITIVE_TYPES |
| 4770 |
#undef EPTYPE2 |
| 4771 |
#undef EPTYPE |
| 4772 |
#undef ENUM_PRIMITIVE_TYPES_WCHAR |
| 4773 |
#undef ENUM_PRIMITIVE_TYPES |
| 4774 |
{ NULL } |
| 4775 |
}; |
| 4776 |
const struct descr_s *ptypes; |
| 4777 |
const void *unique_key[1]; |
| 4778 |
int name_size; |
| 4779 |
ffi_type *ffitype; |
| 4780 |
|
| 4781 |
for (ptypes=types; ; ptypes++) { |
| 4782 |
if (ptypes->name == NULL) { |
| 4783 |
#ifndef HAVE_WCHAR_H |
| 4784 |
if (strcmp(name, "wchar_t")) |
| 4785 |
PyErr_SetString(PyExc_NotImplementedError, name); |
| 4786 |
else |
| 4787 |
#endif |
| 4788 |
PyErr_SetString(PyExc_KeyError, name); |
| 4789 |
return NULL; |
| 4790 |
} |
| 4791 |
if (strcmp(name, ptypes->name) == 0) |
| 4792 |
break; |
| 4793 |
} |
| 4794 |
|
| 4795 |
if (ptypes->flags & CT_PRIMITIVE_SIGNED) { |
| 4796 |
switch (ptypes->size) { |
| 4797 |
case 1: ffitype = &ffi_type_sint8; break; |
| 4798 |
case 2: ffitype = &ffi_type_sint16; break; |
| 4799 |
case 4: ffitype = &ffi_type_sint32; break; |
| 4800 |
case 8: ffitype = &ffi_type_sint64; break; |
| 4801 |
default: goto bad_ffi_type; |
| 4802 |
} |
| 4803 |
} |
| 4804 |
else if (ptypes->flags & CT_PRIMITIVE_FLOAT) { |
| 4805 |
if (strcmp(ptypes->name, "float") == 0) |
| 4806 |
ffitype = &ffi_type_float; |
| 4807 |
else if (strcmp(ptypes->name, "double") == 0) |
| 4808 |
ffitype = &ffi_type_double; |
| 4809 |
else if (strcmp(ptypes->name, "long double") == 0) { |
| 4810 |
/* assume that if sizeof(double) == sizeof(long double), then |
| 4811 |
the two types are equivalent for C. libffi bugs on Win64 |
| 4812 |
if a function's return type is ffi_type_longdouble... */ |
| 4813 |
if (sizeof(double) == sizeof(long double)) |
| 4814 |
ffitype = &ffi_type_double; |
| 4815 |
else |
| 4816 |
ffitype = &ffi_type_longdouble; |
| 4817 |
} |
| 4818 |
else |
| 4819 |
goto bad_ffi_type; |
| 4820 |
} |
| 4821 |
else if (ptypes->flags & CT_PRIMITIVE_COMPLEX) { |
| 4822 |
/* As of March 2017, still no libffi support for complex. |
| 4823 |
It fails silently if we try to use ffi_type_complex_float |
| 4824 |
or ffi_type_complex_double. Better not use it at all. |
| 4825 |
*/ |
| 4826 |
ffitype = NULL; |
| 4827 |
} |
| 4828 |
else { |
| 4829 |
switch (ptypes->size) { |
| 4830 |
case 1: ffitype = &ffi_type_uint8; break; |
| 4831 |
case 2: ffitype = &ffi_type_uint16; break; |
| 4832 |
case 4: ffitype = &ffi_type_uint32; break; |
| 4833 |
case 8: ffitype = &ffi_type_uint64; break; |
| 4834 |
default: goto bad_ffi_type; |
| 4835 |
} |
| 4836 |
} |
| 4837 |
|
| 4838 |
name_size = strlen(ptypes->name) + 1; |
| 4839 |
td = ctypedescr_new(name_size); |
| 4840 |
if (td == NULL) |
| 4841 |
return NULL; |
| 4842 |
|
| 4843 |
memcpy(td->ct_name, name, name_size); |
| 4844 |
td->ct_size = ptypes->size; |
| 4845 |
td->ct_length = ptypes->align; |
| 4846 |
td->ct_extra = ffitype; |
| 4847 |
td->ct_flags = ptypes->flags; |
| 4848 |
if (td->ct_flags & (CT_PRIMITIVE_SIGNED | CT_PRIMITIVE_CHAR)) { |
| 4849 |
if (td->ct_size <= (Py_ssize_t)sizeof(long)) |
| 4850 |
td->ct_flags |= CT_PRIMITIVE_FITS_LONG; |
| 4851 |
} |
| 4852 |
else if (td->ct_flags & CT_PRIMITIVE_UNSIGNED) { |
| 4853 |
if (td->ct_size < (Py_ssize_t)sizeof(long)) |
| 4854 |
td->ct_flags |= CT_PRIMITIVE_FITS_LONG; |
| 4855 |
} |
| 4856 |
td->ct_name_position = strlen(td->ct_name); |
| 4857 |
unique_key[0] = ptypes; |
| 4858 |
return get_unique_type(td, unique_key, 1); |
| 4859 |
|
| 4860 |
bad_ffi_type: |
| 4861 |
PyErr_Format(PyExc_NotImplementedError, |
| 4862 |
"primitive type '%s' has size %d; " |
| 4863 |
"the supported sizes are 1, 2, 4, 8", |
| 4864 |
name, (int)ptypes->size); |
| 4865 |
return NULL; |
| 4866 |
} |
| 4867 |
|
| 4868 |
static PyObject *b_new_primitive_type(PyObject *self, PyObject *args) |
| 4869 |
{ |
| 4870 |
char *name; |
| 4871 |
if (!PyArg_ParseTuple(args, "s:new_primitive_type", &name)) |
| 4872 |
return NULL; |
| 4873 |
return new_primitive_type(name); |
| 4874 |
} |
| 4875 |
|
| 4876 |
static PyObject *new_pointer_type(CTypeDescrObject *ctitem) |
| 4877 |
{ |
| 4878 |
CTypeDescrObject *td; |
| 4879 |
const char *extra; |
| 4880 |
const void *unique_key[1]; |
| 4881 |
|
| 4882 |
if (ctitem->ct_flags & CT_ARRAY) |
| 4883 |
extra = "(*)"; /* obscure case: see test_array_add */ |
| 4884 |
else |
| 4885 |
extra = " *"; |
| 4886 |
td = ctypedescr_new_on_top(ctitem, extra, 2); |
| 4887 |
if (td == NULL) |
| 4888 |
return NULL; |
| 4889 |
|
| 4890 |
td->ct_size = sizeof(void *); |
| 4891 |
td->ct_length = -1; |
| 4892 |
td->ct_flags = CT_POINTER; |
| 4893 |
if (ctitem->ct_flags & (CT_STRUCT|CT_UNION)) |
| 4894 |
td->ct_flags |= CT_IS_PTR_TO_OWNED; |
| 4895 |
if (ctitem->ct_flags & CT_VOID) |
| 4896 |
td->ct_flags |= CT_IS_VOID_PTR; |
| 4897 |
if ((ctitem->ct_flags & CT_VOID) || |
| 4898 |
((ctitem->ct_flags & CT_PRIMITIVE_CHAR) && |
| 4899 |
ctitem->ct_size == sizeof(char))) |
| 4900 |
td->ct_flags |= CT_IS_VOIDCHAR_PTR; /* 'void *' or 'char *' only */ |
| 4901 |
unique_key[0] = ctitem; |
| 4902 |
return get_unique_type(td, unique_key, 1); |
| 4903 |
} |
| 4904 |
|
| 4905 |
static PyObject *b_new_pointer_type(PyObject *self, PyObject *args) |
| 4906 |
{ |
| 4907 |
CTypeDescrObject *ctitem; |
| 4908 |
if (!PyArg_ParseTuple(args, "O!:new_pointer_type", |
| 4909 |
&CTypeDescr_Type, &ctitem)) |
| 4910 |
return NULL; |
| 4911 |
return new_pointer_type(ctitem); |
| 4912 |
} |
| 4913 |
|
| 4914 |
static PyObject *b_new_array_type(PyObject *self, PyObject *args) |
| 4915 |
{ |
| 4916 |
PyObject *lengthobj; |
| 4917 |
Py_ssize_t length; |
| 4918 |
CTypeDescrObject *ctptr; |
| 4919 |
|
| 4920 |
if (!PyArg_ParseTuple(args, "O!O:new_array_type", |
| 4921 |
&CTypeDescr_Type, &ctptr, &lengthobj)) |
| 4922 |
return NULL; |
| 4923 |
|
| 4924 |
if (lengthobj == Py_None) { |
| 4925 |
length = -1; |
| 4926 |
} |
| 4927 |
else { |
| 4928 |
length = PyNumber_AsSsize_t(lengthobj, PyExc_OverflowError); |
| 4929 |
if (length < 0) { |
| 4930 |
if (!PyErr_Occurred()) |
| 4931 |
PyErr_SetString(PyExc_ValueError, "negative array length"); |
| 4932 |
return NULL; |
| 4933 |
} |
| 4934 |
} |
| 4935 |
return new_array_type(ctptr, length); |
| 4936 |
} |
| 4937 |
|
| 4938 |
static PyObject * |
| 4939 |
new_array_type(CTypeDescrObject *ctptr, Py_ssize_t length) |
| 4940 |
{ |
| 4941 |
CTypeDescrObject *td, *ctitem; |
| 4942 |
char extra_text[32]; |
| 4943 |
Py_ssize_t arraysize; |
| 4944 |
int flags = CT_ARRAY; |
| 4945 |
const void *unique_key[2]; |
| 4946 |
|
| 4947 |
if (!(ctptr->ct_flags & CT_POINTER)) { |
| 4948 |
PyErr_SetString(PyExc_TypeError, "first arg must be a pointer ctype"); |
| 4949 |
return NULL; |
| 4950 |
} |
| 4951 |
ctitem = ctptr->ct_itemdescr; |
| 4952 |
if (cffi_get_size(ctitem) < 0) { |
| 4953 |
PyErr_Format(PyExc_ValueError, "array item of unknown size: '%s'", |
| 4954 |
ctitem->ct_name); |
| 4955 |
return NULL; |
| 4956 |
} |
| 4957 |
|
| 4958 |
if (length < 0) { |
| 4959 |
sprintf(extra_text, "[]"); |
| 4960 |
length = -1; |
| 4961 |
arraysize = -1; |
| 4962 |
} |
| 4963 |
else { |
| 4964 |
sprintf(extra_text, "[%llu]", (unsigned PY_LONG_LONG)length); |
| 4965 |
arraysize = MUL_WRAPAROUND(length, cffi_get_size(ctitem)); |
| 4966 |
if (length > 0 && (arraysize / length) != cffi_get_size(ctitem)) { |
| 4967 |
PyErr_SetString(PyExc_OverflowError, |
| 4968 |
"array size would overflow a Py_ssize_t"); |
| 4969 |
return NULL; |
| 4970 |
} |
| 4971 |
} |
| 4972 |
td = ctypedescr_new_on_top(ctitem, extra_text, 0); |
| 4973 |
if (td == NULL) |
| 4974 |
return NULL; |
| 4975 |
|
| 4976 |
Py_INCREF(ctptr); |
| 4977 |
td->ct_stuff = (PyObject *)ctptr; |
| 4978 |
td->ct_size = arraysize; |
| 4979 |
td->ct_length = length; |
| 4980 |
td->ct_flags = flags; |
| 4981 |
unique_key[0] = ctptr; |
| 4982 |
unique_key[1] = (void *)length; |
| 4983 |
return get_unique_type(td, unique_key, 2); |
| 4984 |
} |
| 4985 |
|
| 4986 |
static PyObject *new_void_type(void) |
| 4987 |
{ |
| 4988 |
int name_size = strlen("void") + 1; |
| 4989 |
const void *unique_key[1]; |
| 4990 |
CTypeDescrObject *td = ctypedescr_new(name_size); |
| 4991 |
if (td == NULL) |
| 4992 |
return NULL; |
| 4993 |
|
| 4994 |
memcpy(td->ct_name, "void", name_size); |
| 4995 |
td->ct_size = -1; |
| 4996 |
td->ct_flags = CT_VOID | CT_IS_OPAQUE; |
| 4997 |
td->ct_name_position = strlen("void"); |
| 4998 |
unique_key[0] = "void"; |
| 4999 |
return get_unique_type(td, unique_key, 1); |
| 5000 |
} |
| 5001 |
|
| 5002 |
static PyObject *b_new_void_type(PyObject *self, PyObject *args) |
| 5003 |
{ |
| 5004 |
return new_void_type(); |
| 5005 |
} |
| 5006 |
|
| 5007 |
static PyObject *new_struct_or_union_type(const char *name, int flag) |
| 5008 |
{ |
| 5009 |
int namelen = strlen(name); |
| 5010 |
CTypeDescrObject *td = ctypedescr_new(namelen + 1); |
| 5011 |
if (td == NULL) |
| 5012 |
return NULL; |
| 5013 |
|
| 5014 |
td->ct_size = -1; |
| 5015 |
td->ct_length = -1; |
| 5016 |
td->ct_flags = flag; |
| 5017 |
td->ct_unrealized_struct_or_union = 1; |
| 5018 |
td->ct_extra = NULL; |
| 5019 |
memcpy(td->ct_name, name, namelen + 1); |
| 5020 |
td->ct_name_position = namelen; |
| 5021 |
return (PyObject *)td; |
| 5022 |
} |
| 5023 |
|
| 5024 |
static PyObject *b_new_struct_type(PyObject *self, PyObject *args) |
| 5025 |
{ |
| 5026 |
char *name; |
| 5027 |
int flag; |
| 5028 |
if (!PyArg_ParseTuple(args, "s:new_struct_type", &name)) |
| 5029 |
return NULL; |
| 5030 |
|
| 5031 |
flag = CT_STRUCT; |
| 5032 |
if (strcmp(name, "struct _IO_FILE") == 0 || strcmp(name, "FILE") == 0) |
| 5033 |
flag |= CT_IS_FILE; |
| 5034 |
return new_struct_or_union_type(name, flag); |
| 5035 |
} |
| 5036 |
|
| 5037 |
static PyObject *b_new_union_type(PyObject *self, PyObject *args) |
| 5038 |
{ |
| 5039 |
char *name; |
| 5040 |
if (!PyArg_ParseTuple(args, "s:new_union_type", &name)) |
| 5041 |
return NULL; |
| 5042 |
return new_struct_or_union_type(name, CT_UNION); |
| 5043 |
} |
| 5044 |
|
| 5045 |
static CFieldObject * |
| 5046 |
_add_field(PyObject *interned_fields, PyObject *fname, CTypeDescrObject *ftype, |
| 5047 |
Py_ssize_t offset, int bitshift, int fbitsize, int flags) |
| 5048 |
{ |
| 5049 |
int err; |
| 5050 |
Py_ssize_t prev_size; |
| 5051 |
CFieldObject *cf = PyObject_New(CFieldObject, &CField_Type); |
| 5052 |
if (cf == NULL) |
| 5053 |
return NULL; |
| 5054 |
|
| 5055 |
Py_INCREF(ftype); |
| 5056 |
cf->cf_type = ftype; |
| 5057 |
cf->cf_offset = offset; |
| 5058 |
cf->cf_bitshift = bitshift; |
| 5059 |
cf->cf_bitsize = fbitsize; |
| 5060 |
cf->cf_flags = flags; |
| 5061 |
|
| 5062 |
Py_INCREF(fname); |
| 5063 |
PyUnicode_InternInPlace(&fname); |
| 5064 |
prev_size = PyDict_Size(interned_fields); |
| 5065 |
err = PyDict_SetItem(interned_fields, fname, (PyObject *)cf); |
| 5066 |
Py_DECREF(fname); |
| 5067 |
Py_DECREF(cf); |
| 5068 |
if (err < 0) |
| 5069 |
return NULL; |
| 5070 |
|
| 5071 |
if (PyDict_Size(interned_fields) != prev_size + 1) { |
| 5072 |
PyErr_Format(PyExc_KeyError, "duplicate field name '%s'", |
| 5073 |
PyUnicode_AsUTF8(fname)); |
| 5074 |
return NULL; |
| 5075 |
} |
| 5076 |
return cf; /* borrowed reference */ |
| 5077 |
} |
| 5078 |
|
| 5079 |
#define SF_MSVC_BITFIELDS 0x01 |
| 5080 |
#define SF_GCC_ARM_BITFIELDS 0x02 |
| 5081 |
#define SF_GCC_X86_BITFIELDS 0x10 |
| 5082 |
|
| 5083 |
#define SF_GCC_BIG_ENDIAN 0x04 |
| 5084 |
#define SF_GCC_LITTLE_ENDIAN 0x40 |
| 5085 |
|
| 5086 |
#define SF_PACKED 0x08 |
| 5087 |
#define SF_STD_FIELD_POS 0x80 |
| 5088 |
|
| 5089 |
#ifdef MS_WIN32 |
| 5090 |
# define SF_DEFAULT_PACKING 8 |
| 5091 |
#else |
| 5092 |
# define SF_DEFAULT_PACKING 0x40000000 /* a huge power of two */ |
| 5093 |
#endif |
| 5094 |
|
| 5095 |
static int complete_sflags(int sflags) |
| 5096 |
{ |
| 5097 |
/* add one of the SF_xxx_BITFIELDS flags if none is specified */ |
| 5098 |
if (!(sflags & (SF_MSVC_BITFIELDS | SF_GCC_ARM_BITFIELDS | |
| 5099 |
SF_GCC_X86_BITFIELDS))) { |
| 5100 |
#ifdef MS_WIN32 |
| 5101 |
sflags |= SF_MSVC_BITFIELDS; |
| 5102 |
#else |
| 5103 |
# if defined(__APPLE__) && defined(__arm64__) |
| 5104 |
sflags |= SF_GCC_X86_BITFIELDS; |
| 5105 |
# elif defined(__arm__) || defined(__aarch64__) |
| 5106 |
sflags |= SF_GCC_ARM_BITFIELDS; |
| 5107 |
# else |
| 5108 |
sflags |= SF_GCC_X86_BITFIELDS; |
| 5109 |
# endif |
| 5110 |
#endif |
| 5111 |
} |
| 5112 |
/* add one of SF_GCC_xx_ENDIAN if none is specified */ |
| 5113 |
if (!(sflags & (SF_GCC_BIG_ENDIAN | SF_GCC_LITTLE_ENDIAN))) { |
| 5114 |
int _check_endian = 1; |
| 5115 |
if (*(char *)&_check_endian == 0) |
| 5116 |
sflags |= SF_GCC_BIG_ENDIAN; |
| 5117 |
else |
| 5118 |
sflags |= SF_GCC_LITTLE_ENDIAN; |
| 5119 |
} |
| 5120 |
return sflags; |
| 5121 |
} |
| 5122 |
|
| 5123 |
static int detect_custom_layout(CTypeDescrObject *ct, int sflags, |
| 5124 |
Py_ssize_t cdef_value, |
| 5125 |
Py_ssize_t compiler_value, |
| 5126 |
const char *msg1, const char *txt, |
| 5127 |
const char *msg2) |
| 5128 |
{ |
| 5129 |
if (compiler_value != cdef_value) { |
| 5130 |
if (sflags & SF_STD_FIELD_POS) { |
| 5131 |
PyErr_Format(FFIError, |
| 5132 |
"%s: %s%s%s (cdef says %zd, but C compiler says %zd)." |
| 5133 |
" fix it or use \"...;\" as the last field in the " |
| 5134 |
"cdef for %s to make it flexible", |
| 5135 |
ct->ct_name, msg1, txt, msg2, |
| 5136 |
cdef_value, compiler_value, |
| 5137 |
ct->ct_name); |
| 5138 |
return -1; |
| 5139 |
} |
| 5140 |
ct->ct_flags_mut |= CT_CUSTOM_FIELD_POS; |
| 5141 |
} |
| 5142 |
return 0; |
| 5143 |
} |
| 5144 |
|
| 5145 |
#define ROUNDUP_BYTES(bytes, bits) ((bytes) + ((bits) > 0)) |
| 5146 |
|
| 5147 |
|
| 5148 |
|
| 5149 |
static PyObject *b_complete_struct_or_union_lock_held(CTypeDescrObject *ct, |
| 5150 |
PyObject *fields, |
| 5151 |
Py_ssize_t totalsize, int totalalignment, int sflags, |
| 5152 |
int pack) |
| 5153 |
{ |
| 5154 |
int is_union, alignment; |
| 5155 |
Py_ssize_t byteoffset, i, nb_fields, byteoffsetmax, alignedsize; |
| 5156 |
int bitoffset, fflags; |
| 5157 |
Py_ssize_t byteoffsetorg; |
| 5158 |
CFieldObject **previous; |
| 5159 |
int prev_bitfield_size, prev_bitfield_free; |
| 5160 |
PyObject *interned_fields = NULL; |
| 5161 |
|
| 5162 |
sflags = complete_sflags(sflags); |
| 5163 |
if (sflags & SF_PACKED) |
| 5164 |
pack = 1; |
| 5165 |
else if (pack <= 0) |
| 5166 |
pack = SF_DEFAULT_PACKING; |
| 5167 |
else |
| 5168 |
sflags |= SF_PACKED; |
| 5169 |
|
| 5170 |
PyObject *res = NULL; |
| 5171 |
is_union = ct->ct_flags & CT_UNION; |
| 5172 |
if (!((ct->ct_flags & CT_UNION) || (ct->ct_flags & CT_STRUCT)) || |
| 5173 |
!(cffi_check_flag(ct->ct_unrealized_struct_or_union) || cffi_check_flag(ct->ct_under_construction))) { |
| 5174 |
PyErr_SetString(PyExc_TypeError, |
| 5175 |
"first arg must be a non-initialized struct or union ctype"); |
| 5176 |
goto finally; |
| 5177 |
} |
| 5178 |
ct->ct_flags_mut &= ~CT_CUSTOM_FIELD_POS; |
| 5179 |
ct->ct_flags_mut &= ~CT_WITH_PACKED_CHANGE; |
| 5180 |
|
| 5181 |
alignment = 1; |
| 5182 |
byteoffset = 0; /* the real value is 'byteoffset+bitoffset*8', which */ |
| 5183 |
bitoffset = 0; /* counts the offset in bits */ |
| 5184 |
byteoffsetmax = 0; /* the maximum value of byteoffset-rounded-up-to-byte */ |
| 5185 |
prev_bitfield_size = 0; |
| 5186 |
prev_bitfield_free = 0; |
| 5187 |
nb_fields = PyList_GET_SIZE(fields); |
| 5188 |
interned_fields = PyDict_New(); |
| 5189 |
if (interned_fields == NULL) |
| 5190 |
goto finally; |
| 5191 |
|
| 5192 |
previous = (CFieldObject **)&ct->ct_extra; |
| 5193 |
|
| 5194 |
for (i=0; i<nb_fields; i++) { |
| 5195 |
PyObject *fname; |
| 5196 |
CTypeDescrObject *ftype; |
| 5197 |
int fbitsize = -1, falign, falignorg, do_align; |
| 5198 |
Py_ssize_t foffset = -1; |
| 5199 |
|
| 5200 |
if (!PyArg_ParseTuple(PyList_GET_ITEM(fields, i), "O!O!|in:list item", |
| 5201 |
&PyUnicode_Type, &fname, |
| 5202 |
&CTypeDescr_Type, &ftype, |
| 5203 |
&fbitsize, &foffset)) |
| 5204 |
goto finally; |
| 5205 |
|
| 5206 |
if ((ftype->ct_flags & (CT_STRUCT | CT_UNION)) && |
| 5207 |
!(cffi_check_flag(ct->ct_unrealized_struct_or_union))) { |
| 5208 |
assert((ftype->ct_flags & CT_IS_OPAQUE) == 0); |
| 5209 |
/* force now the type of the nested field */ |
| 5210 |
if (force_lazy_struct(ftype) < 0) |
| 5211 |
goto finally; |
| 5212 |
} |
| 5213 |
|
| 5214 |
if (cffi_get_size(ftype) < 0) { |
| 5215 |
if ((ftype->ct_flags & CT_ARRAY) && fbitsize < 0 |
| 5216 |
&& (i == nb_fields - 1 || foffset != -1)) { |
| 5217 |
ct->ct_flags_mut |= CT_WITH_VAR_ARRAY; |
| 5218 |
} |
| 5219 |
else { |
| 5220 |
PyErr_Format(PyExc_TypeError, |
| 5221 |
"field '%s.%s' has ctype '%s' of unknown size", |
| 5222 |
ct->ct_name, PyUnicode_AsUTF8(fname), |
| 5223 |
ftype->ct_name); |
| 5224 |
goto finally; |
| 5225 |
} |
| 5226 |
} |
| 5227 |
else if (ftype->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 5228 |
/* GCC (or maybe C99) accepts var-sized struct fields that are not |
| 5229 |
the last field of a larger struct. That's why there is no |
| 5230 |
check here for "last field": we propagate the flag |
| 5231 |
CT_WITH_VAR_ARRAY to any struct that contains either an open- |
| 5232 |
ended array or another struct that recursively contains an |
| 5233 |
open-ended array. */ |
| 5234 |
if (ftype->ct_flags_mut & CT_WITH_VAR_ARRAY) { |
| 5235 |
ct->ct_flags_mut |= CT_WITH_VAR_ARRAY; |
| 5236 |
} |
| 5237 |
} |
| 5238 |
|
| 5239 |
if (is_union) |
| 5240 |
byteoffset = bitoffset = 0; /* reset each field at offset 0 */ |
| 5241 |
|
| 5242 |
/* update the total alignment requirement, but skip it if the |
| 5243 |
field is an anonymous bitfield or if SF_PACKED */ |
| 5244 |
falignorg = get_alignment(ftype); |
| 5245 |
if (falignorg < 0) |
| 5246 |
goto finally; |
| 5247 |
falign = (pack < falignorg) ? pack : falignorg; |
| 5248 |
|
| 5249 |
do_align = 1; |
| 5250 |
if (!(sflags & SF_GCC_ARM_BITFIELDS) && fbitsize >= 0) { |
| 5251 |
if (!(sflags & SF_MSVC_BITFIELDS)) { |
| 5252 |
/* GCC: anonymous bitfields (of any size) don't cause alignment */ |
| 5253 |
do_align = PyUnicode_GetLength(fname) > 0; |
| 5254 |
} |
| 5255 |
else { |
| 5256 |
/* MSVC: zero-sized bitfields don't cause alignment */ |
| 5257 |
do_align = fbitsize > 0; |
| 5258 |
} |
| 5259 |
} |
| 5260 |
if (alignment < falign && do_align) |
| 5261 |
alignment = falign; |
| 5262 |
|
| 5263 |
fflags = (is_union && i > 0) ? BF_IGNORE_IN_CTOR : 0; |
| 5264 |
|
| 5265 |
if (fbitsize < 0) { |
| 5266 |
/* not a bitfield: common case */ |
| 5267 |
int bs_flag; |
| 5268 |
|
| 5269 |
if ((ftype->ct_flags & CT_ARRAY) && ftype->ct_length <= 0) |
| 5270 |
bs_flag = BS_EMPTY_ARRAY; |
| 5271 |
else |
| 5272 |
bs_flag = BS_REGULAR; |
| 5273 |
|
| 5274 |
/* align this field to its own 'falign' by inserting padding */ |
| 5275 |
|
| 5276 |
/* first, pad to the next byte, |
| 5277 |
* then pad to 'falign' or 'falignorg' bytes */ |
| 5278 |
byteoffset = ROUNDUP_BYTES(byteoffset, bitoffset); |
| 5279 |
bitoffset = 0; |
| 5280 |
byteoffsetorg = (byteoffset + falignorg-1) & ~(falignorg-1); |
| 5281 |
byteoffset = (byteoffset + falign-1) & ~(falign-1); |
| 5282 |
|
| 5283 |
if (byteoffsetorg != byteoffset) { |
| 5284 |
ct->ct_flags_mut |= CT_WITH_PACKED_CHANGE; |
| 5285 |
} |
| 5286 |
|
| 5287 |
if (foffset >= 0) { |
| 5288 |
/* a forced field position: ignore the offset just computed, |
| 5289 |
except to know if we must set CT_CUSTOM_FIELD_POS */ |
| 5290 |
if (detect_custom_layout(ct, sflags, byteoffset, foffset, |
| 5291 |
"wrong offset for field '", |
| 5292 |
PyUnicode_AsUTF8(fname), "'") < 0) |
| 5293 |
goto finally; |
| 5294 |
byteoffset = foffset; |
| 5295 |
} |
| 5296 |
|
| 5297 |
if (PyUnicode_GetLength(fname) == 0 && |
| 5298 |
ftype->ct_flags & (CT_STRUCT|CT_UNION)) { |
| 5299 |
/* a nested anonymous struct or union */ |
| 5300 |
CFieldObject *cfsrc = (CFieldObject *)ftype->ct_extra; |
| 5301 |
for (; cfsrc != NULL; cfsrc = cfsrc->cf_next) { |
| 5302 |
/* broken complexity in the call to get_field_name(), |
| 5303 |
but we'll assume you never do that with nested |
| 5304 |
anonymous structures with thousand of fields */ |
| 5305 |
*previous = _add_field(interned_fields, |
| 5306 |
get_field_name(ftype, cfsrc), |
| 5307 |
cfsrc->cf_type, |
| 5308 |
byteoffset + cfsrc->cf_offset, |
| 5309 |
cfsrc->cf_bitshift, |
| 5310 |
cfsrc->cf_bitsize, |
| 5311 |
cfsrc->cf_flags | fflags); |
| 5312 |
if (*previous == NULL) |
| 5313 |
goto finally; |
| 5314 |
previous = &(*previous)->cf_next; |
| 5315 |
} |
| 5316 |
/* always forbid such structures from being passed by value */ |
| 5317 |
ct->ct_flags_mut |= CT_CUSTOM_FIELD_POS; |
| 5318 |
} |
| 5319 |
else { |
| 5320 |
*previous = _add_field(interned_fields, fname, ftype, |
| 5321 |
byteoffset, bs_flag, -1, fflags); |
| 5322 |
if (*previous == NULL) |
| 5323 |
goto finally; |
| 5324 |
previous = &(*previous)->cf_next; |
| 5325 |
} |
| 5326 |
if (ftype->ct_size >= 0) |
| 5327 |
byteoffset += ftype->ct_size; |
| 5328 |
prev_bitfield_size = 0; |
| 5329 |
} |
| 5330 |
else { |
| 5331 |
/* this is the case of a bitfield */ |
| 5332 |
Py_ssize_t field_offset_bytes; |
| 5333 |
int bits_already_occupied, bitshift; |
| 5334 |
|
| 5335 |
if (foffset >= 0) { |
| 5336 |
PyErr_Format(PyExc_TypeError, |
| 5337 |
"field '%s.%s' is a bitfield, " |
| 5338 |
"but a fixed offset is specified", |
| 5339 |
ct->ct_name, PyUnicode_AsUTF8(fname)); |
| 5340 |
goto finally; |
| 5341 |
} |
| 5342 |
|
| 5343 |
if (!(ftype->ct_flags & (CT_PRIMITIVE_SIGNED | |
| 5344 |
CT_PRIMITIVE_UNSIGNED | |
| 5345 |
CT_PRIMITIVE_CHAR))) { |
| 5346 |
PyErr_Format(PyExc_TypeError, |
| 5347 |
"field '%s.%s' declared as '%s' cannot be a bit field", |
| 5348 |
ct->ct_name, PyUnicode_AsUTF8(fname), |
| 5349 |
ftype->ct_name); |
| 5350 |
goto finally; |
| 5351 |
} |
| 5352 |
if (fbitsize > 8 * ftype->ct_size) { |
| 5353 |
PyErr_Format(PyExc_TypeError, |
| 5354 |
"bit field '%s.%s' is declared '%s:%d', which " |
| 5355 |
"exceeds the width of the type", |
| 5356 |
ct->ct_name, PyUnicode_AsUTF8(fname), |
| 5357 |
ftype->ct_name, fbitsize); |
| 5358 |
goto finally; |
| 5359 |
} |
| 5360 |
|
| 5361 |
/* compute the starting position of the theoretical field |
| 5362 |
that covers a complete 'ftype', inside of which we will |
| 5363 |
locate the real bitfield */ |
| 5364 |
field_offset_bytes = byteoffset; |
| 5365 |
field_offset_bytes &= ~(falign - 1); |
| 5366 |
|
| 5367 |
if (fbitsize == 0) { |
| 5368 |
if (PyUnicode_GetLength(fname) > 0) { |
| 5369 |
PyErr_Format(PyExc_TypeError, |
| 5370 |
"field '%s.%s' is declared with :0", |
| 5371 |
ct->ct_name, PyUnicode_AsUTF8(fname)); |
| 5372 |
goto finally; |
| 5373 |
} |
| 5374 |
if (!(sflags & SF_MSVC_BITFIELDS)) { |
| 5375 |
/* GCC's notion of "ftype :0;" */ |
| 5376 |
|
| 5377 |
/* pad byteoffset to a value aligned for "ftype" */ |
| 5378 |
if (ROUNDUP_BYTES(byteoffset, bitoffset) > field_offset_bytes) { |
| 5379 |
field_offset_bytes += falign; |
| 5380 |
assert(byteoffset < field_offset_bytes); |
| 5381 |
} |
| 5382 |
byteoffset = field_offset_bytes; |
| 5383 |
bitoffset = 0; |
| 5384 |
} |
| 5385 |
else { |
| 5386 |
/* MSVC's notion of "ftype :0;" */ |
| 5387 |
|
| 5388 |
/* Mostly ignored. It seems they only serve as |
| 5389 |
separator between other bitfields, to force them |
| 5390 |
into separate words. */ |
| 5391 |
} |
| 5392 |
prev_bitfield_size = 0; |
| 5393 |
} |
| 5394 |
else { |
| 5395 |
if (!(sflags & SF_MSVC_BITFIELDS)) { |
| 5396 |
/* GCC's algorithm */ |
| 5397 |
|
| 5398 |
/* Can the field start at the offset given by 'boffset'? It |
| 5399 |
can if it would entirely fit into an aligned ftype field. */ |
| 5400 |
bits_already_occupied = (byteoffset-field_offset_bytes) * 8 |
| 5401 |
+ bitoffset; |
| 5402 |
|
| 5403 |
if (bits_already_occupied + fbitsize > 8 * ftype->ct_size) { |
| 5404 |
/* it would not fit, we need to start at the next |
| 5405 |
allowed position */ |
| 5406 |
if ((sflags & SF_PACKED) && |
| 5407 |
(bits_already_occupied & 7)) { |
| 5408 |
PyErr_Format(PyExc_NotImplementedError, |
| 5409 |
"with 'packed', gcc would compile field " |
| 5410 |
"'%s.%s' to reuse some bits in the previous " |
| 5411 |
"field", ct->ct_name, PyUnicode_AsUTF8(fname)); |
| 5412 |
goto finally; |
| 5413 |
} |
| 5414 |
field_offset_bytes += falign; |
| 5415 |
assert(byteoffset < field_offset_bytes); |
| 5416 |
byteoffset = field_offset_bytes; |
| 5417 |
bitoffset = 0; |
| 5418 |
bitshift = 0; |
| 5419 |
} |
| 5420 |
else { |
| 5421 |
bitshift = bits_already_occupied; |
| 5422 |
assert(bitshift >= 0); |
| 5423 |
} |
| 5424 |
bitoffset += fbitsize; |
| 5425 |
byteoffset += (bitoffset >> 3); |
| 5426 |
bitoffset &= 7; |
| 5427 |
} |
| 5428 |
else { |
| 5429 |
/* MSVC's algorithm */ |
| 5430 |
|
| 5431 |
/* A bitfield is considered as taking the full width |
| 5432 |
of their declared type. It can share some bits |
| 5433 |
with the previous field only if it was also a |
| 5434 |
bitfield and used a type of the same size. */ |
| 5435 |
if (prev_bitfield_size == ftype->ct_size && |
| 5436 |
prev_bitfield_free >= fbitsize) { |
| 5437 |
/* yes: reuse */ |
| 5438 |
bitshift = 8 * prev_bitfield_size - prev_bitfield_free; |
| 5439 |
} |
| 5440 |
else { |
| 5441 |
/* no: start a new full field */ |
| 5442 |
byteoffset = ROUNDUP_BYTES(byteoffset, bitoffset); |
| 5443 |
bitoffset = 0; |
| 5444 |
/* align */ |
| 5445 |
byteoffset = (byteoffset + falign-1) & ~(falign-1); |
| 5446 |
byteoffset += ftype->ct_size; |
| 5447 |
bitshift = 0; |
| 5448 |
prev_bitfield_size = ftype->ct_size; |
| 5449 |
prev_bitfield_free = 8 * prev_bitfield_size; |
| 5450 |
} |
| 5451 |
prev_bitfield_free -= fbitsize; |
| 5452 |
field_offset_bytes = byteoffset - ftype->ct_size; |
| 5453 |
} |
| 5454 |
if (sflags & SF_GCC_BIG_ENDIAN) |
| 5455 |
bitshift = 8 * ftype->ct_size - fbitsize - bitshift; |
| 5456 |
|
| 5457 |
if (PyUnicode_GetLength(fname) > 0) { |
| 5458 |
|
| 5459 |
*previous = _add_field(interned_fields, fname, ftype, |
| 5460 |
field_offset_bytes, bitshift, fbitsize, |
| 5461 |
fflags); |
| 5462 |
if (*previous == NULL) |
| 5463 |
goto finally; |
| 5464 |
previous = &(*previous)->cf_next; |
| 5465 |
} |
| 5466 |
} |
| 5467 |
} |
| 5468 |
|
| 5469 |
assert(bitoffset == (bitoffset & 7)); |
| 5470 |
if (ROUNDUP_BYTES(byteoffset, bitoffset) > byteoffsetmax) |
| 5471 |
byteoffsetmax = ROUNDUP_BYTES(byteoffset, bitoffset); |
| 5472 |
} |
| 5473 |
*previous = NULL; |
| 5474 |
|
| 5475 |
/* Like C, if the size of this structure would be zero, we compute it |
| 5476 |
as 1 instead. But for ctypes support, we allow the manually- |
| 5477 |
specified totalsize to be zero in this case. */ |
| 5478 |
alignedsize = (byteoffsetmax + alignment - 1) & ~(alignment-1); |
| 5479 |
if (alignedsize == 0) |
| 5480 |
alignedsize = 1; |
| 5481 |
|
| 5482 |
if (totalsize < 0) { |
| 5483 |
totalsize = alignedsize; |
| 5484 |
} |
| 5485 |
else { |
| 5486 |
if (detect_custom_layout(ct, sflags, alignedsize, |
| 5487 |
totalsize, "wrong total size", "", "") < 0) |
| 5488 |
goto finally; |
| 5489 |
if (totalsize < byteoffsetmax) { |
| 5490 |
PyErr_Format(PyExc_TypeError, |
| 5491 |
"%s cannot be of size %zd: there are fields at least " |
| 5492 |
"up to %zd", ct->ct_name, totalsize, byteoffsetmax); |
| 5493 |
goto finally; |
| 5494 |
} |
| 5495 |
} |
| 5496 |
if (totalalignment < 0) { |
| 5497 |
totalalignment = alignment; |
| 5498 |
} |
| 5499 |
else { |
| 5500 |
if (detect_custom_layout(ct, sflags, alignment, totalalignment, |
| 5501 |
"wrong total alignment", "", "") < 0) |
| 5502 |
goto finally; |
| 5503 |
} |
| 5504 |
|
| 5505 |
cffi_set_size(ct, totalsize); |
| 5506 |
ct->ct_length = totalalignment; |
| 5507 |
ct->ct_stuff = interned_fields; |
| 5508 |
cffi_set_flag(ct->ct_unrealized_struct_or_union, 0); |
| 5509 |
res = Py_None; |
| 5510 |
Py_INCREF(res); |
| 5511 |
|
| 5512 |
finally:; |
| 5513 |
if (res == NULL) { |
| 5514 |
ct->ct_extra = NULL; |
| 5515 |
Py_XDECREF(interned_fields); |
| 5516 |
} |
| 5517 |
return res; |
| 5518 |
} |
| 5519 |
|
| 5520 |
static PyObject *b_complete_struct_or_union(PyObject *self, PyObject *args) |
| 5521 |
{ |
| 5522 |
CTypeDescrObject *ct; |
| 5523 |
PyObject *fields, *ignored; |
| 5524 |
Py_ssize_t totalsize = -1; |
| 5525 |
int totalalignment = -1; |
| 5526 |
int sflags = 0; |
| 5527 |
int pack = 0; |
| 5528 |
|
| 5529 |
if (!PyArg_ParseTuple(args, "O!O!|Oniii:complete_struct_or_union", |
| 5530 |
&CTypeDescr_Type, &ct, |
| 5531 |
&PyList_Type, &fields, |
| 5532 |
&ignored, &totalsize, &totalalignment, &sflags, |
| 5533 |
&pack)) |
| 5534 |
return NULL; |
| 5535 |
|
| 5536 |
PyObject *res; |
| 5537 |
CFFI_LOCK(); |
| 5538 |
res = b_complete_struct_or_union_lock_held(ct, fields, totalsize, totalalignment, sflags, pack); |
| 5539 |
CFFI_UNLOCK(); |
| 5540 |
return res; |
| 5541 |
} |
| 5542 |
|
| 5543 |
|
| 5544 |
struct funcbuilder_s { |
| 5545 |
Py_ssize_t nb_bytes; |
| 5546 |
char *bufferp; |
| 5547 |
ffi_type **atypes; |
| 5548 |
ffi_type *rtype; |
| 5549 |
Py_ssize_t nargs; |
| 5550 |
CTypeDescrObject *fct; |
| 5551 |
}; |
| 5552 |
|
| 5553 |
static void *fb_alloc(struct funcbuilder_s *fb, Py_ssize_t size) |
| 5554 |
{ |
| 5555 |
if (fb->bufferp == NULL) { |
| 5556 |
fb->nb_bytes += size; |
| 5557 |
return NULL; |
| 5558 |
} |
| 5559 |
else { |
| 5560 |
char *result = fb->bufferp; |
| 5561 |
fb->bufferp += size; |
| 5562 |
return result; |
| 5563 |
} |
| 5564 |
} |
| 5565 |
|
| 5566 |
#define SUPPORTED_IN_API_MODE \ |
| 5567 |
" are only supported as %s if the function is " \ |
| 5568 |
"'API mode' and non-variadic (i.e. declared inside ffibuilder" \ |
| 5569 |
".cdef()+ffibuilder.set_source() and not taking a final '...' " \ |
| 5570 |
"argument)" |
| 5571 |
|
| 5572 |
static ffi_type *fb_unsupported(CTypeDescrObject *ct, const char *place, |
| 5573 |
const char *detail) |
| 5574 |
{ |
| 5575 |
PyErr_Format(PyExc_NotImplementedError, |
| 5576 |
"ctype '%s' not supported as %s. %s. " |
| 5577 |
"Such structs" SUPPORTED_IN_API_MODE, |
| 5578 |
ct->ct_name, place, detail, place); |
| 5579 |
return NULL; |
| 5580 |
} |
| 5581 |
|
| 5582 |
static ffi_type *fb_fill_type(struct funcbuilder_s *fb, CTypeDescrObject *ct, |
| 5583 |
int is_result_type) |
| 5584 |
{ |
| 5585 |
const char *place = is_result_type ? "return value" : "argument"; |
| 5586 |
|
| 5587 |
if (ct->ct_flags & (CT_PRIMITIVE_ANY & ~CT_PRIMITIVE_COMPLEX)) { |
| 5588 |
return (ffi_type *)ct->ct_extra; |
| 5589 |
} |
| 5590 |
else if (ct->ct_flags & (CT_POINTER|CT_FUNCTIONPTR)) { |
| 5591 |
return &ffi_type_pointer; |
| 5592 |
} |
| 5593 |
else if ((ct->ct_flags & CT_VOID) && is_result_type) { |
| 5594 |
return &ffi_type_void; |
| 5595 |
} |
| 5596 |
|
| 5597 |
if (cffi_get_size(ct) <= 0) { |
| 5598 |
PyErr_Format(PyExc_TypeError, |
| 5599 |
ct->ct_size < 0 ? "ctype '%s' has incomplete type" |
| 5600 |
: "ctype '%s' has size 0", |
| 5601 |
ct->ct_name); |
| 5602 |
return NULL; |
| 5603 |
} |
| 5604 |
if (ct->ct_flags & CT_STRUCT) { |
| 5605 |
ffi_type *ffistruct, *ffifield; |
| 5606 |
ffi_type **elements; |
| 5607 |
Py_ssize_t i, n, nflat; |
| 5608 |
CFieldObject *cf; |
| 5609 |
|
| 5610 |
/* We can't pass a struct that was completed by verify(). |
| 5611 |
Issue: assume verify() is given "struct { long b; ...; }". |
| 5612 |
Then it will complete it in the same way whether it is actually |
| 5613 |
"struct { long a, b; }" or "struct { double a; long b; }". |
| 5614 |
But on 64-bit UNIX, these two structs are passed by value |
| 5615 |
differently: e.g. on x86-64, "b" ends up in register "rsi" in |
| 5616 |
the first case and "rdi" in the second case. |
| 5617 |
|
| 5618 |
Another reason for CT_CUSTOM_FIELD_POS would be anonymous |
| 5619 |
nested structures: we lost the information about having it |
| 5620 |
here, so better safe (and forbid it) than sorry (and maybe |
| 5621 |
crash). Note: it seems we only get in this case with |
| 5622 |
ffi.verify(). |
| 5623 |
*/ |
| 5624 |
if (force_lazy_struct(ct) < 0) |
| 5625 |
return NULL; |
| 5626 |
if (ct->ct_flags_mut & CT_CUSTOM_FIELD_POS) { |
| 5627 |
/* these NotImplementedErrors may be caught and ignored until |
| 5628 |
a real call is made to a function of this type */ |
| 5629 |
return fb_unsupported(ct, place, |
| 5630 |
"It is a struct declared with \"...;\", but the C " |
| 5631 |
"calling convention may depend on the missing fields; " |
| 5632 |
"or, it contains anonymous struct/unions"); |
| 5633 |
} |
| 5634 |
/* Another reason: __attribute__((packed)) is not supported by libffi. |
| 5635 |
*/ |
| 5636 |
if (ct->ct_flags_mut & CT_WITH_PACKED_CHANGE) { |
| 5637 |
return fb_unsupported(ct, place, |
| 5638 |
"It is a 'packed' structure, with a different layout than " |
| 5639 |
"expected by libffi"); |
| 5640 |
} |
| 5641 |
|
| 5642 |
n = PyDict_Size(ct->ct_stuff); |
| 5643 |
nflat = 0; |
| 5644 |
|
| 5645 |
/* walk the fields, expanding arrays into repetitions; first, |
| 5646 |
only count how many flattened fields there are */ |
| 5647 |
cf = (CFieldObject *)ct->ct_extra; |
| 5648 |
for (i=0; i<n; i++) { |
| 5649 |
Py_ssize_t flat; |
| 5650 |
CTypeDescrObject *ct1; |
| 5651 |
assert(cf != NULL); |
| 5652 |
if (cf->cf_bitshift >= 0) { |
| 5653 |
return fb_unsupported(ct, place, |
| 5654 |
"It is a struct with bit fields, which libffi does not " |
| 5655 |
"support"); |
| 5656 |
} |
| 5657 |
flat = 1; |
| 5658 |
ct1 = cf->cf_type; |
| 5659 |
while (ct1->ct_flags & CT_ARRAY) { |
| 5660 |
flat *= ct1->ct_length; |
| 5661 |
ct1 = ct1->ct_itemdescr; |
| 5662 |
} |
| 5663 |
if (flat <= 0) { |
| 5664 |
return fb_unsupported(ct, place, |
| 5665 |
"It is a struct with a zero-length array, which libffi " |
| 5666 |
"does not support"); |
| 5667 |
} |
| 5668 |
nflat += flat; |
| 5669 |
cf = cf->cf_next; |
| 5670 |
} |
| 5671 |
assert(cf == NULL); |
| 5672 |
|
| 5673 |
/* next, allocate and fill the flattened list */ |
| 5674 |
elements = fb_alloc(fb, (nflat + 1) * sizeof(ffi_type*)); |
| 5675 |
nflat = 0; |
| 5676 |
cf = (CFieldObject *)ct->ct_extra; |
| 5677 |
for (i=0; i<n; i++) { |
| 5678 |
Py_ssize_t j, flat = 1; |
| 5679 |
CTypeDescrObject *ct = cf->cf_type; |
| 5680 |
while (ct->ct_flags & CT_ARRAY) { |
| 5681 |
flat *= ct->ct_length; |
| 5682 |
ct = ct->ct_itemdescr; |
| 5683 |
} |
| 5684 |
ffifield = fb_fill_type(fb, ct, 0); |
| 5685 |
if (PyErr_Occurred()) |
| 5686 |
return NULL; |
| 5687 |
if (elements != NULL) { |
| 5688 |
for (j=0; j<flat; j++) |
| 5689 |
elements[nflat++] = ffifield; |
| 5690 |
} |
| 5691 |
cf = cf->cf_next; |
| 5692 |
} |
| 5693 |
|
| 5694 |
/* finally, allocate the FFI_TYPE_STRUCT */ |
| 5695 |
ffistruct = fb_alloc(fb, sizeof(ffi_type)); |
| 5696 |
if (ffistruct != NULL) { |
| 5697 |
elements[nflat] = NULL; |
| 5698 |
ffistruct->size = ct->ct_size; |
| 5699 |
ffistruct->alignment = ct->ct_length; |
| 5700 |
ffistruct->type = FFI_TYPE_STRUCT; |
| 5701 |
ffistruct->elements = elements; |
| 5702 |
} |
| 5703 |
return ffistruct; |
| 5704 |
} |
| 5705 |
else if (ct->ct_flags & CT_UNION) { |
| 5706 |
PyErr_Format(PyExc_NotImplementedError, |
| 5707 |
"ctype '%s' not supported as %s by libffi. " |
| 5708 |
"Unions" SUPPORTED_IN_API_MODE, |
| 5709 |
ct->ct_name, place, place); |
| 5710 |
return NULL; |
| 5711 |
} |
| 5712 |
else { |
| 5713 |
char *extra = ""; |
| 5714 |
if (ct->ct_flags & CT_PRIMITIVE_COMPLEX) |
| 5715 |
extra = " (the support for complex types inside libffi " |
| 5716 |
"is mostly missing at this point, so CFFI only " |
| 5717 |
"supports complex types as arguments or return " |
| 5718 |
"value in API-mode functions)"; |
| 5719 |
|
| 5720 |
PyErr_Format(PyExc_NotImplementedError, |
| 5721 |
"ctype '%s' (size %zd) not supported as %s%s", |
| 5722 |
ct->ct_name, ct->ct_size, place, extra); |
| 5723 |
return NULL; |
| 5724 |
} |
| 5725 |
} |
| 5726 |
|
| 5727 |
#define ALIGN_TO(n, a) ((n) + ((a)-1)) & ~((a)-1) |
| 5728 |
#define ALIGN_ARG(n) ALIGN_TO(n, 8) |
| 5729 |
|
| 5730 |
static int fb_build(struct funcbuilder_s *fb, PyObject *fargs, |
| 5731 |
CTypeDescrObject *fresult) |
| 5732 |
{ |
| 5733 |
Py_ssize_t i, nargs = PyTuple_GET_SIZE(fargs); |
| 5734 |
Py_ssize_t exchange_offset; |
| 5735 |
cif_description_t *cif_descr; |
| 5736 |
|
| 5737 |
/* ffi buffer: start with a cif_description */ |
| 5738 |
cif_descr = fb_alloc(fb, sizeof(cif_description_t) + |
| 5739 |
nargs * sizeof(Py_ssize_t)); |
| 5740 |
|
| 5741 |
/* ffi buffer: next comes an array of 'ffi_type*', one per argument */ |
| 5742 |
fb->atypes = fb_alloc(fb, nargs * sizeof(ffi_type*)); |
| 5743 |
fb->nargs = nargs; |
| 5744 |
|
| 5745 |
/* ffi buffer: next comes the result type */ |
| 5746 |
fb->rtype = fb_fill_type(fb, fresult, 1); |
| 5747 |
if (PyErr_Occurred()) |
| 5748 |
return -1; |
| 5749 |
if (cif_descr != NULL) { |
| 5750 |
/* exchange data size */ |
| 5751 |
/* first, enough room for an array of 'nargs' pointers */ |
| 5752 |
exchange_offset = nargs * sizeof(void*); |
| 5753 |
/* then enough room for the result --- which means at least |
| 5754 |
sizeof(ffi_arg), according to the ffi docs, but we also |
| 5755 |
align according to the result type, for issue #531 */ |
| 5756 |
exchange_offset = ALIGN_TO(exchange_offset, fb->rtype->alignment); |
| 5757 |
exchange_offset = ALIGN_ARG(exchange_offset); |
| 5758 |
cif_descr->exchange_offset_arg[0] = exchange_offset; |
| 5759 |
i = fb->rtype->size; |
| 5760 |
if (i < (Py_ssize_t)sizeof(ffi_arg)) |
| 5761 |
i = sizeof(ffi_arg); |
| 5762 |
exchange_offset += i; |
| 5763 |
} |
| 5764 |
else |
| 5765 |
exchange_offset = 0; /* not used */ |
| 5766 |
|
| 5767 |
/* loop over the arguments */ |
| 5768 |
for (i=0; i<nargs; i++) { |
| 5769 |
CTypeDescrObject *farg; |
| 5770 |
ffi_type *atype; |
| 5771 |
|
| 5772 |
farg = (CTypeDescrObject *)PyTuple_GET_ITEM(fargs, i); |
| 5773 |
/* convert arrays to pointers */ |
| 5774 |
if (farg->ct_flags & CT_ARRAY) |
| 5775 |
farg = (CTypeDescrObject *)farg->ct_stuff; |
| 5776 |
|
| 5777 |
/* ffi buffer: fill in the ffi for the i'th argument */ |
| 5778 |
assert(farg != NULL); |
| 5779 |
atype = fb_fill_type(fb, farg, 0); |
| 5780 |
if (PyErr_Occurred()) |
| 5781 |
return -1; |
| 5782 |
|
| 5783 |
if (fb->atypes != NULL) { |
| 5784 |
fb->atypes[i] = atype; |
| 5785 |
/* exchange data size */ |
| 5786 |
exchange_offset = ALIGN_TO(exchange_offset, atype->alignment); |
| 5787 |
exchange_offset = ALIGN_ARG(exchange_offset); |
| 5788 |
cif_descr->exchange_offset_arg[1 + i] = exchange_offset; |
| 5789 |
exchange_offset += atype->size; |
| 5790 |
} |
| 5791 |
} |
| 5792 |
|
| 5793 |
if (cif_descr != NULL) { |
| 5794 |
/* exchange data size */ |
| 5795 |
/* we also align it to the next multiple of 8, in an attempt to |
| 5796 |
work around bugs(?) of libffi like #241 */ |
| 5797 |
cif_descr->exchange_size = ALIGN_ARG(exchange_offset); |
| 5798 |
} |
| 5799 |
return 0; |
| 5800 |
} |
| 5801 |
|
| 5802 |
#undef ALIGN_ARG |
| 5803 |
#undef ALIGN_TO |
| 5804 |
|
| 5805 |
static void fb_cat_name(struct funcbuilder_s *fb, const char *piece, |
| 5806 |
int piecelen) |
| 5807 |
{ |
| 5808 |
if (fb->bufferp == NULL) { |
| 5809 |
fb->nb_bytes += piecelen; |
| 5810 |
} |
| 5811 |
else { |
| 5812 |
memcpy(fb->bufferp, piece, piecelen); |
| 5813 |
fb->bufferp += piecelen; |
| 5814 |
} |
| 5815 |
} |
| 5816 |
|
| 5817 |
static int fb_build_name(struct funcbuilder_s *fb, const char *repl, |
| 5818 |
CTypeDescrObject **pfargs, Py_ssize_t nargs, |
| 5819 |
CTypeDescrObject *fresult, int ellipsis) |
| 5820 |
{ |
| 5821 |
Py_ssize_t i; |
| 5822 |
fb->nargs = nargs; |
| 5823 |
|
| 5824 |
/* name: the function type name we build here is, like in C, made |
| 5825 |
as follows: |
| 5826 |
|
| 5827 |
RESULT_TYPE_HEAD (*)(ARG_1_TYPE, ARG_2_TYPE, etc) RESULT_TYPE_TAIL |
| 5828 |
*/ |
| 5829 |
fb_cat_name(fb, fresult->ct_name, fresult->ct_name_position); |
| 5830 |
if (repl[0] != '(' && |
| 5831 |
fresult->ct_name[fresult->ct_name_position - 1] != '*') |
| 5832 |
fb_cat_name(fb, " ", 1); /* add a space */ |
| 5833 |
fb_cat_name(fb, repl, strlen(repl)); |
| 5834 |
if (fb->fct) { |
| 5835 |
i = strlen(repl) - 1; /* between '(*' and ')' */ |
| 5836 |
assert(repl[i] == ')'); |
| 5837 |
fb->fct->ct_name_position = fresult->ct_name_position + i; |
| 5838 |
} |
| 5839 |
fb_cat_name(fb, "(", 1); |
| 5840 |
|
| 5841 |
/* loop over the arguments */ |
| 5842 |
for (i=0; i<nargs; i++) { |
| 5843 |
CTypeDescrObject *farg; |
| 5844 |
|
| 5845 |
farg = pfargs[i]; |
| 5846 |
if (!CTypeDescr_Check(farg)) { |
| 5847 |
PyErr_SetString(PyExc_TypeError, "expected a tuple of ctypes"); |
| 5848 |
return -1; |
| 5849 |
} |
| 5850 |
/* name: concatenate the name of the i'th argument's type */ |
| 5851 |
if (i > 0) |
| 5852 |
fb_cat_name(fb, ", ", 2); |
| 5853 |
fb_cat_name(fb, farg->ct_name, strlen(farg->ct_name)); |
| 5854 |
} |
| 5855 |
|
| 5856 |
/* name: add the '...' if needed */ |
| 5857 |
if (ellipsis) { |
| 5858 |
if (nargs > 0) |
| 5859 |
fb_cat_name(fb, ", ", 2); |
| 5860 |
fb_cat_name(fb, "...", 3); |
| 5861 |
} |
| 5862 |
|
| 5863 |
/* name: concatenate the tail of the result type */ |
| 5864 |
fb_cat_name(fb, ")", 1); |
| 5865 |
fb_cat_name(fb, fresult->ct_name + fresult->ct_name_position, |
| 5866 |
strlen(fresult->ct_name) - fresult->ct_name_position + 1); |
| 5867 |
return 0; |
| 5868 |
} |
| 5869 |
|
| 5870 |
static CTypeDescrObject *fb_prepare_ctype(struct funcbuilder_s *fb, |
| 5871 |
PyObject *fargs, |
| 5872 |
CTypeDescrObject *fresult, |
| 5873 |
int ellipsis, int fabi) |
| 5874 |
{ |
| 5875 |
CTypeDescrObject *fct, **pfargs; |
| 5876 |
Py_ssize_t nargs; |
| 5877 |
char *repl = "(*)"; |
| 5878 |
|
| 5879 |
fb->nb_bytes = 0; |
| 5880 |
fb->bufferp = NULL; |
| 5881 |
fb->fct = NULL; |
| 5882 |
|
| 5883 |
pfargs = (CTypeDescrObject **)&PyTuple_GET_ITEM(fargs, 0); |
| 5884 |
nargs = PyTuple_GET_SIZE(fargs); |
| 5885 |
#if defined(MS_WIN32) && !defined(_WIN64) |
| 5886 |
if (fabi == FFI_STDCALL) |
| 5887 |
repl = "(__stdcall *)"; |
| 5888 |
#endif |
| 5889 |
|
| 5890 |
/* compute the total size needed for the name */ |
| 5891 |
if (fb_build_name(fb, repl, pfargs, nargs, fresult, ellipsis) < 0) |
| 5892 |
return NULL; |
| 5893 |
|
| 5894 |
/* allocate the function type */ |
| 5895 |
fct = ctypedescr_new(fb->nb_bytes); |
| 5896 |
if (fct == NULL) |
| 5897 |
return NULL; |
| 5898 |
fb->fct = fct; |
| 5899 |
|
| 5900 |
/* call again fb_build_name() to really build the ct_name */ |
| 5901 |
fb->bufferp = fct->ct_name; |
| 5902 |
if (fb_build_name(fb, repl, pfargs, nargs, fresult, ellipsis) < 0) |
| 5903 |
goto error; |
| 5904 |
assert(fb->bufferp == fct->ct_name + fb->nb_bytes); |
| 5905 |
|
| 5906 |
fct->ct_extra = NULL; |
| 5907 |
fct->ct_size = sizeof(void(*)(void)); |
| 5908 |
fct->ct_flags = CT_FUNCTIONPTR; |
| 5909 |
return fct; |
| 5910 |
|
| 5911 |
error: |
| 5912 |
Py_DECREF(fct); |
| 5913 |
return NULL; |
| 5914 |
} |
| 5915 |
|
| 5916 |
static cif_description_t *fb_prepare_cif(PyObject *fargs, |
| 5917 |
CTypeDescrObject *fresult, |
| 5918 |
Py_ssize_t variadic_nargs_declared, |
| 5919 |
ffi_abi fabi) |
| 5920 |
|
| 5921 |
{ |
| 5922 |
char *buffer; |
| 5923 |
cif_description_t *cif_descr; |
| 5924 |
struct funcbuilder_s funcbuffer; |
| 5925 |
ffi_status status = (ffi_status)-1; |
| 5926 |
|
| 5927 |
funcbuffer.nb_bytes = 0; |
| 5928 |
funcbuffer.bufferp = NULL; |
| 5929 |
|
| 5930 |
/* compute the total size needed in the buffer for libffi */ |
| 5931 |
if (fb_build(&funcbuffer, fargs, fresult) < 0) |
| 5932 |
return NULL; |
| 5933 |
|
| 5934 |
/* allocate the buffer */ |
| 5935 |
buffer = PyObject_Malloc(funcbuffer.nb_bytes); |
| 5936 |
if (buffer == NULL) { |
| 5937 |
PyErr_NoMemory(); |
| 5938 |
return NULL; |
| 5939 |
} |
| 5940 |
|
| 5941 |
/* call again fb_build() to really build the libffi data structures */ |
| 5942 |
funcbuffer.bufferp = buffer; |
| 5943 |
if (fb_build(&funcbuffer, fargs, fresult) < 0) |
| 5944 |
goto error; |
| 5945 |
assert(funcbuffer.bufferp == buffer + funcbuffer.nb_bytes); |
| 5946 |
|
| 5947 |
cif_descr = (cif_description_t *)buffer; |
| 5948 |
|
| 5949 |
/* use `ffi_prep_cif_var` if necessary and available */ |
| 5950 |
#if CFFI_CHECK_FFI_PREP_CIF_VAR_MAYBE |
| 5951 |
if (variadic_nargs_declared >= 0) { |
| 5952 |
if (CFFI_CHECK_FFI_PREP_CIF_VAR) { |
| 5953 |
status = ffi_prep_cif_var(&cif_descr->cif, fabi, |
| 5954 |
variadic_nargs_declared, funcbuffer.nargs, |
| 5955 |
funcbuffer.rtype, funcbuffer.atypes); |
| 5956 |
} |
| 5957 |
} |
| 5958 |
#endif |
| 5959 |
|
| 5960 |
if (status == (ffi_status)-1) { |
| 5961 |
status = ffi_prep_cif(&cif_descr->cif, fabi, funcbuffer.nargs, |
| 5962 |
funcbuffer.rtype, funcbuffer.atypes); |
| 5963 |
} |
| 5964 |
|
| 5965 |
if (status != FFI_OK) { |
| 5966 |
PyErr_SetString(PyExc_SystemError, |
| 5967 |
"libffi failed to build this function type"); |
| 5968 |
goto error; |
| 5969 |
} |
| 5970 |
return cif_descr; |
| 5971 |
|
| 5972 |
error: |
| 5973 |
PyObject_Free(buffer); |
| 5974 |
return NULL; |
| 5975 |
} |
| 5976 |
|
| 5977 |
static PyObject *new_function_type(PyObject *fargs, /* tuple */ |
| 5978 |
CTypeDescrObject *fresult, |
| 5979 |
int ellipsis, int fabi) |
| 5980 |
{ |
| 5981 |
PyObject *fabiobj; |
| 5982 |
CTypeDescrObject *fct; |
| 5983 |
struct funcbuilder_s funcbuilder; |
| 5984 |
Py_ssize_t i; |
| 5985 |
const void **unique_key; |
| 5986 |
|
| 5987 |
if ((cffi_get_size(fresult) < 0 && !(fresult->ct_flags & CT_VOID)) || |
| 5988 |
(fresult->ct_flags & CT_ARRAY)) { |
| 5989 |
char *msg; |
| 5990 |
if (fresult->ct_flags & CT_IS_OPAQUE) |
| 5991 |
msg = "result type '%s' is opaque"; |
| 5992 |
else if (cffi_check_flag(fresult->ct_unrealized_struct_or_union)) |
| 5993 |
msg = "result type '%s' is not yet initialized"; |
| 5994 |
else if (cffi_check_flag(fresult->ct_under_construction)) |
| 5995 |
msg = "result type '%s' is under construction"; |
| 5996 |
else |
| 5997 |
msg = "invalid result type: '%s'"; |
| 5998 |
PyErr_Format(PyExc_TypeError, msg, fresult->ct_name); |
| 5999 |
return NULL; |
| 6000 |
} |
| 6001 |
|
| 6002 |
fct = fb_prepare_ctype(&funcbuilder, fargs, fresult, ellipsis, fabi); |
| 6003 |
if (fct == NULL) |
| 6004 |
return NULL; |
| 6005 |
|
| 6006 |
if (!ellipsis) { |
| 6007 |
/* Functions with '...' varargs are stored without a cif_descr |
| 6008 |
at all. The cif is computed on every call from the actual |
| 6009 |
types passed in. For all other functions, the cif_descr |
| 6010 |
is computed here. */ |
| 6011 |
cif_description_t *cif_descr; |
| 6012 |
|
| 6013 |
cif_descr = fb_prepare_cif(fargs, fresult, -1, fabi); |
| 6014 |
if (cif_descr == NULL) { |
| 6015 |
if (PyErr_ExceptionMatches(PyExc_NotImplementedError)) { |
| 6016 |
PyErr_Clear(); /* will get the exception if we see an |
| 6017 |
actual call */ |
| 6018 |
} |
| 6019 |
else |
| 6020 |
goto error; |
| 6021 |
} |
| 6022 |
|
| 6023 |
fct->ct_extra = (char *)cif_descr; |
| 6024 |
} |
| 6025 |
|
| 6026 |
/* build the signature, given by a tuple of ctype objects */ |
| 6027 |
fct->ct_stuff = PyTuple_New(2 + funcbuilder.nargs); |
| 6028 |
if (fct->ct_stuff == NULL) |
| 6029 |
goto error; |
| 6030 |
fabiobj = PyLong_FromLong(fabi); |
| 6031 |
if (fabiobj == NULL) |
| 6032 |
goto error; |
| 6033 |
PyTuple_SET_ITEM(fct->ct_stuff, 0, fabiobj); |
| 6034 |
|
| 6035 |
Py_INCREF(fresult); |
| 6036 |
PyTuple_SET_ITEM(fct->ct_stuff, 1, (PyObject *)fresult); |
| 6037 |
for (i=0; i<funcbuilder.nargs; i++) { |
| 6038 |
PyObject *o = PyTuple_GET_ITEM(fargs, i); |
| 6039 |
/* convert arrays into pointers */ |
| 6040 |
if (((CTypeDescrObject *)o)->ct_flags & CT_ARRAY) |
| 6041 |
o = ((CTypeDescrObject *)o)->ct_stuff; |
| 6042 |
Py_INCREF(o); |
| 6043 |
PyTuple_SET_ITEM(fct->ct_stuff, 2 + i, o); |
| 6044 |
} |
| 6045 |
|
| 6046 |
/* [ctresult, ellipsis+abi, num_args, ctargs...] */ |
| 6047 |
unique_key = alloca((3 + funcbuilder.nargs) * sizeof(void *)); |
| 6048 |
unique_key[0] = fresult; |
| 6049 |
unique_key[1] = (const void *)(Py_ssize_t)((fabi << 1) | !!ellipsis); |
| 6050 |
unique_key[2] = (const void *)(Py_ssize_t)(funcbuilder.nargs); |
| 6051 |
for (i=0; i<funcbuilder.nargs; i++) |
| 6052 |
unique_key[3 + i] = PyTuple_GET_ITEM(fct->ct_stuff, 2 + i); |
| 6053 |
return get_unique_type(fct, unique_key, 3 + funcbuilder.nargs); |
| 6054 |
|
| 6055 |
error: |
| 6056 |
Py_DECREF(fct); |
| 6057 |
return NULL; |
| 6058 |
} |
| 6059 |
|
| 6060 |
static PyObject *b_new_function_type(PyObject *self, PyObject *args) |
| 6061 |
{ |
| 6062 |
PyObject *fargs; |
| 6063 |
CTypeDescrObject *fresult; |
| 6064 |
int ellipsis = 0, fabi = FFI_DEFAULT_ABI; |
| 6065 |
|
| 6066 |
if (!PyArg_ParseTuple(args, "O!O!|ii:new_function_type", |
| 6067 |
&PyTuple_Type, &fargs, |
| 6068 |
&CTypeDescr_Type, &fresult, |
| 6069 |
&ellipsis, |
| 6070 |
&fabi)) |
| 6071 |
return NULL; |
| 6072 |
|
| 6073 |
return new_function_type(fargs, fresult, ellipsis, fabi); |
| 6074 |
} |
| 6075 |
|
| 6076 |
static int convert_from_object_fficallback(char *result, |
| 6077 |
CTypeDescrObject *ctype, |
| 6078 |
PyObject *pyobj, |
| 6079 |
int encode_result_for_libffi) |
| 6080 |
{ |
| 6081 |
/* work work work around a libffi irregularity: for integer return |
| 6082 |
types we have to fill at least a complete 'ffi_arg'-sized result |
| 6083 |
buffer. */ |
| 6084 |
if (ctype->ct_size < (Py_ssize_t)sizeof(ffi_arg)) { |
| 6085 |
if (ctype->ct_flags & CT_VOID) { |
| 6086 |
if (pyobj == Py_None) { |
| 6087 |
return 0; |
| 6088 |
} |
| 6089 |
else { |
| 6090 |
PyErr_SetString(PyExc_TypeError, |
| 6091 |
"callback with the return type 'void' must return None"); |
| 6092 |
return -1; |
| 6093 |
} |
| 6094 |
} |
| 6095 |
if (!encode_result_for_libffi) |
| 6096 |
goto skip; |
| 6097 |
if (ctype->ct_flags & CT_PRIMITIVE_SIGNED) { |
| 6098 |
PY_LONG_LONG value; |
| 6099 |
/* It's probably fine to always zero-extend, but you never |
| 6100 |
know: maybe some code somewhere expects a negative |
| 6101 |
'short' result to be returned into EAX as a 32-bit |
| 6102 |
negative number. Better safe than sorry. This code |
| 6103 |
is about that case. Let's ignore this for enums. |
| 6104 |
*/ |
| 6105 |
/* do a first conversion only to detect overflows. This |
| 6106 |
conversion produces stuff that is otherwise ignored. */ |
| 6107 |
if (convert_from_object(result, ctype, pyobj) < 0) |
| 6108 |
return -1; |
| 6109 |
/* manual inlining and tweaking of convert_from_object() |
| 6110 |
in order to write a whole 'ffi_arg'. */ |
| 6111 |
value = _my_PyLong_AsLongLong(pyobj); |
| 6112 |
if (value == -1 && PyErr_Occurred()) |
| 6113 |
return -1; |
| 6114 |
write_raw_integer_data(result, value, sizeof(ffi_arg)); |
| 6115 |
return 0; |
| 6116 |
} |
| 6117 |
else if (ctype->ct_flags & (CT_PRIMITIVE_CHAR | CT_PRIMITIVE_SIGNED | |
| 6118 |
CT_PRIMITIVE_UNSIGNED | |
| 6119 |
CT_POINTER | CT_FUNCTIONPTR)) { |
| 6120 |
/* zero extension: fill the '*result' with zeros, and (on big- |
| 6121 |
endian machines) correct the 'result' pointer to write to. |
| 6122 |
We also do that for pointers, even though we're normally not |
| 6123 |
in this branch because ctype->ct_size == sizeof(ffi_arg) for |
| 6124 |
pointers---except on some architectures like x32 (issue #372). |
| 6125 |
*/ |
| 6126 |
memset(result, 0, sizeof(ffi_arg)); |
| 6127 |
#ifdef WORDS_BIGENDIAN |
| 6128 |
result += (sizeof(ffi_arg) - ctype->ct_size); |
| 6129 |
#endif |
| 6130 |
} |
| 6131 |
} |
| 6132 |
skip: |
| 6133 |
return convert_from_object(result, ctype, pyobj); |
| 6134 |
} |
| 6135 |
|
| 6136 |
static void _my_PyErr_WriteUnraisable(PyObject *t, PyObject *v, PyObject *tb, |
| 6137 |
char *objdescr, PyObject *obj, |
| 6138 |
char *extra_error_line) |
| 6139 |
{ |
| 6140 |
/* like PyErr_WriteUnraisable(), but write a full traceback */ |
| 6141 |
|
| 6142 |
/* PyErr_WriteUnraisable actually writes the full traceback anyway |
| 6143 |
from Python 3.4, but we can't really get the formatting of the |
| 6144 |
custom text to be what we want. We can do better from Python |
| 6145 |
3.8 by calling the new _PyErr_WriteUnraisableMsg(). |
| 6146 |
Luckily it's also Python 3.8 that adds new functionality that |
| 6147 |
people might want: the new sys.unraisablehook(). |
| 6148 |
*/ |
| 6149 |
PyObject *s; |
| 6150 |
int first_char; |
| 6151 |
assert(objdescr != NULL && objdescr[0] != 0); /* non-empty */ |
| 6152 |
first_char = objdescr[0]; |
| 6153 |
if (first_char >= 'A' && first_char <= 'Z') |
| 6154 |
first_char += 'a' - 'A'; /* lower() the very first character */ |
| 6155 |
if (extra_error_line == NULL) |
| 6156 |
extra_error_line = ""; |
| 6157 |
|
| 6158 |
if (obj != NULL) |
| 6159 |
s = PyUnicode_FromFormat("%c%s%R%s", |
| 6160 |
first_char, objdescr + 1, obj, extra_error_line); |
| 6161 |
else |
| 6162 |
s = PyUnicode_FromFormat("%c%s%s", |
| 6163 |
first_char, objdescr + 1, extra_error_line); |
| 6164 |
|
| 6165 |
PyErr_Restore(t, v, tb); |
| 6166 |
if (s != NULL) { |
| 6167 |
#if PY_VERSION_HEX >= 0x030D0000 |
| 6168 |
PyErr_FormatUnraisable("Exception ignored %S", s); |
| 6169 |
#else |
| 6170 |
_PyErr_WriteUnraisableMsg(PyUnicode_AsUTF8(s), NULL); |
| 6171 |
#endif |
| 6172 |
Py_DECREF(s); |
| 6173 |
} |
| 6174 |
else |
| 6175 |
PyErr_WriteUnraisable(obj); /* best effort */ |
| 6176 |
PyErr_Clear(); |
| 6177 |
} |
| 6178 |
|
| 6179 |
static void general_invoke_callback(int decode_args_from_libffi, |
| 6180 |
void *result, char *args, void *userdata) |
| 6181 |
{ |
| 6182 |
PyObject *cb_args = (PyObject *)userdata; |
| 6183 |
CTypeDescrObject *ct = (CTypeDescrObject *)PyTuple_GET_ITEM(cb_args, 0); |
| 6184 |
PyObject *signature = ct->ct_stuff; |
| 6185 |
PyObject *py_ob = PyTuple_GET_ITEM(cb_args, 1); |
| 6186 |
PyObject *py_args = NULL; |
| 6187 |
PyObject *py_res = NULL; |
| 6188 |
PyObject *py_rawerr; |
| 6189 |
PyObject *onerror_cb; |
| 6190 |
Py_ssize_t i, n; |
| 6191 |
char *extra_error_line = NULL; |
| 6192 |
|
| 6193 |
#define SIGNATURE(i) ((CTypeDescrObject *)PyTuple_GET_ITEM(signature, i)) |
| 6194 |
|
| 6195 |
Py_INCREF(cb_args); |
| 6196 |
|
| 6197 |
n = PyTuple_GET_SIZE(signature) - 2; |
| 6198 |
py_args = PyTuple_New(n); |
| 6199 |
if (py_args == NULL) |
| 6200 |
goto error; |
| 6201 |
|
| 6202 |
for (i=0; i<n; i++) { |
| 6203 |
char *a_src; |
| 6204 |
PyObject *a; |
| 6205 |
CTypeDescrObject *a_ct = SIGNATURE(2 + i); |
| 6206 |
|
| 6207 |
if (decode_args_from_libffi) { |
| 6208 |
a_src = ((void **)args)[i]; |
| 6209 |
} |
| 6210 |
else { |
| 6211 |
a_src = args + i * 8; |
| 6212 |
if (a_ct->ct_flags & (CT_IS_LONGDOUBLE | CT_STRUCT | CT_UNION)) |
| 6213 |
a_src = *(char **)a_src; |
| 6214 |
} |
| 6215 |
a = convert_to_object(a_src, a_ct); |
| 6216 |
if (a == NULL) |
| 6217 |
goto error; |
| 6218 |
PyTuple_SET_ITEM(py_args, i, a); |
| 6219 |
} |
| 6220 |
|
| 6221 |
py_res = PyObject_Call(py_ob, py_args, NULL); |
| 6222 |
if (py_res == NULL) |
| 6223 |
goto error; |
| 6224 |
if (convert_from_object_fficallback(result, SIGNATURE(1), py_res, |
| 6225 |
decode_args_from_libffi) < 0) { |
| 6226 |
extra_error_line = ", trying to convert the result back to C"; |
| 6227 |
goto error; |
| 6228 |
} |
| 6229 |
done: |
| 6230 |
Py_XDECREF(py_args); |
| 6231 |
Py_XDECREF(py_res); |
| 6232 |
Py_DECREF(cb_args); |
| 6233 |
return; |
| 6234 |
|
| 6235 |
error: |
| 6236 |
if (SIGNATURE(1)->ct_size > 0) { |
| 6237 |
py_rawerr = PyTuple_GET_ITEM(cb_args, 2); |
| 6238 |
memcpy(result, PyBytes_AS_STRING(py_rawerr), |
| 6239 |
PyBytes_GET_SIZE(py_rawerr)); |
| 6240 |
} |
| 6241 |
onerror_cb = PyTuple_GET_ITEM(cb_args, 3); |
| 6242 |
if (onerror_cb == Py_None) { |
| 6243 |
PyObject *ecap, *t, *v, *tb; |
| 6244 |
PyErr_Fetch(&t, &v, &tb); |
| 6245 |
ecap = _cffi_start_error_capture(); |
| 6246 |
_my_PyErr_WriteUnraisable(t, v, tb, "From cffi callback ", py_ob, |
| 6247 |
extra_error_line); |
| 6248 |
_cffi_stop_error_capture(ecap); |
| 6249 |
} |
| 6250 |
else { |
| 6251 |
PyObject *exc1, *val1, *tb1, *res1, *exc2, *val2, *tb2; |
| 6252 |
PyErr_Fetch(&exc1, &val1, &tb1); |
| 6253 |
PyErr_NormalizeException(&exc1, &val1, &tb1); |
| 6254 |
res1 = PyObject_CallFunctionObjArgs(onerror_cb, |
| 6255 |
exc1 ? exc1 : Py_None, |
| 6256 |
val1 ? val1 : Py_None, |
| 6257 |
tb1 ? tb1 : Py_None, |
| 6258 |
NULL); |
| 6259 |
if (res1 != NULL) { |
| 6260 |
if (res1 != Py_None) |
| 6261 |
convert_from_object_fficallback(result, SIGNATURE(1), res1, |
| 6262 |
decode_args_from_libffi); |
| 6263 |
Py_DECREF(res1); |
| 6264 |
} |
| 6265 |
if (!PyErr_Occurred()) { |
| 6266 |
Py_XDECREF(exc1); |
| 6267 |
Py_XDECREF(val1); |
| 6268 |
Py_XDECREF(tb1); |
| 6269 |
} |
| 6270 |
else { |
| 6271 |
/* double exception! print a double-traceback... */ |
| 6272 |
PyObject *ecap; |
| 6273 |
PyErr_Fetch(&exc2, &val2, &tb2); |
| 6274 |
ecap = _cffi_start_error_capture(); |
| 6275 |
_my_PyErr_WriteUnraisable(exc1, val1, tb1, |
| 6276 |
"From cffi callback ", py_ob, |
| 6277 |
extra_error_line); |
| 6278 |
_my_PyErr_WriteUnraisable(exc2, val2, tb2, |
| 6279 |
"during handling of the above exception by 'onerror'", |
| 6280 |
NULL, NULL); |
| 6281 |
_cffi_stop_error_capture(ecap); |
| 6282 |
} |
| 6283 |
} |
| 6284 |
goto done; |
| 6285 |
|
| 6286 |
#undef SIGNATURE |
| 6287 |
} |
| 6288 |
|
| 6289 |
static void invoke_callback(ffi_cif *cif, void *result, void **args, |
| 6290 |
void *userdata) |
| 6291 |
{ |
| 6292 |
save_errno(); |
| 6293 |
{ |
| 6294 |
PyGILState_STATE state = gil_ensure(); |
| 6295 |
general_invoke_callback(1, result, (char *)args, userdata); |
| 6296 |
gil_release(state); |
| 6297 |
} |
| 6298 |
restore_errno(); |
| 6299 |
} |
| 6300 |
|
| 6301 |
static PyObject *prepare_callback_info_tuple(CTypeDescrObject *ct, |
| 6302 |
PyObject *ob, |
| 6303 |
PyObject *error_ob, |
| 6304 |
PyObject *onerror_ob, |
| 6305 |
int decode_args_from_libffi) |
| 6306 |
{ |
| 6307 |
CTypeDescrObject *ctresult; |
| 6308 |
PyObject *py_rawerr, *infotuple; |
| 6309 |
Py_ssize_t size; |
| 6310 |
|
| 6311 |
if (!(ct->ct_flags & CT_FUNCTIONPTR)) { |
| 6312 |
PyErr_Format(PyExc_TypeError, "expected a function ctype, got '%s'", |
| 6313 |
ct->ct_name); |
| 6314 |
return NULL; |
| 6315 |
} |
| 6316 |
if (!PyCallable_Check(ob)) { |
| 6317 |
PyErr_Format(PyExc_TypeError, |
| 6318 |
"expected a callable object, not %.200s", |
| 6319 |
Py_TYPE(ob)->tp_name); |
| 6320 |
return NULL; |
| 6321 |
} |
| 6322 |
if (onerror_ob != Py_None && !PyCallable_Check(onerror_ob)) { |
| 6323 |
PyErr_Format(PyExc_TypeError, |
| 6324 |
"expected a callable object for 'onerror', not %.200s", |
| 6325 |
Py_TYPE(onerror_ob)->tp_name); |
| 6326 |
return NULL; |
| 6327 |
} |
| 6328 |
|
| 6329 |
ctresult = (CTypeDescrObject *)PyTuple_GET_ITEM(ct->ct_stuff, 1); |
| 6330 |
size = ctresult->ct_size; |
| 6331 |
if (size < (Py_ssize_t)sizeof(ffi_arg)) |
| 6332 |
size = sizeof(ffi_arg); |
| 6333 |
py_rawerr = PyBytes_FromStringAndSize(NULL, size); |
| 6334 |
if (py_rawerr == NULL) |
| 6335 |
return NULL; |
| 6336 |
memset(PyBytes_AS_STRING(py_rawerr), 0, size); |
| 6337 |
if (error_ob != Py_None) { |
| 6338 |
if (convert_from_object_fficallback( |
| 6339 |
PyBytes_AS_STRING(py_rawerr), ctresult, error_ob, |
| 6340 |
decode_args_from_libffi) < 0) { |
| 6341 |
Py_DECREF(py_rawerr); |
| 6342 |
return NULL; |
| 6343 |
} |
| 6344 |
} |
| 6345 |
infotuple = Py_BuildValue("OOOO", ct, ob, py_rawerr, onerror_ob); |
| 6346 |
Py_DECREF(py_rawerr); |
| 6347 |
|
| 6348 |
return infotuple; |
| 6349 |
} |
| 6350 |
|
| 6351 |
/* messily try to silence a gcc/clang deprecation warning for |
| 6352 |
ffi_prep_closure. Don't miss the "pragma pop" after the function. |
| 6353 |
This is done around the whole function because very old GCCs don't |
| 6354 |
support it inside a function. */ |
| 6355 |
#if defined(__clang__) |
| 6356 |
# pragma clang diagnostic push |
| 6357 |
# pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 6358 |
#elif defined(__GNUC__) |
| 6359 |
# pragma GCC diagnostic push |
| 6360 |
# pragma GCC diagnostic ignored "-Wdeprecated-declarations" |
| 6361 |
#endif |
| 6362 |
static PyObject *b_callback(PyObject *self, PyObject *args) |
| 6363 |
{ |
| 6364 |
CTypeDescrObject *ct; |
| 6365 |
CDataObject_closure *cd; |
| 6366 |
PyObject *ob, *error_ob = Py_None, *onerror_ob = Py_None; |
| 6367 |
PyObject *infotuple; |
| 6368 |
cif_description_t *cif_descr; |
| 6369 |
ffi_closure *closure; |
| 6370 |
ffi_status status; |
| 6371 |
void *closure_exec; |
| 6372 |
|
| 6373 |
if (!PyArg_ParseTuple(args, "O!O|OO:callback", &CTypeDescr_Type, &ct, &ob, |
| 6374 |
&error_ob, &onerror_ob)) |
| 6375 |
return NULL; |
| 6376 |
|
| 6377 |
infotuple = prepare_callback_info_tuple(ct, ob, error_ob, onerror_ob, 1); |
| 6378 |
if (infotuple == NULL) |
| 6379 |
return NULL; |
| 6380 |
|
| 6381 |
#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE |
| 6382 |
if (CFFI_CHECK_FFI_CLOSURE_ALLOC) { |
| 6383 |
closure = ffi_closure_alloc(sizeof(ffi_closure), &closure_exec); |
| 6384 |
} else |
| 6385 |
#endif |
| 6386 |
{ |
| 6387 |
closure = cffi_closure_alloc(); |
| 6388 |
closure_exec = closure; |
| 6389 |
} |
| 6390 |
|
| 6391 |
if (closure == NULL) { |
| 6392 |
Py_DECREF(infotuple); |
| 6393 |
PyErr_SetString(PyExc_MemoryError, |
| 6394 |
"Cannot allocate write+execute memory for ffi.callback(). " |
| 6395 |
"You might be running on a system that prevents this. " |
| 6396 |
"For more information, see " |
| 6397 |
"https://cffi.readthedocs.io/en/latest/using.html#callbacks"); |
| 6398 |
return NULL; |
| 6399 |
} |
| 6400 |
cd = PyObject_GC_New(CDataObject_closure, &CDataOwningGC_Type); |
| 6401 |
if (cd == NULL) |
| 6402 |
goto error; |
| 6403 |
Py_INCREF(ct); |
| 6404 |
cd->head.c_type = ct; |
| 6405 |
cd->head.c_data = CFFI_CLOSURE_TO_FNPTR(char *, closure_exec); |
| 6406 |
cd->head.c_weakreflist = NULL; |
| 6407 |
closure->user_data = NULL; |
| 6408 |
cd->closure = closure; |
| 6409 |
|
| 6410 |
cif_descr = (cif_description_t *)ct->ct_extra; |
| 6411 |
if (cif_descr == NULL) { |
| 6412 |
PyErr_Format(PyExc_NotImplementedError, |
| 6413 |
"%s: callback with unsupported argument or " |
| 6414 |
"return type or with '...'", ct->ct_name); |
| 6415 |
goto error; |
| 6416 |
} |
| 6417 |
|
| 6418 |
#if CFFI_CHECK_FFI_PREP_CLOSURE_LOC_MAYBE |
| 6419 |
if (CFFI_CHECK_FFI_PREP_CLOSURE_LOC) { |
| 6420 |
status = ffi_prep_closure_loc(closure, &cif_descr->cif, |
| 6421 |
invoke_callback, infotuple, closure_exec); |
| 6422 |
} |
| 6423 |
else |
| 6424 |
#endif |
| 6425 |
{ |
| 6426 |
#if defined(__APPLE__) && defined(FFI_AVAILABLE_APPLE) && !FFI_LEGACY_CLOSURE_API |
| 6427 |
PyErr_Format(PyExc_SystemError, "ffi_prep_closure_loc() is missing"); |
| 6428 |
goto error; |
| 6429 |
#else |
| 6430 |
status = ffi_prep_closure(closure, &cif_descr->cif, |
| 6431 |
invoke_callback, infotuple); |
| 6432 |
#endif |
| 6433 |
} |
| 6434 |
|
| 6435 |
if (status != FFI_OK) { |
| 6436 |
PyErr_SetString(PyExc_SystemError, |
| 6437 |
"libffi failed to build this callback"); |
| 6438 |
goto error; |
| 6439 |
} |
| 6440 |
|
| 6441 |
if (closure->user_data != infotuple) { |
| 6442 |
/* Issue #266. Should not occur, but could, if we are using |
| 6443 |
at runtime a version of libffi compiled with a different |
| 6444 |
'ffi_closure' structure than the one we expect from ffi.h |
| 6445 |
(e.g. difference in details of the platform): a difference |
| 6446 |
in FFI_TRAMPOLINE_SIZE means that the 'user_data' field |
| 6447 |
ends up somewhere else, and so the test above fails. |
| 6448 |
*/ |
| 6449 |
PyErr_SetString(PyExc_SystemError, |
| 6450 |
"ffi_prep_closure(): bad user_data (it seems that the " |
| 6451 |
"version of the libffi library seen at runtime is " |
| 6452 |
"different from the 'ffi.h' file seen at compile-time)"); |
| 6453 |
goto error; |
| 6454 |
} |
| 6455 |
PyObject_GC_Track(cd); |
| 6456 |
return (PyObject *)cd; |
| 6457 |
|
| 6458 |
error: |
| 6459 |
closure->user_data = NULL; |
| 6460 |
if (cd == NULL) { |
| 6461 |
#if CFFI_CHECK_FFI_CLOSURE_ALLOC_MAYBE |
| 6462 |
if (CFFI_CHECK_FFI_CLOSURE_ALLOC) { |
| 6463 |
ffi_closure_free(closure); |
| 6464 |
} |
| 6465 |
else |
| 6466 |
#endif |
| 6467 |
cffi_closure_free(closure); |
| 6468 |
} |
| 6469 |
else |
| 6470 |
Py_DECREF(cd); |
| 6471 |
Py_XDECREF(infotuple); |
| 6472 |
return NULL; |
| 6473 |
} |
| 6474 |
#if defined(__clang__) |
| 6475 |
# pragma clang diagnostic pop |
| 6476 |
#elif defined(__GNUC__) |
| 6477 |
# pragma GCC diagnostic pop |
| 6478 |
#endif |
| 6479 |
|
| 6480 |
static PyObject *b_new_enum_type(PyObject *self, PyObject *args) |
| 6481 |
{ |
| 6482 |
char *ename; |
| 6483 |
PyObject *enumerators, *enumvalues; |
| 6484 |
PyObject *dict1 = NULL, *dict2 = NULL, *combined = NULL, *tmpkey = NULL; |
| 6485 |
int name_size; |
| 6486 |
CTypeDescrObject *td, *basetd; |
| 6487 |
Py_ssize_t i, n; |
| 6488 |
|
| 6489 |
if (!PyArg_ParseTuple(args, "sO!O!O!:new_enum_type", |
| 6490 |
&ename, |
| 6491 |
&PyTuple_Type, &enumerators, |
| 6492 |
&PyTuple_Type, &enumvalues, |
| 6493 |
&CTypeDescr_Type, &basetd)) |
| 6494 |
return NULL; |
| 6495 |
|
| 6496 |
n = PyTuple_GET_SIZE(enumerators); |
| 6497 |
if (n != PyTuple_GET_SIZE(enumvalues)) { |
| 6498 |
PyErr_SetString(PyExc_ValueError, |
| 6499 |
"tuple args must have the same size"); |
| 6500 |
return NULL; |
| 6501 |
} |
| 6502 |
|
| 6503 |
if (!(basetd->ct_flags & (CT_PRIMITIVE_SIGNED|CT_PRIMITIVE_UNSIGNED))) { |
| 6504 |
PyErr_SetString(PyExc_TypeError, |
| 6505 |
"expected a primitive signed or unsigned base type"); |
| 6506 |
return NULL; |
| 6507 |
} |
| 6508 |
|
| 6509 |
dict1 = PyDict_New(); |
| 6510 |
if (dict1 == NULL) |
| 6511 |
goto error; |
| 6512 |
dict2 = PyDict_New(); |
| 6513 |
if (dict2 == NULL) |
| 6514 |
goto error; |
| 6515 |
|
| 6516 |
for (i=n; --i >= 0; ) { |
| 6517 |
long long lvalue; |
| 6518 |
PyObject *value = PyTuple_GET_ITEM(enumvalues, i); |
| 6519 |
tmpkey = PyTuple_GET_ITEM(enumerators, i); |
| 6520 |
Py_INCREF(tmpkey); |
| 6521 |
if (!PyUnicode_Check(tmpkey)) { |
| 6522 |
{ |
| 6523 |
PyErr_SetString(PyExc_TypeError, |
| 6524 |
"enumerators must be a list of strings"); |
| 6525 |
goto error; |
| 6526 |
} |
| 6527 |
} |
| 6528 |
if (convert_from_object((char*)&lvalue, basetd, value) < 0) |
| 6529 |
goto error; /* out-of-range or badly typed 'value' */ |
| 6530 |
if (PyDict_SetItem(dict1, tmpkey, value) < 0) |
| 6531 |
goto error; |
| 6532 |
if (PyDict_SetItem(dict2, value, tmpkey) < 0) |
| 6533 |
goto error; |
| 6534 |
Py_DECREF(tmpkey); |
| 6535 |
tmpkey = NULL; |
| 6536 |
} |
| 6537 |
|
| 6538 |
combined = PyTuple_Pack(2, dict1, dict2); |
| 6539 |
if (combined == NULL) |
| 6540 |
goto error; |
| 6541 |
|
| 6542 |
Py_CLEAR(dict2); |
| 6543 |
Py_CLEAR(dict1); |
| 6544 |
|
| 6545 |
name_size = strlen(ename) + 1; |
| 6546 |
td = ctypedescr_new(name_size); |
| 6547 |
if (td == NULL) |
| 6548 |
goto error; |
| 6549 |
|
| 6550 |
memcpy(td->ct_name, ename, name_size); |
| 6551 |
td->ct_stuff = combined; |
| 6552 |
td->ct_size = basetd->ct_size; |
| 6553 |
td->ct_length = basetd->ct_length; /* alignment */ |
| 6554 |
td->ct_extra = basetd->ct_extra; /* ffi type */ |
| 6555 |
td->ct_flags = basetd->ct_flags | CT_IS_ENUM; |
| 6556 |
td->ct_name_position = name_size - 1; |
| 6557 |
return (PyObject *)td; |
| 6558 |
|
| 6559 |
error: |
| 6560 |
Py_XDECREF(tmpkey); |
| 6561 |
Py_XDECREF(combined); |
| 6562 |
Py_XDECREF(dict2); |
| 6563 |
Py_XDECREF(dict1); |
| 6564 |
return NULL; |
| 6565 |
} |
| 6566 |
|
| 6567 |
static PyObject *b_alignof(PyObject *self, PyObject *arg) |
| 6568 |
{ |
| 6569 |
int align; |
| 6570 |
if (!CTypeDescr_Check(arg)) { |
| 6571 |
PyErr_SetString(PyExc_TypeError, "expected a 'ctype' object"); |
| 6572 |
return NULL; |
| 6573 |
} |
| 6574 |
align = get_alignment((CTypeDescrObject *)arg); |
| 6575 |
if (align < 0) |
| 6576 |
return NULL; |
| 6577 |
return PyLong_FromLong(align); |
| 6578 |
} |
| 6579 |
|
| 6580 |
static Py_ssize_t direct_sizeof_cdata(CDataObject *cd) |
| 6581 |
{ |
| 6582 |
Py_ssize_t size; |
| 6583 |
if (cd->c_type->ct_flags & CT_ARRAY) |
| 6584 |
size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size; |
| 6585 |
else { |
| 6586 |
size = -1; |
| 6587 |
if (cd->c_type->ct_flags & (CT_STRUCT | CT_UNION)) |
| 6588 |
size = _cdata_var_byte_size(cd); |
| 6589 |
if (size < 0) |
| 6590 |
size = cd->c_type->ct_size; |
| 6591 |
} |
| 6592 |
return size; |
| 6593 |
} |
| 6594 |
|
| 6595 |
static PyObject *b_sizeof(PyObject *self, PyObject *arg) |
| 6596 |
{ |
| 6597 |
Py_ssize_t size; |
| 6598 |
|
| 6599 |
if (CData_Check(arg)) { |
| 6600 |
size = direct_sizeof_cdata((CDataObject *)arg); |
| 6601 |
} |
| 6602 |
else if (CTypeDescr_Check(arg)) { |
| 6603 |
size = ((CTypeDescrObject *)arg)->ct_size; |
| 6604 |
if (size < 0) { |
| 6605 |
PyErr_Format(PyExc_ValueError, "ctype '%s' is of unknown size", |
| 6606 |
((CTypeDescrObject *)arg)->ct_name); |
| 6607 |
return NULL; |
| 6608 |
} |
| 6609 |
} |
| 6610 |
else { |
| 6611 |
PyErr_SetString(PyExc_TypeError, |
| 6612 |
"expected a 'cdata' or 'ctype' object"); |
| 6613 |
return NULL; |
| 6614 |
} |
| 6615 |
return PyLong_FromSsize_t(size); |
| 6616 |
} |
| 6617 |
|
| 6618 |
static PyObject *b_typeof(PyObject *self, PyObject *arg) |
| 6619 |
{ |
| 6620 |
PyObject *res; |
| 6621 |
|
| 6622 |
if (!CData_Check(arg)) { |
| 6623 |
PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object"); |
| 6624 |
return NULL; |
| 6625 |
} |
| 6626 |
res = (PyObject *)((CDataObject *)arg)->c_type; |
| 6627 |
Py_INCREF(res); |
| 6628 |
return res; |
| 6629 |
} |
| 6630 |
|
| 6631 |
static CTypeDescrObject *direct_typeoffsetof(CTypeDescrObject *ct, |
| 6632 |
PyObject *fieldname, |
| 6633 |
int following, Py_ssize_t *offset) |
| 6634 |
{ |
| 6635 |
/* Does not return a new reference! */ |
| 6636 |
CTypeDescrObject *res; |
| 6637 |
CFieldObject *cf; |
| 6638 |
|
| 6639 |
if (PyUnicode_Check(fieldname)) { |
| 6640 |
if (!following && (ct->ct_flags & CT_POINTER)) |
| 6641 |
ct = ct->ct_itemdescr; |
| 6642 |
if (!(ct->ct_flags & (CT_STRUCT|CT_UNION))) { |
| 6643 |
PyErr_SetString(PyExc_TypeError, |
| 6644 |
"with a field name argument, expected a " |
| 6645 |
"struct or union ctype"); |
| 6646 |
return NULL; |
| 6647 |
} |
| 6648 |
if (force_lazy_struct(ct) <= 0) { |
| 6649 |
if (!PyErr_Occurred()) |
| 6650 |
PyErr_SetString(PyExc_TypeError, "struct/union is opaque"); |
| 6651 |
return NULL; |
| 6652 |
} |
| 6653 |
cf = (CFieldObject *)PyDict_GetItem(ct->ct_stuff, fieldname); |
| 6654 |
if (cf == NULL) { |
| 6655 |
PyErr_SetObject(PyExc_KeyError, fieldname); |
| 6656 |
return NULL; |
| 6657 |
} |
| 6658 |
if (cf->cf_bitshift >= 0) { |
| 6659 |
PyErr_SetString(PyExc_TypeError, "not supported for bitfields"); |
| 6660 |
return NULL; |
| 6661 |
} |
| 6662 |
res = cf->cf_type; |
| 6663 |
*offset = cf->cf_offset; |
| 6664 |
} |
| 6665 |
else { |
| 6666 |
Py_ssize_t index = PyLong_AsSsize_t(fieldname); |
| 6667 |
if (index < 0 && PyErr_Occurred()) { |
| 6668 |
PyErr_SetString(PyExc_TypeError, |
| 6669 |
"field name or array index expected"); |
| 6670 |
return NULL; |
| 6671 |
} |
| 6672 |
|
| 6673 |
if (!(ct->ct_flags & (CT_ARRAY|CT_POINTER)) || |
| 6674 |
ct->ct_itemdescr->ct_size < 0) { |
| 6675 |
PyErr_SetString(PyExc_TypeError, "with an integer argument, " |
| 6676 |
"expected an array ctype or a " |
| 6677 |
"pointer to non-opaque"); |
| 6678 |
return NULL; |
| 6679 |
} |
| 6680 |
res = ct->ct_itemdescr; |
| 6681 |
*offset = MUL_WRAPAROUND(index, ct->ct_itemdescr->ct_size); |
| 6682 |
if ((*offset / ct->ct_itemdescr->ct_size) != index) { |
| 6683 |
PyErr_SetString(PyExc_OverflowError, |
| 6684 |
"array offset would overflow a Py_ssize_t"); |
| 6685 |
return NULL; |
| 6686 |
} |
| 6687 |
} |
| 6688 |
return res; |
| 6689 |
} |
| 6690 |
|
| 6691 |
static PyObject *b_typeoffsetof(PyObject *self, PyObject *args) |
| 6692 |
{ |
| 6693 |
PyObject *res, *fieldname; |
| 6694 |
CTypeDescrObject *ct; |
| 6695 |
Py_ssize_t offset; |
| 6696 |
int following = 0; |
| 6697 |
|
| 6698 |
if (!PyArg_ParseTuple(args, "O!O|i:typeoffsetof", |
| 6699 |
&CTypeDescr_Type, &ct, &fieldname, &following)) |
| 6700 |
return NULL; |
| 6701 |
|
| 6702 |
res = (PyObject *)direct_typeoffsetof(ct, fieldname, following, &offset); |
| 6703 |
if (res == NULL) |
| 6704 |
return NULL; |
| 6705 |
|
| 6706 |
return Py_BuildValue("(On)", res, offset); |
| 6707 |
} |
| 6708 |
|
| 6709 |
static PyObject *b_rawaddressof(PyObject *self, PyObject *args) |
| 6710 |
{ |
| 6711 |
CTypeDescrObject *ct; |
| 6712 |
CDataObject *cd; |
| 6713 |
Py_ssize_t offset; |
| 6714 |
int accepted_flags; |
| 6715 |
|
| 6716 |
if (!PyArg_ParseTuple(args, "O!O!n:rawaddressof", |
| 6717 |
&CTypeDescr_Type, &ct, |
| 6718 |
&CData_Type, &cd, |
| 6719 |
&offset)) |
| 6720 |
return NULL; |
| 6721 |
|
| 6722 |
accepted_flags = CT_STRUCT | CT_UNION | CT_ARRAY | CT_POINTER; |
| 6723 |
if ((cd->c_type->ct_flags & accepted_flags) == 0) { |
| 6724 |
PyErr_SetString(PyExc_TypeError, |
| 6725 |
"expected a cdata struct/union/array/pointer object"); |
| 6726 |
return NULL; |
| 6727 |
} |
| 6728 |
if ((ct->ct_flags & CT_POINTER) == 0) { |
| 6729 |
PyErr_SetString(PyExc_TypeError, |
| 6730 |
"expected a pointer ctype"); |
| 6731 |
return NULL; |
| 6732 |
} |
| 6733 |
return new_simple_cdata(cd->c_data + offset, ct); |
| 6734 |
} |
| 6735 |
|
| 6736 |
static PyObject *b_getcname(PyObject *self, PyObject *args) |
| 6737 |
{ |
| 6738 |
CTypeDescrObject *ct; |
| 6739 |
char *replace_with, *p, *s; |
| 6740 |
Py_ssize_t namelen, replacelen; |
| 6741 |
|
| 6742 |
if (!PyArg_ParseTuple(args, "O!s:getcname", |
| 6743 |
&CTypeDescr_Type, &ct, &replace_with)) |
| 6744 |
return NULL; |
| 6745 |
|
| 6746 |
namelen = strlen(ct->ct_name); |
| 6747 |
replacelen = strlen(replace_with); |
| 6748 |
s = p = alloca(namelen + replacelen + 1); |
| 6749 |
memcpy(p, ct->ct_name, ct->ct_name_position); |
| 6750 |
p += ct->ct_name_position; |
| 6751 |
memcpy(p, replace_with, replacelen); |
| 6752 |
p += replacelen; |
| 6753 |
memcpy(p, ct->ct_name + ct->ct_name_position, |
| 6754 |
namelen - ct->ct_name_position); |
| 6755 |
|
| 6756 |
return PyUnicode_FromStringAndSize(s, namelen + replacelen); |
| 6757 |
} |
| 6758 |
|
| 6759 |
static PyObject *b_string(PyObject *self, PyObject *args, PyObject *kwds) |
| 6760 |
{ |
| 6761 |
CDataObject *cd; |
| 6762 |
Py_ssize_t maxlen = -1; |
| 6763 |
static char *keywords[] = {"cdata", "maxlen", NULL}; |
| 6764 |
|
| 6765 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|n:string", keywords, |
| 6766 |
&CData_Type, &cd, &maxlen)) |
| 6767 |
return NULL; |
| 6768 |
|
| 6769 |
if (cd->c_type->ct_itemdescr != NULL && |
| 6770 |
cd->c_type->ct_itemdescr->ct_flags & (CT_PRIMITIVE_CHAR | |
| 6771 |
CT_PRIMITIVE_SIGNED | |
| 6772 |
CT_PRIMITIVE_UNSIGNED) && |
| 6773 |
!(cd->c_type->ct_itemdescr->ct_flags & CT_IS_BOOL)) { |
| 6774 |
Py_ssize_t length = maxlen; |
| 6775 |
if (cd->c_data == NULL) { |
| 6776 |
PyObject *s = cdata_repr(cd); |
| 6777 |
if (s != NULL) { |
| 6778 |
PyErr_Format(PyExc_RuntimeError, |
| 6779 |
"cannot use string() on %s", |
| 6780 |
PyUnicode_AsUTF8(s)); |
| 6781 |
Py_DECREF(s); |
| 6782 |
} |
| 6783 |
return NULL; |
| 6784 |
} |
| 6785 |
if (length < 0 && cd->c_type->ct_flags & CT_ARRAY) { |
| 6786 |
length = get_array_length(cd); |
| 6787 |
} |
| 6788 |
if (cd->c_type->ct_itemdescr->ct_size == sizeof(char)) { |
| 6789 |
const char *start = cd->c_data; |
| 6790 |
if (length < 0) { |
| 6791 |
/*READ(start, 1)*/ |
| 6792 |
length = strlen(start); |
| 6793 |
/*READ(start, length)*/ |
| 6794 |
} |
| 6795 |
else { |
| 6796 |
const char *end; |
| 6797 |
/*READ(start, length)*/ |
| 6798 |
end = (const char *)memchr(start, 0, length); |
| 6799 |
if (end != NULL) |
| 6800 |
length = end - start; |
| 6801 |
} |
| 6802 |
return PyBytes_FromStringAndSize(start, length); |
| 6803 |
} |
| 6804 |
else if (cd->c_type->ct_itemdescr->ct_flags & CT_PRIMITIVE_CHAR) { |
| 6805 |
switch (cd->c_type->ct_itemdescr->ct_size) { |
| 6806 |
case 2: { |
| 6807 |
const cffi_char16_t *start = (cffi_char16_t *)cd->c_data; |
| 6808 |
if (length < 0) { |
| 6809 |
/*READ(start, 2)*/ |
| 6810 |
length = 0; |
| 6811 |
while (start[length]) |
| 6812 |
length++; |
| 6813 |
/*READ(start, 2 * length)*/ |
| 6814 |
} |
| 6815 |
else { |
| 6816 |
/*READ(start, 2 * length)*/ |
| 6817 |
maxlen = length; |
| 6818 |
length = 0; |
| 6819 |
while (length < maxlen && start[length]) |
| 6820 |
length++; |
| 6821 |
} |
| 6822 |
return _my_PyUnicode_FromChar16(start, length); |
| 6823 |
} |
| 6824 |
case 4: { |
| 6825 |
const cffi_char32_t *start = (cffi_char32_t *)cd->c_data; |
| 6826 |
if (length < 0) { |
| 6827 |
/*READ(start, 4)*/ |
| 6828 |
length = 0; |
| 6829 |
while (start[length]) |
| 6830 |
length++; |
| 6831 |
/*READ(start, 4 * length)*/ |
| 6832 |
} |
| 6833 |
else { |
| 6834 |
/*READ(start, 4 * length)*/ |
| 6835 |
maxlen = length; |
| 6836 |
length = 0; |
| 6837 |
while (length < maxlen && start[length]) |
| 6838 |
length++; |
| 6839 |
} |
| 6840 |
return _my_PyUnicode_FromChar32(start, length); |
| 6841 |
} |
| 6842 |
} |
| 6843 |
} |
| 6844 |
} |
| 6845 |
else if (cd->c_type->ct_flags & CT_IS_ENUM) { |
| 6846 |
return convert_cdata_to_enum_string(cd, 0); |
| 6847 |
} |
| 6848 |
else if (cd->c_type->ct_flags & CT_IS_BOOL) { |
| 6849 |
/* fall through to TypeError */ |
| 6850 |
} |
| 6851 |
else if (cd->c_type->ct_flags & (CT_PRIMITIVE_CHAR | |
| 6852 |
CT_PRIMITIVE_SIGNED | |
| 6853 |
CT_PRIMITIVE_UNSIGNED)) { |
| 6854 |
/*READ(cd->c_data, cd->c_type->ct_size)*/ |
| 6855 |
if (cd->c_type->ct_size == sizeof(char)) |
| 6856 |
return PyBytes_FromStringAndSize(cd->c_data, 1); |
| 6857 |
else if (cd->c_type->ct_flags & CT_PRIMITIVE_CHAR) { |
| 6858 |
switch (cd->c_type->ct_size) { |
| 6859 |
case 2: |
| 6860 |
return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data, 1); |
| 6861 |
case 4: |
| 6862 |
return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data, 1); |
| 6863 |
} |
| 6864 |
} |
| 6865 |
} |
| 6866 |
PyErr_Format(PyExc_TypeError, "string(): unexpected cdata '%s' argument", |
| 6867 |
cd->c_type->ct_name); |
| 6868 |
return NULL; |
| 6869 |
} |
| 6870 |
|
| 6871 |
static PyObject *b_unpack(PyObject *self, PyObject *args, PyObject *kwds) |
| 6872 |
{ |
| 6873 |
CDataObject *cd; |
| 6874 |
CTypeDescrObject *ctitem; |
| 6875 |
Py_ssize_t i, length, itemsize; |
| 6876 |
PyObject *result; |
| 6877 |
char *src; |
| 6878 |
int casenum; |
| 6879 |
static char *keywords[] = {"cdata", "length", NULL}; |
| 6880 |
|
| 6881 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!n:unpack", keywords, |
| 6882 |
&CData_Type, &cd, &length)) |
| 6883 |
return NULL; |
| 6884 |
|
| 6885 |
if (!(cd->c_type->ct_flags & (CT_ARRAY|CT_POINTER))) { |
| 6886 |
PyErr_Format(PyExc_TypeError, |
| 6887 |
"expected a pointer or array, got '%s'", |
| 6888 |
cd->c_type->ct_name); |
| 6889 |
return NULL; |
| 6890 |
} |
| 6891 |
if (length < 0) { |
| 6892 |
PyErr_SetString(PyExc_ValueError, "'length' cannot be negative"); |
| 6893 |
return NULL; |
| 6894 |
} |
| 6895 |
if (cd->c_data == NULL) { |
| 6896 |
PyObject *s = cdata_repr(cd); |
| 6897 |
if (s != NULL) { |
| 6898 |
PyErr_Format(PyExc_RuntimeError, |
| 6899 |
"cannot use unpack() on %s", |
| 6900 |
PyUnicode_AsUTF8(s)); |
| 6901 |
Py_DECREF(s); |
| 6902 |
} |
| 6903 |
return NULL; |
| 6904 |
} |
| 6905 |
|
| 6906 |
/* byte- and unicode strings */ |
| 6907 |
ctitem = cd->c_type->ct_itemdescr; |
| 6908 |
if (ctitem->ct_flags & CT_PRIMITIVE_CHAR) { |
| 6909 |
switch (ctitem->ct_size) { |
| 6910 |
case sizeof(char): |
| 6911 |
return PyBytes_FromStringAndSize(cd->c_data, length); |
| 6912 |
case 2: |
| 6913 |
return _my_PyUnicode_FromChar16((cffi_char16_t *)cd->c_data,length); |
| 6914 |
case 4: |
| 6915 |
return _my_PyUnicode_FromChar32((cffi_char32_t *)cd->c_data,length); |
| 6916 |
} |
| 6917 |
} |
| 6918 |
|
| 6919 |
/* else, the result is a list. This implementation should be |
| 6920 |
equivalent to but much faster than '[p[i] for i in range(length)]'. |
| 6921 |
(Note that on PyPy, 'list(p[0:length])' should be equally fast, |
| 6922 |
but arguably, finding out that there *is* such an unexpected way |
| 6923 |
to write things down is the real problem.) |
| 6924 |
*/ |
| 6925 |
result = PyList_New(length); |
| 6926 |
if (result == NULL) |
| 6927 |
return NULL; |
| 6928 |
|
| 6929 |
src = cd->c_data; |
| 6930 |
itemsize = ctitem->ct_size; |
| 6931 |
if (itemsize < 0) { |
| 6932 |
Py_DECREF(result); |
| 6933 |
PyErr_Format(PyExc_ValueError, "'%s' points to items of unknown size", |
| 6934 |
cd->c_type->ct_name); |
| 6935 |
return NULL; |
| 6936 |
} |
| 6937 |
|
| 6938 |
/* Determine some common fast-paths for the loop below. The case -1 |
| 6939 |
is the fall-back, which always gives the right answer. */ |
| 6940 |
|
| 6941 |
#define ALIGNMENT_CHECK(align) \ |
| 6942 |
(((align) & ((align) - 1)) == 0 && \ |
| 6943 |
(((uintptr_t)src) & ((align) - 1)) == 0) |
| 6944 |
|
| 6945 |
casenum = -1; |
| 6946 |
|
| 6947 |
if ((ctitem->ct_flags & CT_PRIMITIVE_ANY) && |
| 6948 |
ALIGNMENT_CHECK(ctitem->ct_length)) { |
| 6949 |
/* Source data is fully aligned; we can directly read without |
| 6950 |
memcpy(). The unaligned case is expected to be rare; in |
| 6951 |
this situation it is ok to fall back to the general |
| 6952 |
convert_to_object() in the loop. For now we also use this |
| 6953 |
fall-back for types that are too large. |
| 6954 |
*/ |
| 6955 |
if (ctitem->ct_flags & CT_PRIMITIVE_SIGNED) { |
| 6956 |
if (itemsize == sizeof(long)) casenum = 3; |
| 6957 |
else if (itemsize == sizeof(int)) casenum = 2; |
| 6958 |
else if (itemsize == sizeof(short)) casenum = 1; |
| 6959 |
else if (itemsize == sizeof(signed char)) casenum = 0; |
| 6960 |
} |
| 6961 |
else if (ctitem->ct_flags & CT_PRIMITIVE_UNSIGNED) { |
| 6962 |
/* Note: we never pick case 6 if sizeof(int) == sizeof(long), |
| 6963 |
so that case 6 below can assume that the 'unsigned int' result |
| 6964 |
would always fit in a 'signed long'. */ |
| 6965 |
if (ctitem->ct_flags & CT_IS_BOOL) casenum = 11; |
| 6966 |
else if (itemsize == sizeof(unsigned long)) casenum = 7; |
| 6967 |
else if (itemsize == sizeof(unsigned int)) casenum = 6; |
| 6968 |
else if (itemsize == sizeof(unsigned short)) casenum = 5; |
| 6969 |
else if (itemsize == sizeof(unsigned char)) casenum = 4; |
| 6970 |
} |
| 6971 |
else if (ctitem->ct_flags & CT_PRIMITIVE_FLOAT) { |
| 6972 |
if (itemsize == sizeof(double)) casenum = 9; |
| 6973 |
else if (itemsize == sizeof(float)) casenum = 8; |
| 6974 |
} |
| 6975 |
} |
| 6976 |
else if (ctitem->ct_flags & (CT_POINTER | CT_FUNCTIONPTR)) { |
| 6977 |
casenum = 10; /* any pointer */ |
| 6978 |
} |
| 6979 |
#undef ALIGNMENT_CHECK |
| 6980 |
|
| 6981 |
for (i = 0; i < length; i++) { |
| 6982 |
PyObject *x; |
| 6983 |
switch (casenum) { |
| 6984 |
/* general case */ |
| 6985 |
default: x = convert_to_object(src, ctitem); break; |
| 6986 |
|
| 6987 |
/* special cases for performance only */ |
| 6988 |
case 0: x = PyLong_FromLong(*(signed char *)src); break; |
| 6989 |
case 1: x = PyLong_FromLong(*(short *)src); break; |
| 6990 |
case 2: x = PyLong_FromLong(*(int *)src); break; |
| 6991 |
case 3: x = PyLong_FromLong(*(long *)src); break; |
| 6992 |
case 4: x = PyLong_FromLong(*(unsigned char *)src); break; |
| 6993 |
case 5: x = PyLong_FromLong(*(unsigned short *)src); break; |
| 6994 |
case 6: x = PyLong_FromLong((long)*(unsigned int *)src); break; |
| 6995 |
case 7: x = PyLong_FromUnsignedLong(*(unsigned long *)src); break; |
| 6996 |
case 8: x = PyFloat_FromDouble(*(float *)src); break; |
| 6997 |
case 9: x = PyFloat_FromDouble(*(double *)src); break; |
| 6998 |
case 10: x = new_simple_cdata(*(char **)src, ctitem); break; |
| 6999 |
case 11: |
| 7000 |
switch (*(unsigned char *)src) { |
| 7001 |
case 0: x = Py_False; Py_INCREF(x); break; |
| 7002 |
case 1: x = Py_True; Py_INCREF(x); break; |
| 7003 |
default: x = convert_to_object(src, ctitem); /* error */ |
| 7004 |
} |
| 7005 |
break; |
| 7006 |
} |
| 7007 |
if (x == NULL) { |
| 7008 |
Py_DECREF(result); |
| 7009 |
return NULL; |
| 7010 |
} |
| 7011 |
PyList_SET_ITEM(result, i, x); |
| 7012 |
src += itemsize; |
| 7013 |
} |
| 7014 |
return result; |
| 7015 |
} |
| 7016 |
|
| 7017 |
static PyObject * |
| 7018 |
b_buffer_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 7019 |
{ |
| 7020 |
/* this is the constructor of the type implemented in minibuffer.h */ |
| 7021 |
CDataObject *cd; |
| 7022 |
Py_ssize_t size = -1; |
| 7023 |
int explicit_size; |
| 7024 |
static char *keywords[] = {"cdata", "size", NULL}; |
| 7025 |
|
| 7026 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|n:buffer", keywords, |
| 7027 |
&CData_Type, &cd, &size)) |
| 7028 |
return NULL; |
| 7029 |
|
| 7030 |
explicit_size = size >= 0; |
| 7031 |
if (size < 0) |
| 7032 |
size = _cdata_var_byte_size(cd); |
| 7033 |
|
| 7034 |
if (cd->c_type->ct_flags & CT_POINTER) { |
| 7035 |
if (size < 0) |
| 7036 |
size = cd->c_type->ct_itemdescr->ct_size; |
| 7037 |
} |
| 7038 |
else if (cd->c_type->ct_flags & CT_ARRAY) { |
| 7039 |
if (size < 0) |
| 7040 |
size = get_array_length(cd) * cd->c_type->ct_itemdescr->ct_size; |
| 7041 |
} |
| 7042 |
else { |
| 7043 |
PyErr_Format(PyExc_TypeError, |
| 7044 |
"expected a pointer or array cdata, got '%s'", |
| 7045 |
cd->c_type->ct_name); |
| 7046 |
return NULL; |
| 7047 |
} |
| 7048 |
if (size < 0) { |
| 7049 |
PyErr_Format(PyExc_TypeError, |
| 7050 |
"don't know the size pointed to by '%s'", |
| 7051 |
cd->c_type->ct_name); |
| 7052 |
return NULL; |
| 7053 |
} |
| 7054 |
|
| 7055 |
if (explicit_size && CDataOwn_Check(cd)) { |
| 7056 |
Py_ssize_t size_max = cdataowning_size_bytes(cd); |
| 7057 |
if (size > size_max) { |
| 7058 |
char msg[256]; |
| 7059 |
sprintf(msg, "ffi.buffer(cdata, bytes): creating a buffer of %llu " |
| 7060 |
"bytes over a cdata that owns only %llu bytes. This " |
| 7061 |
"will crash if you access the extra memory", |
| 7062 |
(unsigned PY_LONG_LONG)size, |
| 7063 |
(unsigned PY_LONG_LONG)size_max); |
| 7064 |
if (PyErr_WarnEx(PyExc_UserWarning, msg, 1)) |
| 7065 |
return NULL; |
| 7066 |
} |
| 7067 |
} |
| 7068 |
/*WRITE(cd->c_data, size)*/ |
| 7069 |
return minibuffer_new(cd->c_data, size, (PyObject *)cd); |
| 7070 |
} |
| 7071 |
|
| 7072 |
static PyObject *b_get_errno(PyObject *self, PyObject *noarg) |
| 7073 |
{ |
| 7074 |
int err; |
| 7075 |
restore_errno_only(); |
| 7076 |
err = errno; |
| 7077 |
errno = 0; |
| 7078 |
return PyLong_FromLong(err); |
| 7079 |
} |
| 7080 |
|
| 7081 |
static PyObject *b_set_errno(PyObject *self, PyObject *arg) |
| 7082 |
{ |
| 7083 |
long ival = PyLong_AsLong(arg); |
| 7084 |
if (ival == -1 && PyErr_Occurred()) |
| 7085 |
return NULL; |
| 7086 |
else if (ival < INT_MIN || ival > INT_MAX) { |
| 7087 |
PyErr_SetString(PyExc_OverflowError, "errno value too large"); |
| 7088 |
return NULL; |
| 7089 |
} |
| 7090 |
errno = (int)ival; |
| 7091 |
save_errno_only(); |
| 7092 |
errno = 0; |
| 7093 |
Py_INCREF(Py_None); |
| 7094 |
return Py_None; |
| 7095 |
} |
| 7096 |
|
| 7097 |
static PyObject *newp_handle(CTypeDescrObject *ct_voidp, PyObject *x) |
| 7098 |
{ |
| 7099 |
CDataObject_own_structptr *cd; |
| 7100 |
cd = (CDataObject_own_structptr *)PyObject_GC_New(CDataObject_own_structptr, |
| 7101 |
&CDataOwningGC_Type); |
| 7102 |
if (cd == NULL) |
| 7103 |
return NULL; |
| 7104 |
Py_INCREF(ct_voidp); /* must be "void *" */ |
| 7105 |
cd->head.c_type = ct_voidp; |
| 7106 |
cd->head.c_data = (char *)cd; |
| 7107 |
cd->head.c_weakreflist = NULL; |
| 7108 |
Py_INCREF(x); |
| 7109 |
cd->structobj = x; |
| 7110 |
PyObject_GC_Track(cd); |
| 7111 |
return (PyObject *)cd; |
| 7112 |
} |
| 7113 |
|
| 7114 |
static PyObject *b_newp_handle(PyObject *self, PyObject *args) |
| 7115 |
{ |
| 7116 |
CTypeDescrObject *ct; |
| 7117 |
PyObject *x; |
| 7118 |
if (!PyArg_ParseTuple(args, "O!O", &CTypeDescr_Type, &ct, &x)) |
| 7119 |
return NULL; |
| 7120 |
|
| 7121 |
if (!(ct->ct_flags & CT_IS_VOID_PTR)) { |
| 7122 |
PyErr_Format(PyExc_TypeError, "needs 'void *', got '%s'", ct->ct_name); |
| 7123 |
return NULL; |
| 7124 |
} |
| 7125 |
|
| 7126 |
return newp_handle(ct, x); |
| 7127 |
} |
| 7128 |
|
| 7129 |
static PyObject *b_from_handle(PyObject *self, PyObject *arg) |
| 7130 |
{ |
| 7131 |
CTypeDescrObject *ct; |
| 7132 |
CDataObject_own_structptr *orgcd; |
| 7133 |
PyObject *x; |
| 7134 |
if (!CData_Check(arg)) { |
| 7135 |
PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object"); |
| 7136 |
return NULL; |
| 7137 |
} |
| 7138 |
ct = ((CDataObject *)arg)->c_type; |
| 7139 |
if (!(ct->ct_flags & CT_IS_VOIDCHAR_PTR)) { |
| 7140 |
PyErr_Format(PyExc_TypeError, |
| 7141 |
"expected a 'cdata' object with a 'void *' out of " |
| 7142 |
"new_handle(), got '%s'", ct->ct_name); |
| 7143 |
return NULL; |
| 7144 |
} |
| 7145 |
orgcd = (CDataObject_own_structptr *)((CDataObject *)arg)->c_data; |
| 7146 |
if (!orgcd) { |
| 7147 |
PyErr_SetString(PyExc_RuntimeError, |
| 7148 |
"cannot use from_handle() on NULL pointer"); |
| 7149 |
return NULL; |
| 7150 |
} |
| 7151 |
if (Py_REFCNT(orgcd) <= 0 || Py_TYPE(orgcd) != &CDataOwningGC_Type) { |
| 7152 |
Py_FatalError("ffi.from_handle() detected that the address passed " |
| 7153 |
"points to garbage. If it is really the result of " |
| 7154 |
"ffi.new_handle(), then the Python object has already " |
| 7155 |
"been garbage collected"); |
| 7156 |
} |
| 7157 |
x = orgcd->structobj; |
| 7158 |
Py_INCREF(x); |
| 7159 |
return x; |
| 7160 |
} |
| 7161 |
|
| 7162 |
static int _my_PyObject_GetContiguousBuffer(PyObject *x, Py_buffer *view, |
| 7163 |
int writable_only) |
| 7164 |
{ |
| 7165 |
if (PyObject_GetBuffer(x, view, writable_only ? PyBUF_WRITABLE |
| 7166 |
: PyBUF_SIMPLE) < 0) |
| 7167 |
return -1; |
| 7168 |
|
| 7169 |
if (!PyBuffer_IsContiguous(view, 'A')) { |
| 7170 |
PyBuffer_Release(view); |
| 7171 |
PyErr_SetString(PyExc_TypeError, "contiguous buffer expected"); |
| 7172 |
return -1; |
| 7173 |
} |
| 7174 |
return 0; |
| 7175 |
} |
| 7176 |
|
| 7177 |
static PyObject *direct_from_buffer(CTypeDescrObject *ct, PyObject *x, |
| 7178 |
int require_writable) |
| 7179 |
{ |
| 7180 |
CDataObject *cd; |
| 7181 |
Py_buffer *view; |
| 7182 |
Py_ssize_t arraylength, minimumlength = 0; |
| 7183 |
|
| 7184 |
if (!(ct->ct_flags & (CT_ARRAY | CT_POINTER))) { |
| 7185 |
PyErr_Format(PyExc_TypeError, |
| 7186 |
"expected a pointer or array ctype, got '%s'", |
| 7187 |
ct->ct_name); |
| 7188 |
return NULL; |
| 7189 |
} |
| 7190 |
|
| 7191 |
/* PyPy 5.7 can obtain buffers for string (python 2) |
| 7192 |
or bytes (python 3). from_buffer(u"foo") is disallowed. |
| 7193 |
*/ |
| 7194 |
if (PyUnicode_Check(x)) { |
| 7195 |
PyErr_SetString(PyExc_TypeError, |
| 7196 |
"from_buffer() cannot return the address " |
| 7197 |
"of a unicode object"); |
| 7198 |
return NULL; |
| 7199 |
} |
| 7200 |
|
| 7201 |
view = PyObject_Malloc(sizeof(Py_buffer)); |
| 7202 |
if (view == NULL) { |
| 7203 |
PyErr_NoMemory(); |
| 7204 |
return NULL; |
| 7205 |
} |
| 7206 |
if (_my_PyObject_GetContiguousBuffer(x, view, require_writable) < 0) |
| 7207 |
goto error1; |
| 7208 |
|
| 7209 |
if (ct->ct_flags & CT_POINTER) |
| 7210 |
{ |
| 7211 |
arraylength = view->len; /* number of bytes, not used so far */ |
| 7212 |
} |
| 7213 |
else { |
| 7214 |
/* ct->ct_flags & CT_ARRAY */ |
| 7215 |
if (ct->ct_length >= 0) { |
| 7216 |
/* it's an array with a fixed length; make sure that the |
| 7217 |
buffer contains enough bytes. */ |
| 7218 |
minimumlength = ct->ct_size; |
| 7219 |
arraylength = ct->ct_length; |
| 7220 |
} |
| 7221 |
else { |
| 7222 |
/* it's an open 'array[]' */ |
| 7223 |
if (ct->ct_itemdescr->ct_size == 1) { |
| 7224 |
/* fast path, performance only */ |
| 7225 |
arraylength = view->len; |
| 7226 |
} |
| 7227 |
else if (ct->ct_itemdescr->ct_size > 0) { |
| 7228 |
/* give it as many items as fit the buffer. Ignore a |
| 7229 |
partial last element. */ |
| 7230 |
arraylength = view->len / ct->ct_itemdescr->ct_size; |
| 7231 |
} |
| 7232 |
else { |
| 7233 |
/* it's an array 'empty[]'. Unsupported obscure case: |
| 7234 |
the problem is that setting the length of the result |
| 7235 |
to anything large (like SSIZE_T_MAX) is dangerous, |
| 7236 |
because if someone tries to loop over it, it will |
| 7237 |
turn effectively into an infinite loop. */ |
| 7238 |
PyErr_Format(PyExc_ZeroDivisionError, |
| 7239 |
"from_buffer('%s', ..): the actual length of the array " |
| 7240 |
"cannot be computed", ct->ct_name); |
| 7241 |
goto error2; |
| 7242 |
} |
| 7243 |
} |
| 7244 |
} |
| 7245 |
if (view->len < minimumlength) { |
| 7246 |
PyErr_Format(PyExc_ValueError, |
| 7247 |
"buffer is too small (%zd bytes) for '%s' (%zd bytes)", |
| 7248 |
view->len, ct->ct_name, minimumlength); |
| 7249 |
goto error2; |
| 7250 |
} |
| 7251 |
|
| 7252 |
cd = (CDataObject *)PyObject_GC_New(CDataObject_frombuf, |
| 7253 |
&CDataFromBuf_Type); |
| 7254 |
if (cd == NULL) |
| 7255 |
goto error2; |
| 7256 |
|
| 7257 |
Py_INCREF(ct); |
| 7258 |
cd->c_type = ct; |
| 7259 |
cd->c_data = view->buf; |
| 7260 |
cd->c_weakreflist = NULL; |
| 7261 |
((CDataObject_frombuf *)cd)->length = arraylength; |
| 7262 |
((CDataObject_frombuf *)cd)->bufferview = view; |
| 7263 |
PyObject_GC_Track(cd); |
| 7264 |
return (PyObject *)cd; |
| 7265 |
|
| 7266 |
error2: |
| 7267 |
PyBuffer_Release(view); |
| 7268 |
error1: |
| 7269 |
PyObject_Free(view); |
| 7270 |
return NULL; |
| 7271 |
} |
| 7272 |
|
| 7273 |
static PyObject *b_from_buffer(PyObject *self, PyObject *args) |
| 7274 |
{ |
| 7275 |
CTypeDescrObject *ct; |
| 7276 |
PyObject *x; |
| 7277 |
int require_writable = 0; |
| 7278 |
|
| 7279 |
if (!PyArg_ParseTuple(args, "O!O|i", &CTypeDescr_Type, &ct, &x, |
| 7280 |
&require_writable)) |
| 7281 |
return NULL; |
| 7282 |
|
| 7283 |
return direct_from_buffer(ct, x, require_writable); |
| 7284 |
} |
| 7285 |
|
| 7286 |
static int _fetch_as_buffer(PyObject *x, Py_buffer *view, int writable_only) |
| 7287 |
{ |
| 7288 |
if (CData_Check(x)) { |
| 7289 |
CTypeDescrObject *ct = ((CDataObject *)x)->c_type; |
| 7290 |
if (!(ct->ct_flags & (CT_POINTER|CT_ARRAY))) { |
| 7291 |
PyErr_Format(PyExc_TypeError, |
| 7292 |
"expected a pointer or array ctype, got '%s'", |
| 7293 |
ct->ct_name); |
| 7294 |
return -1; |
| 7295 |
} |
| 7296 |
view->buf = ((CDataObject *)x)->c_data; |
| 7297 |
view->obj = NULL; |
| 7298 |
return 0; |
| 7299 |
} |
| 7300 |
else { |
| 7301 |
return _my_PyObject_GetContiguousBuffer(x, view, writable_only); |
| 7302 |
} |
| 7303 |
} |
| 7304 |
|
| 7305 |
static PyObject *b_memmove(PyObject *self, PyObject *args, PyObject *kwds) |
| 7306 |
{ |
| 7307 |
PyObject *dest_obj, *src_obj; |
| 7308 |
Py_buffer dest_view, src_view; |
| 7309 |
Py_ssize_t n; |
| 7310 |
static char *keywords[] = {"dest", "src", "n", NULL}; |
| 7311 |
|
| 7312 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOn", keywords, |
| 7313 |
&dest_obj, &src_obj, &n)) |
| 7314 |
return NULL; |
| 7315 |
if (n < 0) { |
| 7316 |
PyErr_SetString(PyExc_ValueError, "negative size"); |
| 7317 |
return NULL; |
| 7318 |
} |
| 7319 |
|
| 7320 |
if (_fetch_as_buffer(src_obj, &src_view, 0) < 0) { |
| 7321 |
return NULL; |
| 7322 |
} |
| 7323 |
if (_fetch_as_buffer(dest_obj, &dest_view, 1) < 0) { |
| 7324 |
PyBuffer_Release(&src_view); |
| 7325 |
return NULL; |
| 7326 |
} |
| 7327 |
|
| 7328 |
memmove(dest_view.buf, src_view.buf, n); |
| 7329 |
|
| 7330 |
PyBuffer_Release(&dest_view); |
| 7331 |
PyBuffer_Release(&src_view); |
| 7332 |
Py_INCREF(Py_None); |
| 7333 |
return Py_None; |
| 7334 |
} |
| 7335 |
|
| 7336 |
static PyObject *b__get_types(PyObject *self, PyObject *noarg) |
| 7337 |
{ |
| 7338 |
return PyTuple_Pack(2, (PyObject *)&CData_Type, |
| 7339 |
(PyObject *)&CTypeDescr_Type); |
| 7340 |
} |
| 7341 |
|
| 7342 |
/* forward, in commontypes.c */ |
| 7343 |
static PyObject *b__get_common_types(PyObject *self, PyObject *arg); |
| 7344 |
|
| 7345 |
static PyObject *b_gcp(PyObject *self, PyObject *args, PyObject *kwds) |
| 7346 |
{ |
| 7347 |
CDataObject *cd; |
| 7348 |
CDataObject *origobj; |
| 7349 |
PyObject *destructor; |
| 7350 |
Py_ssize_t ignored; /* for pypy */ |
| 7351 |
static char *keywords[] = {"cdata", "destructor", "size", NULL}; |
| 7352 |
|
| 7353 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!O|n:gc", keywords, |
| 7354 |
&CData_Type, &origobj, &destructor, |
| 7355 |
&ignored)) |
| 7356 |
return NULL; |
| 7357 |
|
| 7358 |
if (destructor == Py_None) { |
| 7359 |
if (!PyObject_TypeCheck(origobj, &CDataGCP_Type)) { |
| 7360 |
PyErr_SetString(PyExc_TypeError, |
| 7361 |
"Can remove destructor only on a object " |
| 7362 |
"previously returned by ffi.gc()"); |
| 7363 |
return NULL; |
| 7364 |
} |
| 7365 |
Py_CLEAR(((CDataObject_gcp *)origobj)->destructor); |
| 7366 |
Py_RETURN_NONE; |
| 7367 |
} |
| 7368 |
|
| 7369 |
cd = allocate_gcp_object(origobj, origobj->c_type, destructor); |
| 7370 |
return (PyObject *)cd; |
| 7371 |
} |
| 7372 |
|
| 7373 |
static PyObject *b_release(PyObject *self, PyObject *arg) |
| 7374 |
{ |
| 7375 |
if (!CData_Check(arg)) { |
| 7376 |
PyErr_SetString(PyExc_TypeError, "expected a 'cdata' object"); |
| 7377 |
return NULL; |
| 7378 |
} |
| 7379 |
return cdata_exit(arg, NULL); |
| 7380 |
} |
| 7381 |
|
| 7382 |
/************************************************************/ |
| 7383 |
|
| 7384 |
static char _testfunc0(char a, char b) |
| 7385 |
{ |
| 7386 |
return a + b; |
| 7387 |
} |
| 7388 |
static long _testfunc1(int a, long b) |
| 7389 |
{ |
| 7390 |
return (long)a + b; |
| 7391 |
} |
| 7392 |
static PY_LONG_LONG _testfunc2(PY_LONG_LONG a, PY_LONG_LONG b) |
| 7393 |
{ |
| 7394 |
return a + b; |
| 7395 |
} |
| 7396 |
static double _testfunc3(float a, double b) |
| 7397 |
{ |
| 7398 |
return a + b; |
| 7399 |
} |
| 7400 |
static float _testfunc4(float a, double b) |
| 7401 |
{ |
| 7402 |
return (float)(a + b); |
| 7403 |
} |
| 7404 |
static void _testfunc5(void) |
| 7405 |
{ |
| 7406 |
errno = errno + 15; |
| 7407 |
} |
| 7408 |
static int *_testfunc6(int *x) |
| 7409 |
{ |
| 7410 |
static int y; |
| 7411 |
y = *x - 1000; |
| 7412 |
return &y; |
| 7413 |
} |
| 7414 |
struct _testfunc7_s { unsigned char a1; short a2; }; |
| 7415 |
static short _testfunc7(struct _testfunc7_s inlined) |
| 7416 |
{ |
| 7417 |
return inlined.a1 + inlined.a2; |
| 7418 |
} |
| 7419 |
static int _testfunc9(int num, ...) |
| 7420 |
{ |
| 7421 |
va_list vargs; |
| 7422 |
int i, total = 0; |
| 7423 |
va_start(vargs, num); |
| 7424 |
for (i=0; i<num; i++) { |
| 7425 |
int value = va_arg(vargs, int); |
| 7426 |
if (value == 0) |
| 7427 |
value = -66666666; |
| 7428 |
total += value; |
| 7429 |
} |
| 7430 |
va_end(vargs); |
| 7431 |
return total; |
| 7432 |
} |
| 7433 |
|
| 7434 |
static struct _testfunc7_s _testfunc10(int n) |
| 7435 |
{ |
| 7436 |
struct _testfunc7_s result; |
| 7437 |
result.a1 = n; |
| 7438 |
result.a2 = n * n; |
| 7439 |
return result; |
| 7440 |
} |
| 7441 |
|
| 7442 |
struct _testfunc11_s { int a1, a2; }; |
| 7443 |
static struct _testfunc11_s _testfunc11(int n) |
| 7444 |
{ |
| 7445 |
struct _testfunc11_s result; |
| 7446 |
result.a1 = n; |
| 7447 |
result.a2 = n * n; |
| 7448 |
return result; |
| 7449 |
} |
| 7450 |
|
| 7451 |
struct _testfunc12_s { double a1; }; |
| 7452 |
static struct _testfunc12_s _testfunc12(int n) |
| 7453 |
{ |
| 7454 |
struct _testfunc12_s result; |
| 7455 |
result.a1 = n; |
| 7456 |
return result; |
| 7457 |
} |
| 7458 |
|
| 7459 |
struct _testfunc13_s { int a1, a2, a3; }; |
| 7460 |
static struct _testfunc13_s _testfunc13(int n) |
| 7461 |
{ |
| 7462 |
struct _testfunc13_s result; |
| 7463 |
result.a1 = n; |
| 7464 |
result.a2 = n * n; |
| 7465 |
result.a3 = n * n * n; |
| 7466 |
return result; |
| 7467 |
} |
| 7468 |
|
| 7469 |
struct _testfunc14_s { float a1; }; |
| 7470 |
static struct _testfunc14_s _testfunc14(int n) |
| 7471 |
{ |
| 7472 |
struct _testfunc14_s result; |
| 7473 |
result.a1 = (float)n; |
| 7474 |
return result; |
| 7475 |
} |
| 7476 |
|
| 7477 |
struct _testfunc15_s { float a1; int a2; }; |
| 7478 |
static struct _testfunc15_s _testfunc15(int n) |
| 7479 |
{ |
| 7480 |
struct _testfunc15_s result; |
| 7481 |
result.a1 = (float)n; |
| 7482 |
result.a2 = n * n; |
| 7483 |
return result; |
| 7484 |
} |
| 7485 |
|
| 7486 |
struct _testfunc16_s { float a1, a2; }; |
| 7487 |
static struct _testfunc16_s _testfunc16(int n) |
| 7488 |
{ |
| 7489 |
struct _testfunc16_s result; |
| 7490 |
result.a1 = (float)n; |
| 7491 |
result.a2 = -(float)n; |
| 7492 |
return result; |
| 7493 |
} |
| 7494 |
|
| 7495 |
struct _testfunc17_s { int a1; float a2; }; |
| 7496 |
static struct _testfunc17_s _testfunc17(int n) |
| 7497 |
{ |
| 7498 |
struct _testfunc17_s result; |
| 7499 |
result.a1 = n; |
| 7500 |
result.a2 = (float)n * (float)n; |
| 7501 |
return result; |
| 7502 |
} |
| 7503 |
|
| 7504 |
static int _testfunc18(struct _testfunc17_s *ptr) |
| 7505 |
{ |
| 7506 |
return ptr->a1 + (int)ptr->a2; |
| 7507 |
} |
| 7508 |
|
| 7509 |
static long double _testfunc19(long double x, int count) |
| 7510 |
{ |
| 7511 |
int i; |
| 7512 |
for (i=0; i<count; i++) { |
| 7513 |
x = 4*x - x*x; |
| 7514 |
} |
| 7515 |
return x; |
| 7516 |
} |
| 7517 |
|
| 7518 |
static short _testfunc20(struct _testfunc7_s *ptr) |
| 7519 |
{ |
| 7520 |
return ptr->a1 + ptr->a2; |
| 7521 |
} |
| 7522 |
|
| 7523 |
struct _testfunc21_s { int a, b, c, d, e, f, g, h, i, j; }; |
| 7524 |
static int _testfunc21(struct _testfunc21_s inlined) |
| 7525 |
{ |
| 7526 |
return ((inlined.a << 0) + |
| 7527 |
(inlined.b << 1) + |
| 7528 |
(inlined.c << 2) + |
| 7529 |
(inlined.d << 3) + |
| 7530 |
(inlined.e << 4) + |
| 7531 |
(inlined.f << 5) + |
| 7532 |
(inlined.g << 6) + |
| 7533 |
(inlined.h << 7) + |
| 7534 |
(inlined.i << 8) + |
| 7535 |
(inlined.j << 9)); |
| 7536 |
} |
| 7537 |
|
| 7538 |
struct _testfunc22_s { int a[10]; }; |
| 7539 |
static struct _testfunc22_s _testfunc22(struct _testfunc22_s s1, |
| 7540 |
struct _testfunc22_s s2) |
| 7541 |
{ |
| 7542 |
struct _testfunc22_s result; |
| 7543 |
int i; |
| 7544 |
for (i=0; i<10; i++) |
| 7545 |
result.a[i] = s1.a[i] - s2.a[i]; |
| 7546 |
return result; |
| 7547 |
} |
| 7548 |
|
| 7549 |
static int _testfunc23(char *p) |
| 7550 |
{ |
| 7551 |
if (p) |
| 7552 |
return 1000 * p[0]; |
| 7553 |
return -42; |
| 7554 |
} |
| 7555 |
|
| 7556 |
#if 0 /* libffi doesn't properly support complexes currently */ |
| 7557 |
/* if this is enabled one day, remember to also add _Complex |
| 7558 |
* arguments in addition to return values. */ |
| 7559 |
static _cffi_float_complex_t _testfunc24(float a, float b) |
| 7560 |
{ |
| 7561 |
return a + I*2.0*b; |
| 7562 |
} |
| 7563 |
static _cffi_double_complex_t _testfunc25(double a, double b) |
| 7564 |
{ |
| 7565 |
return a + I*2.0*b; |
| 7566 |
} |
| 7567 |
#endif |
| 7568 |
|
| 7569 |
static PyObject *b__testfunc(PyObject *self, PyObject *args) |
| 7570 |
{ |
| 7571 |
/* for testing only */ |
| 7572 |
int i; |
| 7573 |
void *f; |
| 7574 |
if (!PyArg_ParseTuple(args, "i:_testfunc", &i)) |
| 7575 |
return NULL; |
| 7576 |
switch (i) { |
| 7577 |
case 0: f = &_testfunc0; break; |
| 7578 |
case 1: f = &_testfunc1; break; |
| 7579 |
case 2: f = &_testfunc2; break; |
| 7580 |
case 3: f = &_testfunc3; break; |
| 7581 |
case 4: f = &_testfunc4; break; |
| 7582 |
case 5: f = &_testfunc5; break; |
| 7583 |
case 6: f = &_testfunc6; break; |
| 7584 |
case 7: f = &_testfunc7; break; |
| 7585 |
case 8: f = stderr; break; |
| 7586 |
case 9: f = &_testfunc9; break; |
| 7587 |
case 10: f = &_testfunc10; break; |
| 7588 |
case 11: f = &_testfunc11; break; |
| 7589 |
case 12: f = &_testfunc12; break; |
| 7590 |
case 13: f = &_testfunc13; break; |
| 7591 |
case 14: f = &_testfunc14; break; |
| 7592 |
case 15: f = &_testfunc15; break; |
| 7593 |
case 16: f = &_testfunc16; break; |
| 7594 |
case 17: f = &_testfunc17; break; |
| 7595 |
case 18: f = &_testfunc18; break; |
| 7596 |
case 19: f = &_testfunc19; break; |
| 7597 |
case 20: f = &_testfunc20; break; |
| 7598 |
case 21: f = &_testfunc21; break; |
| 7599 |
case 22: f = &_testfunc22; break; |
| 7600 |
case 23: f = &_testfunc23; break; |
| 7601 |
#if 0 |
| 7602 |
case 24: f = &_testfunc24; break; |
| 7603 |
case 25: f = &_testfunc25; break; |
| 7604 |
#endif |
| 7605 |
default: |
| 7606 |
PyErr_SetNone(PyExc_ValueError); |
| 7607 |
return NULL; |
| 7608 |
} |
| 7609 |
return PyLong_FromVoidPtr(f); |
| 7610 |
} |
| 7611 |
|
| 7612 |
static int _test_getbuf(PyObject *self, Py_buffer *view, int flags) |
| 7613 |
{ |
| 7614 |
static char buf[] = "GTB"; |
| 7615 |
return PyBuffer_FillInfo(view, self, buf, 3, /*readonly=*/0, flags); |
| 7616 |
} |
| 7617 |
static int _test_getbuf_ro(PyObject *self, Py_buffer *view, int flags) |
| 7618 |
{ |
| 7619 |
static char buf[] = "ROB"; |
| 7620 |
return PyBuffer_FillInfo(view, self, buf, 3, /*readonly=*/1, flags); |
| 7621 |
} |
| 7622 |
|
| 7623 |
|
| 7624 |
static PyObject *b__testbuff(PyObject *self, PyObject *args) |
| 7625 |
{ |
| 7626 |
/* for testing only */ |
| 7627 |
int methods; |
| 7628 |
PyTypeObject *obj; |
| 7629 |
if (!PyArg_ParseTuple(args, "O!i|_testbuff", &PyType_Type, &obj, &methods)) |
| 7630 |
return NULL; |
| 7631 |
|
| 7632 |
assert(obj->tp_as_buffer != NULL); |
| 7633 |
|
| 7634 |
if (methods & 8) obj->tp_as_buffer->bf_getbuffer = &_test_getbuf; |
| 7635 |
if (methods & 16) obj->tp_as_buffer->bf_getbuffer = &_test_getbuf_ro; |
| 7636 |
|
| 7637 |
Py_INCREF(Py_None); |
| 7638 |
return Py_None; |
| 7639 |
} |
| 7640 |
|
| 7641 |
static PyObject *b_init_cffi_1_0_external_module(PyObject *, PyObject *); |
| 7642 |
/* forward, see cffi1_module.c */ |
| 7643 |
|
| 7644 |
|
| 7645 |
static PyMethodDef FFIBackendMethods[] = { |
| 7646 |
{"load_library", b_load_library, METH_VARARGS}, |
| 7647 |
{"new_primitive_type", b_new_primitive_type, METH_VARARGS}, |
| 7648 |
{"new_pointer_type", b_new_pointer_type, METH_VARARGS}, |
| 7649 |
{"new_array_type", b_new_array_type, METH_VARARGS}, |
| 7650 |
{"new_void_type", b_new_void_type, METH_NOARGS}, |
| 7651 |
{"new_struct_type", b_new_struct_type, METH_VARARGS}, |
| 7652 |
{"new_union_type", b_new_union_type, METH_VARARGS}, |
| 7653 |
{"complete_struct_or_union", b_complete_struct_or_union, METH_VARARGS}, |
| 7654 |
{"new_function_type", b_new_function_type, METH_VARARGS}, |
| 7655 |
{"new_enum_type", b_new_enum_type, METH_VARARGS}, |
| 7656 |
{"newp", b_newp, METH_VARARGS}, |
| 7657 |
{"cast", b_cast, METH_VARARGS}, |
| 7658 |
{"callback", b_callback, METH_VARARGS}, |
| 7659 |
{"alignof", b_alignof, METH_O}, |
| 7660 |
{"sizeof", b_sizeof, METH_O}, |
| 7661 |
{"typeof", b_typeof, METH_O}, |
| 7662 |
{"typeoffsetof", b_typeoffsetof, METH_VARARGS}, |
| 7663 |
{"rawaddressof", b_rawaddressof, METH_VARARGS}, |
| 7664 |
{"getcname", b_getcname, METH_VARARGS}, |
| 7665 |
{"string", (PyCFunction)b_string, METH_VARARGS | METH_KEYWORDS}, |
| 7666 |
{"unpack", (PyCFunction)b_unpack, METH_VARARGS | METH_KEYWORDS}, |
| 7667 |
{"get_errno", b_get_errno, METH_NOARGS}, |
| 7668 |
{"set_errno", b_set_errno, METH_O}, |
| 7669 |
{"newp_handle", b_newp_handle, METH_VARARGS}, |
| 7670 |
{"from_handle", b_from_handle, METH_O}, |
| 7671 |
{"from_buffer", b_from_buffer, METH_VARARGS}, |
| 7672 |
{"memmove", (PyCFunction)b_memmove, METH_VARARGS | METH_KEYWORDS}, |
| 7673 |
{"gcp", (PyCFunction)b_gcp, METH_VARARGS | METH_KEYWORDS}, |
| 7674 |
{"release", b_release, METH_O}, |
| 7675 |
#ifdef MS_WIN32 |
| 7676 |
{"getwinerror", (PyCFunction)b_getwinerror, METH_VARARGS | METH_KEYWORDS}, |
| 7677 |
#endif |
| 7678 |
{"_get_types", b__get_types, METH_NOARGS}, |
| 7679 |
{"_get_common_types", b__get_common_types, METH_O}, |
| 7680 |
{"_testfunc", b__testfunc, METH_VARARGS}, |
| 7681 |
{"_testbuff", b__testbuff, METH_VARARGS}, |
| 7682 |
{"_init_cffi_1_0_external_module", b_init_cffi_1_0_external_module, METH_O}, |
| 7683 |
{NULL, NULL} /* Sentinel */ |
| 7684 |
}; |
| 7685 |
|
| 7686 |
/************************************************************/ |
| 7687 |
/* Functions used by '_cffi_N.so', the generated modules */ |
| 7688 |
|
| 7689 |
#define _cffi_to_c_SIGNED_FN(RETURNTYPE, SIZE) \ |
| 7690 |
static RETURNTYPE _cffi_to_c_i##SIZE(PyObject *obj) { \ |
| 7691 |
PY_LONG_LONG tmp = _my_PyLong_AsLongLong(obj); \ |
| 7692 |
if ((tmp > (PY_LONG_LONG)((1ULL<<(SIZE-1)) - 1)) || \ |
| 7693 |
(tmp < (PY_LONG_LONG)(0ULL-(1ULL<<(SIZE-1))))) \ |
| 7694 |
if (!PyErr_Occurred()) \ |
| 7695 |
return (RETURNTYPE)_convert_overflow(obj, #SIZE "-bit int"); \ |
| 7696 |
return (RETURNTYPE)tmp; \ |
| 7697 |
} |
| 7698 |
|
| 7699 |
#define _cffi_to_c_UNSIGNED_FN(RETURNTYPE, SIZE) \ |
| 7700 |
static RETURNTYPE _cffi_to_c_u##SIZE(PyObject *obj) { \ |
| 7701 |
unsigned PY_LONG_LONG tmp = _my_PyLong_AsUnsignedLongLong(obj, 1); \ |
| 7702 |
if (tmp > ~(((unsigned PY_LONG_LONG)-2) << (SIZE-1))) \ |
| 7703 |
if (!PyErr_Occurred()) \ |
| 7704 |
return (RETURNTYPE)_convert_overflow(obj, \ |
| 7705 |
#SIZE "-bit unsigned int"); \ |
| 7706 |
return (RETURNTYPE)tmp; \ |
| 7707 |
} |
| 7708 |
|
| 7709 |
_cffi_to_c_SIGNED_FN(int, 8) |
| 7710 |
_cffi_to_c_SIGNED_FN(int, 16) |
| 7711 |
_cffi_to_c_SIGNED_FN(int, 32) |
| 7712 |
_cffi_to_c_SIGNED_FN(PY_LONG_LONG, 64) |
| 7713 |
_cffi_to_c_UNSIGNED_FN(int, 8) |
| 7714 |
_cffi_to_c_UNSIGNED_FN(int, 16) |
| 7715 |
_cffi_to_c_UNSIGNED_FN(unsigned int, 32) |
| 7716 |
_cffi_to_c_UNSIGNED_FN(unsigned PY_LONG_LONG, 64) |
| 7717 |
|
| 7718 |
static PyObject *_cffi_from_c_pointer(char *ptr, CTypeDescrObject *ct) |
| 7719 |
{ |
| 7720 |
return convert_to_object((char *)&ptr, ct); |
| 7721 |
} |
| 7722 |
|
| 7723 |
static char *_cffi_to_c_pointer(PyObject *obj, CTypeDescrObject *ct) |
| 7724 |
{ |
| 7725 |
char *result; |
| 7726 |
if (convert_from_object((char *)&result, ct, obj) < 0) { |
| 7727 |
if ((ct->ct_flags & CT_POINTER) && |
| 7728 |
(ct->ct_itemdescr->ct_flags & CT_IS_FILE) && |
| 7729 |
PyFile_Check(obj)) { |
| 7730 |
PyErr_Clear(); |
| 7731 |
return (char *)PyFile_AsFile(obj); |
| 7732 |
} |
| 7733 |
return NULL; |
| 7734 |
} |
| 7735 |
return result; |
| 7736 |
} |
| 7737 |
|
| 7738 |
static long double _cffi_to_c_long_double(PyObject *obj) |
| 7739 |
{ |
| 7740 |
if (CData_Check(obj) && |
| 7741 |
(((CDataObject *)obj)->c_type->ct_flags & CT_IS_LONGDOUBLE)) { |
| 7742 |
char *data = ((CDataObject *)obj)->c_data; |
| 7743 |
/*READ(data, sizeof(long double))*/ |
| 7744 |
return read_raw_longdouble_data(data); |
| 7745 |
} |
| 7746 |
else |
| 7747 |
return PyFloat_AsDouble(obj); |
| 7748 |
} |
| 7749 |
|
| 7750 |
static _Bool _cffi_to_c__Bool(PyObject *obj) |
| 7751 |
{ |
| 7752 |
PY_LONG_LONG tmp = _my_PyLong_AsLongLong(obj); |
| 7753 |
if (tmp == 0) |
| 7754 |
return 0; |
| 7755 |
else if (tmp == 1) |
| 7756 |
return 1; |
| 7757 |
else if (PyErr_Occurred()) |
| 7758 |
return (_Bool)-1; |
| 7759 |
else |
| 7760 |
return (_Bool)_convert_overflow(obj, "_Bool"); |
| 7761 |
} |
| 7762 |
|
| 7763 |
static PyObject *_cffi_get_struct_layout(Py_ssize_t nums[]) |
| 7764 |
{ |
| 7765 |
PyObject *result; |
| 7766 |
int count = 0; |
| 7767 |
while (nums[count] >= 0) |
| 7768 |
count++; |
| 7769 |
|
| 7770 |
result = PyList_New(count); |
| 7771 |
if (result == NULL) |
| 7772 |
return NULL; |
| 7773 |
|
| 7774 |
while (--count >= 0) { |
| 7775 |
PyObject *o = PyLong_FromSsize_t(nums[count]); |
| 7776 |
if (o == NULL) { |
| 7777 |
Py_DECREF(result); |
| 7778 |
return NULL; |
| 7779 |
} |
| 7780 |
PyList_SET_ITEM(result, count, o); |
| 7781 |
} |
| 7782 |
return result; |
| 7783 |
} |
| 7784 |
|
| 7785 |
static PyObject *_cffi_from_c_char(char x) { |
| 7786 |
return PyBytes_FromStringAndSize(&x, 1); |
| 7787 |
} |
| 7788 |
|
| 7789 |
/* backward-compatibility hack: instead of _cffi_to_c_char16_t() and |
| 7790 |
* _cffi_to_c_char32_t(), we have _cffi_to_c_wchar_t() handling whatever |
| 7791 |
* size is wchar_t, and _cffi_to_c_wchar3216_t() handling the opposite. |
| 7792 |
*/ |
| 7793 |
#ifdef HAVE_WCHAR_H |
| 7794 |
typedef wchar_t cffi_wchar_t; |
| 7795 |
#else |
| 7796 |
typedef uint16_t cffi_wchar_t; /* random pick... */ |
| 7797 |
#endif |
| 7798 |
|
| 7799 |
static cffi_wchar_t _cffi_to_c_wchar_t(PyObject *init) |
| 7800 |
{ |
| 7801 |
if (sizeof(cffi_wchar_t) == 2) |
| 7802 |
return (cffi_wchar_t)_convert_to_char16_t(init); |
| 7803 |
else |
| 7804 |
return (cffi_wchar_t)_convert_to_char32_t(init); |
| 7805 |
} |
| 7806 |
static PyObject *_cffi_from_c_wchar_t(cffi_wchar_t x) { |
| 7807 |
if (sizeof(cffi_wchar_t) == 2) { |
| 7808 |
cffi_char16_t input = x; |
| 7809 |
return _my_PyUnicode_FromChar16(&input, 1); |
| 7810 |
} |
| 7811 |
else { |
| 7812 |
cffi_char32_t input = x; |
| 7813 |
return _my_PyUnicode_FromChar32(&input, 1); |
| 7814 |
} |
| 7815 |
} |
| 7816 |
static int _cffi_to_c_wchar3216_t(PyObject *init) |
| 7817 |
{ |
| 7818 |
if (sizeof(cffi_wchar_t) == 4) |
| 7819 |
return (int)_convert_to_char16_t(init); |
| 7820 |
else |
| 7821 |
return (int)_convert_to_char32_t(init); |
| 7822 |
} |
| 7823 |
static PyObject *_cffi_from_c_wchar3216_t(int x) { |
| 7824 |
if (sizeof(cffi_wchar_t) == 4) { |
| 7825 |
cffi_char16_t input = x; |
| 7826 |
return _my_PyUnicode_FromChar16(&input, 1); |
| 7827 |
} |
| 7828 |
else { |
| 7829 |
cffi_char32_t input = x; |
| 7830 |
return _my_PyUnicode_FromChar32(&input, 1); |
| 7831 |
} |
| 7832 |
} |
| 7833 |
|
| 7834 |
struct _cffi_externpy_s; /* forward declaration */ |
| 7835 |
static void cffi_call_python(struct _cffi_externpy_s *, char *args); |
| 7836 |
|
| 7837 |
static void *cffi_exports[] = { |
| 7838 |
NULL, |
| 7839 |
_cffi_to_c_i8, |
| 7840 |
_cffi_to_c_u8, |
| 7841 |
_cffi_to_c_i16, |
| 7842 |
_cffi_to_c_u16, |
| 7843 |
_cffi_to_c_i32, |
| 7844 |
_cffi_to_c_u32, |
| 7845 |
_cffi_to_c_i64, |
| 7846 |
_cffi_to_c_u64, |
| 7847 |
_convert_to_char, |
| 7848 |
_cffi_from_c_pointer, |
| 7849 |
_cffi_to_c_pointer, |
| 7850 |
_cffi_get_struct_layout, |
| 7851 |
restore_errno, |
| 7852 |
save_errno, |
| 7853 |
_cffi_from_c_char, |
| 7854 |
convert_to_object, |
| 7855 |
convert_from_object, |
| 7856 |
convert_struct_to_owning_object, |
| 7857 |
_cffi_to_c_wchar_t, |
| 7858 |
_cffi_from_c_wchar_t, |
| 7859 |
_cffi_to_c_long_double, |
| 7860 |
_cffi_to_c__Bool, |
| 7861 |
_prepare_pointer_call_argument, |
| 7862 |
convert_array_from_object, |
| 7863 |
cffi_call_python, |
| 7864 |
_cffi_to_c_wchar3216_t, |
| 7865 |
_cffi_from_c_wchar3216_t, |
| 7866 |
}; |
| 7867 |
|
| 7868 |
static struct { const char *name; int value; } all_dlopen_flags[] = { |
| 7869 |
{ "RTLD_LAZY", RTLD_LAZY }, |
| 7870 |
{ "RTLD_NOW", RTLD_NOW }, |
| 7871 |
{ "RTLD_GLOBAL", RTLD_GLOBAL }, |
| 7872 |
#ifdef RTLD_LOCAL |
| 7873 |
{ "RTLD_LOCAL", RTLD_LOCAL }, |
| 7874 |
#else |
| 7875 |
{ "RTLD_LOCAL", 0 }, |
| 7876 |
#endif |
| 7877 |
#ifdef RTLD_NODELETE |
| 7878 |
{ "RTLD_NODELETE", RTLD_NODELETE }, |
| 7879 |
#endif |
| 7880 |
#ifdef RTLD_NOLOAD |
| 7881 |
{ "RTLD_NOLOAD", RTLD_NOLOAD }, |
| 7882 |
#endif |
| 7883 |
#ifdef RTLD_DEEPBIND |
| 7884 |
{ "RTLD_DEEPBIND", RTLD_DEEPBIND }, |
| 7885 |
#endif |
| 7886 |
{ NULL, 0 } |
| 7887 |
}; |
| 7888 |
|
| 7889 |
|
| 7890 |
/************************************************************/ |
| 7891 |
|
| 7892 |
#include "cffi1_module.c" |
| 7893 |
|
| 7894 |
/************************************************************/ |
| 7895 |
|
| 7896 |
static struct PyModuleDef FFIBackendModuleDef = { |
| 7897 |
PyModuleDef_HEAD_INIT, |
| 7898 |
"_cffi_backend", |
| 7899 |
NULL, |
| 7900 |
-1, |
| 7901 |
FFIBackendMethods, |
| 7902 |
NULL, NULL, NULL, NULL |
| 7903 |
}; |
| 7904 |
#define INITERROR return NULL |
| 7905 |
|
| 7906 |
PyMODINIT_FUNC |
| 7907 |
PyInit__cffi_backend(void) |
| 7908 |
{ |
| 7909 |
PyObject *m, *v; |
| 7910 |
int i; |
| 7911 |
static char init_done = 0; |
| 7912 |
static PyTypeObject *all_types[] = { |
| 7913 |
&dl_type, |
| 7914 |
&CTypeDescr_Type, |
| 7915 |
&CField_Type, |
| 7916 |
&CData_Type, |
| 7917 |
&CDataOwning_Type, |
| 7918 |
&CDataOwningGC_Type, |
| 7919 |
&CDataFromBuf_Type, |
| 7920 |
&CDataGCP_Type, |
| 7921 |
&CDataIter_Type, |
| 7922 |
&MiniBuffer_Type, |
| 7923 |
&FFI_Type, |
| 7924 |
&Lib_Type, |
| 7925 |
&GlobSupport_Type, |
| 7926 |
NULL |
| 7927 |
}; |
| 7928 |
|
| 7929 |
v = PySys_GetObject("version"); |
| 7930 |
if (v == NULL || !PyUnicode_Check(v) || |
| 7931 |
strncmp(PyUnicode_AsUTF8(v), PY_VERSION, 3) != 0) { |
| 7932 |
PyErr_Format(PyExc_ImportError, |
| 7933 |
"this module was compiled for Python %c%c%c", |
| 7934 |
PY_VERSION[0], PY_VERSION[1], PY_VERSION[2]); |
| 7935 |
INITERROR; |
| 7936 |
} |
| 7937 |
|
| 7938 |
m = PyModule_Create(&FFIBackendModuleDef); |
| 7939 |
|
| 7940 |
if (m == NULL) |
| 7941 |
INITERROR; |
| 7942 |
|
| 7943 |
#ifdef Py_GIL_DISABLED |
| 7944 |
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED); |
| 7945 |
#endif |
| 7946 |
|
| 7947 |
if (unique_cache == NULL) { |
| 7948 |
unique_cache = PyDict_New(); |
| 7949 |
if (unique_cache == NULL) |
| 7950 |
INITERROR; |
| 7951 |
} |
| 7952 |
|
| 7953 |
/* readify all types and add them to the module */ |
| 7954 |
for (i = 0; all_types[i] != NULL; i++) { |
| 7955 |
PyTypeObject *tp = all_types[i]; |
| 7956 |
PyObject *tpo = (PyObject *)tp; |
| 7957 |
if (strncmp(tp->tp_name, "_cffi_backend.", 14) != 0) { |
| 7958 |
PyErr_Format(PyExc_ImportError, |
| 7959 |
"'%s' is an ill-formed type name", tp->tp_name); |
| 7960 |
INITERROR; |
| 7961 |
} |
| 7962 |
if (PyType_Ready(tp) < 0) |
| 7963 |
INITERROR; |
| 7964 |
|
| 7965 |
Py_INCREF(tpo); |
| 7966 |
if (PyModule_AddObject(m, tp->tp_name + 14, tpo) < 0) |
| 7967 |
INITERROR; |
| 7968 |
} |
| 7969 |
|
| 7970 |
if (!init_done) { |
| 7971 |
v = PyUnicode_FromString("_cffi_backend"); |
| 7972 |
if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict, |
| 7973 |
"__module__", v) < 0) |
| 7974 |
INITERROR; |
| 7975 |
v = PyUnicode_FromString("<cdata>"); |
| 7976 |
if (v == NULL || PyDict_SetItemString(CData_Type.tp_dict, |
| 7977 |
"__name__", v) < 0) |
| 7978 |
INITERROR; |
| 7979 |
init_done = 1; |
| 7980 |
} |
| 7981 |
|
| 7982 |
/* this is for backward compatibility only */ |
| 7983 |
v = PyCapsule_New((void *)cffi_exports, "cffi", NULL); |
| 7984 |
if (v == NULL || PyModule_AddObject(m, "_C_API", v) < 0) |
| 7985 |
INITERROR; |
| 7986 |
|
| 7987 |
v = PyUnicode_FromString(CFFI_VERSION); |
| 7988 |
if (v == NULL || PyModule_AddObject(m, "__version__", v) < 0) |
| 7989 |
INITERROR; |
| 7990 |
|
| 7991 |
if (PyModule_AddIntConstant(m, "FFI_DEFAULT_ABI", FFI_DEFAULT_ABI) < 0 || |
| 7992 |
#if defined(MS_WIN32) && !defined(_WIN64) |
| 7993 |
PyModule_AddIntConstant(m, "FFI_STDCALL", FFI_STDCALL) < 0 || |
| 7994 |
#endif |
| 7995 |
PyModule_AddIntConstant(m, "FFI_CDECL", FFI_DEFAULT_ABI) < 0 || |
| 7996 |
|
| 7997 |
#ifdef MS_WIN32 |
| 7998 |
# ifdef _WIN64 |
| 7999 |
PyModule_AddIntConstant(m, "_WIN", 64) < 0 || /* win64 */ |
| 8000 |
# else |
| 8001 |
PyModule_AddIntConstant(m, "_WIN", 32) < 0 || /* win32 */ |
| 8002 |
# endif |
| 8003 |
#endif |
| 8004 |
0) |
| 8005 |
INITERROR; |
| 8006 |
|
| 8007 |
for (i = 0; all_dlopen_flags[i].name != NULL; i++) { |
| 8008 |
if (PyModule_AddIntConstant(m, |
| 8009 |
all_dlopen_flags[i].name, |
| 8010 |
all_dlopen_flags[i].value) < 0) |
| 8011 |
INITERROR; |
| 8012 |
} |
| 8013 |
|
| 8014 |
init_cffi_tls(); |
| 8015 |
if (PyErr_Occurred()) |
| 8016 |
INITERROR; |
| 8017 |
init_cffi_tls_zombie(); |
| 8018 |
if (PyErr_Occurred()) |
| 8019 |
INITERROR; |
| 8020 |
|
| 8021 |
if (init_ffi_lib(m) < 0) |
| 8022 |
INITERROR; |
| 8023 |
|
| 8024 |
if (init_file_emulator() < 0) |
| 8025 |
INITERROR; |
| 8026 |
return m; |
| 8027 |
} |