Fixed #32705 -- Prevented database cache backend from checking .rowcount on closed cursor.

Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com>
This commit is contained in:
ecogels 2021-05-02 10:42:23 +02:00 committed by Mariusz Felisiak
parent 136ff592ad
commit a0a5e0f4c8
2 changed files with 23 additions and 1 deletions

22
tests/cache/tests.py vendored
View File

@ -24,6 +24,7 @@ from django.core.cache import (
from django.core.cache.backends.base import InvalidCacheBackendError from django.core.cache.backends.base import InvalidCacheBackendError
from django.core.cache.utils import make_template_fragment_key from django.core.cache.utils import make_template_fragment_key
from django.db import close_old_connections, connection, connections from django.db import close_old_connections, connection, connections
from django.db.backends.utils import CursorWrapper
from django.http import ( from django.http import (
HttpRequest, HttpResponse, HttpResponseNotModified, StreamingHttpResponse, HttpRequest, HttpResponse, HttpResponseNotModified, StreamingHttpResponse,
) )
@ -1116,6 +1117,27 @@ class DBCacheTests(BaseCacheTests, TransactionTestCase):
with self.assertNumQueries(1): with self.assertNumQueries(1):
cache.delete_many(['a', 'b', 'c']) cache.delete_many(['a', 'b', 'c'])
def test_delete_cursor_rowcount(self):
"""
The rowcount attribute should not be checked on a closed cursor.
"""
class MockedCursorWrapper(CursorWrapper):
is_closed = False
def close(self):
self.cursor.close()
self.is_closed = True
@property
def rowcount(self):
if self.is_closed:
raise Exception('Cursor is closed.')
return self.cursor.rowcount
cache.set_many({'a': 1, 'b': 2})
with mock.patch('django.db.backends.utils.CursorWrapper', MockedCursorWrapper):
self.assertIs(cache.delete('a'), True)
def test_zero_cull(self): def test_zero_cull(self):
self._perform_cull_test('zero_cull', 50, 18) self._perform_cull_test('zero_cull', 50, 18)