[ctypes-commit] ctypes/source _ctypes.c,1.150,1.151
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2004-08-18 12:23:29
|
Update of /cvsroot/ctypes/ctypes/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26057 Modified Files: _ctypes.c Log Message: Array and POINTER instance now support slicing, for POINTER only getslice is implemented (setslice is too dangerous, probably). Slices are accepted or returned as lists of the elements, except for character and unicode character pointer and arrays, where strings resp. unicode strings are used - much more convenient. Add some docstrings. Index: _ctypes.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/_ctypes.c,v retrieving revision 1.150 retrieving revision 1.151 diff -C2 -d -r1.150 -r1.151 *** _ctypes.c 22 Jul 2004 13:48:03 -0000 1.150 --- _ctypes.c 18 Aug 2004 12:23:18 -0000 1.151 *************** *** 183,186 **** --- 183,189 ---- } + static char from_address_doc[] = + "C.from_address(integer) -> C instance\naccess a C instance at the specified address"; + static PyObject * CDataType_from_address(PyObject *type, PyObject *value) *************** *** 196,199 **** --- 199,205 ---- } + static char in_dll_doc[] = + "C.in_dll(dll, name) -> C instance\naccess a C instance in a dll"; + static PyObject * CDataType_in_dll(PyObject *type, PyObject *args) *************** *** 289,298 **** static PyMethodDef CDataType_methods[] = { ! { "from_param", CDataType_from_param, METH_O, ! from_param_doc }, ! { "from_address", CDataType_from_address, METH_O, ! "create an instance from an address"}, ! { "in_dll", CDataType_in_dll, METH_VARARGS, ! "access an instance in a dll"}, { NULL, NULL }, }; --- 295,301 ---- static PyMethodDef CDataType_methods[] = { ! { "from_param", CDataType_from_param, METH_O, from_param_doc }, ! { "from_address", CDataType_from_address, METH_O, from_address_doc }, ! { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc }, { NULL, NULL }, }; *************** *** 537,546 **** static PyMethodDef PointerType_methods[] = { ! { "from_address", CDataType_from_address, METH_O, ! "create an instance from an address"}, ! { "in_dll", CDataType_in_dll, METH_VARARGS, ! "access an instance in a dll"}, ! { "from_param", (PyCFunction)PointerType_from_param, METH_O, ! from_param_doc}, { "set_type", (PyCFunction)PointerType_set_type, METH_O }, { NULL, NULL }, --- 540,546 ---- static PyMethodDef PointerType_methods[] = { ! { "from_address", CDataType_from_address, METH_O, from_address_doc }, ! { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc}, ! { "from_param", (PyCFunction)PointerType_from_param, METH_O, from_param_doc}, { "set_type", (PyCFunction)PointerType_set_type, METH_O }, { NULL, NULL }, *************** *** 1290,1299 **** static PyMethodDef SimpleType_methods[] = { ! { "from_param", SimpleType_from_param, METH_O, ! from_param_doc }, ! { "from_address", CDataType_from_address, METH_O, ! "create an instance from an address"}, ! { "in_dll", CDataType_in_dll, METH_VARARGS, ! "access an instance in a dll"}, { NULL, NULL }, }; --- 1290,1296 ---- static PyMethodDef SimpleType_methods[] = { ! { "from_param", SimpleType_from_param, METH_O, from_param_doc }, ! { "from_address", CDataType_from_address, METH_O, from_address_doc }, ! { "in_dll", CDataType_in_dll, METH_VARARGS, in_dll_doc}, { NULL, NULL }, }; *************** *** 2824,2831 **** } ! #ifdef CAN_SLICE ! static PyListObject * Array_slice(CDataObject *self, int ilow, int ihigh) { PyListObject *np; int i, len; --- 2821,2829 ---- } ! static PyObject * Array_slice(CDataObject *self, int ilow, int ihigh) { + StgDictObject *stgdict, *itemdict; + PyObject *proto; PyListObject *np; int i, len; *************** *** 2841,2844 **** --- 2839,2855 ---- len = ihigh - ilow; + stgdict = PyObject_stgdict((PyObject *)self); + proto = stgdict->proto; + itemdict = PyType_stgdict(proto); + if (itemdict->getfunc == getentry("c")->getfunc) { + char *ptr = (char *)self->b_ptr; + return PyString_FromStringAndSize(ptr + ilow, len); + #ifdef HAVE_USABLE_WCHAR_T + } else if (itemdict->getfunc == getentry("u")->getfunc) { + wchar_t *ptr = (wchar_t *)self->b_ptr; + return PyUnicode_FromWideChar(ptr + ilow, len); + #endif + } + np = (PyListObject *) PyList_New(len); if (np == NULL) *************** *** 2849,2855 **** PyList_SET_ITEM(np, i, v); } ! return np; } - #endif static int --- 2860,2865 ---- PyList_SET_ITEM(np, i, v); } ! return (PyObject *)np; } static int *************** *** 2880,2889 **** } - #ifdef CAN_SLICE static int Array_ass_slice(CDataObject *self, int ilow, int ihigh, PyObject *value) { int i, len; - PyObject *item; if (value == NULL) { --- 2890,2897 ---- *************** *** 2922,2926 **** return 0; } - #endif static int --- 2930,2933 ---- *************** *** 2935,2949 **** 0, /* sq_repeat; */ (intargfunc)Array_item, /* sq_item; */ - #ifdef CAN_SLICE (intintargfunc)Array_slice, /* sq_slice; */ - #else - 0, /* sq_slice; */ - #endif (intobjargproc)Array_ass_item, /* sq_ass_item; */ - #ifdef CAN_SLICE (intintobjargproc)Array_ass_slice, /* sq_ass_slice; */ - #else - 0, /* sq_ass_slice; */ - #endif 0, /* sq_contains; */ --- 2942,2948 ---- *************** *** 3353,3356 **** --- 3352,3393 ---- } + static PyObject * + Pointer_slice(CDataObject *self, int ilow, int ihigh) + { + PyListObject *np; + StgDictObject *stgdict, *itemdict; + PyObject *proto; + int i, len; + + if (ilow < 0) + ilow = 0; + if (ihigh < ilow) + ihigh = ilow; + len = ihigh - ilow; + + stgdict = PyObject_stgdict((PyObject *)self); + proto = stgdict->proto; + itemdict = PyType_stgdict(proto); + if (itemdict->getfunc == getentry("c")->getfunc) { + char *ptr = *(char **)self->b_ptr; + return PyString_FromStringAndSize(ptr + ilow, len); + #ifdef HAVE_USABLE_WCHAR_T + } else if (itemdict->getfunc == getentry("u")->getfunc) { + wchar_t *ptr = *(wchar_t **)self->b_ptr; + return PyUnicode_FromWideChar(ptr + ilow, len); + #endif + } + + np = (PyListObject *) PyList_New(len); + if (np == NULL) + return NULL; + + for (i = 0; i < len; i++) { + PyObject *v = Pointer_item(self, i+ilow); + PyList_SET_ITEM(np, i, v); + } + return (PyObject *)np; + } + static PySequenceMethods Pointer_as_sequence = { 0, /* inquiry sq_length; */ *************** *** 3358,3362 **** 0, /* intargfunc sq_repeat; */ (intargfunc)Pointer_item, /* intargfunc sq_item; */ ! 0, /* intintargfunc sq_slice; */ (intobjargproc)Pointer_ass_item, /* intobjargproc sq_ass_item; */ 0, /* intintobjargproc sq_ass_slice; */ --- 3395,3399 ---- 0, /* intargfunc sq_repeat; */ (intargfunc)Pointer_item, /* intargfunc sq_item; */ ! (intintargfunc)Pointer_slice, /* intintargfunc sq_slice; */ (intobjargproc)Pointer_ass_item, /* intobjargproc sq_ass_item; */ 0, /* intintobjargproc sq_ass_slice; */ |