From 16034d18822bcbc36b2a32a604c6b1e4f8e0bc33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Thu, 5 Mar 2026 08:10:46 +0100 Subject: [PATCH] Explicitly close sqlite connection The context manage on Connection only commit the connection but it should still be explicitly closed. Closes #13859 (grafted from b529128ca42c72ed76c0b19c71277f917488d05c) --HG-- branch : 7.0 --- trytond/trytond/backend/sqlite/database.py | 4 ++-- trytond/trytond/tests/test_tryton.py | 27 ++++++++++++---------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/trytond/trytond/backend/sqlite/database.py b/trytond/trytond/backend/sqlite/database.py index b72d2e092e..7d54279718 100644 --- a/trytond/trytond/backend/sqlite/database.py +++ b/trytond/trytond/backend/sqlite/database.py @@ -485,8 +485,8 @@ class Database(DatabaseInterface): path = os.path.join(config.get('database', 'path'), database_name + '.sqlite') with sqlite.connect(path) as conn: - cursor = conn.cursor() - cursor.close() + conn.cursor() + conn.close() @classmethod def drop(cls, connection, database_name): diff --git a/trytond/trytond/tests/test_tryton.py b/trytond/trytond/tests/test_tryton.py index 7fbc338a9c..23eaf9cf65 100644 --- a/trytond/trytond/tests/test_tryton.py +++ b/trytond/trytond/tests/test_tryton.py @@ -165,19 +165,22 @@ def _sqlite_copy(file_, restore=False): finally: database.put_connection(connection, True) - with Transaction().start(DB_NAME, 0) as transaction, \ - sqlite.connect(file_) as conn2: + with Transaction().start(DB_NAME, 0) as transaction: conn1 = transaction.connection - if restore: - conn2, conn1 = conn1, conn2 - if hasattr(conn1, 'backup'): - conn1.backup(conn2) - else: - try: - import sqlitebck - except ImportError: - return False - sqlitebck.copy(conn1, conn2) + with sqlite.connect(file_) as conn2: + if restore: + in_, out = conn2, conn1 + else: + in_, out = conn1, conn2 + if hasattr(in_, 'backup'): + in_.backup(out) + else: + try: + import sqlitebck + except ImportError: + return False + sqlitebck.copy(in_, out) + conn2.close() return True