summaryrefslogtreecommitdiff
path: root/django/db
diff options
context:
space:
mode:
authorMichael Manfre <mmanfre@gmail.com>2013-09-23 20:17:59 -0400
committerAnssi Kääriäinen <akaariai@gmail.com>2013-09-25 21:47:26 +0300
commit99c87f1410106ce543a1a0332428afc472beef7f (patch)
treeee2c17e7f246e842ede59c45af504209c036170d /django/db
parent04a2a6b0f9cb6bb98edfe84bf4361216d60a4e38 (diff)
Fixed #17671 - Cursors are now context managers.
Diffstat (limited to 'django/db')
-rw-r--r--django/db/backends/__init__.py5
-rw-r--r--django/db/backends/postgresql_psycopg2/base.py2
-rw-r--r--django/db/backends/utils.py8
3 files changed, 14 insertions, 1 deletions
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 131a73715d..74046a0d9b 100644
--- a/django/db/backends/__init__.py
+++ b/django/db/backends/__init__.py
@@ -1,7 +1,7 @@
import datetime
import time
-from django.db.utils import DatabaseError
+from django.db.utils import DatabaseError, ProgrammingError
try:
from django.utils.six.moves import _thread as thread
@@ -664,6 +664,9 @@ class BaseDatabaseFeatures(object):
# Does the backend require a connection reset after each material schema change?
connection_persists_old_columns = False
+ # What kind of error does the backend throw when accessing closed cursor?
+ closed_cursor_error_class = ProgrammingError
+
def __init__(self, connection):
self.connection = connection
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index 71ff2811e2..b22c653bd7 100644
--- a/django/db/backends/postgresql_psycopg2/base.py
+++ b/django/db/backends/postgresql_psycopg2/base.py
@@ -15,6 +15,7 @@ from django.db.backends.postgresql_psycopg2.creation import DatabaseCreation
from django.db.backends.postgresql_psycopg2.version import get_version
from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection
from django.db.backends.postgresql_psycopg2.schema import DatabaseSchemaEditor
+from django.db.utils import InterfaceError
from django.utils.encoding import force_str
from django.utils.functional import cached_property
from django.utils.safestring import SafeText, SafeBytes
@@ -60,6 +61,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
can_rollback_ddl = True
supports_combined_alters = True
nulls_order_largest = True
+ closed_cursor_error_class = InterfaceError
class DatabaseWrapper(BaseDatabaseWrapper):
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index 0e2aa4503e..ccda77f658 100644
--- a/django/db/backends/utils.py
+++ b/django/db/backends/utils.py
@@ -36,6 +36,14 @@ class CursorWrapper(object):
def __iter__(self):
return iter(self.cursor)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, type, value, traceback):
+ # Ticket #17671 - Close instead of passing thru to avoid backend
+ # specific behavior.
+ self.close()
+
class CursorDebugWrapper(CursorWrapper):