diff --git a/kitty/cursor.c b/kitty/cursor.c index f44724411..4f433b439 100644 --- a/kitty/cursor.c +++ b/kitty/cursor.c @@ -134,3 +134,4 @@ copy(Cursor *self, PyObject UNUSED *args) { return (PyObject*)ans; } +INIT_TYPE(Cursor) diff --git a/kitty/data-types.c b/kitty/data-types.c index 0610e6bae..b4885ac0e 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -6,10 +6,9 @@ */ #include "data-types.h" - -extern PyTypeObject LineBuf_Type; -extern PyTypeObject Cursor_Type; -extern PyTypeObject Line_Type; +extern int init_LineBuf(PyObject *); +extern int init_Cursor(PyObject *); +extern int init_Line(PyObject *); static PyMethodDef module_methods[] = { {NULL, NULL, 0, NULL} /* Sentinel */ @@ -28,19 +27,13 @@ PyInit_fast_data_types(void) { PyObject *m; - if (PyType_Ready(&LineBuf_Type) < 0) return NULL; - if (PyType_Ready(&Cursor_Type) < 0) return NULL; - if (PyType_Ready(&Line_Type) < 0) return NULL; m = PyModule_Create(&module); if (m == NULL) return NULL; if (m != NULL) { - Py_INCREF(&LineBuf_Type); - PyModule_AddObject(m, "LineBuf", (PyObject *)&LineBuf_Type); - Py_INCREF(&Cursor_Type); - PyModule_AddObject(m, "Cursor", (PyObject *)&Cursor_Type); - Py_INCREF(&Line_Type); - PyModule_AddObject(m, "Line", (PyObject *)&Line_Type); + if (!init_LineBuf(m)) return NULL; + if (!init_Line(m)) return NULL; + if (!init_Cursor(m)) return NULL; } return m; diff --git a/kitty/data-types.h b/kitty/data-types.h index 6e48c6781..1c30ecba6 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -47,6 +47,14 @@ typedef unsigned int index_type; #define METHOD(name, arg_type) {#name, (PyCFunction)name, arg_type, name##_doc}, +#define INIT_TYPE(type) \ + int init_##type(PyObject *module) {\ + if (PyType_Ready(&type##_Type) < 0) return 0; \ + if (PyModule_AddObject(module, #type, (PyObject *)&type##_Type) != 0) return 0; \ + Py_INCREF(&type##_Type); \ + return 1; \ + } + typedef struct { PyObject_HEAD @@ -83,3 +91,5 @@ typedef struct { uint32_t fg, bg, decoration_fg; } Cursor; + +Line *alloc_line(); diff --git a/kitty/line-buf.c b/kitty/line-buf.c index 12ed16772..c4f7b2276 100644 --- a/kitty/line-buf.c +++ b/kitty/line-buf.c @@ -6,7 +6,6 @@ */ #include "data-types.h" -extern PyTypeObject Line_Type; static inline void clear_chars_to_space(LineBuf* linebuf, index_type y) { @@ -39,7 +38,7 @@ new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { self->buf = PyMem_Calloc(xnum * ynum, CELL_SIZE); self->line_map = PyMem_Calloc(ynum, sizeof(index_type)); self->continued_map = PyMem_Calloc(ynum, sizeof(uint8_t)); - self->line = (Line*)PyType_GenericAlloc(&Line_Type, 0); + self->line = alloc_line(); if (self->buf == NULL || self->line_map == NULL || self->continued_map == NULL || self->line == NULL) { PyErr_NoMemory(); PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); Py_XDECREF(self->line); @@ -93,7 +92,7 @@ static PyMethodDef methods[] = { {NULL} /* Sentinel */ }; -PyTypeObject LineBuf_Type = { +static PyTypeObject LineBuf_Type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "fast_data_types.LineBuf", .tp_basicsize = sizeof(LineBuf), @@ -103,5 +102,7 @@ PyTypeObject LineBuf_Type = { .tp_methods = methods, .tp_new = new }; + +INIT_TYPE(LineBuf) // }} diff --git a/kitty/line.c b/kitty/line.c index 3be832611..e2319d3e5 100644 --- a/kitty/line.c +++ b/kitty/line.c @@ -203,7 +203,7 @@ static PyMethodDef methods[] = { {NULL} /* Sentinel */ }; -PyTypeObject Line_Type = { +static PyTypeObject Line_Type = { PyVarObject_HEAD_INIT(NULL, 0) .tp_name = "fast_data_types.Line", .tp_basicsize = sizeof(Line), @@ -215,6 +215,10 @@ PyTypeObject Line_Type = { .tp_methods = methods, .tp_new = new }; + +Line *alloc_line() { + return (Line*)PyType_GenericAlloc(&Line_Type, 0); +} // }} static PyObject* @@ -234,4 +238,4 @@ copy_char(Line* self, PyObject *args) { Py_RETURN_NONE; } - +INIT_TYPE(Line)