From ae88e73fa630694a152b54df68d53ce535312e45 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 29 Aug 2012 22:40:51 +0200 Subject: Replaced some smart_xxx by force_xxx equivalent smart_str/smart_text should only be used when a potential lazy string should be preserved in the result of the function call. --- django/utils/formats.py | 10 +++++----- django/utils/http.py | 14 +++++++------- django/utils/translation/trans_real.py | 8 ++++---- django/utils/tzinfo.py | 6 +++--- 4 files changed, 19 insertions(+), 19 deletions(-) (limited to 'django/utils') diff --git a/django/utils/formats.py b/django/utils/formats.py index 2e54d792fa..555982eede 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -4,7 +4,7 @@ import datetime from django.conf import settings from django.utils import dateformat, numberformat, datetime_safe from django.utils.importlib import import_module -from django.utils.encoding import smart_str +from django.utils.encoding import force_str from django.utils.functional import lazy from django.utils.safestring import mark_safe from django.utils import six @@ -66,7 +66,7 @@ def get_format(format_type, lang=None, 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. """ - format_type = smart_str(format_type) + format_type = force_str(format_type) if use_l10n or (use_l10n is None and settings.USE_L10N): if lang is None: lang = get_language() @@ -160,14 +160,14 @@ def localize_input(value, default=None): return number_format(value) elif isinstance(value, datetime.datetime): value = datetime_safe.new_datetime(value) - format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0]) + format = force_str(default or get_format('DATETIME_INPUT_FORMATS')[0]) return value.strftime(format) elif isinstance(value, datetime.date): value = datetime_safe.new_date(value) - format = smart_str(default or get_format('DATE_INPUT_FORMATS')[0]) + format = force_str(default or get_format('DATE_INPUT_FORMATS')[0]) return value.strftime(format) elif isinstance(value, datetime.time): - format = smart_str(default or get_format('TIME_INPUT_FORMATS')[0]) + format = force_str(default or get_format('TIME_INPUT_FORMATS')[0]) return value.strftime(format) return value diff --git a/django/utils/http.py b/django/utils/http.py index cdd56bfed1..d3c70f1209 100644 --- a/django/utils/http.py +++ b/django/utils/http.py @@ -15,7 +15,7 @@ except ImportError: # Python 2 from email.utils import formatdate from django.utils.datastructures import MultiValueDict -from django.utils.encoding import force_text, smart_str +from django.utils.encoding import force_str, force_text from django.utils.functional import allow_lazy from django.utils import six @@ -39,7 +39,7 @@ def urlquote(url, safe='/'): can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ - return force_text(urllib_parse.quote(smart_str(url), smart_str(safe))) + return force_text(urllib_parse.quote(force_str(url), force_str(safe))) urlquote = allow_lazy(urlquote, six.text_type) def urlquote_plus(url, safe=''): @@ -49,7 +49,7 @@ def urlquote_plus(url, safe=''): returned string can safely be used as part of an argument to a subsequent iri_to_uri() call without double-quoting occurring. """ - return force_text(urllib_parse.quote_plus(smart_str(url), smart_str(safe))) + return force_text(urllib_parse.quote_plus(force_str(url), force_str(safe))) urlquote_plus = allow_lazy(urlquote_plus, six.text_type) def urlunquote(quoted_url): @@ -57,7 +57,7 @@ def urlunquote(quoted_url): A wrapper for Python's urllib.unquote() function that can operate on the result of django.utils.http.urlquote(). """ - return force_text(urllib_parse.unquote(smart_str(quoted_url))) + return force_text(urllib_parse.unquote(force_str(quoted_url))) urlunquote = allow_lazy(urlunquote, six.text_type) def urlunquote_plus(quoted_url): @@ -65,7 +65,7 @@ def urlunquote_plus(quoted_url): A wrapper for Python's urllib.unquote_plus() function that can operate on the result of django.utils.http.urlquote_plus(). """ - return force_text(urllib_parse.unquote_plus(smart_str(quoted_url))) + return force_text(urllib_parse.unquote_plus(force_str(quoted_url))) urlunquote_plus = allow_lazy(urlunquote_plus, six.text_type) def urlencode(query, doseq=0): @@ -79,8 +79,8 @@ def urlencode(query, doseq=0): elif hasattr(query, 'items'): query = query.items() return urllib_parse.urlencode( - [(smart_str(k), - [smart_str(i) for i in v] if isinstance(v, (list,tuple)) else smart_str(v)) + [(force_str(k), + [force_str(i) for i in v] if isinstance(v, (list,tuple)) else force_str(v)) for k, v in query], doseq) diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 71282ff2b3..9fd33a7ea8 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -9,7 +9,7 @@ import gettext as gettext_module from threading import local from django.utils.importlib import import_module -from django.utils.encoding import smart_str, smart_text +from django.utils.encoding import force_str, force_text from django.utils.safestring import mark_safe, SafeData from django.utils import six from django.utils.six import StringIO @@ -454,7 +454,7 @@ def templatize(src, origin=None): from django.conf import settings from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK) - src = smart_text(src, settings.FILE_CHARSET) + src = force_text(src, settings.FILE_CHARSET) out = StringIO() message_context = None intrans = False @@ -469,7 +469,7 @@ def templatize(src, origin=None): content = ''.join(comment) translators_comment_start = None for lineno, line in enumerate(content.splitlines(True)): - if line.lstrip().startswith(smart_text(TRANSLATOR_COMMENT_MARK)): + if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK): translators_comment_start = lineno for lineno, line in enumerate(content.splitlines(True)): if translators_comment_start is not None and lineno >= translators_comment_start: @@ -584,7 +584,7 @@ def templatize(src, origin=None): out.write(' # %s' % t.contents) else: out.write(blankout(t.contents, 'X')) - return smart_str(out.getvalue()) + return force_str(out.getvalue()) def parse_accept_lang_header(lang_string): """ diff --git a/django/utils/tzinfo.py b/django/utils/tzinfo.py index 208b7e7191..654c99778e 100644 --- a/django/utils/tzinfo.py +++ b/django/utils/tzinfo.py @@ -5,7 +5,7 @@ from __future__ import unicode_literals import time from datetime import timedelta, tzinfo -from django.utils.encoding import smart_text, smart_str, DEFAULT_LOCALE_ENCODING +from django.utils.encoding import force_str, force_text, DEFAULT_LOCALE_ENCODING # Python's doc say: "A tzinfo subclass must have an __init__() method that can # be called with no arguments". FixedOffset and LocalTimezone don't honor this @@ -53,7 +53,7 @@ class LocalTimezone(tzinfo): self._tzname = self.tzname(dt) def __repr__(self): - return smart_str(self._tzname) + return force_str(self._tzname) def __getinitargs__(self): return self.__dt, @@ -72,7 +72,7 @@ class LocalTimezone(tzinfo): def tzname(self, dt): try: - return smart_text(time.tzname[self._isdst(dt)], + return force_text(time.tzname[self._isdst(dt)], DEFAULT_LOCALE_ENCODING) except UnicodeDecodeError: return None -- cgit v1.3