3

In the following scenario, the object is not meant to be instantiated within Python (hence no tp_new or tp_init). Calling the ThingType as a function using PyObject_CallObject results in a segfault in _PyObject_FastCallDict, I believe because there are no constructors.

After creating the object using CreatePythonThing, the function get_height is not set unless a workaround is applied - to call dir(thing), which as a side effect initializes the object's properties. The workaround is already present in CreatePythonThing.

#include <Python.h>

typedef struct {
    PyObject_HEAD
    /* Type-specific fields go here. */
    Eval *eval;
} Thing;

static PyObject* ThingGetHeight(PyObject* self, PyObject* args)
{
    return PyLong_FromLong(1);
}

static PyMethodDef ThingMethods[] = {
    {"get_height", ThingGetHeight, METH_NOARGS, "Get thing height"},
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

static PyTypeObject ThingType = {
    PyVarObject_HEAD_INIT(NULL, 0)
    "thing.Thing",             /* tp_name */
    sizeof(Thing),             /* tp_basicsize */
    0,                         /* tp_itemsize */
    0,                         /* tp_dealloc */
    0,                         /* tp_print */
    0,                         /* tp_getattr */
    0,                         /* tp_setattr */
    0,                         /* tp_reserved */
    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 */
    "Thing",                   /* tp_doc */
    0, 0, 0, 0, 0, 0,
    ThingMethods,              /* tp_methods */
};


Thing* CreatePythonThing(Eval *eval)
{
    Thing* obj = PyObject_New(Thing, &ThingType);
    obj->eval = eval;
    obj = (Thing*) PyObject_Init((PyObject*) obj, &ThingType);
    // I don't understand why, but the above does not fully initialize the object. The method
    // table is not set. Calling `dir` on the object causes it to be initialized, so this is 
    // a workaround.
    PyObject_Dir((PyObject*) obj);
    return obj;
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Scott
  • 4,070
  • 3
  • 21
  • 16

1 Answers1

0

As in DavidW's comment, I was missing PyType_Ready(&ThingType); in my global initializer.

Scott
  • 4,070
  • 3
  • 21
  • 16