[issue1309567] linecache module returns wrong results
report at bugs.python.org
Mon Nov 17 08:58:27 CET 2008
Rafe Sacks <rafesacks at gmail.com> added the comment:
This seems related to http://bugs.python.org/issue1218234 as well.
This is my first bug report. I hope I do this right.
I posted this to comp.lang.python and was directed here. Here is a
chopped paste of that post:
I am getting an error on line 510 of inspect.py:
504 if iscode(object):
505 if not hasattr(object, 'co_firstlineno'):
506 raise IOError('could not find function definition')
507 lnum = object.co_firstlineno - 1
508 pat = re.compile(r'^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)')
509 while lnum > 0:
510 if pat.match(lines[lnum]): break
511 lnum = lnum - 1
512 return lines, lnum
I finally figured out that there was a caching problem. The function I
passed was changed, but the code lines (strings) retrieved by
linecache.getlines() (on lines 464 and 466) didn't update with the new
module contents... To get around this, I invoke linecache.clearcache()...
lines, n = inspect.getsourcelines(fn)
While inspect uses the cached module to get the text, the line number
used to find the block of source lines is retrieved from the passed
object. So, if I pass a function which reports that it starts on line
50, but in the cache it starts on line 40, an error isn't raised but the
lines of code returned are wrong. The error only occurs when the line
number is higher than the number of lines in the cached module.
Python tracker <report at bugs.python.org>
More information about the Python-bugs-list