[ctypes-commit] ctypes/source callproc.c,1.127.2.23,1.127.2.24 _ctypes.c,1.226.2.46,1.226.2.47
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2006-02-09 20:23:18
|
Update of /cvsroot/ctypes/ctypes/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv28118 Modified Files: Tag: branch_1_0 callproc.c _ctypes.c Log Message: memmove and memset are now implemented by using foreign functions. Requires to extend c_void_p.from_param to accept int/long arguments as well. The addresses are exported by the _ctypes extension, restype and argtypes are set in ctypes/__init__.py. Index: callproc.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/callproc.c,v retrieving revision 1.127.2.23 retrieving revision 1.127.2.24 diff -C2 -d -r1.127.2.23 -r1.127.2.24 *** callproc.c 7 Feb 2006 20:20:04 -0000 1.127.2.23 --- callproc.c 9 Feb 2006 20:23:06 -0000 1.127.2.24 *************** *** 1479,1535 **** } - static char memmove_doc[] = - "memmove(dst, src, count) -> adress\n\ - \n\ - Copy count bytes from src to dst, return the dst address as integer.\n"; - - static PyObject * - c_memmove(PyObject *self, PyObject *args) - { - struct argument a_dst, a_src; - int size; - void *c_result; - PyObject *result = NULL; - PyObject *dst, *src; - - memset(&a_dst, 0, sizeof(struct argument)); - memset(&a_src, 0, sizeof(struct argument)); - if (!PyArg_ParseTuple(args, "OOi:memmove", &dst, &src, &size)) - return NULL; - if (-1 == ConvParam(dst, 1, &a_dst)) - goto done; - if (-1 == ConvParam(src, 2, &a_src)) - goto done; - c_result = memmove(a_dst.value.p, a_src.value.p, size); - result = PyLong_FromVoidPtr(c_result); - done: - Py_XDECREF(a_dst.keep); - Py_XDECREF(a_src.keep); - return result; - } - - static char memset_doc[] = - "memset(dst, c, count) -> adress\n\ - \n\ - Set count bytes starting at dst to c, return the dst address as integer.\n"; - static PyObject * - c_memset(PyObject *self, PyObject *args) - { - PyObject *dst, *result; - struct argument a_dst; - void *c_result; - int c, count; - - if (!PyArg_ParseTuple(args, "Oii:memset", &dst, &c, &count)) - return NULL; - memset(&a_dst, 0, sizeof(struct argument)); - if (-1 == ConvParam(dst, 1, &a_dst)) - return NULL; - c_result = memset(a_dst.value.p, c, count); - result = PyLong_FromVoidPtr(c_result); - Py_XDECREF(a_dst.keep); - return result; - } - static char string_at_doc[] = "string_at(addr[, size]) -> string\n\ --- 1479,1482 ---- *************** *** 1589,1594 **** PyMethodDef module_methods[] = { {"string_at", string_at, METH_VARARGS, string_at_doc}, - {"memmove", c_memmove, METH_VARARGS, memmove_doc}, - {"memset", c_memset, METH_VARARGS, memset_doc}, {"cast", cast, METH_VARARGS, cast_doc}, #ifdef CTYPES_UNICODE --- 1536,1539 ---- Index: _ctypes.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/_ctypes.c,v retrieving revision 1.226.2.46 retrieving revision 1.226.2.47 diff -C2 -d -r1.226.2.46 -r1.226.2.47 *** _ctypes.c 2 Feb 2006 20:38:22 -0000 1.226.2.46 --- _ctypes.c 9 Feb 2006 20:23:06 -0000 1.226.2.47 *************** *** 1148,1155 **** --- 1148,1173 ---- #endif + /* None */ if (value == Py_None) { Py_INCREF(Py_None); return Py_None; } + /* Should probably allow buffer interface as well */ + /* int, long */ + if (PyInt_Check(value) || PyLong_Check(value)) { + PyCArgObject *parg; + struct fielddesc *fd = getentry("P"); + + parg = new_CArgObject(); + parg->pffi_type = &ffi_type_pointer; + parg->tag = 'P'; + parg->obj = fd->setfunc(&parg->value, value, 0); + if (parg->obj == NULL) { + Py_DECREF(parg); + return NULL; + } + return (PyObject *)parg; + } + /* string */ if (PyString_Check(value)) { PyCArgObject *parg; *************** *** 1166,1169 **** --- 1184,1188 ---- return (PyObject *)parg; } + /* unicode */ if (PyUnicode_Check(value)) { PyCArgObject *parg; *************** *** 1180,1183 **** --- 1199,1203 ---- return (PyObject *)parg; } + /* c_void_p instance (or subclass) */ if (PyObject_IsInstance(value, type)) { /* c_void_p instances */ *************** *** 1185,1188 **** --- 1205,1209 ---- return value; } + /* ctypes array or pointer instance */ if (ArrayObject_Check(value) || PointerObject_Check(value)) { /* Any array or pointer is accepted */ *************** *** 1190,1193 **** --- 1211,1215 ---- return value; } + /* byref(...) */ if (PyCArg_CheckExact(value)) { /* byref(c_xxx()) */ *************** *** 1198,1201 **** --- 1220,1224 ---- } } + /* c_char_p, c_wchar_p */ stgd = PyObject_stgdict(value); if (stgd && CDataObject_Check(value) && stgd->proto && PyString_Check(stgd->proto)) { *************** *** 4417,4420 **** --- 4440,4446 ---- PyModule_AddObject(m, "COMError", ComError); + PyModule_AddObject(m, "memmove_addr", PyLong_FromVoidPtr(memmove)); + PyModule_AddObject(m, "memset_addr", PyLong_FromVoidPtr(memset)); + PyModule_AddObject(m, "FUNCFLAG_HRESULT", PyInt_FromLong(FUNCFLAG_HRESULT)); PyModule_AddObject(m, "FUNCFLAG_STDCALL", PyInt_FromLong(FUNCFLAG_STDCALL)); |