From 8ad18103a190d04bb54f37425b4f87394b71a205 Mon Sep 17 00:00:00 2001 From: Benjamin Bach Date: Sat, 26 Dec 2015 21:11:53 +0100 Subject: [PATCH] Replaced dict.setdefault() usage to avoid unnecessary object instantiations. --- django/contrib/postgres/forms/ranges.py | 6 ++++-- django/template/backends/jinja2.py | 3 ++- django/test/client.py | 4 +++- django/utils/formats.py | 4 +++- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/django/contrib/postgres/forms/ranges.py b/django/contrib/postgres/forms/ranges.py index 75e5691d833..9ed95cd7c5f 100644 --- a/django/contrib/postgres/forms/ranges.py +++ b/django/contrib/postgres/forms/ranges.py @@ -15,8 +15,10 @@ class BaseRangeField(forms.MultiValueField): } def __init__(self, **kwargs): - kwargs.setdefault('widget', RangeWidget(self.base_field.widget)) - kwargs.setdefault('fields', [self.base_field(required=False), self.base_field(required=False)]) + if 'widget' not in kwargs: + kwargs['widget'] = RangeWidget(self.base_field.widget) + if 'fields' not in kwargs: + kwargs['fields'] = [self.base_field(required=False), self.base_field(required=False)] kwargs.setdefault('required', False) kwargs.setdefault('require_all_fields', False) super(BaseRangeField, self).__init__(**kwargs) diff --git a/django/template/backends/jinja2.py b/django/template/backends/jinja2.py index 2d5190122a1..7cdf4cd1906 100644 --- a/django/template/backends/jinja2.py +++ b/django/template/backends/jinja2.py @@ -26,8 +26,9 @@ class Jinja2(BaseEngine): environment = options.pop('environment', 'jinja2.Environment') environment_cls = import_string(environment) + if 'loader' not in options: + options['loader'] = jinja2.FileSystemLoader(self.template_dirs) options.setdefault('autoescape', True) - options.setdefault('loader', jinja2.FileSystemLoader(self.template_dirs)) options.setdefault('auto_reload', settings.DEBUG) options.setdefault('undefined', jinja2.DebugUndefined if settings.DEBUG else jinja2.Undefined) diff --git a/django/test/client.py b/django/test/client.py index a2eaf6da040..bd926645487 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -146,7 +146,9 @@ def store_rendered_templates(store, signal, sender, template, context, **kwargs) of rendering. """ store.setdefault('templates', []).append(template) - store.setdefault('context', ContextList()).append(copy(context)) + if 'context' not in store: + store['context'] = ContextList() + store['context'].append(copy(context)) def encode_multipart(boundary, data): diff --git a/django/utils/formats.py b/django/utils/formats.py index 9d50f297551..401f1871630 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -94,7 +94,9 @@ def get_format_modules(lang=None, reverse=False): """ if lang is None: lang = get_language() - modules = _format_modules_cache.setdefault(lang, list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH))) + if lang not in _format_modules_cache: + _format_modules_cache[lang] = list(iter_format_modules(lang, settings.FORMAT_MODULE_PATH)) + modules = _format_modules_cache[lang] if reverse: return list(reversed(modules)) return modules