Message 77392 - Python tracker (original) (raw)

We are also using inspect.getsourceline() in a large system at my company and we observed some unreliable behavior in the past which we never had time to analyze till today. Interesting coincidence, but today we finally took the effort and traced the issue, and we found that:

getsourceline() does not report the last line of a class definition body IF: (1) that line is the last line of the file, AND (2) that line is not terminated with a newline (\n)

This also answers Alexandre's question: yes, the problem happens only if the line is unterminated.

I understand that an unterminated line may be non-conforming per Python specs, but my WingIDE happily saved the file, so I suspect other editors may just do the same, and I do agree with Rafe that it would be nice if inspect would be resilient against the missing \n. I assume there would be no harm, and it could eliminate some angst (it is really not obvious first why it fails).

I would really love to see this fixed in a patch release on the 2.5+ lines. Any chance?