Debian Codegraph @

Modules/_ctypes/ctypes.h

raw
1 #if defined (__SVR4) && defined (__sun)
2 # include <alloca.h>
3 #endif
4
5 #include <stdbool.h>
6
7 #include "pycore_moduleobject.h" // _PyModule_GetState()
8 #include "pycore_typeobject.h" // _PyType_GetModuleState()
9 #include "pycore_critical_section.h"
10 #include "pycore_pyatomic_ft_wrappers.h"
11
12 // Do we support C99 complex types in ffi?
13 // For Apple's libffi, this must be determined at runtime (see gh-128156).
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 * bpo-13097: Max number of arguments CFuncPtr._argtypes_ and
39 * _ctypes_callproc() will accept.
40 *
41 * This limit is enforced for the `alloca()` call in `_ctypes_callproc`,
42 * to avoid allocating a massive buffer on the stack.
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> // for IUnknown interface
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 /* a callable object used for unpickling:
86 strong reference to _ctypes._unpickle() function */
87 PyObject *_unpickle;
88 PyObject *array_cache;
89 PyObject *error_object_name; // callproc.c
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 // GETFUNC: convert the C value at *ptr* to Python object, return the object
135 // SETFUNC: write content of the PyObject *value* to the location at *ptr*;
136 // return a new reference to either *value*, or None for simple types
137 // (see _CTYPES_DEBUG_KEEP).
138 // Note that the *size* arg can have different meanings depending on context:
139 // for string-like arrays it's the size in bytes
140 // for int-style fields it's either the type size, or bitfiled info
141 // that can be unpacked using the LOW_BIT & NUM_BITS macros.
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 /* A default buffer in CDataObject, which can be used for small C types. If
147 this buffer is too small, PyMem_Malloc will be called to create a larger one,
148 and this one is not used.
149
150 Making CDataObject a variable size object would be a better solution, but more
151 difficult in the presence of PyCFuncPtrObject. Maybe later.
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 Hm. Are there CDataObject's which do not need the b_objects member? In
166 this case we probably should introduce b_flags to mark it as present... If
167 b_objects is not present/unused b_length is unneeded as well.
168 */
169
170 struct tagCDataObject {
171 PyObject_HEAD
172 char *b_ptr; /* pointer to memory block */
173 int b_needsfree; /* need _we_ free the memory? */
174 CDataObject *b_base; /* pointer to base object or NULL */
175 Py_ssize_t b_size; /* size of memory block in bytes */
176 Py_ssize_t b_length; /* number of references we need */
177 Py_ssize_t b_index; /* index of this object into base's
178 b_object list */
179 PyObject *b_objects; /* dictionary of references we need to keep, or Py_None */
180 union value b_value;
181 };
182
183 typedef struct {
184 PyObject_VAR_HEAD
185 ffi_closure *pcl_write; /* the C callable, writeable */
186 void *pcl_exec; /* the C callable, executable */
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 /* First part identical to tagCDataObject */
202 PyObject_HEAD
203 char *b_ptr; /* pointer to memory block */
204 int b_needsfree; /* need _we_ free the memory? */
205 CDataObject *b_base; /* pointer to base object or NULL */
206 Py_ssize_t b_size; /* size of memory block in bytes */
207 Py_ssize_t b_length; /* number of references we need */
208 Py_ssize_t b_index; /* index of this object into base's
209 b_object list */
210 PyObject *b_objects; /* list of references we need to keep */
211 union value b_value;
212 /* end of tagCDataObject, additional fields follow */
213
214 CThunkObject *thunk;
215 PyObject *callable;
216
217 /* These two fields will override the ones in the type's stginfo if
218 they are set */
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 /* a table entry describing a predefined ctypes type */
270 struct fielddesc {
271 char code;
272 ffi_type *pffi_type; /* always statically allocated */
273 SETFUNC setfunc;
274 GETFUNC getfunc;
275 SETFUNC setfunc_swapped;
276 GETFUNC getfunc_swapped;
277 };
278
279 // Get all single-character type codes (for use in error messages)
280 extern char *_ctypes_get_simple_type_chars(void);
281
282 typedef struct CFieldObject {
283 PyObject_HEAD
284
285 /* byte size & offset
286 * For bit fields, this identifies a chunk of memory that the bits are
287 * extracted from. The entire chunk needs to be contained in the enclosing
288 * struct/union.
289 * byte_size is the same as the underlying ctype size (and thus it is
290 * redundant and could be eliminated).
291 * Note that byte_offset might not be aligned to proto's alignment.
292 */
293 Py_ssize_t byte_offset;
294 Py_ssize_t byte_size;
295
296 Py_ssize_t index; /* Index into CDataObject's object array */
297 PyObject *proto; /* underlying ctype; must have StgInfo */
298 GETFUNC getfunc; /* getter function if proto is NULL */
299 SETFUNC setfunc; /* setter function if proto is NULL */
300 bool anonymous: 1;
301
302 /* If this is a bit field, bitfield_size must be positive.
303 * bitfield_size and bit_offset specify the field inside the chunk of
304 * memory identified by byte_offset & byte_size.
305 * Otherwise, these are both zero.
306 *
307 * Note that for NON-bitfields:
308 * - `bit_size` (user-facing Python attribute) `is byte_size*8`
309 * - `bitfield_size` (this) is zero
310 * Hence the different name.
311 */
312 uint8_t bitfield_size;
313 uint8_t bit_offset;
314
315 PyObject *name; /* exact PyUnicode */
316 } CFieldObject;
317
318 #define _CFieldObject_CAST(op) ((CFieldObject *)(op))
319
320 /****************************************************************
321 StgInfo
322
323 Since Python 3.13, ctypes-specific type information is stored in the
324 corresponding type object, in a `StgInfo` struct accessed by the helpers
325 below.
326 Before that, each type's `tp_dict` was set to a dict *subclass* that included
327 the fields that are now in StgInfo. The mechanism was called "StgDict"; a few
328 references to that name might remain.
329
330 Functions for accessing StgInfo are `static inline` for performance;
331 see later in this file.
332
333 ****************************************************************
334
335 StgInfo fields
336
337 setfunc and getfunc is only set for simple data types, it is copied from the
338 corresponding fielddesc entry. These are functions to set and get the value
339 in a memory block.
340 They should probably by used by other types as well.
341
342 proto is only used for Pointer and Array types - it points to the item type
343 object.
344
345 Probably all the magic ctypes methods (like from_param) should have C
346 callable wrappers in the StgInfo. For simple data type, for example,
347 the fielddesc table could have entries for C codec from_param functions or
348 other methods as well, if a subtype overrides this method in Python at
349 construction time, or assigns to it later, tp_setattro should update the
350 StgInfo function to a generic one.
351
352 Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
353 type dict. They are only used to cache attributes from other entries, which
354 is wrong.
355
356 One use case is the .value attribute that all simple types have. But some
357 complex structures, like VARIANT, represent a single value also, and should
358 have this attribute.
359
360 Another use case is a _check_retval_ function, which is called when a ctypes
361 type is used as return type of a function to validate and compute the return
362 value.
363
364 Common ctypes protocol:
365
366 - setfunc: store a python value in a memory block
367 - getfunc: convert data from a memory block into a python value
368
369 - checkfunc: validate and convert a return value from a function call
370 - toparamfunc: convert a python value into a function argument
371
372 *****************************************************************/
373
374 typedef struct {
375 int initialized;
376 Py_ssize_t size; /* number of bytes */
377 Py_ssize_t align; /* alignment reqwuirements */
378 Py_ssize_t length; /* number of fields */
379 ffi_type ffi_type_pointer;
380 PyObject *proto; /* Only for Pointer/ArrayObject */
381 SETFUNC setfunc; /* Only for simple objects */
382 GETFUNC getfunc; /* Only for simple objects */
383 PARAMFUNC paramfunc;
384
385 /* Following fields only used by PyCFuncPtrType_Type instances */
386 PyObject *argtypes; /* tuple of CDataObjects */
387 PyObject *converters; /* tuple([t.from_param for t in argtypes]) */
388 PyObject *restype; /* CDataObject or NULL */
389 PyObject *checker;
390 PyObject *pointer_type; /* __pointer_type__ attribute;
391 arbitrary object or NULL */
392 PyObject *module;
393 int flags; /* calling convention and such */
394 #ifdef Py_GIL_DISABLED
395 PyMutex mutex; /* critical section mutex */
396 #endif
397 uint8_t dict_final;
398
399 /* pep3118 fields, pointers need PyMem_Free */
400 char *format;
401 int ndim;
402 Py_ssize_t *shape;
403 /* Py_ssize_t *strides; */ /* unused in ctypes */
404 /* Py_ssize_t *suboffsets; */ /* unused in ctypes */
405 } StgInfo;
406
407
408 /*
409 To ensure thread safety in the free threading build, the `STGINFO_LOCK` and
410 `STGINFO_UNLOCK` macros use critical sections to protect against concurrent
411 modifications to `StgInfo` and assignment of the `dict_final` field. Once
412 `dict_final` is set, `StgInfo` is treated as read-only, and no further
413 modifications are allowed. This approach allows most read operations to
414 proceed without acquiring the critical section lock.
415
416 The `dict_final` field is written only after all other modifications to
417 `StgInfo` are complete. The reads and writes of `dict_final` use the
418 sequentially consistent memory ordering to ensure that all other fields are
419 visible to other threads before the `dict_final` bit is set.
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 // Set the `dict_final` bit in StgInfo. It checks if the bit is already set
440 // and in that avoids acquiring the critical section (general case).
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; /* for the 'V' tag */
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 * Accessing StgInfo -- these are inlined for performance reasons.
551 */
552
553 // `PyStgInfo_From**` functions get a PyCTypeDataObject.
554 // These return -1 on error, 0 if "not found", 1 on OK.
555 // (Currently, these do not return -1 in practice. This might change
556 // in the future.)
557
558 //
559 // Common helper:
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 // not a ctypes class.
566 return 0;
567 }
568 StgInfo *info = PyObject_GetTypeData((PyObject *)type, state->PyCType_Type);
569 assert(info != NULL);
570 if (!info->initialized) {
571 // StgInfo is not initialized. This happens in abstract classes.
572 return 0;
573 }
574 *result = info;
575 return 1;
576 }
577 // from a type:
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 // from an instance:
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 // from either a type or an instance:
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 /* A variant of PyStgInfo_FromType that doesn't need the state
600 * and doesn't modify any refcounts,
601 * so it can be called from finalization functions when the module
602 * state is torn down.
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 // Initialize StgInfo on a newly created type
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 }