| 1 |
#if defined (__SVR4) && defined (__sun) |
| 2 |
# include <alloca.h> |
| 3 |
#endif |
| 4 |
|
| 5 |
#include <stdbool.h> |
| 6 |
|
| 7 |
#include "pycore_moduleobject.h" |
| 8 |
#include "pycore_typeobject.h" |
| 9 |
#include "pycore_critical_section.h" |
| 10 |
#include "pycore_pyatomic_ft_wrappers.h" |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
#if defined(_Py_FFI_SUPPORT_C_COMPLEX) |
| 15 |
# if USING_APPLE_OS_LIBFFI && defined(__has_builtin) |
| 16 |
# if __has_builtin(__builtin_available) |
| 17 |
# define Py_FFI_COMPLEX_AVAILABLE __builtin_available(macOS 10.15, *) |
| 18 |
# else |
| 19 |
# define Py_FFI_COMPLEX_AVAILABLE 1 |
| 20 |
# endif |
| 21 |
# else |
| 22 |
# define Py_FFI_COMPLEX_AVAILABLE 1 |
| 23 |
# endif |
| 24 |
#else |
| 25 |
# define Py_FFI_COMPLEX_AVAILABLE 0 |
| 26 |
#endif |
| 27 |
|
| 28 |
#ifndef MS_WIN32 |
| 29 |
#define max(a, b) ((a) > (b) ? (a) : (b)) |
| 30 |
#define min(a, b) ((a) < (b) ? (a) : (b)) |
| 31 |
|
| 32 |
#define PARAMFLAG_FIN 0x1 |
| 33 |
#define PARAMFLAG_FOUT 0x2 |
| 34 |
#define PARAMFLAG_FLCID 0x4 |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#ifndef CTYPES_MAX_ARGCOUNT |
| 45 |
#ifdef __EMSCRIPTEN__ |
| 46 |
#define CTYPES_MAX_ARGCOUNT 1000 |
| 47 |
#else |
| 48 |
#define CTYPES_MAX_ARGCOUNT 1024 |
| 49 |
#endif |
| 50 |
#endif |
| 51 |
|
| 52 |
#if defined(__has_builtin) |
| 53 |
#if __has_builtin(__builtin_available) |
| 54 |
#define HAVE_BUILTIN_AVAILABLE 1 |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
|
| 58 |
#ifdef MS_WIN32 |
| 59 |
#include <Unknwn.h> |
| 60 |
#endif |
| 61 |
|
| 62 |
typedef struct { |
| 63 |
PyTypeObject *DictRemover_Type; |
| 64 |
PyTypeObject *PyCArg_Type; |
| 65 |
PyTypeObject *PyCField_Type; |
| 66 |
PyTypeObject *PyCThunk_Type; |
| 67 |
PyTypeObject *StructParam_Type; |
| 68 |
PyTypeObject *PyCType_Type; |
| 69 |
PyTypeObject *PyCStructType_Type; |
| 70 |
PyTypeObject *UnionType_Type; |
| 71 |
PyTypeObject *PyCPointerType_Type; |
| 72 |
PyTypeObject *PyCArrayType_Type; |
| 73 |
PyTypeObject *PyCSimpleType_Type; |
| 74 |
PyTypeObject *PyCFuncPtrType_Type; |
| 75 |
PyTypeObject *PyCData_Type; |
| 76 |
PyTypeObject *Struct_Type; |
| 77 |
PyTypeObject *Union_Type; |
| 78 |
PyTypeObject *PyCArray_Type; |
| 79 |
PyTypeObject *Simple_Type; |
| 80 |
PyTypeObject *PyCPointer_Type; |
| 81 |
PyTypeObject *PyCFuncPtr_Type; |
| 82 |
#ifdef MS_WIN32 |
| 83 |
PyTypeObject *PyComError_Type; |
| 84 |
#endif |
| 85 |
|
| 86 |
|
| 87 |
PyObject *_unpickle; |
| 88 |
PyObject *array_cache; |
| 89 |
PyObject *error_object_name; |
| 90 |
PyObject *PyExc_ArgError; |
| 91 |
PyObject *swapped_suffix; |
| 92 |
} ctypes_state; |
| 93 |
|
| 94 |
|
| 95 |
extern struct PyModuleDef _ctypesmodule; |
| 96 |
|
| 97 |
|
| 98 |
static inline ctypes_state * |
| 99 |
get_module_state(PyObject *module) |
| 100 |
{ |
| 101 |
void *state = _PyModule_GetState(module); |
| 102 |
assert(state != NULL); |
| 103 |
return (ctypes_state *)state; |
| 104 |
} |
| 105 |
|
| 106 |
static inline ctypes_state * |
| 107 |
get_module_state_by_class(PyTypeObject *cls) |
| 108 |
{ |
| 109 |
ctypes_state *state = (ctypes_state *)_PyType_GetModuleState(cls); |
| 110 |
assert(state != NULL); |
| 111 |
return state; |
| 112 |
} |
| 113 |
|
| 114 |
static inline ctypes_state * |
| 115 |
get_module_state_by_def(PyTypeObject *cls) |
| 116 |
{ |
| 117 |
PyObject *mod = PyType_GetModuleByDef(cls, &_ctypesmodule); |
| 118 |
assert(mod != NULL); |
| 119 |
return get_module_state(mod); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
extern PyType_Spec pyctype_type_spec; |
| 124 |
extern PyType_Spec carg_spec; |
| 125 |
extern PyType_Spec cfield_spec; |
| 126 |
extern PyType_Spec cthunk_spec; |
| 127 |
|
| 128 |
typedef struct tagPyCArgObject PyCArgObject; |
| 129 |
#define _PyCArgObject_CAST(op) ((PyCArgObject *)(op)) |
| 130 |
|
| 131 |
typedef struct tagCDataObject CDataObject; |
| 132 |
#define _CDataObject_CAST(op) ((CDataObject *)(op)) |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
typedef PyObject *(* GETFUNC)(void *ptr, Py_ssize_t size); |
| 143 |
typedef PyObject *(* SETFUNC)(void *ptr, PyObject *value, Py_ssize_t size); |
| 144 |
typedef PyCArgObject *(* PARAMFUNC)(ctypes_state *st, CDataObject *obj); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
union value { |
| 154 |
char c[16]; |
| 155 |
short s; |
| 156 |
int i; |
| 157 |
long l; |
| 158 |
float f; |
| 159 |
double d; |
| 160 |
long long ll; |
| 161 |
long double D; |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
struct tagCDataObject { |
| 171 |
PyObject_HEAD |
| 172 |
char *b_ptr; |
| 173 |
int b_needsfree; |
| 174 |
CDataObject *b_base; |
| 175 |
Py_ssize_t b_size; |
| 176 |
Py_ssize_t b_length; |
| 177 |
Py_ssize_t b_index; |
| 178 |
|
| 179 |
PyObject *b_objects; |
| 180 |
union value b_value; |
| 181 |
}; |
| 182 |
|
| 183 |
typedef struct { |
| 184 |
PyObject_VAR_HEAD |
| 185 |
ffi_closure *pcl_write; |
| 186 |
void *pcl_exec; |
| 187 |
ffi_cif cif; |
| 188 |
int flags; |
| 189 |
PyObject *converters; |
| 190 |
PyObject *callable; |
| 191 |
PyObject *restype; |
| 192 |
SETFUNC setfunc; |
| 193 |
ffi_type *ffi_restype; |
| 194 |
ffi_type *atypes[1]; |
| 195 |
} CThunkObject; |
| 196 |
|
| 197 |
#define _CThunkObject_CAST(op) ((CThunkObject *)(op)) |
| 198 |
#define CThunk_CheckExact(st, v) Py_IS_TYPE(v, st->PyCThunk_Type) |
| 199 |
|
| 200 |
typedef struct { |
| 201 |
|
| 202 |
PyObject_HEAD |
| 203 |
char *b_ptr; |
| 204 |
int b_needsfree; |
| 205 |
CDataObject *b_base; |
| 206 |
Py_ssize_t b_size; |
| 207 |
Py_ssize_t b_length; |
| 208 |
Py_ssize_t b_index; |
| 209 |
|
| 210 |
PyObject *b_objects; |
| 211 |
union value b_value; |
| 212 |
|
| 213 |
|
| 214 |
CThunkObject *thunk; |
| 215 |
PyObject *callable; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
PyObject *converters; |
| 220 |
PyObject *argtypes; |
| 221 |
PyObject *restype; |
| 222 |
PyObject *checker; |
| 223 |
PyObject *errcheck; |
| 224 |
#ifdef MS_WIN32 |
| 225 |
int index; |
| 226 |
GUID *iid; |
| 227 |
#endif |
| 228 |
PyObject *paramflags; |
| 229 |
} PyCFuncPtrObject; |
| 230 |
|
| 231 |
#define _PyCFuncPtrObject_CAST(op) ((PyCFuncPtrObject *)(op)) |
| 232 |
|
| 233 |
extern int PyCStructUnionType_update_stginfo(PyObject *fields, PyObject *type, int isStruct); |
| 234 |
extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength); |
| 235 |
extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength); |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#define CDataObject_CheckExact(st, v) Py_IS_TYPE((v), (st)->PyCData_Type) |
| 240 |
#define CDataObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCData_Type) |
| 241 |
#define _CDataObject_HasExternalBuffer(v) ((v)->b_ptr != (char *)&(v)->b_value) |
| 242 |
|
| 243 |
#define PyCSimpleTypeObject_CheckExact(st, v) Py_IS_TYPE((v), (st)->PyCSimpleType_Type) |
| 244 |
#define PyCSimpleTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCSimpleType_Type) |
| 245 |
|
| 246 |
extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt); |
| 247 |
|
| 248 |
extern PyObject *PyCData_AtAddress(ctypes_state *st, PyObject *type, void *buf); |
| 249 |
extern PyObject *PyCData_FromBytes(ctypes_state *st, PyObject *type, char *data, Py_ssize_t length); |
| 250 |
|
| 251 |
#define PyCArrayTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCArrayType_Type) |
| 252 |
#define ArrayObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCArray_Type) |
| 253 |
#define PointerObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCPointer_Type) |
| 254 |
#define PyCPointerTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCPointerType_Type) |
| 255 |
#define PyCFuncPtrObject_Check(st,v) PyObject_TypeCheck((v), (st)->PyCFuncPtr_Type) |
| 256 |
#define PyCFuncPtrTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCFuncPtrType_Type) |
| 257 |
#define PyCStructTypeObject_Check(st, v) PyObject_TypeCheck((v), (st)->PyCStructType_Type) |
| 258 |
|
| 259 |
extern PyObject * |
| 260 |
PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length); |
| 261 |
|
| 262 |
extern PyMethodDef _ctypes_module_methods[]; |
| 263 |
|
| 264 |
extern CThunkObject *_ctypes_alloc_callback(ctypes_state *st, |
| 265 |
PyObject *callable, |
| 266 |
PyObject *converters, |
| 267 |
PyObject *restype, |
| 268 |
int flags); |
| 269 |
|
| 270 |
struct fielddesc { |
| 271 |
char code; |
| 272 |
ffi_type *pffi_type; |
| 273 |
SETFUNC setfunc; |
| 274 |
GETFUNC getfunc; |
| 275 |
SETFUNC setfunc_swapped; |
| 276 |
GETFUNC getfunc_swapped; |
| 277 |
}; |
| 278 |
|
| 279 |
|
| 280 |
extern char *_ctypes_get_simple_type_chars(void); |
| 281 |
|
| 282 |
typedef struct CFieldObject { |
| 283 |
PyObject_HEAD |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
Py_ssize_t byte_offset; |
| 294 |
Py_ssize_t byte_size; |
| 295 |
|
| 296 |
Py_ssize_t index; |
| 297 |
PyObject *proto; |
| 298 |
GETFUNC getfunc; |
| 299 |
SETFUNC setfunc; |
| 300 |
bool anonymous: 1; |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
uint8_t bitfield_size; |
| 313 |
uint8_t bit_offset; |
| 314 |
|
| 315 |
PyObject *name; |
| 316 |
} CFieldObject; |
| 317 |
|
| 318 |
#define _CFieldObject_CAST(op) ((CFieldObject *)(op)) |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
typedef struct { |
| 375 |
int initialized; |
| 376 |
Py_ssize_t size; |
| 377 |
Py_ssize_t align; |
| 378 |
Py_ssize_t length; |
| 379 |
ffi_type ffi_type_pointer; |
| 380 |
PyObject *proto; |
| 381 |
SETFUNC setfunc; |
| 382 |
GETFUNC getfunc; |
| 383 |
PARAMFUNC paramfunc; |
| 384 |
|
| 385 |
|
| 386 |
PyObject *argtypes; |
| 387 |
PyObject *converters; |
| 388 |
PyObject *restype; |
| 389 |
PyObject *checker; |
| 390 |
PyObject *pointer_type; |
| 391 |
|
| 392 |
PyObject *module; |
| 393 |
int flags; |
| 394 |
#ifdef Py_GIL_DISABLED |
| 395 |
PyMutex mutex; |
| 396 |
#endif |
| 397 |
uint8_t dict_final; |
| 398 |
|
| 399 |
|
| 400 |
char *format; |
| 401 |
int ndim; |
| 402 |
Py_ssize_t *shape; |
| 403 |
|
| 404 |
|
| 405 |
} StgInfo; |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
#define STGINFO_LOCK(stginfo) Py_BEGIN_CRITICAL_SECTION_MUTEX(&(stginfo)->mutex) |
| 423 |
#define STGINFO_UNLOCK() Py_END_CRITICAL_SECTION() |
| 424 |
|
| 425 |
static inline uint8_t |
| 426 |
stginfo_get_dict_final(StgInfo *info) |
| 427 |
{ |
| 428 |
return FT_ATOMIC_LOAD_UINT8(info->dict_final); |
| 429 |
} |
| 430 |
|
| 431 |
static inline void |
| 432 |
stginfo_set_dict_final_lock_held(StgInfo *info) |
| 433 |
{ |
| 434 |
_Py_CRITICAL_SECTION_ASSERT_MUTEX_LOCKED(&info->mutex); |
| 435 |
FT_ATOMIC_STORE_UINT8(info->dict_final, 1); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
static inline void |
| 442 |
stginfo_set_dict_final(StgInfo *info) |
| 443 |
{ |
| 444 |
if (stginfo_get_dict_final(info) == 1) { |
| 445 |
return; |
| 446 |
} |
| 447 |
STGINFO_LOCK(info); |
| 448 |
stginfo_set_dict_final_lock_held(info); |
| 449 |
STGINFO_UNLOCK(); |
| 450 |
} |
| 451 |
|
| 452 |
extern int PyCStgInfo_clone(StgInfo *dst_info, StgInfo *src_info); |
| 453 |
extern void ctype_clear_stginfo(StgInfo *info); |
| 454 |
extern void ctype_free_stginfo_members(StgInfo *info); |
| 455 |
|
| 456 |
typedef int(* PPROC)(void); |
| 457 |
|
| 458 |
PyObject *_ctypes_callproc(ctypes_state *st, |
| 459 |
PPROC pProc, |
| 460 |
PyObject *arguments, |
| 461 |
#ifdef MS_WIN32 |
| 462 |
IUnknown *pIUnk, |
| 463 |
GUID *iid, |
| 464 |
#endif |
| 465 |
int flags, |
| 466 |
PyObject *argtypes, |
| 467 |
PyObject *restype, |
| 468 |
PyObject *checker); |
| 469 |
|
| 470 |
|
| 471 |
#define FUNCFLAG_STDCALL 0x0 |
| 472 |
#define FUNCFLAG_CDECL 0x1 |
| 473 |
#define FUNCFLAG_HRESULT 0x2 |
| 474 |
#define FUNCFLAG_PYTHONAPI 0x4 |
| 475 |
#define FUNCFLAG_USE_ERRNO 0x8 |
| 476 |
#define FUNCFLAG_USE_LASTERROR 0x10 |
| 477 |
|
| 478 |
#define TYPEFLAG_ISPOINTER 0x100 |
| 479 |
#define TYPEFLAG_HASPOINTER 0x200 |
| 480 |
|
| 481 |
struct tagPyCArgObject { |
| 482 |
PyObject_HEAD |
| 483 |
ffi_type *pffi_type; |
| 484 |
char tag; |
| 485 |
union { |
| 486 |
char c; |
| 487 |
char b; |
| 488 |
short h; |
| 489 |
int i; |
| 490 |
long l; |
| 491 |
long long q; |
| 492 |
long double g; |
| 493 |
double d; |
| 494 |
float f; |
| 495 |
void *p; |
| 496 |
double D[2]; |
| 497 |
float F[2]; |
| 498 |
long double G[2]; |
| 499 |
} value; |
| 500 |
PyObject *obj; |
| 501 |
Py_ssize_t size; |
| 502 |
}; |
| 503 |
|
| 504 |
#define _PyCArgObject_CAST(op) ((PyCArgObject *)(op)) |
| 505 |
|
| 506 |
#define PyCArg_CheckExact(st, v) Py_IS_TYPE(v, st->PyCArg_Type) |
| 507 |
extern PyCArgObject *PyCArgObject_new(ctypes_state *st); |
| 508 |
|
| 509 |
extern PyObject * |
| 510 |
PyCData_get(ctypes_state *st, PyObject *type, GETFUNC getfunc, PyObject *src, |
| 511 |
Py_ssize_t index, Py_ssize_t size, char *ptr); |
| 512 |
|
| 513 |
extern int |
| 514 |
PyCData_set(ctypes_state *st, |
| 515 |
PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value, |
| 516 |
Py_ssize_t index, Py_ssize_t size, char *ptr); |
| 517 |
|
| 518 |
extern void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...); |
| 519 |
|
| 520 |
struct basespec { |
| 521 |
CDataObject *base; |
| 522 |
Py_ssize_t index; |
| 523 |
char *adr; |
| 524 |
}; |
| 525 |
|
| 526 |
extern ffi_type *_ctypes_get_ffi_type(ctypes_state *st, PyObject *obj); |
| 527 |
|
| 528 |
extern void _ctypes_free_closure(void *); |
| 529 |
extern void *_ctypes_alloc_closure(void); |
| 530 |
|
| 531 |
extern PyObject *PyCData_FromBaseObj(ctypes_state *st, PyObject *type, |
| 532 |
PyObject *base, Py_ssize_t index, char *adr); |
| 533 |
|
| 534 |
extern int _ctypes_simple_instance(ctypes_state *st, PyObject *obj); |
| 535 |
|
| 536 |
PyObject *_ctypes_get_errobj(ctypes_state *st, int **pspace); |
| 537 |
|
| 538 |
extern void _ctypes_init_fielddesc(void); |
| 539 |
|
| 540 |
#ifdef USING_MALLOC_CLOSURE_DOT_C |
| 541 |
void Py_ffi_closure_free(void *p); |
| 542 |
void *Py_ffi_closure_alloc(size_t size, void** codeloc); |
| 543 |
#else |
| 544 |
#define Py_ffi_closure_free ffi_closure_free |
| 545 |
#define Py_ffi_closure_alloc ffi_closure_alloc |
| 546 |
#endif |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static inline int |
| 561 |
_stginfo_from_type(ctypes_state *state, PyTypeObject *type, StgInfo **result) |
| 562 |
{ |
| 563 |
*result = NULL; |
| 564 |
if (!PyObject_IsInstance((PyObject *)type, (PyObject *)state->PyCType_Type)) { |
| 565 |
|
| 566 |
return 0; |
| 567 |
} |
| 568 |
StgInfo *info = PyObject_GetTypeData((PyObject *)type, state->PyCType_Type); |
| 569 |
assert(info != NULL); |
| 570 |
if (!info->initialized) { |
| 571 |
|
| 572 |
return 0; |
| 573 |
} |
| 574 |
*result = info; |
| 575 |
return 1; |
| 576 |
} |
| 577 |
|
| 578 |
static inline int |
| 579 |
PyStgInfo_FromType(ctypes_state *state, PyObject *type, StgInfo **result) |
| 580 |
{ |
| 581 |
return _stginfo_from_type(state, (PyTypeObject *)type, result); |
| 582 |
} |
| 583 |
|
| 584 |
static inline int |
| 585 |
PyStgInfo_FromObject(ctypes_state *state, PyObject *obj, StgInfo **result) |
| 586 |
{ |
| 587 |
return _stginfo_from_type(state, Py_TYPE(obj), result); |
| 588 |
} |
| 589 |
|
| 590 |
static inline int |
| 591 |
PyStgInfo_FromAny(ctypes_state *state, PyObject *obj, StgInfo **result) |
| 592 |
{ |
| 593 |
if (PyType_Check(obj)) { |
| 594 |
return _stginfo_from_type(state, (PyTypeObject *)obj, result); |
| 595 |
} |
| 596 |
return _stginfo_from_type(state, Py_TYPE(obj), result); |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
static inline StgInfo * |
| 605 |
_PyStgInfo_FromType_NoState(PyObject *type) |
| 606 |
{ |
| 607 |
PyTypeObject *PyCType_Type; |
| 608 |
if (_PyType_GetBaseByToken_Borrow(Py_TYPE(type), &pyctype_type_spec, &PyCType_Type) < 0 || |
| 609 |
PyCType_Type == NULL) { |
| 610 |
return NULL; |
| 611 |
} |
| 612 |
|
| 613 |
return PyObject_GetTypeData(type, PyCType_Type); |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
static inline StgInfo * |
| 618 |
PyStgInfo_Init(ctypes_state *state, PyTypeObject *type) |
| 619 |
{ |
| 620 |
if (!PyObject_IsInstance((PyObject *)type, (PyObject *)state->PyCType_Type)) { |
| 621 |
PyErr_Format(PyExc_SystemError, |
| 622 |
"'%s' is not a ctypes class.", |
| 623 |
type->tp_name); |
| 624 |
return NULL; |
| 625 |
} |
| 626 |
StgInfo *info = PyObject_GetTypeData((PyObject *)type, state->PyCType_Type); |
| 627 |
if (info->initialized) { |
| 628 |
PyErr_Format(PyExc_SystemError, |
| 629 |
"StgInfo of '%s' is already initialized.", |
| 630 |
type->tp_name); |
| 631 |
return NULL; |
| 632 |
} |
| 633 |
PyObject *module = PyType_GetModule(state->PyCType_Type); |
| 634 |
if (!module) { |
| 635 |
return NULL; |
| 636 |
} |
| 637 |
info->pointer_type = NULL; |
| 638 |
info->module = Py_NewRef(module); |
| 639 |
|
| 640 |
info->initialized = 1; |
| 641 |
return info; |
| 642 |
} |