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 *
|
2005-12-24 02:03:06 -04:00
|
|
|
PyMember_Get(const char *addr, struct memberlist *mlist, const char *name)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
struct memberlist *l;
|
2001-09-20 17:46:19 -03:00
|
|
|
|
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) {
|
2001-09-20 17:46:19 -03:00
|
|
|
PyMemberDef copy;
|
|
|
|
copy.name = l->name;
|
|
|
|
copy.type = l->type;
|
|
|
|
copy.offset = l->offset;
|
|
|
|
copy.flags = l->flags;
|
|
|
|
copy.doc = NULL;
|
|
|
|
return PyMember_GetOne(addr, ©);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
}
|
1997-04-29 17:08:16 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError, name);
|
1990-12-20 11:06:42 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
PyObject *
|
2005-12-24 02:03:06 -04:00
|
|
|
PyMember_GetOne(const char *addr, PyMemberDef *l)
|
2001-09-20 17:46:19 -03:00
|
|
|
{
|
|
|
|
PyObject *v;
|
|
|
|
if ((l->flags & READ_RESTRICTED) &&
|
|
|
|
PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
addr += l->offset;
|
|
|
|
switch (l->type) {
|
|
|
|
case T_BYTE:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyInt_FromLong(*(char*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_UBYTE:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyLong_FromUnsignedLong(*(unsigned char*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_SHORT:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyInt_FromLong(*(short*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_USHORT:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyLong_FromUnsignedLong(*(unsigned short*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_INT:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyInt_FromLong(*(int*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_UINT:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyLong_FromUnsignedLong(*(unsigned int*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_LONG:
|
|
|
|
v = PyInt_FromLong(*(long*)addr);
|
|
|
|
break;
|
|
|
|
case T_ULONG:
|
2006-10-27 03:16:31 -03:00
|
|
|
v = PyLong_FromUnsignedLong(*(unsigned long*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_FLOAT:
|
|
|
|
v = PyFloat_FromDouble((double)*(float*)addr);
|
|
|
|
break;
|
|
|
|
case T_DOUBLE:
|
|
|
|
v = PyFloat_FromDouble(*(double*)addr);
|
|
|
|
break;
|
|
|
|
case T_STRING:
|
|
|
|
if (*(char**)addr == NULL) {
|
|
|
|
Py_INCREF(Py_None);
|
|
|
|
v = Py_None;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
v = PyString_FromString(*(char**)addr);
|
|
|
|
break;
|
|
|
|
case T_STRING_INPLACE:
|
|
|
|
v = PyString_FromString((char*)addr);
|
|
|
|
break;
|
|
|
|
case T_CHAR:
|
|
|
|
v = PyString_FromStringAndSize((char*)addr, 1);
|
|
|
|
break;
|
|
|
|
case T_OBJECT:
|
|
|
|
v = *(PyObject **)addr;
|
|
|
|
if (v == NULL)
|
|
|
|
v = Py_None;
|
|
|
|
Py_INCREF(v);
|
|
|
|
break;
|
2001-12-04 12:23:42 -04:00
|
|
|
case T_OBJECT_EX:
|
|
|
|
v = *(PyObject **)addr;
|
|
|
|
if (v == NULL)
|
|
|
|
PyErr_SetString(PyExc_AttributeError, l->name);
|
|
|
|
Py_XINCREF(v);
|
|
|
|
break;
|
2005-03-03 19:00:26 -04:00
|
|
|
#ifdef HAVE_LONG_LONG
|
|
|
|
case T_LONGLONG:
|
|
|
|
v = PyLong_FromLongLong(*(PY_LONG_LONG *)addr);
|
|
|
|
break;
|
|
|
|
case T_ULONGLONG:
|
|
|
|
v = PyLong_FromUnsignedLongLong(*(unsigned PY_LONG_LONG *)addr);
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_LONG_LONG */
|
2001-09-20 17:46:19 -03:00
|
|
|
default:
|
|
|
|
PyErr_SetString(PyExc_SystemError, "bad memberdescr type");
|
|
|
|
v = NULL;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
int
|
2005-12-24 02:03:06 -04:00
|
|
|
PyMember_Set(char *addr, struct memberlist *mlist, const char *name, PyObject *v)
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
|
|
|
struct memberlist *l;
|
2001-09-20 17:46:19 -03:00
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
for (l = mlist; l->name != NULL; l++) {
|
|
|
|
if (strcmp(l->name, name) == 0) {
|
2001-09-20 17:46:19 -03:00
|
|
|
PyMemberDef copy;
|
|
|
|
copy.name = l->name;
|
|
|
|
copy.type = l->type;
|
|
|
|
copy.offset = l->offset;
|
|
|
|
copy.flags = l->flags;
|
|
|
|
copy.doc = NULL;
|
|
|
|
return PyMember_SetOne(addr, ©, v);
|
1990-12-20 11:06:42 -04:00
|
|
|
}
|
|
|
|
}
|
2001-09-20 17:46:19 -03:00
|
|
|
|
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
|
|
|
}
|
2001-09-20 17:46:19 -03:00
|
|
|
|
|
|
|
int
|
|
|
|
PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
|
|
|
|
{
|
|
|
|
PyObject *oldv;
|
|
|
|
|
2003-11-19 21:44:59 -04:00
|
|
|
if ((l->flags & READONLY) || l->type == T_STRING)
|
2001-09-20 17:46:19 -03:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "readonly attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((l->flags & WRITE_RESTRICTED) && PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-12-04 12:23:42 -04:00
|
|
|
if (v == NULL && l->type != T_OBJECT_EX && l->type != T_OBJECT) {
|
2001-09-20 17:46:19 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"can't delete numeric/char attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addr += l->offset;
|
|
|
|
switch (l->type) {
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_BYTE:{
|
|
|
|
long long_val;
|
|
|
|
long_val = PyInt_AsLong(v);
|
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2006-10-27 03:16:31 -03:00
|
|
|
/* XXX: For compatibility, only warn about truncations
|
|
|
|
for now. */
|
|
|
|
if ((long_val > CHAR_MAX) || (long_val < CHAR_MIN))
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Truncation of value to char");
|
|
|
|
*(char*)addr = (char)long_val;
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_UBYTE:{
|
|
|
|
long long_val;
|
|
|
|
long_val = PyInt_AsLong(v);
|
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
if ((long_val > UCHAR_MAX) || (long_val < 0))
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Truncation of value to unsigned char");
|
|
|
|
*(unsigned char*)addr = (unsigned char)long_val;
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
2006-10-27 03:16:31 -03:00
|
|
|
}
|
|
|
|
case T_SHORT:{
|
|
|
|
long long_val;
|
|
|
|
long_val = PyInt_AsLong(v);
|
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN))
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Truncation of value to short");
|
|
|
|
*(short*)addr = (short)long_val;
|
|
|
|
break;
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_USHORT:{
|
|
|
|
long long_val;
|
|
|
|
long_val = PyInt_AsLong(v);
|
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
if ((long_val > USHRT_MAX) || (long_val < 0))
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Truncation of value to unsigned short");
|
|
|
|
*(unsigned short*)addr = (unsigned short)long_val;
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
2006-10-27 03:16:31 -03:00
|
|
|
}
|
|
|
|
case T_INT:{
|
|
|
|
long long_val;
|
|
|
|
long_val = PyInt_AsLong(v);
|
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val > INT_MAX) || (long_val < INT_MIN))
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Truncation of value to int");
|
|
|
|
*(int *)addr = (int)long_val;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_UINT:{
|
|
|
|
unsigned long ulong_val;
|
|
|
|
ulong_val = PyLong_AsUnsignedLong(v);
|
|
|
|
if ((ulong_val == (unsigned int)-1) && PyErr_Occurred()) {
|
|
|
|
/* XXX: For compatibility, accept negative int values
|
|
|
|
as well. */
|
|
|
|
PyErr_Clear();
|
|
|
|
ulong_val = PyLong_AsLong(v);
|
|
|
|
if ((ulong_val == (unsigned int)-1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Writing negative value into unsigned field");
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
if (ulong_val > UINT_MAX)
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Truncation of value to unsigned int");
|
|
|
|
*(unsigned int *)addr = (unsigned int)ulong_val;
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
2006-10-27 03:16:31 -03:00
|
|
|
}
|
|
|
|
case T_LONG:{
|
|
|
|
*(long*)addr = PyLong_AsLong(v);
|
|
|
|
if ((*(long*)addr == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2006-10-27 03:16:31 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_ULONG:{
|
|
|
|
*(unsigned long*)addr = PyLong_AsUnsignedLong(v);
|
|
|
|
if ((*(unsigned long*)addr == (unsigned long)-1)
|
|
|
|
&& PyErr_Occurred()) {
|
|
|
|
/* XXX: For compatibility, accept negative int values
|
|
|
|
as well. */
|
|
|
|
PyErr_Clear();
|
|
|
|
*(unsigned long*)addr = PyLong_AsLong(v);
|
|
|
|
if ((*(unsigned long*)addr == (unsigned int)-1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
PyErr_Warn(PyExc_RuntimeWarning, "Writing negative value into unsigned field");
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_FLOAT:{
|
|
|
|
double double_val;
|
|
|
|
double_val = PyFloat_AsDouble(v);
|
|
|
|
if ((double_val == -1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
*(float*)addr = (float)double_val;
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
2006-10-27 03:16:31 -03:00
|
|
|
}
|
2001-09-20 17:46:19 -03:00
|
|
|
case T_DOUBLE:
|
2006-10-27 03:16:31 -03:00
|
|
|
*(double*)addr = PyFloat_AsDouble(v);
|
|
|
|
if ((*(double*)addr == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
case T_OBJECT:
|
2001-12-04 12:23:42 -04:00
|
|
|
case T_OBJECT_EX:
|
2001-09-20 17:46:19 -03:00
|
|
|
Py_XINCREF(v);
|
|
|
|
oldv = *(PyObject **)addr;
|
|
|
|
*(PyObject **)addr = v;
|
|
|
|
Py_XDECREF(oldv);
|
|
|
|
break;
|
|
|
|
case T_CHAR:
|
|
|
|
if (PyString_Check(v) && PyString_Size(v) == 1) {
|
|
|
|
*(char*)addr = PyString_AsString(v)[0];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2005-03-03 19:00:26 -04:00
|
|
|
#ifdef HAVE_LONG_LONG
|
|
|
|
case T_LONGLONG:
|
|
|
|
if (!PyLong_Check(v)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return -1;
|
|
|
|
} else {
|
2006-04-28 02:28:54 -03:00
|
|
|
PY_LONG_LONG value;
|
|
|
|
*(PY_LONG_LONG*)addr = value = PyLong_AsLongLong(v);
|
|
|
|
if ((value == -1) && PyErr_Occurred()) {
|
2005-03-03 19:00:26 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_ULONGLONG:
|
|
|
|
if (!PyLong_Check(v)) {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return -1;
|
|
|
|
} else {
|
2006-04-28 02:28:54 -03:00
|
|
|
unsigned PY_LONG_LONG value;
|
|
|
|
*(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
|
|
|
|
if ((value == (unsigned PY_LONG_LONG)-1) &&
|
|
|
|
PyErr_Occurred()) {
|
2005-03-03 19:00:26 -04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
#endif /* HAVE_LONG_LONG */
|
2001-09-20 17:46:19 -03:00
|
|
|
default:
|
2001-10-16 13:51:56 -03:00
|
|
|
PyErr_Format(PyExc_SystemError,
|
|
|
|
"bad memberdescr type for %s", l->name);
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|