I'm learning how to write a native module in Python 3.7. I'm struggling on how to create and return a new object. It seems using it generates segmentation faults at random points, as I illustrate below.

I've seen a lot of questions that sort of answer this. They seem to suggest using PyObject_CallObject vs PyObject_New for native object creation. It seems PyObject_New does not initialize all field members in a new object, however details on this are scant.

Unfortunately a lot of references are either 1) 10+ years or older 2) Vague and unclear.

In the below example when I replace PyObject_New with PyObject_CallObject on the type test1_obj_type I get another segmentation fault.

 PyObject_CallObject( (PyObject*) &test1_obj_type, NULL);

What is the correct way to initialize a Python object in 2019? Again there are a lot of asked-and-answered questions on this topic, however none seem to provide functioning example of correct usage of PyObject_CallObject for object creation and initialization.

test.c

#include <Python.h>

typedef struct test1_obj
{
    PyObject_HEAD

} test1_obj;

static PyObject* create_obj()
{

    static PyTypeObject test1_obj_type = {
        PyObject_HEAD_INIT(NULL)
        .tp_name = "test1_obj",
        .tp_doc = "test1_obj",
        .tp_basicsize = sizeof(test1_obj),
        .tp_itemsize = 0,
        .tp_methods = NULL,
        .tp_flags = Py_TPFLAGS_DEFAULT,
        .tp_new = PyType_GenericNew,
    };

    return (PyObject*) PyObject_New(test1_obj, &test1_obj_type);

}

PyMODINIT_FUNC PyInit_libmiketest(void)
{
    PyObject *module;

    static PyMethodDef methods[] = {
        {"create_obj", create_obj, METH_NOARGS, "doc"},
        {NULL, NULL, 0, ""}
    };

    static struct PyModuleDef moduledef = 
    {
        PyModuleDef_HEAD_INIT,
        "create_obj",
        "",
        -1,
        methods,
        NULL,
        NULL,
        NULL,
        NULL
    };

    module = PyModule_Create(&moduledef);

    return module;
}

python

>>> import libmiketest
>>> a = libmiketest.create_obj()
>>> help(a)
Segmentation fault: 11

0 Answers