8000 Address review: add global state and add LRU cache func to it · python/cpython@a95a386 · GitHub
[go: up one dir, main page]

Skip to content

Commit a95a386

Browse files
author
Erlend E. Aasland
committed
Address review: add global state and add LRU cache func to it
1 parent 0067cef commit a95a386

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

Modules/_sqlite/connection.c

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,15 @@ static const char * const begin_statements[] = {
5656
static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
5757
static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
5858

59-
static PyObject *_lru_cache = NULL;
60-
61-
int
62-
load_functools_lru_cache(PyObject *Py_UNUSED(module))
63-
{
64-
PyObject *functools = PyImport_ImportModule("functools");
65-
if (functools == NULL) {
66-
return -1;
67-
}
68-
_lru_cache = PyObject_GetAttrString(functools, "lru_cache");
69-
Py_DECREF(functools);
70-
if (_lru_cache == NULL) {
71-
return -1;
72-
}
73-
return 0;
74-
}
75-
7659
static PyObject *
7760
new_statement_cache(pysqlite_Connection *self, int maxsize)
7861
{
7962
PyObject *args[] = { PyLong_FromLong(maxsize), };
8063
if (args[0] == NULL) {
8164
return NULL;
8265
}
83-
PyObject *inner = PyObject_Vectorcall(_lru_cache, args, 1, NULL);
66+
pysqlite_state *state = pysqlite_get_state(NULL);
67+
PyObject *inner = PyObject_Vectorcall(state->lru_cache, args, 1, NULL);
8468
Py_DECREF(args[0]);
8569
if (inner == NULL) {
8670
return NULL;

Modules/_sqlite/connection.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,5 @@ int pysqlite_check_thread(pysqlite_Connection* self);
114114
int pysqlite_check_connection(pysqlite_Connection* con);
115115

116116
int pysqlite_connection_setup_types(PyObject *module);
117-
int load_functools_lru_cache(PyObject *module);
118117

119118
#endif

Modules/_sqlite/module.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ PyObject* _pysqlite_converters = NULL;
5555
int _pysqlite_enable_callback_tracebacks = 0;
5656
int pysqlite_BaseTypeAdapted = 0;
5757

58+
pysqlite_state pysqlite_global_state;
59+
60+
pysqlite_state *
61+
pysqlite_get_state(PyObject *Py_UNUSED(module))
62+
{
63+
return &pysqlite_global_state;
64+
}
65+
5866
static PyObject* module_connect(PyObject* self, PyObject* args, PyObject*
5967
kwargs)
6068
{
@@ -260,6 +268,23 @@ static int converters_init(PyObject* module)
260268
return res;
261269
}
262270

271+
static int
272+
load_functools_lru_cache(PyObject *module)
273+
{
274+
PyObject *functools = PyImport_ImportModule("functools");
275+
if (functools == NULL) {
276+
return -1;
277+
}
278+
279+
pysqlite_state *state = pysqlite_get_state(module);
280+
state->lru_cache = PyObject_GetAttrString(functools, "lru_cache");
281+
Py_DECREF(functools);
282+
if (state->lru_cache == NULL) {
283+
return -1;
284+
}
285+
return 0;
286+
}
287+
263288
static PyMethodDef module_methods[] = {
264289
{"connect", (PyCFunction)(void(*)(void))module_connect,
265290
METH_VARARGS | METH_KEYWORDS, module_connect_doc},

Modules/_sqlite/module.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
#define PYSQLITE_VERSION "2.6.0"
3030
#define MODULE_NAME "sqlite3"
3131

32+
typedef struct {
33+
PyObject *lru_cache;
34+
} pysqlite_state;
35+
36+
extern pysqlite_state *pysqlite_get_state(PyObject *module);
37+
3238
extern PyObject* pysqlite_Error;
3339
extern PyObject* pysqlite_Warning;
3440
extern PyObject* pysqlite_InterfaceError;

0 commit comments

Comments
 (0)
0