diff options
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/postgresql/client.py | 9 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/operations.py | 10 | ||||
| -rw-r--r-- | django/db/migrations/autodetector.py | 5 | ||||
| -rw-r--r-- | django/db/migrations/state.py | 6 | ||||
| -rw-r--r-- | django/db/migrations/writer.py | 5 | ||||
| -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 |
9 files changed, 46 insertions, 26 deletions
diff --git a/django/db/backends/postgresql/client.py b/django/db/backends/postgresql/client.py index 8d08b0d5cf..6d4cc9b692 100644 --- a/django/db/backends/postgresql/client.py +++ b/django/db/backends/postgresql/client.py @@ -1,7 +1,6 @@ import os import signal import subprocess -from contextlib import suppress from django.core.files.temp import NamedTemporaryFile from django.db.backends.base.client import BaseDatabaseClient @@ -41,9 +40,7 @@ class DatabaseClient(BaseDatabaseClient): if passwd: # Create temporary .pgpass file. temp_pgpass = NamedTemporaryFile(mode='w+') - # If the current locale can't encode the data, let the user - # input the password manually. - with suppress(UnicodeEncodeError): + try: print( _escape_pgpass(host) or '*', str(port) or '*', @@ -55,6 +52,10 @@ class DatabaseClient(BaseDatabaseClient): flush=True, ) os.environ['PGPASSFILE'] = temp_pgpass.name + except UnicodeEncodeError: + # If the current locale can't encode the data, let the + # user input the password manually. + pass # Allow SIGINT to pass to psql to abort queries. signal.signal(signal.SIGINT, signal.SIG_IGN) subprocess.check_call(args) diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py index 774d549461..408848c9ad 100644 --- a/django/db/backends/sqlite3/operations.py +++ b/django/db/backends/sqlite3/operations.py @@ -1,6 +1,5 @@ import datetime import uuid -from contextlib import suppress from django.conf import settings from django.core.exceptions import FieldError @@ -36,10 +35,13 @@ class DatabaseOperations(BaseDatabaseOperations): bad_aggregates = (aggregates.Sum, aggregates.Avg, aggregates.Variance, aggregates.StdDev) if isinstance(expression, bad_aggregates): for expr in expression.get_source_expressions(): - # Not every subexpression has an output_field which is fine - # to ignore. - with suppress(FieldError): + try: output_field = expr.output_field + except FieldError: + # Not every subexpression has an output_field which is fine + # to ignore. + pass + else: if isinstance(output_field, bad_fields): raise NotImplementedError( 'You cannot use Sum, Avg, StdDev, and Variance ' diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py index e79e745978..ece58b9bab 100644 --- a/django/db/migrations/autodetector.py +++ b/django/db/migrations/autodetector.py @@ -1,6 +1,5 @@ import functools import re -from contextlib import suppress from itertools import chain from django.conf import settings @@ -435,7 +434,7 @@ class MigrationAutodetector: Place potential swappable models first in lists of created models (only real way to solve #22783). """ - with suppress(LookupError): + try: model = self.new_apps.get_model(item[0], item[1]) base_names = [base.__name__ for base in model.__bases__] string_version = "%s.%s" % (item[0], item[1]) @@ -446,6 +445,8 @@ class MigrationAutodetector: settings.AUTH_USER_MODEL.lower() == string_version.lower() ): return ("___" + item[0], "___" + item[1]) + except LookupError: + pass return item def generate_renamed_models(self): diff --git a/django/db/migrations/state.py b/django/db/migrations/state.py index 563f837c3b..dc644161fc 100644 --- a/django/db/migrations/state.py +++ b/django/db/migrations/state.py @@ -1,6 +1,6 @@ import copy from collections import OrderedDict -from contextlib import contextmanager, suppress +from contextlib import contextmanager from django.apps import AppConfig from django.apps.registry import Apps, apps as global_apps @@ -338,9 +338,11 @@ class StateApps(Apps): self.clear_cache() def unregister_model(self, app_label, model_name): - with suppress(KeyError): + try: del self.all_models[app_label][model_name] del self.app_configs[app_label].models[model_name] + except KeyError: + pass class ModelState: diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 00ff03e494..aa296db8c5 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -1,6 +1,5 @@ import os import re -from contextlib import suppress from importlib import import_module from django import get_version @@ -223,8 +222,10 @@ class MigrationWriter: except ImportError: pass else: - with suppress(ValueError): + try: return module_dir(migrations_module) + except ValueError: + pass # Alright, see if it's a direct submodule of the app app_config = apps.get_app_config(self.migration.app_label) 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): |
