Skip to content

Commit 0615065

Browse files
author
Erlend E. Aasland
committed
Convert StatementType to heap type
1 parent cb6db8b commit 0615065

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

Modules/_sqlite/connection.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py
12341234

12351235
_pysqlite_drop_unused_statement_references(self);
12361236

1237-
statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
1237+
statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
12381238
if (!statement) {
12391239
return NULL;
12401240
}

Modules/_sqlite/cursor.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* args)
472472

473473
if (self->statement->in_use) {
474474
Py_SETREF(self->statement,
475-
PyObject_New(pysqlite_Statement, &pysqlite_StatementType));
475+
PyObject_New(pysqlite_Statement, pysqlite_StatementType));
476476
if (!self->statement) {
477477
goto error;
478478
}

Modules/_sqlite/module.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ PyMODINIT_FUNC PyInit__sqlite3(void)
356356
(pysqlite_cursor_setup_types() < 0) ||
357357
(pysqlite_connection_setup_types() < 0) ||
358358
(pysqlite_cache_setup_types(module) < 0) ||
359-
(pysqlite_statement_setup_types() < 0) ||
359+
(pysqlite_statement_setup_types(module) < 0) ||
360360
(pysqlite_prepare_protocol_setup_types(module) < 0)
361361
) {
362362
Py_XDECREF(module);

Modules/_sqlite/statement.c

Lines changed: 27 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ void pysqlite_statement_mark_dirty(pysqlite_Statement* self)
371371

372372
void pysqlite_statement_dealloc(pysqlite_Statement* self)
373373
{
374+
PyTypeObject *tp = Py_TYPE(self);
375+
374376
if (self->st) {
375377
Py_BEGIN_ALLOW_THREADS
376378
sqlite3_finalize(self->st);
@@ -385,7 +387,8 @@ void pysqlite_statement_dealloc(pysqlite_Statement* self)
385387
PyObject_ClearWeakRefs((PyObject*)self);
386388
}
387389

388-
Py_TYPE(self)->tp_free((PyObject*)self);
390+
tp->tp_free(self);
391+
Py_DECREF(tp);
389392
}
390393

391394
/*
@@ -458,50 +461,30 @@ static int pysqlite_check_remaining_sql(const char* tail)
458461
return 0;
459462
}
460463

461-
PyTypeObject pysqlite_StatementType = {
462-
PyVarObject_HEAD_INIT(NULL, 0)
463-
MODULE_NAME ".Statement", /* tp_name */
464-
sizeof(pysqlite_Statement), /* tp_basicsize */
465-
0, /* tp_itemsize */
466-
(destructor)pysqlite_statement_dealloc, /* tp_dealloc */
467-
0, /* tp_vectorcall_offset */
468-
0, /* tp_getattr */
469-
0, /* tp_setattr */
470-
0, /* tp_as_async */
471-
0, /* tp_repr */
472-
0, /* tp_as_number */
473-
0, /* tp_as_sequence */
474-
0, /* tp_as_mapping */
475-
0, /* tp_hash */
476-
0, /* tp_call */
477-
0, /* tp_str */
478-
0, /* tp_getattro */
479-
0, /* tp_setattro */
480-
0, /* tp_as_buffer */
481-
Py_TPFLAGS_DEFAULT, /* tp_flags */
482-
0, /* tp_doc */
483-
0, /* tp_traverse */
484-
0, /* tp_clear */
485-
0, /* tp_richcompare */
486-
offsetof(pysqlite_Statement, in_weakreflist), /* tp_weaklistoffset */
487-
0, /* tp_iter */
488-
0, /* tp_iternext */
489-
0, /* tp_methods */
490-
0, /* tp_members */
491-
0, /* tp_getset */
492-
0, /* tp_base */
493-
0, /* tp_dict */
494-
0, /* tp_descr_get */
495-
0, /* tp_descr_set */
496-
0, /* tp_dictoffset */
497-
(initproc)0, /* tp_init */
498-
0, /* tp_alloc */
499-
0, /* tp_new */
500-
0 /* tp_free */
464+
static PyMemberDef stmt_members[] = {
465+
{"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Statement, in_weakreflist), READONLY},
466+
{NULL},
467+
};
468+
static PyType_Slot stmt_slots[] = {
469+
{Py_tp_members, stmt_members},
470+
{Py_tp_dealloc, pysqlite_statement_dealloc},
471+
{Py_tp_new, PyType_GenericNew},
472+
{0, NULL},
473+
};
474+
475+
static PyType_Spec stmt_spec = {
476+
.name = MODULE_NAME ".Statement",
477+
.basicsize = sizeof(pysqlite_Statement),
478+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE,
479+
.slots = stmt_slots,
501480
};
481+
PyTypeObject *pysqlite_StatementType = NULL;
502482

503-
extern int pysqlite_statement_setup_types(void)
483+
extern int pysqlite_statement_setup_types(PyObject *module)
504484
{
505-
pysqlite_StatementType.tp_new = PyType_GenericNew;
506-
return PyType_Ready(&pysqlite_StatementType);
485+
pysqlite_StatementType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &stmt_spec, NULL);
486+
if (pysqlite_StatementType == NULL) {
487+
return -1;
488+
}
489+
return 0;
507490
}

Modules/_sqlite/statement.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ typedef struct
4343
PyObject* in_weakreflist; /* List of weak references */
4444
} pysqlite_Statement;
4545

46-
extern PyTypeObject pysqlite_StatementType;
46+
extern PyTypeObject *pysqlite_StatementType;
4747

4848
int pysqlite_statement_create(pysqlite_Statement* self, pysqlite_Connection* connection, PyObject* sql);
4949
void pysqlite_statement_dealloc(pysqlite_Statement* self);
@@ -55,6 +55,6 @@ int pysqlite_statement_finalize(pysqlite_Statement* self);
5555
int pysqlite_statement_reset(pysqlite_Statement* self);
5656
void pysqlite_statement_mark_dirty(pysqlite_Statement* self);
5757

58-
int pysqlite_statement_setup_types(void);
58+
int pysqlite_statement_setup_types(PyObject *module);
5959

6060
#endif

0 commit comments

Comments
 (0)