Emitting Line Numbers

53 views
Skip to first unread message

dplepage

unread,
Jan 27, 2010, 4:39:53 PM1/27/10
to Mako Templates for Python
Hi everyone,
I was experimenting with Mako as a preprocessing language for C
code (intending to use it with PyCUDA to customize cuda code on the
fly), and one thing that bugs me about doing this is that the line
numbers reported by the CUDA compiler don't correspond to line numbers
in my actual file because the Mako rendering adds more lines. One way
I could see to fix this would be to have Mako insert '#line %i'
between every pair of lines indicating the line number from the
original file; is there a feature for doing this (or a logical place
to hack one on)?

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

Michael Bayer

unread,
Jan 29, 2010, 3:45:09 PM1/29/10
to mako-d...@googlegroups.com

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.

dplepage

unread,
Feb 1, 2010, 4:34:05 PM2/1/10
to Mako Templates for Python


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

Andrew Wagner

unread,
Feb 2, 2010, 12:45:16 PM2/2/10
to mako-d...@googlegroups.com
Hi 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.
>
>

Reply all
Reply to author
Forward
0 new messages