diff options
| author | Anssi Kääriäinen <akaariai@gmail.com> | 2013-09-15 10:12:16 +0300 |
|---|---|---|
| committer | Anssi Kääriäinen <akaariai@gmail.com> | 2013-09-17 12:31:13 +0300 |
| commit | 5937f291c1dd74b40ab14be7b8a8112ff91ac08e (patch) | |
| tree | a52463ec2bddf12250822d61f29bf43714ca6e7f | |
| parent | c0625a74ce1bdc4e0fe6827a7106f653518b2c4c (diff) | |
[1.6.x] Fixed #21109 -- made db cursor error wrapping faster
Backpatch of 94001421321bd8808c4027a72aa32a1eef005764 from master.
| -rw-r--r-- | django/db/backends/__init__.py | 11 | ||||
| -rw-r--r-- | django/db/backends/util.py | 16 | ||||
| -rw-r--r-- | django/db/utils.py | 3 |
3 files changed, 18 insertions, 12 deletions
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index a0813cceb0..3184c11f48 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -120,29 +120,29 @@ class BaseDatabaseWrapper(object): Guarantees that a connection to the database is established. """ if self.connection is None: - with self.wrap_database_errors(): + with self.wrap_database_errors: self.connect() ##### Backend-specific wrappers for PEP-249 connection methods ##### def _cursor(self): self.ensure_connection() - with self.wrap_database_errors(): + with self.wrap_database_errors: return self.create_cursor() def _commit(self): if self.connection is not None: - with self.wrap_database_errors(): + with self.wrap_database_errors: return self.connection.commit() def _rollback(self): if self.connection is not None: - with self.wrap_database_errors(): + with self.wrap_database_errors: return self.connection.rollback() def _close(self): if self.connection is not None: - with self.wrap_database_errors(): + with self.wrap_database_errors: return self.connection.close() ##### Generic wrappers for PEP-249 connection methods ##### @@ -482,6 +482,7 @@ class BaseDatabaseWrapper(object): ##### Miscellaneous ##### + @cached_property def wrap_database_errors(self): """ Context manager and decorator that re-throws backend-specific database diff --git a/django/db/backends/util.py b/django/db/backends/util.py index aa2601277a..43ceff095b 100644 --- a/django/db/backends/util.py +++ b/django/db/backends/util.py @@ -19,13 +19,17 @@ class CursorWrapper(object): self.cursor = cursor self.db = db + SET_DIRTY_ATTRS = frozenset(['execute', 'executemany', 'callproc']) + WRAP_ERROR_ATTRS = frozenset([ + 'callproc', 'close', 'execute', 'executemany', + 'fetchone', 'fetchmany', 'fetchall', 'nextset']) + def __getattr__(self, attr): - if attr in ('execute', 'executemany', 'callproc'): + if attr in CursorWrapper.SET_DIRTY_ATTRS: self.db.set_dirty() cursor_attr = getattr(self.cursor, attr) - if attr in ('callproc', 'close', 'execute', 'executemany', - 'fetchone', 'fetchmany', 'fetchall', 'nextset'): - return self.db.wrap_database_errors()(cursor_attr) + if attr in CursorWrapper.WRAP_ERROR_ATTRS: + return self.db.wrap_database_errors(cursor_attr) else: return cursor_attr @@ -39,7 +43,7 @@ class CursorDebugWrapper(CursorWrapper): self.db.set_dirty() start = time() try: - with self.db.wrap_database_errors(): + with self.db.wrap_database_errors: if params is None: # params default might be backend specific return self.cursor.execute(sql) @@ -60,7 +64,7 @@ class CursorDebugWrapper(CursorWrapper): self.db.set_dirty() start = time() try: - with self.db.wrap_database_errors(): + with self.db.wrap_database_errors: return self.cursor.executemany(sql, param_list) finally: stop = time() diff --git a/django/db/utils.py b/django/db/utils.py index bd7e10d24c..4afc592e64 100644 --- a/django/db/utils.py +++ b/django/db/utils.py @@ -99,7 +99,8 @@ class DatabaseErrorWrapper(object): six.reraise(dj_exc_type, dj_exc_value, traceback) def __call__(self, func): - @wraps(func) + # Note that we are intentionally not using @wraps here for performance + # reasons. Refs #21109. def inner(*args, **kwargs): with self: return func(*args, **kwargs) |
