Opened 16 years ago

Closed 16 years ago

Last modified 16 years ago

#10014 closed (fixed)

debug template tag fails in utf-8 encoded template

Reported by: ledermann@… Owned by: Malcolm Tredinnick
Component: Template system Version: 1.0
Severity: Keywords: unicode, utf-8, template
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using the {% debug %} template tag inside a UTF-8 encoded template file that contains non-ASCII characters fails with an encoding error. Interestingly, this occurs only if the debug tag is used within a block.

I have attached a minimal test case. If you save that file as UTF-8 and render the template with render_to_response('test.html'), you should be able to see the bug.

Removing the 'ä' character from the template /or/ moving the {% debug %} tag outside the block makes the bug disappear.

Stack trace of the bug:

Caught an exception while rendering: 'ascii' codec can't encode character u'\xe4' in position 14: ordinal not in range(128)

Original Traceback (most recent call last):
  File "C:\lib\Python2.5.2\lib\site-packages\django\template\debug.py", line 71, in render_node
    result = node.render(context)
  File "C:\lib\Python2.5.2\lib\site-packages\django\template\defaulttags.py", line 54, in render
    output = [pformat(val) for val in context]
  File "C:\lib\Python2.5.2\lib\pprint.py", line 59, in pformat
    return PrettyPrinter(indent=indent, width=width, depth=depth).pformat(object)
  File "C:\lib\Python2.5.2\lib\pprint.py", line 111, in pformat
    self._format(object, sio, 0, 0, {}, 0)
  File "C:\lib\Python2.5.2\lib\pprint.py", line 129, in _format
    rep = self._repr(object, context, level - 1)
  File "C:\lib\Python2.5.2\lib\pprint.py", line 195, in _repr
    self._depth, level)
  File "C:\lib\Python2.5.2\lib\pprint.py", line 207, in format
    return _safe_repr(object, context, maxlevels, level)
  File "C:\lib\Python2.5.2\lib\pprint.py", line 251, in _safe_repr
    vrepr, vreadable, vrecur = saferepr(v, context, maxlevels, level)
  File "C:\lib\Python2.5.2\lib\pprint.py", line 292, in _safe_repr
    rep = repr(object)
  File "C:\lib\Python2.5.2\lib\site-packages\django\template\loader_tags.py", line 17, in __repr__
    return "<Block Node: %s. Contents: %r>" % (self.name, self.nodelist)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 14: ordinal not in range(128)

I am using Django 1.0 final.

Attachments (1)

test.html (105 bytes ) - added by ledermann@… 16 years ago.
Test case (Django template)

Download all attachments as: .zip

Change History (4)

by ledermann@…, 16 years ago

Attachment: test.html added

Test case (Django template)

comment:1 by anonymous, 16 years ago

Owner: changed from nobody to Malcolm Tredinnick

comment:2 by Malcolm Tredinnick, 16 years ago

Resolution: fixed
Status: newclosed

(In [9757]) Fixed #10014 -- Don't crash when using debug template tag inside a block node tag.

Returning non-ASCII characters from TextNode.repr was causing
problems in the BlockNode.repr method (and probably in other places
we don't know about yet). We now forcibly convert to ascii and replace any
unconvertible characters, rather than returning some moderately
corrupted data in the non-ASCII case.

comment:3 by Malcolm Tredinnick, 16 years ago

(In [9758]) [1.0.X] Fixed #10014 -- Don't crash when using debug template tag inside a block node tag.

Returning non-ASCII characters from TextNode.repr was causing
problems in the BlockNode.repr method (and probably in other places
we don't know about yet). We now forcibly convert to ascii and replace any
unconvertible characters, rather than returning some moderately
corrupted data in the non-ASCII case.

Backport of r9757 from trunk.

Note: See TracTickets for help on using tickets.
Back to Top