Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-41861: Convert sqlite3 to PEP 384 #22409

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert :mod:`sqlite3` to PEP 384.
128 changes: 37 additions & 91 deletions Modules/_sqlite/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
{
pysqlite_Node* node;

node = (pysqlite_Node*) (pysqlite_NodeType.tp_alloc(&pysqlite_NodeType, 0));
node = (pysqlite_Node*) (pysqlite_NodeType->tp_alloc(pysqlite_NodeType, 0));
if (!node) {
return NULL;
}
Expand All @@ -52,6 +52,7 @@ void pysqlite_node_dealloc(pysqlite_Node* self)
Py_DECREF(self->data);

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

int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
Expand Down Expand Up @@ -110,6 +111,7 @@ void pysqlite_cache_dealloc(pysqlite_Cache* self)
Py_DECREF(self->mapping);

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

PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
Expand Down Expand Up @@ -253,6 +255,20 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
Py_RETURN_NONE;
}

static PyType_Slot pysqlite_NodeType_slots[] = {
{Py_tp_dealloc, pysqlite_node_dealloc},
{Py_tp_new, PyType_GenericNew},
{0, NULL},
};

PyType_Spec pysqlite_NodeType_spec = {
.name = MODULE_NAME ".Node",
.basicsize = sizeof(pysqlite_Node),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE,
.slots = pysqlite_NodeType_slots,
};
PyTypeObject *pysqlite_NodeType = NULL;

