summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaap Roes <jroes@leukeleu.nl>2015-11-12 14:59:37 +0100
committerTim Graham <timograham@gmail.com>2015-11-12 13:24:53 -0500
commit9a2aca60304dc2e98f9ef45636e129d225cb981f (patch)
tree92ee6ab8f73115927ff8fbb29ccc12cf345275d6
parent91a431f48c1fc5ecc9a837e8071a0062d31b490f (diff)
Fixed #25743 -- Optimized utils.localize() and localize_input()
Bail early if the input is a string since that's the most common case.
-rw-r--r--django/utils/formats.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/django/utils/formats.py b/django/utils/formats.py
index e93d5190c5..d2bdda458e 100644
--- a/django/utils/formats.py
+++ b/django/utils/formats.py
@@ -172,7 +172,9 @@ def localize(value, use_l10n=None):
If use_l10n is provided and is not None, that will force the value to
be localized (or not), overriding the value of settings.USE_L10N.
"""
- if isinstance(value, bool):
+ if isinstance(value, six.string_types): # Handle strings first for performance reasons.
+ return value
+ elif isinstance(value, bool): # Make sure booleans don't get treated as numbers
return mark_safe(six.text_type(value))
elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
return number_format(value, use_l10n=use_l10n)
@@ -182,8 +184,7 @@ def localize(value, use_l10n=None):
return date_format(value, use_l10n=use_l10n)
elif isinstance(value, datetime.time):
return time_format(value, 'TIME_FORMAT', use_l10n=use_l10n)
- else:
- return value
+ return value
def localize_input(value, default=None):
@@ -191,7 +192,9 @@ def localize_input(value, default=None):
Checks if an input value is a localizable type and returns it
formatted with the appropriate formatting string of the current locale.
"""
- if isinstance(value, (decimal.Decimal, float) + six.integer_types):
+ if isinstance(value, six.string_types): # Handle strings first for performance reasons.
+ return value
+ elif isinstance(value, (decimal.Decimal, float) + six.integer_types):
return number_format(value)
elif isinstance(value, datetime.datetime):
value = datetime_safe.new_datetime(value)