Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #21109 -- made db cursor error wrapping faster

Backpatch of 9400142 from master.
  • Loading branch information...
commit 5937f291c1dd74b40ab14be7b8a8112ff91ac08e 1 parent c0625a7
Anssi Kääriäinen authored
11  django/db/backends/__init__.py
@@ -120,29 +120,29 @@ def ensure_connection(self):
120 120
         Guarantees that a connection to the database is established.
121 121
         """
122 122
         if self.connection is None:
123  
-            with self.wrap_database_errors():
  123
+            with self.wrap_database_errors:
124 124
                 self.connect()
125 125
 
126 126
     ##### Backend-specific wrappers for PEP-249 connection methods #####
127 127
 
128 128
     def _cursor(self):
129 129
         self.ensure_connection()
130  
-        with self.wrap_database_errors():
  130
+        with self.wrap_database_errors:
131 131
             return self.create_cursor()
132 132
 
133 133
     def _commit(self):
134 134
         if self.connection is not None:
135  
-            with self.wrap_database_errors():
  135
+            with self.wrap_database_errors:
136 136
                 return self.connection.commit()
137 137
 
138 138
     def _rollback(self):
139 139
         if self.connection is not None:
140  
-            with self.wrap_database_errors():
  140
+            with self.wrap_database_errors:
141 141
                 return self.connection.rollback()
142 142
 
143 143
     def _close(self):
144 144
         if self.connection is not None:
145  
-            with self.wrap_database_errors():
  145
+            with self.wrap_database_errors:
146 146
                 return self.connection.close()
147 147
 
148 148
     ##### Generic wrappers for PEP-249 connection methods #####
@@ -482,6 +482,7 @@ def validate_thread_sharing(self):
482 482
 
483 483
     ##### Miscellaneous #####
484 484
 
  485
+    @cached_property
485 486
     def wrap_database_errors(self):
486 487
         """
487 488
         Context manager and decorator that re-throws backend-specific database
16  django/db/backends/util.py
@@ -19,13 +19,17 @@ def __init__(self, cursor, db):
19 19
         self.cursor = cursor
20 20
         self.db = db
21 21
 
  22
+    SET_DIRTY_ATTRS = frozenset(['execute', 'executemany', 'callproc'])
  23
+    WRAP_ERROR_ATTRS = frozenset([
  24
+        'callproc', 'close', 'execute', 'executemany',
  25
+        'fetchone', 'fetchmany', 'fetchall', 'nextset'])
  26
+
22 27
     def __getattr__(self, attr):
23  
-        if attr in ('execute', 'executemany', 'callproc'):
  28
+        if attr in CursorWrapper.SET_DIRTY_ATTRS:
24 29
             self.db.set_dirty()
25 30
         cursor_attr = getattr(self.cursor, attr)
26  
-        if attr in ('callproc', 'close', 'execute', 'executemany',
27  
-                    'fetchone', 'fetchmany', 'fetchall', 'nextset'):
28  
-            return self.db.wrap_database_errors()(cursor_attr)
  31
+        if attr in CursorWrapper.WRAP_ERROR_ATTRS:
  32
+            return self.db.wrap_database_errors(cursor_attr)
29 33
         else:
30 34
             return cursor_attr
31 35
 
@@ -39,7 +43,7 @@ def execute(self, sql, params=None):
39 43
         self.db.set_dirty()
40 44
         start = time()
41 45
         try:
42  
-            with self.db.wrap_database_errors():
  46
+            with self.db.wrap_database_errors:
43 47
                 if params is None:
44 48
                     # params default might be backend specific
45 49
                     return self.cursor.execute(sql)
@@ -60,7 +64,7 @@ def executemany(self, sql, param_list):
60 64
         self.db.set_dirty()
61 65
         start = time()
62 66
         try:
63  
-            with self.db.wrap_database_errors():
  67
+            with self.db.wrap_database_errors:
64 68
                 return self.cursor.executemany(sql, param_list)
65 69
         finally:
66 70
             stop = time()
3  django/db/utils.py
@@ -99,7 +99,8 @@ def __exit__(self, exc_type, exc_value, traceback):
99 99
                 six.reraise(dj_exc_type, dj_exc_value, traceback)
100 100
 
101 101
     def __call__(self, func):
102  
-        @wraps(func)
  102
+        # Note that we are intentionally not using @wraps here for performance
  103
+        # reasons. Refs #21109.
103 104
         def inner(*args, **kwargs):
104 105
             with self:
105 106
                 return func(*args, **kwargs)

0 notes on commit 5937f29

Please sign in to comment.
Something went wrong with that request. Please try again.