300
300
static PyCArgObject *
301
301
StructUnionType_paramfunc(CDataObject *self)
304
StgDictObject *stgdict;
306
parg = PyCArgObject_new();
311
stgdict = PyObject_stgdict((PyObject *)self);
312
assert(stgdict); /* Cannot be NULL for structure/union instances */
313
parg->pffi_type = &stgdict->ffi_type_pointer;
314
/* For structure parameters (by value), parg->value doesn't contain the structure
315
data itself, instead parg->value.p *points* to the structure's data
316
See also _ctypes.c, function _call_function_pointer().
318
parg->value.p = self->b_ptr;
319
parg->size = self->b_size;
321
parg->obj = (PyObject *)self;
304
StgDictObject *stgdict;
306
parg = PyCArgObject_new();
311
stgdict = PyObject_stgdict((PyObject *)self);
312
assert(stgdict); /* Cannot be NULL for structure/union instances */
313
parg->pffi_type = &stgdict->ffi_type_pointer;
314
/* For structure parameters (by value), parg->value doesn't contain the structure
315
data itself, instead parg->value.p *points* to the structure's data
316
See also _ctypes.c, function _call_function_pointer().
318
parg->value.p = self->b_ptr;
319
parg->size = self->b_size;
321
parg->obj = (PyObject *)self;
325
325
static PyObject *
326
326
StructUnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds, int isStruct)
328
PyTypeObject *result;
332
/* create the new instance (which is a class,
333
since we are a metatype!) */
334
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
338
/* keep this for bw compatibility */
339
if (PyDict_GetItemString(result->tp_dict, "_abstract_"))
340
return (PyObject *)result;
342
dict = (StgDictObject *)PyObject_CallObject((PyObject *)&PyCStgDict_Type, NULL);
347
/* replace the class dict by our updated stgdict, which holds info
348
about storage requirements of the instances */
349
if (-1 == PyDict_Update((PyObject *)dict, result->tp_dict)) {
351
Py_DECREF((PyObject *)dict);
354
Py_DECREF(result->tp_dict);
355
result->tp_dict = (PyObject *)dict;
356
dict->format = _ctypes_alloc_format_string(NULL, "B");
357
if (dict->format == NULL) {
362
dict->paramfunc = StructUnionType_paramfunc;
364
fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
366
StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base);
368
if (basedict == NULL)
369
return (PyObject *)result;
371
if (-1 == PyCStgDict_clone(dict, basedict)) {
375
dict->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass dict */
376
basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */
377
return (PyObject *)result;
380
if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) {
384
return (PyObject *)result;
328
PyTypeObject *result;
332
/* create the new instance (which is a class,
333
since we are a metatype!) */
334
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
338
/* keep this for bw compatibility */
339
if (PyDict_GetItemString(result->tp_dict, "_abstract_"))
340
return (PyObject *)result;
342
dict = (StgDictObject *)PyObject_CallObject((PyObject *)&PyCStgDict_Type, NULL);
347
/* replace the class dict by our updated stgdict, which holds info
348
about storage requirements of the instances */
349
if (-1 == PyDict_Update((PyObject *)dict, result->tp_dict)) {
351
Py_DECREF((PyObject *)dict);
354
Py_DECREF(result->tp_dict);
355
result->tp_dict = (PyObject *)dict;
356
dict->format = _ctypes_alloc_format_string(NULL, "B");
357
if (dict->format == NULL) {
362
dict->paramfunc = StructUnionType_paramfunc;
364
fields = PyDict_GetItemString((PyObject *)dict, "_fields_");
366
StgDictObject *basedict = PyType_stgdict((PyObject *)result->tp_base);
368
if (basedict == NULL)
369
return (PyObject *)result;
371
if (-1 == PyCStgDict_clone(dict, basedict)) {
375
dict->flags &= ~DICTFLAG_FINAL; /* clear the 'final' flag in the subclass dict */
376
basedict->flags |= DICTFLAG_FINAL; /* set the 'final' flag in the baseclass dict */
377
return (PyObject *)result;
380
if (-1 == PyObject_SetAttrString((PyObject *)result, "_fields_", fields)) {
384
return (PyObject *)result;
387
387
static PyObject *
388
388
PyCStructType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
390
return StructUnionType_new(type, args, kwds, 1);
390
return StructUnionType_new(type, args, kwds, 1);
393
393
static PyObject *
394
394
UnionType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
396
return StructUnionType_new(type, args, kwds, 0);
396
return StructUnionType_new(type, args, kwds, 0);
399
399
static char from_address_doc[] =
586
586
static PyObject *
587
587
CDataType_from_param(PyObject *type, PyObject *value)
589
PyObject *as_parameter;
590
if (1 == PyObject_IsInstance(value, type)) {
594
if (PyCArg_CheckExact(value)) {
595
PyCArgObject *p = (PyCArgObject *)value;
596
PyObject *ob = p->obj;
599
dict = PyType_stgdict(type);
601
/* If we got a PyCArgObject, we must check if the object packed in it
602
is an instance of the type's dict->proto */
604
&& PyObject_IsInstance(ob, dict->proto)) {
608
ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???";
609
PyErr_Format(PyExc_TypeError,
610
"expected %s instance instead of pointer to %s",
611
((PyTypeObject *)type)->tp_name, ob_name);
615
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
617
value = CDataType_from_param(type, as_parameter);
618
Py_DECREF(as_parameter);
621
PyErr_Format(PyExc_TypeError,
622
"expected %s instance instead of %s",
623
((PyTypeObject *)type)->tp_name,
624
Py_TYPE(value)->tp_name);
589
PyObject *as_parameter;
590
if (1 == PyObject_IsInstance(value, type)) {
594
if (PyCArg_CheckExact(value)) {
595
PyCArgObject *p = (PyCArgObject *)value;
596
PyObject *ob = p->obj;
599
dict = PyType_stgdict(type);
601
/* If we got a PyCArgObject, we must check if the object packed in it
602
is an instance of the type's dict->proto */
604
&& PyObject_IsInstance(ob, dict->proto)) {
608
ob_name = (ob) ? Py_TYPE(ob)->tp_name : "???";
609
PyErr_Format(PyExc_TypeError,
610
"expected %s instance instead of pointer to %s",
611
((PyTypeObject *)type)->tp_name, ob_name);
615
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
617
value = CDataType_from_param(type, as_parameter);
618
Py_DECREF(as_parameter);
621
PyErr_Format(PyExc_TypeError,
622
"expected %s instance instead of %s",
623
((PyTypeObject *)type)->tp_name,
624
Py_TYPE(value)->tp_name);
628
628
static PyMethodDef CDataType_methods[] = {
629
{ "from_param", CDataType_from_param, METH_O, from_param_doc },
630
{ "from_address", CDataType_from_address, METH_O, from_address_doc },
631
{ "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
632
{ "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
633
{ "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc },
629
{ "from_param", CDataType_from_param, METH_O, from_param_doc },
630
{ "from_address", CDataType_from_address, METH_O, from_address_doc },
631
{ "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
632
{ "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
633
{ "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc },
637
637
static PyObject *
638
638
CDataType_repeat(PyObject *self, Py_ssize_t length)
641
return PyErr_Format(PyExc_ValueError,
642
"Array length must be >= 0, not %zd",
644
return PyCArrayType_from_ctype(self, length);
641
return PyErr_Format(PyExc_ValueError,
642
"Array length must be >= 0, not %zd",
644
return PyCArrayType_from_ctype(self, length);
647
647
static PySequenceMethods CDataType_as_sequence = {
648
0, /* inquiry sq_length; */
649
0, /* binaryfunc sq_concat; */
650
CDataType_repeat, /* intargfunc sq_repeat; */
651
0, /* intargfunc sq_item; */
652
0, /* intintargfunc sq_slice; */
653
0, /* intobjargproc sq_ass_item; */
654
0, /* intintobjargproc sq_ass_slice; */
655
0, /* objobjproc sq_contains; */
657
0, /* binaryfunc sq_inplace_concat; */
658
0, /* intargfunc sq_inplace_repeat; */
648
0, /* inquiry sq_length; */
649
0, /* binaryfunc sq_concat; */
650
CDataType_repeat, /* intargfunc sq_repeat; */
651
0, /* intargfunc sq_item; */
652
0, /* intintargfunc sq_slice; */
653
0, /* intobjargproc sq_ass_item; */
654
0, /* intintobjargproc sq_ass_slice; */
655
0, /* objobjproc sq_contains; */
657
0, /* binaryfunc sq_inplace_concat; */
658
0, /* intargfunc sq_inplace_repeat; */
662
662
CDataType_clear(PyTypeObject *self)
664
StgDictObject *dict = PyType_stgdict((PyObject *)self);
666
Py_CLEAR(dict->proto);
667
return PyType_Type.tp_clear((PyObject *)self);
664
StgDictObject *dict = PyType_stgdict((PyObject *)self);
666
Py_CLEAR(dict->proto);
667
return PyType_Type.tp_clear((PyObject *)self);
671
671
CDataType_traverse(PyTypeObject *self, visitproc visit, void *arg)
673
StgDictObject *dict = PyType_stgdict((PyObject *)self);
675
Py_VISIT(dict->proto);
676
return PyType_Type.tp_traverse((PyObject *)self, visit, arg);
673
StgDictObject *dict = PyType_stgdict((PyObject *)self);
675
Py_VISIT(dict->proto);
676
return PyType_Type.tp_traverse((PyObject *)self, visit, arg);
680
680
PyCStructType_setattro(PyObject *self, PyObject *key, PyObject *value)
682
/* XXX Should we disallow deleting _fields_? */
683
if (-1 == PyType_Type.tp_setattro(self, key, value))
686
if (value && PyUnicode_Check(key) &&
687
/* XXX struni _PyUnicode_AsString can fail (also in other places)! */
688
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
689
return PyCStructUnionType_update_stgdict(self, value, 1);
682
/* XXX Should we disallow deleting _fields_? */
683
if (-1 == PyType_Type.tp_setattro(self, key, value))
686
if (value && PyUnicode_Check(key) &&
687
/* XXX struni _PyUnicode_AsString can fail (also in other places)! */
688
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
689
return PyCStructUnionType_update_stgdict(self, value, 1);
695
695
UnionType_setattro(PyObject *self, PyObject *key, PyObject *value)
697
/* XXX Should we disallow deleting _fields_? */
698
if (-1 == PyObject_GenericSetAttr(self, key, value))
701
if (PyUnicode_Check(key) &&
702
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
703
return PyCStructUnionType_update_stgdict(self, value, 0);
697
/* XXX Should we disallow deleting _fields_? */
698
if (-1 == PyObject_GenericSetAttr(self, key, value))
701
if (PyUnicode_Check(key) &&
702
0 == strcmp(_PyUnicode_AsString(key), "_fields_"))
703
return PyCStructUnionType_update_stgdict(self, value, 0);
708
708
PyTypeObject PyCStructType_Type = {
709
PyVarObject_HEAD_INIT(NULL, 0)
710
"_ctypes.PyCStructType", /* tp_name */
711
0, /* tp_basicsize */
719
0, /* tp_as_number */
720
&CDataType_as_sequence, /* tp_as_sequence */
721
0, /* tp_as_mapping */
726
PyCStructType_setattro, /* tp_setattro */
727
0, /* tp_as_buffer */
728
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
729
"metatype for the CData Objects", /* tp_doc */
730
(traverseproc)CDataType_traverse, /* tp_traverse */
731
(inquiry)CDataType_clear, /* tp_clear */
732
0, /* tp_richcompare */
733
0, /* tp_weaklistoffset */
736
CDataType_methods, /* tp_methods */
741
0, /* tp_descr_get */
742
0, /* tp_descr_set */
743
0, /* tp_dictoffset */
746
PyCStructType_new, /* tp_new */
709
PyVarObject_HEAD_INIT(NULL, 0)
710
"_ctypes.PyCStructType", /* tp_name */
711
0, /* tp_basicsize */
719
0, /* tp_as_number */
720
&CDataType_as_sequence, /* tp_as_sequence */
721
0, /* tp_as_mapping */
726
PyCStructType_setattro, /* tp_setattro */
727
0, /* tp_as_buffer */
728
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
729
"metatype for the CData Objects", /* tp_doc */
730
(traverseproc)CDataType_traverse, /* tp_traverse */
731
(inquiry)CDataType_clear, /* tp_clear */
732
0, /* tp_richcompare */
733
0, /* tp_weaklistoffset */
736
CDataType_methods, /* tp_methods */
741
0, /* tp_descr_get */
742
0, /* tp_descr_set */
743
0, /* tp_dictoffset */
746
PyCStructType_new, /* tp_new */
750
750
static PyTypeObject UnionType_Type = {
751
PyVarObject_HEAD_INIT(NULL, 0)
752
"_ctypes.UnionType", /* tp_name */
753
0, /* tp_basicsize */
761
0, /* tp_as_number */
762
&CDataType_as_sequence, /* tp_as_sequence */
763
0, /* tp_as_mapping */
768
UnionType_setattro, /* tp_setattro */
769
0, /* tp_as_buffer */
770
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
771
"metatype for the CData Objects", /* tp_doc */
772
(traverseproc)CDataType_traverse, /* tp_traverse */
773
(inquiry)CDataType_clear, /* tp_clear */
774
0, /* tp_richcompare */
775
0, /* tp_weaklistoffset */
778
CDataType_methods, /* tp_methods */
783
0, /* tp_descr_get */
784
0, /* tp_descr_set */
785
0, /* tp_dictoffset */
788
UnionType_new, /* tp_new */
751
PyVarObject_HEAD_INIT(NULL, 0)
752
"_ctypes.UnionType", /* tp_name */
753
0, /* tp_basicsize */
761
0, /* tp_as_number */
762
&CDataType_as_sequence, /* tp_as_sequence */
763
0, /* tp_as_mapping */
768
UnionType_setattro, /* tp_setattro */
769
0, /* tp_as_buffer */
770
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
771
"metatype for the CData Objects", /* tp_doc */
772
(traverseproc)CDataType_traverse, /* tp_traverse */
773
(inquiry)CDataType_clear, /* tp_clear */
774
0, /* tp_richcompare */
775
0, /* tp_weaklistoffset */
778
CDataType_methods, /* tp_methods */
783
0, /* tp_descr_get */
784
0, /* tp_descr_set */
785
0, /* tp_dictoffset */
788
UnionType_new, /* tp_new */
793
793
/******************************************************************/
810
810
PyCPointerType_SetProto(StgDictObject *stgdict, PyObject *proto)
812
if (!proto || !PyType_Check(proto)) {
813
PyErr_SetString(PyExc_TypeError,
814
"_type_ must be a type");
817
if (!PyType_stgdict(proto)) {
818
PyErr_SetString(PyExc_TypeError,
819
"_type_ must have storage info");
823
Py_XDECREF(stgdict->proto);
824
stgdict->proto = proto;
812
if (!proto || !PyType_Check(proto)) {
813
PyErr_SetString(PyExc_TypeError,
814
"_type_ must be a type");
817
if (!PyType_stgdict(proto)) {
818
PyErr_SetString(PyExc_TypeError,
819
"_type_ must have storage info");
823
Py_XDECREF(stgdict->proto);
824
stgdict->proto = proto;
828
828
static PyCArgObject *
829
829
PyCPointerType_paramfunc(CDataObject *self)
833
parg = PyCArgObject_new();
838
parg->pffi_type = &ffi_type_pointer;
840
parg->obj = (PyObject *)self;
841
parg->value.p = *(void **)self->b_ptr;
833
parg = PyCArgObject_new();
838
parg->pffi_type = &ffi_type_pointer;
840
parg->obj = (PyObject *)self;
841
parg->value.p = *(void **)self->b_ptr;
845
845
static PyObject *
846
846
PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
848
PyTypeObject *result;
849
StgDictObject *stgdict;
848
PyTypeObject *result;
849
StgDictObject *stgdict;
853
typedict = PyTuple_GetItem(args, 2);
853
typedict = PyTuple_GetItem(args, 2);
857
857
stgdict items size, align, length contain info about pointers itself,
858
858
stgdict->proto has info about the pointed to type!
860
stgdict = (StgDictObject *)PyObject_CallObject(
861
(PyObject *)&PyCStgDict_Type, NULL);
864
stgdict->size = sizeof(void *);
865
stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
867
stgdict->ffi_type_pointer = ffi_type_pointer;
868
stgdict->paramfunc = PyCPointerType_paramfunc;
869
stgdict->flags |= TYPEFLAG_ISPOINTER;
871
proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
872
if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) {
873
Py_DECREF((PyObject *)stgdict);
878
StgDictObject *itemdict = PyType_stgdict(proto);
880
/* If itemdict->format is NULL, then this is a pointer to an
881
incomplete type. We create a generic format string
882
'pointer to bytes' in this case. XXX Better would be to
883
fix the format string later...
885
stgdict->format = _ctypes_alloc_format_string("&",
886
itemdict->format ? itemdict->format : "B");
887
if (stgdict->format == NULL) {
888
Py_DECREF((PyObject *)stgdict);
893
/* create the new instance (which is a class,
894
since we are a metatype!) */
895
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
896
if (result == NULL) {
897
Py_DECREF((PyObject *)stgdict);
901
/* replace the class dict by our updated spam dict */
902
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
904
Py_DECREF((PyObject *)stgdict);
907
Py_DECREF(result->tp_dict);
908
result->tp_dict = (PyObject *)stgdict;
910
return (PyObject *)result;
860
stgdict = (StgDictObject *)PyObject_CallObject(
861
(PyObject *)&PyCStgDict_Type, NULL);
864
stgdict->size = sizeof(void *);
865
stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
867
stgdict->ffi_type_pointer = ffi_type_pointer;
868
stgdict->paramfunc = PyCPointerType_paramfunc;
869
stgdict->flags |= TYPEFLAG_ISPOINTER;
871
proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
872
if (proto && -1 == PyCPointerType_SetProto(stgdict, proto)) {
873
Py_DECREF((PyObject *)stgdict);
878
StgDictObject *itemdict = PyType_stgdict(proto);
880
/* If itemdict->format is NULL, then this is a pointer to an
881
incomplete type. We create a generic format string
882
'pointer to bytes' in this case. XXX Better would be to
883
fix the format string later...
885
stgdict->format = _ctypes_alloc_format_string("&",
886
itemdict->format ? itemdict->format : "B");
887
if (stgdict->format == NULL) {
888
Py_DECREF((PyObject *)stgdict);
893
/* create the new instance (which is a class,
894
since we are a metatype!) */
895
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
896
if (result == NULL) {
897
Py_DECREF((PyObject *)stgdict);
901
/* replace the class dict by our updated spam dict */
902
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
904
Py_DECREF((PyObject *)stgdict);
907
Py_DECREF(result->tp_dict);
908
result->tp_dict = (PyObject *)stgdict;
910
return (PyObject *)result;
914
914
static PyObject *
915
915
PyCPointerType_set_type(PyTypeObject *self, PyObject *type)
919
dict = PyType_stgdict((PyObject *)self);
922
if (-1 == PyCPointerType_SetProto(dict, type))
925
if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
919
dict = PyType_stgdict((PyObject *)self);
922
if (-1 == PyCPointerType_SetProto(dict, type))
925
if (-1 == PyDict_SetItemString((PyObject *)dict, "_type_", type))
932
932
static PyObject *_byref(PyObject *);
934
934
static PyObject *
935
935
PyCPointerType_from_param(PyObject *type, PyObject *value)
937
StgDictObject *typedict;
939
if (value == Py_None) {
940
/* ConvParam will convert to a NULL pointer later */
945
typedict = PyType_stgdict(type);
946
assert(typedict); /* Cannot be NULL for pointer types */
948
/* If we expect POINTER(<type>), but receive a <type> instance, accept
949
it by calling byref(<type>).
951
switch (PyObject_IsInstance(value, typedict->proto)) {
953
Py_INCREF(value); /* _byref steals a refcount */
954
return _byref(value);
962
if (PointerObject_Check(value) || ArrayObject_Check(value)) {
963
/* Array instances are also pointers when
964
the item types are the same.
966
StgDictObject *v = PyObject_stgdict(value);
967
assert(v); /* Cannot be NULL for pointer or array objects */
968
if (PyObject_IsSubclass(v->proto, typedict->proto)) {
973
return CDataType_from_param(type, value);
937
StgDictObject *typedict;
939
if (value == Py_None) {
940
/* ConvParam will convert to a NULL pointer later */
945
typedict = PyType_stgdict(type);
946
assert(typedict); /* Cannot be NULL for pointer types */
948
/* If we expect POINTER(<type>), but receive a <type> instance, accept
949
it by calling byref(<type>).
951
switch (PyObject_IsInstance(value, typedict->proto)) {
953
Py_INCREF(value); /* _byref steals a refcount */
954
return _byref(value);
962
if (PointerObject_Check(value) || ArrayObject_Check(value)) {
963
/* Array instances are also pointers when
964
the item types are the same.
966
StgDictObject *v = PyObject_stgdict(value);
967
assert(v); /* Cannot be NULL for pointer or array objects */
968
if (PyObject_IsSubclass(v->proto, typedict->proto)) {
973
return CDataType_from_param(type, value);
976
976
static PyMethodDef PyCPointerType_methods[] = {
977
{ "from_address", CDataType_from_address, METH_O, from_address_doc },
978
{ "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
979
{ "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
980
{ "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
981
{ "from_param", (PyCFunction)PyCPointerType_from_param, METH_O, from_param_doc},
982
{ "set_type", (PyCFunction)PyCPointerType_set_type, METH_O },
977
{ "from_address", CDataType_from_address, METH_O, from_address_doc },
978
{ "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
979
{ "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
980
{ "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
981
{ "from_param", (PyCFunction)PyCPointerType_from_param, METH_O, from_param_doc},
982
{ "set_type", (PyCFunction)PyCPointerType_set_type, METH_O },
986
986
PyTypeObject PyCPointerType_Type = {
987
PyVarObject_HEAD_INIT(NULL, 0)
988
"_ctypes.PyCPointerType", /* tp_name */
989
0, /* tp_basicsize */
997
0, /* tp_as_number */
998
&CDataType_as_sequence, /* tp_as_sequence */
999
0, /* tp_as_mapping */
1003
0, /* tp_getattro */
1004
0, /* tp_setattro */
1005
0, /* tp_as_buffer */
1006
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1007
"metatype for the Pointer Objects", /* tp_doc */
1008
(traverseproc)CDataType_traverse, /* tp_traverse */
1009
(inquiry)CDataType_clear, /* tp_clear */
1010
0, /* tp_richcompare */
1011
0, /* tp_weaklistoffset */
1013
0, /* tp_iternext */
1014
PyCPointerType_methods, /* tp_methods */
1019
0, /* tp_descr_get */
1020
0, /* tp_descr_set */
1021
0, /* tp_dictoffset */
1024
PyCPointerType_new, /* tp_new */
987
PyVarObject_HEAD_INIT(NULL, 0)
988
"_ctypes.PyCPointerType", /* tp_name */
989
0, /* tp_basicsize */
997
0, /* tp_as_number */
998
&CDataType_as_sequence, /* tp_as_sequence */
999
0, /* tp_as_mapping */
1003
0, /* tp_getattro */
1004
0, /* tp_setattro */
1005
0, /* tp_as_buffer */
1006
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1007
"metatype for the Pointer Objects", /* tp_doc */
1008
(traverseproc)CDataType_traverse, /* tp_traverse */
1009
(inquiry)CDataType_clear, /* tp_clear */
1010
0, /* tp_richcompare */
1011
0, /* tp_weaklistoffset */
1013
0, /* tp_iternext */
1014
PyCPointerType_methods, /* tp_methods */
1019
0, /* tp_descr_get */
1020
0, /* tp_descr_set */
1021
0, /* tp_dictoffset */
1024
PyCPointerType_new, /* tp_new */
1029
1029
/******************************************************************/
1031
1031
PyCArrayType_Type
1039
1039
CharArray_set_raw(CDataObject *self, PyObject *value)
1045
if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
1049
if (size > self->b_size) {
1050
PyErr_SetString(PyExc_ValueError,
1055
memcpy(self->b_ptr, ptr, size);
1057
PyBuffer_Release(&view);
1045
if (PyObject_GetBuffer(value, &view, PyBUF_SIMPLE) < 0)
1049
if (size > self->b_size) {
1050
PyErr_SetString(PyExc_ValueError,
1055
memcpy(self->b_ptr, ptr, size);
1057
PyBuffer_Release(&view);
1060
PyBuffer_Release(&view);
1060
PyBuffer_Release(&view);
1064
1064
static PyObject *
1065
1065
CharArray_get_raw(CDataObject *self)
1067
return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
1067
return PyBytes_FromStringAndSize(self->b_ptr, self->b_size);
1070
1070
static PyObject *
1071
1071
CharArray_get_value(CDataObject *self)
1074
char *ptr = self->b_ptr;
1075
for (i = 0; i < self->b_size; ++i)
1078
return PyBytes_FromStringAndSize(self->b_ptr, i);
1074
char *ptr = self->b_ptr;
1075
for (i = 0; i < self->b_size; ++i)
1078
return PyBytes_FromStringAndSize(self->b_ptr, i);
1082
1082
CharArray_set_value(CDataObject *self, PyObject *value)
1087
if (value == NULL) {
1088
PyErr_SetString(PyExc_TypeError,
1089
"can't delete attribute");
1093
if (PyUnicode_Check(value)) {
1094
value = PyUnicode_AsEncodedString(value,
1095
_ctypes_conversion_encoding,
1096
_ctypes_conversion_errors);
1099
} else if (!PyBytes_Check(value)) {
1100
PyErr_Format(PyExc_TypeError,
1101
"str/bytes expected instead of %s instance",
1102
Py_TYPE(value)->tp_name);
1106
size = PyBytes_GET_SIZE(value);
1107
if (size > self->b_size) {
1108
PyErr_SetString(PyExc_ValueError,
1114
ptr = PyBytes_AS_STRING(value);
1115
memcpy(self->b_ptr, ptr, size);
1116
if (size < self->b_size)
1117
self->b_ptr[size] = '\0';
1087
if (value == NULL) {
1088
PyErr_SetString(PyExc_TypeError,
1089
"can't delete attribute");
1093
if (PyUnicode_Check(value)) {
1094
value = PyUnicode_AsEncodedString(value,
1095
_ctypes_conversion_encoding,
1096
_ctypes_conversion_errors);
1099
} else if (!PyBytes_Check(value)) {
1100
PyErr_Format(PyExc_TypeError,
1101
"str/bytes expected instead of %s instance",
1102
Py_TYPE(value)->tp_name);
1106
size = PyBytes_GET_SIZE(value);
1107
if (size > self->b_size) {
1108
PyErr_SetString(PyExc_ValueError,
1114
ptr = PyBytes_AS_STRING(value);
1115
memcpy(self->b_ptr, ptr, size);
1116
if (size < self->b_size)
1117
self->b_ptr[size] = '\0';
1123
1123
static PyGetSetDef CharArray_getsets[] = {
1124
{ "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw,
1126
{ "value", (getter)CharArray_get_value, (setter)CharArray_set_value,
1124
{ "raw", (getter)CharArray_get_raw, (setter)CharArray_set_raw,
1126
{ "value", (getter)CharArray_get_value, (setter)CharArray_set_value,
1131
1131
#ifdef CTYPES_UNICODE
1132
1132
static PyObject *
1133
1133
WCharArray_get_value(CDataObject *self)
1136
wchar_t *ptr = (wchar_t *)self->b_ptr;
1137
for (i = 0; i < self->b_size/sizeof(wchar_t); ++i)
1138
if (*ptr++ == (wchar_t)0)
1140
return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i);
1136
wchar_t *ptr = (wchar_t *)self->b_ptr;
1137
for (i = 0; i < self->b_size/sizeof(wchar_t); ++i)
1138
if (*ptr++ == (wchar_t)0)
1140
return PyUnicode_FromWideChar((wchar_t *)self->b_ptr, i);
1144
1144
WCharArray_set_value(CDataObject *self, PyObject *value)
1146
Py_ssize_t result = 0;
1146
Py_ssize_t result = 0;
1148
if (value == NULL) {
1149
PyErr_SetString(PyExc_TypeError,
1150
"can't delete attribute");
1153
if (PyBytes_Check(value)) {
1154
value = PyUnicode_FromEncodedObject(value,
1155
_ctypes_conversion_encoding,
1156
_ctypes_conversion_errors);
1159
} else if (!PyUnicode_Check(value)) {
1160
PyErr_Format(PyExc_TypeError,
1161
"unicode string expected instead of %s instance",
1162
Py_TYPE(value)->tp_name);
1166
if ((unsigned)PyUnicode_GET_SIZE(value) > self->b_size/sizeof(wchar_t)) {
1167
PyErr_SetString(PyExc_ValueError,
1172
result = PyUnicode_AsWideChar((PyUnicodeObject *)value,
1173
(wchar_t *)self->b_ptr,
1174
self->b_size/sizeof(wchar_t));
1175
if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t))
1176
((wchar_t *)self->b_ptr)[result] = (wchar_t)0;
1148
if (value == NULL) {
1149
PyErr_SetString(PyExc_TypeError,
1150
"can't delete attribute");
1153
if (PyBytes_Check(value)) {
1154
value = PyUnicode_FromEncodedObject(value,
1155
_ctypes_conversion_encoding,
1156
_ctypes_conversion_errors);
1159
} else if (!PyUnicode_Check(value)) {
1160
PyErr_Format(PyExc_TypeError,
1161
"unicode string expected instead of %s instance",
1162
Py_TYPE(value)->tp_name);
1166
if ((unsigned)PyUnicode_GET_SIZE(value) > self->b_size/sizeof(wchar_t)) {
1167
PyErr_SetString(PyExc_ValueError,
1172
result = PyUnicode_AsWideChar((PyUnicodeObject *)value,
1173
(wchar_t *)self->b_ptr,
1174
self->b_size/sizeof(wchar_t));
1175
if (result >= 0 && (size_t)result < self->b_size/sizeof(wchar_t))
1176
((wchar_t *)self->b_ptr)[result] = (wchar_t)0;
1180
return result >= 0 ? 0 : -1;
1180
return result >= 0 ? 0 : -1;
1183
1183
static PyGetSetDef WCharArray_getsets[] = {
1184
{ "value", (getter)WCharArray_get_value, (setter)WCharArray_set_value,
1184
{ "value", (getter)WCharArray_get_value, (setter)WCharArray_set_value,
1199
1199
add_methods(PyTypeObject *type, PyMethodDef *meth)
1201
PyObject *dict = type->tp_dict;
1202
for (; meth->ml_name != NULL; meth++) {
1204
descr = PyDescr_NewMethod(type, meth);
1207
if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0)
1201
PyObject *dict = type->tp_dict;
1202
for (; meth->ml_name != NULL; meth++) {
1204
descr = PyDescr_NewMethod(type, meth);
1207
if (PyDict_SetItemString(dict,meth->ml_name, descr) < 0)
1215
1215
add_members(PyTypeObject *type, PyMemberDef *memb)
1217
PyObject *dict = type->tp_dict;
1218
for (; memb->name != NULL; memb++) {
1220
descr = PyDescr_NewMember(type, memb);
1223
if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1217
PyObject *dict = type->tp_dict;
1218
for (; memb->name != NULL; memb++) {
1220
descr = PyDescr_NewMember(type, memb);
1223
if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1232
1232
add_getset(PyTypeObject *type, PyGetSetDef *gsp)
1234
PyObject *dict = type->tp_dict;
1235
for (; gsp->name != NULL; gsp++) {
1237
descr = PyDescr_NewGetSet(type, gsp);
1240
if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1234
PyObject *dict = type->tp_dict;
1235
for (; gsp->name != NULL; gsp++) {
1237
descr = PyDescr_NewGetSet(type, gsp);
1240
if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1247
1247
static PyCArgObject *
1248
1248
PyCArrayType_paramfunc(CDataObject *self)
1250
PyCArgObject *p = PyCArgObject_new();
1254
p->pffi_type = &ffi_type_pointer;
1255
p->value.p = (char *)self->b_ptr;
1257
p->obj = (PyObject *)self;
1250
PyCArgObject *p = PyCArgObject_new();
1254
p->pffi_type = &ffi_type_pointer;
1255
p->value.p = (char *)self->b_ptr;
1257
p->obj = (PyObject *)self;
1261
1261
static PyObject *
1262
1262
PyCArrayType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1264
PyTypeObject *result;
1265
StgDictObject *stgdict;
1266
StgDictObject *itemdict;
1271
Py_ssize_t itemsize, itemalign;
1274
typedict = PyTuple_GetItem(args, 2);
1278
proto = PyDict_GetItemString(typedict, "_length_"); /* Borrowed ref */
1279
if (!proto || !PyLong_Check(proto)) {
1280
PyErr_SetString(PyExc_AttributeError,
1281
"class must define a '_length_' attribute, "
1282
"which must be a positive integer");
1285
length = PyLong_AsLongAndOverflow(proto, &overflow);
1287
PyErr_SetString(PyExc_OverflowError,
1288
"The '_length_' attribute is too large");
1292
proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
1294
PyErr_SetString(PyExc_AttributeError,
1295
"class must define a '_type_' attribute");
1299
stgdict = (StgDictObject *)PyObject_CallObject(
1300
(PyObject *)&PyCStgDict_Type, NULL);
1304
itemdict = PyType_stgdict(proto);
1306
PyErr_SetString(PyExc_TypeError,
1307
"_type_ must have storage info");
1308
Py_DECREF((PyObject *)stgdict);
1312
assert(itemdict->format);
1313
if (itemdict->format[0] == '(') {
1314
sprintf(buf, "(%ld,", length);
1315
stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format+1);
1317
sprintf(buf, "(%ld)", length);
1318
stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format);
1320
if (stgdict->format == NULL) {
1321
Py_DECREF((PyObject *)stgdict);
1324
stgdict->ndim = itemdict->ndim + 1;
1325
stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim);
1326
if (stgdict->shape == NULL) {
1327
Py_DECREF((PyObject *)stgdict);
1330
stgdict->shape[0] = length;
1331
memmove(&stgdict->shape[1], itemdict->shape,
1332
sizeof(Py_ssize_t) * (stgdict->ndim - 1));
1334
itemsize = itemdict->size;
1335
if (length * itemsize < 0) {
1336
PyErr_SetString(PyExc_OverflowError,
1341
itemalign = itemdict->align;
1343
if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
1344
stgdict->flags |= TYPEFLAG_HASPOINTER;
1346
stgdict->size = itemsize * length;
1347
stgdict->align = itemalign;
1348
stgdict->length = length;
1350
stgdict->proto = proto;
1352
stgdict->paramfunc = &PyCArrayType_paramfunc;
1354
/* Arrays are passed as pointers to function calls. */
1355
stgdict->ffi_type_pointer = ffi_type_pointer;
1357
/* create the new instance (which is a class,
1358
since we are a metatype!) */
1359
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1363
/* replace the class dict by our updated spam dict */
1364
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
1366
Py_DECREF((PyObject *)stgdict);
1369
Py_DECREF(result->tp_dict);
1370
result->tp_dict = (PyObject *)stgdict;
1372
/* Special case for character arrays.
1373
A permanent annoyance: char arrays are also strings!
1375
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
1376
if (-1 == add_getset(result, CharArray_getsets))
1264
PyTypeObject *result;
1265
StgDictObject *stgdict;
1266
StgDictObject *itemdict;
1271
Py_ssize_t itemsize, itemalign;
1274
typedict = PyTuple_GetItem(args, 2);
1278
proto = PyDict_GetItemString(typedict, "_length_"); /* Borrowed ref */
1279
if (!proto || !PyLong_Check(proto)) {
1280
PyErr_SetString(PyExc_AttributeError,
1281
"class must define a '_length_' attribute, "
1282
"which must be a positive integer");
1285
length = PyLong_AsLongAndOverflow(proto, &overflow);
1287
PyErr_SetString(PyExc_OverflowError,
1288
"The '_length_' attribute is too large");
1292
proto = PyDict_GetItemString(typedict, "_type_"); /* Borrowed ref */
1294
PyErr_SetString(PyExc_AttributeError,
1295
"class must define a '_type_' attribute");
1299
stgdict = (StgDictObject *)PyObject_CallObject(
1300
(PyObject *)&PyCStgDict_Type, NULL);
1304
itemdict = PyType_stgdict(proto);
1306
PyErr_SetString(PyExc_TypeError,
1307
"_type_ must have storage info");
1308
Py_DECREF((PyObject *)stgdict);
1312
assert(itemdict->format);
1313
if (itemdict->format[0] == '(') {
1314
sprintf(buf, "(%ld,", length);
1315
stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format+1);
1317
sprintf(buf, "(%ld)", length);
1318
stgdict->format = _ctypes_alloc_format_string(buf, itemdict->format);
1320
if (stgdict->format == NULL) {
1321
Py_DECREF((PyObject *)stgdict);
1324
stgdict->ndim = itemdict->ndim + 1;
1325
stgdict->shape = PyMem_Malloc(sizeof(Py_ssize_t *) * stgdict->ndim);
1326
if (stgdict->shape == NULL) {
1327
Py_DECREF((PyObject *)stgdict);
1330
stgdict->shape[0] = length;
1331
memmove(&stgdict->shape[1], itemdict->shape,
1332
sizeof(Py_ssize_t) * (stgdict->ndim - 1));
1334
itemsize = itemdict->size;
1335
if (length * itemsize < 0) {
1336
PyErr_SetString(PyExc_OverflowError,
1341
itemalign = itemdict->align;
1343
if (itemdict->flags & (TYPEFLAG_ISPOINTER | TYPEFLAG_HASPOINTER))
1344
stgdict->flags |= TYPEFLAG_HASPOINTER;
1346
stgdict->size = itemsize * length;
1347
stgdict->align = itemalign;
1348
stgdict->length = length;
1350
stgdict->proto = proto;
1352
stgdict->paramfunc = &PyCArrayType_paramfunc;
1354
/* Arrays are passed as pointers to function calls. */
1355
stgdict->ffi_type_pointer = ffi_type_pointer;
1357
/* create the new instance (which is a class,
1358
since we are a metatype!) */
1359
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1363
/* replace the class dict by our updated spam dict */
1364
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
1366
Py_DECREF((PyObject *)stgdict);
1369
Py_DECREF(result->tp_dict);
1370
result->tp_dict = (PyObject *)stgdict;
1372
/* Special case for character arrays.
1373
A permanent annoyance: char arrays are also strings!
1375
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
1376
if (-1 == add_getset(result, CharArray_getsets))
1378
1378
#ifdef CTYPES_UNICODE
1379
} else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
1380
if (-1 == add_getset(result, WCharArray_getsets))
1379
} else if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
1380
if (-1 == add_getset(result, WCharArray_getsets))
1385
return (PyObject *)result;
1385
return (PyObject *)result;
1388
1388
PyTypeObject PyCArrayType_Type = {
1389
PyVarObject_HEAD_INIT(NULL, 0)
1390
"_ctypes.PyCArrayType", /* tp_name */
1391
0, /* tp_basicsize */
1392
0, /* tp_itemsize */
1397
0, /* tp_reserved */
1399
0, /* tp_as_number */
1400
&CDataType_as_sequence, /* tp_as_sequence */
1401
0, /* tp_as_mapping */
1405
0, /* tp_getattro */
1406
0, /* tp_setattro */
1407
0, /* tp_as_buffer */
1408
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1409
"metatype for the Array Objects", /* tp_doc */
1410
0, /* tp_traverse */
1412
0, /* tp_richcompare */
1413
0, /* tp_weaklistoffset */
1415
0, /* tp_iternext */
1416
CDataType_methods, /* tp_methods */
1421
0, /* tp_descr_get */
1422
0, /* tp_descr_set */
1423
0, /* tp_dictoffset */
1426
PyCArrayType_new, /* tp_new */
1389
PyVarObject_HEAD_INIT(NULL, 0)
1390
"_ctypes.PyCArrayType", /* tp_name */
1391
0, /* tp_basicsize */
1392
0, /* tp_itemsize */
1397
0, /* tp_reserved */
1399
0, /* tp_as_number */
1400
&CDataType_as_sequence, /* tp_as_sequence */
1401
0, /* tp_as_mapping */
1405
0, /* tp_getattro */
1406
0, /* tp_setattro */
1407
0, /* tp_as_buffer */
1408
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1409
"metatype for the Array Objects", /* tp_doc */
1410
0, /* tp_traverse */
1412
0, /* tp_richcompare */
1413
0, /* tp_weaklistoffset */
1415
0, /* tp_iternext */
1416
CDataType_methods, /* tp_methods */
1421
0, /* tp_descr_get */
1422
0, /* tp_descr_set */
1423
0, /* tp_dictoffset */
1426
PyCArrayType_new, /* tp_new */
1431
1431
/******************************************************************/
1433
1433
PyCSimpleType_Type
1444
1444
static PyObject *
1445
1445
c_wchar_p_from_param(PyObject *type, PyObject *value)
1447
PyObject *as_parameter;
1448
if (value == Py_None) {
1452
if (PyUnicode_Check(value) || PyBytes_Check(value)) {
1454
struct fielddesc *fd = _ctypes_get_fielddesc("Z");
1456
parg = PyCArgObject_new();
1459
parg->pffi_type = &ffi_type_pointer;
1461
parg->obj = fd->setfunc(&parg->value, value, 0);
1462
if (parg->obj == NULL) {
1466
return (PyObject *)parg;
1468
if (PyObject_IsInstance(value, type)) {
1472
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
1473
/* c_wchar array instance or pointer(c_wchar(...)) */
1474
StgDictObject *dt = PyObject_stgdict(value);
1475
StgDictObject *dict;
1476
assert(dt); /* Cannot be NULL for pointer or array objects */
1477
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
1478
if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
1483
if (PyCArg_CheckExact(value)) {
1484
/* byref(c_char(...)) */
1485
PyCArgObject *a = (PyCArgObject *)value;
1486
StgDictObject *dict = PyObject_stgdict(a->obj);
1487
if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
1493
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1495
value = c_wchar_p_from_param(type, as_parameter);
1496
Py_DECREF(as_parameter);
1499
/* XXX better message */
1500
PyErr_SetString(PyExc_TypeError,
1447
PyObject *as_parameter;
1448
if (value == Py_None) {
1452
if (PyUnicode_Check(value) || PyBytes_Check(value)) {
1454
struct fielddesc *fd = _ctypes_get_fielddesc("Z");
1456
parg = PyCArgObject_new();
1459
parg->pffi_type = &ffi_type_pointer;
1461
parg->obj = fd->setfunc(&parg->value, value, 0);
1462
if (parg->obj == NULL) {
1466
return (PyObject *)parg;
1468
if (PyObject_IsInstance(value, type)) {
1472
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
1473
/* c_wchar array instance or pointer(c_wchar(...)) */
1474
StgDictObject *dt = PyObject_stgdict(value);
1475
StgDictObject *dict;
1476
assert(dt); /* Cannot be NULL for pointer or array objects */
1477
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
1478
if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
1483
if (PyCArg_CheckExact(value)) {
1484
/* byref(c_char(...)) */
1485
PyCArgObject *a = (PyCArgObject *)value;
1486
StgDictObject *dict = PyObject_stgdict(a->obj);
1487
if (dict && (dict->setfunc == _ctypes_get_fielddesc("u")->setfunc)) {
1493
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1495
value = c_wchar_p_from_param(type, as_parameter);
1496
Py_DECREF(as_parameter);
1499
/* XXX better message */
1500
PyErr_SetString(PyExc_TypeError,
1505
1505
static PyObject *
1506
1506
c_char_p_from_param(PyObject *type, PyObject *value)
1508
PyObject *as_parameter;
1509
if (value == Py_None) {
1513
if (PyBytes_Check(value) || PyUnicode_Check(value)) {
1515
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1517
parg = PyCArgObject_new();
1520
parg->pffi_type = &ffi_type_pointer;
1522
parg->obj = fd->setfunc(&parg->value, value, 0);
1523
if (parg->obj == NULL) {
1527
return (PyObject *)parg;
1529
if (PyObject_IsInstance(value, type)) {
1533
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
1534
/* c_char array instance or pointer(c_char(...)) */
1535
StgDictObject *dt = PyObject_stgdict(value);
1536
StgDictObject *dict;
1537
assert(dt); /* Cannot be NULL for pointer or array objects */
1538
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
1539
if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
1544
if (PyCArg_CheckExact(value)) {
1545
/* byref(c_char(...)) */
1546
PyCArgObject *a = (PyCArgObject *)value;
1547
StgDictObject *dict = PyObject_stgdict(a->obj);
1548
if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
1554
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1556
value = c_char_p_from_param(type, as_parameter);
1557
Py_DECREF(as_parameter);
1560
/* XXX better message */
1561
PyErr_SetString(PyExc_TypeError,
1508
PyObject *as_parameter;
1509
if (value == Py_None) {
1513
if (PyBytes_Check(value) || PyUnicode_Check(value)) {
1515
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1517
parg = PyCArgObject_new();
1520
parg->pffi_type = &ffi_type_pointer;
1522
parg->obj = fd->setfunc(&parg->value, value, 0);
1523
if (parg->obj == NULL) {
1527
return (PyObject *)parg;
1529
if (PyObject_IsInstance(value, type)) {
1533
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
1534
/* c_char array instance or pointer(c_char(...)) */
1535
StgDictObject *dt = PyObject_stgdict(value);
1536
StgDictObject *dict;
1537
assert(dt); /* Cannot be NULL for pointer or array objects */
1538
dict = dt && dt->proto ? PyType_stgdict(dt->proto) : NULL;
1539
if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
1544
if (PyCArg_CheckExact(value)) {
1545
/* byref(c_char(...)) */
1546
PyCArgObject *a = (PyCArgObject *)value;
1547
StgDictObject *dict = PyObject_stgdict(a->obj);
1548
if (dict && (dict->setfunc == _ctypes_get_fielddesc("c")->setfunc)) {
1554
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1556
value = c_char_p_from_param(type, as_parameter);
1557
Py_DECREF(as_parameter);
1560
/* XXX better message */
1561
PyErr_SetString(PyExc_TypeError,
1566
1566
static PyObject *
1567
1567
c_void_p_from_param(PyObject *type, PyObject *value)
1569
StgDictObject *stgd;
1570
PyObject *as_parameter;
1569
StgDictObject *stgd;
1570
PyObject *as_parameter;
1573
if (value == Py_None) {
1577
/* Should probably allow buffer interface as well */
1573
if (value == Py_None) {
1577
/* Should probably allow buffer interface as well */
1578
1578
/* int, long */
1579
if (PyLong_Check(value)) {
1581
struct fielddesc *fd = _ctypes_get_fielddesc("P");
1579
if (PyLong_Check(value)) {
1581
struct fielddesc *fd = _ctypes_get_fielddesc("P");
1583
parg = PyCArgObject_new();
1586
parg->pffi_type = &ffi_type_pointer;
1588
parg->obj = fd->setfunc(&parg->value, value, 0);
1589
if (parg->obj == NULL) {
1593
return (PyObject *)parg;
1595
/* XXX struni: remove later */
1583
parg = PyCArgObject_new();
1586
parg->pffi_type = &ffi_type_pointer;
1588
parg->obj = fd->setfunc(&parg->value, value, 0);
1589
if (parg->obj == NULL) {
1593
return (PyObject *)parg;
1595
/* XXX struni: remove later */
1597
if (PyBytes_Check(value)) {
1599
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1597
if (PyBytes_Check(value)) {
1599
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1601
parg = PyCArgObject_new();
1604
parg->pffi_type = &ffi_type_pointer;
1606
parg->obj = fd->setfunc(&parg->value, value, 0);
1607
if (parg->obj == NULL) {
1611
return (PyObject *)parg;
1601
parg = PyCArgObject_new();
1604
parg->pffi_type = &ffi_type_pointer;
1606
parg->obj = fd->setfunc(&parg->value, value, 0);
1607
if (parg->obj == NULL) {
1611
return (PyObject *)parg;
1614
if (PyByteArray_Check(value)) {
1616
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1614
if (PyByteArray_Check(value)) {
1616
struct fielddesc *fd = _ctypes_get_fielddesc("z");
1618
parg = PyCArgObject_new();
1621
parg->pffi_type = &ffi_type_pointer;
1623
parg->obj = fd->setfunc(&parg->value, value, 0);
1624
if (parg->obj == NULL) {
1628
return (PyObject *)parg;
1618
parg = PyCArgObject_new();
1621
parg->pffi_type = &ffi_type_pointer;
1623
parg->obj = fd->setfunc(&parg->value, value, 0);
1624
if (parg->obj == NULL) {
1628
return (PyObject *)parg;
1631
if (PyUnicode_Check(value)) {
1633
struct fielddesc *fd = _ctypes_get_fielddesc("Z");
1631
if (PyUnicode_Check(value)) {
1633
struct fielddesc *fd = _ctypes_get_fielddesc("Z");
1635
parg = PyCArgObject_new();
1638
parg->pffi_type = &ffi_type_pointer;
1640
parg->obj = fd->setfunc(&parg->value, value, 0);
1641
if (parg->obj == NULL) {
1645
return (PyObject *)parg;
1635
parg = PyCArgObject_new();
1638
parg->pffi_type = &ffi_type_pointer;
1640
parg->obj = fd->setfunc(&parg->value, value, 0);
1641
if (parg->obj == NULL) {
1645
return (PyObject *)parg;
1647
1647
/* c_void_p instance (or subclass) */
1648
if (PyObject_IsInstance(value, type)) {
1649
/* c_void_p instances */
1648
if (PyObject_IsInstance(value, type)) {
1649
/* c_void_p instances */
1653
1653
/* ctypes array or pointer instance */
1654
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
1655
/* Any array or pointer is accepted */
1654
if (ArrayObject_Check(value) || PointerObject_Check(value)) {
1655
/* Any array or pointer is accepted */
1659
1659
/* byref(...) */
1660
if (PyCArg_CheckExact(value)) {
1661
/* byref(c_xxx()) */
1662
PyCArgObject *a = (PyCArgObject *)value;
1663
if (a->tag == 'P') {
1660
if (PyCArg_CheckExact(value)) {
1661
/* byref(c_xxx()) */
1662
PyCArgObject *a = (PyCArgObject *)value;
1663
if (a->tag == 'P') {
1668
1668
/* function pointer */
1669
if (PyCFuncPtrObject_Check(value)) {
1671
PyCFuncPtrObject *func;
1672
func = (PyCFuncPtrObject *)value;
1673
parg = PyCArgObject_new();
1676
parg->pffi_type = &ffi_type_pointer;
1679
parg->value.p = *(void **)func->b_ptr;
1681
return (PyObject *)parg;
1669
if (PyCFuncPtrObject_Check(value)) {
1671
PyCFuncPtrObject *func;
1672
func = (PyCFuncPtrObject *)value;
1673
parg = PyCArgObject_new();
1676
parg->pffi_type = &ffi_type_pointer;
1679
parg->value.p = *(void **)func->b_ptr;
1681
return (PyObject *)parg;
1683
1683
/* c_char_p, c_wchar_p */
1684
stgd = PyObject_stgdict(value);
1685
if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd->proto)) {
1688
switch (_PyUnicode_AsString(stgd->proto)[0]) {
1689
case 'z': /* c_char_p */
1690
case 'Z': /* c_wchar_p */
1691
parg = PyCArgObject_new();
1694
parg->pffi_type = &ffi_type_pointer;
1698
/* Remember: b_ptr points to where the pointer is stored! */
1699
parg->value.p = *(void **)(((CDataObject *)value)->b_ptr);
1700
return (PyObject *)parg;
1704
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1706
value = c_void_p_from_param(type, as_parameter);
1707
Py_DECREF(as_parameter);
1710
/* XXX better message */
1711
PyErr_SetString(PyExc_TypeError,
1684
stgd = PyObject_stgdict(value);
1685
if (stgd && CDataObject_Check(value) && stgd->proto && PyUnicode_Check(stgd->proto)) {
1688
switch (_PyUnicode_AsString(stgd->proto)[0]) {
1689
case 'z': /* c_char_p */
1690
case 'Z': /* c_wchar_p */
1691
parg = PyCArgObject_new();
1694
parg->pffi_type = &ffi_type_pointer;
1698
/* Remember: b_ptr points to where the pointer is stored! */
1699
parg->value.p = *(void **)(((CDataObject *)value)->b_ptr);
1700
return (PyObject *)parg;
1704
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
1706
value = c_void_p_from_param(type, as_parameter);
1707
Py_DECREF(as_parameter);
1710
/* XXX better message */
1711
PyErr_SetString(PyExc_TypeError,
1716
1716
static PyMethodDef c_void_p_method = { "from_param", c_void_p_from_param, METH_O };
1718
1718
static PyMethodDef c_wchar_p_method = { "from_param", c_wchar_p_from_param, METH_O };
1720
1720
static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject *kwds,
1721
PyObject *proto, struct fielddesc *fmt)
1721
PyObject *proto, struct fielddesc *fmt)
1723
PyTypeObject *result;
1724
StgDictObject *stgdict;
1725
PyObject *name = PyTuple_GET_ITEM(args, 0);
1727
PyObject *swapped_args;
1728
static PyObject *suffix;
1731
swapped_args = PyTuple_New(PyTuple_GET_SIZE(args));
1723
PyTypeObject *result;
1724
StgDictObject *stgdict;
1725
PyObject *name = PyTuple_GET_ITEM(args, 0);
1727
PyObject *swapped_args;
1728
static PyObject *suffix;
1731
swapped_args = PyTuple_New(PyTuple_GET_SIZE(args));
1736
1736
#ifdef WORDS_BIGENDIAN
1737
suffix = PyUnicode_InternFromString("_le");
1737
suffix = PyUnicode_InternFromString("_le");
1739
suffix = PyUnicode_InternFromString("_be");
1739
suffix = PyUnicode_InternFromString("_be");
1742
newname = PyUnicode_Concat(name, suffix);
1743
if (newname == NULL) {
1747
PyTuple_SET_ITEM(swapped_args, 0, newname);
1748
for (i=1; i<PyTuple_GET_SIZE(args); ++i) {
1749
PyObject *v = PyTuple_GET_ITEM(args, i);
1751
PyTuple_SET_ITEM(swapped_args, i, v);
1754
/* create the new instance (which is a class,
1755
since we are a metatype!) */
1756
result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds);
1757
Py_DECREF(swapped_args);
1761
stgdict = (StgDictObject *)PyObject_CallObject(
1762
(PyObject *)&PyCStgDict_Type, NULL);
1763
if (!stgdict) /* XXX leaks result! */
1766
stgdict->ffi_type_pointer = *fmt->pffi_type;
1767
stgdict->align = fmt->pffi_type->alignment;
1768
stgdict->length = 0;
1769
stgdict->size = fmt->pffi_type->size;
1770
stgdict->setfunc = fmt->setfunc_swapped;
1771
stgdict->getfunc = fmt->getfunc_swapped;
1774
stgdict->proto = proto;
1776
/* replace the class dict by our updated spam dict */
1777
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
1779
Py_DECREF((PyObject *)stgdict);
1782
Py_DECREF(result->tp_dict);
1783
result->tp_dict = (PyObject *)stgdict;
1785
return (PyObject *)result;
1742
newname = PyUnicode_Concat(name, suffix);
1743
if (newname == NULL) {
1747
PyTuple_SET_ITEM(swapped_args, 0, newname);
1748
for (i=1; i<PyTuple_GET_SIZE(args); ++i) {
1749
PyObject *v = PyTuple_GET_ITEM(args, i);
1751
PyTuple_SET_ITEM(swapped_args, i, v);
1754
/* create the new instance (which is a class,
1755
since we are a metatype!) */
1756
result = (PyTypeObject *)PyType_Type.tp_new(type, swapped_args, kwds);
1757
Py_DECREF(swapped_args);
1761
stgdict = (StgDictObject *)PyObject_CallObject(
1762
(PyObject *)&PyCStgDict_Type, NULL);
1763
if (!stgdict) /* XXX leaks result! */
1766
stgdict->ffi_type_pointer = *fmt->pffi_type;
1767
stgdict->align = fmt->pffi_type->alignment;
1768
stgdict->length = 0;
1769
stgdict->size = fmt->pffi_type->size;
1770
stgdict->setfunc = fmt->setfunc_swapped;
1771
stgdict->getfunc = fmt->getfunc_swapped;
1774
stgdict->proto = proto;
1776
/* replace the class dict by our updated spam dict */
1777
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
1779
Py_DECREF((PyObject *)stgdict);
1782
Py_DECREF(result->tp_dict);
1783
result->tp_dict = (PyObject *)stgdict;
1785
return (PyObject *)result;
1788
1788
static PyCArgObject *
1789
1789
PyCSimpleType_paramfunc(CDataObject *self)
1791
StgDictObject *dict;
1794
struct fielddesc *fd;
1796
dict = PyObject_stgdict((PyObject *)self);
1797
assert(dict); /* Cannot be NULL for CDataObject instances */
1798
fmt = _PyUnicode_AsString(dict->proto);
1801
fd = _ctypes_get_fielddesc(fmt);
1804
parg = PyCArgObject_new();
1809
parg->pffi_type = fd->pffi_type;
1811
parg->obj = (PyObject *)self;
1812
memcpy(&parg->value, self->b_ptr, self->b_size);
1791
StgDictObject *dict;
1794
struct fielddesc *fd;
1796
dict = PyObject_stgdict((PyObject *)self);
1797
assert(dict); /* Cannot be NULL for CDataObject instances */
1798
fmt = _PyUnicode_AsString(dict->proto);
1801
fd = _ctypes_get_fielddesc(fmt);
1804
parg = PyCArgObject_new();
1809
parg->pffi_type = fd->pffi_type;
1811
parg->obj = (PyObject *)self;
1812
memcpy(&parg->value, self->b_ptr, self->b_size);
1816
1816
static PyObject *
1817
1817
PyCSimpleType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1819
PyTypeObject *result;
1820
StgDictObject *stgdict;
1822
const char *proto_str;
1823
Py_ssize_t proto_len;
1825
struct fielddesc *fmt;
1827
/* create the new instance (which is a class,
1828
since we are a metatype!) */
1829
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1833
proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
1835
PyErr_SetString(PyExc_AttributeError,
1836
"class must define a '_type_' attribute");
1819
PyTypeObject *result;
1820
StgDictObject *stgdict;
1822
const char *proto_str;
1823
Py_ssize_t proto_len;
1825
struct fielddesc *fmt;
1827
/* create the new instance (which is a class,
1828
since we are a metatype!) */
1829
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
1833
proto = PyObject_GetAttrString((PyObject *)result, "_type_"); /* new ref */
1835
PyErr_SetString(PyExc_AttributeError,
1836
"class must define a '_type_' attribute");
1842
if (PyUnicode_Check(proto)) {
1843
PyObject *v = _PyUnicode_AsDefaultEncodedString(proto, NULL);
1846
proto_str = PyBytes_AS_STRING(v);
1847
proto_len = PyBytes_GET_SIZE(v);
1849
PyErr_SetString(PyExc_TypeError,
1850
"class must define a '_type_' string attribute");
1853
if (proto_len != 1) {
1854
PyErr_SetString(PyExc_ValueError,
1855
"class must define a '_type_' attribute "
1856
"which must be a string of length 1");
1859
if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) {
1860
PyErr_Format(PyExc_AttributeError,
1861
"class must define a '_type_' attribute which must be\n"
1862
"a single character string containing one of '%s'.",
1866
fmt = _ctypes_get_fielddesc(proto_str);
1868
PyErr_Format(PyExc_ValueError,
1869
"_type_ '%s' not supported", proto_str);
1873
stgdict = (StgDictObject *)PyObject_CallObject(
1874
(PyObject *)&PyCStgDict_Type, NULL);
1878
stgdict->ffi_type_pointer = *fmt->pffi_type;
1879
stgdict->align = fmt->pffi_type->alignment;
1880
stgdict->length = 0;
1881
stgdict->size = fmt->pffi_type->size;
1882
stgdict->setfunc = fmt->setfunc;
1883
stgdict->getfunc = fmt->getfunc;
1842
if (PyUnicode_Check(proto)) {
1843
PyObject *v = _PyUnicode_AsDefaultEncodedString(proto, NULL);
1846
proto_str = PyBytes_AS_STRING(v);
1847
proto_len = PyBytes_GET_SIZE(v);
1849
PyErr_SetString(PyExc_TypeError,
1850
"class must define a '_type_' string attribute");
1853
if (proto_len != 1) {
1854
PyErr_SetString(PyExc_ValueError,
1855
"class must define a '_type_' attribute "
1856
"which must be a string of length 1");
1859
if (!strchr(SIMPLE_TYPE_CHARS, *proto_str)) {
1860
PyErr_Format(PyExc_AttributeError,
1861
"class must define a '_type_' attribute which must be\n"
1862
"a single character string containing one of '%s'.",
1866
fmt = _ctypes_get_fielddesc(proto_str);
1868
PyErr_Format(PyExc_ValueError,
1869
"_type_ '%s' not supported", proto_str);
1873
stgdict = (StgDictObject *)PyObject_CallObject(
1874
(PyObject *)&PyCStgDict_Type, NULL);
1878
stgdict->ffi_type_pointer = *fmt->pffi_type;
1879
stgdict->align = fmt->pffi_type->alignment;
1880
stgdict->length = 0;
1881
stgdict->size = fmt->pffi_type->size;
1882
stgdict->setfunc = fmt->setfunc;
1883
stgdict->getfunc = fmt->getfunc;
1884
1884
#ifdef WORDS_BIGENDIAN
1885
stgdict->format = _ctypes_alloc_format_string(">", proto_str);
1885
stgdict->format = _ctypes_alloc_format_string(">", proto_str);
1887
stgdict->format = _ctypes_alloc_format_string("<", proto_str);
1887
stgdict->format = _ctypes_alloc_format_string("<", proto_str);
1889
if (stgdict->format == NULL) {
1892
Py_DECREF((PyObject *)stgdict);
1889
if (stgdict->format == NULL) {
1892
Py_DECREF((PyObject *)stgdict);
1896
stgdict->paramfunc = PyCSimpleType_paramfunc;
1896
stgdict->paramfunc = PyCSimpleType_paramfunc;
1898
if (result->tp_base != &Simple_Type) {
1899
stgdict->setfunc = NULL;
1900
stgdict->getfunc = NULL;
1898
if (result->tp_base != &Simple_Type) {
1899
stgdict->setfunc = NULL;
1900
stgdict->getfunc = NULL;
1904
/* This consumes the refcount on proto which we have */
1905
stgdict->proto = proto;
1907
/* replace the class dict by our updated spam dict */
1908
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
1910
Py_DECREF((PyObject *)stgdict);
1913
Py_DECREF(result->tp_dict);
1914
result->tp_dict = (PyObject *)stgdict;
1916
/* Install from_param class methods in ctypes base classes.
1917
Overrides the PyCSimpleType_from_param generic method.
1919
if (result->tp_base == &Simple_Type) {
1920
switch (*proto_str) {
1921
case 'z': /* c_char_p */
1922
ml = &c_char_p_method;
1923
stgdict->flags |= TYPEFLAG_ISPOINTER;
1925
case 'Z': /* c_wchar_p */
1926
ml = &c_wchar_p_method;
1927
stgdict->flags |= TYPEFLAG_ISPOINTER;
1929
case 'P': /* c_void_p */
1930
ml = &c_void_p_method;
1931
stgdict->flags |= TYPEFLAG_ISPOINTER;
1937
stgdict->flags |= TYPEFLAG_ISPOINTER;
1947
meth = PyDescr_NewClassMethod(result, ml);
1950
x = PyDict_SetItemString(result->tp_dict,
1961
if (type == &PyCSimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) {
1962
PyObject *swapped = CreateSwappedType(type, args, kwds,
1964
StgDictObject *sw_dict;
1965
if (swapped == NULL) {
1969
sw_dict = PyType_stgdict(swapped);
1904
/* This consumes the refcount on proto which we have */
1905
stgdict->proto = proto;
1907
/* replace the class dict by our updated spam dict */
1908
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
1910
Py_DECREF((PyObject *)stgdict);
1913
Py_DECREF(result->tp_dict);
1914
result->tp_dict = (PyObject *)stgdict;
1916
/* Install from_param class methods in ctypes base classes.
1917
Overrides the PyCSimpleType_from_param generic method.
1919
if (result->tp_base == &Simple_Type) {
1920
switch (*proto_str) {
1921
case 'z': /* c_char_p */
1922
ml = &c_char_p_method;
1923
stgdict->flags |= TYPEFLAG_ISPOINTER;
1925
case 'Z': /* c_wchar_p */
1926
ml = &c_wchar_p_method;
1927
stgdict->flags |= TYPEFLAG_ISPOINTER;
1929
case 'P': /* c_void_p */
1930
ml = &c_void_p_method;
1931
stgdict->flags |= TYPEFLAG_ISPOINTER;
1937
stgdict->flags |= TYPEFLAG_ISPOINTER;
1947
meth = PyDescr_NewClassMethod(result, ml);
1950
x = PyDict_SetItemString(result->tp_dict,
1961
if (type == &PyCSimpleType_Type && fmt->setfunc_swapped && fmt->getfunc_swapped) {
1962
PyObject *swapped = CreateSwappedType(type, args, kwds,
1964
StgDictObject *sw_dict;
1965
if (swapped == NULL) {
1969
sw_dict = PyType_stgdict(swapped);
1970
1970
#ifdef WORDS_BIGENDIAN
1971
PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped);
1972
PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result);
1973
PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result);
1974
PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
1975
/* We are creating the type for the OTHER endian */
1976
sw_dict->format = _ctypes_alloc_format_string("<", stgdict->format+1);
1971
PyObject_SetAttrString((PyObject *)result, "__ctype_le__", swapped);
1972
PyObject_SetAttrString((PyObject *)result, "__ctype_be__", (PyObject *)result);
1973
PyObject_SetAttrString(swapped, "__ctype_be__", (PyObject *)result);
1974
PyObject_SetAttrString(swapped, "__ctype_le__", swapped);
1975
/* We are creating the type for the OTHER endian */
1976
sw_dict->format = _ctypes_alloc_format_string("<", stgdict->format+1);
1978
PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped);
1979
PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result);
1980
PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result);
1981
PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
1982
/* We are creating the type for the OTHER endian */
1983
sw_dict->format = _ctypes_alloc_format_string(">", stgdict->format+1);
1978
PyObject_SetAttrString((PyObject *)result, "__ctype_be__", swapped);
1979
PyObject_SetAttrString((PyObject *)result, "__ctype_le__", (PyObject *)result);
1980
PyObject_SetAttrString(swapped, "__ctype_le__", (PyObject *)result);
1981
PyObject_SetAttrString(swapped, "__ctype_be__", swapped);
1982
/* We are creating the type for the OTHER endian */
1983
sw_dict->format = _ctypes_alloc_format_string(">", stgdict->format+1);
1986
if (PyErr_Occurred()) {
1986
if (PyErr_Occurred()) {
1992
return (PyObject *)result;
1992
return (PyObject *)result;
1999
1999
static PyObject *
2000
2000
PyCSimpleType_from_param(PyObject *type, PyObject *value)
2002
StgDictObject *dict;
2005
struct fielddesc *fd;
2006
PyObject *as_parameter;
2008
/* If the value is already an instance of the requested type,
2009
we can use it as is */
2010
if (1 == PyObject_IsInstance(value, type)) {
2015
dict = PyType_stgdict(type);
2018
/* I think we can rely on this being a one-character string */
2019
fmt = _PyUnicode_AsString(dict->proto);
2022
fd = _ctypes_get_fielddesc(fmt);
2025
parg = PyCArgObject_new();
2030
parg->pffi_type = fd->pffi_type;
2031
parg->obj = fd->setfunc(&parg->value, value, 0);
2033
return (PyObject *)parg;
2037
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
2039
value = PyCSimpleType_from_param(type, as_parameter);
2040
Py_DECREF(as_parameter);
2043
PyErr_SetString(PyExc_TypeError,
2002
StgDictObject *dict;
2005
struct fielddesc *fd;
2006
PyObject *as_parameter;
2008
/* If the value is already an instance of the requested type,
2009
we can use it as is */
2010
if (1 == PyObject_IsInstance(value, type)) {
2015
dict = PyType_stgdict(type);
2018
/* I think we can rely on this being a one-character string */
2019
fmt = _PyUnicode_AsString(dict->proto);
2022
fd = _ctypes_get_fielddesc(fmt);
2025
parg = PyCArgObject_new();
2030
parg->pffi_type = fd->pffi_type;
2031
parg->obj = fd->setfunc(&parg->value, value, 0);
2033
return (PyObject *)parg;
2037
as_parameter = PyObject_GetAttrString(value, "_as_parameter_");
2039
value = PyCSimpleType_from_param(type, as_parameter);
2040
Py_DECREF(as_parameter);
2043
PyErr_SetString(PyExc_TypeError,
2048
2048
static PyMethodDef PyCSimpleType_methods[] = {
2049
{ "from_param", PyCSimpleType_from_param, METH_O, from_param_doc },
2050
{ "from_address", CDataType_from_address, METH_O, from_address_doc },
2051
{ "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
2052
{ "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
2053
{ "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
2049
{ "from_param", PyCSimpleType_from_param, METH_O, from_param_doc },
2050
{ "from_address", CDataType_from_address, METH_O, from_address_doc },
2051
{ "from_buffer", CDataType_from_buffer, METH_VARARGS, from_buffer_doc, },
2052
{ "from_buffer_copy", CDataType_from_buffer_copy, METH_VARARGS, from_buffer_copy_doc, },
2053
{ "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc},
2057
2057
PyTypeObject PyCSimpleType_Type = {
2058
PyVarObject_HEAD_INIT(NULL, 0)
2059
"_ctypes.PyCSimpleType", /* tp_name */
2060
0, /* tp_basicsize */
2061
0, /* tp_itemsize */
2066
0, /* tp_reserved */
2068
0, /* tp_as_number */
2069
&CDataType_as_sequence, /* tp_as_sequence */
2070
0, /* tp_as_mapping */
2074
0, /* tp_getattro */
2075
0, /* tp_setattro */
2076
0, /* tp_as_buffer */
2077
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2078
"metatype for the PyCSimpleType Objects", /* tp_doc */
2079
0, /* tp_traverse */
2081
0, /* tp_richcompare */
2082
0, /* tp_weaklistoffset */
2084
0, /* tp_iternext */
2085
PyCSimpleType_methods, /* tp_methods */
2090
0, /* tp_descr_get */
2091
0, /* tp_descr_set */
2092
0, /* tp_dictoffset */
2095
PyCSimpleType_new, /* tp_new */
2058
PyVarObject_HEAD_INIT(NULL, 0)
2059
"_ctypes.PyCSimpleType", /* tp_name */
2060
0, /* tp_basicsize */
2061
0, /* tp_itemsize */
2066
0, /* tp_reserved */
2068
0, /* tp_as_number */
2069
&CDataType_as_sequence, /* tp_as_sequence */
2070
0, /* tp_as_mapping */
2074
0, /* tp_getattro */
2075
0, /* tp_setattro */
2076
0, /* tp_as_buffer */
2077
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2078
"metatype for the PyCSimpleType Objects", /* tp_doc */
2079
0, /* tp_traverse */
2081
0, /* tp_richcompare */
2082
0, /* tp_weaklistoffset */
2084
0, /* tp_iternext */
2085
PyCSimpleType_methods, /* tp_methods */
2090
0, /* tp_descr_get */
2091
0, /* tp_descr_set */
2092
0, /* tp_dictoffset */
2095
PyCSimpleType_new, /* tp_new */
2099
2099
/******************************************************************/
2104
2104
static PyObject *
2105
2105
converters_from_argtypes(PyObject *ob)
2107
PyObject *converters;
2111
ob = PySequence_Tuple(ob); /* new reference */
2113
PyErr_SetString(PyExc_TypeError,
2114
"_argtypes_ must be a sequence of types");
2118
nArgs = PyTuple_GET_SIZE(ob);
2119
converters = PyTuple_New(nArgs);
2123
/* I have to check if this is correct. Using c_char, which has a size
2124
of 1, will be assumed to be pushed as only one byte!
2125
Aren't these promoted to integers by the C compiler and pushed as 4 bytes?
2128
for (i = 0; i < nArgs; ++i) {
2129
PyObject *tp = PyTuple_GET_ITEM(ob, i);
2130
PyObject *cnv = PyObject_GetAttrString(tp, "from_param");
2132
goto argtypes_error_1;
2133
PyTuple_SET_ITEM(converters, i, cnv);
2107
PyObject *converters;
2111
ob = PySequence_Tuple(ob); /* new reference */
2113
PyErr_SetString(PyExc_TypeError,
2114
"_argtypes_ must be a sequence of types");
2118
nArgs = PyTuple_GET_SIZE(ob);
2119
converters = PyTuple_New(nArgs);
2123
/* I have to check if this is correct. Using c_char, which has a size
2124
of 1, will be assumed to be pushed as only one byte!
2125
Aren't these promoted to integers by the C compiler and pushed as 4 bytes?
2128
for (i = 0; i < nArgs; ++i) {
2129
PyObject *tp = PyTuple_GET_ITEM(ob, i);
2130
PyObject *cnv = PyObject_GetAttrString(tp, "from_param");
2132
goto argtypes_error_1;
2133
PyTuple_SET_ITEM(converters, i, cnv);
2138
2138
argtypes_error_1:
2139
Py_XDECREF(converters);
2141
PyErr_Format(PyExc_TypeError,
2142
"item %zd in _argtypes_ has no from_param method",
2139
Py_XDECREF(converters);
2141
PyErr_Format(PyExc_TypeError,
2142
"item %zd in _argtypes_ has no from_param method",
2148
2148
make_funcptrtype_dict(StgDictObject *stgdict)
2151
PyObject *converters = NULL;
2153
stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
2154
stgdict->length = 1;
2155
stgdict->size = sizeof(void *);
2156
stgdict->setfunc = NULL;
2157
stgdict->getfunc = NULL;
2158
stgdict->ffi_type_pointer = ffi_type_pointer;
2160
ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_");
2161
if (!ob || !PyLong_Check(ob)) {
2162
PyErr_SetString(PyExc_TypeError,
2163
"class must define _flags_ which must be an integer");
2166
stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER;
2168
/* _argtypes_ is optional... */
2169
ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_");
2171
converters = converters_from_argtypes(ob);
2175
stgdict->argtypes = ob;
2176
stgdict->converters = converters;
2179
ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_");
2181
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
2182
PyErr_SetString(PyExc_TypeError,
2183
"_restype_ must be a type, a callable, or None");
2187
stgdict->restype = ob;
2188
stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_");
2189
if (stgdict->checker == NULL)
2151
PyObject *converters = NULL;
2153
stgdict->align = _ctypes_get_fielddesc("P")->pffi_type->alignment;
2154
stgdict->length = 1;
2155
stgdict->size = sizeof(void *);
2156
stgdict->setfunc = NULL;
2157
stgdict->getfunc = NULL;
2158
stgdict->ffi_type_pointer = ffi_type_pointer;
2160
ob = PyDict_GetItemString((PyObject *)stgdict, "_flags_");
2161
if (!ob || !PyLong_Check(ob)) {
2162
PyErr_SetString(PyExc_TypeError,
2163
"class must define _flags_ which must be an integer");
2166
stgdict->flags = PyLong_AS_LONG(ob) | TYPEFLAG_ISPOINTER;
2168
/* _argtypes_ is optional... */
2169
ob = PyDict_GetItemString((PyObject *)stgdict, "_argtypes_");
2171
converters = converters_from_argtypes(ob);
2175
stgdict->argtypes = ob;
2176
stgdict->converters = converters;
2179
ob = PyDict_GetItemString((PyObject *)stgdict, "_restype_");
2181
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
2182
PyErr_SetString(PyExc_TypeError,
2183
"_restype_ must be a type, a callable, or None");
2187
stgdict->restype = ob;
2188
stgdict->checker = PyObject_GetAttrString(ob, "_check_retval_");
2189
if (stgdict->checker == NULL)
2192
2192
/* XXX later, maybe.
2193
ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_");
2195
if (!PyCallable_Check(ob)) {
2196
PyErr_SetString(PyExc_TypeError,
2197
"_errcheck_ must be callable");
2201
stgdict->errcheck = ob;
2193
ob = PyDict_GetItemString((PyObject *)stgdict, "_errcheck_");
2195
if (!PyCallable_Check(ob)) {
2196
PyErr_SetString(PyExc_TypeError,
2197
"_errcheck_ must be callable");
2201
stgdict->errcheck = ob;
2207
Py_XDECREF(converters);
2207
Py_XDECREF(converters);
2212
2212
static PyCArgObject *
2213
2213
PyCFuncPtrType_paramfunc(CDataObject *self)
2217
parg = PyCArgObject_new();
2222
parg->pffi_type = &ffi_type_pointer;
2224
parg->obj = (PyObject *)self;
2225
parg->value.p = *(void **)self->b_ptr;
2217
parg = PyCArgObject_new();
2222
parg->pffi_type = &ffi_type_pointer;
2224
parg->obj = (PyObject *)self;
2225
parg->value.p = *(void **)self->b_ptr;
2229
2229
static PyObject *
2230
2230
PyCFuncPtrType_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2232
PyTypeObject *result;
2233
StgDictObject *stgdict;
2235
stgdict = (StgDictObject *)PyObject_CallObject(
2236
(PyObject *)&PyCStgDict_Type, NULL);
2240
stgdict->paramfunc = PyCFuncPtrType_paramfunc;
2241
/* We do NOT expose the function signature in the format string. It
2242
is impossible, generally, because the only requirement for the
2243
argtypes items is that they have a .from_param method - we do not
2244
know the types of the arguments (although, in practice, most
2245
argtypes would be a ctypes type).
2247
stgdict->format = _ctypes_alloc_format_string(NULL, "X{}");
2248
stgdict->flags |= TYPEFLAG_ISPOINTER;
2250
/* create the new instance (which is a class,
2251
since we are a metatype!) */
2252
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
2253
if (result == NULL) {
2254
Py_DECREF((PyObject *)stgdict);
2258
/* replace the class dict by our updated storage dict */
2259
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
2261
Py_DECREF((PyObject *)stgdict);
2264
Py_DECREF(result->tp_dict);
2265
result->tp_dict = (PyObject *)stgdict;
2267
if (-1 == make_funcptrtype_dict(stgdict)) {
2272
return (PyObject *)result;
2232
PyTypeObject *result;
2233
StgDictObject *stgdict;
2235
stgdict = (StgDictObject *)PyObject_CallObject(
2236
(PyObject *)&PyCStgDict_Type, NULL);
2240
stgdict->paramfunc = PyCFuncPtrType_paramfunc;
2241
/* We do NOT expose the function signature in the format string. It
2242
is impossible, generally, because the only requirement for the
2243
argtypes items is that they have a .from_param method - we do not
2244
know the types of the arguments (although, in practice, most
2245
argtypes would be a ctypes type).
2247
stgdict->format = _ctypes_alloc_format_string(NULL, "X{}");
2248
stgdict->flags |= TYPEFLAG_ISPOINTER;
2250
/* create the new instance (which is a class,
2251
since we are a metatype!) */
2252
result = (PyTypeObject *)PyType_Type.tp_new(type, args, kwds);
2253
if (result == NULL) {
2254
Py_DECREF((PyObject *)stgdict);
2258
/* replace the class dict by our updated storage dict */
2259
if (-1 == PyDict_Update((PyObject *)stgdict, result->tp_dict)) {
2261
Py_DECREF((PyObject *)stgdict);
2264
Py_DECREF(result->tp_dict);
2265
result->tp_dict = (PyObject *)stgdict;
2267
if (-1 == make_funcptrtype_dict(stgdict)) {
2272
return (PyObject *)result;
2275
2275
PyTypeObject PyCFuncPtrType_Type = {
2276
PyVarObject_HEAD_INIT(NULL, 0)
2277
"_ctypes.PyCFuncPtrType", /* tp_name */
2278
0, /* tp_basicsize */
2279
0, /* tp_itemsize */
2284
0, /* tp_reserved */
2286
0, /* tp_as_number */
2287
&CDataType_as_sequence, /* tp_as_sequence */
2288
0, /* tp_as_mapping */
2292
0, /* tp_getattro */
2293
0, /* tp_setattro */
2294
0, /* tp_as_buffer */
2295
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2296
"metatype for C function pointers", /* tp_doc */
2297
(traverseproc)CDataType_traverse, /* tp_traverse */
2298
(inquiry)CDataType_clear, /* tp_clear */
2299
0, /* tp_richcompare */
2300
0, /* tp_weaklistoffset */
2302
0, /* tp_iternext */
2303
CDataType_methods, /* tp_methods */
2308
0, /* tp_descr_get */
2309
0, /* tp_descr_set */
2310
0, /* tp_dictoffset */
2313
PyCFuncPtrType_new, /* tp_new */
2276
PyVarObject_HEAD_INIT(NULL, 0)
2277
"_ctypes.PyCFuncPtrType", /* tp_name */
2278
0, /* tp_basicsize */
2279
0, /* tp_itemsize */
2284
0, /* tp_reserved */
2286
0, /* tp_as_number */
2287
&CDataType_as_sequence, /* tp_as_sequence */
2288
0, /* tp_as_mapping */
2292
0, /* tp_getattro */
2293
0, /* tp_setattro */
2294
0, /* tp_as_buffer */
2295
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, /* tp_flags */
2296
"metatype for C function pointers", /* tp_doc */
2297
(traverseproc)CDataType_traverse, /* tp_traverse */
2298
(inquiry)CDataType_clear, /* tp_clear */
2299
0, /* tp_richcompare */
2300
0, /* tp_weaklistoffset */
2302
0, /* tp_iternext */
2303
CDataType_methods, /* tp_methods */
2308
0, /* tp_descr_get */
2309
0, /* tp_descr_set */
2310
0, /* tp_dictoffset */
2313
PyCFuncPtrType_new, /* tp_new */
2318
2318
/*****************************************************************
2319
2319
* Code to keep needed objects alive
2544
2544
static PyObject *
2545
2545
PyCData_from_outparam(PyObject *self, PyObject *args)
2551
2551
static PyMethodDef PyCData_methods[] = {
2552
{ "__ctypes_from_outparam__", PyCData_from_outparam, METH_NOARGS, },
2553
{ "__reduce__", PyCData_reduce, METH_NOARGS, },
2554
{ "__setstate__", PyCData_setstate, METH_VARARGS, },
2552
{ "__ctypes_from_outparam__", PyCData_from_outparam, METH_NOARGS, },
2553
{ "__reduce__", PyCData_reduce, METH_NOARGS, },
2554
{ "__setstate__", PyCData_setstate, METH_VARARGS, },
2558
2558
PyTypeObject PyCData_Type = {
2559
PyVarObject_HEAD_INIT(NULL, 0)
2561
sizeof(CDataObject), /* tp_basicsize */
2562
0, /* tp_itemsize */
2563
PyCData_dealloc, /* tp_dealloc */
2567
0, /* tp_reserved */
2569
0, /* tp_as_number */
2570
0, /* tp_as_sequence */
2571
0, /* tp_as_mapping */
2572
PyCData_nohash, /* tp_hash */
2575
0, /* tp_getattro */
2576
0, /* tp_setattro */
2577
&PyCData_as_buffer, /* tp_as_buffer */
2578
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2579
"XXX to be provided", /* tp_doc */
2580
(traverseproc)PyCData_traverse, /* tp_traverse */
2581
(inquiry)PyCData_clear, /* tp_clear */
2582
0, /* tp_richcompare */
2583
0, /* tp_weaklistoffset */
2585
0, /* tp_iternext */
2586
PyCData_methods, /* tp_methods */
2587
PyCData_members, /* tp_members */
2591
0, /* tp_descr_get */
2592
0, /* tp_descr_set */
2593
0, /* tp_dictoffset */
2559
PyVarObject_HEAD_INIT(NULL, 0)
2561
sizeof(CDataObject), /* tp_basicsize */
2562
0, /* tp_itemsize */
2563
PyCData_dealloc, /* tp_dealloc */
2567
0, /* tp_reserved */
2569
0, /* tp_as_number */
2570
0, /* tp_as_sequence */
2571
0, /* tp_as_mapping */
2572
PyCData_nohash, /* tp_hash */
2575
0, /* tp_getattro */
2576
0, /* tp_setattro */
2577
&PyCData_as_buffer, /* tp_as_buffer */
2578
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2579
"XXX to be provided", /* tp_doc */
2580
(traverseproc)PyCData_traverse, /* tp_traverse */
2581
(inquiry)PyCData_clear, /* tp_clear */
2582
0, /* tp_richcompare */
2583
0, /* tp_weaklistoffset */
2585
0, /* tp_iternext */
2586
PyCData_methods, /* tp_methods */
2587
PyCData_members, /* tp_members */
2591
0, /* tp_descr_get */
2592
0, /* tp_descr_set */
2593
0, /* tp_dictoffset */
2600
2600
static int PyCData_MallocBuffer(CDataObject *obj, StgDictObject *dict)
2602
if ((size_t)dict->size <= sizeof(obj->b_value)) {
2603
/* No need to call malloc, can use the default buffer */
2604
obj->b_ptr = (char *)&obj->b_value;
2605
/* The b_needsfree flag does not mean that we actually did
2606
call PyMem_Malloc to allocate the memory block; instead it
2607
means we are the *owner* of the memory and are responsible
2608
for freeing resources associated with the memory. This is
2609
also the reason that b_needsfree is exposed to Python.
2611
obj->b_needsfree = 1;
2613
/* In python 2.4, and ctypes 0.9.6, the malloc call took about
2614
33% of the creation time for c_int().
2616
obj->b_ptr = (char *)PyMem_Malloc(dict->size);
2617
if (obj->b_ptr == NULL) {
2621
obj->b_needsfree = 1;
2622
memset(obj->b_ptr, 0, dict->size);
2624
obj->b_size = dict->size;
2602
if ((size_t)dict->size <= sizeof(obj->b_value)) {
2603
/* No need to call malloc, can use the default buffer */
2604
obj->b_ptr = (char *)&obj->b_value;
2605
/* The b_needsfree flag does not mean that we actually did
2606
call PyMem_Malloc to allocate the memory block; instead it
2607
means we are the *owner* of the memory and are responsible
2608
for freeing resources associated with the memory. This is
2609
also the reason that b_needsfree is exposed to Python.
2611
obj->b_needsfree = 1;
2613
/* In python 2.4, and ctypes 0.9.6, the malloc call took about
2614
33% of the creation time for c_int().
2616
obj->b_ptr = (char *)PyMem_Malloc(dict->size);
2617
if (obj->b_ptr == NULL) {
2621
obj->b_needsfree = 1;
2622
memset(obj->b_ptr, 0, dict->size);
2624
obj->b_size = dict->size;
2629
2629
PyCData_FromBaseObj(PyObject *type, PyObject *base, Py_ssize_t index, char *adr)
2632
StgDictObject *dict;
2634
assert(PyType_Check(type));
2635
dict = PyType_stgdict(type);
2637
PyErr_SetString(PyExc_TypeError,
2641
dict->flags |= DICTFLAG_FINAL;
2642
cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
2645
assert(CDataObject_Check(cmem));
2647
cmem->b_length = dict->length;
2648
cmem->b_size = dict->size;
2649
if (base) { /* use base's buffer */
2650
assert(CDataObject_Check(base));
2652
cmem->b_needsfree = 0;
2654
cmem->b_base = (CDataObject *)base;
2655
cmem->b_index = index;
2656
} else { /* copy contents of adr */
2657
if (-1 == PyCData_MallocBuffer(cmem, dict)) {
2661
memcpy(cmem->b_ptr, adr, dict->size);
2662
cmem->b_index = index;
2664
return (PyObject *)cmem;
2632
StgDictObject *dict;
2634
assert(PyType_Check(type));
2635
dict = PyType_stgdict(type);
2637
PyErr_SetString(PyExc_TypeError,
2641
dict->flags |= DICTFLAG_FINAL;
2642
cmem = (CDataObject *)((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
2645
assert(CDataObject_Check(cmem));
2647
cmem->b_length = dict->length;
2648
cmem->b_size = dict->size;
2649
if (base) { /* use base's buffer */
2650
assert(CDataObject_Check(base));
2652
cmem->b_needsfree = 0;
2654
cmem->b_base = (CDataObject *)base;
2655
cmem->b_index = index;
2656
} else { /* copy contents of adr */
2657
if (-1 == PyCData_MallocBuffer(cmem, dict)) {
2661
memcpy(cmem->b_ptr, adr, dict->size);
2662
cmem->b_index = index;
2664
return (PyObject *)cmem;
2726
2726
static PyObject *
2727
2727
_PyCData_set(CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
2728
Py_ssize_t size, char *ptr)
2728
Py_ssize_t size, char *ptr)
2733
return setfunc(ptr, value, size);
2735
if (!CDataObject_Check(value)) {
2736
StgDictObject *dict = PyType_stgdict(type);
2737
if (dict && dict->setfunc)
2738
return dict->setfunc(ptr, value, size);
2740
If value is a tuple, we try to call the type with the tuple
2743
assert(PyType_Check(type));
2744
if (PyTuple_Check(value)) {
2747
ob = PyObject_CallObject(type, value);
2749
_ctypes_extend_error(PyExc_RuntimeError, "(%s) ",
2750
((PyTypeObject *)type)->tp_name);
2753
result = _PyCData_set(dst, type, setfunc, ob,
2757
} else if (value == Py_None && PyCPointerTypeObject_Check(type)) {
2758
*(void **)ptr = NULL;
2762
PyErr_Format(PyExc_TypeError,
2763
"expected %s instance, got %s",
2764
((PyTypeObject *)type)->tp_name,
2765
Py_TYPE(value)->tp_name);
2769
src = (CDataObject *)value;
2771
if (PyObject_IsInstance(value, type)) {
2776
if (PyCPointerTypeObject_Check(type))
2779
value = GetKeepedObjects(src);
2784
if (PyCPointerTypeObject_Check(type)
2785
&& ArrayObject_Check(value)) {
2786
StgDictObject *p1, *p2;
2788
p1 = PyObject_stgdict(value);
2789
assert(p1); /* Cannot be NULL for array instances */
2790
p2 = PyType_stgdict(type);
2791
assert(p2); /* Cannot be NULL for pointer types */
2793
if (p1->proto != p2->proto) {
2794
PyErr_Format(PyExc_TypeError,
2795
"incompatible types, %s instance instead of %s instance",
2796
Py_TYPE(value)->tp_name,
2797
((PyTypeObject *)type)->tp_name);
2800
*(void **)ptr = src->b_ptr;
2802
keep = GetKeepedObjects(src);
2804
We are assigning an array object to a field which represents
2805
a pointer. This has the same effect as converting an array
2806
into a pointer. So, again, we have to keep the whole object
2807
pointed to (which is the array in this case) alive, and not
2808
only it's object list. So we create a tuple, containing
2809
b_objects list PLUS the array itself, and return that!
2811
return PyTuple_Pack(2, keep, value);
2813
PyErr_Format(PyExc_TypeError,
2814
"incompatible types, %s instance instead of %s instance",
2815
Py_TYPE(value)->tp_name,
2816
((PyTypeObject *)type)->tp_name);
2733
return setfunc(ptr, value, size);
2735
if (!CDataObject_Check(value)) {
2736
StgDictObject *dict = PyType_stgdict(type);
2737
if (dict && dict->setfunc)
2738
return dict->setfunc(ptr, value, size);
2740
If value is a tuple, we try to call the type with the tuple
2743
assert(PyType_Check(type));
2744
if (PyTuple_Check(value)) {
2747
ob = PyObject_CallObject(type, value);
2749
_ctypes_extend_error(PyExc_RuntimeError, "(%s) ",
2750
((PyTypeObject *)type)->tp_name);
2753
result = _PyCData_set(dst, type, setfunc, ob,
2757
} else if (value == Py_None && PyCPointerTypeObject_Check(type)) {
2758
*(void **)ptr = NULL;
2762
PyErr_Format(PyExc_TypeError,
2763
"expected %s instance, got %s",
2764
((PyTypeObject *)type)->tp_name,
2765
Py_TYPE(value)->tp_name);
2769
src = (CDataObject *)value;
2771
if (PyObject_IsInstance(value, type)) {
2776
if (PyCPointerTypeObject_Check(type))
2779
value = GetKeepedObjects(src);
2784
if (PyCPointerTypeObject_Check(type)
2785
&& ArrayObject_Check(value)) {
2786
StgDictObject *p1, *p2;
2788
p1 = PyObject_stgdict(value);
2789
assert(p1); /* Cannot be NULL for array instances */
2790
p2 = PyType_stgdict(type);
2791
assert(p2); /* Cannot be NULL for pointer types */
2793
if (p1->proto != p2->proto) {
2794
PyErr_Format(PyExc_TypeError,
2795
"incompatible types, %s instance instead of %s instance",
2796
Py_TYPE(value)->tp_name,
2797
((PyTypeObject *)type)->tp_name);
2800
*(void **)ptr = src->b_ptr;
2802
keep = GetKeepedObjects(src);
2804
We are assigning an array object to a field which represents
2805
a pointer. This has the same effect as converting an array
2806
into a pointer. So, again, we have to keep the whole object
2807
pointed to (which is the array in this case) alive, and not
2808
only it's object list. So we create a tuple, containing
2809
b_objects list PLUS the array itself, and return that!
2811
return PyTuple_Pack(2, keep, value);
2813
PyErr_Format(PyExc_TypeError,
2814
"incompatible types, %s instance instead of %s instance",
2815
Py_TYPE(value)->tp_name,
2816
((PyTypeObject *)type)->tp_name);
2885
2885
PyCFuncPtr_set_errcheck(PyCFuncPtrObject *self, PyObject *ob)
2887
if (ob && !PyCallable_Check(ob)) {
2888
PyErr_SetString(PyExc_TypeError,
2889
"the errcheck attribute must be callable");
2892
Py_XDECREF(self->errcheck);
2894
self->errcheck = ob;
2887
if (ob && !PyCallable_Check(ob)) {
2888
PyErr_SetString(PyExc_TypeError,
2889
"the errcheck attribute must be callable");
2892
Py_XDECREF(self->errcheck);
2894
self->errcheck = ob;
2898
2898
static PyObject *
2899
2899
PyCFuncPtr_get_errcheck(PyCFuncPtrObject *self)
2901
if (self->errcheck) {
2902
Py_INCREF(self->errcheck);
2903
return self->errcheck;
2901
if (self->errcheck) {
2902
Py_INCREF(self->errcheck);
2903
return self->errcheck;
2910
2910
PyCFuncPtr_set_restype(PyCFuncPtrObject *self, PyObject *ob)
2913
Py_XDECREF(self->restype);
2914
self->restype = NULL;
2915
Py_XDECREF(self->checker);
2916
self->checker = NULL;
2919
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
2920
PyErr_SetString(PyExc_TypeError,
2921
"restype must be a type, a callable, or None");
2924
Py_XDECREF(self->checker);
2925
Py_XDECREF(self->restype);
2928
self->checker = PyObject_GetAttrString(ob, "_check_retval_");
2929
if (self->checker == NULL)
2913
Py_XDECREF(self->restype);
2914
self->restype = NULL;
2915
Py_XDECREF(self->checker);
2916
self->checker = NULL;
2919
if (ob != Py_None && !PyType_stgdict(ob) && !PyCallable_Check(ob)) {
2920
PyErr_SetString(PyExc_TypeError,
2921
"restype must be a type, a callable, or None");
2924
Py_XDECREF(self->checker);
2925
Py_XDECREF(self->restype);
2928
self->checker = PyObject_GetAttrString(ob, "_check_retval_");
2929
if (self->checker == NULL)
2934
2934
static PyObject *
2935
2935
PyCFuncPtr_get_restype(PyCFuncPtrObject *self)
2937
StgDictObject *dict;
2938
if (self->restype) {
2939
Py_INCREF(self->restype);
2940
return self->restype;
2942
dict = PyObject_stgdict((PyObject *)self);
2943
assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
2944
if (dict->restype) {
2945
Py_INCREF(dict->restype);
2946
return dict->restype;
2937
StgDictObject *dict;
2938
if (self->restype) {
2939
Py_INCREF(self->restype);
2940
return self->restype;
2942
dict = PyObject_stgdict((PyObject *)self);
2943
assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
2944
if (dict->restype) {
2945
Py_INCREF(dict->restype);
2946
return dict->restype;
2954
2954
PyCFuncPtr_set_argtypes(PyCFuncPtrObject *self, PyObject *ob)
2956
PyObject *converters;
2956
PyObject *converters;
2958
if (ob == NULL || ob == Py_None) {
2959
Py_XDECREF(self->converters);
2960
self->converters = NULL;
2961
Py_XDECREF(self->argtypes);
2962
self->argtypes = NULL;
2964
converters = converters_from_argtypes(ob);
2967
Py_XDECREF(self->converters);
2968
self->converters = converters;
2969
Py_XDECREF(self->argtypes);
2971
self->argtypes = ob;
2958
if (ob == NULL || ob == Py_None) {
2959
Py_XDECREF(self->converters);
2960
self->converters = NULL;
2961
Py_XDECREF(self->argtypes);
2962
self->argtypes = NULL;
2964
converters = converters_from_argtypes(ob);
2967
Py_XDECREF(self->converters);
2968
self->converters = converters;
2969
Py_XDECREF(self->argtypes);
2971
self->argtypes = ob;
2976
2976
static PyObject *
2977
2977
PyCFuncPtr_get_argtypes(PyCFuncPtrObject *self)
2979
StgDictObject *dict;
2980
if (self->argtypes) {
2981
Py_INCREF(self->argtypes);
2982
return self->argtypes;
2984
dict = PyObject_stgdict((PyObject *)self);
2985
assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
2986
if (dict->argtypes) {
2987
Py_INCREF(dict->argtypes);
2988
return dict->argtypes;
2979
StgDictObject *dict;
2980
if (self->argtypes) {
2981
Py_INCREF(self->argtypes);
2982
return self->argtypes;
2984
dict = PyObject_stgdict((PyObject *)self);
2985
assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
2986
if (dict->argtypes) {
2987
Py_INCREF(dict->argtypes);
2988
return dict->argtypes;
2995
2995
static PyGetSetDef PyCFuncPtr_getsets[] = {
2996
{ "errcheck", (getter)PyCFuncPtr_get_errcheck, (setter)PyCFuncPtr_set_errcheck,
2997
"a function to check for errors", NULL },
2998
{ "restype", (getter)PyCFuncPtr_get_restype, (setter)PyCFuncPtr_set_restype,
2999
"specify the result type", NULL },
3000
{ "argtypes", (getter)PyCFuncPtr_get_argtypes,
3001
(setter)PyCFuncPtr_set_argtypes,
3002
"specify the argument types", NULL },
2996
{ "errcheck", (getter)PyCFuncPtr_get_errcheck, (setter)PyCFuncPtr_set_errcheck,
2997
"a function to check for errors", NULL },
2998
{ "restype", (getter)PyCFuncPtr_get_restype, (setter)PyCFuncPtr_set_restype,
2999
"specify the result type", NULL },
3000
{ "argtypes", (getter)PyCFuncPtr_get_argtypes,
3001
(setter)PyCFuncPtr_set_argtypes,
3002
"specify the argument types", NULL },
3006
3006
#ifdef MS_WIN32
3007
3007
static PPROC FindAddress(void *handle, char *name, PyObject *type)
3009
3009
#ifdef MS_WIN64
3010
/* win64 has no stdcall calling conv, so it should
3011
also not have the name mangling of it.
3013
return (PPROC)GetProcAddress(handle, name);
3010
/* win64 has no stdcall calling conv, so it should
3011
also not have the name mangling of it.
3013
return (PPROC)GetProcAddress(handle, name);
3018
StgDictObject *dict;
3020
address = (PPROC)GetProcAddress(handle, name);
3023
if (((size_t)name & ~0xFFFF) == 0) {
3027
dict = PyType_stgdict((PyObject *)type);
3028
/* It should not happen that dict is NULL, but better be safe */
3029
if (dict==NULL || dict->flags & FUNCFLAG_CDECL)
3032
/* for stdcall, try mangled names:
3033
funcname -> _funcname@<n>
3034
where n is 0, 4, 8, 12, ..., 128
3036
mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */
3039
for (i = 0; i < 32; ++i) {
3040
sprintf(mangled_name, "_%s@%d", name, i*4);
3041
address = (PPROC)GetProcAddress(handle, mangled_name);
3018
StgDictObject *dict;
3020
address = (PPROC)GetProcAddress(handle, name);
3023
if (((size_t)name & ~0xFFFF) == 0) {
3027
dict = PyType_stgdict((PyObject *)type);
3028
/* It should not happen that dict is NULL, but better be safe */
3029
if (dict==NULL || dict->flags & FUNCFLAG_CDECL)
3032
/* for stdcall, try mangled names:
3033
funcname -> _funcname@<n>
3034
where n is 0, 4, 8, 12, ..., 128
3036
mangled_name = alloca(strlen(name) + 1 + 1 + 1 + 3); /* \0 _ @ %d */
3039
for (i = 0; i < 32; ++i) {
3040
sprintf(mangled_name, "_%s@%d", name, i*4);
3041
address = (PPROC)GetProcAddress(handle, mangled_name);
3052
3052
_check_outarg_type(PyObject *arg, Py_ssize_t index)
3054
StgDictObject *dict;
3056
if (PyCPointerTypeObject_Check(arg))
3059
if (PyCArrayTypeObject_Check(arg))
3062
dict = PyType_stgdict(arg);
3064
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
3065
&& PyUnicode_Check(dict->proto)
3054
StgDictObject *dict;
3056
if (PyCPointerTypeObject_Check(arg))
3059
if (PyCArrayTypeObject_Check(arg))
3062
dict = PyType_stgdict(arg);
3064
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
3065
&& PyUnicode_Check(dict->proto)
3066
3066
/* We only allow c_void_p, c_char_p and c_wchar_p as a simple output parameter type */
3067
&& (strchr("PzZ", _PyUnicode_AsString(dict->proto)[0]))) {
3067
&& (strchr("PzZ", _PyUnicode_AsString(dict->proto)[0]))) {
3071
PyErr_Format(PyExc_TypeError,
3072
"'out' parameter %d must be a pointer type, not %s",
3073
Py_SAFE_DOWNCAST(index, Py_ssize_t, int),
3075
((PyTypeObject *)arg)->tp_name :
3076
Py_TYPE(arg)->tp_name);
3071
PyErr_Format(PyExc_TypeError,
3072
"'out' parameter %d must be a pointer type, not %s",
3073
Py_SAFE_DOWNCAST(index, Py_ssize_t, int),
3075
((PyTypeObject *)arg)->tp_name :
3076
Py_TYPE(arg)->tp_name);
3080
3080
/* Returns 1 on success, 0 on error */
3082
3082
_validate_paramflags(PyTypeObject *type, PyObject *paramflags)
3085
StgDictObject *dict;
3088
dict = PyType_stgdict((PyObject *)type);
3089
assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */
3090
argtypes = dict->argtypes;
3092
if (paramflags == NULL || dict->argtypes == NULL)
3095
if (!PyTuple_Check(paramflags)) {
3096
PyErr_SetString(PyExc_TypeError,
3097
"paramflags must be a tuple or None");
3101
len = PyTuple_GET_SIZE(paramflags);
3102
if (len != PyTuple_GET_SIZE(dict->argtypes)) {
3103
PyErr_SetString(PyExc_ValueError,
3104
"paramflags must have the same length as argtypes");
3108
for (i = 0; i < len; ++i) {
3109
PyObject *item = PyTuple_GET_ITEM(paramflags, i);
3114
if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) {
3115
PyErr_SetString(PyExc_TypeError,
3116
"paramflags must be a sequence of (int [,string [,value]]) tuples");
3119
typ = PyTuple_GET_ITEM(argtypes, i);
3120
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
3123
case PARAMFLAG_FIN | PARAMFLAG_FLCID:
3124
case PARAMFLAG_FIN | PARAMFLAG_FOUT:
3126
case PARAMFLAG_FOUT:
3127
if (!_check_outarg_type(typ, i+1))
3131
PyErr_Format(PyExc_TypeError,
3132
"paramflag value %d not supported",
3085
StgDictObject *dict;
3088
dict = PyType_stgdict((PyObject *)type);
3089
assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */
3090
argtypes = dict->argtypes;
3092
if (paramflags == NULL || dict->argtypes == NULL)
3095
if (!PyTuple_Check(paramflags)) {
3096
PyErr_SetString(PyExc_TypeError,
3097
"paramflags must be a tuple or None");
3101
len = PyTuple_GET_SIZE(paramflags);
3102
if (len != PyTuple_GET_SIZE(dict->argtypes)) {
3103
PyErr_SetString(PyExc_ValueError,
3104
"paramflags must have the same length as argtypes");
3108
for (i = 0; i < len; ++i) {
3109
PyObject *item = PyTuple_GET_ITEM(paramflags, i);
3114
if (!PyArg_ParseTuple(item, "i|ZO", &flag, &name, &defval)) {
3115
PyErr_SetString(PyExc_TypeError,
3116
"paramflags must be a sequence of (int [,string [,value]]) tuples");
3119
typ = PyTuple_GET_ITEM(argtypes, i);
3120
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
3123
case PARAMFLAG_FIN | PARAMFLAG_FLCID:
3124
case PARAMFLAG_FIN | PARAMFLAG_FOUT:
3126
case PARAMFLAG_FOUT:
3127
if (!_check_outarg_type(typ, i+1))
3131
PyErr_Format(PyExc_TypeError,
3132
"paramflag value %d not supported",
3141
3141
_get_name(PyObject *obj, char **pname)
3143
3143
#ifdef MS_WIN32
3144
if (PyLong_Check(obj)) {
3145
/* We have to use MAKEINTRESOURCEA for Windows CE.
3146
Works on Windows as well, of course.
3148
*pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF);
3144
if (PyLong_Check(obj)) {
3145
/* We have to use MAKEINTRESOURCEA for Windows CE.
3146
Works on Windows as well, of course.
3148
*pname = MAKEINTRESOURCEA(PyLong_AsUnsignedLongMask(obj) & 0xFFFF);
3152
if (PyBytes_Check(obj)) {
3153
*pname = PyBytes_AS_STRING(obj);
3154
return *pname ? 1 : 0;
3156
if (PyUnicode_Check(obj)) {
3157
*pname = _PyUnicode_AsString(obj);
3158
return *pname ? 1 : 0;
3160
PyErr_SetString(PyExc_TypeError,
3161
"function name must be string or integer");
3152
if (PyBytes_Check(obj)) {
3153
*pname = PyBytes_AS_STRING(obj);
3154
return *pname ? 1 : 0;
3156
if (PyUnicode_Check(obj)) {
3157
*pname = _PyUnicode_AsString(obj);
3158
return *pname ? 1 : 0;
3160
PyErr_SetString(PyExc_TypeError,
3161
"function name must be string or integer");
3166
3166
static PyObject *
3167
3167
PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
3170
int (* address)(void);
3173
PyCFuncPtrObject *self;
3175
PyObject *paramflags = NULL;
3177
if (!PyArg_ParseTuple(args, "(O&O)|O", _get_name, &name, &dll, ¶mflags))
3179
if (paramflags == Py_None)
3182
obj = PyObject_GetAttrString(dll, "_handle");
3185
if (!PyLong_Check(obj)) {
3186
PyErr_SetString(PyExc_TypeError,
3187
"the _handle attribute of the second argument must be an integer");
3191
handle = (void *)PyLong_AsVoidPtr(obj);
3193
if (PyErr_Occurred()) {
3194
PyErr_SetString(PyExc_ValueError,
3195
"could not convert the _handle attribute to a pointer");
3170
int (* address)(void);
3173
PyCFuncPtrObject *self;
3175
PyObject *paramflags = NULL;
3177
if (!PyArg_ParseTuple(args, "(O&O)|O", _get_name, &name, &dll, ¶mflags))
3179
if (paramflags == Py_None)
3182
obj = PyObject_GetAttrString(dll, "_handle");
3185
if (!PyLong_Check(obj)) {
3186
PyErr_SetString(PyExc_TypeError,
3187
"the _handle attribute of the second argument must be an integer");
3191
handle = (void *)PyLong_AsVoidPtr(obj);
3193
if (PyErr_Occurred()) {
3194
PyErr_SetString(PyExc_ValueError,
3195
"could not convert the _handle attribute to a pointer");
3199
3199
#ifdef MS_WIN32
3200
address = FindAddress(handle, name, (PyObject *)type);
3202
if (!IS_INTRESOURCE(name))
3203
PyErr_Format(PyExc_AttributeError,
3204
"function '%s' not found",
3207
PyErr_Format(PyExc_AttributeError,
3208
"function ordinal %d not found",
3209
(WORD)(size_t)name);
3200
address = FindAddress(handle, name, (PyObject *)type);
3202
if (!IS_INTRESOURCE(name))
3203
PyErr_Format(PyExc_AttributeError,
3204
"function '%s' not found",
3207
PyErr_Format(PyExc_AttributeError,
3208
"function ordinal %d not found",
3209
(WORD)(size_t)name);
3213
address = (PPROC)ctypes_dlsym(handle, name);
3213
address = (PPROC)ctypes_dlsym(handle, name);
3215
3215
#ifdef __CYGWIN__
3216
3216
/* dlerror() isn't very helpful on cygwin */
3217
PyErr_Format(PyExc_AttributeError,
3218
"function '%s' not found (%s) ",
3217
PyErr_Format(PyExc_AttributeError,
3218
"function '%s' not found (%s) ",
3221
PyErr_SetString(PyExc_AttributeError, ctypes_dlerror());
3226
if (!_validate_paramflags(type, paramflags))
3229
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3233
Py_XINCREF(paramflags);
3234
self->paramflags = paramflags;
3236
*(void **)self->b_ptr = address;
3238
Py_INCREF((PyObject *)dll); /* for KeepRef */
3239
if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
3240
Py_DECREF((PyObject *)self);
3245
self->callable = (PyObject *)self;
3246
return (PyObject *)self;
3221
PyErr_SetString(PyExc_AttributeError, ctypes_dlerror());
3226
if (!_validate_paramflags(type, paramflags))
3229
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3233
Py_XINCREF(paramflags);
3234
self->paramflags = paramflags;
3236
*(void **)self->b_ptr = address;
3238
Py_INCREF((PyObject *)dll); /* for KeepRef */
3239
if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
3240
Py_DECREF((PyObject *)self);
3245
self->callable = (PyObject *)self;
3246
return (PyObject *)self;
3249
3249
#ifdef MS_WIN32
3250
3250
static PyObject *
3251
3251
PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds)
3253
PyCFuncPtrObject *self;
3256
PyObject *paramflags = NULL;
3258
Py_ssize_t iid_len = 0;
3260
if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, ¶mflags, &iid, &iid_len))
3262
if (paramflags == Py_None)
3265
if (!_validate_paramflags(type, paramflags))
3268
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3269
self->index = index + 0x1000;
3270
Py_XINCREF(paramflags);
3271
self->paramflags = paramflags;
3272
if (iid_len == sizeof(GUID))
3274
return (PyObject *)self;
3253
PyCFuncPtrObject *self;
3256
PyObject *paramflags = NULL;
3258
Py_ssize_t iid_len = 0;
3260
if (!PyArg_ParseTuple(args, "is|Oz#", &index, &name, ¶mflags, &iid, &iid_len))
3262
if (paramflags == Py_None)
3265
if (!_validate_paramflags(type, paramflags))
3268
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3269
self->index = index + 0x1000;
3270
Py_XINCREF(paramflags);
3271
self->paramflags = paramflags;
3272
if (iid_len == sizeof(GUID))
3274
return (PyObject *)self;
3291
3291
static PyObject *
3292
3292
PyCFuncPtr_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3294
PyCFuncPtrObject *self;
3296
StgDictObject *dict;
3297
CThunkObject *thunk;
3299
if (PyTuple_GET_SIZE(args) == 0)
3300
return GenericPyCData_new(type, args, kwds);
3302
if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0)))
3303
return PyCFuncPtr_FromDll(type, args, kwds);
3294
PyCFuncPtrObject *self;
3296
StgDictObject *dict;
3297
CThunkObject *thunk;
3299
if (PyTuple_GET_SIZE(args) == 0)
3300
return GenericPyCData_new(type, args, kwds);
3302
if (1 <= PyTuple_GET_SIZE(args) && PyTuple_Check(PyTuple_GET_ITEM(args, 0)))
3303
return PyCFuncPtr_FromDll(type, args, kwds);
3305
3305
#ifdef MS_WIN32
3306
if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0)))
3307
return PyCFuncPtr_FromVtblIndex(type, args, kwds);
3306
if (2 <= PyTuple_GET_SIZE(args) && PyLong_Check(PyTuple_GET_ITEM(args, 0)))
3307
return PyCFuncPtr_FromVtblIndex(type, args, kwds);
3310
if (1 == PyTuple_GET_SIZE(args)
3311
&& (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
3313
void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
3314
if (ptr == NULL && PyErr_Occurred())
3316
ob = (CDataObject *)GenericPyCData_new(type, args, kwds);
3319
*(void **)ob->b_ptr = ptr;
3320
return (PyObject *)ob;
3323
if (!PyArg_ParseTuple(args, "O", &callable))
3325
if (!PyCallable_Check(callable)) {
3326
PyErr_SetString(PyExc_TypeError,
3327
"argument must be callable or integer function address");
3331
/* XXX XXX This would allow to pass additional options. For COM
3332
method *implementations*, we would probably want different
3333
behaviour than in 'normal' callback functions: return a HRESULT if
3334
an exception occurrs in the callback, and print the traceback not
3335
only on the console, but also to OutputDebugString() or something
3310
if (1 == PyTuple_GET_SIZE(args)
3311
&& (PyLong_Check(PyTuple_GET_ITEM(args, 0)))) {
3313
void *ptr = PyLong_AsVoidPtr(PyTuple_GET_ITEM(args, 0));
3314
if (ptr == NULL && PyErr_Occurred())
3316
ob = (CDataObject *)GenericPyCData_new(type, args, kwds);
3319
*(void **)ob->b_ptr = ptr;
3320
return (PyObject *)ob;
3323
if (!PyArg_ParseTuple(args, "O", &callable))
3325
if (!PyCallable_Check(callable)) {
3326
PyErr_SetString(PyExc_TypeError,
3327
"argument must be callable or integer function address");
3331
/* XXX XXX This would allow to pass additional options. For COM
3332
method *implementations*, we would probably want different
3333
behaviour than in 'normal' callback functions: return a HRESULT if
3334
an exception occurrs in the callback, and print the traceback not
3335
only on the console, but also to OutputDebugString() or something
3339
if (kwds && PyDict_GetItemString(kwds, "options")) {
3339
if (kwds && PyDict_GetItemString(kwds, "options")) {
3344
dict = PyType_stgdict((PyObject *)type);
3345
/* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */
3346
if (!dict || !dict->argtypes) {
3347
PyErr_SetString(PyExc_TypeError,
3348
"cannot construct instance of this class:"
3353
thunk = _ctypes_alloc_callback(callable,
3360
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3366
Py_INCREF(callable);
3367
self->callable = callable;
3369
self->thunk = thunk;
3370
*(void **)self->b_ptr = (void *)thunk->pcl;
3372
Py_INCREF((PyObject *)thunk); /* for KeepRef */
3373
if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
3374
Py_DECREF((PyObject *)self);
3377
return (PyObject *)self;
3344
dict = PyType_stgdict((PyObject *)type);
3345
/* XXXX Fails if we do: 'PyCFuncPtr(lambda x: x)' */
3346
if (!dict || !dict->argtypes) {
3347
PyErr_SetString(PyExc_TypeError,
3348
"cannot construct instance of this class:"
3353
thunk = _ctypes_alloc_callback(callable,
3360
self = (PyCFuncPtrObject *)GenericPyCData_new(type, args, kwds);
3366
Py_INCREF(callable);
3367
self->callable = callable;
3369
self->thunk = thunk;
3370
*(void **)self->b_ptr = (void *)thunk->pcl;
3372
Py_INCREF((PyObject *)thunk); /* for KeepRef */
3373
if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)thunk)) {
3374
Py_DECREF((PyObject *)self);
3377
return (PyObject *)self;
3455
3455
static PyObject *
3456
3456
_build_callargs(PyCFuncPtrObject *self, PyObject *argtypes,
3457
PyObject *inargs, PyObject *kwds,
3458
int *poutmask, int *pinoutmask, unsigned int *pnumretvals)
3457
PyObject *inargs, PyObject *kwds,
3458
int *poutmask, int *pinoutmask, unsigned int *pnumretvals)
3460
PyObject *paramflags = self->paramflags;
3462
StgDictObject *dict;
3464
int inargs_index = 0;
3465
/* It's a little bit difficult to determine how many arguments the
3466
function call requires/accepts. For simplicity, we count the consumed
3467
args and compare this to the number of supplied args. */
3468
Py_ssize_t actual_args;
3474
/* Trivial cases, where we either return inargs itself, or a slice of it. */
3475
if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) {
3478
return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs));
3484
len = PyTuple_GET_SIZE(argtypes);
3485
callargs = PyTuple_New(len); /* the argument tuple we build */
3486
if (callargs == NULL)
3490
/* For a COM method, skip the first arg */
3495
for (i = 0; i < len; ++i) {
3496
PyObject *item = PyTuple_GET_ITEM(paramflags, i);
3499
PyObject *name = NULL;
3500
PyObject *defval = NULL;
3502
/* This way seems to be ~2 us faster than the PyArg_ParseTuple
3504
/* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */
3505
Py_ssize_t tsize = PyTuple_GET_SIZE(item);
3506
flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0));
3507
name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
3508
defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
3510
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
3511
case PARAMFLAG_FIN | PARAMFLAG_FLCID:
3512
/* ['in', 'lcid'] parameter. Always taken from defval,
3513
if given, else the integer 0. */
3514
if (defval == NULL) {
3515
defval = PyLong_FromLong(0);
3520
PyTuple_SET_ITEM(callargs, i, defval);
3522
case (PARAMFLAG_FIN | PARAMFLAG_FOUT):
3523
*pinoutmask |= (1 << i); /* mark as inout arg */
3525
/* fall through to PARAMFLAG_FIN... */
3528
/* 'in' parameter. Copy it from inargs. */
3529
ob =_get_arg(&inargs_index, name, defval, inargs, kwds);
3532
PyTuple_SET_ITEM(callargs, i, ob);
3534
case PARAMFLAG_FOUT:
3535
/* XXX Refactor this code into a separate function. */
3537
argtypes[i] must be a POINTER to a c type.
3539
Cannot by supplied in inargs, but a defval will be used
3540
if available. XXX Should we support getting it from kwds?
3543
/* XXX Using mutable objects as defval will
3544
make the function non-threadsafe, unless we
3545
copy the object in each invocation */
3547
PyTuple_SET_ITEM(callargs, i, defval);
3548
*poutmask |= (1 << i); /* mark as out arg */
3552
ob = PyTuple_GET_ITEM(argtypes, i);
3553
dict = PyType_stgdict(ob);
3555
/* Cannot happen: _validate_paramflags()
3556
would not accept such an object */
3557
PyErr_Format(PyExc_RuntimeError,
3558
"NULL stgdict unexpected");
3561
if (PyUnicode_Check(dict->proto)) {
3564
"%s 'out' parameter must be passed as default value",
3565
((PyTypeObject *)ob)->tp_name);
3568
if (PyCArrayTypeObject_Check(ob))
3569
ob = PyObject_CallObject(ob, NULL);
3571
/* Create an instance of the pointed-to type */
3572
ob = PyObject_CallObject(dict->proto, NULL);
3574
XXX Is the following correct any longer?
3575
We must not pass a byref() to the array then but
3576
the array instance itself. Then, we cannot retrive
3577
the result from the PyCArgObject.
3581
/* The .from_param call that will ocurr later will pass this
3582
as a byref parameter. */
3583
PyTuple_SET_ITEM(callargs, i, ob);
3584
*poutmask |= (1 << i); /* mark as out arg */
3588
PyErr_Format(PyExc_ValueError,
3589
"paramflag %d not yet implemented", flag);
3595
/* We have counted the arguments we have consumed in 'inargs_index'. This
3596
must be the same as len(inargs) + len(kwds), otherwise we have
3597
either too much or not enough arguments. */
3599
actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
3600
if (actual_args != inargs_index) {
3601
/* When we have default values or named parameters, this error
3602
message is misleading. See unittests/test_paramflags.py
3604
PyErr_Format(PyExc_TypeError,
3605
"call takes exactly %d arguments (%zd given)",
3606
inargs_index, actual_args);
3610
/* outmask is a bitmask containing indexes into callargs. Items at
3611
these indexes contain values to return.
3460
PyObject *paramflags = self->paramflags;
3462
StgDictObject *dict;
3464
int inargs_index = 0;
3465
/* It's a little bit difficult to determine how many arguments the
3466
function call requires/accepts. For simplicity, we count the consumed
3467
args and compare this to the number of supplied args. */
3468
Py_ssize_t actual_args;
3474
/* Trivial cases, where we either return inargs itself, or a slice of it. */
3475
if (argtypes == NULL || paramflags == NULL || PyTuple_GET_SIZE(argtypes) == 0) {
3478
return PyTuple_GetSlice(inargs, 1, PyTuple_GET_SIZE(inargs));
3484
len = PyTuple_GET_SIZE(argtypes);
3485
callargs = PyTuple_New(len); /* the argument tuple we build */
3486
if (callargs == NULL)
3490
/* For a COM method, skip the first arg */
3495
for (i = 0; i < len; ++i) {
3496
PyObject *item = PyTuple_GET_ITEM(paramflags, i);
3499
PyObject *name = NULL;
3500
PyObject *defval = NULL;
3502
/* This way seems to be ~2 us faster than the PyArg_ParseTuple
3504
/* We HAVE already checked that the tuple can be parsed with "i|ZO", so... */
3505
Py_ssize_t tsize = PyTuple_GET_SIZE(item);
3506
flag = PyLong_AS_LONG(PyTuple_GET_ITEM(item, 0));
3507
name = tsize > 1 ? PyTuple_GET_ITEM(item, 1) : NULL;
3508
defval = tsize > 2 ? PyTuple_GET_ITEM(item, 2) : NULL;
3510
switch (flag & (PARAMFLAG_FIN | PARAMFLAG_FOUT | PARAMFLAG_FLCID)) {
3511
case PARAMFLAG_FIN | PARAMFLAG_FLCID:
3512
/* ['in', 'lcid'] parameter. Always taken from defval,
3513
if given, else the integer 0. */
3514
if (defval == NULL) {
3515
defval = PyLong_FromLong(0);
3520
PyTuple_SET_ITEM(callargs, i, defval);
3522
case (PARAMFLAG_FIN | PARAMFLAG_FOUT):
3523
*pinoutmask |= (1 << i); /* mark as inout arg */
3525
/* fall through to PARAMFLAG_FIN... */
3528
/* 'in' parameter. Copy it from inargs. */
3529
ob =_get_arg(&inargs_index, name, defval, inargs, kwds);
3532
PyTuple_SET_ITEM(callargs, i, ob);
3534
case PARAMFLAG_FOUT:
3535
/* XXX Refactor this code into a separate function. */
3537
argtypes[i] must be a POINTER to a c type.
3539
Cannot by supplied in inargs, but a defval will be used
3540
if available. XXX Should we support getting it from kwds?
3543
/* XXX Using mutable objects as defval will
3544
make the function non-threadsafe, unless we
3545
copy the object in each invocation */
3547
PyTuple_SET_ITEM(callargs, i, defval);
3548
*poutmask |= (1 << i); /* mark as out arg */
3552
ob = PyTuple_GET_ITEM(argtypes, i);
3553
dict = PyType_stgdict(ob);
3555
/* Cannot happen: _validate_paramflags()
3556
would not accept such an object */
3557
PyErr_Format(PyExc_RuntimeError,
3558
"NULL stgdict unexpected");
3561
if (PyUnicode_Check(dict->proto)) {
3564
"%s 'out' parameter must be passed as default value",
3565
((PyTypeObject *)ob)->tp_name);
3568
if (PyCArrayTypeObject_Check(ob))
3569
ob = PyObject_CallObject(ob, NULL);
3571
/* Create an instance of the pointed-to type */
3572
ob = PyObject_CallObject(dict->proto, NULL);
3574
XXX Is the following correct any longer?
3575
We must not pass a byref() to the array then but
3576
the array instance itself. Then, we cannot retrive
3577
the result from the PyCArgObject.
3581
/* The .from_param call that will ocurr later will pass this
3582
as a byref parameter. */
3583
PyTuple_SET_ITEM(callargs, i, ob);
3584
*poutmask |= (1 << i); /* mark as out arg */
3588
PyErr_Format(PyExc_ValueError,
3589
"paramflag %d not yet implemented", flag);
3595
/* We have counted the arguments we have consumed in 'inargs_index'. This
3596
must be the same as len(inargs) + len(kwds), otherwise we have
3597
either too much or not enough arguments. */
3599
actual_args = PyTuple_GET_SIZE(inargs) + (kwds ? PyDict_Size(kwds) : 0);
3600
if (actual_args != inargs_index) {
3601
/* When we have default values or named parameters, this error
3602
message is misleading. See unittests/test_paramflags.py
3604
PyErr_Format(PyExc_TypeError,
3605
"call takes exactly %d arguments (%zd given)",
3606
inargs_index, actual_args);
3610
/* outmask is a bitmask containing indexes into callargs. Items at
3611
these indexes contain values to return.
3615
Py_DECREF(callargs);
3615
Py_DECREF(callargs);
3627
3627
static PyObject *
3628
3628
_build_result(PyObject *result, PyObject *callargs,
3629
int outmask, int inoutmask, unsigned int numretvals)
3629
int outmask, int inoutmask, unsigned int numretvals)
3631
unsigned int i, index;
3633
PyObject *tup = NULL;
3635
if (callargs == NULL)
3637
if (result == NULL || numretvals == 0) {
3638
Py_DECREF(callargs);
3643
/* tup will not be allocated if numretvals == 1 */
3644
/* allocate tuple to hold the result */
3645
if (numretvals > 1) {
3646
tup = PyTuple_New(numretvals);
3648
Py_DECREF(callargs);
3654
for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) {
3656
if (bit & inoutmask) {
3657
v = PyTuple_GET_ITEM(callargs, i);
3659
if (numretvals == 1) {
3660
Py_DECREF(callargs);
3663
PyTuple_SET_ITEM(tup, index, v);
3665
} else if (bit & outmask) {
3666
v = PyTuple_GET_ITEM(callargs, i);
3667
v = PyObject_CallMethod(v, "__ctypes_from_outparam__", NULL);
3668
if (v == NULL || numretvals == 1) {
3669
Py_DECREF(callargs);
3672
PyTuple_SET_ITEM(tup, index, v);
3675
if (index == numretvals)
3679
Py_DECREF(callargs);
3631
unsigned int i, index;
3633
PyObject *tup = NULL;
3635
if (callargs == NULL)
3637
if (result == NULL || numretvals == 0) {
3638
Py_DECREF(callargs);
3643
/* tup will not be allocated if numretvals == 1 */
3644
/* allocate tuple to hold the result */
3645
if (numretvals > 1) {
3646
tup = PyTuple_New(numretvals);
3648
Py_DECREF(callargs);
3654
for (bit = 1, i = 0; i < 32; ++i, bit <<= 1) {
3656
if (bit & inoutmask) {
3657
v = PyTuple_GET_ITEM(callargs, i);
3659
if (numretvals == 1) {
3660
Py_DECREF(callargs);
3663
PyTuple_SET_ITEM(tup, index, v);
3665
} else if (bit & outmask) {
3666
v = PyTuple_GET_ITEM(callargs, i);
3667
v = PyObject_CallMethod(v, "__ctypes_from_outparam__", NULL);
3668
if (v == NULL || numretvals == 1) {
3669
Py_DECREF(callargs);
3672
PyTuple_SET_ITEM(tup, index, v);
3675
if (index == numretvals)
3679
Py_DECREF(callargs);
3683
3683
static PyObject *
3684
3684
PyCFuncPtr_call(PyCFuncPtrObject *self, PyObject *inargs, PyObject *kwds)
3687
PyObject *converters;
3690
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
3687
PyObject *converters;
3690
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
3694
3694
#ifdef MS_WIN32
3695
IUnknown *piunk = NULL;
3695
IUnknown *piunk = NULL;
3701
unsigned int numretvals;
3703
assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
3704
restype = self->restype ? self->restype : dict->restype;
3705
converters = self->converters ? self->converters : dict->converters;
3706
checker = self->checker ? self->checker : dict->checker;
3707
argtypes = self->argtypes ? self->argtypes : dict->argtypes;
3701
unsigned int numretvals;
3703
assert(dict); /* Cannot be NULL for PyCFuncPtrObject instances */
3704
restype = self->restype ? self->restype : dict->restype;
3705
converters = self->converters ? self->converters : dict->converters;
3706
checker = self->checker ? self->checker : dict->checker;
3707
argtypes = self->argtypes ? self->argtypes : dict->argtypes;
3708
3708
/* later, we probably want to have an errcheck field in stgdict */
3709
errcheck = self->errcheck /* ? self->errcheck : dict->errcheck */;
3712
pProc = *(void **)self->b_ptr;
3715
/* It's a COM method */
3717
this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */
3719
PyErr_SetString(PyExc_ValueError,
3720
"native com method call without 'this' parameter");
3723
if (!CDataObject_Check(this)) {
3724
PyErr_SetString(PyExc_TypeError,
3725
"Expected a COM this pointer as first argument");
3728
/* there should be more checks? No, in Python */
3729
/* First arg is an pointer to an interface instance */
3730
if (!this->b_ptr || *(void **)this->b_ptr == NULL) {
3731
PyErr_SetString(PyExc_ValueError,
3732
"NULL COM pointer access");
3735
piunk = *(IUnknown **)this->b_ptr;
3736
if (NULL == piunk->lpVtbl) {
3737
PyErr_SetString(PyExc_ValueError,
3738
"COM method call without VTable");
3741
pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000];
3744
callargs = _build_callargs(self, argtypes,
3746
&outmask, &inoutmask, &numretvals);
3747
if (callargs == NULL)
3751
int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters),
3753
int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs),
3756
if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) {
3757
/* For cdecl functions, we allow more actual arguments
3758
than the length of the argtypes tuple.
3760
if (required > actual) {
3761
Py_DECREF(callargs);
3762
PyErr_Format(PyExc_TypeError,
3763
"this function takes at least %d argument%s (%d given)",
3765
required == 1 ? "" : "s",
3769
} else if (required != actual) {
3770
Py_DECREF(callargs);
3771
PyErr_Format(PyExc_TypeError,
3772
"this function takes %d argument%s (%d given)",
3774
required == 1 ? "" : "s",
3780
result = _ctypes_callproc(pProc,
3709
errcheck = self->errcheck /* ? self->errcheck : dict->errcheck */;
3712
pProc = *(void **)self->b_ptr;
3715
/* It's a COM method */
3717
this = (CDataObject *)PyTuple_GetItem(inargs, 0); /* borrowed ref! */
3719
PyErr_SetString(PyExc_ValueError,
3720
"native com method call without 'this' parameter");
3723
if (!CDataObject_Check(this)) {
3724
PyErr_SetString(PyExc_TypeError,
3725
"Expected a COM this pointer as first argument");
3728
/* there should be more checks? No, in Python */
3729
/* First arg is an pointer to an interface instance */
3730
if (!this->b_ptr || *(void **)this->b_ptr == NULL) {
3731
PyErr_SetString(PyExc_ValueError,
3732
"NULL COM pointer access");
3735
piunk = *(IUnknown **)this->b_ptr;
3736
if (NULL == piunk->lpVtbl) {
3737
PyErr_SetString(PyExc_ValueError,
3738
"COM method call without VTable");
3741
pProc = ((void **)piunk->lpVtbl)[self->index - 0x1000];
3744
callargs = _build_callargs(self, argtypes,
3746
&outmask, &inoutmask, &numretvals);
3747
if (callargs == NULL)
3751
int required = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(converters),
3753
int actual = Py_SAFE_DOWNCAST(PyTuple_GET_SIZE(callargs),
3756
if ((dict->flags & FUNCFLAG_CDECL) == FUNCFLAG_CDECL) {
3757
/* For cdecl functions, we allow more actual arguments
3758
than the length of the argtypes tuple.
3760
if (required > actual) {
3761
Py_DECREF(callargs);
3762
PyErr_Format(PyExc_TypeError,
3763
"this function takes at least %d argument%s (%d given)",
3765
required == 1 ? "" : "s",
3769
} else if (required != actual) {
3770
Py_DECREF(callargs);
3771
PyErr_Format(PyExc_TypeError,
3772
"this function takes %d argument%s (%d given)",
3774
required == 1 ? "" : "s",
3780
result = _ctypes_callproc(pProc,
3790
3790
/* The 'errcheck' protocol */
3791
if (result != NULL && errcheck) {
3792
PyObject *v = PyObject_CallFunctionObjArgs(errcheck,
3797
/* If the errcheck funtion failed, return NULL.
3798
If the errcheck function returned callargs unchanged,
3799
continue normal processing.
3800
If the errcheck function returned something else,
3803
if (v == NULL || v != callargs) {
3805
Py_DECREF(callargs);
3791
if (result != NULL && errcheck) {
3792
PyObject *v = PyObject_CallFunctionObjArgs(errcheck,
3797
/* If the errcheck funtion failed, return NULL.
3798
If the errcheck function returned callargs unchanged,
3799
continue normal processing.
3800
If the errcheck function returned something else,
3803
if (v == NULL || v != callargs) {
3805
Py_DECREF(callargs);
3811
return _build_result(result, callargs,
3812
outmask, inoutmask, numretvals);
3811
return _build_result(result, callargs,
3812
outmask, inoutmask, numretvals);
3816
3816
PyCFuncPtr_traverse(PyCFuncPtrObject *self, visitproc visit, void *arg)
3818
Py_VISIT(self->callable);
3819
Py_VISIT(self->restype);
3820
Py_VISIT(self->checker);
3821
Py_VISIT(self->errcheck);
3822
Py_VISIT(self->argtypes);
3823
Py_VISIT(self->converters);
3824
Py_VISIT(self->paramflags);
3825
Py_VISIT(self->thunk);
3826
return PyCData_traverse((CDataObject *)self, visit, arg);
3818
Py_VISIT(self->callable);
3819
Py_VISIT(self->restype);
3820
Py_VISIT(self->checker);
3821
Py_VISIT(self->errcheck);
3822
Py_VISIT(self->argtypes);
3823
Py_VISIT(self->converters);
3824
Py_VISIT(self->paramflags);
3825
Py_VISIT(self->thunk);
3826
return PyCData_traverse((CDataObject *)self, visit, arg);
3830
3830
PyCFuncPtr_clear(PyCFuncPtrObject *self)
3832
Py_CLEAR(self->callable);
3833
Py_CLEAR(self->restype);
3834
Py_CLEAR(self->checker);
3835
Py_CLEAR(self->errcheck);
3836
Py_CLEAR(self->argtypes);
3837
Py_CLEAR(self->converters);
3838
Py_CLEAR(self->paramflags);
3839
Py_CLEAR(self->thunk);
3840
return PyCData_clear((CDataObject *)self);
3832
Py_CLEAR(self->callable);
3833
Py_CLEAR(self->restype);
3834
Py_CLEAR(self->checker);
3835
Py_CLEAR(self->errcheck);
3836
Py_CLEAR(self->argtypes);
3837
Py_CLEAR(self->converters);
3838
Py_CLEAR(self->paramflags);
3839
Py_CLEAR(self->thunk);
3840
return PyCData_clear((CDataObject *)self);
3844
3844
PyCFuncPtr_dealloc(PyCFuncPtrObject *self)
3846
PyCFuncPtr_clear(self);
3847
Py_TYPE(self)->tp_free((PyObject *)self);
3846
PyCFuncPtr_clear(self);
3847
Py_TYPE(self)->tp_free((PyObject *)self);
3850
3850
static PyObject *
3851
3851
PyCFuncPtr_repr(PyCFuncPtrObject *self)
3853
3853
#ifdef MS_WIN32
3855
return PyUnicode_FromFormat("<COM method offset %d: %s at %p>",
3856
self->index - 0x1000,
3857
Py_TYPE(self)->tp_name,
3855
return PyUnicode_FromFormat("<COM method offset %d: %s at %p>",
3856
self->index - 0x1000,
3857
Py_TYPE(self)->tp_name,
3860
return PyUnicode_FromFormat("<%s object at %p>",
3861
Py_TYPE(self)->tp_name,
3860
return PyUnicode_FromFormat("<%s object at %p>",
3861
Py_TYPE(self)->tp_name,
3866
3866
PyCFuncPtr_bool(PyCFuncPtrObject *self)
3868
return ((*(void **)self->b_ptr != NULL)
3868
return ((*(void **)self->b_ptr != NULL)
3869
3869
#ifdef MS_WIN32
3870
|| (self->index != 0)
3870
|| (self->index != 0)
3875
3875
static PyNumberMethods PyCFuncPtr_as_number = {
3877
0, /* nb_subtract */
3878
0, /* nb_multiply */
3879
0, /* nb_remainder */
3882
0, /* nb_negative */
3883
0, /* nb_positive */
3884
0, /* nb_absolute */
3885
(inquiry)PyCFuncPtr_bool, /* nb_bool */
3877
0, /* nb_subtract */
3878
0, /* nb_multiply */
3879
0, /* nb_remainder */
3882
0, /* nb_negative */
3883
0, /* nb_positive */
3884
0, /* nb_absolute */
3885
(inquiry)PyCFuncPtr_bool, /* nb_bool */
3888
3888
PyTypeObject PyCFuncPtr_Type = {
3889
PyVarObject_HEAD_INIT(NULL, 0)
3890
"_ctypes.PyCFuncPtr",
3891
sizeof(PyCFuncPtrObject), /* tp_basicsize */
3892
0, /* tp_itemsize */
3893
(destructor)PyCFuncPtr_dealloc, /* tp_dealloc */
3897
0, /* tp_reserved */
3898
(reprfunc)PyCFuncPtr_repr, /* tp_repr */
3899
&PyCFuncPtr_as_number, /* tp_as_number */
3900
0, /* tp_as_sequence */
3901
0, /* tp_as_mapping */
3903
(ternaryfunc)PyCFuncPtr_call, /* tp_call */
3905
0, /* tp_getattro */
3906
0, /* tp_setattro */
3907
&PyCData_as_buffer, /* tp_as_buffer */
3908
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3909
"Function Pointer", /* tp_doc */
3910
(traverseproc)PyCFuncPtr_traverse, /* tp_traverse */
3911
(inquiry)PyCFuncPtr_clear, /* tp_clear */
3912
0, /* tp_richcompare */
3913
0, /* tp_weaklistoffset */
3915
0, /* tp_iternext */
3918
PyCFuncPtr_getsets, /* tp_getset */
3921
0, /* tp_descr_get */
3922
0, /* tp_descr_set */
3923
0, /* tp_dictoffset */
3926
PyCFuncPtr_new, /* tp_new */
3889
PyVarObject_HEAD_INIT(NULL, 0)
3890
"_ctypes.PyCFuncPtr",
3891
sizeof(PyCFuncPtrObject), /* tp_basicsize */
3892
0, /* tp_itemsize */
3893
(destructor)PyCFuncPtr_dealloc, /* tp_dealloc */
3897
0, /* tp_reserved */
3898
(reprfunc)PyCFuncPtr_repr, /* tp_repr */
3899
&PyCFuncPtr_as_number, /* tp_as_number */
3900
0, /* tp_as_sequence */
3901
0, /* tp_as_mapping */
3903
(ternaryfunc)PyCFuncPtr_call, /* tp_call */
3905
0, /* tp_getattro */
3906
0, /* tp_setattro */
3907
&PyCData_as_buffer, /* tp_as_buffer */
3908
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3909
"Function Pointer", /* tp_doc */
3910
(traverseproc)PyCFuncPtr_traverse, /* tp_traverse */
3911
(inquiry)PyCFuncPtr_clear, /* tp_clear */
3912
0, /* tp_richcompare */
3913
0, /* tp_weaklistoffset */
3915
0, /* tp_iternext */
3918
PyCFuncPtr_getsets, /* tp_getset */
3921
0, /* tp_descr_get */
3922
0, /* tp_descr_set */
3923
0, /* tp_dictoffset */
3926
PyCFuncPtr_new, /* tp_new */
3930
3930
/*****************************************************************/
4004
4004
/* Optimization possible: Store the attribute names _fields_[x][0]
4005
4005
* in C accessible fields somewhere ?
4007
if (!PyTuple_Check(args)) {
4008
PyErr_SetString(PyExc_TypeError,
4009
"args not a tuple?");
4012
if (PyTuple_GET_SIZE(args)) {
4013
int res = _init_pos_args(self, Py_TYPE(self),
4017
if (res < PyTuple_GET_SIZE(args)) {
4018
PyErr_SetString(PyExc_TypeError,
4019
"too many initializers");
4007
if (!PyTuple_Check(args)) {
4008
PyErr_SetString(PyExc_TypeError,
4009
"args not a tuple?");
4012
if (PyTuple_GET_SIZE(args)) {
4013
int res = _init_pos_args(self, Py_TYPE(self),
4017
if (res < PyTuple_GET_SIZE(args)) {
4018
PyErr_SetString(PyExc_TypeError,
4019
"too many initializers");
4025
PyObject *key, *value;
4027
while(PyDict_Next(kwds, &pos, &key, &value)) {
4028
if (-1 == PyObject_SetAttr(self, key, value))
4025
PyObject *key, *value;
4027
while(PyDict_Next(kwds, &pos, &key, &value)) {
4028
if (-1 == PyObject_SetAttr(self, key, value))
4035
4035
static PyTypeObject Struct_Type = {
4036
PyVarObject_HEAD_INIT(NULL, 0)
4037
"_ctypes.Structure",
4038
sizeof(CDataObject), /* tp_basicsize */
4039
0, /* tp_itemsize */
4044
0, /* tp_reserved */
4046
0, /* tp_as_number */
4047
0, /* tp_as_sequence */
4048
0, /* tp_as_mapping */
4052
0, /* tp_getattro */
4053
0, /* tp_setattro */
4054
&PyCData_as_buffer, /* tp_as_buffer */
4055
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4056
"Structure base class", /* tp_doc */
4057
(traverseproc)PyCData_traverse, /* tp_traverse */
4058
(inquiry)PyCData_clear, /* tp_clear */
4059
0, /* tp_richcompare */
4060
0, /* tp_weaklistoffset */
4062
0, /* tp_iternext */
4068
0, /* tp_descr_get */
4069
0, /* tp_descr_set */
4070
0, /* tp_dictoffset */
4071
Struct_init, /* tp_init */
4073
GenericPyCData_new, /* tp_new */
4036
PyVarObject_HEAD_INIT(NULL, 0)
4037
"_ctypes.Structure",
4038
sizeof(CDataObject), /* tp_basicsize */
4039
0, /* tp_itemsize */
4044
0, /* tp_reserved */
4046
0, /* tp_as_number */
4047
0, /* tp_as_sequence */
4048
0, /* tp_as_mapping */
4052
0, /* tp_getattro */
4053
0, /* tp_setattro */
4054
&PyCData_as_buffer, /* tp_as_buffer */
4055
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4056
"Structure base class", /* tp_doc */
4057
(traverseproc)PyCData_traverse, /* tp_traverse */
4058
(inquiry)PyCData_clear, /* tp_clear */
4059
0, /* tp_richcompare */
4060
0, /* tp_weaklistoffset */
4062
0, /* tp_iternext */
4068
0, /* tp_descr_get */
4069
0, /* tp_descr_set */
4070
0, /* tp_dictoffset */
4071
Struct_init, /* tp_init */
4073
GenericPyCData_new, /* tp_new */
4077
4077
static PyTypeObject Union_Type = {
4078
PyVarObject_HEAD_INIT(NULL, 0)
4080
sizeof(CDataObject), /* tp_basicsize */
4081
0, /* tp_itemsize */
4086
0, /* tp_reserved */
4088
0, /* tp_as_number */
4089
0, /* tp_as_sequence */
4090
0, /* tp_as_mapping */
4094
0, /* tp_getattro */
4095
0, /* tp_setattro */
4096
&PyCData_as_buffer, /* tp_as_buffer */
4097
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4098
"Union base class", /* tp_doc */
4099
(traverseproc)PyCData_traverse, /* tp_traverse */
4100
(inquiry)PyCData_clear, /* tp_clear */
4101
0, /* tp_richcompare */
4102
0, /* tp_weaklistoffset */
4104
0, /* tp_iternext */
4110
0, /* tp_descr_get */
4111
0, /* tp_descr_set */
4112
0, /* tp_dictoffset */
4113
Struct_init, /* tp_init */
4115
GenericPyCData_new, /* tp_new */
4078
PyVarObject_HEAD_INIT(NULL, 0)
4080
sizeof(CDataObject), /* tp_basicsize */
4081
0, /* tp_itemsize */
4086
0, /* tp_reserved */
4088
0, /* tp_as_number */
4089
0, /* tp_as_sequence */
4090
0, /* tp_as_mapping */
4094
0, /* tp_getattro */
4095
0, /* tp_setattro */
4096
&PyCData_as_buffer, /* tp_as_buffer */
4097
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4098
"Union base class", /* tp_doc */
4099
(traverseproc)PyCData_traverse, /* tp_traverse */
4100
(inquiry)PyCData_clear, /* tp_clear */
4101
0, /* tp_richcompare */
4102
0, /* tp_weaklistoffset */
4104
0, /* tp_iternext */
4110
0, /* tp_descr_get */
4111
0, /* tp_descr_set */
4112
0, /* tp_dictoffset */
4113
Struct_init, /* tp_init */
4115
GenericPyCData_new, /* tp_new */
4120
4120
/******************************************************************/
4125
4125
Array_init(CDataObject *self, PyObject *args, PyObject *kw)
4130
if (!PyTuple_Check(args)) {
4131
PyErr_SetString(PyExc_TypeError,
4132
"args not a tuple?");
4135
n = PyTuple_GET_SIZE(args);
4136
for (i = 0; i < n; ++i) {
4138
v = PyTuple_GET_ITEM(args, i);
4139
if (-1 == PySequence_SetItem((PyObject *)self, i, v))
4130
if (!PyTuple_Check(args)) {
4131
PyErr_SetString(PyExc_TypeError,
4132
"args not a tuple?");
4135
n = PyTuple_GET_SIZE(args);
4136
for (i = 0; i < n; ++i) {
4138
v = PyTuple_GET_ITEM(args, i);
4139
if (-1 == PySequence_SetItem((PyObject *)self, i, v))
4145
4145
static PyObject *
4146
4146
Array_item(PyObject *_self, Py_ssize_t index)
4148
CDataObject *self = (CDataObject *)_self;
4149
Py_ssize_t offset, size;
4150
StgDictObject *stgdict;
4153
if (index < 0 || index >= self->b_length) {
4154
PyErr_SetString(PyExc_IndexError,
4159
stgdict = PyObject_stgdict((PyObject *)self);
4160
assert(stgdict); /* Cannot be NULL for array instances */
4161
/* Would it be clearer if we got the item size from
4162
stgdict->proto's stgdict?
4164
size = stgdict->size / stgdict->length;
4165
offset = index * size;
4167
return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self,
4168
index, size, self->b_ptr + offset);
4148
CDataObject *self = (CDataObject *)_self;
4149
Py_ssize_t offset, size;
4150
StgDictObject *stgdict;
4153
if (index < 0 || index >= self->b_length) {
4154
PyErr_SetString(PyExc_IndexError,
4159
stgdict = PyObject_stgdict((PyObject *)self);
4160
assert(stgdict); /* Cannot be NULL for array instances */
4161
/* Would it be clearer if we got the item size from
4162
stgdict->proto's stgdict?
4164
size = stgdict->size / stgdict->length;
4165
offset = index * size;
4167
return PyCData_get(stgdict->proto, stgdict->getfunc, (PyObject *)self,
4168
index, size, self->b_ptr + offset);
4171
4171
static PyObject *
4172
4172
Array_subscript(PyObject *_self, PyObject *item)
4174
CDataObject *self = (CDataObject *)_self;
4176
if (PyIndex_Check(item)) {
4177
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4179
if (i == -1 && PyErr_Occurred())
4182
i += self->b_length;
4183
return Array_item(_self, i);
4185
else if PySlice_Check(item) {
4186
StgDictObject *stgdict, *itemdict;
4189
Py_ssize_t start, stop, step, slicelen, cur, i;
4191
if (PySlice_GetIndicesEx((PySliceObject *)item,
4192
self->b_length, &start, &stop,
4193
&step, &slicelen) < 0) {
4197
stgdict = PyObject_stgdict((PyObject *)self);
4198
assert(stgdict); /* Cannot be NULL for array object instances */
4199
proto = stgdict->proto;
4200
itemdict = PyType_stgdict(proto);
4201
assert(itemdict); /* proto is the item type of the array, a
4202
ctypes type, so this cannot be NULL */
4204
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
4205
char *ptr = (char *)self->b_ptr;
4209
return PyBytes_FromStringAndSize("", 0);
4211
return PyBytes_FromStringAndSize(ptr + start,
4214
dest = (char *)PyMem_Malloc(slicelen);
4217
return PyErr_NoMemory();
4219
for (cur = start, i = 0; i < slicelen;
4224
np = PyBytes_FromStringAndSize(dest, slicelen);
4174
CDataObject *self = (CDataObject *)_self;
4176
if (PyIndex_Check(item)) {
4177
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4179
if (i == -1 && PyErr_Occurred())
4182
i += self->b_length;
4183
return Array_item(_self, i);
4185
else if PySlice_Check(item) {
4186
StgDictObject *stgdict, *itemdict;
4189
Py_ssize_t start, stop, step, slicelen, cur, i;
4191
if (PySlice_GetIndicesEx((PySliceObject *)item,
4192
self->b_length, &start, &stop,
4193
&step, &slicelen) < 0) {
4197
stgdict = PyObject_stgdict((PyObject *)self);
4198
assert(stgdict); /* Cannot be NULL for array object instances */
4199
proto = stgdict->proto;
4200
itemdict = PyType_stgdict(proto);
4201
assert(itemdict); /* proto is the item type of the array, a
4202
ctypes type, so this cannot be NULL */
4204
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
4205
char *ptr = (char *)self->b_ptr;
4209
return PyBytes_FromStringAndSize("", 0);
4211
return PyBytes_FromStringAndSize(ptr + start,
4214
dest = (char *)PyMem_Malloc(slicelen);
4217
return PyErr_NoMemory();
4219
for (cur = start, i = 0; i < slicelen;
4224
np = PyBytes_FromStringAndSize(dest, slicelen);
4228
4228
#ifdef CTYPES_UNICODE
4229
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
4230
wchar_t *ptr = (wchar_t *)self->b_ptr;
4234
return PyUnicode_FromUnicode(NULL, 0);
4236
return PyUnicode_FromWideChar(ptr + start,
4240
dest = (wchar_t *)PyMem_Malloc(
4241
slicelen * sizeof(wchar_t));
4243
for (cur = start, i = 0; i < slicelen;
4248
np = PyUnicode_FromWideChar(dest, slicelen);
4229
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
4230
wchar_t *ptr = (wchar_t *)self->b_ptr;
4234
return PyUnicode_FromUnicode(NULL, 0);
4236
return PyUnicode_FromWideChar(ptr + start,
4240
dest = (wchar_t *)PyMem_Malloc(
4241
slicelen * sizeof(wchar_t));
4243
for (cur = start, i = 0; i < slicelen;
4248
np = PyUnicode_FromWideChar(dest, slicelen);
4254
np = PyList_New(slicelen);
4254
np = PyList_New(slicelen);
4258
for (cur = start, i = 0; i < slicelen;
4260
PyObject *v = Array_item(_self, cur);
4261
PyList_SET_ITEM(np, i, v);
4266
PyErr_SetString(PyExc_TypeError,
4267
"indices must be integers");
4258
for (cur = start, i = 0; i < slicelen;
4260
PyObject *v = Array_item(_self, cur);
4261
PyList_SET_ITEM(np, i, v);
4266
PyErr_SetString(PyExc_TypeError,
4267
"indices must be integers");
4274
4274
Array_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
4276
CDataObject *self = (CDataObject *)_self;
4277
Py_ssize_t size, offset;
4278
StgDictObject *stgdict;
4281
if (value == NULL) {
4282
PyErr_SetString(PyExc_TypeError,
4283
"Array does not support item deletion");
4287
stgdict = PyObject_stgdict((PyObject *)self);
4288
assert(stgdict); /* Cannot be NULL for array object instances */
4289
if (index < 0 || index >= stgdict->length) {
4290
PyErr_SetString(PyExc_IndexError,
4294
size = stgdict->size / stgdict->length;
4295
offset = index * size;
4296
ptr = self->b_ptr + offset;
4298
return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value,
4276
CDataObject *self = (CDataObject *)_self;
4277
Py_ssize_t size, offset;
4278
StgDictObject *stgdict;
4281
if (value == NULL) {
4282
PyErr_SetString(PyExc_TypeError,
4283
"Array does not support item deletion");
4287
stgdict = PyObject_stgdict((PyObject *)self);
4288
assert(stgdict); /* Cannot be NULL for array object instances */
4289
if (index < 0 || index >= stgdict->length) {
4290
PyErr_SetString(PyExc_IndexError,
4294
size = stgdict->size / stgdict->length;
4295
offset = index * size;
4296
ptr = self->b_ptr + offset;
4298
return PyCData_set((PyObject *)self, stgdict->proto, stgdict->setfunc, value,
4303
4303
Array_ass_subscript(PyObject *_self, PyObject *item, PyObject *value)
4305
CDataObject *self = (CDataObject *)_self;
4307
if (value == NULL) {
4308
PyErr_SetString(PyExc_TypeError,
4309
"Array does not support item deletion");
4313
if (PyIndex_Check(item)) {
4314
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4316
if (i == -1 && PyErr_Occurred())
4319
i += self->b_length;
4320
return Array_ass_item(_self, i, value);
4322
else if (PySlice_Check(item)) {
4323
Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;
4325
if (PySlice_GetIndicesEx((PySliceObject *)item,
4326
self->b_length, &start, &stop,
4327
&step, &slicelen) < 0) {
4330
if ((step < 0 && start < stop) ||
4331
(step > 0 && start > stop))
4334
otherlen = PySequence_Length(value);
4335
if (otherlen != slicelen) {
4336
PyErr_SetString(PyExc_ValueError,
4337
"Can only assign sequence of same size");
4340
for (cur = start, i = 0; i < otherlen; cur += step, i++) {
4341
PyObject *item = PySequence_GetItem(value, i);
4345
result = Array_ass_item(_self, cur, item);
4353
PyErr_SetString(PyExc_TypeError,
4354
"indices must be integer");
4305
CDataObject *self = (CDataObject *)_self;
4307
if (value == NULL) {
4308
PyErr_SetString(PyExc_TypeError,
4309
"Array does not support item deletion");
4313
if (PyIndex_Check(item)) {
4314
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4316
if (i == -1 && PyErr_Occurred())
4319
i += self->b_length;
4320
return Array_ass_item(_self, i, value);
4322
else if (PySlice_Check(item)) {
4323
Py_ssize_t start, stop, step, slicelen, otherlen, i, cur;
4325
if (PySlice_GetIndicesEx((PySliceObject *)item,
4326
self->b_length, &start, &stop,
4327
&step, &slicelen) < 0) {
4330
if ((step < 0 && start < stop) ||
4331
(step > 0 && start > stop))
4334
otherlen = PySequence_Length(value);
4335
if (otherlen != slicelen) {
4336
PyErr_SetString(PyExc_ValueError,
4337
"Can only assign sequence of same size");
4340
for (cur = start, i = 0; i < otherlen; cur += step, i++) {
4341
PyObject *item = PySequence_GetItem(value, i);
4345
result = Array_ass_item(_self, cur, item);
4353
PyErr_SetString(PyExc_TypeError,
4354
"indices must be integer");
4359
4359
static Py_ssize_t
4360
4360
Array_length(PyObject *_self)
4362
CDataObject *self = (CDataObject *)_self;
4363
return self->b_length;
4362
CDataObject *self = (CDataObject *)_self;
4363
return self->b_length;
4366
4366
static PySequenceMethods Array_as_sequence = {
4367
Array_length, /* sq_length; */
4370
Array_item, /* sq_item; */
4372
Array_ass_item, /* sq_ass_item; */
4373
0, /* sq_ass_slice; */
4374
0, /* sq_contains; */
4376
0, /* sq_inplace_concat; */
4377
0, /* sq_inplace_repeat; */
4367
Array_length, /* sq_length; */
4370
Array_item, /* sq_item; */
4372
Array_ass_item, /* sq_ass_item; */
4373
0, /* sq_ass_slice; */
4374
0, /* sq_contains; */
4376
0, /* sq_inplace_concat; */
4377
0, /* sq_inplace_repeat; */
4380
4380
static PyMappingMethods Array_as_mapping = {
4383
Array_ass_subscript,
4383
Array_ass_subscript,
4386
4386
PyTypeObject PyCArray_Type = {
4387
PyVarObject_HEAD_INIT(NULL, 0)
4389
sizeof(CDataObject), /* tp_basicsize */
4390
0, /* tp_itemsize */
4395
0, /* tp_reserved */
4397
0, /* tp_as_number */
4398
&Array_as_sequence, /* tp_as_sequence */
4399
&Array_as_mapping, /* tp_as_mapping */
4403
0, /* tp_getattro */
4404
0, /* tp_setattro */
4405
&PyCData_as_buffer, /* tp_as_buffer */
4406
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4407
"XXX to be provided", /* tp_doc */
4408
(traverseproc)PyCData_traverse, /* tp_traverse */
4409
(inquiry)PyCData_clear, /* tp_clear */
4410
0, /* tp_richcompare */
4411
0, /* tp_weaklistoffset */
4413
0, /* tp_iternext */
4419
0, /* tp_descr_get */
4420
0, /* tp_descr_set */
4421
0, /* tp_dictoffset */
4422
(initproc)Array_init, /* tp_init */
4424
GenericPyCData_new, /* tp_new */
4387
PyVarObject_HEAD_INIT(NULL, 0)
4389
sizeof(CDataObject), /* tp_basicsize */
4390
0, /* tp_itemsize */
4395
0, /* tp_reserved */
4397
0, /* tp_as_number */
4398
&Array_as_sequence, /* tp_as_sequence */
4399
&Array_as_mapping, /* tp_as_mapping */
4403
0, /* tp_getattro */
4404
0, /* tp_setattro */
4405
&PyCData_as_buffer, /* tp_as_buffer */
4406
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4407
"XXX to be provided", /* tp_doc */
4408
(traverseproc)PyCData_traverse, /* tp_traverse */
4409
(inquiry)PyCData_clear, /* tp_clear */
4410
0, /* tp_richcompare */
4411
0, /* tp_weaklistoffset */
4413
0, /* tp_iternext */
4419
0, /* tp_descr_get */
4420
0, /* tp_descr_set */
4421
0, /* tp_dictoffset */
4422
(initproc)Array_init, /* tp_init */
4424
GenericPyCData_new, /* tp_new */
4429
4429
PyCArrayType_from_ctype(PyObject *itemtype, Py_ssize_t length)
4431
static PyObject *cache;
4437
if (cache == NULL) {
4438
cache = PyDict_New();
4442
len = PyLong_FromSsize_t(length);
4445
key = PyTuple_Pack(2, itemtype, len);
4449
result = PyDict_GetItemProxy(cache, key);
4456
if (!PyType_Check(itemtype)) {
4457
PyErr_SetString(PyExc_TypeError,
4458
"Expected a type object");
4431
static PyObject *cache;
4437
if (cache == NULL) {
4438
cache = PyDict_New();
4442
len = PyLong_FromSsize_t(length);
4445
key = PyTuple_Pack(2, itemtype, len);
4449
result = PyDict_GetItemProxy(cache, key);
4456
if (!PyType_Check(itemtype)) {
4457
PyErr_SetString(PyExc_TypeError,
4458
"Expected a type object");
4461
4461
#ifdef MS_WIN64
4462
sprintf(name, "%.200s_Array_%Id",
4463
((PyTypeObject *)itemtype)->tp_name, length);
4462
sprintf(name, "%.200s_Array_%Id",
4463
((PyTypeObject *)itemtype)->tp_name, length);
4465
sprintf(name, "%.200s_Array_%ld",
4466
((PyTypeObject *)itemtype)->tp_name, (long)length);
4465
sprintf(name, "%.200s_Array_%ld",
4466
((PyTypeObject *)itemtype)->tp_name, (long)length);
4469
result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type,
4478
if (result == NULL) {
4482
if (-1 == PyDict_SetItemProxy(cache, key, result)) {
4469
result = PyObject_CallFunction((PyObject *)&PyCArrayType_Type,
4478
if (result == NULL) {
4482
if (-1 == PyDict_SetItemProxy(cache, key, result)) {
4492
4492
/******************************************************************/
4498
4498
Simple_set_value(CDataObject *self, PyObject *value)
4501
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
4503
if (value == NULL) {
4504
PyErr_SetString(PyExc_TypeError,
4505
"can't delete attribute");
4508
assert(dict); /* Cannot be NULL for CDataObject instances */
4509
assert(dict->setfunc);
4510
result = dict->setfunc(self->b_ptr, value, dict->size);
4514
/* consumes the refcount the setfunc returns */
4515
return KeepRef(self, 0, result);
4501
StgDictObject *dict = PyObject_stgdict((PyObject *)self);
4503
if (value == NULL) {
4504
PyErr_SetString(PyExc_TypeError,
4505
"can't delete attribute");
4508
assert(dict); /* Cannot be NULL for CDataObject instances */
4509
assert(dict->setfunc);
4510
result = dict->setfunc(self->b_ptr, value, dict->size);
4514
/* consumes the refcount the setfunc returns */
4515
return KeepRef(self, 0, result);
4519
4519
Simple_init(CDataObject *self, PyObject *args, PyObject *kw)
4521
PyObject *value = NULL;
4522
if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value))
4525
return Simple_set_value(self, value);
4521
PyObject *value = NULL;
4522
if (!PyArg_UnpackTuple(args, "__init__", 0, 1, &value))
4525
return Simple_set_value(self, value);
4529
4529
static PyObject *
4530
4530
Simple_get_value(CDataObject *self)
4532
StgDictObject *dict;
4533
dict = PyObject_stgdict((PyObject *)self);
4534
assert(dict); /* Cannot be NULL for CDataObject instances */
4535
assert(dict->getfunc);
4536
return dict->getfunc(self->b_ptr, self->b_size);
4532
StgDictObject *dict;
4533
dict = PyObject_stgdict((PyObject *)self);
4534
assert(dict); /* Cannot be NULL for CDataObject instances */
4535
assert(dict->getfunc);
4536
return dict->getfunc(self->b_ptr, self->b_size);
4539
4539
static PyGetSetDef Simple_getsets[] = {
4540
{ "value", (getter)Simple_get_value, (setter)Simple_set_value,
4541
"current value", NULL },
4540
{ "value", (getter)Simple_get_value, (setter)Simple_set_value,
4541
"current value", NULL },
4545
4545
static PyObject *
4546
4546
Simple_from_outparm(PyObject *self, PyObject *args)
4548
if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) {
4552
/* call stgdict->getfunc */
4553
return Simple_get_value((CDataObject *)self);
4548
if (_ctypes_simple_instance((PyObject *)Py_TYPE(self))) {
4552
/* call stgdict->getfunc */
4553
return Simple_get_value((CDataObject *)self);
4556
4556
static PyMethodDef Simple_methods[] = {
4557
{ "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, },
4557
{ "__ctypes_from_outparam__", Simple_from_outparm, METH_NOARGS, },
4561
4561
static int Simple_bool(CDataObject *self)
4563
return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size);
4563
return memcmp(self->b_ptr, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", self->b_size);
4566
4566
static PyNumberMethods Simple_as_number = {
4568
0, /* nb_subtract */
4569
0, /* nb_multiply */
4570
0, /* nb_remainder */
4573
0, /* nb_negative */
4574
0, /* nb_positive */
4575
0, /* nb_absolute */
4576
(inquiry)Simple_bool, /* nb_bool */
4568
0, /* nb_subtract */
4569
0, /* nb_multiply */
4570
0, /* nb_remainder */
4573
0, /* nb_negative */
4574
0, /* nb_positive */
4575
0, /* nb_absolute */
4576
(inquiry)Simple_bool, /* nb_bool */
4579
4579
/* "%s(%s)" % (self.__class__.__name__, self.value) */
4580
4580
static PyObject *
4581
4581
Simple_repr(CDataObject *self)
4583
PyObject *val, *name, *args, *result;
4584
static PyObject *format;
4586
if (Py_TYPE(self)->tp_base != &Simple_Type) {
4587
return PyUnicode_FromFormat("<%s object at %p>",
4588
Py_TYPE(self)->tp_name, self);
4591
if (format == NULL) {
4592
format = PyUnicode_InternFromString("%s(%r)");
4597
val = Simple_get_value(self);
4601
name = PyUnicode_FromString(Py_TYPE(self)->tp_name);
4607
args = PyTuple_Pack(2, name, val);
4613
result = PyUnicode_Format(format, args);
4583
PyObject *val, *name, *args, *result;
4584
static PyObject *format;
4586
if (Py_TYPE(self)->tp_base != &Simple_Type) {
4587
return PyUnicode_FromFormat("<%s object at %p>",
4588
Py_TYPE(self)->tp_name, self);
4591
if (format == NULL) {
4592
format = PyUnicode_InternFromString("%s(%r)");
4597
val = Simple_get_value(self);
4601
name = PyUnicode_FromString(Py_TYPE(self)->tp_name);
4607
args = PyTuple_Pack(2, name, val);
4613
result = PyUnicode_Format(format, args);
4618
4618
static PyTypeObject Simple_Type = {
4619
PyVarObject_HEAD_INIT(NULL, 0)
4620
"_ctypes._SimpleCData",
4621
sizeof(CDataObject), /* tp_basicsize */
4622
0, /* tp_itemsize */
4627
0, /* tp_reserved */
4628
(reprfunc)&Simple_repr, /* tp_repr */
4629
&Simple_as_number, /* tp_as_number */
4630
0, /* tp_as_sequence */
4631
0, /* tp_as_mapping */
4635
0, /* tp_getattro */
4636
0, /* tp_setattro */
4637
&PyCData_as_buffer, /* tp_as_buffer */
4638
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4639
"XXX to be provided", /* tp_doc */
4640
(traverseproc)PyCData_traverse, /* tp_traverse */
4641
(inquiry)PyCData_clear, /* tp_clear */
4642
0, /* tp_richcompare */
4643
0, /* tp_weaklistoffset */
4645
0, /* tp_iternext */
4646
Simple_methods, /* tp_methods */
4648
Simple_getsets, /* tp_getset */
4651
0, /* tp_descr_get */
4652
0, /* tp_descr_set */
4653
0, /* tp_dictoffset */
4654
(initproc)Simple_init, /* tp_init */
4656
GenericPyCData_new, /* tp_new */
4619
PyVarObject_HEAD_INIT(NULL, 0)
4620
"_ctypes._SimpleCData",
4621
sizeof(CDataObject), /* tp_basicsize */
4622
0, /* tp_itemsize */
4627
0, /* tp_reserved */
4628
(reprfunc)&Simple_repr, /* tp_repr */
4629
&Simple_as_number, /* tp_as_number */
4630
0, /* tp_as_sequence */
4631
0, /* tp_as_mapping */
4635
0, /* tp_getattro */
4636
0, /* tp_setattro */
4637
&PyCData_as_buffer, /* tp_as_buffer */
4638
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
4639
"XXX to be provided", /* tp_doc */
4640
(traverseproc)PyCData_traverse, /* tp_traverse */
4641
(inquiry)PyCData_clear, /* tp_clear */
4642
0, /* tp_richcompare */
4643
0, /* tp_weaklistoffset */
4645
0, /* tp_iternext */
4646
Simple_methods, /* tp_methods */
4648
Simple_getsets, /* tp_getset */
4651
0, /* tp_descr_get */
4652
0, /* tp_descr_set */
4653
0, /* tp_dictoffset */
4654
(initproc)Simple_init, /* tp_init */
4656
GenericPyCData_new, /* tp_new */
4660
4660
/******************************************************************/
4662
4662
PyCPointer_Type
4664
4664
static PyObject *
4665
4665
Pointer_item(PyObject *_self, Py_ssize_t index)
4667
CDataObject *self = (CDataObject *)_self;
4670
StgDictObject *stgdict, *itemdict;
4673
if (*(void **)self->b_ptr == NULL) {
4674
PyErr_SetString(PyExc_ValueError,
4675
"NULL pointer access");
4679
stgdict = PyObject_stgdict((PyObject *)self);
4680
assert(stgdict); /* Cannot be NULL for pointer object instances */
4682
proto = stgdict->proto;
4684
itemdict = PyType_stgdict(proto);
4685
assert(itemdict); /* proto is the item type of the pointer, a ctypes
4686
type, so this cannot be NULL */
4688
size = itemdict->size;
4689
offset = index * itemdict->size;
4691
return PyCData_get(proto, stgdict->getfunc, (PyObject *)self,
4692
index, size, (*(char **)self->b_ptr) + offset);
4667
CDataObject *self = (CDataObject *)_self;
4670
StgDictObject *stgdict, *itemdict;
4673
if (*(void **)self->b_ptr == NULL) {
4674
PyErr_SetString(PyExc_ValueError,
4675
"NULL pointer access");
4679
stgdict = PyObject_stgdict((PyObject *)self);
4680
assert(stgdict); /* Cannot be NULL for pointer object instances */
4682
proto = stgdict->proto;
4684
itemdict = PyType_stgdict(proto);
4685
assert(itemdict); /* proto is the item type of the pointer, a ctypes
4686
type, so this cannot be NULL */
4688
size = itemdict->size;
4689
offset = index * itemdict->size;
4691
return PyCData_get(proto, stgdict->getfunc, (PyObject *)self,
4692
index, size, (*(char **)self->b_ptr) + offset);
4696
4696
Pointer_ass_item(PyObject *_self, Py_ssize_t index, PyObject *value)
4698
CDataObject *self = (CDataObject *)_self;
4701
StgDictObject *stgdict, *itemdict;
4704
if (value == NULL) {
4705
PyErr_SetString(PyExc_TypeError,
4706
"Pointer does not support item deletion");
4710
if (*(void **)self->b_ptr == NULL) {
4711
PyErr_SetString(PyExc_ValueError,
4712
"NULL pointer access");
4716
stgdict = PyObject_stgdict((PyObject *)self);
4717
assert(stgdict); /* Cannot be NULL fr pointer instances */
4719
proto = stgdict->proto;
4722
itemdict = PyType_stgdict(proto);
4723
assert(itemdict); /* Cannot be NULL because the itemtype of a pointer
4724
is always a ctypes type */
4726
size = itemdict->size;
4727
offset = index * itemdict->size;
4729
return PyCData_set((PyObject *)self, proto, stgdict->setfunc, value,
4730
index, size, (*(char **)self->b_ptr) + offset);
4698
CDataObject *self = (CDataObject *)_self;
4701
StgDictObject *stgdict, *itemdict;
4704
if (value == NULL) {
4705
PyErr_SetString(PyExc_TypeError,
4706
"Pointer does not support item deletion");
4710
if (*(void **)self->b_ptr == NULL) {
4711
PyErr_SetString(PyExc_ValueError,
4712
"NULL pointer access");
4716
stgdict = PyObject_stgdict((PyObject *)self);
4717
assert(stgdict); /* Cannot be NULL fr pointer instances */
4719
proto = stgdict->proto;
4722
itemdict = PyType_stgdict(proto);
4723
assert(itemdict); /* Cannot be NULL because the itemtype of a pointer
4724
is always a ctypes type */
4726
size = itemdict->size;
4727
offset = index * itemdict->size;
4729
return PyCData_set((PyObject *)self, proto, stgdict->setfunc, value,
4730
index, size, (*(char **)self->b_ptr) + offset);
4733
4733
static PyObject *
4734
4734
Pointer_get_contents(CDataObject *self, void *closure)
4736
StgDictObject *stgdict;
4738
if (*(void **)self->b_ptr == NULL) {
4739
PyErr_SetString(PyExc_ValueError,
4740
"NULL pointer access");
4744
stgdict = PyObject_stgdict((PyObject *)self);
4745
assert(stgdict); /* Cannot be NULL fr pointer instances */
4746
return PyCData_FromBaseObj(stgdict->proto,
4747
(PyObject *)self, 0,
4748
*(void **)self->b_ptr);
4736
StgDictObject *stgdict;
4738
if (*(void **)self->b_ptr == NULL) {
4739
PyErr_SetString(PyExc_ValueError,
4740
"NULL pointer access");
4744
stgdict = PyObject_stgdict((PyObject *)self);
4745
assert(stgdict); /* Cannot be NULL fr pointer instances */
4746
return PyCData_FromBaseObj(stgdict->proto,
4747
(PyObject *)self, 0,
4748
*(void **)self->b_ptr);
4752
4752
Pointer_set_contents(CDataObject *self, PyObject *value, void *closure)
4754
StgDictObject *stgdict;
4758
if (value == NULL) {
4759
PyErr_SetString(PyExc_TypeError,
4760
"Pointer does not support item deletion");
4763
stgdict = PyObject_stgdict((PyObject *)self);
4764
assert(stgdict); /* Cannot be NULL fr pointer instances */
4765
assert(stgdict->proto);
4766
if (!CDataObject_Check(value)
4767
|| 0 == PyObject_IsInstance(value, stgdict->proto)) {
4768
/* XXX PyObject_IsInstance could return -1! */
4769
PyErr_Format(PyExc_TypeError,
4770
"expected %s instead of %s",
4771
((PyTypeObject *)(stgdict->proto))->tp_name,
4772
Py_TYPE(value)->tp_name);
4776
dst = (CDataObject *)value;
4777
*(void **)self->b_ptr = dst->b_ptr;
4780
A Pointer instance must keep a the value it points to alive. So, a
4781
pointer instance has b_length set to 2 instead of 1, and we set
4782
'value' itself as the second item of the b_objects list, additionally.
4785
if (-1 == KeepRef(self, 1, value))
4788
keep = GetKeepedObjects(dst);
4790
return KeepRef(self, 0, keep);
4754
StgDictObject *stgdict;
4758
if (value == NULL) {
4759
PyErr_SetString(PyExc_TypeError,
4760
"Pointer does not support item deletion");
4763
stgdict = PyObject_stgdict((PyObject *)self);
4764
assert(stgdict); /* Cannot be NULL fr pointer instances */
4765
assert(stgdict->proto);
4766
if (!CDataObject_Check(value)
4767
|| 0 == PyObject_IsInstance(value, stgdict->proto)) {
4768
/* XXX PyObject_IsInstance could return -1! */
4769
PyErr_Format(PyExc_TypeError,
4770
"expected %s instead of %s",
4771
((PyTypeObject *)(stgdict->proto))->tp_name,
4772
Py_TYPE(value)->tp_name);
4776
dst = (CDataObject *)value;
4777
*(void **)self->b_ptr = dst->b_ptr;
4780
A Pointer instance must keep a the value it points to alive. So, a
4781
pointer instance has b_length set to 2 instead of 1, and we set
4782
'value' itself as the second item of the b_objects list, additionally.
4785
if (-1 == KeepRef(self, 1, value))
4788
keep = GetKeepedObjects(dst);
4790
return KeepRef(self, 0, keep);
4793
4793
static PyGetSetDef Pointer_getsets[] = {
4794
{ "contents", (getter)Pointer_get_contents,
4795
(setter)Pointer_set_contents,
4796
"the object this pointer points to (read-write)", NULL },
4794
{ "contents", (getter)Pointer_get_contents,
4795
(setter)Pointer_set_contents,
4796
"the object this pointer points to (read-write)", NULL },
4801
4801
Pointer_init(CDataObject *self, PyObject *args, PyObject *kw)
4803
PyObject *value = NULL;
4803
PyObject *value = NULL;
4805
if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value))
4809
return Pointer_set_contents(self, value, NULL);
4805
if (!PyArg_UnpackTuple(args, "POINTER", 0, 1, &value))
4809
return Pointer_set_contents(self, value, NULL);
4812
4812
static PyObject *
4813
4813
Pointer_new(PyTypeObject *type, PyObject *args, PyObject *kw)
4815
StgDictObject *dict = PyType_stgdict((PyObject *)type);
4816
if (!dict || !dict->proto) {
4817
PyErr_SetString(PyExc_TypeError,
4818
"Cannot create instance: has no _type_");
4821
return GenericPyCData_new(type, args, kw);
4815
StgDictObject *dict = PyType_stgdict((PyObject *)type);
4816
if (!dict || !dict->proto) {
4817
PyErr_SetString(PyExc_TypeError,
4818
"Cannot create instance: has no _type_");
4821
return GenericPyCData_new(type, args, kw);
4824
4824
static PyObject *
4825
4825
Pointer_subscript(PyObject *_self, PyObject *item)
4827
CDataObject *self = (CDataObject *)_self;
4828
if (PyIndex_Check(item)) {
4829
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4830
if (i == -1 && PyErr_Occurred())
4832
return Pointer_item(_self, i);
4834
else if (PySlice_Check(item)) {
4835
PySliceObject *slice = (PySliceObject *)item;
4836
Py_ssize_t start, stop, step;
4838
StgDictObject *stgdict, *itemdict;
4840
Py_ssize_t i, len, cur;
4842
/* Since pointers have no length, and we want to apply
4843
different semantics to negative indices than normal
4844
slicing, we have to dissect the slice object ourselves.*/
4845
if (slice->step == Py_None) {
4849
step = PyNumber_AsSsize_t(slice->step,
4851
if (step == -1 && PyErr_Occurred())
4854
PyErr_SetString(PyExc_ValueError,
4855
"slice step cannot be zero");
4859
if (slice->start == Py_None) {
4861
PyErr_SetString(PyExc_ValueError,
4862
"slice start is required "
4869
start = PyNumber_AsSsize_t(slice->start,
4871
if (start == -1 && PyErr_Occurred())
4874
if (slice->stop == Py_None) {
4875
PyErr_SetString(PyExc_ValueError,
4876
"slice stop is required");
4879
stop = PyNumber_AsSsize_t(slice->stop,
4881
if (stop == -1 && PyErr_Occurred())
4883
if ((step > 0 && start > stop) ||
4884
(step < 0 && start < stop))
4887
len = (stop - start - 1) / step + 1;
4889
len = (stop - start + 1) / step + 1;
4891
stgdict = PyObject_stgdict((PyObject *)self);
4892
assert(stgdict); /* Cannot be NULL for pointer instances */
4893
proto = stgdict->proto;
4895
itemdict = PyType_stgdict(proto);
4897
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
4898
char *ptr = *(char **)self->b_ptr;
4902
return PyBytes_FromStringAndSize("", 0);
4904
return PyBytes_FromStringAndSize(ptr + start,
4907
dest = (char *)PyMem_Malloc(len);
4909
return PyErr_NoMemory();
4910
for (cur = start, i = 0; i < len; cur += step, i++) {
4913
np = PyBytes_FromStringAndSize(dest, len);
4827
CDataObject *self = (CDataObject *)_self;
4828
if (PyIndex_Check(item)) {
4829
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
4830
if (i == -1 && PyErr_Occurred())
4832
return Pointer_item(_self, i);
4834
else if (PySlice_Check(item)) {
4835
PySliceObject *slice = (PySliceObject *)item;
4836
Py_ssize_t start, stop, step;
4838
StgDictObject *stgdict, *itemdict;
4840
Py_ssize_t i, len, cur;
4842
/* Since pointers have no length, and we want to apply
4843
different semantics to negative indices than normal
4844
slicing, we have to dissect the slice object ourselves.*/
4845
if (slice->step == Py_None) {
4849
step = PyNumber_AsSsize_t(slice->step,
4851
if (step == -1 && PyErr_Occurred())
4854
PyErr_SetString(PyExc_ValueError,
4855
"slice step cannot be zero");
4859
if (slice->start == Py_None) {
4861
PyErr_SetString(PyExc_ValueError,
4862
"slice start is required "
4869
start = PyNumber_AsSsize_t(slice->start,
4871
if (start == -1 && PyErr_Occurred())
4874
if (slice->stop == Py_None) {
4875
PyErr_SetString(PyExc_ValueError,
4876
"slice stop is required");
4879
stop = PyNumber_AsSsize_t(slice->stop,
4881
if (stop == -1 && PyErr_Occurred())
4883
if ((step > 0 && start > stop) ||
4884
(step < 0 && start < stop))
4887
len = (stop - start - 1) / step + 1;
4889
len = (stop - start + 1) / step + 1;
4891
stgdict = PyObject_stgdict((PyObject *)self);
4892
assert(stgdict); /* Cannot be NULL for pointer instances */
4893
proto = stgdict->proto;
4895
itemdict = PyType_stgdict(proto);
4897
if (itemdict->getfunc == _ctypes_get_fielddesc("c")->getfunc) {
4898
char *ptr = *(char **)self->b_ptr;
4902
return PyBytes_FromStringAndSize("", 0);
4904
return PyBytes_FromStringAndSize(ptr + start,
4907
dest = (char *)PyMem_Malloc(len);
4909
return PyErr_NoMemory();
4910
for (cur = start, i = 0; i < len; cur += step, i++) {
4913
np = PyBytes_FromStringAndSize(dest, len);
4917
4917
#ifdef CTYPES_UNICODE
4918
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
4919
wchar_t *ptr = *(wchar_t **)self->b_ptr;
4923
return PyUnicode_FromUnicode(NULL, 0);
4925
return PyUnicode_FromWideChar(ptr + start,
4928
dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t));
4930
return PyErr_NoMemory();
4931
for (cur = start, i = 0; i < len; cur += step, i++) {
4934
np = PyUnicode_FromWideChar(dest, len);
4918
if (itemdict->getfunc == _ctypes_get_fielddesc("u")->getfunc) {
4919
wchar_t *ptr = *(wchar_t **)self->b_ptr;
4923
return PyUnicode_FromUnicode(NULL, 0);
4925
return PyUnicode_FromWideChar(ptr + start,
4928
dest = (wchar_t *)PyMem_Malloc(len * sizeof(wchar_t));
4930
return PyErr_NoMemory();
4931
for (cur = start, i = 0; i < len; cur += step, i++) {
4934
np = PyUnicode_FromWideChar(dest, len);
4940
np = PyList_New(len);
4940
np = PyList_New(len);
4944
for (cur = start, i = 0; i < len; cur += step, i++) {
4945
PyObject *v = Pointer_item(_self, cur);
4946
PyList_SET_ITEM(np, i, v);
4951
PyErr_SetString(PyExc_TypeError,
4952
"Pointer indices must be integer");
4944
for (cur = start, i = 0; i < len; cur += step, i++) {
4945
PyObject *v = Pointer_item(_self, cur);
4946
PyList_SET_ITEM(np, i, v);
4951
PyErr_SetString(PyExc_TypeError,
4952
"Pointer indices must be integer");
4957
4957
static PySequenceMethods Pointer_as_sequence = {
4958
0, /* inquiry sq_length; */
4959
0, /* binaryfunc sq_concat; */
4960
0, /* intargfunc sq_repeat; */
4961
Pointer_item, /* intargfunc sq_item; */
4962
0, /* intintargfunc sq_slice; */
4963
Pointer_ass_item, /* intobjargproc sq_ass_item; */
4964
0, /* intintobjargproc sq_ass_slice; */
4965
0, /* objobjproc sq_contains; */
4966
/* Added in release 2.0 */
4967
0, /* binaryfunc sq_inplace_concat; */
4968
0, /* intargfunc sq_inplace_repeat; */
4958
0, /* inquiry sq_length; */
4959
0, /* binaryfunc sq_concat; */
4960
0, /* intargfunc sq_repeat; */
4961
Pointer_item, /* intargfunc sq_item; */
4962
0, /* intintargfunc sq_slice; */
4963
Pointer_ass_item, /* intobjargproc sq_ass_item; */
4964
0, /* intintobjargproc sq_ass_slice; */
4965
0, /* objobjproc sq_contains; */
4966
/* Added in release 2.0 */
4967
0, /* binaryfunc sq_inplace_concat; */
4968
0, /* intargfunc sq_inplace_repeat; */
4971
4971
static PyMappingMethods Pointer_as_mapping = {
4977
4977
Pointer_bool(CDataObject *self)
4979
return (*(void **)self->b_ptr != NULL);
4979
return (*(void **)self->b_ptr != NULL);
4982
4982
static PyNumberMethods Pointer_as_number = {
4984
0, /* nb_subtract */
4985
0, /* nb_multiply */
4986
0, /* nb_remainder */
4989
0, /* nb_negative */
4990
0, /* nb_positive */
4991
0, /* nb_absolute */
4992
(inquiry)Pointer_bool, /* nb_bool */
4984
0, /* nb_subtract */
4985
0, /* nb_multiply */
4986
0, /* nb_remainder */
4989
0, /* nb_negative */
4990
0, /* nb_positive */
4991
0, /* nb_absolute */
4992
(inquiry)Pointer_bool, /* nb_bool */
4995
4995
PyTypeObject PyCPointer_Type = {
4996
PyVarObject_HEAD_INIT(NULL, 0)
4998
sizeof(CDataObject), /* tp_basicsize */
4999
0, /* tp_itemsize */
5004
0, /* tp_reserved */
5006
&Pointer_as_number, /* tp_as_number */
5007
&Pointer_as_sequence, /* tp_as_sequence */
5008
&Pointer_as_mapping, /* tp_as_mapping */
5012
0, /* tp_getattro */
5013
0, /* tp_setattro */
5014
&PyCData_as_buffer, /* tp_as_buffer */
5015
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5016
"XXX to be provided", /* tp_doc */
5017
(traverseproc)PyCData_traverse, /* tp_traverse */
5018
(inquiry)PyCData_clear, /* tp_clear */
5019
0, /* tp_richcompare */
5020
0, /* tp_weaklistoffset */
5022
0, /* tp_iternext */
5025
Pointer_getsets, /* tp_getset */
5028
0, /* tp_descr_get */
5029
0, /* tp_descr_set */
5030
0, /* tp_dictoffset */
5031
(initproc)Pointer_init, /* tp_init */
5033
Pointer_new, /* tp_new */
4996
PyVarObject_HEAD_INIT(NULL, 0)
4998
sizeof(CDataObject), /* tp_basicsize */
4999
0, /* tp_itemsize */
5004
0, /* tp_reserved */
5006
&Pointer_as_number, /* tp_as_number */
5007
&Pointer_as_sequence, /* tp_as_sequence */
5008
&Pointer_as_mapping, /* tp_as_mapping */
5012
0, /* tp_getattro */
5013
0, /* tp_setattro */
5014
&PyCData_as_buffer, /* tp_as_buffer */
5015
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
5016
"XXX to be provided", /* tp_doc */
5017
(traverseproc)PyCData_traverse, /* tp_traverse */
5018
(inquiry)PyCData_clear, /* tp_clear */
5019
0, /* tp_richcompare */
5020
0, /* tp_weaklistoffset */
5022
0, /* tp_iternext */
5025
Pointer_getsets, /* tp_getset */
5028
0, /* tp_descr_get */
5029
0, /* tp_descr_set */
5030
0, /* tp_dictoffset */
5031
(initproc)Pointer_init, /* tp_init */
5033
Pointer_new, /* tp_new */
5038
5038
/******************************************************************/
5040
5040
* Module initialization.
5143
5143
static PyObject *
5144
5144
string_at(const char *ptr, int size)
5147
return PyBytes_FromStringAndSize(ptr, strlen(ptr));
5148
return PyBytes_FromStringAndSize(ptr, size);
5147
return PyBytes_FromStringAndSize(ptr, strlen(ptr));
5148
return PyBytes_FromStringAndSize(ptr, size);
5152
5152
cast_check_pointertype(PyObject *arg)
5154
StgDictObject *dict;
5154
StgDictObject *dict;
5156
if (PyCPointerTypeObject_Check(arg))
5158
if (PyCFuncPtrTypeObject_Check(arg))
5160
dict = PyType_stgdict(arg);
5162
if (PyUnicode_Check(dict->proto)
5163
&& (strchr("sPzUZXO", _PyUnicode_AsString(dict->proto)[0]))) {
5164
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
5168
PyErr_Format(PyExc_TypeError,
5169
"cast() argument 2 must be a pointer type, not %s",
5171
? ((PyTypeObject *)arg)->tp_name
5172
: Py_TYPE(arg)->tp_name);
5156
if (PyCPointerTypeObject_Check(arg))
5158
if (PyCFuncPtrTypeObject_Check(arg))
5160
dict = PyType_stgdict(arg);
5162
if (PyUnicode_Check(dict->proto)
5163
&& (strchr("sPzUZXO", _PyUnicode_AsString(dict->proto)[0]))) {
5164
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */
5168
PyErr_Format(PyExc_TypeError,
5169
"cast() argument 2 must be a pointer type, not %s",
5171
? ((PyTypeObject *)arg)->tp_name
5172
: Py_TYPE(arg)->tp_name);
5176
5176
static PyObject *
5177
5177
cast(void *ptr, PyObject *src, PyObject *ctype)
5179
CDataObject *result;
5180
if (0 == cast_check_pointertype(ctype))
5182
result = (CDataObject *)PyObject_CallFunctionObjArgs(ctype, NULL);
5187
The casted objects '_objects' member:
5189
It must certainly contain the source objects one.
5190
It must contain the source object itself.
5192
if (CDataObject_Check(src)) {
5193
CDataObject *obj = (CDataObject *)src;
5194
/* PyCData_GetContainer will initialize src.b_objects, we need
5195
this so it can be shared */
5196
PyCData_GetContainer(obj);
5197
/* But we need a dictionary! */
5198
if (obj->b_objects == Py_None) {
5200
obj->b_objects = PyDict_New();
5201
if (obj->b_objects == NULL)
5204
Py_XINCREF(obj->b_objects);
5205
result->b_objects = obj->b_objects;
5206
if (result->b_objects && PyDict_CheckExact(result->b_objects)) {
5209
index = PyLong_FromVoidPtr((void *)src);
5212
rc = PyDict_SetItem(result->b_objects, index, src);
5218
/* Should we assert that result is a pointer type? */
5219
memcpy(result->b_ptr, &ptr, sizeof(void *));
5220
return (PyObject *)result;
5179
CDataObject *result;
5180
if (0 == cast_check_pointertype(ctype))
5182
result = (CDataObject *)PyObject_CallFunctionObjArgs(ctype, NULL);
5187
The casted objects '_objects' member:
5189
It must certainly contain the source objects one.
5190
It must contain the source object itself.
5192
if (CDataObject_Check(src)) {
5193
CDataObject *obj = (CDataObject *)src;
5194
/* PyCData_GetContainer will initialize src.b_objects, we need
5195
this so it can be shared */
5196
PyCData_GetContainer(obj);
5197
/* But we need a dictionary! */
5198
if (obj->b_objects == Py_None) {
5200
obj->b_objects = PyDict_New();
5201
if (obj->b_objects == NULL)
5204
Py_XINCREF(obj->b_objects);
5205
result->b_objects = obj->b_objects;
5206
if (result->b_objects && PyDict_CheckExact(result->b_objects)) {
5209
index = PyLong_FromVoidPtr((void *)src);
5212
rc = PyDict_SetItem(result->b_objects, index, src);
5218
/* Should we assert that result is a pointer type? */
5219
memcpy(result->b_ptr, &ptr, sizeof(void *));
5220
return (PyObject *)result;
5227
5227
#ifdef CTYPES_UNICODE
5228
5228
static PyObject *
5229
5229
wstring_at(const wchar_t *ptr, int size)
5231
Py_ssize_t ssize = size;
5233
ssize = wcslen(ptr);
5234
return PyUnicode_FromWideChar(ptr, ssize);
5231
Py_ssize_t ssize = size;
5233
ssize = wcslen(ptr);
5234
return PyUnicode_FromWideChar(ptr, ssize);
5239
5239
static struct PyModuleDef _ctypesmodule = {
5240
PyModuleDef_HEAD_INIT,
5244
_ctypes_module_methods,
5240
PyModuleDef_HEAD_INIT,
5244
_ctypes_module_methods,
5252
5252
PyInit__ctypes(void)
5257
5257
ob_type is the metatype (the 'type'), defaults to PyType_Type,
5258
5258
tp_base is the base type, defaults to 'object' aka PyBaseObject_Type.
5260
5260
#ifdef WITH_THREAD
5261
PyEval_InitThreads();
5261
PyEval_InitThreads();
5263
m = PyModule_Create(&_ctypesmodule);
5267
_ctypes_ptrtype_cache = PyDict_New();
5268
if (_ctypes_ptrtype_cache == NULL)
5271
PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cache);
5273
_unpickle = PyObject_GetAttrString(m, "_unpickle");
5274
if (_unpickle == NULL)
5277
if (PyType_Ready(&PyCArg_Type) < 0)
5280
if (PyType_Ready(&PyCThunk_Type) < 0)
5283
/* StgDict is derived from PyDict_Type */
5284
PyCStgDict_Type.tp_base = &PyDict_Type;
5285
if (PyType_Ready(&PyCStgDict_Type) < 0)
5288
/*************************************************
5293
PyCStructType_Type.tp_base = &PyType_Type;
5294
if (PyType_Ready(&PyCStructType_Type) < 0)
5297
UnionType_Type.tp_base = &PyType_Type;
5298
if (PyType_Ready(&UnionType_Type) < 0)
5301
PyCPointerType_Type.tp_base = &PyType_Type;
5302
if (PyType_Ready(&PyCPointerType_Type) < 0)
5305
PyCArrayType_Type.tp_base = &PyType_Type;
5306
if (PyType_Ready(&PyCArrayType_Type) < 0)
5309
PyCSimpleType_Type.tp_base = &PyType_Type;
5310
if (PyType_Ready(&PyCSimpleType_Type) < 0)
5313
PyCFuncPtrType_Type.tp_base = &PyType_Type;
5314
if (PyType_Ready(&PyCFuncPtrType_Type) < 0)
5317
/*************************************************
5319
* Classes using a custom metaclass
5322
if (PyType_Ready(&PyCData_Type) < 0)
5325
Py_TYPE(&Struct_Type) = &PyCStructType_Type;
5326
Struct_Type.tp_base = &PyCData_Type;
5327
if (PyType_Ready(&Struct_Type) < 0)
5329
PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
5331
Py_TYPE(&Union_Type) = &UnionType_Type;
5332
Union_Type.tp_base = &PyCData_Type;
5333
if (PyType_Ready(&Union_Type) < 0)
5335
PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
5337
Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
5338
PyCPointer_Type.tp_base = &PyCData_Type;
5339
if (PyType_Ready(&PyCPointer_Type) < 0)
5341
PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
5343
Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
5344
PyCArray_Type.tp_base = &PyCData_Type;
5345
if (PyType_Ready(&PyCArray_Type) < 0)
5347
PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
5349
Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
5350
Simple_Type.tp_base = &PyCData_Type;
5351
if (PyType_Ready(&Simple_Type) < 0)
5353
PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
5355
Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
5356
PyCFuncPtr_Type.tp_base = &PyCData_Type;
5357
if (PyType_Ready(&PyCFuncPtr_Type) < 0)
5359
PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type);
5361
/*************************************************
5366
/* PyCField_Type is derived from PyBaseObject_Type */
5367
if (PyType_Ready(&PyCField_Type) < 0)
5370
/*************************************************
5375
DictRemover_Type.tp_new = PyType_GenericNew;
5376
if (PyType_Ready(&DictRemover_Type) < 0)
5263
m = PyModule_Create(&_ctypesmodule);
5267
_ctypes_ptrtype_cache = PyDict_New();
5268
if (_ctypes_ptrtype_cache == NULL)
5271
PyModule_AddObject(m, "_pointer_type_cache", (PyObject *)_ctypes_ptrtype_cache);
5273
_unpickle = PyObject_GetAttrString(m, "_unpickle");
5274
if (_unpickle == NULL)
5277
if (PyType_Ready(&PyCArg_Type) < 0)
5280
if (PyType_Ready(&PyCThunk_Type) < 0)
5283
/* StgDict is derived from PyDict_Type */
5284
PyCStgDict_Type.tp_base = &PyDict_Type;
5285
if (PyType_Ready(&PyCStgDict_Type) < 0)
5288
/*************************************************
5293
PyCStructType_Type.tp_base = &PyType_Type;
5294
if (PyType_Ready(&PyCStructType_Type) < 0)
5297
UnionType_Type.tp_base = &PyType_Type;
5298
if (PyType_Ready(&UnionType_Type) < 0)
5301
PyCPointerType_Type.tp_base = &PyType_Type;
5302
if (PyType_Ready(&PyCPointerType_Type) < 0)
5305
PyCArrayType_Type.tp_base = &PyType_Type;
5306
if (PyType_Ready(&PyCArrayType_Type) < 0)
5309
PyCSimpleType_Type.tp_base = &PyType_Type;
5310
if (PyType_Ready(&PyCSimpleType_Type) < 0)
5313
PyCFuncPtrType_Type.tp_base = &PyType_Type;
5314
if (PyType_Ready(&PyCFuncPtrType_Type) < 0)
5317
/*************************************************
5319
* Classes using a custom metaclass
5322
if (PyType_Ready(&PyCData_Type) < 0)
5325
Py_TYPE(&Struct_Type) = &PyCStructType_Type;
5326
Struct_Type.tp_base = &PyCData_Type;
5327
if (PyType_Ready(&Struct_Type) < 0)
5329
PyModule_AddObject(m, "Structure", (PyObject *)&Struct_Type);
5331
Py_TYPE(&Union_Type) = &UnionType_Type;
5332
Union_Type.tp_base = &PyCData_Type;
5333
if (PyType_Ready(&Union_Type) < 0)
5335
PyModule_AddObject(m, "Union", (PyObject *)&Union_Type);
5337
Py_TYPE(&PyCPointer_Type) = &PyCPointerType_Type;
5338
PyCPointer_Type.tp_base = &PyCData_Type;
5339
if (PyType_Ready(&PyCPointer_Type) < 0)
5341
PyModule_AddObject(m, "_Pointer", (PyObject *)&PyCPointer_Type);
5343
Py_TYPE(&PyCArray_Type) = &PyCArrayType_Type;
5344
PyCArray_Type.tp_base = &PyCData_Type;
5345
if (PyType_Ready(&PyCArray_Type) < 0)
5347
PyModule_AddObject(m, "Array", (PyObject *)&PyCArray_Type);
5349
Py_TYPE(&Simple_Type) = &PyCSimpleType_Type;
5350
Simple_Type.tp_base = &PyCData_Type;
5351
if (PyType_Ready(&Simple_Type) < 0)
5353
PyModule_AddObject(m, "_SimpleCData", (PyObject *)&Simple_Type);
5355
Py_TYPE(&PyCFuncPtr_Type) = &PyCFuncPtrType_Type;
5356
PyCFuncPtr_Type.tp_base = &PyCData_Type;
5357
if (PyType_Ready(&PyCFuncPtr_Type) < 0)
5359
PyModule_AddObject(m, "CFuncPtr", (PyObject *)&PyCFuncPtr_Type);
5361
/*************************************************
5366
/* PyCField_Type is derived from PyBaseObject_Type */
5367
if (PyType_Ready(&PyCField_Type) < 0)
5370
/*************************************************
5375
DictRemover_Type.tp_new = PyType_GenericNew;
5376
if (PyType_Ready(&DictRemover_Type) < 0)
5379
5379
#ifdef MS_WIN32
5380
if (create_comerror() < 0)
5382
PyModule_AddObject(m, "COMError", ComError);
5380
if (create_comerror() < 0)
5382
PyModule_AddObject(m, "COMError", ComError);
5384
PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT));
5385
PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL));
5384
PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyLong_FromLong(FUNCFLAG_HRESULT));
5385
PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyLong_FromLong(FUNCFLAG_STDCALL));
5387
PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL));
5388
PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO));
5389
PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR));
5390
PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI));
5391
PyModule_AddStringConstant(m, "__version__", "1.1.0");
5387
PyModule_AddObject(m, "FUNCFLAG_CDECL", PyLong_FromLong(FUNCFLAG_CDECL));
5388
PyModule_AddObject(m, "FUNCFLAG_USE_ERRNO", PyLong_FromLong(FUNCFLAG_USE_ERRNO));
5389
PyModule_AddObject(m, "FUNCFLAG_USE_LASTERROR", PyLong_FromLong(FUNCFLAG_USE_LASTERROR));
5390
PyModule_AddObject(m, "FUNCFLAG_PYTHONAPI", PyLong_FromLong(FUNCFLAG_PYTHONAPI));
5391
PyModule_AddStringConstant(m, "__version__", "1.1.0");
5393
PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
5394
PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
5395
PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at));
5396
PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast));
5393
PyModule_AddObject(m, "_memmove_addr", PyLong_FromVoidPtr(memmove));
5394
PyModule_AddObject(m, "_memset_addr", PyLong_FromVoidPtr(memset));
5395
PyModule_AddObject(m, "_string_at_addr", PyLong_FromVoidPtr(string_at));
5396
PyModule_AddObject(m, "_cast_addr", PyLong_FromVoidPtr(cast));
5397
5397
#ifdef CTYPES_UNICODE
5398
PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
5398
PyModule_AddObject(m, "_wstring_at_addr", PyLong_FromVoidPtr(wstring_at));
5401
5401
/* If RTLD_LOCAL is not defined (Windows!), set it to zero. */