Skip to content
Open
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
Expand Up @@ -278,22 +278,27 @@ pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
goto error;
}

// Create LRU statement cache; returns a new reference.
PyObject *statement_cache = new_statement_cache(self, state, cache_size);
if (statement_cache == NULL) {
goto error;
// GraalPy change: avoid the managed LRU wrapper when caching is disabled.
PyObject *statement_cache = NULL;
if (cache_size > 0) {
statement_cache = new_statement_cache(self, state, cache_size);
if (statement_cache == NULL) {
goto error;
}
}

/* Create lists of weak references to cursors and blobs */
PyObject *cursors = PyList_New(0);
if (cursors == NULL) {
Py_DECREF(statement_cache);
// GraalPy change: the statement cache can be NULL.
Py_XDECREF(statement_cache);
goto error;
}

PyObject *blobs = PyList_New(0);
if (blobs == NULL) {
Py_DECREF(statement_cache);
// GraalPy change: the statement cache can be NULL.
Py_XDECREF(statement_cache);
Py_DECREF(cursors);
goto error;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,15 @@ begin_transaction(pysqlite_Connection *self)
return 0;
}

// GraalPy change: uncached statements stay native so close can finalize them.
static PyObject *
get_statement_from_cache(pysqlite_Cursor *self, PyObject *operation)
get_statement(pysqlite_Cursor *self, PyObject *operation)
{
PyObject *args[] = { NULL, operation, }; // Borrowed ref.
PyObject *cache = self->connection->statement_cache;
if (cache == NULL) {
return (PyObject *)pysqlite_statement_create(self->connection, operation);
}
PyObject *args[] = { NULL, operation, }; // Borrowed ref.
size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
return PyObject_Vectorcall(cache, args + 1, nargsf, NULL);
}
Expand Down Expand Up @@ -838,7 +842,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
(void)stmt_reset(self->statement);
}

PyObject *stmt = get_statement_from_cache(self, operation);
// GraalPy change: the cache can be disabled.
PyObject *stmt = get_statement(self, operation);
Py_XSETREF(self->statement, (pysqlite_Statement *)stmt);
if (!self->statement) {
goto error;
Expand Down
28 changes: 28 additions & 0 deletions graalpython/com.oracle.graal.python.test/src/tests/test_sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import os
import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from tests.util import needs_capi


Expand Down Expand Up @@ -72,3 +77,26 @@ def test_fts5_works():
import sys
assert sys.implementation.name != "graalpy"
conn.close()


@needs_capi
def test_close_releases_uncached_statement_descriptor() -> None:
import sqlite3

descriptors = Path("/proc/self/fd")
if not descriptors.is_dir():
raise unittest.SkipTest("Reading open descriptors requires /proc/self/fd")

with TemporaryDirectory() as directory:
database = Path(directory, "database.sqlite").resolve()
connection = sqlite3.connect(database, cached_statements=0)
try:
cursor = connection.execute("SELECT 1")
try:
assert cursor.fetchone() == (1,)
finally:
cursor.close()
finally:
connection.close()

assert all(os.path.realpath(descriptor) != str(database) for descriptor in descriptors.iterdir())