Skip to content


Support unicode characters in printable-function #66

wants to merge 1 commit into from

3 participants


repr function returns str, which is not liked by jinja2 if non-ascii characters occur.

@jmagnusson jmagnusson Support unicode characters for printables
`repr` function returns `str`, which is not liked by `jinja2` if non-ascii characters occur.
@mgood mgood closed this pull request from a commit
@mgood `printable` filter should replace non-ascii bytes
In Python 2, when repr() returns bytes, replace any non-ascii bytes
with the unicode ? character to ensure that the result is printable.

Fixes #66
@mgood mgood closed this in 82295aa

Thanks for the patch, but I've committed a different fix that uses Python's encoding error handling to replace any non-ASCII charters with the � character, which is similar to what would happen if you tried to print that value in the Python console.

I also didn't want to depend on importing Flask's internal _compat module, which given the _ prefix is probably not intended as part of the public API.

I would also recommend looking into the __repr__ method that returned this non-ASCII value, since it would be better if it escaped its result instead.

I commonly see people use something like this in __repr__:

return 'Foo(%s)' % self.value

However, it's better to use the %r substitution instead:

return 'Foo(%r)' % self.value

Fair enough. Would it be possible to see a new release with this fix soon? :)

@jmagnusson jmagnusson deleted the jmagnusson:unicode-repr branch

@mgood what about a new release with your fix?


@mgood, any chance to get this released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 1, 2014
  1. @jmagnusson

    Support unicode characters for printables

    jmagnusson committed
    `repr` function returns `str`, which is not liked by `jinja2` if non-ascii characters occur.
This page is out of date. Refresh to see the latest.
Showing with 2 additions and 2 deletions.
  1. +2 −2 flask_debugtoolbar/
4 flask_debugtoolbar/
@@ -1,6 +1,6 @@
import os
-from flask import current_app, request, g
+from flask import current_app, request, g, _compat
from flask.globals import _request_ctx_stack
from flask import send_from_directory
from jinja2 import Environment, PackageLoader
@@ -30,7 +30,7 @@ def replace_insensitive(string, target, replacement):
def _printable(value):
- return repr(value)
+ return _compat.text_type(repr(value))
except Exception as e:
return '<repr(%s) raised %s: %s>' % (
object.__repr__(value), type(e).__name__, e)
Something went wrong with that request. Please try again.