static PyMethodDef cache_methods[] = {
{"get", (PyCFunction)pysqlite_cache_get, METH_O,
PyDoc_STR("Gets an entry from the cache or calls the factory function to produce one.")},
Expand All @@ -261,102 +277,32 @@ static PyMethodDef cache_methods[] = {
{NULL, NULL}
};

PyTypeObject pysqlite_NodeType = {
PyVarObject_HEAD_INIT(NULL, 0)
MODULE_NAME "Node", /* tp_name */
sizeof(pysqlite_Node), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pysqlite_node_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 */
0, /* 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 */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* 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 */
(initproc)0, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0 /* tp_free */
static PyType_Slot pysqlite_CacheType_slots[] = {
{Py_tp_dealloc, pysqlite_cache_dealloc},
{Py_tp_methods, cache_methods},
{Py_tp_new, PyType_GenericNew},
{Py_tp_init, pysqlite_cache_init},
{0, NULL},
};

PyTypeObject pysqlite_CacheType = {
PyVarObject_HEAD_INIT(NULL, 0)
MODULE_NAME ".Cache", /* tp_name */
sizeof(pysqlite_Cache), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pysqlite_cache_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 */
0, /* 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 */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
cache_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 */
(initproc)pysqlite_cache_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0 /* tp_free */
static PyType_Spec pysqlite_CacheType_spec = {
.name = MODULE_NAME ".Cache",
.basicsize = sizeof(pysqlite_Cache),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE,
.slots = pysqlite_CacheType_slots,
};
PyTypeObject *pysqlite_CacheType = NULL;

extern int pysqlite_cache_setup_types(void)
{
int rc;

pysqlite_NodeType.tp_new = PyType_GenericNew;
pysqlite_CacheType.tp_new = PyType_GenericNew;

rc = PyType_Ready(&pysqlite_NodeType);
if (rc < 0) {
return rc;
pysqlite_NodeType = (PyTypeObject *)PyType_FromSpec(&pysqlite_NodeType_spec);
if (pysqlite_NodeType == NULL) {
return -1;
}

rc = PyType_Ready(&pysqlite_CacheType);
return rc;
pysqlite_CacheType = (PyTypeObject *)PyType_FromSpec(&pysqlite_CacheType_spec);
if (pysqlite_CacheType == NULL) {
return -1;
}
return 0;
}
4 changes: 2 additions & 2 deletions Modules/_sqlite/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ typedef struct
int decref_factory;
} pysqlite_Cache;

extern PyTypeObject pysqlite_NodeType;
extern PyTypeObject pysqlite_CacheType;
extern PyTypeObject *pysqlite_NodeType;
extern PyTypeObject *pysqlite_CacheType;

int pysqlite_node_init(pysqlite_Node* self, PyObject* args, PyObject* kwargs);
void pysqlite_node_dealloc(pysqlite_Node* self);
Expand Down
76 changes: 29 additions & 47 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
}
Py_DECREF(isolation_level);

self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
if (PyErr_Occurred()) {
return -1;
}
Expand Down Expand Up @@ -237,6 +237,7 @@ void pysqlite_connection_dealloc(pysqlite_Connection* self)
Py_XDECREF(self->statements);
Py_XDECREF(self->cursors);
Py_TYPE(self)->tp_free((PyObject*)self);
//Py_DECREF(pysqlite_ConnectionType); FIXME
}

/*
Expand Down Expand Up @@ -281,13 +282,13 @@ PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args,
}

if (factory == NULL) {
factory = (PyObject*)&pysqlite_CursorType;
factory = (PyObject*)pysqlite_CursorType;
}

cursor = PyObject_CallOneArg(factory, (PyObject *)self);
if (cursor == NULL)
return NULL;
if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
PyErr_Format(PyExc_TypeError,
"factory must return a cursor, not %.100s",
Py_TYPE(cursor)->tp_name);
Expand Down Expand Up @@ -1234,7 +1235,7 @@ PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, Py

_pysqlite_drop_unused_statement_references(self);

statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
if (!statement) {
return NULL;
}
Expand Down Expand Up @@ -1494,7 +1495,7 @@ pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *
static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
&pysqlite_ConnectionType, &target,
pysqlite_ConnectionType, &target,
&pages, &progress, &name, &sleep_obj)) {
return NULL;
}
Expand Down Expand Up @@ -1831,50 +1832,31 @@ static struct PyMemberDef connection_members[] =
{NULL}
};

PyTypeObject pysqlite_ConnectionType = {
PyVarObject_HEAD_INIT(NULL, 0)
MODULE_NAME ".Connection", /* tp_name */
sizeof(pysqlite_Connection), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)pysqlite_connection_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 */
0, /* tp_hash */
(ternaryfunc)pysqlite_connection_call, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /* tp_flags */
connection_doc, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
connection_methods, /* tp_methods */
connection_members, /* tp_members */
connection_getset, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)pysqlite_connection_init, /* tp_init */
0, /* tp_alloc */
0, /* tp_new */
0 /* tp_free */
static PyType_Slot pysqlite_ConnectionType_slots[] = {
{Py_tp_dealloc, pysqlite_connection_dealloc},
{Py_tp_doc, (void *)connection_doc},
{Py_tp_methods, connection_methods},
{Py_tp_members, connection_members},
{Py_tp_getset, connection_getset},
{Py_tp_new, PyType_GenericNew},
{Py_tp_init, pysqlite_connection_init},
{Py_tp_call, pysqlite_connection_call},
{0, NULL},
};

static PyType_Spec pysqlite_ConnectionType_spec = {
.name = MODULE_NAME ".Connection",
.basicsize = sizeof(pysqlite_Connection),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HEAPTYPE,
.slots = pysqlite_ConnectionType_slots,
};
PyTypeObject *pysqlite_ConnectionType = NULL;

extern int pysqlite_connection_setup_types(void)
{
pysqlite_ConnectionType.tp_new = PyType_GenericNew;
return PyType_Ready(&pysqlite_ConnectionType);
pysqlite_ConnectionType = (PyTypeObject *)PyType_FromSpec(&pysqlite_ConnectionType_spec);
if (pysqlite_ConnectionType == NULL) {
return -1;
}
return 0;
}
2 changes: 1 addition & 1 deletion Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ typedef struct
PyObject* NotSupportedError;
} pysqlite_Connection;

extern PyTypeObject pysqlite_ConnectionType;
extern PyTypeObject *pysqlite_ConnectionType;

PyObject* pysqlite_connection_alloc(PyTypeObject* type, int aware);
void pysqlite_connection_dealloc(pysqlite_Connection* self);
Expand Down
Loading