#ifndef Py_BUILD_CORE_BUILTIN
#define Py_BUILD_CORE_MODULE …
#endif
#include "Python.h"
#ifdef MS_WIN32
# include <windows.h>
#endif
#include "pycore_call.h"
#include "pycore_ceval.h"
#ifdef MS_WIN32
# include "pycore_modsupport.h"
#endif
#include <ffi.h>
#ifdef MS_WIN32
#include <malloc.h>
#ifndef IS_INTRESOURCE
#define IS_INTRESOURCE …
#endif
#else
#include <dlfcn.h>
#endif
#include "ctypes.h"
#include "pycore_long.h"
#define clinic_state …
#define clinic_state_sub …
#include "clinic/_ctypes.c.h"
#undef clinic_state
#undef clinic_state_sub
DictRemoverObject;
static int
_DictRemover_traverse(DictRemoverObject *self, visitproc visit, void *arg)
{ … }
static int
_DictRemover_clear(DictRemoverObject *self)
{ … }
static void
_DictRemover_dealloc(PyObject *myself)
{ … }
static PyObject *
_DictRemover_call(PyObject *myself, PyObject *args, PyObject *kw)
{ … }
PyDoc_STRVAR(dictremover_doc, "deletes a key from a dictionary");
static PyType_Slot dictremover_slots[] = …;
static PyType_Spec dictremover_spec = …;
int
PyDict_SetItemProxy(ctypes_state *st, PyObject *dict, PyObject *key, PyObject *item)
{ … }
static int
_PyDict_GetItemProxy(PyObject *dict, PyObject *key, PyObject **presult)
{ … }
static char *
_ctypes_alloc_format_string_for_type(char code, int big_endian)
{ … }
static char *
_ctypes_alloc_format_string(const char *prefix, const char *suffix)
{ … }
static char *
_ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape,
const char *prefix, const char *suffix)
{ … }
StructParamObject;
static int
StructParam_traverse(StructParamObject *self, visitproc visit, void *arg)
{ … }
static int
StructParam_clear(StructParamObject *self)
{ … }
static void
StructParam_dealloc(PyObject *myself)
{ … }
static PyType_Slot structparam_slots[] = …;
static PyType_Spec structparam_spec = …;
static int
CType_Type_traverse(PyObject *self, visitproc visit, void *arg)
{ … }
void
ctype_clear_stginfo(StgInfo *info)
{ … }
static int
CType_Type_clear(PyObject *self)
{ … }
static void
CType_Type_dealloc(PyObject *self)
{ … }
static PyObject *
_ctypes_CType_Type___sizeof___impl(PyObject *self, PyTypeObject *cls)
{ … }
static PyObject *
CType_Type_repeat(PyObject *self, Py_ssize_t length);
static PyMethodDef ctype_methods[] = …;
static PyType_Slot ctype_type_slots[] = …;
PyType_Spec pyctype_type_spec = …;
static PyCArgObject *
StructUnionType_paramfunc(ctypes_state *st, CDataObject *self)
{ … }
static int
StructUnionType_init(PyObject *self, PyObject *args, PyObject *kwds, int isStruct)
{ … }
static int
PyCStructType_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static int
UnionType_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static PyObject *
CDataType_from_address_impl(PyObject *type, PyTypeObject *cls,
PyObject *value)
{ … }
static int
KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep);
static PyObject *
CDataType_from_buffer_impl(PyObject *type, PyTypeObject *cls, PyObject *obj,
Py_ssize_t offset)
{ … }
static inline PyObject *
generic_pycdata_new(ctypes_state *st,
PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
static PyObject *
CDataType_from_buffer_copy_impl(PyObject *type, PyTypeObject *cls,
Py_buffer *buffer, Py_ssize_t offset)
{ … }
static PyObject *
CDataType_in_dll_impl(PyObject *type, PyTypeObject *cls, PyObject *dll,
const char *name)
{ … }
static PyObject *
CDataType_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
{ … }
static PyMethodDef CDataType_methods[] = …;
static PyObject *
CType_Type_repeat(PyObject *self, Py_ssize_t length)
{ … }
static int
_structunion_setattro(PyObject *self, PyObject *key, PyObject *value, int is_struct)
{ … }
static int
PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
{ … }
static int
UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
{ … }
static PyType_Slot pycstruct_type_slots[] = …;
static PyType_Spec pycstruct_type_spec = …;
static PyType_Slot union_type_slots[] = …;
static PyType_Spec union_type_spec = …;
static int
PyCPointerType_SetProto(ctypes_state *st, StgInfo *stginfo, PyObject *proto)
{ … }
static PyCArgObject *
PyCPointerType_paramfunc(ctypes_state *st, CDataObject *self)
{ … }
static int
PyCPointerType_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static PyObject *
PyCPointerType_set_type_impl(PyTypeObject *self, PyTypeObject *cls,
PyObject *type)
{ … }
static PyObject *_byref(ctypes_state *, PyObject *);
static PyObject *
PyCPointerType_from_param_impl(PyObject *type, PyTypeObject *cls,
PyObject *value)
{ … }
static PyMethodDef PyCPointerType_methods[] = …;
static PyType_Slot pycpointer_type_slots[] = …;
static PyType_Spec pycpointer_type_spec = …;
static int
CharArray_set_raw(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{ … }
static PyObject *
CharArray_get_raw(CDataObject *self, void *Py_UNUSED(ignored))
{ … }
static PyObject *
CharArray_get_value(CDataObject *self, void *Py_UNUSED(ignored))
{ … }
static int
CharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{ … }
static PyGetSetDef CharArray_getsets[] = …;
static PyObject *
WCharArray_get_value(CDataObject *self, void *Py_UNUSED(ignored))
{ … }
static int
WCharArray_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{ … }
static PyGetSetDef WCharArray_getsets[] = …;
static int
add_getset(PyTypeObject *type, PyGetSetDef *gsp)
{ … }
static PyCArgObject *
PyCArrayType_paramfunc(ctypes_state *st, CDataObject *self)
{ … }
static int
PyCArrayType_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static PyType_Slot pycarray_type_slots[] = …;
static PyType_Spec pycarray_type_spec = …;
#if defined(Py_HAVE_C_COMPLEX) && defined(Py_FFI_SUPPORT_C_COMPLEX)
static const char SIMPLE_TYPE_CHARS[] = "cbBhHiIlLdCEFfuzZqQPXOv?g";
#else
static const char SIMPLE_TYPE_CHARS[] = …;
#endif
static PyObject *
c_wchar_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
{ … }
static PyObject *
c_char_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
{ … }
static PyObject *
c_void_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
{ … }
static PyMethodDef c_void_p_methods[] = …;
static PyMethodDef c_char_p_methods[] = …;
static PyMethodDef c_wchar_p_methods[] = …;
static PyObject *CreateSwappedType(ctypes_state *st, PyTypeObject *type,
PyObject *args, PyObject *kwds,
PyObject *proto, struct fielddesc *fmt)
{ … }
static PyCArgObject *
PyCSimpleType_paramfunc(ctypes_state *st, CDataObject *self)
{ … }
static int
PyCSimpleType_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static PyObject *
PyCSimpleType_from_param_impl(PyObject *type, PyTypeObject *cls,
PyObject *value)
{ … }
static PyMethodDef PyCSimpleType_methods[] = …;
static PyType_Slot pycsimple_type_slots[] = …;
static PyType_Spec pycsimple_type_spec = …;
static PyObject *
converters_from_argtypes(ctypes_state *st, PyObject *ob)
{ … }
static int
make_funcptrtype_dict(ctypes_state *st, PyObject *attrdict, StgInfo *stginfo)
{ … }
static PyCArgObject *
PyCFuncPtrType_paramfunc(ctypes_state *st, CDataObject *self)
{ … }
static int
PyCFuncPtrType_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static PyType_Slot pycfuncptr_type_slots[] = …;
static PyType_Spec pycfuncptr_type_spec = …;
static CDataObject *
PyCData_GetContainer(CDataObject *self)
{ … }
static PyObject *
GetKeepedObjects(CDataObject *target)
{ … }
static PyObject *
unique_key(CDataObject *target, Py_ssize_t index)
{ … }
static int
KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep)
{ … }
static int
PyCData_traverse(CDataObject *self, visitproc visit, void *arg)
{ … }
static int
PyCData_clear(CDataObject *self)
{ … }
static void
PyCData_dealloc(PyObject *self)
{ … }
static PyMemberDef PyCData_members[] = …;
static PyObject *
PyCData_item_type(ctypes_state *st, PyObject *type)
{ … }
static int
PyCData_NewGetBuffer(PyObject *myself, Py_buffer *view, int flags)
{ … }
static Py_hash_t
PyCData_nohash(PyObject *self)
{ … }
static PyObject *
PyCData_reduce_impl(PyObject *myself, PyTypeObject *cls)
{ … }
static PyObject *
PyCData_setstate(PyObject *myself, PyObject *args)
{ … }
static PyObject *
PyCData_from_outparam(PyObject *self, PyObject *args)
{ … }
static PyMethodDef PyCData_methods[] = …;
static PyType_Slot pycdata_slots[] = …;
static PyType_Spec pycdata_spec = …;
static int
PyCData_MallocBuffer(CDataObject *obj, StgInfo *info)
{ … }
PyObject *
PyCData_FromBaseObj(ctypes_state *st,
PyObject *type, PyObject *base, Py_ssize_t index, char *adr)
{ … }
PyObject *
PyCData_AtAddress(ctypes_state *st, PyObject *type, void *buf)
{ … }
int _ctypes_simple_instance(ctypes_state *st, PyObject *obj)
{ … }
PyObject *
PyCData_get(ctypes_state *st, PyObject *type, GETFUNC getfunc, PyObject *src,
Py_ssize_t index, Py_ssize_t size, char *adr)
{ … }
static PyObject *
_PyCData_set(ctypes_state *st,
CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
Py_ssize_t size, char *ptr)
{ … }
int
PyCData_set(ctypes_state *st,
PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
Py_ssize_t index, Py_ssize_t size, char *ptr)
{ … }
static PyObject *
GenericPyCData_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ … }
static inline PyObject *
generic_pycdata_new(ctypes_state *st,
PyTypeObject *type, PyObject *args, PyObject *kwds)
{ … }
static int
PyCFuncPtr_set_errcheck(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ignored))
{ … }
static PyObject *
PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
{ … }
static int
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ignored))
{ … }
static PyObject *
PyCFuncPtr_get_restype(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
{ … }
static int
PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob, void *Py_UNUSED(ignored))
{ … }
static PyObject *
PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self, void *Py_UNUSED(ignored))
{ … }
static PyGetSetDef PyCFuncPtr_getsets[] = …;
#ifdef MS_WIN32
static PPROC FindAddress(void *handle, const char *name, PyObject *type)
{
PPROC address;
#ifdef MS_WIN64
Py_BEGIN_ALLOW_THREADS
address = (PPROC)GetProcAddress(handle, name);
Py_END_ALLOW_THREADS
return address;
#else
char *mangled_name;
int i;
Py_BEGIN_ALLOW_THREADS
address = (PPROC)GetProcAddress(handle, name);
Py_END_ALLOW_THREADS
if (address)
return address;
if (((size_t)name & ~0xFFFF) == 0) {
return NULL;
}
ctypes_state *st = get_module_state_by_def(Py_TYPE(type));
StgInfo *info;
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) {
return NULL;
}
if (info==NULL || info->flags & FUNCFLAG_CDECL)
return address;
mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3);
if (!mangled_name)
return NULL;
for (i = 0; i < 32; ++i) {
sprintf(mangled_name, "_%s@%d", name, i*4);
Py_BEGIN_ALLOW_THREADS
address = (PPROC)GetProcAddress(handle, mangled_name);
Py_END_ALLOW_THREADS
if (address)
return address;
}
return NULL;
#endif
}
#endif
static int
_check_outarg_type(ctypes_state *st, PyObject *arg, Py_ssize_t index)
{ … }
static int
_validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags)
{ … }
static int
_get_name(PyObject *obj, const char **pname)
{ … }
static PyObject *
PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ … }
#ifdef MS_WIN32
static PyObject *
PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyCFuncPtrObject *self;
int index;
char *name = NULL;
PyObject *paramflags = NULL;
GUID *iid = NULL;
Py_ssize_t iid_len = 0;
if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, ¶mflags, &iid, &iid_len))
return NULL;
if (paramflags == Py_None)
paramflags = NULL;
ctypes_state *st = get_module_state_by_def(Py_TYPE(type));
if (!_validate_paramflags(st, type, paramflags)) {
return NULL;
}
self = (PyCFuncPtrObject *)generic_pycdata_new(st, type, args, kwds);
self->index = index + 0x1000;
self->paramflags = Py_XNewRef(paramflags);
if (iid_len == sizeof(GUID))
self->iid = iid;
return (PyObject *)self;
}
#endif
static PyObject *
PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{ … }
static PyObject *
_byref(ctypes_state *st, PyObject *obj)
{ … }
static PyObject *
_get_arg(int *pindex, PyObject *name, PyObject *defval, PyObject *inargs, PyObject *kwds)
{ … }
static PyObject *
_build_callargs(ctypes_state *st, PyCFuncPtrObject *self, PyObject *argtypes,
PyObject *inargs, PyObject *kwds,
int *poutmask, int *pinoutmask, unsigned int *pnumretvals)
{ … }
static PyObject *
_build_result(PyObject *result, PyObject *callargs,
int outmask, int inoutmask, unsigned int numretvals)
{ … }
static PyObject *
PyCFuncPtr_call(PyCFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
{ … }
static int
PyCFuncPtr_traverse(PyCFuncPtrObject *self, visitproc visit, void *arg)
{ … }
static int
PyCFuncPtr_clear(PyCFuncPtrObject *self)
{ … }
static void
PyCFuncPtr_dealloc(PyCFuncPtrObject *self)
{ … }
static PyObject *
PyCFuncPtr_repr(PyCFuncPtrObject *self)
{ … }
static int
PyCFuncPtr_bool(PyCFuncPtrObject *self)
{ … }
static PyType_Slot pycfuncptr_slots[] = …;
static PyType_Spec pycfuncptr_spec = …;
static Py_ssize_t
_init_pos_args(PyObject *self, PyTypeObject *type,
PyObject *args, PyObject *kwds,
Py_ssize_t index)
{ … }
static int
Struct_init(PyObject *self, PyObject *args, PyObject *kwds)
{ … }
static PyType_Slot pycstruct_slots[] = …;
static PyType_Spec pycstruct_spec = …;
static PyType_Slot pycunion_slots[] = …;
static PyType_Spec pycunion_spec = …;
static int
Array_init(CDataObject *self, PyObject *args, PyObject *kw)
{ … }
static PyObject *
Array_item(PyObject *myself, Py_ssize_t index)
{ … }
static PyObject *
Array_subscript(PyObject *myself, PyObject *item)
{ … }
static int
Array_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value)
{ … }
static int
Array_ass_subscript(PyObject *myself, PyObject *item, PyObject *value)
{ … }
static Py_ssize_t
Array_length(PyObject *myself)
{ … }
static PyMethodDef Array_methods[] = …;
PyDoc_STRVAR(array_doc,
"Abstract base class for arrays.\n"
"\n"
"The recommended way to create concrete array types is by multiplying any\n"
"ctypes data type with a non-negative integer. Alternatively, you can subclass\n"
"this type and define _length_ and _type_ class variables. Array elements can\n"
"be read and written using standard subscript and slice accesses for slice\n"
"reads, the resulting object is not itself an Array."
);
static PyType_Slot pycarray_slots[] = …;
static PyType_Spec pycarray_spec = …;
PyObject *
PyCArrayType_from_ctype(ctypes_state *st, PyObject *itemtype, Py_ssize_t length)
{ … }
static int
Simple_set_value(CDataObject *self, PyObject *value, void *Py_UNUSED(ignored))
{ … }
static int
Simple_init(CDataObject *self, PyObject *args, PyObject *kw)
{ … }
static PyObject *
Simple_get_value(CDataObject *self, void *Py_UNUSED(ignored))
{ … }
static PyGetSetDef Simple_getsets[] = …;
static PyObject *
Simple_from_outparm_impl(PyObject *self, PyTypeObject *cls)
{ … }
static PyMethodDef Simple_methods[] = …;
static int Simple_bool(CDataObject *self)
{ … }
static PyObject *
Simple_repr(CDataObject *self)
{ … }
static PyType_Slot pycsimple_slots[] = …;
static PyType_Spec pycsimple_spec = …;
static PyObject *
Pointer_item(PyObject *myself, Py_ssize_t index)
{ … }
static int
Pointer_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value)
{ … }
static PyObject *
Pointer_get_contents(CDataObject *self, void *closure)
{ … }
static int
Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)
{ … }
static PyGetSetDef Pointer_getsets[] = …;
static int
Pointer_init(CDataObject *self, PyObject *args, PyObject *kw)
{ … }
static PyObject *
Pointer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
{ … }
static PyObject *
Pointer_subscript(PyObject *myself, PyObject *item)
{ … }
static int
Pointer_bool(CDataObject *self)
{ … }
static PyType_Slot pycpointer_slots[] = …;
static PyType_Spec pycpointer_spec = …;
PyDoc_STRVAR(_ctypes__doc__,
"Create and manipulate C compatible data types in Python.");
#ifdef MS_WIN32
PyDoc_STRVAR(comerror_doc, "Raised when a COM method call failed.");
int
comerror_init(PyObject *self, PyObject *args, PyObject *kwds)
{
PyObject *hresult, *text, *details;
PyObject *a;
int status;
if (!_PyArg_NoKeywords(Py_TYPE(self)->tp_name, kwds))
return -1;
if (!PyArg_ParseTuple(args, "OOO:COMError", &hresult, &text, &details))
return -1;
a = PySequence_GetSlice(args, 1, PyTuple_GET_SIZE(args));
if (!a)
return -1;
status = PyObject_SetAttrString(self, "args", a);
Py_DECREF(a);
if (status < 0)
return -1;
if (PyObject_SetAttrString(self, "hresult", hresult) < 0)
return -1;
if (PyObject_SetAttrString(self, "text", text) < 0)
return -1;
if (PyObject_SetAttrString(self, "details", details) < 0)
return -1;
Py_INCREF(args);
Py_SETREF(((PyBaseExceptionObject *)self)->args, args);
return 0;
}
static int
comerror_clear(PyObject *self)
{
return ((PyTypeObject *)PyExc_BaseException)->tp_clear(self);
}
static int
comerror_traverse(PyObject *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return ((PyTypeObject *)PyExc_BaseException)->tp_traverse(self, visit, arg);
}
static void
comerror_dealloc(PyObject *self)
{
PyTypeObject *tp = Py_TYPE(self);
PyObject_GC_UnTrack(self);
(void)comerror_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
}
static PyType_Slot comerror_slots[] = {
{Py_tp_doc, (void *)PyDoc_STR(comerror_doc)},
{Py_tp_init, comerror_init},
{Py_tp_traverse, comerror_traverse},
{Py_tp_dealloc, comerror_dealloc},
{Py_tp_clear, comerror_clear},
{0, NULL},
};
static PyType_Spec comerror_spec = {
.name = "_ctypes.COMError",
.basicsize = sizeof(PyBaseExceptionObject),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
.slots = comerror_slots,
};
#endif
static PyObject *
string_at(const char *ptr, int size)
{ … }
static int
cast_check_pointertype(ctypes_state *st, PyObject *arg)
{ … }
static PyObject *
cast(void *ptr, PyObject *src, PyObject *ctype)
{ … }
static PyObject *
wstring_at(const wchar_t *ptr, int size)
{ … }
static int
_ctypes_add_types(PyObject *mod)
{ … }
static int
_ctypes_add_objects(PyObject *mod)
{ … }
static int
_ctypes_mod_exec(PyObject *mod)
{ … }
static int
module_traverse(PyObject *module, visitproc visit, void *arg) { … }
static int
module_clear(PyObject *module) { … }
static void
module_free(void *module)
{ … }
static PyModuleDef_Slot module_slots[] = …;
struct PyModuleDef _ctypesmodule = …;
PyMODINIT_FUNC
PyInit__ctypes(void)
{ … }