Add an optional 'offset' parameter to byref, defaultingto zero.

This commit is contained in:
Thomas Heller 2008-06-10 14:02:46 +00:00
parent 60977c73c5
commit dcea9a91c6
1 changed files with 16 additions and 4 deletions

View File

@ -1561,7 +1561,7 @@ align_func(PyObject *self, PyObject *obj)
} }
static char byref_doc[] = static char byref_doc[] =
"byref(C instance) -> byref-object\n" "byref(C instance[, offset=0]) -> byref-object\n"
"Return a pointer lookalike to a C instance, only usable\n" "Return a pointer lookalike to a C instance, only usable\n"
"as function argument"; "as function argument";
@ -1570,9 +1570,21 @@ static char byref_doc[] =
* but still has a reference to self. * but still has a reference to self.
*/ */
static PyObject * static PyObject *
byref(PyObject *self, PyObject *obj) byref(PyObject *self, PyObject *args)
{ {
PyCArgObject *parg; PyCArgObject *parg;
PyObject *obj;
PyObject *pyoffset = NULL;
Py_ssize_t offset = 0;
if (!PyArg_UnpackTuple(args, "byref", 1, 2,
&obj, &pyoffset))
return NULL;
if (pyoffset) {
offset = PyNumber_AsSsize_t(pyoffset, NULL);
if (offset == -1 && PyErr_Occurred())
return NULL;
}
if (!CDataObject_Check(obj)) { if (!CDataObject_Check(obj)) {
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"byref() argument must be a ctypes instance, not '%s'", "byref() argument must be a ctypes instance, not '%s'",
@ -1588,7 +1600,7 @@ byref(PyObject *self, PyObject *obj)
parg->pffi_type = &ffi_type_pointer; parg->pffi_type = &ffi_type_pointer;
Py_INCREF(obj); Py_INCREF(obj);
parg->obj = obj; parg->obj = obj;
parg->value.p = ((CDataObject *)obj)->b_ptr; parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset;
return (PyObject *)parg; return (PyObject *)parg;
} }
@ -1876,7 +1888,7 @@ PyMethodDef module_methods[] = {
#endif #endif
{"alignment", align_func, METH_O, alignment_doc}, {"alignment", align_func, METH_O, alignment_doc},
{"sizeof", sizeof_func, METH_O, sizeof_doc}, {"sizeof", sizeof_func, METH_O, sizeof_doc},
{"byref", byref, METH_O, byref_doc}, {"byref", byref, METH_VARARGS, byref_doc},
{"addressof", addressof, METH_O, addressof_doc}, {"addressof", addressof, METH_O, addressof_doc},
{"call_function", call_function, METH_VARARGS }, {"call_function", call_function, METH_VARARGS },
{"call_cdeclfunction", call_cdeclfunction, METH_VARARGS }, {"call_cdeclfunction", call_cdeclfunction, METH_VARARGS },