I was writing similar tools, and found strange things related to f_lineno.
For multi-line statements, python 2.2.1 sometimes sets f_lineno to the
start line, and sometimes to the current line. This is not related to -O
I reported this to SF bug 607092, but this case is now closed with
"won't fix" status. Anyway, there seems to be an extensive change to
f_lineno in 2.3, so I'm just waiting for it.