1991-02-19 08:39:46 -04:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
/* Map C struct members to Python object attributes */
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
#include "structmember.h"
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
static PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
listmembers(struct memberlist *mlist)
|
1991-10-20 17:24:14 -03:00
|
|
|
{
|
|
|
|
int i, n;
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
1991-10-20 17:24:14 -03:00
|
|
|
for (n = 0; mlist[n].name != NULL; n++)
|
|
|
|
;
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyList_New(n);
|
1991-10-20 17:24:14 -03:00
|
|
|
if (v != NULL) {
|
|
|
|
for (i = 0; i < n; i++)
|
1997-04-29 17:08:16 -03:00
|
|
|
PyList_SetItem(v, i,
|
|
|
|
PyString_FromString(mlist[i].name));
|
|
|
|
if (PyErr_Occurred()) {
|
|
|
|
Py_DECREF(v);
|
1991-10-20 17:24:14 -03:00
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyList_Sort(v);
|
1991-10-20 17:24:14 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *
|
2000-07-22 15:47:25 -03:00
|
|
|
PyMember_Get(char *addr, struct memberlist *mlist, char *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
struct memberlist *l;
|
|
|
|
|
1991-10-20 17:24:14 -03:00
|
|
|
if (strcmp(name, "__members__") == 0)
|
|
|
|
return listmembers(mlist);
|
1990-12-20 11:06:42 -04:00
|
|
|
for (l = mlist; l->name != NULL; l++) {
|
|
|
|
if (strcmp(l->name, name) == 0) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyObject *v;
|
2001-09-17 16:28:08 -03:00
|
|
|
if ((l->flags & READ_RESTRICTED) &&
|
|
|
|
PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"restricted attribute");
|
|
|
|
return NULL;
|
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
addr += l->offset;
|
|
|
|
switch (l->type) {
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_BYTE:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong((long)
|
1992-06-03 14:07:23 -03:00
|
|
|
(((*(char*)addr & 0xff)
|
|
|
|
^ 0x80) - 0x80));
|
|
|
|
break;
|
|
|
|
case T_UBYTE:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong((long) *(char*)addr & 0xff);
|
1992-06-03 14:07:23 -03:00
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_SHORT:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong((long) *(short*)addr);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_USHORT:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong((long)
|
1992-06-03 14:07:23 -03:00
|
|
|
*(unsigned short*)addr);
|
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_INT:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong((long) *(int*)addr);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_UINT:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong((long)
|
|
|
|
*(unsigned int*)addr);
|
1992-06-03 14:07:23 -03:00
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_LONG:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyInt_FromLong(*(long*)addr);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_ULONG:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyLong_FromDouble((double)
|
1992-06-03 14:07:23 -03:00
|
|
|
*(unsigned long*)addr);
|
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_FLOAT:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyFloat_FromDouble((double)*(float*)addr);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
|
|
|
case T_DOUBLE:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyFloat_FromDouble(*(double*)addr);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
|
|
|
case T_STRING:
|
|
|
|
if (*(char**)addr == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
v = Py_None;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromString(*(char**)addr);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1994-12-14 09:04:05 -04:00
|
|
|
case T_STRING_INPLACE:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromString((char*)addr);
|
1994-12-14 09:04:05 -04:00
|
|
|
break;
|
|
|
|
#ifdef macintosh
|
|
|
|
case T_PSTRING:
|
|
|
|
if (*(char**)addr == NULL) {
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_INCREF(Py_None);
|
|
|
|
v = Py_None;
|
1994-12-14 09:04:05 -04:00
|
|
|
}
|
|
|
|
else
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromStringAndSize(
|
|
|
|
(*(char**)addr)+1,
|
|
|
|
**(unsigned char**)addr);
|
1994-12-14 09:04:05 -04:00
|
|
|
break;
|
|
|
|
case T_PSTRING_INPLACE:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromStringAndSize(
|
|
|
|
((char*)addr)+1,
|
|
|
|
*(unsigned char*)addr);
|
1994-12-14 09:04:05 -04:00
|
|
|
break;
|
|
|
|
#endif /* macintosh */
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_CHAR:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = PyString_FromStringAndSize((char*)addr, 1);
|
1992-06-03 14:07:23 -03:00
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_OBJECT:
|
1997-04-29 17:08:16 -03:00
|
|
|
v = *(PyObject **)addr;
|
1990-12-20 11:06:42 -04:00
|
|
|
if (v == NULL)
|
1997-04-29 17:08:16 -03:00
|
|
|
v = Py_None;
|
|
|
|
Py_INCREF(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"bad memberlist type");
|
1990-12-20 11:06:42 -04:00
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2000-07-22 15:47:25 -03:00
|
|
|
PyMember_Set(char *addr, struct memberlist *mlist, char *name, PyObject *v)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
struct memberlist *l;
|
1998-05-20 19:25:32 -03:00
|
|
|
PyObject *oldv;
|
1990-12-20 11:06:42 -04:00
|
|
|
|
|
|
|
for (l = mlist; l->name != NULL; l++) {
|
|
|
|
if (strcmp(l->name, name) == 0) {
|
2001-09-17 16:28:08 -03:00
|
|
|
if ((l->flags & READONLY) || l->type == T_STRING
|
1994-12-14 09:04:05 -04:00
|
|
|
#ifdef macintosh
|
2001-09-17 16:28:08 -03:00
|
|
|
|| l->type == T_PSTRING
|
|
|
|
#endif
|
|
|
|
)
|
1997-04-29 17:08:16 -03:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"readonly attribute");
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
2001-09-17 16:28:08 -03:00
|
|
|
}
|
|
|
|
if ((l->flags & WRITE_RESTRICTED) &&
|
|
|
|
PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"restricted attribute");
|
|
|
|
return -1;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
1994-08-30 05:27:36 -03:00
|
|
|
if (v == NULL && l->type != T_OBJECT) {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1994-08-30 05:27:36 -03:00
|
|
|
"can't delete numeric/char attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
1990-12-20 11:06:42 -04:00
|
|
|
addr += l->offset;
|
|
|
|
switch (l->type) {
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_BYTE:
|
|
|
|
case T_UBYTE:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyInt_Check(v)) {
|
|
|
|
PyErr_BadArgument();
|
1992-06-03 14:07:23 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
*(char*)addr = (char) PyInt_AsLong(v);
|
1992-06-03 14:07:23 -03:00
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_SHORT:
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_USHORT:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyInt_Check(v)) {
|
|
|
|
PyErr_BadArgument();
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
*(short*)addr = (short) PyInt_AsLong(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_UINT:
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_INT:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyInt_Check(v)) {
|
|
|
|
PyErr_BadArgument();
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
*(int*)addr = (int) PyInt_AsLong(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
|
|
|
case T_LONG:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (!PyInt_Check(v)) {
|
|
|
|
PyErr_BadArgument();
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
*(long*)addr = PyInt_AsLong(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_ULONG:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyInt_Check(v))
|
|
|
|
*(long*)addr = PyInt_AsLong(v);
|
|
|
|
else if (PyLong_Check(v))
|
|
|
|
*(long*)addr = PyLong_AsLong(v);
|
1992-06-03 14:07:23 -03:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadArgument();
|
1992-06-03 14:07:23 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
case T_FLOAT:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyInt_Check(v))
|
1997-04-09 16:24:53 -03:00
|
|
|
*(float*)addr =
|
1997-04-29 17:08:16 -03:00
|
|
|
(float) PyInt_AsLong(v);
|
|
|
|
else if (PyFloat_Check(v))
|
|
|
|
*(float*)addr =
|
|
|
|
(float) PyFloat_AsDouble(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadArgument();
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_DOUBLE:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyInt_Check(v))
|
1997-04-09 16:24:53 -03:00
|
|
|
*(double*)addr =
|
1997-04-29 17:08:16 -03:00
|
|
|
(double) PyInt_AsLong(v);
|
|
|
|
else if (PyFloat_Check(v))
|
|
|
|
*(double*)addr = PyFloat_AsDouble(v);
|
1990-12-20 11:06:42 -04:00
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadArgument();
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_OBJECT:
|
1997-04-29 17:08:16 -03:00
|
|
|
Py_XINCREF(v);
|
1998-05-20 19:25:32 -03:00
|
|
|
oldv = *(PyObject **)addr;
|
1997-04-29 17:08:16 -03:00
|
|
|
*(PyObject **)addr = v;
|
1998-05-20 19:25:32 -03:00
|
|
|
Py_XDECREF(oldv);
|
1990-12-20 11:06:42 -04:00
|
|
|
break;
|
1992-06-03 14:07:23 -03:00
|
|
|
case T_CHAR:
|
1997-04-29 17:08:16 -03:00
|
|
|
if (PyString_Check(v) &&
|
|
|
|
PyString_Size(v) == 1) {
|
1992-06-03 14:07:23 -03:00
|
|
|
*(char*)addr =
|
1997-04-29 17:08:16 -03:00
|
|
|
PyString_AsString(v)[0];
|
1992-06-03 14:07:23 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_BadArgument();
|
1992-06-03 14:07:23 -03:00
|
|
|
return -1;
|
|
|
|
}
|
2001-01-27 02:20:08 -04:00
|
|
|
break;
|
1990-12-20 11:06:42 -04:00
|
|
|
default:
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_SystemError,
|
|
|
|
"bad memberlist type");
|
1990-12-20 11:06:42 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
1990-12-20 19:12:40 -04:00
|
|
|
return -1;
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|