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,
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_FromString(mlist[i].name));
|
1997-04-29 17:08:16 -03:00
|
|
|
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) {
|
2008-01-21 17:23:15 -04:00
|
|
|
case T_BOOL:
|
|
|
|
v = PyBool_FromLong(*(char*)addr);
|
|
|
|
break;
|
2001-09-20 17:46:19 -03:00
|
|
|
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;
|
2007-06-13 13:57:12 -03:00
|
|
|
case T_PYSSIZET:
|
|
|
|
v = PyInt_FromSsize_t(*(Py_ssize_t*)addr);
|
|
|
|
break;
|
2001-09-20 17:46:19 -03:00
|
|
|
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
|
2008-06-09 01:58:54 -03:00
|
|
|
v = PyString_FromString(*(char**)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_STRING_INPLACE:
|
2008-06-09 01:58:54 -03:00
|
|
|
v = PyString_FromString((char*)addr);
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
case T_CHAR:
|
2008-06-09 01:58:54 -03:00
|
|
|
v = PyString_FromStringAndSize((char*)addr, 1);
|
2001-09-20 17:46:19 -03:00
|
|
|
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
|
|
|
|
2007-08-05 00:23:31 -03:00
|
|
|
#define WARN(msg) \
|
|
|
|
do { \
|
|
|
|
if (PyErr_Warn(PyExc_RuntimeWarning, msg) < 0) \
|
|
|
|
return -1; \
|
|
|
|
} while (0)
|
|
|
|
|
2001-09-20 17:46:19 -03:00
|
|
|
int
|
|
|
|
PyMember_SetOne(char *addr, PyMemberDef *l, PyObject *v)
|
|
|
|
{
|
|
|
|
PyObject *oldv;
|
|
|
|
|
2009-12-30 15:41:48 -04:00
|
|
|
addr += l->offset;
|
|
|
|
|
2010-04-02 22:40:44 -03:00
|
|
|
if ((l->flags & READONLY))
|
2001-09-20 17:46:19 -03:00
|
|
|
{
|
|
|
|
PyErr_SetString(PyExc_TypeError, "readonly attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2007-11-22 07:21:16 -04:00
|
|
|
if ((l->flags & PY_WRITE_RESTRICTED) && PyEval_GetRestricted()) {
|
2001-09-20 17:46:19 -03:00
|
|
|
PyErr_SetString(PyExc_RuntimeError, "restricted attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-30 15:41:48 -04:00
|
|
|
if (v == NULL) {
|
|
|
|
if (l->type == T_OBJECT_EX) {
|
|
|
|
/* Check if the attribute is set. */
|
|
|
|
if (*(PyObject **)addr == NULL) {
|
|
|
|
PyErr_SetString(PyExc_AttributeError, l->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (l->type != T_OBJECT) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"can't delete numeric/char attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
|
|
|
switch (l->type) {
|
2008-01-21 17:23:15 -04:00
|
|
|
case T_BOOL:{
|
|
|
|
if (!PyBool_Check(v)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"attribute value type must be bool");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (v == Py_True)
|
|
|
|
*(char*)addr = (char) 1;
|
|
|
|
else
|
|
|
|
*(char*)addr = (char) 0;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_BYTE:{
|
2007-08-05 00:23:31 -03:00
|
|
|
long long_val = PyInt_AsLong(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2007-08-05 00:23:31 -03:00
|
|
|
*(char*)addr = (char)long_val;
|
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))
|
2007-08-05 00:23:31 -03:00
|
|
|
WARN("Truncation of value to char");
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_UBYTE:{
|
2007-08-05 00:23:31 -03:00
|
|
|
long long_val = PyInt_AsLong(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
*(unsigned char*)addr = (unsigned char)long_val;
|
2007-08-05 00:23:31 -03:00
|
|
|
if ((long_val > UCHAR_MAX) || (long_val < 0))
|
|
|
|
WARN("Truncation of value to unsigned char");
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
2006-10-27 03:16:31 -03:00
|
|
|
}
|
|
|
|
case T_SHORT:{
|
2007-08-05 00:23:31 -03:00
|
|
|
long long_val = PyInt_AsLong(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2006-10-27 03:16:31 -03:00
|
|
|
*(short*)addr = (short)long_val;
|
2007-08-05 00:23:31 -03:00
|
|
|
if ((long_val > SHRT_MAX) || (long_val < SHRT_MIN))
|
|
|
|
WARN("Truncation of value to short");
|
2006-10-27 03:16:31 -03:00
|
|
|
break;
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_USHORT:{
|
2007-08-05 00:23:31 -03:00
|
|
|
long long_val = PyInt_AsLong(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
*(unsigned short*)addr = (unsigned short)long_val;
|
2007-08-05 00:23:31 -03:00
|
|
|
if ((long_val > USHRT_MAX) || (long_val < 0))
|
|
|
|
WARN("Truncation of value to unsigned short");
|
2001-09-20 17:46:19 -03:00
|
|
|
break;
|
2006-10-27 03:16:31 -03:00
|
|
|
}
|
|
|
|
case T_INT:{
|
2007-08-05 00:23:31 -03:00
|
|
|
long long_val = PyInt_AsLong(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
if ((long_val == -1) && PyErr_Occurred())
|
2001-09-20 17:46:19 -03:00
|
|
|
return -1;
|
2006-10-27 03:16:31 -03:00
|
|
|
*(int *)addr = (int)long_val;
|
2007-08-05 00:23:31 -03:00
|
|
|
if ((long_val > INT_MAX) || (long_val < INT_MIN))
|
|
|
|
WARN("Truncation of value to int");
|
2006-10-27 03:16:31 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_UINT:{
|
2007-08-05 00:23:31 -03:00
|
|
|
unsigned long ulong_val = PyLong_AsUnsignedLong(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
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;
|
2007-08-05 00:23:31 -03:00
|
|
|
*(unsigned int *)addr = (unsigned int)ulong_val;
|
|
|
|
WARN("Writing negative value into unsigned field");
|
|
|
|
} else
|
|
|
|
*(unsigned int *)addr = (unsigned int)ulong_val;
|
2006-10-27 03:16:31 -03:00
|
|
|
if (ulong_val > UINT_MAX)
|
2007-08-05 00:23:31 -03:00
|
|
|
WARN("Truncation of value to unsigned int");
|
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);
|
2007-08-05 00:23:31 -03:00
|
|
|
if ((*(unsigned long*)addr == (unsigned int)-1)
|
|
|
|
&& PyErr_Occurred())
|
2006-10-27 03:16:31 -03:00
|
|
|
return -1;
|
2007-08-05 00:23:31 -03:00
|
|
|
WARN("Writing negative value into unsigned field");
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-06-13 13:57:12 -03:00
|
|
|
case T_PYSSIZET:{
|
|
|
|
*(Py_ssize_t*)addr = PyInt_AsSsize_t(v);
|
|
|
|
if ((*(Py_ssize_t*)addr == (Py_ssize_t)-1)
|
|
|
|
&& PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2006-10-27 03:16:31 -03:00
|
|
|
case T_FLOAT:{
|
2007-08-05 00:23:31 -03:00
|
|
|
double double_val = PyFloat_AsDouble(v);
|
2006-10-27 03:16:31 -03:00
|
|
|
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:
|
2008-06-09 01:58:54 -03:00
|
|
|
if (PyString_Check(v) && PyString_Size(v) == 1) {
|
|
|
|
*(char*)addr = PyString_AsString(v)[0];
|
2001-09-20 17:46:19 -03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_BadArgument();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
2010-04-02 22:40:44 -03:00
|
|
|
case T_STRING:
|
|
|
|
case T_STRING_INPLACE:
|
|
|
|
PyErr_SetString(PyExc_TypeError, "readonly attribute");
|
|
|
|
return -1;
|
2005-03-03 19:00:26 -04:00
|
|
|
#ifdef HAVE_LONG_LONG
|
2007-06-09 04:42:52 -03:00
|
|
|
case T_LONGLONG:{
|
|
|
|
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;
|
2007-06-09 04:42:52 -03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case T_ULONGLONG:{
|
|
|
|
unsigned PY_LONG_LONG value;
|
|
|
|
/* ??? PyLong_AsLongLong accepts an int, but PyLong_AsUnsignedLongLong
|
|
|
|
doesn't ??? */
|
|
|
|
if (PyLong_Check(v))
|
|
|
|
*(unsigned PY_LONG_LONG*)addr = value = PyLong_AsUnsignedLongLong(v);
|
|
|
|
else
|
|
|
|
*(unsigned PY_LONG_LONG*)addr = value = PyInt_AsLong(v);
|
|
|
|
if ((value == (unsigned PY_LONG_LONG)-1) && PyErr_Occurred())
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2005-03-03 19:00:26 -04:00
|
|
|
#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;
|
|
|
|
}
|