Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnicodeDecodeError in HTMLFormatter.quote with extended ASCII characters #17

Open
qqrs opened this issue Jun 1, 2016 · 1 comment
Open

Comments

@qqrs
Copy link

qqrs commented Jun 1, 2016

I am unable to see tracebacks for a character encoding error in a Flask app I am debugging. Flask uses weberror to show tracebacks and there appears to be a character encoding issue somewhere along the way.

Either the character encoding issue is in weberror as illustrated by the test case below, or weberror has an implied contract on the encoding it expects and this is not being respected by Flask.

$ pip show weberror

---
Metadata-Version: 2.0
Name: WebError
Version: 0.13.1
from weberror import formatter

hf = formatter.HTMLFormatter()

#s = "<Request 'http://example.com?abc=def\xc5' [GET]>"
s = "\xc5"
hf.quote(s)
Traceback (most recent call last):
  File "weberror_test_case.py", line 7, in <module>
    hf.quote(s)
  File "/home/vagrant/envs/web/local/lib/python2.7/site-packages/weberror/formatter.py", line 296, in quote
    s = s.encode('latin1', 'htmlentityreplace')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)

Quick hack fix weberror/formatter.py:

class HTMLFormatter(TextFormatter):

    def quote(self, s):
        if isinstance(s, str) and hasattr(self, 'frame'):
            s = s.decode(self.frame.source_encoding, 'replace')
        #s = s.encode('latin1', 'htmlentityreplace')
        s = s.decode('latin1').encode('latin1', 'htmlentityreplace')
        return html_quote(s)

Adding the .decode('latin1') works for my purposes in that I'm now able to see tracebacks in Flask.

It's not clear to me what the expected encoding is at this point so I don't know whether this would be a correct fix for the general case, or even whether this error is expected behavior (perhaps Flask should be decoding before passing to weberror).

@digitalresistor
Copy link
Member

Patches accepted. Project is not maintained.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants