[docs] [issue11726] linecache becomes specific to Python scripts in Python 3

Terry J. Reedy report at bugs.python.org
Sat Apr 2 03:08:39 CEST 2011


Terry J. Reedy <tjreedy at udel.edu> added the comment:

The help(linecache) Description is more specific as to the intention (based on traceback usage):

"This is intended to read lines from modules imported -- hence if a filename is not found, it will look down the module search path for a file by that name."

My experiments show that this is too specific. It *can* read any file that it can find and decode as utf-8 (default, or you say, locale encoding or coding in cookie line). 

Find = absolute path
>>> linecache.getline('c:/programs/pydev/py32/LICENSE', 1)
'A. HISTORY OF THE SOFTWARE\n'

or relative path on sys.path
>>> linecache.getline('idlelib/ChangeLog', 1)
'Please refer to the IDLEfork and IDLE CVS repositories for\n'
>>> linecache.getline('idlelib/extend.txt', 1)
'Writing an IDLE extension\n'

Decode fails on byte illegal for utf-8:
>>> linecache.getline('idlelib/CREDITS.txt', 1)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 1566: invalid start byte

(It reads and decodes entire file even though only line 1 was requested. It choked on Löwis. I believe Py3 distributed text files should be utf-8 instead of latin-1.)

If I got rules right, doc should say "Filename must be an absolute path or relative path that can be found on sys.path." and "File must be utf-8 encoded or locale encoded or a Python file with a coding cookie."

(If you tried /etc/passwd, how did it fail?)

----------
nosy: +terry.reedy

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11726>
_______________________________________


More information about the docs mailing list