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

exceptions.html_error_template render fail in python3 #293

Closed
hilarryxu opened this issue Apr 30, 2019 · 4 comments
Closed

exceptions.html_error_template render fail in python3 #293

hilarryxu opened this issue Apr 30, 2019 · 4 comments
Labels

Comments

@hilarryxu
Copy link

@hilarryxu hilarryxu commented Apr 30, 2019

When use exceptions.html_error_template in python3, got:

    File "python3.7/site-packages/mako/runtime.py", line 899, in _exec_template
        callable_(context, *args, **kwargs)
    File "memory:0x7efcac2aa908", line 49, in render_body
    TypeError: a bytes-like object is required, not 'str'

caused by

    tback = RichTraceback(error=error, traceback=traceback)
    src = tback.source
    line = tback.lineno
    if src:
        lines = src.split('\n')

here tback.source is bytes in python3, so src.split raise TypeError

@zzzeek
Copy link
Member

@zzzeek zzzeek commented Apr 30, 2019

hi there -

can you please provide a complete MCVE test case? I'm not able to reproduce, although Im not sure it's working correctly nonetheless:


from mako.template import Template
from mako import exceptions

try:
    t = Template("<% print(x) %>")
    print(t.render())
except:
    print(exceptions.html_error_template().render())

I get back HTML source as bytes. however if I call render_unicode(), I get good HTML. No stack trace in either case. Using Python 3.7.0.

@zzzeek zzzeek added the bug label Apr 30, 2019
@hilarryxu
Copy link
Author

@hilarryxu hilarryxu commented May 6, 2019

Like this when use TemplateLookup

from mako.lookup import TemplateLookup
from mako.exceptions import MakoException, html_error_template

try:
    template_lookup = TemplateLookup()
    tpl = template_lookup.get_template('not_found.html')
    tpl.render()
except MakoException:
    print(html_error_template().render())

Using Python 3.7.1 Mako 1.0.9

@sqla-tester
Copy link
Collaborator

@sqla-tester sqla-tester commented May 10, 2019

Mike Bayer has proposed a fix for this issue in the master branch:

Use utf-8 encoding for traceback source with no encoding in py3k https://gerrit.sqlalchemy.org/1261

@hilarryxu
Copy link
Author

@hilarryxu hilarryxu commented May 10, 2019

It works, thank you.

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

Successfully merging a pull request may close this issue.

None yet
3 participants