Message132782
| Author |
terry.reedy |
| Recipients |
docs@python, terry.reedy, vstinner |
| Date |
2011年04月02日.01:08:38 |
| SpamBayes Score |
9.975354e-14 |
| Marked as misclassified |
No |
| Message-id |
<1301706519.79.0.702124743663.issue11726@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
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?) |
|
History
|
|---|
| Date |
User |
Action |
Args |
| 2011年04月02日 01:08:39 | terry.reedy | set | recipients:
+ terry.reedy, vstinner, docs@python |
| 2011年04月02日 01:08:39 | terry.reedy | set | messageid: <1301706519.79.0.702124743663.issue11726@psf.upfronthosting.co.za> |
| 2011年04月02日 01:08:39 | terry.reedy | link | issue11726 messages |
| 2011年04月02日 01:08:38 | terry.reedy | create |
|