Reduce the size of the boilerplate

This commit is contained in:
Kovid Goyal 2016-11-02 11:26:36 +05:30
parent 437593d4ee
commit c0d5719eb4
3 changed files with 33 additions and 118 deletions

View File

@ -12,7 +12,7 @@
#define INIT_NONE(x) Py_INCREF(Py_None); x = Py_None; #define INIT_NONE(x) Py_INCREF(Py_None); x = Py_None;
static PyObject * static PyObject *
Cursor_new(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED *kwds) { new(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED *kwds) {
Cursor *self; Cursor *self;
self = (Cursor *)type->tp_alloc(type, 0); self = (Cursor *)type->tp_alloc(type, 0);
@ -30,7 +30,7 @@ Cursor_new(PyTypeObject *type, PyObject UNUSED *args, PyObject UNUSED *kwds) {
} }
static void static void
Cursor_dealloc(Cursor* self) { dealloc(Cursor* self) {
Py_XDECREF(self->shape); Py_XDECREF(self->shape);
Py_XDECREF(self->blink); Py_XDECREF(self->blink);
Py_XDECREF(self->color); Py_XDECREF(self->color);
@ -61,7 +61,7 @@ copy(Cursor *self, PyObject *args);
// Boilerplate {{{ // Boilerplate {{{
static PyMemberDef Cursor_members[] = { static PyMemberDef members[] = {
{"x", T_OBJECT_EX, offsetof(Cursor, x), 0, "x"}, {"x", T_OBJECT_EX, offsetof(Cursor, x), 0, "x"},
{"y", T_OBJECT_EX, offsetof(Cursor, y), 0, "y"}, {"y", T_OBJECT_EX, offsetof(Cursor, y), 0, "y"},
{"shape", T_OBJECT_EX, offsetof(Cursor, shape), 0, "shape"}, {"shape", T_OBJECT_EX, offsetof(Cursor, shape), 0, "shape"},
@ -80,7 +80,7 @@ static PyMemberDef Cursor_members[] = {
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
static PyMethodDef Cursor_methods[] = { static PyMethodDef methods[] = {
METHOD(copy, METH_NOARGS) METHOD(copy, METH_NOARGS)
{NULL} /* Sentinel */ {NULL} /* Sentinel */
}; };
@ -91,43 +91,16 @@ richcmp(PyObject *obj1, PyObject *obj2, int op);
PyTypeObject Cursor_Type = { PyTypeObject Cursor_Type = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"fast_data_types.Cursor", .tp_name = "fast_data_types.Cursor",
sizeof(Cursor), .tp_basicsize = sizeof(Cursor),
0, /* tp_itemsize */ .tp_dealloc = (destructor)dealloc,
(destructor)Cursor_dealloc, /* tp_dealloc */ .tp_repr = (reprfunc)repr,
0, /* tp_print */ .tp_flags = Py_TPFLAGS_DEFAULT,
0, /* tp_getattr */ .tp_doc = "Cursors",
0, /* tp_setattr */ .tp_richcompare = richcmp,
0, /* tp_reserved */ .tp_methods = methods,
(reprfunc)repr, /* tp_repr */ .tp_members = members,
0, /* tp_as_number */ .tp_new = new,
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 */
"Cursors", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
richcmp, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Cursor_methods, /* tp_methods */
Cursor_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
Cursor_new, /* tp_new */
}; };

View File

@ -15,7 +15,7 @@ clear_chars_to_space(LineBuf* linebuf, index_type y) {
} }
static PyObject * static PyObject *
LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) { new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
LineBuf *self; LineBuf *self;
index_type xnum, ynum; index_type xnum, ynum;
@ -62,7 +62,7 @@ LineBuf_new(PyTypeObject *type, PyObject *args, PyObject UNUSED *kwds) {
} }
static void static void
LineBuf_dealloc(LineBuf* self) { dealloc(LineBuf* self) {
PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map); PyMem_Free(self->buf); PyMem_Free(self->line_map); PyMem_Free(self->continued_map);
Py_XDECREF(self->line); Py_XDECREF(self->line);
Py_TYPE(self)->tp_free((PyObject*)self); Py_TYPE(self)->tp_free((PyObject*)self);
@ -86,7 +86,7 @@ line(LineBuf *self, PyObject *y) {
} }
// Boilerplate {{{ // Boilerplate {{{
static PyMethodDef LineBuf_methods[] = { static PyMethodDef methods[] = {
{"line", (PyCFunction)line, METH_O, {"line", (PyCFunction)line, METH_O,
"Return the specified line as a Line object. Note the Line Object is a live view into the underlying buffer. And only a single line object can be used at a time." "Return the specified line as a Line object. Note the Line Object is a live view into the underlying buffer. And only a single line object can be used at a time."
}, },
@ -95,43 +95,13 @@ static PyMethodDef LineBuf_methods[] = {
PyTypeObject LineBuf_Type = { PyTypeObject LineBuf_Type = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"fast_data_types.LineBuf", .tp_name = "fast_data_types.LineBuf",
sizeof(LineBuf), .tp_basicsize = sizeof(LineBuf),
0, /* tp_itemsize */ .tp_dealloc = (destructor)dealloc,
(destructor)LineBuf_dealloc, /* tp_dealloc */ .tp_flags = Py_TPFLAGS_DEFAULT,
0, /* tp_print */ .tp_doc = "Line buffers",
0, /* tp_getattr */ .tp_methods = methods,
0, /* tp_setattr */ .tp_new = new
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 */
"Line buffers", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
LineBuf_methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
LineBuf_new, /* tp_new */
}; };
// }} // }}

View File

@ -205,43 +205,15 @@ static PyMethodDef methods[] = {
PyTypeObject Line_Type = { PyTypeObject Line_Type = {
PyVarObject_HEAD_INIT(NULL, 0) PyVarObject_HEAD_INIT(NULL, 0)
"fast_data_types.Line", .tp_name = "fast_data_types.Line",
sizeof(Line), .tp_basicsize = sizeof(Line),
0, /* tp_itemsize */ .tp_dealloc = (destructor)dealloc,
(destructor)dealloc, /* tp_dealloc */ .tp_repr = (reprfunc)as_unicode,
0, /* tp_print */ .tp_as_sequence = &sequence_methods,
0, /* tp_getattr */ .tp_flags = Py_TPFLAGS_DEFAULT,
0, /* tp_setattr */ .tp_doc = "Lines",
0, /* tp_reserved */ .tp_methods = methods,
(reprfunc)as_unicode, /* tp_repr */ .tp_new = new
0, /* tp_as_number */
&sequence_methods, /* 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 */
"Lines", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
methods, /* tp_methods */
0, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
new, /* tp_new */
}; };
// }} // }}