summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/_sqlite/cache.c
diff options
context:
space:
mode:
authorshadchin <[email protected]>2022-04-18 12:39:32 +0300
committershadchin <[email protected]>2022-04-18 12:39:32 +0300
commitd4be68e361f4258cf0848fc70018dfe37a2acc24 (patch)
tree153e294cd97ac8b5d7a989612704a0c1f58e8ad4 /contrib/tools/python3/src/Modules/_sqlite/cache.c
parent260c02f5ccf242d9d9b8a873afaf6588c00237d6 (diff)
IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4
ref:9f96be6d02ee8044fdd6f124b799b270c20ce641
Diffstat (limited to 'contrib/tools/python3/src/Modules/_sqlite/cache.c')
-rw-r--r--contrib/tools/python3/src/Modules/_sqlite/cache.c231
1 files changed, 110 insertions, 121 deletions
diff --git a/contrib/tools/python3/src/Modules/_sqlite/cache.c b/contrib/tools/python3/src/Modules/_sqlite/cache.c
index 758fc022f78..fd4e619f6a0 100644
--- a/contrib/tools/python3/src/Modules/_sqlite/cache.c
+++ b/contrib/tools/python3/src/Modules/_sqlite/cache.c
@@ -25,20 +25,18 @@
#include <limits.h>
/* only used internally */
-pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
+static 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;
}
- Py_INCREF(key);
- node->key = key;
-
- Py_INCREF(data);
- node->data = data;
+ node->key = Py_NewRef(key);
+ node->data = Py_NewRef(data);
node->prev = NULL;
node->next = NULL;
@@ -46,15 +44,35 @@ pysqlite_Node* pysqlite_new_node(PyObject* key, PyObject* data)
return node;
}
-void pysqlite_node_dealloc(pysqlite_Node* self)
+static int
+node_traverse(pysqlite_Node *self, visitproc visit, void *arg)
+{
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->key);
+ Py_VISIT(self->data);
+ return 0;
+}
+
+static int
+node_clear(pysqlite_Node *self)
{
- Py_DECREF(self->key);
- Py_DECREF(self->data);
+ Py_CLEAR(self->key);
+ Py_CLEAR(self->data);
+ return 0;
+}
- Py_TYPE(self)->tp_free((PyObject*)self);
+static void
+pysqlite_node_dealloc(pysqlite_Node *self)
+{
+ PyTypeObject *tp = Py_TYPE(self);
+ PyObject_GC_UnTrack(self);
+ tp->tp_clear((PyObject *)self);
+ tp->tp_free(self);
+ Py_DECREF(tp);
}
-int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
+static int
+pysqlite_cache_init(pysqlite_Cache *self, PyObject *args, PyObject *kwargs)
{
PyObject* factory;
int size = 10;
@@ -78,38 +96,62 @@ int pysqlite_cache_init(pysqlite_Cache* self, PyObject* args, PyObject* kwargs)
return -1;
}
- Py_INCREF(factory);
- self->factory = factory;
+ self->factory = Py_NewRef(factory);
self->decref_factory = 1;
return 0;
}
-void pysqlite_cache_dealloc(pysqlite_Cache* self)
+static int
+cache_traverse(pysqlite_Cache *self, visitproc visit, void *arg)
{
- pysqlite_Node* node;
- pysqlite_Node* delete_node;
+ Py_VISIT(Py_TYPE(self));
+ Py_VISIT(self->mapping);
+ if (self->decref_factory) {
+ Py_VISIT(self->factory);
+ }
- if (!self->factory) {
- /* constructor failed, just get out of here */
- return;
+ pysqlite_Node *node = self->first;
+ while (node) {
+ Py_VISIT(node);
+ node = node->next;
+ }
+ return 0;
+}
+
+static int
+cache_clear(pysqlite_Cache *self)
+{
+ Py_CLEAR(self->mapping);
+ if (self->decref_factory) {
+ Py_CLEAR(self->factory);
}
/* iterate over all nodes and deallocate them */
- node = self->first;
+ pysqlite_Node *node = self->first;
+ self->first = NULL;
while (node) {
- delete_node = node;
+ pysqlite_Node *delete_node = node;
node = node->next;
- Py_DECREF(delete_node);
+ Py_CLEAR(delete_node);
}
+ return 0;
+}
- if (self->decref_factory) {
- Py_DECREF(self->factory);
+static void
+pysqlite_cache_dealloc(pysqlite_Cache *self)
+{
+ if (!self->factory) {
+ /* constructor failed, just get out of here */
+ return;
}
- Py_DECREF(self->mapping);
- Py_TYPE(self)->tp_free((PyObject*)self);
+ PyObject_GC_UnTrack(self);
+ PyTypeObject *tp = Py_TYPE(self);
+ tp->tp_clear((PyObject *)self);
+ tp->tp_free(self);
+ Py_DECREF(tp);
}
PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
@@ -213,11 +255,11 @@ PyObject* pysqlite_cache_get(pysqlite_Cache* self, PyObject* key)
self->last = node;
}
- Py_INCREF(node->data);
- return node->data;
+ return Py_NewRef(node->data);
}
-PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
+static PyObject *
+pysqlite_cache_display(pysqlite_Cache *self, PyObject *args)
{
pysqlite_Node* ptr;
PyObject* prevkey;
@@ -253,6 +295,21 @@ PyObject* pysqlite_cache_display(pysqlite_Cache* self, PyObject* args)
Py_RETURN_NONE;
}
+static PyType_Slot node_slots[] = {
+ {Py_tp_dealloc, pysqlite_node_dealloc},
+ {Py_tp_traverse, node_traverse},
+ {Py_tp_clear, node_clear},
+ {0, NULL},
+};
+
+static PyType_Spec node_spec = {
+ .name = MODULE_NAME ".Node",
+ .basicsize = sizeof(pysqlite_Node),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
+ .slots = node_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.")},
@@ -261,102 +318,34 @@ 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 cache_slots[] = {
+ {Py_tp_dealloc, pysqlite_cache_dealloc},
+ {Py_tp_methods, cache_methods},
+ {Py_tp_init, pysqlite_cache_init},
+ {Py_tp_traverse, cache_traverse},
+ {Py_tp_clear, cache_clear},
+ {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 cache_spec = {
+ .name = MODULE_NAME ".Cache",
+ .basicsize = sizeof(pysqlite_Cache),
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
+ .slots = cache_slots,
};
+PyTypeObject *pysqlite_CacheType = NULL;
-extern int pysqlite_cache_setup_types(void)
+int
+pysqlite_cache_setup_types(PyObject *mod)
{
- 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_FromModuleAndSpec(mod, &node_spec, NULL);
+ if (pysqlite_NodeType == NULL) {
+ return -1;
}
- rc = PyType_Ready(&pysqlite_CacheType);
- return rc;
+ pysqlite_CacheType = (PyTypeObject *)PyType_FromModuleAndSpec(mod, &cache_spec, NULL);
+ if (pysqlite_CacheType == NULL) {
+ return -1;
+ }
+ return 0;
}