Skip to content

Commit df92d94

Browse files
committed
Fix conversion of ctypes pointers passed to C extension
As reported in issue #153 there appears to be a problem in converting a Python integer generated by ctypes.address() back to a void* pointer when naively using PyArg_ParseTuple. Don't fully understand why this issue arises, but the fix is straightforward. Fixes issue #153
1 parent f2330b6 commit df92d94

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

softioc/extension.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,12 @@ static PyObject *db_put_field(PyObject *self, PyObject *args)
9898
{
9999
const char *name;
100100
short dbrType;
101-
void *pbuffer;
101+
PyObject *buffer_ptr;
102102
long length;
103-
if (!PyArg_ParseTuple(args, "shnl", &name, &dbrType, &pbuffer, &length))
103+
if (!PyArg_ParseTuple(args, "shOl", &name, &dbrType, &buffer_ptr, &length))
104+
return NULL;
105+
void *pbuffer = PyLong_AsVoidPtr(buffer_ptr);
106+
if (!pbuffer)
104107
return NULL;
105108

106109
struct dbAddr dbAddr;
@@ -133,9 +136,12 @@ static PyObject *db_get_field(PyObject *self, PyObject *args)
133136
{
134137
const char *name;
135138
short dbrType;
136-
void *pbuffer;
139+
PyObject *buffer_ptr;
137140
long length;
138-
if (!PyArg_ParseTuple(args, "shnl", &name, &dbrType, &pbuffer, &length))
141+
if (!PyArg_ParseTuple(args, "shOl", &name, &dbrType, &buffer_ptr, &length))
142+
return NULL;
143+
void *pbuffer = PyLong_AsVoidPtr(buffer_ptr);
144+
if (!pbuffer)
139145
return NULL;
140146

141147
struct dbAddr dbAddr;

0 commit comments

Comments
 (0)