Skip to content
Merged
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
Address review: add global state and add LRU cache func to it
  • Loading branch information
Erlend E. Aasland committed May 25, 2021
commit a95a386adad9bce2e667ac23bac1beb69b2eae28
20 changes: 2 additions & 18 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,31 +56,15 @@ static const char * const begin_statements[] = {
static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);

static PyObject *_lru_cache = NULL;

int
load_functools_lru_cache(PyObject *Py_UNUSED(module))
{
PyObject *functools = PyImport_ImportModule("functools");
if (functools == NULL) {
return -1;
}
_lru_cache = PyObject_GetAttrString(functools, "lru_cache");
Py_DECREF(functools);
if (_lru_cache == NULL) {
return -1;
}
return 0;
}

static PyObject *
new_statement_cache(pysqlite_Connection *self, int maxsize)
{
PyObject *args[] = { PyLong_FromLong(maxsize), };
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
if (args[0] == NULL) {
return NULL;
}
PyObject *inner = PyObject_Vectorcall(_lru_cache, args, 1, NULL);
pysqlite_state *state = pysqlite_get_state(NULL);
PyObject *inner = PyObject_Vectorcall(state->lru_cache, args, 1, NULL);
Py_DECREF(args[0]);
if (inner == NULL) {
return NULL;
Expand Down
1 change: 0 additions & 1 deletion Modules/_sqlite/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,5 @@ int pysqlite_check_thread(pysqlite_Connection* self);
int pysqlite_check_connection(pysqlite_Connection* con);

int pysqlite_connection_setup_types(PyObject *module);
int load_functools_lru_cache(PyObject *module);

#endif
25 changes: 25 additions & 0 deletions Modules/_sqlite/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ PyObject* _pysqlite_converters = NULL;
int _pysqlite_enable_callback_tracebacks = 0;
int pysqlite_BaseTypeAdapted = 0;

pysqlite_state pysqlite_global_state;

pysqlite_state *
pysqlite_get_state(PyObject *Py_UNUSED(module))
{
return &pysqlite_global_state;
}

static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
kwargs)
{
Expand Down Expand Up @@ -260,6 +268,23 @@ static int converters_init(PyObject* module)
return res;
}

static int
load_functools_lru_cache(PyObject *module)
{
PyObject *functools = PyImport_ImportModule("functools");
if (functools == NULL) {
return -1;
}

pysqlite_state *state = pysqlite_get_state(module);
state->lru_cache = PyObject_GetAttrString(functools, "lru_cache");
Py_DECREF(functools);
if (state->lru_cache == NULL) {
return -1;
}
return 0;
}

static PyMethodDef module_methods[] = {
{"connect", (PyCFunction)(void(*)(void))module_connect,
METH_VARARGS | METH_KEYWORDS, module_connect_doc},
Expand Down
6 changes: 6 additions & 0 deletions Modules/_sqlite/module.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
#define PYSQLITE_VERSION "2.6.0"
#define MODULE_NAME "sqlite3"

typedef struct {
PyObject *lru_cache;
} pysqlite_state;

extern pysqlite_state *pysqlite_get_state(PyObject *module);

extern PyObject* pysqlite_Error;
extern PyObject* pysqlite_Warning;
extern PyObject* pysqlite_InterfaceError;
Expand Down