diff options
| author | Tim Graham <timograham@gmail.com> | 2017-09-07 08:16:21 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-09-07 08:16:21 -0400 |
| commit | 6e4c6281dbb7ee12bcdc22620894edb4e9cf623f (patch) | |
| tree | 1c21218d4b6f00c499f18943d5190ebe7b5248c9 /django/db/models | |
| parent | 8b2515a450ef376b9205029090af0a79c8341bd7 (diff) | |
Reverted "Fixed #27818 -- Replaced try/except/pass with contextlib.suppress()."
This reverts commit 550cb3a365dee4edfdd1563224d5304de2a57fda
because try/except performs better.
Diffstat (limited to 'django/db/models')
| -rw-r--r-- | django/db/models/expressions.py | 5 | ||||
| -rw-r--r-- | django/db/models/options.py | 17 | ||||
| -rw-r--r-- | django/db/models/query.py | 10 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 5 |
4 files changed, 25 insertions, 12 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index ff7921b520..db62541559 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -1,6 +1,5 @@ import copy import datetime -from contextlib import suppress from decimal import Decimal from django.core.exceptions import EmptyResultSet, FieldError @@ -17,9 +16,11 @@ class SQLiteNumericMixin: """ def as_sqlite(self, compiler, connection, **extra_context): sql, params = self.as_sql(compiler, connection, **extra_context) - with suppress(FieldError): + try: if self.output_field.get_internal_type() == 'DecimalField': sql = 'CAST(%s AS NUMERIC)' % sql + except FieldError: + pass return sql, params diff --git a/django/db/models/options.py b/django/db/models/options.py index c81a61c916..9f0746bd58 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -3,7 +3,6 @@ import inspect import warnings from bisect import bisect from collections import OrderedDict, defaultdict -from contextlib import suppress from django.apps import apps from django.conf import settings @@ -269,8 +268,10 @@ class Options: # is a cached property, and all the models haven't been loaded yet, so # we need to make sure we don't cache a string reference. if field.is_relation and hasattr(field.remote_field, 'model') and field.remote_field.model: - with suppress(AttributeError): + try: field.remote_field.model._meta._expire_cache(forward=False) + except AttributeError: + pass self._expire_cache() else: self._expire_cache(reverse=False) @@ -518,8 +519,10 @@ class Options: # Due to the way Django's internals work, get_field() should also # be able to fetch a field by attname. In the case of a concrete # field with relation, includes the *_id name too - with suppress(AttributeError): + try: res[field.attname] = field + except AttributeError: + pass return res @cached_property @@ -531,8 +534,10 @@ class Options: # Due to the way Django's internals work, get_field() should also # be able to fetch a field by attname. In the case of a concrete # field with relation, includes the *_id name too - with suppress(AttributeError): + try: res[field.attname] = field + except AttributeError: + pass return res def get_field(self, field_name): @@ -749,10 +754,12 @@ class Options: # Creates a cache key composed of all arguments cache_key = (forward, reverse, include_parents, include_hidden, topmost_call) - with suppress(KeyError): + try: # In order to avoid list manipulation. Always return a shallow copy # of the results. return self._get_fields_cache[cache_key] + except KeyError: + pass fields = [] # Recursively call _get_fields() on each parent, with the same diff --git a/django/db/models/query.py b/django/db/models/query.py index a690ba4b72..42fb728190 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -7,7 +7,6 @@ import operator import sys import warnings from collections import OrderedDict, namedtuple -from contextlib import suppress from functools import lru_cache from itertools import chain @@ -521,8 +520,10 @@ class QuerySet: return obj, True except IntegrityError: exc_info = sys.exc_info() - with suppress(self.model.DoesNotExist): + try: return self.get(**lookup), False + except self.model.DoesNotExist: + pass raise exc_info[0](exc_info[1]).with_traceback(exc_info[2]) def _extract_model_params(self, defaults, **kwargs): @@ -1337,8 +1338,11 @@ class RawQuerySet: # Adjust any column names which don't match field names for (query_name, model_name) in self.translations.items(): # Ignore translations for nonexistent column names - with suppress(ValueError): + try: index = columns.index(query_name) + except ValueError: + pass + else: columns[index] = model_name return columns diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 509d97e65d..017edea873 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -7,7 +7,6 @@ databases). The abstraction barrier only works one way: this module has to know all about the internals of models in order to get the information it needs. """ from collections import Counter, Iterator, Mapping, OrderedDict, namedtuple -from contextlib import suppress from itertools import chain, count, product from string import ascii_uppercase @@ -313,8 +312,10 @@ class Query: obj.subq_aliases = self.subq_aliases.copy() obj.used_aliases = self.used_aliases.copy() # Clear the cached_property - with suppress(AttributeError): + try: del obj.base_table + except AttributeError: + pass return obj def chain(self, klass=None): |
