Ticket #14290: ticket14290_for_12.diff

File ticket14290_for_12.diff, 6.0 KB (added by Anssi Kääriäinen, 14 years ago)

For 1.2

  • django/template/defaultfilters.py

    diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py
    index 1500a05..3d1bff4 100644
    a b def date(value, arg=None):  
    706706        arg = settings.DATE_FORMAT
    707707    try:
    708708        return formats.date_format(value, arg)
    709     except AttributeError:
     709    except (AttributeError, UnicodeEncodeError):
    710710        try:
    711711            return format(value, arg)
    712712        except AttributeError:
    def time(value, arg=None):  
    722722        arg = settings.TIME_FORMAT
    723723    try:
    724724        return formats.time_format(value, arg)
    725     except AttributeError:
     725    except (AttributeError, UnicodeEncodeError):
    726726        try:
    727727            return dateformat.time_format(value, arg)
    728728        except AttributeError:
  • django/utils/formats.py

    diff --git a/django/utils/formats.py b/django/utils/formats.py
    index 372998f..600c5cc 100644
    a b from django.utils.importlib import import_module  
    77from django.utils.encoding import smart_str
    88from django.utils import dateformat, numberformat, datetime_safe
    99
     10# format_cache is a mapping from (format_type, lang) to the format string.
     11# By using the cache, it is possible to avoid running get_format_modules
     12# repeatedly.
     13_format_cache = {}
     14
    1015def get_format_modules(reverse=False):
    1116    """
    1217    Returns an iterator over the format modules found in the project and Django
    1318    """
     19    lang = get_language()
    1420    modules = []
    15     if not check_for_language(get_language()) or not settings.USE_L10N:
     21    if not check_for_language(lang) or not settings.USE_L10N:
    1622        return modules
    17     locale = to_locale(get_language())
     23    locale = to_locale(lang)
    1824    if settings.FORMAT_MODULE_PATH:
    1925        format_locations = [settings.FORMAT_MODULE_PATH + '.%s']
    2026    else:
    def get_format(format_type):  
    4046    language (locale), defaults to the format in the settings.
    4147    format_type is the name of the format, e.g. 'DATE_FORMAT'
    4248    """
    43     format_type = smart_str(format_type)
     49    global _format_cache
    4450    if settings.USE_L10N:
    45         for module in get_format_modules():
    46             try:
    47                 return getattr(module, format_type)
    48             except AttributeError:
    49                 pass
     51        lang = get_language()
     52        try:
     53            return _format_cache[(format_type, lang)] \
     54                or getattr(settings, format_type)
     55        except KeyError:
     56            for module in get_format_modules():
     57                try:
     58                    val = getattr(module, format_type)
     59                    _format_cache[(format_type, lang)] = val
     60                    return val
     61                except AttributeError:
     62                    pass
     63            _format_cache[(format_type, lang)] = None
    5064    return getattr(settings, format_type)
    5165
    5266def date_format(value, format=None):
  • django/utils/numberformat.py

    diff --git a/django/utils/numberformat.py b/django/utils/numberformat.py
    index 129c27f..d6dbef8 100644
    a b  
    11from django.conf import settings
     2from django.utils.safestring import mark_safe
     3
    24
    35def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''):
    46    """
    def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''):  
    1113    * thousand_sep: Thousand separator symbol (for example ",")
    1214
    1315    """
     16    use_grouping = settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR \
     17                   and grouping
     18    # Make the common case fast:
     19    if isinstance(number, int) and not use_grouping and not decimal_pos:
     20        return mark_safe(unicode(number))
    1421    # sign
    1522    if float(number) < 0:
    1623        sign = '-'
    1724    else:
    1825        sign = ''
    19     # decimal part
    2026    str_number = unicode(number)
    2127    if str_number[0] == '-':
    2228        str_number = str_number[1:]
     29    # decimal part
    2330    if '.' in str_number:
    2431        int_part, dec_part = str_number.split('.')
    2532        if decimal_pos:
    def format(number, decimal_sep, decimal_pos, grouping=0, thousand_sep=''):  
    3037        dec_part = dec_part + ('0' * (decimal_pos - len(dec_part)))
    3138    if dec_part: dec_part = decimal_sep + dec_part
    3239    # grouping
    33     if settings.USE_L10N and settings.USE_THOUSAND_SEPARATOR and grouping:
     40    if use_grouping:
    3441        int_part_gd = ''
    3542        for cnt, digit in enumerate(int_part[::-1]):
    3643            if cnt and not cnt % grouping:
    3744                int_part_gd += thousand_sep
    3845            int_part_gd += digit
    3946        int_part = int_part_gd[::-1]
    40 
    4147    return sign + int_part + dec_part
    4248
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index 6d81726..2a14252 100644
    a b __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',  
    2020# replace the functions with their real counterparts (once we do access the
    2121# settings).
    2222
     23# Cache the real translations provider in a global variable. Accessing settings
     24# is slow in 1.2
     25_trans_provider = None
     26
    2327def delayed_loader(real_name, *args, **kwargs):
    2428    """
    2529    Call the real, underlying function.  We have a level of indirection here so
    2630    that modules can use the translation bits without actually requiring
    2731    Django's settings bits to be configured before import.
    2832    """
    29     from django.conf import settings
    30     if settings.USE_I18N:
    31         from django.utils.translation import trans_real as trans
    32     else:
    33         from django.utils.translation import trans_null as trans
     33    global _trans_provider
     34    if not _trans_provider:
     35        from django.conf import settings
     36        if settings.USE_I18N:
     37            from django.utils.translation import trans_real as trans
     38        else:
     39            from django.utils.translation import trans_null as trans
     40        _trans_provider = trans
    3441
    3542    # Make the originally requested function call on the way out the door.
    36     return getattr(trans, real_name)(*args, **kwargs)
     43    return getattr(_trans_provider, real_name)(*args, **kwargs)
    3744
    3845g = globals()
    3946for name in __all__:
Back to Top