Sequence Protocol, assign item
Torsten Mohr
tmohr at s.netic.de
Fri Sep 3 12:42:01 EDT 2004
Hi,
thanks for that hint. I already tried to get some hints from the sources
but at another place.
A) How it is done in Objects/listobject.c:
static int
list_ass_item(PyListObject *a, int i, PyObject *v)
{
PyObject *old_value;
if (i < 0 || i >= a->ob_size) {
PyErr_SetString(PyExc_IndexError,
"list assignment index out of range");
return -1;
}
if (v == NULL)
return list_ass_slice(a, i, i+1, v);
Py_INCREF(v);
old_value = a->ob_item[i];
a->ob_item[i] = v;
Py_DECREF(old_value);
return 0;
}
B) How i do it:
static int pytypeseq_ass_item(PyObject* s, int nr, PyObject* val) {
int v;
pytype_obj* self = (pytype_obj*)s;
if(!PyArg_ParseTuple(val, "i", &v)) {
return 0;
}
self->msg.d[nr] = v;
/* if(nr > dlc && nr < 8) dlc = nr; */
return 0;
}
In part A some INCREF/DECREF stuff is done, but i think i don't need
this as the PyObjects are only temporarily.
In the test script i use this:
#! /usr/bin/python
from pytype import *
m = PyType();
m.setContent(17, 22, 13);
print m[0], m[1], m[2];
m[0] = 12;
print m[0], m[1], m[2];
When i do it like this, i get:
C:\home\vc++\type\python>qwe.py
Exception exceptions.SystemError: 'new style getargs format but argument \
is not a tuple' in 'garbage collection' ignored
Fatal Python error: unexpected exception during garbage collection
#abnormal program termination
When i delete the line "m[0] = 12;" i do NOT get this error.
I don't really know what this message means, i think i don't correctly
handle the internal data structures somehow.
Has anybody got a hint for me?
Thanks,
Torsten.
More information about the Python-list
mailing list