From 23bf4ad87f86f44a5ecf9aea722ced76fe7b7fdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B8=D0=BB=D1=8F=D0=BD=20=D0=9F=D0=B0=D0=BB=D0=B0?= =?UTF-8?q?=D1=83=D0=B7=D0=BE=D0=B2?= Date: Mon, 13 Nov 2017 16:15:49 -0500 Subject: [PATCH] Fixed #28795 -- Removed 'not in' checks and used dict.setdefault(). --- django/contrib/gis/apps.py | 3 +-- django/contrib/gis/db/models/fields.py | 5 ++--- django/contrib/gis/gdal/raster/source.py | 27 ++++++----------------- django/db/migrations/operations/models.py | 4 ++-- django/db/models/sql/query.py | 3 +-- django/forms/boundfield.py | 16 +++----------- django/http/response.py | 5 +---- django/middleware/locale.py | 3 +-- django/middleware/security.py | 8 +++---- django/template/defaulttags.py | 3 +-- django/views/decorators/http.py | 4 ++-- django/views/generic/base.py | 3 +-- tests/runtests.py | 3 +-- 13 files changed, 27 insertions(+), 60 deletions(-) diff --git a/django/contrib/gis/apps.py b/django/contrib/gis/apps.py index ffbbe63273..662ae43d8d 100644 --- a/django/contrib/gis/apps.py +++ b/django/contrib/gis/apps.py @@ -8,5 +8,4 @@ class GISConfig(AppConfig): verbose_name = _("GIS") def ready(self): - if 'geojson' not in serializers.BUILTIN_SERIALIZERS: - serializers.BUILTIN_SERIALIZERS['geojson'] = "django.contrib.gis.serializers.geojson" + serializers.BUILTIN_SERIALIZERS.setdefault('geojson', 'django.contrib.gis.serializers.geojson') diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py index f0329b2ec1..fea3cfc260 100644 --- a/django/contrib/gis/db/models/fields.py +++ b/django/contrib/gis/db/models/fields.py @@ -255,9 +255,8 @@ class GeometryField(BaseSpatialField): 'srid': self.srid, } defaults.update(kwargs) - if (self.dim > 2 and 'widget' not in kwargs and - not getattr(defaults['form_class'].widget, 'supports_3d', False)): - defaults['widget'] = forms.Textarea + if self.dim > 2 and not getattr(defaults['form_class'].widget, 'supports_3d', False): + defaults.setdefault('widget', forms.Textarea) return super().formfield(**defaults) def select_format(self, compiler, sql, params): diff --git a/django/contrib/gis/gdal/raster/source.py b/django/contrib/gis/gdal/raster/source.py index bf84eeef1a..8c35b45cb5 100644 --- a/django/contrib/gis/gdal/raster/source.py +++ b/django/contrib/gis/gdal/raster/source.py @@ -381,27 +381,14 @@ class GDALRaster(GDALRasterBase): consult the GDAL_RESAMPLE_ALGORITHMS constant. """ # Get the parameters defining the geotransform, srid, and size of the raster - if 'width' not in ds_input: - ds_input['width'] = self.width - - if 'height' not in ds_input: - ds_input['height'] = self.height - - if 'srid' not in ds_input: - ds_input['srid'] = self.srs.srid - - if 'origin' not in ds_input: - ds_input['origin'] = self.origin - - if 'scale' not in ds_input: - ds_input['scale'] = self.scale - - if 'skew' not in ds_input: - ds_input['skew'] = self.skew - + ds_input.setdefault('width', self.width) + ds_input.setdefault('height', self.height) + ds_input.setdefault('srid', self.srs.srid) + ds_input.setdefault('origin', self.origin) + ds_input.setdefault('scale', self.scale) + ds_input.setdefault('skew', self.skew) # Get the driver, name, and datatype of the target raster - if 'driver' not in ds_input: - ds_input['driver'] = self.driver.name + ds_input.setdefault('driver', self.driver.name) if 'name' not in ds_input: ds_input['name'] = self.name + '_copy.' + self.driver.name diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 9754181e84..a1ff594602 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -693,8 +693,8 @@ class AlterModelOptions(ModelOptionOperation): model_state.options = dict(model_state.options) model_state.options.update(self.options) for key in self.ALTER_OPTION_KEYS: - if key not in self.options and key in model_state.options: - del model_state.options[key] + if key not in self.options: + model_state.options.pop(key, False) state.reload_model(app_label, self.name_lower, delay=True) def database_forwards(self, app_label, schema_editor, from_state, to_state): diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 57da2fe68b..e7456de037 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -692,8 +692,7 @@ class Query: # in the parent list. Again, it must be mentioned to ensure that # only "must include" fields are pulled in. for model in orig_opts.get_parent_list(): - if model not in seen: - seen[model] = set() + seen.setdefault(model, set()) for model, values in seen.items(): callback(target, model, values) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 6acae1c59b..dba54e588a 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -87,20 +87,10 @@ class BoundField: attrs = attrs or {} attrs = self.build_widget_attrs(attrs, widget) - auto_id = self.auto_id - if auto_id and 'id' not in attrs and 'id' not in widget.attrs: - if not only_initial: - attrs['id'] = auto_id - else: - attrs['id'] = self.html_initial_id - - if not only_initial: - name = self.html_name - else: - name = self.html_initial_name - + if self.auto_id and 'id' not in widget.attrs: + attrs.setdefault('id', self.html_initial_id if only_initial else self.auto_id) return widget.render( - name=name, + name=self.html_initial_name if only_initial else self.html_name, value=self.value(), attrs=attrs, renderer=self.form.renderer, diff --git a/django/http/response.py b/django/http/response.py index ae14333053..c07dc5b6e0 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -136,10 +136,7 @@ class HttpResponseBase: self._headers[header.lower()] = (header, value) def __delitem__(self, header): - try: - del self._headers[header.lower()] - except KeyError: - pass + self._headers.pop(header.lower(), False) def __getitem__(self, header): return self._headers[header.lower()][1] diff --git a/django/middleware/locale.py b/django/middleware/locale.py index ee057786ee..2399debf6e 100644 --- a/django/middleware/locale.py +++ b/django/middleware/locale.py @@ -57,6 +57,5 @@ class LocaleMiddleware(MiddlewareMixin): if not (i18n_patterns_used and language_from_path): patch_vary_headers(response, ('Accept-Language',)) - if 'Content-Language' not in response: - response['Content-Language'] = language + response.setdefault('Content-Language', language) return response diff --git a/django/middleware/security.py b/django/middleware/security.py index 7bcb72738e..296567432f 100644 --- a/django/middleware/security.py +++ b/django/middleware/security.py @@ -37,10 +37,10 @@ class SecurityMiddleware(MiddlewareMixin): sts_header = sts_header + "; preload" response["strict-transport-security"] = sts_header - if self.content_type_nosniff and 'x-content-type-options' not in response: - response["x-content-type-options"] = "nosniff" + if self.content_type_nosniff: + response.setdefault('x-content-type-options', 'nosniff') - if self.xss_filter and 'x-xss-protection' not in response: - response["x-xss-protection"] = "1; mode=block" + if self.xss_filter: + response.setdefault('x-xss-protection', '1; mode=block') return response diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 212aa8987a..143082c47d 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -225,8 +225,7 @@ class IfChangedNode(Node): def render(self, context): # Init state storage state_frame = self._get_context_stack_frame(context) - if self not in state_frame: - state_frame[self] = None + state_frame.setdefault(self) nodelist_true_output = None if self._varlist: diff --git a/django/views/decorators/http.py b/django/views/decorators/http.py index 81355d7725..1c8502fb1e 100644 --- a/django/views/decorators/http.py +++ b/django/views/decorators/http.py @@ -103,8 +103,8 @@ def condition(etag_func=None, last_modified_func=None): if request.method in ('GET', 'HEAD'): if res_last_modified and not response.has_header('Last-Modified'): response['Last-Modified'] = http_date(res_last_modified) - if res_etag and not response.has_header('ETag'): - response['ETag'] = res_etag + if res_etag: + response.setdefault('ETag', res_etag) return response diff --git a/django/views/generic/base.py b/django/views/generic/base.py index 7938aeaebc..cea077c6d1 100644 --- a/django/views/generic/base.py +++ b/django/views/generic/base.py @@ -21,8 +21,7 @@ class ContextMixin: extra_context = None def get_context_data(self, **kwargs): - if 'view' not in kwargs: - kwargs['view'] = self + kwargs.setdefault('view', self) if self.extra_context is not None: kwargs.update(self.extra_context) return kwargs diff --git a/tests/runtests.py b/tests/runtests.py index 57b8a45404..ae2d693ac2 100755 --- a/tests/runtests.py +++ b/tests/runtests.py @@ -455,8 +455,7 @@ if __name__ == "__main__": if options.settings: os.environ['DJANGO_SETTINGS_MODULE'] = options.settings else: - if "DJANGO_SETTINGS_MODULE" not in os.environ: - os.environ['DJANGO_SETTINGS_MODULE'] = 'test_sqlite' + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_sqlite') options.settings = os.environ['DJANGO_SETTINGS_MODULE'] if options.selenium: