A trivial example:
Mako template file:
int main(int argc, char** argv) {
${make_standard_greeting()}
return 0
}
where make_standard_greeting() outputs
cout << "hello ";
cout << "world." << endl;
Then the rendered template reads
int main(int argc, char** argv) {
cout << "hello ";
cout << "world." << endl;
return 0
}
and so the compiler reports the missing semicolon as being on line 4,
even though it was line 3 in the original.
I'd like it to instead render to:
#line 1
int main(int argc, char** argv) {
#line 2
cout << "hello ";
#line 2
cout << "world." << endl;
#line 3
return 0
#line 4
}
Is there any way to hack Mako to do that?
Thanks,
Dan Lepage
you wouldn't have the "#line 2" comment twice and embedded in the output
of make_standard_greeting() like that, just once is all that's needed.
Add a preprocessor to your Template or TemplateLookup that does something
like:
from itertools import chain
def preproc(source):
lines = source.split('\n')
comments = ["#line %d" % i for i in xrange(len(lines))]
return "\n".join(chain(*zip(comments, lines)))
t = Template(source, preprocessor=preproc)
you might want to try checking for a directive or such in the given source
to keep the line number rendering from getting out of control.
Ah, fantastic. Thanks! Is the preprocessor option documented
somewhere? I couldn't find any mention of it.
Also, it would actually be preferable to get #line 2 outputted twice,
since gcc keeps counting up (without it, an error on the second line
emitted by make_standard_greeting would show up as an error on line 3
instead of line 2), but this is already worlds better than what I'd
been doing (which involved writing the mako render to a file, finding
the errors there, and then manually finding the corresponding lines in
the true file).
Thanks!
Dan
I'm also using Mako with PyCUDA for runtime code generation. I have
experienced this same inconvenience and have been resorting to
similarly primitive debugging techniques (like commenting out vast
swaths of code just to figure out what's causing the error), so I'm
glad to hear there's a better solution.
Thanks!
Drew
> --
> You received this message because you are subscribed to the Google Groups "Mako Templates for Python" group.
> To post to this group, send email to mako-d...@googlegroups.com.
> To unsubscribe from this group, send email to mako-discuss...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mako-discuss?hl=en.
>
>