(root)/
Python-3.12.0/
Modules/
_ctypes/
ctypes.h
       1  #if defined (__SVR4) && defined (__sun)
       2  #   include <alloca.h>
       3  #endif
       4  
       5  #ifndef MS_WIN32
       6  #define max(a, b) ((a) > (b) ? (a) : (b))
       7  #define min(a, b) ((a) < (b) ? (a) : (b))
       8  
       9  #define PARAMFLAG_FIN 0x1
      10  #define PARAMFLAG_FOUT 0x2
      11  #define PARAMFLAG_FLCID 0x4
      12  #endif
      13  
      14  /*
      15   * bpo-13097: Max number of arguments CFuncPtr._argtypes_ and
      16   * _ctypes_callproc() will accept.
      17   *
      18   * This limit is enforced for the `alloca()` call in `_ctypes_callproc`,
      19   * to avoid allocating a massive buffer on the stack.
      20   */
      21  #ifndef CTYPES_MAX_ARGCOUNT
      22    #ifdef __EMSCRIPTEN__
      23      #define CTYPES_MAX_ARGCOUNT 1000
      24    #else
      25      #define CTYPES_MAX_ARGCOUNT 1024
      26    #endif
      27  #endif
      28  
      29  #if defined(__has_builtin)
      30  #if __has_builtin(__builtin_available)
      31  #define HAVE_BUILTIN_AVAILABLE 1
      32  #endif
      33  #endif
      34  
      35  typedef struct {
      36      PyTypeObject *DictRemover_Type;
      37      PyTypeObject *PyCArg_Type;
      38      PyTypeObject *PyCField_Type;
      39      PyTypeObject *PyCThunk_Type;
      40  #ifdef MS_WIN32
      41      PyTypeObject *PyComError_Type;
      42  #endif
      43      PyTypeObject *StructParam_Type;
      44  } ctypes_state;
      45  
      46  extern ctypes_state global_state;
      47  
      48  #define GLOBAL_STATE() (&global_state)
      49  
      50  extern PyType_Spec carg_spec;
      51  extern PyType_Spec cfield_spec;
      52  extern PyType_Spec cthunk_spec;
      53  
      54  typedef struct tagPyCArgObject PyCArgObject;
      55  typedef struct tagCDataObject CDataObject;
      56  typedef PyObject *(* GETFUNC)(void *, Py_ssize_t size);
      57  typedef PyObject *(* SETFUNC)(void *, PyObject *value, Py_ssize_t size);
      58  typedef PyCArgObject *(* PARAMFUNC)(CDataObject *obj);
      59  
      60  /* A default buffer in CDataObject, which can be used for small C types.  If
      61  this buffer is too small, PyMem_Malloc will be called to create a larger one,
      62  and this one is not used.
      63  
      64  Making CDataObject a variable size object would be a better solution, but more
      65  difficult in the presence of PyCFuncPtrObject.  Maybe later.
      66  */
      67  union value {
      68                  char c[16];
      69                  short s;
      70                  int i;
      71                  long l;
      72                  float f;
      73                  double d;
      74                  long long ll;
      75                  long double D;
      76  };
      77  
      78  /*
      79    Hm. Are there CDataObject's which do not need the b_objects member?  In
      80    this case we probably should introduce b_flags to mark it as present...  If
      81    b_objects is not present/unused b_length is unneeded as well.
      82  */
      83  
      84  struct tagCDataObject {
      85      PyObject_HEAD
      86      char *b_ptr;                /* pointer to memory block */
      87      int  b_needsfree;           /* need _we_ free the memory? */
      88      CDataObject *b_base;        /* pointer to base object or NULL */
      89      Py_ssize_t b_size;          /* size of memory block in bytes */
      90      Py_ssize_t b_length;        /* number of references we need */
      91      Py_ssize_t b_index;         /* index of this object into base's
      92                                 b_object list */
      93      PyObject *b_objects;        /* dictionary of references we need to keep, or Py_None */
      94      union value b_value;
      95  };
      96  
      97  typedef struct {
      98      PyObject_VAR_HEAD
      99      ffi_closure *pcl_write; /* the C callable, writeable */
     100      void *pcl_exec;         /* the C callable, executable */
     101      ffi_cif cif;
     102      int flags;
     103      PyObject *converters;
     104      PyObject *callable;
     105      PyObject *restype;
     106      SETFUNC setfunc;
     107      ffi_type *ffi_restype;
     108      ffi_type *atypes[1];
     109  } CThunkObject;
     110  #define CThunk_CheckExact(st, v)        Py_IS_TYPE(v, st->PyCThunk_Type)
     111  
     112  typedef struct {
     113      /* First part identical to tagCDataObject */
     114      PyObject_HEAD
     115      char *b_ptr;                /* pointer to memory block */
     116      int  b_needsfree;           /* need _we_ free the memory? */
     117      CDataObject *b_base;        /* pointer to base object or NULL */
     118      Py_ssize_t b_size;          /* size of memory block in bytes */
     119      Py_ssize_t b_length;        /* number of references we need */
     120      Py_ssize_t b_index;         /* index of this object into base's
     121                                 b_object list */
     122      PyObject *b_objects;        /* list of references we need to keep */
     123      union value b_value;
     124      /* end of tagCDataObject, additional fields follow */
     125  
     126      CThunkObject *thunk;
     127      PyObject *callable;
     128  
     129      /* These two fields will override the ones in the type's stgdict if
     130         they are set */
     131      PyObject *converters;
     132      PyObject *argtypes;
     133      PyObject *restype;
     134      PyObject *checker;
     135      PyObject *errcheck;
     136  #ifdef MS_WIN32
     137      int index;
     138      GUID *iid;
     139  #endif
     140      PyObject *paramflags;
     141  } PyCFuncPtrObject;
     142  
     143  extern PyTypeObject PyCStgDict_Type;
     144  #define PyCStgDict_CheckExact(v)            Py_IS_TYPE(v, &PyCStgDict_Type)
     145  #define PyCStgDict_Check(v)         PyObject_TypeCheck(v, &PyCStgDict_Type)
     146  
     147  extern int PyCStructUnionType_update_stgdict(PyObject *fields, PyObject *type, int isStruct);
     148  extern int PyType_stginfo(PyTypeObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
     149  extern int PyObject_stginfo(PyObject *self, Py_ssize_t *psize, Py_ssize_t *palign, Py_ssize_t *plength);
     150  
     151  
     152  
     153  extern PyTypeObject PyCData_Type;
     154  #define CDataObject_CheckExact(v)       Py_IS_TYPE(v, &PyCData_Type)
     155  #define CDataObject_Check(v)            PyObject_TypeCheck(v, &PyCData_Type)
     156  #define _CDataObject_HasExternalBuffer(v)  ((v)->b_ptr != (char *)&(v)->b_value)
     157  
     158  extern PyTypeObject PyCSimpleType_Type;
     159  #define PyCSimpleTypeObject_CheckExact(v)       Py_IS_TYPE(v, &PyCSimpleType_Type)
     160  #define PyCSimpleTypeObject_Check(v)    PyObject_TypeCheck(v, &PyCSimpleType_Type)
     161  
     162  extern struct fielddesc *_ctypes_get_fielddesc(const char *fmt);
     163  
     164  
     165  extern PyObject *
     166  PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
     167                  Py_ssize_t *pfield_size, int bitsize, int *pbitofs,
     168                  Py_ssize_t *psize, Py_ssize_t *poffset, Py_ssize_t *palign,
     169                  int pack, int is_big_endian);
     170  
     171  extern PyObject *PyCData_AtAddress(PyObject *type, void *buf);
     172  extern PyObject *PyCData_FromBytes(PyObject *type, char *data, Py_ssize_t length);
     173  
     174  extern PyTypeObject PyCArrayType_Type;
     175  extern PyTypeObject PyCArray_Type;
     176  extern PyTypeObject PyCPointerType_Type;
     177  extern PyTypeObject PyCPointer_Type;
     178  extern PyTypeObject PyCFuncPtr_Type;
     179  extern PyTypeObject PyCFuncPtrType_Type;
     180  extern PyTypeObject PyCStructType_Type;
     181  
     182  #define PyCArrayTypeObject_Check(v)     PyObject_TypeCheck(v, &PyCArrayType_Type)
     183  #define ArrayObject_Check(v)            PyObject_TypeCheck(v, &PyCArray_Type)
     184  #define PointerObject_Check(v)          PyObject_TypeCheck(v, &PyCPointer_Type)
     185  #define PyCPointerTypeObject_Check(v)   PyObject_TypeCheck(v, &PyCPointerType_Type)
     186  #define PyCFuncPtrObject_Check(v)               PyObject_TypeCheck(v, &PyCFuncPtr_Type)
     187  #define PyCFuncPtrTypeObject_Check(v)   PyObject_TypeCheck(v, &PyCFuncPtrType_Type)
     188  #define PyCStructTypeObject_Check(v)    PyObject_TypeCheck(v, &PyCStructType_Type)
     189  
     190  extern PyObject *
     191  PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length);
     192  
     193  extern PyMethodDef _ctypes_module_methods[];
     194  
     195  extern CThunkObject *_ctypes_alloc_callback(PyObject *callable,
     196                                             PyObject *converters,
     197                                             PyObject *restype,
     198                                             int flags);
     199  /* a table entry describing a predefined ctypes type */
     200  struct fielddesc {
     201      char code;
     202      SETFUNC setfunc;
     203      GETFUNC getfunc;
     204      ffi_type *pffi_type; /* always statically allocated */
     205      SETFUNC setfunc_swapped;
     206      GETFUNC getfunc_swapped;
     207  };
     208  
     209  typedef struct {
     210      PyObject_HEAD
     211      Py_ssize_t offset;
     212      Py_ssize_t size;
     213      Py_ssize_t index;                   /* Index into CDataObject's
     214                                         object array */
     215      PyObject *proto;                    /* a type or NULL */
     216      GETFUNC getfunc;                    /* getter function if proto is NULL */
     217      SETFUNC setfunc;                    /* setter function if proto is NULL */
     218      int anonymous;
     219  } CFieldObject;
     220  
     221  /* A subclass of PyDictObject, used as the instance dictionary of ctypes
     222     metatypes */
     223  typedef struct {
     224      PyDictObject dict;          /* first part identical to PyDictObject */
     225  /* The size and align fields are unneeded, they are in ffi_type as well.  As
     226     an experiment shows, it's trivial to get rid of them, the only thing to
     227     remember is that in PyCArrayType_new the ffi_type fields must be filled in -
     228     so far it was unneeded because libffi doesn't support arrays at all
     229     (because they are passed as pointers to function calls anyway).  But it's
     230     too much risk to change that now, and there are other fields which doesn't
     231     belong into this structure anyway.  Maybe in ctypes 2.0... (ctypes 2000?)
     232  */
     233      Py_ssize_t size;            /* number of bytes */
     234      Py_ssize_t align;           /* alignment requirements */
     235      Py_ssize_t length;          /* number of fields */
     236      ffi_type ffi_type_pointer;
     237      PyObject *proto;            /* Only for Pointer/ArrayObject */
     238      SETFUNC setfunc;            /* Only for simple objects */
     239      GETFUNC getfunc;            /* Only for simple objects */
     240      PARAMFUNC paramfunc;
     241  
     242      /* Following fields only used by PyCFuncPtrType_Type instances */
     243      PyObject *argtypes;         /* tuple of CDataObjects */
     244      PyObject *converters;       /* tuple([t.from_param for t in argtypes]) */
     245      PyObject *restype;          /* CDataObject or NULL */
     246      PyObject *checker;
     247      int flags;                  /* calling convention and such */
     248  
     249      /* pep3118 fields, pointers need PyMem_Free */
     250      char *format;
     251      int ndim;
     252      Py_ssize_t *shape;
     253  /*      Py_ssize_t *strides;    */ /* unused in ctypes */
     254  /*      Py_ssize_t *suboffsets; */ /* unused in ctypes */
     255  
     256  } StgDictObject;
     257  
     258  /****************************************************************
     259   StgDictObject fields
     260  
     261   setfunc and getfunc is only set for simple data types, it is copied from the
     262   corresponding fielddesc entry.  These are functions to set and get the value
     263   in a memory block.
     264   They should probably by used by other types as well.
     265  
     266   proto is only used for Pointer and Array types - it points to the item type
     267   object.
     268  
     269   Probably all the magic ctypes methods (like from_param) should have C
     270   callable wrappers in the StgDictObject.  For simple data type, for example,
     271   the fielddesc table could have entries for C codec from_param functions or
     272   other methods as well, if a subtype overrides this method in Python at
     273   construction time, or assigns to it later, tp_setattro should update the
     274   StgDictObject function to a generic one.
     275  
     276   Currently, PyCFuncPtr types have 'converters' and 'checker' entries in their
     277   type dict.  They are only used to cache attributes from other entries, which
     278   is wrong.
     279  
     280   One use case is the .value attribute that all simple types have.  But some
     281   complex structures, like VARIANT, represent a single value also, and should
     282   have this attribute.
     283  
     284   Another use case is a _check_retval_ function, which is called when a ctypes
     285   type is used as return type of a function to validate and compute the return
     286   value.
     287  
     288   Common ctypes protocol:
     289  
     290    - setfunc: store a python value in a memory block
     291    - getfunc: convert data from a memory block into a python value
     292  
     293    - checkfunc: validate and convert a return value from a function call
     294    - toparamfunc: convert a python value into a function argument
     295  
     296  *****************************************************************/
     297  
     298  /* May return NULL, but does not set an exception! */
     299  extern StgDictObject *PyType_stgdict(PyObject *obj);
     300  
     301  /* May return NULL, but does not set an exception! */
     302  extern StgDictObject *PyObject_stgdict(PyObject *self);
     303  
     304  extern int PyCStgDict_clone(StgDictObject *src, StgDictObject *dst);
     305  
     306  typedef int(* PPROC)(void);
     307  
     308  PyObject *_ctypes_callproc(PPROC pProc,
     309                      PyObject *arguments,
     310  #ifdef MS_WIN32
     311                      IUnknown *pIUnk,
     312                      GUID *iid,
     313  #endif
     314                      int flags,
     315                      PyObject *argtypes,
     316                      PyObject *restype,
     317                      PyObject *checker);
     318  
     319  
     320  #define FUNCFLAG_STDCALL 0x0
     321  #define FUNCFLAG_CDECL   0x1
     322  #define FUNCFLAG_HRESULT 0x2
     323  #define FUNCFLAG_PYTHONAPI 0x4
     324  #define FUNCFLAG_USE_ERRNO 0x8
     325  #define FUNCFLAG_USE_LASTERROR 0x10
     326  
     327  #define TYPEFLAG_ISPOINTER 0x100
     328  #define TYPEFLAG_HASPOINTER 0x200
     329  #define TYPEFLAG_HASUNION 0x400
     330  #define TYPEFLAG_HASBITFIELD 0x800
     331  
     332  #define DICTFLAG_FINAL 0x1000
     333  
     334  struct tagPyCArgObject {
     335      PyObject_HEAD
     336      ffi_type *pffi_type;
     337      char tag;
     338      union {
     339          char c;
     340          char b;
     341          short h;
     342          int i;
     343          long l;
     344          long long q;
     345          long double D;
     346          double d;
     347          float f;
     348          void *p;
     349      } value;
     350      PyObject *obj;
     351      Py_ssize_t size; /* for the 'V' tag */
     352  };
     353  
     354  #define PyCArg_CheckExact(st, v)        Py_IS_TYPE(v, st->PyCArg_Type)
     355  extern PyCArgObject *PyCArgObject_new(void);
     356  
     357  extern PyObject *
     358  PyCData_get(PyObject *type, GETFUNC getfunc, PyObject *src,
     359            Py_ssize_t index, Py_ssize_t size, char *ptr);
     360  
     361  extern int
     362  PyCData_set(PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
     363            Py_ssize_t index, Py_ssize_t size, char *ptr);
     364  
     365  extern void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...);
     366  
     367  struct basespec {
     368      CDataObject *base;
     369      Py_ssize_t index;
     370      char *adr;
     371  };
     372  
     373  extern char basespec_string[];
     374  
     375  extern ffi_type *_ctypes_get_ffi_type(PyObject *obj);
     376  
     377  /* exception classes */
     378  extern PyObject *PyExc_ArgError;
     379  
     380  extern char *_ctypes_conversion_encoding;
     381  extern char *_ctypes_conversion_errors;
     382  
     383  
     384  extern void _ctypes_free_closure(void *);
     385  extern void *_ctypes_alloc_closure(void);
     386  
     387  extern PyObject *PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr);
     388  extern char *_ctypes_alloc_format_string(const char *prefix, const char *suffix);
     389  extern char *_ctypes_alloc_format_string_with_shape(int ndim,
     390                                                  const Py_ssize_t *shape,
     391                                                  const char *prefix, const char *suffix);
     392  
     393  extern int _ctypes_simple_instance(PyObject *obj);
     394  
     395  extern PyObject *_ctypes_ptrtype_cache;
     396  PyObject *_ctypes_get_errobj(int **pspace);
     397  
     398  #ifdef USING_MALLOC_CLOSURE_DOT_C
     399  void Py_ffi_closure_free(void *p);
     400  void *Py_ffi_closure_alloc(size_t size, void** codeloc);
     401  #else
     402  #define Py_ffi_closure_free ffi_closure_free
     403  #define Py_ffi_closure_alloc ffi_closure_alloc
     404  #endif
     405  
     406  /*
     407   Local Variables:
     408   compile-command: "python setup.py -q build install --home ~"
     409   End:
     410  */