Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert RowType to heap type
  • Loading branch information
Erlend E. Aasland committed Sep 25, 2020
commit 55ad447daea193b548e4e70bc6a8b17bd41f23bc
2 changes: 1 addition & 1 deletion Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
ADD_TYPE(module, pysqlite_ConnectionType);
ADD_TYPE(module, pysqlite_CursorType);
ADD_TYPE(module, pysqlite_PrepareProtocolType);
ADD_TYPE(module, pysqlite_RowType);
ADD_TYPE(module, *pysqlite_RowType);

if (!(dict = PyModule_GetDict(module))) {
goto error;
Expand Down
86 changes: 27 additions & 59 deletions Modules/_sqlite/row.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void pysqlite_row_dealloc(pysqlite_Row* self)
Py_XDECREF(self->description);

Py_TYPE(self)->tp_free((PyObject*)self);
Py_DECREF(pysqlite_RowType);
}

static PyObject *
Expand Down Expand Up @@ -192,7 +193,7 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
if (opid != Py_EQ && opid != Py_NE)
Py_RETURN_NOTIMPLEMENTED;

if (PyObject_TypeCheck(_other, &pysqlite_RowType)) {
if (PyObject_TypeCheck(_other, pysqlite_RowType)) {
pysqlite_Row *other = (pysqlite_Row *)_other;
int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
if (eq < 0) {
Expand All @@ -206,73 +207,40 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
Py_RETURN_NOTIMPLEMENTED;
}

PyMappingMethods pysqlite_row_as_mapping = {
/* mp_length */ (lenfunc)pysqlite_row_length,
/* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
/* mp_ass_subscript */ (objobjargproc)0,
};

static PySequenceMethods pysqlite_row_as_sequence = {
/* sq_length */ (lenfunc)pysqlite_row_length,
/* sq_concat */ 0,
/* sq_repeat */ 0,
/* sq_item */ (ssizeargfunc)pysqlite_row_item,
};


static PyMethodDef pysqlite_row_methods[] = {
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
PyDoc_STR("Returns the keys of the row.")},
{NULL, NULL}
};

static PyType_Slot pysqlite_RowType_slots[] = {
{Py_tp_dealloc, pysqlite_row_dealloc},
{Py_tp_hash, pysqlite_row_hash},
{Py_tp_methods, pysqlite_row_methods},
{Py_tp_richcompare, pysqlite_row_richcompare},
{Py_tp_iter, pysqlite_iter},
{Py_mp_length, pysqlite_row_length},
{Py_mp_subscript, pysqlite_row_subscript},
{Py_sq_length, pysqlite_row_length},
{Py_sq_item, pysqlite_row_item},
{Py_tp_new, pysqlite_row_new},
{0, NULL},
};

PyTypeObject pysqlite_RowType = {
PyVarObject_HEAD_INIT(NULL, 0)
MODULE_NAME ".Row", /* tp_name */
sizeof(pysqlite_Row), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pysqlite_row_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_as_async */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
(hashfunc)pysqlite_row_hash, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
0, /* tp_doc */
(traverseproc)0, /* tp_traverse */
0, /* tp_clear */
(richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */
0, /* tp_weaklistoffset */
(getiterfunc)pysqlite_iter, /* tp_iter */
0, /* tp_iternext */
pysqlite_row_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 */
0, /* tp_new */
0 /* tp_free */
static PyType_Spec pysqlite_RowType_spec = {
.name = MODULE_NAME ".Row",
.basicsize = sizeof(pysqlite_Row),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE,
.slots = pysqlite_RowType_slots,
};

PyTypeObject *pysqlite_RowType = NULL;

extern int pysqlite_row_setup_types(void)
{
pysqlite_RowType.tp_new = pysqlite_row_new;
pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
return PyType_Ready(&pysqlite_RowType);
pysqlite_RowType = (PyTypeObject *)PyType_FromSpec(&pysqlite_RowType_spec);
if (pysqlite_RowType == NULL) {
return -1;
}
return 0;
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/row.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ typedef struct _Row
PyObject* description;
} pysqlite_Row;

extern PyTypeObject pysqlite_RowType;
extern PyTypeObject *pysqlite_RowType;

int pysqlite_row_setup_types(void);

Expand Down