Код: Выделить всё
static PyObject *
scenarioToDict(PyObject *self)
{
/*
* Put all our fields into a "dictionary" object:
*/
PyObject *result = PyDict_New();
for (const auto *m = scenarioGetSet; m->name != NULL; m++) {
if (m->get == NULL)
continue;
PyDict_SetItemString(result,
m->name, m->get(self, m->closure));
}
for (const auto *m = self->ob_type->tp_members; m->name != NULL; m++) {
void *p = ((char *)self + m->offset);
switch (m->type) {
case T_DOUBLE:
PyDict_SetItemString(result, m->name,
PyFloat_FromDouble(*(double *)p));
continue;
case T_INT:
PyDict_SetItemString(result, m->name,
PyLong_FromLong(*(int *)p));
continue;
default:
PyErr_Format(PyExc_NotImplementedError,
"%s: field '%s' is of unexpected type %d.",
__func__, m->name, m->type);
result->ob_type->tp_free(result);
return NULL;
}
}
return result;
}
static PyObject *
scenarioRepr(PyObject *self)
{
PyObject *result = scenarioToDict(self);
/*
* Convert the dictionary object with all our fields
* into a string -- using its own "repr"
*/
return result->ob_type->tp_repr(result);
}
У меня есть подозрение, мне нужно увеличить ref-count , но где?
Подробнее здесь: https://stackoverflow.com/questions/797 ... ementation
Мобильная версия