[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
This commit is contained in:
Rémy Voet (ryv)
2022-09-09 11:11:58 +02:00
parent 16d55910c1
commit db76b065ee
2 changed files with 15 additions and 4 deletions
+13 -4
View File
@@ -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):
+2
View File
@@ -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