Ich habe folgenden Code geschrieben um ein C Objekt fuer Python nutzbar zu machen sry ist etwas lang:
Code: Alles auswählen
typedef struct RoomInfoObject
{
PyObject_HEAD
int ID;
PyObject* name;
int src;
PyObject* srcname;
PyObject* descr;
int colf;
int texf;
} RoomInfoObject;
static PyMemberDef RoomInfo_members[] = {
{"ID", T_INT, offsetof(RoomInfoObject, ID), 0, "ID of the room"},
{"src", T_INT, offsetof(RoomInfoObject, src), 0, "src of the room"},
{"colf", T_INT, offsetof(RoomInfoObject, colf), 0, ""},
{"texf", T_INT, offsetof(RoomInfoObject, texf), 0, ""},
{NULL} /* Sentinel */
};
static void RoomInfo_dealloc(RoomInfoObject* self)
{
Py_XDECREF(self->name);
Py_XDECREF(self->srcname);
Py_XDECREF(self->descr);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject* RoomInfo_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
RoomInfoObject* self;
self= (RoomInfoObject *)type->tp_alloc(type, 0);
if (self != NULL)
{
self->name= PyString_FromString("");
if( self->name == NULL )
{
Py_DECREF( self );
return NULL ;
}
self->srcname= PyString_FromString("");
if( self->srcname == NULL )
{
Py_DECREF( self );
return NULL ;
}
self->descr= PyString_FromString("");
if( self->descr == NULL )
{
Py_DECREF( self );
return NULL ;
}
self->ID= 0;
self->src= 0;
self->colf= 0;
self->texf= 0;
}
return (PyObject *)self;
}
static int RoomInfo_init(RoomInfoObject* self, PyObject *args, PyObject *kwds)
{
PyObject *name=NULL, *srcname=NULL, *descr= NULL, *tmp;
//printf("asdf\n");
static char *kwlist[] = {"ID", "name", "src", "srcname" "descr",
"colf", "texf", NULL};
if (! PyArg_ParseTupleAndKeywords(args, kwds, "|iSiSSii", kwlist,
&self->ID,
&name,
&self->src,
&srcname,
&descr,
&self->colf,
&self->texf))
return -1;
printf("asdf\n");
if( name )
{
tmp= self->name;
Py_INCREF( name );
self->name= name;
Py_DECREF( tmp );
}
if( srcname )
{
tmp= self->srcname;
Py_INCREF( srcname );
self->srcname= srcname;
Py_DECREF(tmp);
}
if( descr )
{
tmp= self->descr;
Py_INCREF( descr );
self->descr= descr;
Py_DECREF(tmp);
}
return 0;
}
static int _RoomInfo_name_set(RoomInfoObject* self, PyObject* value, void* closure);
static PyObject* _RoomInfo_name_get(RoomInfoObject* self, void* closure);
static int _RoomInfo_srcname_set(RoomInfoObject* self, PyObject* value, void* closure);
static PyObject* _RoomInfo_srcname_get(RoomInfoObject* self, void* closure);
static int _RoomInfo_descr_set(RoomInfoObject* self, PyObject* value, void* closure);
static PyObject* _RoomInfo_descr_get(RoomInfoObject* self, void* closure);
static PyGetSetDef RoomInfo_getseters[]= {
{"name", (getter)_RoomInfo_name_get, (setter)_RoomInfo_name_set, NULL},
{"srcname", (getter)_RoomInfo_srcname_get, (setter)_RoomInfo_srcname_set, NULL},
{"descr", (getter)_RoomInfo_descr_get, (setter)_RoomInfo_descr_set, NULL},
{NULL} /* Sentinel */
};
static int _RoomInfo_name_set(RoomInfoObject* self, PyObject* value, void* closure)
{
if( value == NULL )
{
PyErr_SetString(PyExc_TypeError, "Cannot delete the 'name' attribute");
return -1;
}
if( !PyString_Check(value) )
{
PyErr_SetString(PyExc_TypeError,
"The first attribute value must be a string");
return -1;
}
Py_DECREF( self->name );
Py_INCREF(value);
self->name= value;
return 0;
}
static PyObject* _RoomInfo_name_get(RoomInfoObject* self, void* closure)
{
Py_INCREF(self->name);
return self->name;
}
static int _RoomInfo_srcname_set(RoomInfoObject* self, PyObject* value, void* closure)
{
if( value == NULL )
{
PyErr_SetString(PyExc_TypeError, "Cannot delete the 'name' attribute");
return -1;
}
if( !PyString_Check(value) )
{
PyErr_SetString(PyExc_TypeError,
"The first attribute value must be a string");
return -1;
}
Py_DECREF(self->srcname);
Py_INCREF(value);
self->srcname= value;
return 0;
}
static PyObject* _RoomInfo_srcname_get(RoomInfoObject* self, void* closure)
{
Py_INCREF(self->srcname);
return self->srcname;
}
static int _RoomInfo_descr_set(RoomInfoObject* self, PyObject* value, void* closure)
{
if( value == NULL )
{
PyErr_SetString(PyExc_TypeError, "Cannot delete the 'name' attribute");
return -1;
}
if( !PyString_Check(value) )
{
PyErr_SetString(PyExc_TypeError,
"The first attribute value must be a string");
return -1;
}
Py_DECREF(self->descr);
Py_INCREF(value);
self->descr= value;
return 0;
}
static PyObject* _RoomInfo_descr_get(RoomInfoObject* self, void* closure)
{
Py_INCREF( self->descr );
return self->descr ;
}
static PyTypeObject RoomInfoType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"RoomInfo", /*tp_name*/
sizeof(RoomInfoObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)RoomInfo_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"RoomInfo object", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
RoomInfo_members, /* tp_members */
RoomInfo_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)RoomInfo_init, /* tp_init */
0, /* tp_alloc */
RoomInfo_new, /* tp_new */
};
Code: Alles auswählen
Py_INCREF(&RoomInfoType);
PyModule_AddObject(m, "RoomInfo", (PyObject *)&RoomInfoType);
Core dump
Code: Alles auswählen
Exception: STATUS_ACCESS_VIOLATION at eip=6A870FED
eax=00000000 ebx=712C9000 ecx=00000889 edx=FFFFFFFF esi=610E3038 edi=00000000
ebp=0022EB78 esp=0022EB50 program=D:\cygwin\bin\python2.4.exe, pid 3972, thread main
cs=001B ds=0023 es=0023 fs=003B gs=0000 ss=0023
Stack trace:
Frame Function Args
0022EB78 6A870FED (712C9000, 610E3038, 00000000, 00000000)
0022EB98 6A871135 (712C9000, 610E3038, 00000000, 6A8A499B)
0022EBC8 6A858AD3 (712C9000, 0046E068, 00000000, 004DC36C)
0022EBE8 6A8D5B34 (00000000, 712C9000, 004DA8B8, 004DA8B8)
0022EC18 6A87023A (0046BBEC, 004DC36C, 00000000, 6A86E2CE)
0022EC38 6A84A189 (0046BBEC, 004DC36C, 00000000, 712C9000)
0022EC58 6A8A4A77 (0046BBEC, 004DC36C, 00000000, 6A841840)
0022ED08 6A8A1F61 (00519AB4, 00495C60, 00478824, 00478824)
0022ED88 6A8A384E (00495C60, 00478824, 00478824, 00000000)
0022EDB8 6A8A0DC9 (00495C60, 00478824, 00478824, 00000001)
0022EDD8 6A8D37EB (00461470, 6A92CB22, 00478824, 00478824)
0022EE48 6A8D29AE (610E2FD0, 6A92CB22, 0022EEF4, 6A8D3FC2)
0022EE78 6A8D2773 (610E2FD0, 6A92CB22, 0022EEF4, 00000000)
0022EEA8 6A8D26C2 (610E2FD0, 6A92CB22, 00000000, 0022EEF4)
0022EF08 6A8DA0B1 (00000000, 611576C0, 0022EFD8, 61004DD2)
0022EF18 00401087 (00000001, 611576C0, 00460090, 7C929AF0)
End of stack trace (more stack frames may be present)
Danke Martin