[ctypes-commit] ctypes/source _ctypes.c,1.183,1.184
Brought to you by:
theller
From: Thomas H. <th...@us...> - 2004-11-25 09:01:43
|
Update of /cvsroot/ctypes/ctypes/source In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3763 Modified Files: _ctypes.c Log Message: Refactored the code to keep references to objects. Index: _ctypes.c =================================================================== RCS file: /cvsroot/ctypes/ctypes/source/_ctypes.c,v retrieving revision 1.183 retrieving revision 1.184 diff -C2 -d -r1.183 -r1.184 *** _ctypes.c 19 Nov 2004 11:07:23 -0000 1.183 --- _ctypes.c 25 Nov 2004 09:01:20 -0000 1.184 *************** *** 1581,1590 **** ! /******************************************************************/ ! /* ! CData_Type */ ! char basespec_string[] = "base specification"; /* --- 1581,1611 ---- ! /***************************************************************** ! * Code to keep needed objects alive */ + static int + CanKeepRef(CDataObject *target, int index) + { + PyObject *objects = CData_GetList(target); + if (!objects) + return -1; + if (index < 0 || PyList_Size(objects) <= index) { + PyErr_SetString(PyExc_IndexError, + "invalid index"); + return -1; + } + return 0; + } ! static int ! KeepRef(CDataObject *target, int index, PyObject *keep) ! { ! int result; ! PyObject *list = CData_GetList(target); ! result = PyList_SetItem(list, index, keep); ! if (result == -1) ! return -1; ! return 0; ! } /* *************** *** 1671,1674 **** --- 1692,1702 ---- } + /******************************************************************/ + /* + CData_Type + */ + + char basespec_string[] = "base specification"; + static int CData_traverse(CDataObject *self, visitproc visit, void *arg) *************** *** 1988,1992 **** { CDataObject *mem = (CDataObject *)dst; ! PyObject *objects, *result; if (!CDataObject_Check(dst)) { --- 2016,2020 ---- { CDataObject *mem = (CDataObject *)dst; ! PyObject *result; if (!CDataObject_Check(dst)) { *************** *** 1995,2006 **** return -1; } ! objects = CData_GetList(mem); ! if (!objects) ! return -1; ! if (index < 0 || PyList_Size(objects) <= index) { ! PyErr_SetString(PyExc_IndexError, ! "invalid index"); return -1; - } result = _CData_set(mem, type, setfunc, value, size, ptr); --- 2023,2030 ---- return -1; } ! ! /* Make sure KeepRef will not fail! */ ! if (-1 == CanKeepRef(mem, index)) return -1; result = _CData_set(mem, type, setfunc, value, size, ptr); *************** *** 2008,2012 **** return -1; ! return PyList_SetItem(objects, index, result); } --- 2032,2037 ---- return -1; ! /* KeepRef steals a refcount from it's last argument */ ! return KeepRef(mem, index, result); } *************** *** 2265,2269 **** CFuncPtrObject *self; void *handle; - PyObject *objects; if (!PyArg_ParseTuple(args, "sO", &name, &dll)) --- 2290,2293 ---- *************** *** 2304,2318 **** *(void **)self->b_ptr = address; ! objects = CData_GetList((CDataObject *)self); ! if (!objects) { ! Py_DECREF((PyObject *)self); ! return NULL; ! } ! ! if (-1 == PyList_SetItem(objects, 0, dll)) { Py_DECREF((PyObject *)self); return NULL; } ! Py_INCREF((PyObject *)dll); /* for PyList_SetItem above */ Py_INCREF(self); --- 2328,2336 ---- *(void **)self->b_ptr = address; ! if (-1 == KeepRef((CDataObject *)self, 0, dll)) { Py_DECREF((PyObject *)self); return NULL; } ! Py_INCREF((PyObject *)dll); /* for KeepRef above */ Py_INCREF(self); *************** *** 2355,2359 **** StgDictObject *dict; THUNK thunk; - PyObject *objects; if (kwds && PyDict_GetItemString(kwds, "_basespec_")) { --- 2373,2376 ---- *************** *** 2438,2447 **** *(void **)self->b_ptr = *(void **)thunk; - objects = CData_GetList((CDataObject *)self); - if (!objects) { - Py_DECREF((PyObject *)self); - return NULL; - } - /* We store ourself in self->b_objects[0], because the whole instance must be kept alive if stored in a structure field, for example. --- 2455,2458 ---- *************** *** 2450,2458 **** */ ! if (-1 == PyList_SetItem(objects, 0, (PyObject *)self)) { Py_DECREF((PyObject *)self); return NULL; } ! Py_INCREF((PyObject *)self); /* for PyList_SetItem above */ return (PyObject *)self; --- 2461,2469 ---- */ ! if (-1 == KeepRef((CDataObject *)self, 0, (PyObject *)self)) { Py_DECREF((PyObject *)self); return NULL; } ! Py_INCREF((PyObject *)self); /* for KeepRef above */ return (PyObject *)self; *************** *** 3136,3140 **** { PyObject *result; - PyObject *objects; StgDictObject *dict = PyObject_stgdict((PyObject *)self); --- 3147,3150 ---- *************** *** 3142,3152 **** if (!result) return -1; ! ! /* Keep the object alive */ ! objects = CData_GetList(self); ! if (!objects) ! return -1; /* Hm. Severe bug. What now? Undo all the above? */ ! /* setfunc returns a new reference, PyList_SetItem() consumes it */ ! return PyList_SetItem(objects, 0, result); /* index is always 0 */ } --- 3152,3158 ---- if (!result) return -1; ! ! /* consumes the refcount the setfunc returns */ ! return KeepRef(self, 0, result); } *************** *** 3330,3334 **** StgDictObject *stgdict; CDataObject *dst; ! PyObject *objects, *keep; if (value == NULL) { --- 3336,3340 ---- StgDictObject *stgdict; CDataObject *dst; ! PyObject *keep; if (value == NULL) { *************** *** 3351,3356 **** *(void **)self->b_ptr = dst->b_ptr; - objects = CData_GetList(self); - /* A Pointer instance must keep a the value it points to alive. So, a --- 3357,3360 ---- *************** *** 3359,3368 **** */ Py_INCREF(value); ! if (-1 == PyList_SetItem(objects, 1, value)) return -1; keep = CData_GetList(dst); Py_INCREF(keep); ! return PyList_SetItem(objects, 0, keep); } --- 3363,3372 ---- */ Py_INCREF(value); ! if (-1 == KeepRef(self, 1, value)) return -1; keep = CData_GetList(dst); Py_INCREF(keep); ! return KeepRef(self, 0, keep); } |