Skip to content

Commit 099b054

Browse files
author
thomas.heller
committed
Merged revisions 64070 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r64070 | thomas.heller | 2008-06-10 16:02:46 +0200 (Di, 10 Jun 2008) | 2 lines Add an optional 'offset' parameter to byref, defaultingto zero. ........ git-svn-id: http://svn.python.org/projects/python/branches/py3k@64075 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 835cad7 commit 099b054

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

Modules/_ctypes/callproc.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ align_func(PyObject *self, PyObject *obj)
15281528
}
15291529

15301530
static char byref_doc[] =
1531-
"byref(C instance) -> byref-object\n"
1531+
"byref(C instance[, offset=0]) -> byref-object\n"
15321532
"Return a pointer lookalike to a C instance, only usable\n"
15331533
"as function argument";
15341534

@@ -1537,9 +1537,21 @@ static char byref_doc[] =
15371537
* but still has a reference to self.
15381538
*/
15391539
static PyObject *
1540-
byref(PyObject *self, PyObject *obj)
1540+
byref(PyObject *self, PyObject *args)
15411541
{
15421542
PyCArgObject *parg;
1543+
PyObject *obj;
1544+
PyObject *pyoffset = NULL;
1545+
Py_ssize_t offset = 0;
1546+
1547+
if (!PyArg_UnpackTuple(args, "byref", 1, 2,
1548+
&obj, &pyoffset))
1549+
return NULL;
1550+
if (pyoffset) {
1551+
offset = PyNumber_AsSsize_t(pyoffset, NULL);
1552+
if (offset == -1 && PyErr_Occurred())
1553+
return NULL;
1554+
}
15431555
if (!CDataObject_Check(obj)) {
15441556
PyErr_Format(PyExc_TypeError,
15451557
"byref() argument must be a ctypes instance, not '%s'",
@@ -1555,7 +1567,7 @@ byref(PyObject *self, PyObject *obj)
15551567
parg->pffi_type = &ffi_type_pointer;
15561568
Py_INCREF(obj);
15571569
parg->obj = obj;
1558-
parg->value.p = ((CDataObject *)obj)->b_ptr;
1570+
parg->value.p = (char *)((CDataObject *)obj)->b_ptr + offset;
15591571
return (PyObject *)parg;
15601572
}
15611573

@@ -1835,7 +1847,7 @@ PyMethodDef module_methods[] = {
18351847
#endif
18361848
{"alignment", align_func, METH_O, alignment_doc},
18371849
{"sizeof", sizeof_func, METH_O, sizeof_doc},
1838-
{"byref", byref, METH_O, byref_doc},
1850+
{"byref", byref, METH_VARARGS, byref_doc},
18391851
{"addressof", addressof, METH_O, addressof_doc},
18401852
{"call_function", call_function, METH_VARARGS },
18411853
{"call_cdeclfunction", call_cdeclfunction, METH_VARARGS },

0 commit comments

Comments
 (0)