From db76b065ee739740216cd6f1c8cd11de1a362288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Fri, 18 Feb 2022 08:23:13 +0000 Subject: [PATCH] [FIX] sql_db.py: fix error type raised by closed cursor Since e014cc88394fb9c8f6cff556ca73d5efafea030f, our proxy `Cursor` object doesn't return a understandable error when we try to use it when it is already closed (`NoneType` or `Recursion` error). Instead of reverting e014cc88394fb9c8f6cff556ca73d5efafea030f, add a check in `__getitem__` raising a IterfaceError (like psycopg) if we try to use psycopg cursor (`self._obj`) task-2766494 Part-of: odoo/odoo#85078 --- odoo/addons/base/tests/test_db_cursor.py | 17 +++++++++++++---- odoo/sql_db.py | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/odoo/addons/base/tests/test_db_cursor.py b/odoo/addons/base/tests/test_db_cursor.py index ff67016a464..1edaf62c766 100644 --- a/odoo/addons/base/tests/test_db_cursor.py +++ b/odoo/addons/base/tests/test_db_cursor.py @@ -3,11 +3,12 @@ import logging from functools import partial +import psycopg2 + import odoo from odoo.sql_db import TestCursor from odoo.tests import common from odoo.tests.common import BaseCase -from odoo.tools.misc import mute_logger ADMIN_USER_ID = common.ADMIN_USER_ID @@ -15,10 +16,8 @@ def registry(): return odoo.registry(common.get_db_name()) -class TestExecute(BaseCase): - """ Try cr.execute with wrong parameters """ +class TestRealCursor(BaseCase): - @mute_logger('odoo.sql_db') def test_execute_bad_params(self): """ Try to use iterable but non-list or int params in query parameters. @@ -31,6 +30,16 @@ class TestExecute(BaseCase): with self.assertRaises(ValueError): cr.execute("SELECT id FROM res_users WHERE id=%s", '1') + def test_using_closed_cursor(self): + with registry().cursor() as cr: + cr.close() + with self.assertRaises(psycopg2.InterfaceError): + cr.execute("SELECT 1") + + def test_multiple_close_call_cursor(self): + cr = registry().cursor() + cr.close() + cr.close() class TestTestCursor(common.TransactionCase): def setUp(self): diff --git a/odoo/sql_db.py b/odoo/sql_db.py index 3915268d34c..1c639cfbd90 100644 --- a/odoo/sql_db.py +++ b/odoo/sql_db.py @@ -539,6 +539,8 @@ class Cursor(BaseCursor): return result def __getattr__(self, name): + if self._closed and name == '_obj': + raise psycopg2.InterfaceError("Cursor already closed") return getattr(self._obj, name) @property