From d796c94b03ffbe90895ec68c5041806907cb9577 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Fri, 20 Jul 2012 12:18:38 +0200 Subject: [PATCH] [py3] Used six.reraise wherever necessary. --- django/core/handlers/base.py | 3 ++- django/db/backends/mysql/base.py | 17 +++++++++-------- django/db/backends/oracle/base.py | 19 ++++++++++--------- .../db/backends/postgresql_psycopg2/base.py | 11 ++++++----- django/db/backends/sqlite3/base.py | 9 +++++---- django/db/models/query.py | 3 ++- django/http/__init__.py | 3 ++- django/test/client.py | 3 ++- 8 files changed, 38 insertions(+), 30 deletions(-) diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index 4c07524ecc..7fd7d19c4a 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -7,6 +7,7 @@ from django.core import signals from django.utils.encoding import force_unicode from django.utils.importlib import import_module from django.utils.log import getLogger +from django.utils import six logger = getLogger('django.request') @@ -224,7 +225,7 @@ class BaseHandler(object): # If Http500 handler is not installed, re-raise last exception if resolver.urlconf_module is None: - raise exc_info[1], None, exc_info[2] + six.reraise(exc_info[1], None, exc_info[2]) # Return an HttpResponse that displays a friendly error message. callback, param_dict = resolver.resolve500() return callback(request, **param_dict) diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index f381d48307..201fed66a6 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -39,6 +39,7 @@ from django.db.backends.mysql.introspection import DatabaseIntrospection from django.db.backends.mysql.validation import DatabaseValidation from django.utils.functional import cached_property from django.utils.safestring import SafeString, SafeUnicode +from django.utils import six from django.utils import timezone # Raise exceptions for database warnings if DEBUG is on @@ -117,29 +118,29 @@ class CursorWrapper(object): try: return self.cursor.execute(query, args) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.OperationalError as e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place. if e[0] in self.codes_for_integrityerror: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def executemany(self, query, args): try: return self.cursor.executemany(query, args) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.OperationalError as e: # Map some error codes to IntegrityError, since they seem to be # misclassified and Django would prefer the more logical place. if e[0] in self.codes_for_integrityerror: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def __getattr__(self, attr): if attr in self.__dict__: diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 49b628075b..705ae30ccc 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -53,6 +53,7 @@ from django.db.backends.oracle.client import DatabaseClient from django.db.backends.oracle.creation import DatabaseCreation from django.db.backends.oracle.introspection import DatabaseIntrospection from django.utils.encoding import smart_str, force_unicode +from django.utils import six from django.utils import timezone DatabaseError = Database.DatabaseError @@ -549,7 +550,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): except Database.IntegrityError as e: # In case cx_Oracle implements (now or in a future version) # raising this specific exception - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: # cx_Oracle 5.0.4 raises a cx_Oracle.DatabaseError exception # with the following attributes and values: @@ -561,8 +562,8 @@ class DatabaseWrapper(BaseDatabaseWrapper): x = e.args[0] if hasattr(x, 'code') and hasattr(x, 'message') \ and x.code == 2091 and 'ORA-02291' in x.message: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) class OracleParam(object): @@ -688,12 +689,12 @@ class FormatStylePlaceholderCursor(object): try: return self.cursor.execute(query, self._param_generator(params)) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: # cx_Oracle <= 4.4.0 wrongly raises a DatabaseError for ORA-01400. if hasattr(e.args[0], 'code') and e.args[0].code == 1400 and not isinstance(e, IntegrityError): - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def executemany(self, query, params=None): # cx_Oracle doesn't support iterators, convert them to lists @@ -717,12 +718,12 @@ class FormatStylePlaceholderCursor(object): return self.cursor.executemany(query, [self._param_generator(p) for p in formatted]) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: # cx_Oracle <= 4.4.0 wrongly raises a DatabaseError for ORA-01400. if hasattr(e.args[0], 'code') and e.args[0].code == 1400 and not isinstance(e, IntegrityError): - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def fetchone(self): row = self.cursor.fetchone() diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index f76b91ddd6..8b21b8ceb8 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.version import get_version from django.db.backends.postgresql_psycopg2.introspection import DatabaseIntrospection from django.utils.log import getLogger from django.utils.safestring import SafeUnicode, SafeString +from django.utils import six from django.utils.timezone import utc try: @@ -51,17 +52,17 @@ class CursorWrapper(object): try: return self.cursor.execute(query, args) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def executemany(self, query, args): try: return self.cursor.executemany(query, args) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def __getattr__(self, attr): if attr in self.__dict__: @@ -236,4 +237,4 @@ class DatabaseWrapper(BaseDatabaseWrapper): try: return self.connection.commit() except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 60723124c1..4c7b881e11 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -21,6 +21,7 @@ from django.db.backends.sqlite3.introspection import DatabaseIntrospection from django.utils.dateparse import parse_date, parse_datetime, parse_time from django.utils.functional import cached_property from django.utils.safestring import SafeString +from django.utils import six from django.utils import timezone try: @@ -348,18 +349,18 @@ class SQLiteCursorWrapper(Database.Cursor): try: return Database.Cursor.execute(self, query, params) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def executemany(self, query, param_list): query = self.convert_query(query) try: return Database.Cursor.executemany(self, query, param_list) except Database.IntegrityError as e: - raise utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.IntegrityError, utils.IntegrityError(*tuple(e)), sys.exc_info()[2]) except Database.DatabaseError as e: - raise utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2] + six.reraise(utils.DatabaseError, utils.DatabaseError(*tuple(e)), sys.exc_info()[2]) def convert_query(self, query): return FORMAT_QMARK_REGEX.sub('?', query).replace('%%','%') diff --git a/django/db/models/query.py b/django/db/models/query.py index ebe61a1226..f9e757d9f1 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -14,6 +14,7 @@ from django.db.models.query_utils import (Q, select_related_descend, from django.db.models.deletion import Collector from django.db.models import sql from django.utils.functional import partition +from django.utils import six # Used to control how many objects are worked with at once in some cases (e.g. # when deleting objects). @@ -470,7 +471,7 @@ class QuerySet(object): return self.get(**lookup), False except self.model.DoesNotExist: # Re-raise the IntegrityError with its original traceback. - raise exc_info[1], None, exc_info[2] + six.reraise(exc_info[1], None, exc_info[2]) def latest(self, field_name=None): """ diff --git a/django/http/__init__.py b/django/http/__init__.py index 4b8f70fb4b..da1f9edc63 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -84,6 +84,7 @@ from django.http.utils import * from django.utils.datastructures import MultiValueDict, ImmutableList from django.utils.encoding import smart_str, iri_to_uri, force_unicode from django.utils.http import cookie_date +from django.utils import six from django.utils import timezone RESERVED_CHARS="!*'();:@&=+$,/?%#[]" @@ -290,7 +291,7 @@ class HttpRequest(object): try: self._body = self.read() except IOError as e: - raise UnreadablePostError, e, sys.exc_traceback + six.reraise(UnreadablePostError, e, sys.exc_traceback) self._stream = BytesIO(self._body) return self._body diff --git a/django/test/client.py b/django/test/client.py index 79844426f1..74e11a0efd 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -20,6 +20,7 @@ from django.utils.encoding import smart_str from django.utils.http import urlencode from django.utils.importlib import import_module from django.utils.itercompat import is_iterable +from django.utils import six from django.db import close_connection from django.test.utils import ContextList @@ -381,7 +382,7 @@ class Client(RequestFactory): if self.exc_info: exc_info = self.exc_info self.exc_info = None - raise exc_info[1], None, exc_info[2] + six.reraise(exc_info[1], None, exc_info[2]) # Save the client and request that stimulated the response. response.client = self