[issue1309567] linecache module returns wrong results
Rafe Sacks <[EMAIL PROTECTED]> 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: 504if iscode(object): 505if not hasattr(object, 'co_firstlineno'): 506raise IOError('could not find function definition') 507lnum = object.co_firstlineno - 1 508pat = re.compile(r'^(\s*def\s)|(.*(? 0: 510if pat.match(lines[lnum]): break 511lnum = lnum - 1 512return 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()... 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. Cheers, - Rafe -- nosy: +RafeSacks ___ Python tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue1309567> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4335] inspect.getsourcelines ignores last line in module
New submission from Rafe Sacks <[EMAIL PROTECTED]>: Hi, If the last line of the function is also the last line of the module, it is ignored. To repro: 1) Create module 'repro' def test(): module_line_2 = None module_line_3 = None module_line_4 = None module_line_5 = None 2) Run test: >>> import repro >>> import inspect >>> inspect.getsourcelines(tmp.test) (['def test():\n', 'module_line_2 = None\n', 'module_line_3 = None\n', 'module_line_4 = None\n'], 1) notice no module_line_5. cheers, - Rafe -- components: Library (Lib) messages: 75953 nosy: RafeSacks severity: normal status: open title: inspect.getsourcelines ignores last line in module versions: Python 2.5 ___ Python tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue4335> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue4335] inspect.getsourcelines ignores last line in module
Rafe Sacks <[EMAIL PROTECTED]> added the comment: "I feel this is not a bug because a file that ends with an unterminated line is not a well-formed python program." I used Komodo to make the file. Nothing special. I just made a new python document and used it. If there is a Komodo Bug that is fine, but if this issue can happen (and it has), then the module should be made more robust to protect against it happening, shouldn't it? ___ Python tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue4335> ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com