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

format_exception fails if exception occured in child template of inheritance chain #71

Closed
sqlalchemy-bot opened this issue Jan 24, 2008 · 3 comments
Labels
bug Something isn't working low priority runtime

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Anonymous

Very strange behavior, but:

base.htm

${self.body()}

test.htm

<%inherit file="base.htm" />

## should throw exception
${undefined_var}

Test1.htm stuff

test.py

import os
from mako.lookup import TemplateLookup
from mako.template import Template

l = TemplateLookup(directories=[os.getcwd(),])
t = Template(filename='test1.htm', lookup=l, format_exceptions=True)

print t.render()

As a result there will be 3 blank lines :). Expected: formatted error message.

Representable with 0.1.10 and latest trunk.

Problem is in your way of copying context. After Context._copy() copied instance will share all buffers and even buffer list with source Context. This can make a lot of hard-to-find errors. As this :).

mako.runtime, _exec_template(), 332 line:

context._buffer_stack = [util.StringIO()]

Here you overwrite buffer stack, but only for current context instance. And expect changes on all Contexts :).

c1 = Context()
c2 = Context()

c1._buffer_stack => list #1
c2._buffer_stack => list #1 (!)
c1._buffer_stack[0] => buff #1
c2._buffer_stack[0] => buff #1

c2._buffer_stack[0].write('asd')
c1._buffer_stack[0].getvalue() => 'asd' as expected

c2._buffer_stack = [StringIO]
c2._buffer_stack[0] => buff #2
c1._buffer_stack[0] => buff #1 (!!!)

In patch I had also changed the way buffers are created. At least this will allow multilingual template errors messages in the future ;). Also - use cStringIO ONLY for unicode (I mean use cStringIO if non-unicode encoding supplied).

If you dont want that much changes - to fix error problem you just need to replace
mako.runtime, _exec_template(), 332 line:

context._buffer_stack = [util.StringIO()]

to

while context._buffer_stack: del context._buffer_stack[0]
context._buffer_stack.append(util.StringIO())

Cheers!


Attachments: runtime_buffers.patch

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

sorry for the delay, just noticed this one. will have to review in order to fully understand what's going on with this one.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

OK, sorry we can't change the constructor to Context here as it's public API that it takes a buffer as an argument. The FastEncodingBuffer is good enough to use for formatting exceptions in all cases. The context collection bug is fixed in d19ff8d, and the html_error_template() encoding is used with format_exceptions in 5214f0f. thanks for spotting this.

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working low priority runtime
Projects
None yet
Development

No branches or pull requests

1 participant