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

str() of Chameleon Exception throws UnicodeDecodeError #356

Closed
wlang42 opened this issue May 17, 2022 · 0 comments
Closed

str() of Chameleon Exception throws UnicodeDecodeError #356

wlang42 opened this issue May 17, 2022 · 0 comments

Comments

@wlang42
Copy link
Contributor

wlang42 commented May 17, 2022

In Zope PageTemplates there is a handy method called
"find_bad_templates", which does the obvious: traverse through all
PageTemplates in a Folder and listing those, where the "_cook" method
fails.

However, we encountered a PageTemplate where the traversing stops with
a "UnicodeDecodeError". The reason is that described method catches the
Chameleon Exceptions and saves them as a string. Schematically:

def _cook(self):
    ...
    try:
        ..._cook()
    except:
        etype, e = sys.exc_info()[:2]
        self._v_errors = [
            "Compilation failed",
            "%s.%s: %s" % (etype.__module__, etype.__name__, e)
    #               ^^                                       ^
            ]

If an exception occurs, it is converted to a string. And that can fail,
if the token contains a non-ascii char. We can reproduce it with
Python2 like this:

from chameleon import exc, tokenize
body = u'<p>ein text mit uumlaut\xfc</p>'
string = body[3:-4]
token = tokenize.Token(string, 3, body)
e = exc.LanguageError('Invalid define syntax', token)
str(e)

The last line throws an exception:

Traceback (most recent call last):
  File ...
    str(e)
  File ".../chameleon/src/chameleon/exc.py", line 192, in __str__
    text += "\n" + line
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 47:
  ordinal not in range(128)

I will make a pull request to fix this...

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