Ticket #2588: r3641_unicode.patch

File r3641_unicode.patch, 7.3 KB (added by Victor Ng <victor.ng@…>, 9 years ago)

patch against django r3641 to fix various unicode problems

  • django/http/__init__.py

     
    172172
    173173    def __str__(self):
    174174        "Full HTTP message, including headers"
     175        # Coerce the output to be in the correct charset
     176        # Note that we are doing this at the last possible moment
     177        # because we assume that all strings internal in django
     178        # are proper unicode strings, or they're the ASCII portion
     179        # of UTF-8 (codepoint 127 or less)
    175180        return '\n'.join(['%s: %s' % (key, value)
    176181            for key, value in self.headers.items()]) \
    177             + '\n\n' + self.content
     182            + '\n\n' + self.content.encode(settings.DEFAULT_CHARSET)
    178183
    179184    def __setitem__(self, header, value):
    180185        self.headers[header] = value
  • django/db/models/fields/__init__.py

     
    290290            return first_choice + list(self.choices)
    291291        rel_model = self.rel.to
    292292        if hasattr(self.rel, 'get_related_field'):
    293             lst = [(getattr(x, self.rel.get_related_field().attname), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
     293            lst = [(getattr(x, self.rel.get_related_field().attname), unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
    294294        else:
    295             lst = [(x._get_pk_val(), str(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
     295            lst = [(x._get_pk_val(), unicode(x)) for x in rel_model._default_manager.complex_filter(self.rel.limit_choices_to)]
    296296        return first_choice + lst
    297297
    298298    def get_choices_default(self):
  • django/forms/__init__.py

     
    158158
    159159    def __str__(self):
    160160        "Renders the field"
    161         return str(self.formfield.render(self.data))
     161        return unicode(self.formfield.render(self.data))
    162162
    163163    def __repr__(self):
    164164        return '<FormFieldWrapper for "%s">' % self.formfield.field_name
  • django/core/urlresolvers.py

     
    7979            test_regex = grouped
    8080        # Note we're using re.match here on purpose because the start of
    8181        # to string needs to match.
    82         if not re.match(test_regex + '$', str(value)): # TODO: Unicode?
     82        if not re.match(test_regex + '$', unicode(value), re.UNICODE):
    8383            raise NoReverseMatch("Value %r didn't match regular expression %r" % (value, test_regex))
    84         return str(value) # TODO: Unicode?
     84        return unicode(value)
    8585
    8686class RegexURLPattern(object):
    8787    def __init__(self, regex, callback, default_args=None):
     
    8989        # callback is either a string like 'foo.views.news.stories.story_detail'
    9090        # which represents the path to a module and a view function name, or a
    9191        # callable object (view).
    92         self.regex = re.compile(regex)
     92        self.regex = re.compile(regex, re.UNICODE)
    9393        if callable(callback):
    9494            self._callback = callback
    9595        else:
     
    143143    def __init__(self, regex, urlconf_name, default_kwargs=None):
    144144        # regex is a string representing a regular expression.
    145145        # urlconf_name is a string representing the module containing urlconfs.
    146         self.regex = re.compile(regex)
     146        self.regex = re.compile(regex, re.UNICODE)
    147147        self.urlconf_name = urlconf_name
    148148        self.callback = None
    149149        self.default_kwargs = default_kwargs or {}
     
    225225        from django.conf import settings
    226226        urlconf = settings.ROOT_URLCONF
    227227    resolver = RegexURLResolver(r'^/', urlconf)
     228
    228229    return resolver.resolve(path)
    229230
    230231def reverse(viewname, urlconf=None, args=None, kwargs=None):
  • django/core/handlers/base.py

     
    5050
    5151    def get_response(self, path, request):
    5252        "Returns an HttpResponse object for the given HttpRequest"
     53        from exceptions import UnicodeDecodeError
    5354        from django.core import exceptions, urlresolvers
    5455        from django.core.mail import mail_admins
    5556        from django.conf import settings
    5657
     58        # URLs are encoded in UTF8 (RFC3986, RFC2718)
     59
     60        try:
     61            path = path.decode('utf8')
     62        except UnicodeDecodeError, ude:
     63            raise exceptions.SuspiciousOperation, "Failed to decode the URL using UTF8.  This is probably a user agent."
     64
    5765        # Apply request middleware
    5866        for middleware_method in self._request_middleware:
    5967            response = middleware_method(request)
  • django/template/__init__.py

     
    688688                bits.append(self.render_node(node, context))
    689689            else:
    690690                bits.append(node)
    691         return ''.join(bits)
     691        return u''.join(bits)
    692692
    693693    def get_nodes_by_type(self, nodetype):
    694694        "Return a list of all nodes of the given type"
     
    734734        return "<Variable Node: %s>" % self.filter_expression
    735735
    736736    def encode_output(self, output):
    737         # Check type so that we don't run str() on a Unicode object
     737        # We only want unicode.  encoding to the default charset (utf8 for
     738        # monkeybean) will result in a string of indeterminate encoding.
     739        # We want to just convert the whole damn document into the right
     740        # encoding just before we write to the output stream.  That happens in
     741        # django.http.__init__
     742        return unicode(output)
     743
     744        '''
    738745        if not isinstance(output, basestring):
    739             return str(output)
     746            return unicode(output)
    740747        elif isinstance(output, unicode):
    741748            return output.encode(settings.DEFAULT_CHARSET)
    742749        else:
    743             return output
     750            return unicode(output)
     751        '''
    744752
    745753    def render(self, context):
    746754        output = self.filter_expression.resolve(context)
  • tests/othertests/urlpatterns_reverse.py

     
    2323    ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', NoReverseMatch, [], {'name': 'adrian'}),
    2424    ('^people/(?P<state>\w\w)/(\w+)/$', NoReverseMatch, ['il'], {'name': 'adrian'}),
    2525    ('^people/(?P<state>\w\w)/(\w+)/$', 'people/il/adrian/', ['adrian'], {'state': 'il'}),
     26    ('^people/(?P<state>\w\w)/(\w+)/$', 'people/il/adrian/', ['adrian'], {'state': 'il'}),
     27    ('^people/(?P<state>\w\w)/(?P<name>\w+)/$', u'people/il/adria\xd0/', [], {'state': u'il', 'name': u'adria\xd0'}),
    2628)
    2729
    2830def run_tests(verbosity=0):
Back to Top