Skip to content

Commit 9226ab5

Browse files
author
Erlend E. Aasland
committed
Convert RowType to heap type
1 parent 0615065 commit 9226ab5

File tree

3 files changed

+36
-66
lines changed

3 files changed

+36
-66
lines changed

Modules/_sqlite/module.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
352352
module = PyModule_Create(&_sqlite3module);
353353

354354
if (!module ||
355-
(pysqlite_row_setup_types() < 0) ||
355+
(pysqlite_row_setup_types(module) < 0) ||
356356
(pysqlite_cursor_setup_types() < 0) ||
357357
(pysqlite_connection_setup_types() < 0) ||
358358
(pysqlite_cache_setup_types(module) < 0) ||
@@ -366,7 +366,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
366366
ADD_TYPE(module, pysqlite_ConnectionType);
367367
ADD_TYPE(module, pysqlite_CursorType);
368368
ADD_TYPE(module, *pysqlite_PrepareProtocolType);
369-
ADD_TYPE(module, pysqlite_RowType);
369+
ADD_TYPE(module, *pysqlite_RowType);
370370

371371
if (!(dict = PyModule_GetDict(module))) {
372372
goto error;

Modules/_sqlite/row.c

+32-62
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626

2727
void pysqlite_row_dealloc(pysqlite_Row* self)
2828
{
29+
PyTypeObject *tp = Py_TYPE(self);
30+
2931
Py_XDECREF(self->data);
3032
Py_XDECREF(self->description);
3133

32-
Py_TYPE(self)->tp_free((PyObject*)self);
34+
tp->tp_free(self);
35+
Py_DECREF(tp);
3336
}
3437

3538
static PyObject *
@@ -192,7 +195,7 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
192195
if (opid != Py_EQ && opid != Py_NE)
193196
Py_RETURN_NOTIMPLEMENTED;
194197

195-
if (PyObject_TypeCheck(_other, &pysqlite_RowType)) {
198+
if (PyObject_TypeCheck(_other, pysqlite_RowType)) {
196199
pysqlite_Row *other = (pysqlite_Row *)_other;
197200
int eq = PyObject_RichCompareBool(self->description, other->description, Py_EQ);
198201
if (eq < 0) {
@@ -206,73 +209,40 @@ static PyObject* pysqlite_row_richcompare(pysqlite_Row *self, PyObject *_other,
206209
Py_RETURN_NOTIMPLEMENTED;
207210
}
208211

209-
PyMappingMethods pysqlite_row_as_mapping = {
210-
/* mp_length */ (lenfunc)pysqlite_row_length,
211-
/* mp_subscript */ (binaryfunc)pysqlite_row_subscript,
212-
/* mp_ass_subscript */ (objobjargproc)0,
213-
};
214-
215-
static PySequenceMethods pysqlite_row_as_sequence = {
216-
/* sq_length */ (lenfunc)pysqlite_row_length,
217-
/* sq_concat */ 0,
218-
/* sq_repeat */ 0,
219-
/* sq_item */ (ssizeargfunc)pysqlite_row_item,
220-
};
221-
222-
223-
static PyMethodDef pysqlite_row_methods[] = {
212+
static PyMethodDef row_methods[] = {
224213
{"keys", (PyCFunction)pysqlite_row_keys, METH_NOARGS,
225214
PyDoc_STR("Returns the keys of the row.")},
226215
{NULL, NULL}
227216
};
228217

218+
static PyType_Slot row_slots[] = {
219+
{Py_tp_dealloc, pysqlite_row_dealloc},
220+
{Py_tp_hash, pysqlite_row_hash},
221+
{Py_tp_methods, row_methods},
222+
{Py_tp_richcompare, pysqlite_row_richcompare},
223+
{Py_tp_iter, pysqlite_iter},
224+
{Py_mp_length, pysqlite_row_length},
225+
{Py_mp_subscript, pysqlite_row_subscript},
226+
{Py_sq_length, pysqlite_row_length},
227+
{Py_sq_item, pysqlite_row_item},
228+
{Py_tp_new, pysqlite_row_new},
229+
{0, NULL},
230+
};
229231

230-
PyTypeObject pysqlite_RowType = {
231-
PyVarObject_HEAD_INIT(NULL, 0)
232-
MODULE_NAME ".Row", /* tp_name */
233-
sizeof(pysqlite_Row), /* tp_basicsize */
234-
0, /* tp_itemsize */
235-
(destructor)pysqlite_row_dealloc, /* tp_dealloc */
236-
0, /* tp_vectorcall_offset */
237-
0, /* tp_getattr */
238-
0, /* tp_setattr */
239-
0, /* tp_as_async */
240-
0, /* tp_repr */
241-
0, /* tp_as_number */
242-
0, /* tp_as_sequence */
243-
0, /* tp_as_mapping */
244-
(hashfunc)pysqlite_row_hash, /* tp_hash */
245-
0, /* tp_call */
246-
0, /* tp_str */
247-
0, /* tp_getattro */
248-
0, /* tp_setattro */
249-
0, /* tp_as_buffer */
250-
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
251-
0, /* tp_doc */
252-
(traverseproc)0, /* tp_traverse */
253-
0, /* tp_clear */
254-
(richcmpfunc)pysqlite_row_richcompare, /* tp_richcompare */
255-
0, /* tp_weaklistoffset */
256-
(getiterfunc)pysqlite_iter, /* tp_iter */
257-
0, /* tp_iternext */
258-
pysqlite_row_methods, /* tp_methods */
259-
0, /* tp_members */
260-
0, /* tp_getset */
261-
0, /* tp_base */
262-
0, /* tp_dict */
263-
0, /* tp_descr_get */
264-
0, /* tp_descr_set */
265-
0, /* tp_dictoffset */
266-
0, /* tp_init */
267-
0, /* tp_alloc */
268-
0, /* tp_new */
269-
0 /* tp_free */
232+
static PyType_Spec row_spec = {
233+
.name = MODULE_NAME ".Row",
234+
.basicsize = sizeof(pysqlite_Row),
235+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE,
236+
.slots = row_slots,
270237
};
271238

272-
extern int pysqlite_row_setup_types(void)
239+
PyTypeObject *pysqlite_RowType = NULL;
240+
241+
extern int pysqlite_row_setup_types(PyObject *module)
273242
{
274-
pysqlite_RowType.tp_new = pysqlite_row_new;
275-
pysqlite_RowType.tp_as_mapping = &pysqlite_row_as_mapping;
276-
pysqlite_RowType.tp_as_sequence = &pysqlite_row_as_sequence;
277-
return PyType_Ready(&pysqlite_RowType);
243+
pysqlite_RowType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &row_spec, NULL);
244+
if (pysqlite_RowType == NULL) {
245+
return -1;
246+
}
247+
return 0;
278248
}

Modules/_sqlite/row.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ typedef struct _Row
3333
PyObject* description;
3434
} pysqlite_Row;
3535

36-
extern PyTypeObject pysqlite_RowType;
36+
extern PyTypeObject *pysqlite_RowType;
3737

38-
int pysqlite_row_setup_types(void);
38+
int pysqlite_row_setup_types(PyObject *module);
3939

4040
#endif

0 commit comments

Comments
 (0)